Skip to content

Commit

Permalink
wip: setup c# rust ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine committed Mar 25, 2024
1 parent eb4aace commit 092b178
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
bin/
obj/
target/
/packages/
/_ReSharper.Caches/
*.idea
Expand Down
2 changes: 2 additions & 0 deletions Osu.Patcher.Hook/Hook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Performance.ROsu;
using Osu.Utils;

namespace Osu.Patcher.Hook;
Expand All @@ -22,6 +23,7 @@ public static int Initialize(string _)

try
{
Console.WriteLine(Test.test());
_harmony = new Harmony("osu!patcher");
InitializePatches(_harmony);

Expand Down
3 changes: 3 additions & 0 deletions Osu.Patcher.Hook/Osu.Patcher.Hook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="all"/>
<ProjectReference Include="../Osu.Performance/Osu.Performance.csproj">
<SetTargetFramework>TargetFramework=net452</SetTargetFramework>
</ProjectReference>
<ProjectReference Include="../Osu.Stubs/Osu.Stubs.csproj">
<SetTargetFramework>TargetFramework=net452</SetTargetFramework>
</ProjectReference>
Expand Down
2 changes: 1 addition & 1 deletion Osu.Patcher.Injector/Osu.Patcher.Injector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ApplicationVersion>1.0.0</ApplicationVersion>
<AssemblyName>osu!.patcher</AssemblyName>
<RootNamespace>Osu.Patcher.Injector</RootNamespace>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn);NU1702</NoWarn> <!-- ProjectReferences used here are expected to not match target framework -->
Expand Down
3 changes: 3 additions & 0 deletions Osu.Performance.ROsu/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build]
# osu! is a 32 bit process and we only support windows
target = "i686-pc-windows-msvc"
16 changes: 16 additions & 0 deletions Osu.Performance.ROsu/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Osu.Performance.ROsu/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "rosu"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["dylib"]

[dependencies]
rosu-pp = "0.10.0"
32 changes: 32 additions & 0 deletions Osu.Performance.ROsu/Osu.Performance.ROsu.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="Build">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net452</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<DebugType>none</DebugType>
</PropertyGroup>

<ItemGroup>
<CompileNative Include="src\**.rs"/>
</ItemGroup>
<PropertyGroup>
<NativeTarget>i686-pc-windows-msvc</NativeTarget>
</PropertyGroup>
<Target Name="BuildNative"
BeforeTargets="BeforeBuild"
Inputs="@(CompileNative);cargo.toml;cargo.lock"
Outputs=".\target\$(NativeTarget)\$(Configuration.ToLower())\rosu.dll">
<Exec Command="cargo build --color=never --message-format=human --target $(NativeTarget)" Condition="'$(Configuration)' == 'Debug'"/>
<Exec Command="cargo build --color=never --message-format=human --target $(NativeTarget) --release" Condition="'$(Configuration)' == 'Release'"/>
<ItemGroup>
<ROsuOutput Include=".\target\$(NativeTarget)\$(Configuration.ToLower())\rosu.dll"/>
<ROsuOutput Include=".\target\$(NativeTarget)\$(Configuration.ToLower())\rosu.pdb" Condition="'$(Configuration)' == 'Debug'"/>
<Content Include="@(ROsuOutput)">
<TargetPath>%(Filename)%(Extension)</TargetPath>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Target>
</Project>
11 changes: 11 additions & 0 deletions Osu.Performance.ROsu/Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Runtime.InteropServices;

namespace Osu.Performance.ROsu
{
public class Test
{
[DllImport("rosu.dll")]
public static extern int test();
}
}
56 changes: 56 additions & 0 deletions Osu.Performance.ROsu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use rosu_pp::{Beatmap, OsuPP};
use rosu_pp::osu::OsuDifficultyAttributes;

