Skip to content

Commit

Permalink
build(platform): copy native libraries base on RuntimeIdentifier
Browse files Browse the repository at this point in the history
Also updates the README with instructions on how to install and configure your own project with ZenKitCS.
  • Loading branch information
lmichaelis committed Nov 12, 2023
1 parent 5faa387 commit 71bb932
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@ A C#-library wrapping the [ZenKit](https://github.com/GothicKit/ZenKit) library
[PiranhaBytes'](https://www.piranha-bytes.com/) early 2000's games [Gothic](https://en.wikipedia.org/wiki/Gothic_(video_game))
and [Gothic II](https://en.wikipedia.org/wiki/Gothic_II).

## Using

You can install `ZenKitCS` from the [NuGet Package Gallery](https://www.nuget.org/packages/ZenKit). Simply add the
following snippet to your `.csproj` file, replacing the version with the approprite version identifier from NuGet.

```
<ItemGroup>
<PackageReference Include="ZenKit" Version="x.x.x" />
</ItemGroup>
```

To build your project then, you will need to add a `RuntimeIdentifiers` property to your `.csproj`. You can simply
use this once and copy it the topmost `<PropertyGroup>` in your `.csproj` like this:

```xml
<PropertyGroup>
<!-- ... -->

<RuntimeIdentifiers>linux-x64;win-x64;osx-x64;android-arm64</RuntimeIdentifiers>

<!-- ... -->
</PropertyGroup>
```

You can now also build your project for those runtimes by supplying the runtime identifier in `dotnet build` using the
`-r` parameter. This is how you would build your project for Android:

```
dotnet build -r android-arm64 -c Release --self-contained
```

## Building

You will need:
Expand Down
8 changes: 8 additions & 0 deletions ZenKit/NativeLoader/NativePathResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@ public override IEnumerable<string> EnumeratePossibleLibraryLoadTargets(string n
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
yield return Path.Combine(AppContext.BaseDirectory, $"{name}.dll");
yield return Path.Combine(AppContext.BaseDirectory, $"runtimes\\win-x64\\native\\{name}.dll");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
{
yield return Path.Combine(AppContext.BaseDirectory, $"lib{name}.so");
yield return Path.Combine(AppContext.BaseDirectory, $"runtimes/linux-x64/native/lib{name}.so");
}
else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
{
yield return Path.Combine(AppContext.BaseDirectory, $"lib{name}.so");
yield return Path.Combine(AppContext.BaseDirectory, $"runtimes/android-arm64/native/lib{name}.so");
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
yield return Path.Combine(AppContext.BaseDirectory, $"lib{name}.dylib");
yield return Path.Combine(AppContext.BaseDirectory, $"runtimes/osx-x64/native/lib{name}.dylib");
}
}
Expand Down
34 changes: 31 additions & 3 deletions ZenKit/ZenKit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<RootNamespace>ZenKit</RootNamespace>
<PackRelease>true</PackRelease>
<PackageId>ZenKit</PackageId>
<Version>0.1.7</Version>
<Version>0.1.9</Version>
<Authors>Luis Michaelis</Authors>
<Company>GothicKit</Company>
<Copyright>© 2023. GothicKit Contributors</Copyright>
Expand All @@ -16,19 +16,47 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/GothicKit/ZenKitCS</RepositoryUrl>
<Description>C#-bindings for ZenKit, the ZenGin asset parser.</Description>

<RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('Android'))">android-arm64</RuntimeIdentifier>
<RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('Linux'))">linux-x64</RuntimeIdentifier>
<RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('macOS'))">osx-x64</RuntimeIdentifier>
<RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('Windows'))">win-x64</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="/"/>
</ItemGroup>

<ItemGroup Label="NativeLibraries">
<ItemGroup Label="Packaging">
<None Include="ZenKit.targets" Pack="true" PackagePath="build" />

<None Include="runtimes\**" Pack="true" PackagePath="runtimes">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<PackageCopyToOutput>true</PackageCopyToOutput>
</None>
</ItemGroup>

