66using System . Threading ;
77using Aquatrax ;
88using HarmonyLib ;
9+ using LitJson ;
910using Server . Emulator . Tools ;
1011using SharpCompress . Archive . Zip ;
1112using UnityEngine ;
1213using Path = System . IO . Path ;
1314
1415namespace Server . OsuManiaLoader ;
1516
17+ // Partially stolen from: https://github.com/MoeGrid/InvaxionCustomBeatmap/tree/9c90c9937c5c940b4a5f981aecf6ea20f0ba0ff7/Plugin
1618// ReSharper disable once InconsistentNaming
1719public 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 ) ) ;
0 commit comments