Skip to content

Commit

Permalink
Merge pull request #3 from zerodegress/fix-hotreloading-not-working
Browse files Browse the repository at this point in the history
Fixed "Hot reloading not working"
  • Loading branch information
zerodegress authored Nov 24, 2023
2 parents 59116de + c812e97 commit 27a58c6
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 29 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

`Mod`放置在游戏根目录下的`mods`文件夹中即可加载,关于`Mod`的更多信息请参考[Mod开发](#mod开发)

<del>在游戏中,按下`P`键可以热重载资源。</del>目前由于游戏框架变动导致功能失效,具体参考[#2](https://github.com/zerodegress/SFHR_ZModLoader/issues/2).
在游戏中,按下`P`键可以热重载资源。请注意:目前热重载仅对已经加载的Mod生效,如果你的Mod在热重载时尚未加载那么贴图可能不会被替换。(也就是说不生效的话重启即可)

## Mod开发

Expand Down
2 changes: 1 addition & 1 deletion SFHR_ZModLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<AssemblyName>SFHR_ZModLoader</AssemblyName>
<Description>An Unofficial Mod loader for Strike Force Heroes Remastered.</Description>
<Version>3.1.0</Version>
<Version>3.1.1</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
<RestoreAdditionalProjectSources>
Expand Down
4 changes: 3 additions & 1 deletion src/Hooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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
{
Expand Down
3 changes: 3 additions & 0 deletions src/ModData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ public readonly void PatchToGameContext(GameContext gctx, string? namespaceName)
{
if(camoData.Texture != null && camoData.Texture.isReadable)
{
SFHRZModLoaderPlugin.Logger?.LogInfo($"Hot reloading camo texture: {camoData.Texture.name}");
ImageConversion.LoadImage(camoData.Texture, self.texture.EncodeToPNG());
}
else
Expand All @@ -316,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
Expand All @@ -327,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
Expand Down
9 changes: 5 additions & 4 deletions src/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
{
Expand Down
23 changes: 1 addition & 22 deletions src/SFHRZModLoaderPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using UnityEngine;
using Il2CppInterop.Runtime;

namespace SFHR_ZModLoader;

Expand Down Expand Up @@ -68,28 +69,6 @@ public override void Load()
data = GameContext,
});
});
InputMonitor.SetAction(UnityEngine.KeyCode.U, () => {
foreach(var spriteRenderer in Resources.FindObjectsOfTypeAll<SpriteRenderer>())
{
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<MeshRenderer>())
{
if(mesh != null)
{
mesh.material = new Material(mesh.material)
{
mainTexture = new Texture2D(1, 1)
};
}
}
});

Harmony.CreateAndPatchAll(typeof(Hooks));
}
Expand Down

0 comments on commit 27a58c6

Please sign in to comment.