Skip to content

Commit 89edbbb

Browse files
committed
remove preloader
1 parent 7ce489d commit 89edbbb

File tree

6 files changed

+14
-375
lines changed

6 files changed

+14
-375
lines changed

COSML/COSML.cs

Lines changed: 9 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
using System.Reflection;
99
using System.Text;
1010
using UnityEngine;
11-
using UnityEngine.SceneManagement;
1211
using UObject = UnityEngine.Object;
13-
using USceneManager = UnityEngine.SceneManagement.SceneManager;
1412

1513
namespace 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)

COSML/COSML.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<NoWarn>$(NoWarn);1591;626</NoWarn>
1313
</PropertyGroup>
1414

15-
<Import Project="LocalBuildProperties.props" Condition="Exists('LocalBuildProperties.props')"/>
15+
<Import Project="LocalBuildProperties.props" Condition="Exists('LocalBuildProperties.props')" />
1616

1717
<PropertyGroup>
1818
<Mono Condition="$(OS) == WINDOWS_NT" />
@@ -31,8 +31,8 @@
3131
</ItemGroup>
3232

3333
<ItemGroup>
34-
<EmbeddedResource Include="Resources/InputButton_background.png"/>
35-
<EmbeddedResource Include="Resources/MainMenu_chevron_over_white.png"/>
34+
<EmbeddedResource Include="Resources/InputButton_background.png" />
35+
<EmbeddedResource Include="Resources/MainMenu_chevron_over_white.png" />
3636
</ItemGroup>
3737

3838
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

COSML/Modding/IMod.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using COSML.Log;
22
using System;
3-
using System.Collections;
4-
using System.Collections.Generic;
5-
using UnityEngine;
63

74
namespace COSML.Modding
85
{
@@ -18,23 +15,10 @@ public interface IMod : ILogging
1815
/// <returns></returns>
1916
string GetName();
2017

21-
/// <summary>
22-
/// Returns the objects to preload in order for the mod to work.
23-
/// </summary>
24-
/// <returns>A List of tuples containing scene name, object name</returns>
25-
List<(string, string)> GetPreloadNames();
26-
27-
/// <summary>
28-
/// A list of requested scenes to be preloaded and actions to execute on loading of those scenes
29-
/// </summary>
30-
/// <returns>List of tuples containg scene names and the respective actions.</returns>
31-
(string, Func<IEnumerator>)[] PreloadSceneHooks();
32-
3318
/// <summary>
3419
/// Called after preloading of all mods.
3520
/// </summary>
36-
/// <param name="preloadedObjects">The preloaded objects relevant to this <see cref="Mod" /></param>
37-
void Init(Dictionary<string, Dictionary<string, GameObject>> preloadedObjects);
21+
void Init();
3822

3923
/// <summary>
4024
/// Returns version of Mod

COSML/Modding/ModHooks.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ public class ModHooks
1616
private static readonly string SettingsPath = Path.Combine(Application.persistentDataPath, "COSML.GlobalSettings.json");
1717
private static COSMLConsole _console;
1818

19-
/// <summary>
20-
/// Version of COSML.
21-
/// </summary>
22-
public static string COSMLVersion;
23-
2419
/// <summary>
2520
/// Version of the game.
2621
/// </summary>

0 commit comments

Comments
 (0)