<ItemGroup Label="NativeLibraries">
<ContentWithTargetPath Include="runtimes\android-arm64\native\libzenkitcapi.so" Condition="'$(RuntimeIdentifier)' == 'android-arm64'">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>libzenkitcapi.so</TargetPath>
</ContentWithTargetPath>

<ContentWithTargetPath Include="runtimes\linux-x64\native\libzenkitcapi.so" Condition="'$(RuntimeIdentifier)' == 'linux-x64'">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>libzenkitcapi.so</TargetPath>
</ContentWithTargetPath>

<ContentWithTargetPath Include="runtimes\osx-x64\native\libzenkitcapi.dylib" Condition="'$(RuntimeIdentifier)' == 'osx-x64'">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>libzenkitcapi.dylib</TargetPath>
</ContentWithTargetPath>

<ContentWithTargetPath Include="runtimes\win-x64\native\zenkitcapi.dll" Condition="'$(RuntimeIdentifier)' == 'win-x64'">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>zenkitcapi.dll</TargetPath>
</ContentWithTargetPath>
</ItemGroup>

<ItemGroup>
<PackageReference Include="NativeLibraryLoader" Version="1.0.13"/>
Expand Down
16 changes: 11 additions & 5 deletions ZenKit/ZenKit.targets
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Well, I hate this. -->
<PropertyGroup>
<RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('Android'))">android-arm64</RuntimeIdentifier>
<RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('Linux'))">linux-x64</RuntimeIdentifier>
<RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('macOS'))">osx-x64</RuntimeIdentifier>
<RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('Windows'))">win-x64</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
<NativeAndroid Include="$(MSBuildThisFileDirectory)..\runtimes\android-arm64\native\libzenkitcapi.so" />
<NativeLinux Include="$(MSBuildThisFileDirectory)..\runtimes\linux-x64\native\libzenkitcapi.so" />
<NativeOSX Include="$(MSBuildThisFileDirectory)..\runtimes\osx-x64\native\libzenkitcapi.dylib" />
<NativeWindows Include="$(MSBuildThisFileDirectory)..\runtimes\win-x64\native\zenkitcapi.dll" />
</ItemGroup>
<Target Name="CopyNativeLibraries" BeforeTargets="Build">
<Copy SourceFiles="@(NativeAndroid)" DestinationFiles="$(TargetDir)\runtimes\android-arm64\native\libzenkitcapi.so" ContinueOnError="true" />
<Copy SourceFiles="@(NativeLinux)" DestinationFiles="$(TargetDir)\runtimes\linux-x64\native\libzenkitcapi.so" ContinueOnError="true" />
<Copy SourceFiles="@(NativeOSX)" DestinationFiles="$(TargetDir)\runtimes\osx-x64\native\libzenkitcapi.dylib" ContinueOnError="true" />
<Copy SourceFiles="@(NativeWindows)" DestinationFiles="$(TargetDir)\runtimes\win-x64\native\zenkitcapi.dll" ContinueOnError="true" />
<Copy SourceFiles="@(NativeAndroid)" Condition="'$(RuntimeIdentifier)' == 'android-arm64'" DestinationFiles="$(TargetDir)\libzenkitcapi.so" ContinueOnError="true" />
<Copy SourceFiles="@(NativeLinux)" Condition="'$(RuntimeIdentifier)' == 'linux-x64'" DestinationFiles="$(TargetDir)\libzenkitcapi.so" ContinueOnError="true" />
<Copy SourceFiles="@(NativeOSX)" Condition="'$(RuntimeIdentifier)' == 'osx-x64'" DestinationFiles="$(TargetDir)\libzenkitcapi.dylib" ContinueOnError="true" />
<Copy SourceFiles="@(NativeWindows)" Condition="'$(RuntimeIdentifier)' == 'win-x64'" DestinationFiles="$(TargetDir)\zenkitcapi.dll" ContinueOnError="true" />
</Target>
</Project>

0 comments on commit 71bb932

Please sign in to comment.