Skip to content

Commit 1194d8a

Browse files
committed
load bg as cover image
1 parent 3d4ec0c commit 1194d8a

File tree

6 files changed

+113
-55
lines changed

6 files changed

+113
-55
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,12 @@ jobs:
3838
architecture: "x64"
3939

4040
- name: Build
41-
run: dotnet publish -c=Debug
41+
run: dotnet publish -c=Release
4242

4343
- name: Set-tag-env
4444
run: |
4545
"RE_TAG_NAME=$(python .github/scripts/get_version_tag.py)" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
4646
47-
# - name: Rename dll
48-
# run: |
49-
# Copy-Item -Force Server\bin\Debug\net35\ServerEmulatorFinal.dll Server\bin\Debug\net35\ServerEmulator.dll
50-
5147
- name: Publish the build
5248
uses: "marvinpinto/action-automatic-releases@latest"
5349
with:

Server/OsuManiaLoader/DataStructures.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class OsuMania
6060
public int BeatmapSetId;
6161
public string StageBg;
6262
public int BeatDivisor;
63+
public string BackgroundImage;
6364

6465
public int KeyCount;
6566
public int OverallDifficulty;

Server/OsuManiaLoader/Injector.cs

Lines changed: 91 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
using System.Threading;
77
using Aquatrax;
88
using HarmonyLib;
9+
using LitJson;
910
using Server.Emulator.Tools;
1011
using SharpCompress.Archive.Zip;
1112
using UnityEngine;
1213
using Path = System.IO.Path;
1314

1415
namespace Server.OsuManiaLoader;
1516

17+
// Partially stolen from: https://github.com/MoeGrid/InvaxionCustomBeatmap/tree/9c90c9937c5c940b4a5f981aecf6ea20f0ba0ff7/Plugin
1618
// ReSharper disable once InconsistentNaming
1719
public static class SongPatches
1820
{
@@ -31,25 +33,73 @@ public static void classifyMusicInfoList_Patch(ref GlobalConfig __instance)
3133
// ReSharper disable once InconsistentNaming
3234
public static bool GetCoverSprite_Patch(string path, ref Sprite __result)
3335
{
34-
var paths = path.Split(' ');
35-
if (paths.Length <= 1) return true;
36+
var paths = path.Split('_');
3637
var songId = int.Parse(paths[0]);
37-
if (Server.ManiaBeatmapsLoader.BeatmapPacks.Any(pack => pack.PackId + Salt == songId))
38+
var pack = Server.ManiaBeatmapsLoader.BeatmapPacks.FirstOrDefault(pack => pack.PackId + Salt == songId);
39+
if (pack == null) return true;
40+
41+
var bgDir = Path.Combine(Directory.GetCurrentDirectory(), Path.Combine("osu!mania_beatmaps", "Backgrounds!"));
42+
if (!Directory.Exists(bgDir)) Directory.CreateDirectory(bgDir);
43+
var bgPath = Path.Combine(bgDir, $"{pack.PackId}_{pack.PackName}_bg.jpg");
44+
45+
var found = File.Exists(bgPath);
46+
47+
if (!found)
3848
{
39-
// TODO: See if cropping the bg image is worthwhile
40-
__result = new Sprite();
41-
return false;
49+
var backupImg = "";
50+
if (pack.BackgroundImages.Length != 0)
51+
backupImg = pack.BackgroundImages.OrderBy(n => Guid.NewGuid()).ToArray()[0];
52+
53+
var archive = ZipArchive.Open(new MemoryStream(File.ReadAllBytes(pack.PackFile)));
54+
foreach (var entry in archive.Entries)
55+
{
56+
var name = Path.GetFileNameWithoutExtension(entry.FilePath.Replace('\\', '/').Split('/').Last())
57+
.ToLower();
58+
if (entry.IsDirectory || name != "background" && name != "bg") continue;
59+
60+
var bytes = new byte[entry.Size];
61+
var _ = entry.OpenEntryStream().Read(bytes, 0, (int)entry.Size);
62+
63+
File.WriteAllBytes(bgPath, bytes);
64+
found = true;
65+
break;
66+
}
67+
68+
if (!found && backupImg != "")
69+
{
70+
foreach (var entry in archive.Entries)
71+
{
72+
var name = entry.FilePath.Replace('\\', '/').Split('/').Last();
73+
if (name != backupImg) continue;
74+
75+
var bytes = new byte[entry.Size];
76+
var _ = entry.OpenEntryStream().Read(bytes, 0, (int)entry.Size);
77+
78+
File.WriteAllBytes(bgPath, bytes);
79+
found = true;
80+
break;
81+
}
82+
}
4283
}
4384

44-
return true;
85+
if (!found) return true;
86+
87+
var www = new WWW("file:///" + bgPath);
88+
while (!www.isDone && www.error == null)
89+
Thread.Sleep(10);
90+
91+
var texture = www.GetTexture(true);
92+
__result = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(.5f, .5f),
93+
texture.width / 3.333f);
94+
return false;
4595
}
4696

