From 18bd089664d1d9720834ffe72750488083ceb22d Mon Sep 17 00:00:00 2001 From: ZeroDegress Date: Fri, 24 Nov 2023 19:25:12 +0800 Subject: [PATCH 1/5] Fix:Hot reloading not working. --- src/Hooks.cs | 4 +++- src/ModData.cs | 2 ++ src/ModLoader.cs | 9 +++++---- src/SFHRZModLoaderPlugin.cs | 23 +---------------------- 4 files changed, 11 insertions(+), 27 deletions(-) diff --git a/src/Hooks.cs b/src/Hooks.cs index 8d2866a..f62366a 100644 --- a/src/Hooks.cs +++ b/src/Hooks.cs @@ -7,6 +7,7 @@ namespace SFHR_ZModLoader { public class Hooks { + private static bool isGameContextLoaded = false; private static GlobalData? globalData; private static ManualLogSource? Logger { get => SFHRZModLoaderPlugin.Logger; } private static EventManager? EventManager { get => SFHRZModLoaderPlugin.EventManager; } @@ -24,8 +25,9 @@ public static void Postfix_GlobalData_Load() type = "GLOBALDATA_LOADED", data = globalData, }); - if (Logger != null) + if (Logger != null && !isGameContextLoaded) { + isGameContextLoaded = true; SFHRZModLoaderPlugin.GameContext = new(globalData, Logger); EventManager?.EmitEvent(new Event { diff --git a/src/ModData.cs b/src/ModData.cs index d94b56c..e16edd6 100644 --- a/src/ModData.cs +++ b/src/ModData.cs @@ -305,7 +305,9 @@ public readonly void PatchToGameContext(GameContext gctx, string? namespaceName) { if(camoData.Texture != null && camoData.Texture.isReadable) { + SFHRZModLoaderPlugin.Logger?.LogInfo($"Hot reloading texture: {camoData.Texture.name}"); ImageConversion.LoadImage(camoData.Texture, self.texture.EncodeToPNG()); + camoData.Texture.name += "+1"; } else { diff --git a/src/ModLoader.cs b/src/ModLoader.cs index 87162fe..22dd8d6 100644 --- a/src/ModLoader.cs +++ b/src/ModLoader.cs @@ -61,7 +61,7 @@ public static ModNamespace LoadFromDirectory(string dir, ModNamespace? ns = null var camoName = Path.GetFileName(item); if (ns?.camoDatas.TryGetValue(item, out var camoData) ?? false) { - camoDatas.Add(camoName, ModCamoData.LoadFromDirectory(item, camoData)); + camoDatas[camoName] = ModCamoData.LoadFromDirectory(item, camoData); } else { @@ -88,7 +88,7 @@ public static ModNamespace LoadFromDirectory(string dir, ModNamespace? ns = null var weaponName = Path.GetFileName(item); if (ns?.weaponDatas.TryGetValue(item, out var weaponData) ?? false) { - weaponDatas.Add(weaponName, ModWeaponData.LoadFromDirectory(item, weaponData)); + weaponDatas[weaponName] = ModWeaponData.LoadFromDirectory(item, weaponData); } else { @@ -152,7 +152,7 @@ public static Mod LoadFromDirectory(string dir, Mod? mod = null) { if(namespaces.TryGetValue(Path.GetFileName(nsdir), out var ns)) { - namespaces.Add(Path.GetFileName(nsdir), ModNamespace.LoadFromDirectory(nsdir, ns)); + namespaces[Path.GetFileName(nsdir)] = ModNamespace.LoadFromDirectory(nsdir, ns); } else { @@ -208,6 +208,7 @@ public void RegisterEvents(EventManager eventManager) logger.LogError("GAMECONTEXT_PATCH data incorrect!"); return; } + LoadMods(); var gctx = (GameContext)ev.data; logger.LogInfo("Game patching..."); PatchToGameContext(gctx); @@ -238,7 +239,7 @@ public void LoadMods() logger.LogInfo($"Loading Mod from directory: {item}..."); if(mods.TryGetValue(metadata.id, out var mod)) { - this.mods.Add(mod.metadata.id, Mod.LoadFromDirectory(item, mod)); + this.mods[mod.metadata.id] = Mod.LoadFromDirectory(item, mod); } else { diff --git a/src/SFHRZModLoaderPlugin.cs b/src/SFHRZModLoaderPlugin.cs index f39c7b3..6ec0021 100644 --- a/src/SFHRZModLoaderPlugin.cs +++ b/src/SFHRZModLoaderPlugin.cs @@ -6,6 +6,7 @@ using HarmonyLib; using Il2CppInterop.Runtime.Injection; using UnityEngine; +using Il2CppInterop.Runtime; namespace SFHR_ZModLoader; @@ -68,28 +69,6 @@ public override void Load() data = GameContext, }); }); - InputMonitor.SetAction(UnityEngine.KeyCode.U, () => { - foreach(var spriteRenderer in Resources.FindObjectsOfTypeAll()) - { - if(spriteRenderer != null) - { - Logger.LogInfo("az"); - var texture = new Texture2D(1, 1); - spriteRenderer.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f); - } - } - - foreach(var mesh in Resources.FindObjectsOfTypeAll()) - { - if(mesh != null) - { - mesh.material = new Material(mesh.material) - { - mainTexture = new Texture2D(1, 1) - }; - } - } - }); Harmony.CreateAndPatchAll(typeof(Hooks)); } From 4101c0fd7edbef698e644c2d652578344d652f32 Mon Sep 17 00:00:00 2001 From: ZeroDegress Date: Fri, 24 Nov 2023 19:27:38 +0800 Subject: [PATCH 2/5] Added some logs. --- src/ModData.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ModData.cs b/src/ModData.cs index e16edd6..9a102d2 100644 --- a/src/ModData.cs +++ b/src/ModData.cs @@ -305,9 +305,8 @@ public readonly void PatchToGameContext(GameContext gctx, string? namespaceName) { if(camoData.Texture != null && camoData.Texture.isReadable) { - SFHRZModLoaderPlugin.Logger?.LogInfo($"Hot reloading texture: {camoData.Texture.name}"); + SFHRZModLoaderPlugin.Logger?.LogInfo($"Hot reloading camo texture: {camoData.Texture.name}"); ImageConversion.LoadImage(camoData.Texture, self.texture.EncodeToPNG()); - camoData.Texture.name += "+1"; } else { @@ -318,6 +317,7 @@ public readonly void PatchToGameContext(GameContext gctx, string? namespaceName) { if(camoData.RedCamo != null && camoData.RedCamo.isReadable) { + SFHRZModLoaderPlugin.Logger?.LogInfo($"Hot reloading camo redCamo: {camoData.Texture.name}"); ImageConversion.LoadImage(camoData.RedCamo, self.redCamo.EncodeToPNG()); } else @@ -329,6 +329,7 @@ public readonly void PatchToGameContext(GameContext gctx, string? namespaceName) { if(camoData.Icon != null && camoData.Icon.isReadable) { + SFHRZModLoaderPlugin.Logger?.LogInfo($"Hot reloading camo icon: {camoData.Texture.name}"); ImageConversion.LoadImage(camoData.Icon, self.icon.EncodeToPNG()); } else From b601e38f7fc4d67ec314bed30b40a954845565c1 Mon Sep 17 00:00:00 2001 From: ZeroDegress Date: Fri, 24 Nov 2023 19:28:44 +0800 Subject: [PATCH 3/5] Version:3.1.1 --- SFHR_ZModLoader.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SFHR_ZModLoader.csproj b/SFHR_ZModLoader.csproj index de557ba..33f78e0 100644 --- a/SFHR_ZModLoader.csproj +++ b/SFHR_ZModLoader.csproj @@ -4,7 +4,7 @@ net6.0 SFHR_ZModLoader An Unofficial Mod loader for Strike Force Heroes Remastered. - 3.1.0 + 3.1.1 true latest From 7828d45453398662a78cd1eb4055bc9b69ce771c Mon Sep 17 00:00:00 2001 From: ZeroDegress Date: Fri, 24 Nov 2023 19:28:52 +0800 Subject: [PATCH 4/5] Update CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed16201..68e833e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [3.1.1] - 2023-11-24 + +### Fixed + +- Fixed "Hot Reloading not working". ## [3.1.0] - 2023-11-23 From c812e97e564b63be97d20373b7c7248f7dc7d810 Mon Sep 17 00:00:00 2001 From: ZeroDegress Date: Fri, 24 Nov 2023 19:28:59 +0800 Subject: [PATCH 5/5] upodate README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a2b2aad..156bbf3 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ 将`Mod`放置在游戏根目录下的`mods`文件夹中即可加载,关于`Mod`的更多信息请参考[Mod开发](#mod开发)。 -在游戏中,按下`P`键可以热重载资源。目前由于游戏框架变动导致功能失效,具体参考[#2](https://github.com/zerodegress/SFHR_ZModLoader/issues/2). +在游戏中,按下`P`键可以热重载资源。请注意:目前热重载仅对已经加载的Mod生效,如果你的Mod在热重载时尚未加载那么贴图可能不会被替换。(也就是说不生效的话重启即可) ## Mod开发