#[repr(C)]
struct _OsuDifficultyAttributes {
aim: f64,
speed: f64,
flashlight: f64,
slider_factor: f64,
speed_note_count: f64,
ar: f64,
od: f64,
hp: f64,
circles: usize,
sliders: usize,
spinners: usize,
stars: f64,
max_combo: usize,
}

impl From<&_OsuDifficultyAttributes> for OsuDifficultyAttributes {
fn from(value: &_OsuDifficultyAttributes) -> Self {
OsuDifficultyAttributes {
aim: value.aim,
speed: value.speed,
flashlight: value.flashlight,
slider_factor: value.slider_factor,
speed_note_count: value.speed_note_count,
ar: value.ar,
od: value.od,
hp: value.hp,
n_circles: value.circles,
n_sliders: value.sliders,
n_spinners: value.spinners,
stars: value.stars,
max_combo: value.max_combo,
}
}
}

#[no_mangle]
extern "C" fn test() -> i32 {
727
}

#[no_mangle]
extern "C" fn calculate_osu(difficulty: &_OsuDifficultyAttributes) -> f64 {
let diff: OsuDifficultyAttributes = difficulty.into();

return OsuPP::new(&Beatmap::default())
// https://github.com/MaxOhn/rosu-pp/issues/28#issuecomment-1871814571
.passed_objects(diff.n_circles + diff.n_sliders + diff.n_spinners)
.attributes(diff)
.calculate()
.pp();
}
33 changes: 33 additions & 0 deletions Osu.Performance/Osu.Performance.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="Build">
<PropertyGroup>
<OutputType>Library</OutputType>
<RootNamespace>Osu.Performance</RootNamespace>
<AssemblyName>osu!.performance</AssemblyName>
<TargetFramework>net452</TargetFramework>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="all"/>
<ProjectReference Include="../Osu.Performance.ROsu/Osu.Performance.ROsu.csproj">
<SetTargetFramework>TargetFramework=net452</SetTargetFramework>
</ProjectReference>
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions osu-patcher.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Osu.Stubs", "Osu.Stubs\Osu.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Osu.Utils", "Osu.Utils\Osu.Utils.csproj", "{9FA3B15D-8421-4E04-BA84-85331845A01D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Osu.Performance", "Osu.Performance\Osu.Performance.csproj", "{79B81600-142A-4DF7-B1B1-09D18ABE4537}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Osu.Performance.ROsu", "Osu.Performance.ROsu\Osu.Performance.ROsu.csproj", "{E97D5D8F-81D5-4E37-8BE8-5B1DCEDE9A14}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Expand All @@ -29,5 +33,13 @@ Global
{9FA3B15D-8421-4E04-BA84-85331845A01D}.Debug|x86.Build.0 = Debug|Any CPU
{9FA3B15D-8421-4E04-BA84-85331845A01D}.Release|x86.ActiveCfg = Release|Any CPU
{9FA3B15D-8421-4E04-BA84-85331845A01D}.Release|x86.Build.0 = Release|Any CPU
{79B81600-142A-4DF7-B1B1-09D18ABE4537}.Debug|x86.ActiveCfg = Debug|Any CPU
{79B81600-142A-4DF7-B1B1-09D18ABE4537}.Debug|x86.Build.0 = Debug|Any CPU
{79B81600-142A-4DF7-B1B1-09D18ABE4537}.Release|x86.ActiveCfg = Release|Any CPU
{79B81600-142A-4DF7-B1B1-09D18ABE4537}.Release|x86.Build.0 = Release|Any CPU
{E97D5D8F-81D5-4E37-8BE8-5B1DCEDE9A14}.Debug|x86.ActiveCfg = Debug|Any CPU
{E97D5D8F-81D5-4E37-8BE8-5B1DCEDE9A14}.Debug|x86.Build.0 = Debug|Any CPU
{E97D5D8F-81D5-4E37-8BE8-5B1DCEDE9A14}.Release|x86.ActiveCfg = Release|Any CPU
{E97D5D8F-81D5-4E37-8BE8-5B1DCEDE9A14}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 092b178

Please sign in to comment.