4797
class PlayDataHook
4898
{
4999
public static IEnumerable<CodeInstruction> LoadPlayMusicTranspiler(ILGenerator il,
50100
IEnumerable<CodeInstruction> instructions)
51101
{
52-
List<CodeInstruction> tmp = new List<CodeInstruction>();
102+
var tmp = new List<CodeInstruction>();
53103
foreach (var i in instructions)
54104
{
55105
tmp.Add(i);
@@ -74,51 +124,42 @@ public static AudioClip LoadPlayMusicHook(AudioClip audioClip, string id)
74124

75125
var audioDir = Path.Combine(Directory.GetCurrentDirectory(), Path.Combine("osu!mania_beatmaps", "Audio!"));
76126
if (!Directory.Exists(audioDir)) Directory.CreateDirectory(audioDir);
77-
127+
78128
var songPath = Path.Combine(audioDir, $"{pack.PackId}_{pack.PackName}_{pack.Beatmaps[0].AudioFilename}");
79-
if (File.Exists(songPath))
129+
var audioType = pack.Beatmaps[0].AudioFilename.Split('.').Last().ToLower() switch
80130
{
81-
var www = new WWW("file:///" + songPath);
82-
while (!www.isDone && www.error == null)
83-
Thread.Sleep(10);
84-
85-
return www.GetAudioClip(true, true,
86-
pack.Beatmaps[0].AudioFilename.Split('.').Last().ToLower() switch
87-
{
88-
"mp3" => AudioType.MPEG,
89-
"wav" => AudioType.WAV,
90-
"ogg" => AudioType.OGGVORBIS
91-
}
92-
);
93-
}
131+
"mp3" => AudioType.MPEG,
132+
"wav" => AudioType.WAV,
133+
"ogg" => AudioType.OGGVORBIS
134+
};
94135

95-
var archive = ZipArchive.Open(new MemoryStream(File.ReadAllBytes(pack.PackFile)));
96-
foreach (var entry in archive.Entries)
136+
var found = File.Exists(songPath);
137+
138+
if (!found)
97139
{
98-
if (entry.IsDirectory || !entry.FilePath.EndsWith(pack.Beatmaps[0].AudioFilename)) continue;
99-
var bytes = new byte[entry.Size];
100-
var _ = entry.OpenEntryStream().Read(bytes, 0, (int)entry.Size);
101-
102-
File.WriteAllBytes(songPath, bytes);
103-
104-
var www = new WWW("file:///" + songPath);
105-
while (!www.isDone && www.error == null)
106-
Thread.Sleep(10);
107-
108-
return www.GetAudioClip(true, true,
109-
pack.Beatmaps[0].AudioFilename.Split('.').Last().ToLower() switch
110-
{
111-
"mp3" => AudioType.MPEG,
112-
"wav" => AudioType.WAV,
113-
"ogg" => AudioType.OGGVORBIS
114-
}
115-
);
140+
var archive = ZipArchive.Open(new MemoryStream(File.ReadAllBytes(pack.PackFile)));
141+
foreach (var entry in archive.Entries)
142+
{
143+
if (entry.IsDirectory || !entry.FilePath.EndsWith(pack.Beatmaps[0].AudioFilename)) continue;
144+
var bytes = new byte[entry.Size];
145+
var _ = entry.OpenEntryStream().Read(bytes, 0, (int)entry.Size);
146+
File.WriteAllBytes(songPath, bytes);
147+
148+
found = true;
149+
}
116150
}
117151

118-
return audioClip;
152+
if (!found) return audioClip;
153+
154+
var www = new WWW("file:///" + songPath);
155+
while (!www.isDone && www.error == null)
156+
Thread.Sleep(10);
157+
158+
return www.GetAudioClip(true, true, audioType);
119159
}
120160
}
121161

