Skip to content

Commit d7d49fa

Browse files
committed
hmm
1 parent 0a45638 commit d7d49fa

File tree

6 files changed

+310
-8
lines changed

6 files changed

+310
-8
lines changed

Server/Emulator/Tools/Extensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ public static void AddAll<K, V>(this IDictionary<K, V> target,
1414
target[_.Key] = _.Value;
1515
}
1616
}
17+
public static class ListExtension
18+
{
19+
public static T Pop<T>(this List<T> list, int index)
20+
{
21+
var r = list[index];
22+
list.RemoveAt(index);
23+
return r;
24+
}
25+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using Server.Emulator.Tools;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace Server.OsuManiaLoader;
7+
8+
9+
public class Difficulty
10+
{
11+
public string HPDrainRate;
12+
public string CircleSize;
13+
public string OverallDifficulty;
14+
public string ApproachRate;
15+
public string SliderMultiplier;
16+
public string SliderTickRate;
17+
}
18+
19+
public class Editor
20+
{
21+
public string DistanceSpacing;
22+
public string BeatDivisor;
23+
public string GridSize;
24+
public string TimelineZoom;
25+
public string Bookmarks;
26+
}
27+
28+
public class General
29+
{
30+
public string AudioFilename;
31+
public string AudioLeadIn;
32+
public string PreviewTime;
33+
public string Countdown;
34+
public string SampleSet;
35+
public string StackLeniency;
36+
public string Mode;
37+
public string LetterboxInBreaks;
38+
public string SpecialStyle;
39+
public string WidescreenStoryboard;
40+
}
41+
42+
public class Metadata
43+
{
44+
public string Title;
45+
public string TitleUnicode;
46+
public string Artist;
47+
public string ArtistUnicode;
48+
public string Creator;
49+
public string Version;
50+
public string Source;
51+
public string Tags;
52+
public string BeatmapID;
53+
public string BeatmapSetID;
54+
}
55+
56+
public class Beatmap
57+
{
58+
public int version;
59+
public General General;
60+
public Editor Editor;
61+
public Metadata Metadata;
62+
public Difficulty Difficulty;
63+
public string[] Events;
64+
public string[] TimingPoints;
65+
public string[] HitObjects;
66+
}
67+
68+
69+
public static class Base36
70+
{
71+
public static char[] alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToArray();
72+
public static string Encode(int number)
73+
{
74+
var output = new List<char>();
75+
76+
if (0 <= number && number < alphabet.Length)
77+
{
78+
output.Insert(0, alphabet[number]);
79+
}
80+
else
81+
{
82+
while (number != 0)
83+
{
84+
number = Math.DivRem(number, alphabet.Length, out int i);
85+
output.Insert(0, alphabet[i]);
86+
}
87+
}
88+
89+
return string.Join("", output.Select(ch => ch.ToString()).ToArray()).PadLeft(2, '0');
90+
}
91+
}
92+
93+
public class BeatmapParser
94+
{
95+
public static Beatmap Parse(string beatmapContent)
96+
{
97+
var lines = beatmapContent.Trim().Split('\n')
98+
.Select(line => line.Trim())
99+
.Where(line => line.Length > 0 && !line.StartsWith("//"))
100+
.ToList();
101+
102+
var version = Convert.ToInt32(lines.Pop(0).Split('v').Last());
103+
var sections = new Dictionary<string, List<string>>();
104+
string curSection = null;
105+
106+
foreach (var line in lines)
107+
{
108+
if (line.StartsWith("[") && line.EndsWith("]"))
109+
{
110+
curSection = line.Substring(1, line.Length - 2);
111+
sections[curSection] = new List<string>();
112+
}
113+
else
114+
{
115+
sections[curSection!].Add(line.Trim());
116+
}
117+
}
118+
119+
var parsedSections = new Dictionary<string, Dictionary<string, string>>();
120+
121+
foreach (var section in sections.Keys)
122+
{
123+
if (new string[] { "HitObjects", "TimingPoints", "Events" }.Contains(section)) continue;
124+
125+
parsedSections[section] = sections[section].ToDictionary(line => line.Split(':')[0].Trim(), line => line.Split(':')[1].Trim());
126+
}
127+
128+
var beatmap = LitJson.JsonMapper.ToObject<Beatmap>(LitJson.JsonMapper.ToJson(parsedSections));
129+
beatmap.version = version;
130+
beatmap.TimingPoints = sections["TimingPoints"].ToArray();
131+
beatmap.HitObjects = sections["HitObjects"].ToArray();
132+
beatmap.Events = sections["Events"].ToArray();
133+
return beatmap;
134+
}
135+
}

Server/OsuManiaLoader/Loader.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using SharpCompress.Archive.Zip;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace Server.OsuManiaLoader;
8+
9+
public class OsuManiaBeatmapPack
10+
{
11+
public string packName;
12+
public string packFile;
13+
public List<Beatmap> Beatmaps = new();
14+
}
15+
16+
public class Loader
17+
{
18+
private static readonly string OszDir = Path.Combine(Directory.GetCurrentDirectory(), "osu!mania_beatmaps");
19+
public List<OsuManiaBeatmapPack> BeatmapPacks = new();
20+
21+
public Loader()
22+
{
23+
var utf8 = new UTF8Encoding();
24+
25+
if (!Directory.Exists(OszDir))
26+
{
27+
Directory.CreateDirectory(OszDir);
28+
}
29+
30+
foreach (var osuFile in Directory.GetFiles(OszDir, "*.osz"))
31+
{
32+
try
33+
{
34+
var pack = new OsuManiaBeatmapPack
35+
{
36+
packName = osuFile.Substring(0, osuFile.Length - 4).Replace("\\", "/").Split('/').Last(),
37+
packFile = osuFile,
38+
};
39+
40+
Logger.LogInfo($"Beatmap {pack.packName} found!");
41+
var archive = ZipArchive.Open(new MemoryStream(File.ReadAllBytes(osuFile)));
42+
43+
foreach (var entry in archive.Entries)
44+
{
45+
if (entry.IsDirectory || !entry.FilePath.EndsWith(".osu")) continue;
46+
var bytes = new byte[entry.Size];
47+
var _ = entry.OpenEntryStream().Read(bytes, 0, (int)entry.Size);
48+
var textContent = utf8.GetString(bytes);
49+
pack.Beatmaps.Add(BeatmapParser.Parse(textContent));
50+
}
51+
52+
if (pack.Beatmaps.Count > 0)
53+
{
54+
BeatmapPacks.Add(pack);
55+
}
56+
}
57+
catch (System.Exception)
58+
{
59+
Logger.LogError($"Failed to load `{osuFile}`");
60+
}
61+
}
62+
}
63+
}

Server/OsuManiaLoader/Logger.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Server.OsuManiaLoader;
2+
3+
public class Logger
4+
{
5+
public static void LogInfo(string message)
6+
{
7+
Server.logger.LogInfo($"[OsuManiaLoader]: {message}");
8+
}
9+
10+
public static void LogError(string message)
11+
{
12+
Server.logger.LogError($"[OsuManiaLoader]: {message}");
13+
}
14+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Collections.Generic;
2+
3+
namespace Server.OsuManiaLoader;
4+
5+
public class OsuManiaToBMS
6+
{
7+
private readonly static Dictionary<int, int> maniaNoteToChannel = new() {
8+
{0, 16},
9+
{1, 11},
10+
{2, 12},
11+
{3, 13},
12+
{4, 14},
13+
{5, 15},
14+
{6, 18},
15+
{7, 19}
16+
};
17+
18+
private readonly static Dictionary<int, int> maniaLnToChannel = new() {
19+
{0, 56},
20+
{1, 51},
21+
{2, 52},
22+
{3, 53},
23+
{4, 54},
24+
{5, 55},
25+
{6, 58},
26+
{7, 59}
27+
};
28+
29+
private static List<string> CreateHeader(Beatmap beatmap)
30+
{
31+
var header = new List<string> {
32+
"", "*---------------------- HEADER FIELD", "",
33+
"#PLAYER 1", $"#GENRE {beatmap.Metadata.Creator}",
34+
$"#TITLE {beatmap.Metadata.TitleUnicode}", $"#SUBTITLE {beatmap.version}",
35+
$"#ARTIST {beatmap.Metadata.ArtistUnicode}", "#BPM {calcuateTheBPM}",
36+
"#DIFFICULTY 5", "#RANK 3", "", "*---------------------- EXPANSION FIELD",
37+
"", "*---------------------- MAIN DATA FIELD", "", ""
38+
};
39+
40+
return header;
41+
}
42+
43+
public static void Convert(Beatmap beatmap)
44+
{
45+
var buffer = CreateHeader(beatmap);
46+
}
47+
}

Server/Server.cs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public class Server : BaseUnityPlugin
1515
public static BepInEx.Logging.ManualLogSource logger;
1616
public static Emulator.Database.Database Database;
1717
public static Emulator.PlaceholderServerData PlaceholderServerData;
18+
public static OsuManiaLoader.Loader ManiaBeatmapsLoader;
1819
public static List<string> MustImplement = new();
19-
public static bool Debug = false;
20+
public static bool Debug = true;
2021
private static Process _process;
2122
private static bool ShuttingDown = false;
2223

@@ -32,27 +33,62 @@ private void Awake()
3233
var currentVersion = new AutoUpdater.Tag(PluginInfo.PLUGIN_VERSION);
3334

3435
var releasesApiLink = "https://api.github.com/repos/Invaxion-Server-Emulator/invaxion-server-emulator/releases";
35-
var headers = new Dictionary<string, string> { {"Accept", "application/vnd.github.v3+json"} };
36+
var headers = new Dictionary<string, string> { { "Accept", "application/vnd.github.v3+json" } };
3637
var checkUpdateReq = new Networking.Request(releasesApiLink, headers);
3738
StartCoroutine(checkUpdateReq.Download(((request, success) =>
3839
{
3940
if (success)
4041
{
42+
AutoUpdater.GithubReleases.Release NewVersion = null;
4143
var releases = LitJson.JsonMapper.ToObject<AutoUpdater.GithubReleases.Release[]>(request._www.text);
4244
foreach (var release in releases)
4345
{
4446
var releaseVersion = new AutoUpdater.Tag(release.tag_name);
4547
if (releaseVersion > currentVersion)
4648
{
47-
logger.LogInfo($"A new version is available on GitHub! (v{releaseVersion.ToString()})");
49+
NewVersion = release;
4850
}
4951
}
52+
if (NewVersion != null)
53+
{
54+
var releaseVersion = new AutoUpdater.Tag(NewVersion.tag_name);
55+
logger.LogInfo($"A new version is available on GitHub! (v{releaseVersion})");
56+
void ShowUpdateDialog()
57+
{
58+
if (DiscordRichPresence.GameState.CurrentScene == "MenuScene")
59+
{
60+
Emulator.Tools.Run.After(1f, () =>
61+
{
62+
Aquatrax.TipHelper.Instance.InitTipMode(Aquatrax.TipHelper.TipsMode.two);
63+
Aquatrax.TipHelper.Instance.InitText($"An update for ServerEmulator has been found ({currentVersion} -> {releaseVersion}), update now?");
64+
Aquatrax.TipHelper.Instance.Commit = delegate ()
65+
{
66+
// TODO: Download the update and install it.
67+
Logger.LogInfo("Clicked yes!");
68+
};
69+
Aquatrax.TipHelper.Instance.Cancel = delegate ()
70+
{
71+
Logger.LogInfo("Clicked no!");
72+
};
73+
});
74+
DiscordRichPresence.GameEvents.switchScene -= ShowUpdateDialog;
75+
}
76+
}
77+
DiscordRichPresence.GameEvents.switchScene += ShowUpdateDialog;
78+
}
5079
}
5180
})));
52-
53-
DiscordRichPresence.Data.Init();
5481

55-
if (File.Exists("BepInEx/watch_logs.py") && Debug)
82+
ManiaBeatmapsLoader = new OsuManiaLoader.Loader();
83+
if (ManiaBeatmapsLoader.BeatmapPacks.Count > 0) Logger.LogInfo($"Loaded {ManiaBeatmapsLoader.BeatmapPacks.Count} osu!mania packs!");
84+
85+
if (File.Exists(Path.Combine(Path.Combine("INVAXION_Data", "Plugins"), "discord_game_sdk.dll")))
86+
{
87+
DiscordRichPresence.Data.Init();
88+
DiscordRichPresence.GameEventHooks.Hook();
89+
}
90+
91+
if (Debug && File.Exists("BepInEx/watch_logs.py"))
5692
{
5793
_process = new Process()
5894
{
@@ -65,8 +101,6 @@ private void Awake()
65101
};
66102
_process.Start();
67103
}
68-
69-
DiscordRichPresence.GameEventHooks.Hook();
70104
}
71105

72106
private static long timeDelta = TimeHelper.getCurUnixTimeOfSec();

0 commit comments

Comments
 (0)