88using System . Reflection ;
99using System . Text ;
1010using UnityEngine ;
11- using UnityEngine . SceneManagement ;
1211using UObject = UnityEngine . Object ;
13- using USceneManager = UnityEngine . SceneManagement . SceneManager ;
1412
1513namespace COSML
1614{
@@ -28,7 +26,7 @@ public enum ModLoadState
2826 Loaded = 4 ,
2927 }
3028
31- public const int Version = 1 ;
29+ public const int Version = 2 ;
3230
3331 public static ModLoadState LoadState = ModLoadState . NotStarted ;
3432
@@ -73,13 +71,13 @@ public static IEnumerator LoadModsInit(GameObject coroutineHolder)
7371 {
7472 Logging . InitializeFileStream ( ) ;
7573 }
76- catch ( Exception ex )
74+ catch ( Exception e )
7775 {
7876 // We can still log to the console at least, if that's enabled.
79- Logging . API . Error ( ex ) ;
77+ Debug . Log ( $ "Error while initializing ModLog.txt: { e } " ) ;
8078 }
8179
82- Logging . API . Info ( $ "Mod loader: { ModHooks . COSMLVersion } ") ;
80+ Logging . API . Info ( $ "Mod loader: { Version } ") ;
8381 Logging . API . Info ( "Starting mod loading" ) ;
8482
8583 string managed_path = SystemInfo . operatingSystemFamily switch
@@ -99,15 +97,12 @@ public static IEnumerator LoadModsInit(GameObject coroutineHolder)
9997 }
10098
10199 ModHooks . LoadGlobalSettings ( ) ;
102- Logging . ClearOldModlogs ( ) ;
103100
104101 Logging . API . Debug ( $ "Loading assemblies and constructing mods") ;
105102
103+ Directory . CreateDirectory ( "Mods" ) ;
106104 string mods = Path . Combine ( managed_path , "Mods" ) ;
107- string [ ] files = Directory . GetDirectories ( mods )
108- . Except ( new string [ ] { Path . Combine ( mods , "Disabled" ) } )
109- . SelectMany ( d => Directory . GetFiles ( d , "*.dll" ) )
110- . ToArray ( ) ;
105+ string [ ] files = Directory . GetFiles ( mods , "*.dll" ) ;
111106
112107 Logging . API . Debug ( $ "DLL files: { string . Join ( ",\n " , files ) } ") ;
113108
@@ -224,35 +219,10 @@ Assembly Resolve(object sender, ResolveEventArgs args)
224219 }
225220 }
226221
227- var scenes = new List < string > ( ) ;
228- for ( int i = 0 ; i < USceneManager . sceneCountInBuildSettings ; i ++ )
229- {
230- string scenePath = SceneUtility . GetScenePathByBuildIndex ( i ) ;
231- scenes . Add ( Path . GetFileNameWithoutExtension ( scenePath ) ) ;
232- }
233-
234222 ModInstance [ ] orderedMods = ModInstanceTypeMap . Values
235223 . OrderBy ( x => x . Mod ? . LoadPriority ( ) ?? 0 )
236224 . ToArray ( ) ;
237225
238- // dict<scene name, list<(mod, list<objectNames>)>
239- var toPreload = new Dictionary < string , List < ( ModInstance , List < string > objectNames ) > > ( ) ;
240- // dict<mod, dict<scene, dict<objName, object>>>
241- var preloadedObjects = new Dictionary < ModInstance , Dictionary < string , Dictionary < string , GameObject > > > ( ) ;
242- // scene -> respective hooks
243- var sceneHooks = new Dictionary < string , List < Func < IEnumerator > > > ( ) ;
244-
245- Logging . API . Info ( "Creating mod preloads" ) ;
246-
247- // Setup dict of scene preloads
248- GetPreloads ( orderedMods , scenes , toPreload , sceneHooks ) ;
249-
250- if ( toPreload . Count > 0 || sceneHooks . Count > 0 )
251- {
252- Preloader pld = coroutineHolder . GetComponent < Preloader > ( ) ?? coroutineHolder . AddComponent < Preloader > ( ) ;
253- yield return pld . Preload ( toPreload , preloadedObjects , sceneHooks ) ;
254- }
255-
256226 foreach ( ModInstance mod in orderedMods )
257227 {
258228 if ( mod . Error is not null )
@@ -263,8 +233,7 @@ Assembly Resolve(object sender, ResolveEventArgs args)
263233
264234 try
265235 {
266- preloadedObjects . TryGetValue ( mod , out Dictionary < string , Dictionary < string , GameObject > > preloads ) ;
267- LoadMod ( mod , false , preloads ) ;
236+ LoadMod ( mod , false ) ;
268237 if ( ! ModHooks . GlobalSettings . ModEnabledSettings . TryGetValue ( mod . Name , out var enabled ) )
269238 {
270239 enabled = true ;
@@ -289,97 +258,6 @@ Assembly Resolve(object sender, ResolveEventArgs args)
289258 UObject . Destroy ( coroutineHolder . gameObject ) ;
290259 }
291260
292- private static void GetPreloads
293- (
294- ModInstance [ ] orderedMods ,
295- List < string > scenes ,
296- Dictionary < string , List < ( ModInstance , List < string > objectNames ) > > toPreload ,
297- Dictionary < string , List < Func < IEnumerator > > > sceneHooks
298- )
299- {
300- foreach ( var mod in orderedMods )
301- {
302- if ( mod . Error != null )
303- {
304- continue ;
305- }
306-
307- Logging . API . Debug ( $ "Checking preloads for mod \" { mod . Mod . GetName ( ) } \" ") ;
308-
309- List < ( string , string ) > preloadNames = null ;
310- try
311- {
312- preloadNames = mod . Mod . GetPreloadNames ( ) ;
313- }
314- catch ( Exception ex )
315- {
316- Logging . API . Error ( $ "Error getting preload names for mod { mod . Name } :\n " + ex ) ;
317- }
318-
319- try
320- {
321- foreach ( var ( scene , hook ) in mod . Mod . PreloadSceneHooks ( ) )
322- {
323- if ( ! sceneHooks . TryGetValue ( scene , out var hooks ) )
324- sceneHooks [ scene ] = hooks = new List < Func < IEnumerator > > ( ) ;
325-
326- hooks . Add ( hook ) ;
327- }
328- }
329- catch ( Exception ex )
330- {
331- Logging . API . Error ( $ "Error getting preload hooks for mod { mod . Name } :\n " + ex ) ;
332- }
333-
334- if ( preloadNames == null )
335- continue ;
336-
337- // dict<scene, list<objects>>
338- Dictionary < string , List < string > > modPreloads = new ( ) ;
339-
340- foreach ( ( string scene , string obj ) in preloadNames )
341- {
342- if ( string . IsNullOrEmpty ( scene ) || string . IsNullOrEmpty ( obj ) )
343- {
344- Logging . API . Warn ( $ "Mod `{ mod . Mod . GetName ( ) } ` passed null values to preload") ;
345- continue ;
346- }
347-
348- if ( ! scenes . Contains ( scene ) )
349- {
350- Logging . API . Warn (
351- $ "Mod `{ mod . Mod . GetName ( ) } ` attempted preload from non-existent scene `{ scene } `"
352- ) ;
353- continue ;
354- }
355-
356- if ( ! modPreloads . TryGetValue ( scene , out List < string > objects ) )
357- {
358- objects = new List < string > ( ) ;
359- modPreloads [ scene ] = objects ;
360- }
361-
362- Logging . API . Debug ( $ "Found object `{ scene } .{ obj } `") ;
363-
364- objects . Add ( obj ) ;
365- }
366-
367- foreach ( ( string scene , List < string > objects ) in modPreloads )
368- {
369- if ( ! toPreload . TryGetValue ( scene , out List < ( ModInstance , List < string > ) > scenePreloads ) )
370- {
371- scenePreloads = new List < ( ModInstance , List < string > ) > ( ) ;
372- toPreload [ scene ] = scenePreloads ;
373- }
374-
375- Logging . API . Debug ( $ "`{ mod . Name } ` preloads { objects . Count } objects in the `{ scene } ` scene") ;
376-
377- scenePreloads . Add ( ( mod , objects ) ) ;
378- toPreload [ scene ] = scenePreloads ;
379- }
380- }
381- }
382-
383261 private static string UpdateModText ( )
384262 {
385263 StringBuilder builder = new ( ) ;
@@ -418,16 +296,15 @@ private static string UpdateModText()
418296 internal static void LoadMod
419297 (
420298 ModInstance mod ,
421- bool updateModText = true ,
422- Dictionary < string , Dictionary < string , GameObject > > preloadedObjects = null
299+ bool updateModText = true
423300 )
424301 {
425302 try
426303 {
427304 if ( mod is { Enabled : false , Error : null } )
428305 {
429306 mod . Enabled = true ;
430- mod . Mod . Init ( preloadedObjects ) ;
307+ mod . Mod . Init ( ) ;
431308 }
432309 }
433310 catch ( Exception ex )
0 commit comments