Skip to content

Commit

Permalink
0.3.0
Browse files Browse the repository at this point in the history
- Allows mods to set a `Preload` method in their `mod_meta` file which will run immediately when the mod assembly is loaded, before the game itself loads.
  • Loading branch information
xen-42 authored Dec 5, 2023
2 parents baa897e + 8bbb2f1 commit f348f76
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
16 changes: 12 additions & 4 deletions Winch/Core/Initializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommandTerminal;
using System;
using System.Net.Http;
using System.Text.RegularExpressions;
using UnityEngine;
Expand All @@ -15,12 +16,19 @@ internal static void Initialize()
{
WinchCore.Log.Debug("Initializer started.");

InitializeAssetLoader();
try
{
InitializeAssetLoader();

if(WinchConfig.GetProperty("EnableDeveloperConsole", false))
InitializeDevConsole();
if (WinchConfig.GetProperty("EnableDeveloperConsole", false))
InitializeDevConsole();

DredgeEvent.TriggerManagersLoaded();
DredgeEvent.TriggerManagersLoaded();
}
catch (Exception e)
{
WinchCore.Log.Error($"Failed to initialize mods {e}");
}
}

internal static void InitializePostUnityLoad()
Expand Down
23 changes: 23 additions & 0 deletions Winch/Core/ModAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ internal void LoadAssembly()
LoadedAssembly = Assembly.LoadFrom(assemblyPath);

WinchCore.Log.Debug($"Loaded Assembly '{LoadedAssembly.GetName().Name}'.");

if (Metadata.ContainsKey("Preload"))
{
ProcessPreload();
}
}

internal void ExecuteAssembly()
Expand Down Expand Up @@ -130,5 +135,23 @@ private void ProcessEntrypoint()
WinchCore.Log.Debug($"Invoking entrypoint {entrypointType}.{entrypointMethodName}...");
entrypoint.Invoke(null, new object[0]);
}

private void ProcessPreload()
{
string preloadSetting = Metadata["Preload"].ToString();
if (!preloadSetting.Contains("/"))
throw new ArgumentException("Malformed Preload in mod_meta.json");

string preloadTypeName = preloadSetting.Split('/')[0];
string preloadMethodName = preloadSetting.Split('/')[1];

Type entrypointType = LoadedAssembly?.GetType(preloadTypeName) ??
throw new EntryPointNotFoundException($"Could not find type {preloadTypeName} in Mod Assembly");
MethodInfo preloader = entrypointType.GetMethod(preloadMethodName) ??
throw new EntryPointNotFoundException($"Could not find method {preloadTypeName} in type {preloadTypeName} in Mod Assembly");

WinchCore.Log.Debug($"Invoking preloader {entrypointType}.{preloadMethodName}...");
preloader.Invoke(null, new object[0]);
}
}
}
19 changes: 18 additions & 1 deletion Winch/Patches/GameLoadPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
using HarmonyLib;
using System.CodeDom.Compiler;
using System.Threading;
using UnityEngine.Localization.Settings;
using Winch.Core;

namespace Winch.Patches
{

/// <summary>
/// Was on Wait for game managers or whatever before but that gives this error:
///
/*
Failed to initialize mods UnityEngine.UnityException: Internal_CreateGameObject can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
at(wrapper managed-to-native) UnityEngine.GameObject.Internal_CreateGameObject(UnityEngine.GameObject, string)
at UnityEngine.GameObject..ctor()[0x00008] in <fbf5779b092846b08cf20985bc90dcd0>:0
at Winch.Core.Initializer.InitializeAssetLoader() [0x00000] in C:\GitHub\Winch\Winch\Core\Initializer.cs:41
at Winch.Core.Initializer.Initialize() [0x0000f] in C:\GitHub\Winch\Winch\Core\Initializer.cs:21
*/
/// </summary>
[HarmonyPatch(typeof(GameManager))]
[HarmonyPatch(nameof(GameManager.WaitForAllAsyncManagers))]
[HarmonyPatch(nameof(GameManager.Start))]
class GameLoadPatcher
{
static void Postfix()
Expand Down
2 changes: 1 addition & 1 deletion Winch/mod_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"Name": "Winch",
"Author": "Hacktix",
"ModGUID": "hacktix.winch",
"Version": "0.2.4"
"Version": "0.3.0"
}

0 comments on commit f348f76

Please sign in to comment.