162+
// ReSharper disable once InconsistentNaming
122163
public static bool ReadOneMusicMap_Patch(string musicMap, int flag, ref MazicData __instance)
123164
{
124165
try
@@ -132,7 +173,7 @@ public static bool ReadOneMusicMap_Patch(string musicMap, int flag, ref MazicDat
132173
{
133174
if (beatmap.Difficulty.KeyMode != strs[1] ||
134175
beatmap.Difficulty.Diff != strs[2]) continue;
135-
176+
136177
__instance.ParseText(0, beatmap.INVAXION);
137178
return false;
138179
}
@@ -143,12 +184,14 @@ public static bool ReadOneMusicMap_Patch(string musicMap, int flag, ref MazicDat
143184
Logger.LogInfo("ERROR " + e.Message);
144185
Logger.LogInfo(e.StackTrace);
145186
}
187+
146188
return true;
147189
}
148190

149191
public static void Inject()
150192
{
151-
var classifyMusicInfoList = AccessTools.Method(typeof(GlobalConfig), nameof(GlobalConfig.classifyMusicInfoList));
193+
var classifyMusicInfoList =
194+
AccessTools.Method(typeof(GlobalConfig), nameof(GlobalConfig.classifyMusicInfoList));
152195
var classifyMusicInfoListPatch = AccessTools.Method(typeof(SongPatches), nameof(classifyMusicInfoList_Patch));
153196
_harmony.Patch(classifyMusicInfoList, prefix: new HarmonyMethod(classifyMusicInfoListPatch));
154197

@@ -157,7 +200,8 @@ public static void Inject()
157200
_harmony.Patch(getCoverSprite, prefix: new HarmonyMethod(getCoverSpritePatch));
158201

159202
var loadPlayMusic = AccessTools.Method(typeof(PlayData), nameof(PlayData.LoadPlayMusic));
160-
var loadPlayMusicTranspiler = AccessTools.Method(typeof(PlayDataHook), nameof(PlayDataHook.LoadPlayMusicTranspiler));
203+
var loadPlayMusicTranspiler =
204+
AccessTools.Method(typeof(PlayDataHook), nameof(PlayDataHook.LoadPlayMusicTranspiler));
161205
_harmony.Patch(loadPlayMusic, transpiler: new HarmonyMethod(loadPlayMusicTranspiler));
162206

163207
var readOneMusicMap = AccessTools.Method(typeof(MazicData), nameof(MazicData.ReadOneMusicMap));

Server/OsuManiaLoader/Loader.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ namespace Server.OsuManiaLoader;
1010

1111
public class OsuManiaBeatmapPack
1212
{
13+
public int PackId;
1314
public string PackName;
1415
public string PackFile;
1516
public List<OsuMania> Beatmaps = new();
16-
public int PackId;
17+
public string[] BackgroundImages;
1718

1819
public int MinBpm;
1920
public int MaxBpm;
@@ -62,7 +63,9 @@ public Loader()
6263

6364
BeatmapPacks.Add(pack);
6465
pack.PackId = pack.Beatmaps.Select(btm => btm.BeatmapSetId).First();
65-
66+
pack.BackgroundImages =
67+
pack.Beatmaps.Select(btm => btm.BackgroundImage).Where(i => i != null).ToArray();
68+
6669
var BPMs = pack.Beatmaps.Select(btm => (int)Stuff.CalculateBpm(btm.TimingPoints[0])).ToArray();
6770
pack.MaxBpm = BPMs.Max();
6871
pack.MinBpm = BPMs.Min();
@@ -97,7 +100,7 @@ public Loader()
97100
new ManiaToInvaxion(beatmap).Convert(out var map, out var audioFill);
98101
beatmap.INVAXION = map;
99102

100-
var diff = (int)beatmap.OverallDifficulty;
103+
var diff = beatmap.OverallDifficulty;
101104
var notes = beatmap.HitObjects.Count;
102105

103106
switch (beatmap.KeyCount)

Server/OsuManiaLoader/ManiaToInvaxion.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Server.OsuManiaLoader;
88

9+
// Stolen from: https://github.com/MoeGrid/InvaxionCustomBeatmap/blob/9c90c9937c5c940b4a5f981aecf6ea20f0ba0ff7/Osu2Invaxion/MapConverter.cs
910
public class ManiaToInvaxion
1011
{
1112
private static readonly int[,] KeyMap = {

Server/OsuManiaLoader/Osu.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ void HeaderGeneral(string line)
205205
}
206206
}
207207

208+
void HeaderEvents(string line)
209+
{
210+
var lineProperty = line.Split(',');
211+
if (lineProperty[0] != "0") return;
212+
213+
if (lineProperty.Length != 5) return;
214+
beatmap.BackgroundImage = lineProperty[2].Replace("\"", "");
215+
}
216+
208217
void HeaderMetadata(string line)
209218
{
210219
var lineProperty = line.Split(':');
@@ -311,6 +320,10 @@ void HeaderMetadata(string line)
311320
break;
312321
}
313322
case "Events":
323+
{
324+
HeaderEvents(line);
325+
break;
326+
}
314327
case "FileFormat":
315328
case "Colours":
316329
{

0 commit comments

Comments
 (0)