diff options
| author | Joel Stålnacke <joel@saker.fi> | 2024-10-11 13:31:44 +0300 |
|---|---|---|
| committer | Joel Stålnacke <joel@saker.fi> | 2024-10-13 13:34:46 +0300 |
| commit | 53a1cdf5bee2955995dfbf441f5354d1dcfc1e0c (patch) | |
| tree | be8c2894226a2b7e1a47f7583f2041df75f795b3 /Djup.Native | |
| parent | 4bac6ae2e725a1997674fd3369bf4ea032235d8b (diff) | |
Add Godot client
Diffstat (limited to 'Djup.Native')
| -rw-r--r-- | Djup.Native/.gitignore | 2 | ||||
| -rw-r--r-- | Djup.Native/AddRuntimeTargetsToDepsJson.targets | 12 | ||||
| -rw-r--r-- | Djup.Native/Djup.Native.csproj | 39 | ||||
| -rw-r--r-- | Djup.Native/LibDjup.cs | 36 | ||||
| -rw-r--r-- | Djup.Native/Program.cs | 3 | ||||
| -rw-r--r-- | Djup.Native/Types.cs | 41 | ||||
| -rwxr-xr-x | Djup.Native/patch-deps.sh | 4 |
7 files changed, 137 insertions, 0 deletions
diff --git a/Djup.Native/.gitignore b/Djup.Native/.gitignore new file mode 100644 index 0000000..c6e49ef --- /dev/null +++ b/Djup.Native/.gitignore @@ -0,0 +1,2 @@ +obj/ +bin/ diff --git a/Djup.Native/AddRuntimeTargetsToDepsJson.targets b/Djup.Native/AddRuntimeTargetsToDepsJson.targets new file mode 100644 index 0000000..fbb4c43 --- /dev/null +++ b/Djup.Native/AddRuntimeTargetsToDepsJson.targets @@ -0,0 +1,12 @@ +<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <!-- + The .NET SDK does not support project-to-project dependencies + with native binary dependencies, so we need to add them manually. + Patch Project.deps.json by adding the required depencies to + Djup.Native "runtimeTargets" section. + --> + <!-- https://github.com/dotnet/sdk/issues/19929 --> + <Target Name="AddRuntimeTargetsToDepsJson" AfterTargets="AfterBuild"> + <Exec Command="$(MSBuildThisFileDirectory)patch-deps.sh '$(OutputPath)'" /> + </Target> +</Project> diff --git a/Djup.Native/Djup.Native.csproj b/Djup.Native/Djup.Native.csproj new file mode 100644 index 0000000..107e293 --- /dev/null +++ b/Djup.Native/Djup.Native.csproj @@ -0,0 +1,39 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>net8.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + <AllowUnsafeBlocks>true</AllowUnsafeBlocks> + </PropertyGroup> + + <PropertyGroup> + <BuildDir>$(MSBuildThisFileDirectory)../lib/build</BuildDir> + </PropertyGroup> + + <ItemGroup Condition=" '$(Configuration)'=='Release' "> + <Content Include="$(BuildDir)/linux-x64/libdjup.so"> + <TargetPath>runtimes/linux-x64/native/libdjup.so</TargetPath> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + <PackagePath>runtimes/linux-x64/native/</PackagePath> + <Pack>true</Pack> + </Content> + </ItemGroup> + + <Choose> + <When Condition=" '$(Configuration)'=='Debug'"> + <PropertyGroup> + <Arch>$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)</Arch> + <IsLinux Condition="$([MSBuild]::IsOsPlatform('Linux'))">true</IsLinux> + </PropertyGroup> + + <ItemGroup Condition=" $(IsLinux) And '$(Arch)' == X64 "> + <Content Include="$(BuildDir)/linux-x64/libdjup.so"> + <TargetPath>runtimes/linux-x64/native/libdjup.so</TargetPath> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </Content> + </ItemGroup> + </When> + </Choose> + +</Project> diff --git a/Djup.Native/LibDjup.cs b/Djup.Native/LibDjup.cs new file mode 100644 index 0000000..fece18a --- /dev/null +++ b/Djup.Native/LibDjup.cs @@ -0,0 +1,36 @@ +using System.Runtime.InteropServices; + +namespace Djup.Native; + +public static partial class LibDjup +{ + const string LibraryName = "djup"; + const string Prefix = "dp_"; + + [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(hello_world))] + public static partial void hello_world(); + + [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(get_num))] + public static partial int get_num(); + + [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(get_vec2))] + public static partial Vec2 get_vec2(); + + [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(world_create))] + public static partial IntPtr world_create(); + + [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(world_free))] + public static partial void world_free(IntPtr world); + + [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(world_create_entity))] + public static partial int world_create_entity(IntPtr world, EntityKind kind); + + [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(world_find_entity))] + public static unsafe partial Entity* world_find_entity(IntPtr world, int entityId); + + [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(world_remove_entity))] + public static partial void world_remove_entity(IntPtr world, int entityId); + + [LibraryImport(LibraryName, EntryPoint = Prefix + nameof(world_tick))] + public static partial int world_tick(IntPtr world, double delta); +} diff --git a/Djup.Native/Program.cs b/Djup.Native/Program.cs new file mode 100644 index 0000000..1b374b8 --- /dev/null +++ b/Djup.Native/Program.cs @@ -0,0 +1,3 @@ +/*using Djup.Native;*/ +/**/ +/*LibDjup.hello_world();*/ diff --git a/Djup.Native/Types.cs b/Djup.Native/Types.cs new file mode 100644 index 0000000..c930bd6 --- /dev/null +++ b/Djup.Native/Types.cs @@ -0,0 +1,41 @@ +using System.Runtime.InteropServices; + +namespace Djup.Native; + +[StructLayout(LayoutKind.Sequential)] +public struct Vec2 +{ + public float X { get; } + public float Y { get; } + + public Vec2(float x, float y) + { + X = x; + Y = y; + } + + public override string ToString() => $"({X}, {Y})"; +} + +public enum EntityKind +{ + None = 0, + Ball +} + +[StructLayout(LayoutKind.Explicit)] +public struct Entity +{ + [FieldOffset(0)] + public EntityKind kind; + + [FieldOffset(4)] + public Ball ball; +} + +[StructLayout(LayoutKind.Sequential)] +public struct Ball +{ + public Vec2 Position { get; set; } + public Vec2 Velocity { get; set; } +} diff --git a/Djup.Native/patch-deps.sh b/Djup.Native/patch-deps.sh new file mode 100755 index 0000000..d1f3bbf --- /dev/null +++ b/Djup.Native/patch-deps.sh @@ -0,0 +1,4 @@ +#!/bin/sh +depsjson="$(find "$1" -name '*.deps.json')" +patched="$(jq '.targets.".NETCoreApp,Version=v8.0"."Djup.Native/1.0.0" += {"runtimeTargets": {"runtimes/linux-x64/native/libdjup.so": {"rid": "linux-x64", "assetType": "native"} }}' "$depsjson")" +echo -n "$patched" > "$depsjson" |
