-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
169 lines (147 loc) · 6.39 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Beatmaps.Formats;
using osu.Game.Beatmaps.Legacy;
using osu.Game.Native;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Catch.Mods;
using osu.Game.Rulesets.Mania.Mods;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Rulesets.Taiko.Mods;
// ReSharper disable once CheckNamespace
public unsafe class Program
{
static Program()
{
// This is kinda unfortunate, but I want to avoid mocking these.
// Sigh... Probably aaaaaaaaaalllllll of these should not use Activator.CreateInstance().
_ = Activator.CreateInstance<LegacyControlPointInfo>();
_ = Activator.CreateInstance<TimingControlPoint>();
_ = Activator.CreateInstance<SampleControlPoint>();
_ = Activator.CreateInstance<EffectControlPoint>();
_ = Activator.CreateInstance<DifficultyControlPoint>();
_ = Activator.CreateInstance<TimingControlPoint>();
_ = Activator.CreateInstance<LegacyDecoder<Beatmap>.LegacySampleControlPoint>();
_ = Activator.CreateInstance<OsuModTouchDevice>();
_ = Activator.CreateInstance<OsuModDoubleTime>();
_ = Activator.CreateInstance<OsuModHalfTime>();
_ = Activator.CreateInstance<OsuModEasy>();
_ = Activator.CreateInstance<OsuModHardRock>();
_ = Activator.CreateInstance<OsuModFlashlight>();
_ = Activator.CreateInstance<OsuModHidden>();
_ = Activator.CreateInstance<OsuModRelax>();
_ = Activator.CreateInstance<TaikoModDoubleTime>();
_ = Activator.CreateInstance<TaikoModHalfTime>();
_ = Activator.CreateInstance<TaikoModEasy>();
_ = Activator.CreateInstance<TaikoModHardRock>();
_ = Activator.CreateInstance<CatchModDoubleTime>();
_ = Activator.CreateInstance<CatchModHalfTime>();
_ = Activator.CreateInstance<CatchModEasy>();
_ = Activator.CreateInstance<CatchModHardRock>();
_ = Activator.CreateInstance<ManiaModDoubleTime>();
_ = Activator.CreateInstance<ManiaModHalfTime>();
_ = Activator.CreateInstance<ManiaModEasy>();
_ = Activator.CreateInstance<ManiaModHardRock>();
_ = Activator.CreateInstance<ManiaModKey1>();
_ = Activator.CreateInstance<ManiaModKey2>();
_ = Activator.CreateInstance<ManiaModKey3>();
_ = Activator.CreateInstance<ManiaModKey4>();
_ = Activator.CreateInstance<ManiaModKey5>();
_ = Activator.CreateInstance<ManiaModKey6>();
_ = Activator.CreateInstance<ManiaModKey7>();
_ = Activator.CreateInstance<ManiaModKey8>();
_ = Activator.CreateInstance<ManiaModKey9>();
_ = Activator.CreateInstance<ManiaModDualStages>();
}
private static LogDelegate? logger;
/// <summary>
/// Sets the logger.
/// </summary>
/// <param name="handler">A <see cref="LogDelegate"/> callback to handle the message.</param>
[UnmanagedCallersOnly(EntryPoint = "SetLogger", CallConvs = [typeof(CallConvCdecl)])]
public static ErrorCode SetLogger(IntPtr handler)
{
logger = Marshal.GetDelegateForFunctionPointer<LogDelegate>(handler);
return ErrorCode.Success;
}
/// <summary>
/// Computes difficulty given a beatmap file path.
/// </summary>
/// <param name="filePathPtr">The file path.</param>
/// <param name="rulesetId">The ruleset.</param>
/// <param name="mods">The mods.</param>
/// <param name="starRating">The star rating.</param>
[UnmanagedCallersOnly(EntryPoint = "ComputeDifficulty_FromFile", CallConvs = [typeof(CallConvCdecl)])]
public static ErrorCode ComputeDifficultyFromFile(char* filePathPtr, int rulesetId, uint mods, double* starRating)
{
string? filePath = Marshal.PtrToStringUTF8((IntPtr)filePathPtr);
if (string.IsNullOrEmpty(filePath))
return error(ErrorCode.FileReadError, "Path is empty.");
try
{
return computeDifficulty(File.ReadAllText(filePath), rulesetId, mods, starRating);
}
catch (Exception ex)
{
return error(ErrorCode.Failure, ex.ToString());
}
}
/// <summary>
/// Computes difficulty given beatmap content.
/// </summary>
/// <param name="beatmapTextPtr">The beatmap content.</param>
/// <param name="rulesetId">The ruleset.</param>
/// <param name="mods">The mods.</param>
/// <param name="starRating">The star rating.</param>
[UnmanagedCallersOnly(EntryPoint = "ComputeDifficulty_FromText", CallConvs = [typeof(CallConvCdecl)])]
public static ErrorCode ComputeDifficultyFromText(char* beatmapTextPtr, int rulesetId, uint mods, double* starRating)
{
string? beatmapText = Marshal.PtrToStringUTF8((IntPtr)beatmapTextPtr);
return computeDifficulty(beatmapText, rulesetId, mods, starRating);
}
private static ErrorCode computeDifficulty(string? beatmapText, int rulesetId, uint mods, double* starRating)
{
if (string.IsNullOrEmpty(beatmapText))
return error(ErrorCode.EmptyBeatmap, "Beatmap is empty.");
try
{
WorkingBeatmap workingBeatmap = new StringBackedWorkingBeatmap(beatmapText);
Ruleset ruleset = RulesetHelper.CreateRuleset(rulesetId);
Mod[] rulesetMods = ruleset.ConvertFromLegacyMods((LegacyMods)mods).ToArray();
*starRating = ruleset.CreateDifficultyCalculator(workingBeatmap)
.Calculate(rulesetMods)
.StarRating;
return ErrorCode.Success;
}
catch (Exception ex)
{
return error(ErrorCode.Failure, ex.ToString());
}
}
private static ErrorCode error(ErrorCode code, string description)
{
if (logger != null)
{
IntPtr msgPtr = Marshal.StringToHGlobalUni(description);
logger((char*)msgPtr);
Marshal.FreeHGlobal(msgPtr);
}
return code;
}
public enum ErrorCode : byte
{
Success = 0,
EmptyBeatmap = 1,
FileReadError = 2,
Failure = 255
}
public delegate void LogDelegate(char* message);
}