Skip to content

Commit

Permalink
Resolve "GlobalValue" issues
Browse files Browse the repository at this point in the history
  • Loading branch information
DexyThePuppy committed Jan 23, 2025
1 parent 565fb71 commit 846be4b
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 228 deletions.
4 changes: 2 additions & 2 deletions ProtoFluxVisualsOverhaul/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
[assembly: AssemblyDescription("A ResoniteModLoader mod for Resonite that adds Overhauled ProtoFlux Visuals Customizations!")]
[assembly: AssemblyCompany("Dexy")]
[assembly: AssemblyCopyright("Copyright © 2024 Dexy")]
[assembly: AssemblyVersion(ProtoFluxVisualsOverhaul.ProtoFluxVisualsOverhaul.VERSION_CONSTANT)]
[assembly: AssemblyFileVersion(ProtoFluxVisualsOverhaul.ProtoFluxVisualsOverhaul.VERSION_CONSTANT)]
[assembly: AssemblyVersion("1.4.1")]
[assembly: AssemblyFileVersion("1.4.1")]
Empty file.
183 changes: 0 additions & 183 deletions ProtoFluxVisualsOverhaul/ProtoFluxRoundedUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -935,187 +935,4 @@ public static void Postfix(ProtoFluxNodeVisual __instance) {
}
}
}

// Patch to handle global references panel styling
[HarmonyPatch(typeof(ProtoFluxNodeVisual), "GenerateGlobalRefs")]
public class ProtoFluxNodeVisual_GenerateGlobalRefs_Patch {
public static void Postfix(ProtoFluxNodeVisual __instance, UIBuilder ui, ProtoFluxNode node) {
try {
// Skip if disabled
if (!ProtoFluxVisualsOverhaul.Config.GetValue(ProtoFluxVisualsOverhaul.ENABLED)) return;

// === User Permission Check ===
if (!PermissionHelper.HasPermission(__instance)) return;

Logger.LogUI("Global Refs", "Styling global references panel");

// Find the global references panel
var globalRefsPanel = ui.Root.FindChild("Global References");
if (globalRefsPanel == null) return;

// Style the panel
var images = globalRefsPanel.GetComponentsInChildren<Image>();
foreach (var img in images) {
// Skip connectors
if (img.Slot.Name == "Connector") continue;

// Apply rounded corners to backgrounds
if (img.Tint.Value == RadiantUI_Constants.BG_COLOR) {
RoundedCornersHelper.ApplyRoundedCorners(img, false);
}
}

Logger.LogUI("Global Refs", "Global references panel styled successfully!");
}
catch (Exception e) {
Logger.LogError("Error in GenerateGlobalRefs patch", e, LogCategory.UI);
}
}
}

// Patch to handle global reference element generation
[HarmonyPatch(typeof(ProtoFluxNodeVisual))]
[HarmonyPatch("GenerateGlobalRefElement")]
[HarmonyPriority(HarmonyLib.Priority.High)]
public class ProtoFluxNodeVisual_GenerateGlobalRefElement_Patch {
private static bool IsUpdateNodeField(ProtoFluxNode node, ISyncRef globalRef, Type referenceType) {
if (node == null) return false;

// Check if this is an Update node
if (node.GetType().IsSubclassOf(typeof(UpdateBase)) || node.GetType().IsSubclassOf(typeof(UserUpdateBase))) {
// Check if this is the SkipIfNull field
var field = node.GetType().GetField("SkipIfNull");
return field != null && field.FieldType == globalRef.GetType();
}
return false;
}

public static bool Prefix(ProtoFluxNodeVisual __instance, UIBuilder ui, Type referenceType, ISyncRef globalRef) {
try {
// Skip if disabled
if (!ProtoFluxVisualsOverhaul.Config.GetValue(ProtoFluxVisualsOverhaul.ENABLED)) return true;

// === User Permission Check ===
if (!PermissionHelper.HasPermission(__instance)) return true;

var node = __instance.Node.Target;
if (node == null) return true;

// Special handling for Update nodes
if (IsUpdateNodeField(node, globalRef, referenceType)) {
Logger.LogUI("Global Refs", $"Styling global reference for Update node: {referenceType.Name}");

try {
// Try to find an existing global value of the correct type
var world = __instance.World;
var existingGlobals = world.RootSlot.GetComponentsInChildren<FrooxEngine.ProtoFlux.GlobalValue<bool>>();

// Try to use an existing global value first
foreach (var existing in existingGlobals) {
try {
if (existing.GetType() == typeof(FrooxEngine.ProtoFlux.GlobalValue<bool>)) {
globalRef.TrySet(existing);
Logger.LogUI("Global Value", "Found and set existing global value");
return false;
}
}
catch { } // Ignore if this one doesn't work
}

// If no existing value works, create a new one
var globalValueSlot = world.RootSlot.AddSlot("Global Value");
var globalValue = globalValueSlot.AttachComponent<FrooxEngine.ProtoFlux.GlobalValue<bool>>();
globalRef.TrySet(globalValue);
Logger.LogUI("Global Value", "Created and set new global value");
return false;
}
catch (Exception e) {
Logger.LogError("Error handling global value", e, LogCategory.UI);
}
}

return true; // Let original method run for non-Update nodes
}
catch (Exception e) {
Logger.LogError("Error in GenerateGlobalRefElement patch", e, LogCategory.UI);
return true; // Run original on error
}
}

public static void Postfix(ProtoFluxNodeVisual __instance, UIBuilder ui, Type referenceType, ISyncRef globalRef) {
try {
// Skip if disabled
if (!ProtoFluxVisualsOverhaul.Config.GetValue(ProtoFluxVisualsOverhaul.ENABLED)) return;

// === User Permission Check ===
if (!PermissionHelper.HasPermission(__instance)) return;

var node = __instance.Node.Target;
if (node == null) return;

// Special handling for Update nodes
if (IsUpdateNodeField(node, globalRef, referenceType)) {
Logger.LogUI("Global Refs", $"Styling global reference for Update node: {referenceType.Name}");

// Find the most recently added global reference UI elements
var images = ui.Current.GetComponentsInChildren<Image>();
foreach (var img in images) {
// Skip connectors
if (img.Slot.Name == "Connector") continue;

// Apply rounded corners to backgrounds
if (img.Tint.Value == RadiantUI_Constants.BG_COLOR) {
RoundedCornersHelper.ApplyRoundedCorners(img, false);
}
}
}
}
catch (Exception e) {
Logger.LogError("Error in GenerateGlobalRefElement postfix", e, LogCategory.UI);
}
}
}

// Patch to handle global value type mismatches
[HarmonyPatch]
public class SyncRef_SetTarget_Patch {
static MethodBase TargetMethod() {
// Find the set_Target method in the ISyncRef interface implementation of SyncRef<IGlobalValueProxy<bool>>
var syncRefType = typeof(SyncRef<>).MakeGenericType(typeof(IGlobalValueProxy<bool>));
var interfaceMap = syncRefType.GetInterfaceMap(typeof(ISyncRef));
return interfaceMap.TargetMethods.First(m => m.Name.Contains("set_Target"));
}

public static bool Prefix(object __instance, IWorldElement value) {
try {
if (value == null) return true;

// Check if this is a global value with wrong type
if (value is GlobalValue<string>) {
Logger.LogUI("Global Value", "Intercepted string global value assignment");

// Get the world from the value since it's a WorldElement
var world = value.World;
if (world != null) {
// Create a new boolean global value
var globalValueSlot = world.RootSlot.AddSlot("Global Value");
var globalValue = globalValueSlot.AttachComponent<FrooxEngine.ProtoFlux.GlobalValue<bool>>();

// Set the target to our new boolean global value
var setTargetMethod = __instance.GetType().GetMethod("set_Target", BindingFlags.Public | BindingFlags.Instance);
setTargetMethod.Invoke(__instance, new object[] { globalValue });

Logger.LogUI("Global Value", "Created and set new boolean global value");
return false;
}
}

return true;
}
catch (Exception e) {
Logger.LogError("Error in set_Target patch", e, LogCategory.UI);
return true;
}
}
}
}
31 changes: 1 addition & 30 deletions ProtoFluxVisualsOverhaul/ProtoFluxVisualsOverhaul.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
using System.Collections.Generic;
using Elements.Assets;
using System.Linq;
using ResoniteHotReloadLib;
using FrooxEngine.UIX;
using System.Reflection;
using static ProtoFluxVisualsOverhaul.Logger;

namespace ProtoFluxVisualsOverhaul;
//More info on creating mods can be found https://github.com/resonite-modding-group/ResoniteModLoader/wiki/Creating-Mods
public class ProtoFluxVisualsOverhaul : ResoniteMod {
internal const string VERSION_CONSTANT = "1.1.0";
internal const string VERSION_CONSTANT = "1.4.1";
public override string Name => "ProtoFluxVisualsOverhaul";
public override string Author => "Dexy, NepuShiro";
public override string Version => VERSION_CONSTANT;
Expand Down Expand Up @@ -139,9 +138,6 @@ public override void OnEngineInit() {
harmony.PatchAll();
Logger.LogUI("Startup", "ProtoFluxVisualsOverhaul successfully loaded and patched");

// Register for hot reload
HotReloader.RegisterForHotReload(this);

Config.OnThisConfigurationChanged += (k) => {
if (k.Key != ENABLED) {
Engine.Current.GlobalCoroutineManager.StartTask(async () => {
Expand Down Expand Up @@ -397,31 +393,6 @@ private static StaticTexture2D GetOrCreateSharedTexture(Slot slot, Uri uri) {
return texture;
}

// Hot reload methods
[HarmonyPatch("BeforeHotReload")]
public static void BeforeHotReload()
{
// Cleanup Harmony patches
var harmony = new Harmony("com.Dexy.ProtoFluxVisualsOverhaul");
harmony.UnpatchAll("com.Dexy.ProtoFluxVisualsOverhaul");

// Clear cached data
pannerCache.Clear();
Logger.LogUI("Cleanup", "Cleaned up before hot reload");
}

public static void OnHotReload(ResoniteMod modInstance)
{
// Get the config from the mod instance
Config = modInstance.GetConfiguration();

// Re-apply Harmony patches
var harmony = new Harmony("com.Dexy.ProtoFluxVisualsOverhaul");
harmony.PatchAll();

Logger.LogUI("Reload", "Hot reload complete");
}

[HarmonyPatch(typeof(ProtoFluxNodeVisual), "BuildUI")]
class ProtoFluxNodeVisual_BuildUI_Patch {
private static bool hasPreloadedAudio = false;
Expand Down
19 changes: 6 additions & 13 deletions ProtoFluxVisualsOverhaul/ProtoFluxVisualsOverhaul.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>ProtoFluxVisualsOverhaul</RootNamespace>
<AssemblyName>ProtoFluxVisualsOverhaul</AssemblyName>
Expand All @@ -15,10 +16,11 @@

<PropertyGroup Condition="'$(ResonitePath)'==''">
<!-- If you don't want to provide a ResonitePath in dotnet build, you can specify one here -->
<ResonitePath>$(MSBuildThisFileDirectory)Resonite/</ResonitePath>
<ResonitePath>G:\SteamLibrary\steamapps\common\Resonite\</ResonitePath>
<!-- Fallback paths if the above doesn't exist -->
<ResonitePath Condition="Exists('C:\Program Files (x86)\Steam\steamapps\common\Resonite\')">C:\Program Files (x86)\Steam\steamapps\common\Resonite\</ResonitePath>
<ResonitePath Condition="Exists('$(HOME)/.steam/steam/steamapps/common/Resonite/')">$(HOME)/.steam/steam/steamapps/common/Resonite/</ResonitePath>
<ResonitePath Condition="Exists('G:\SteamLibrary\steamapps\common\Resonite\')">G:\SteamLibrary\steamapps\common\Resonite\</ResonitePath>
<ResonitePath Condition="Exists('$(MSBuildThisFileDirectory)Resonite/')">$(MSBuildThisFileDirectory)Resonite/</ResonitePath>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -60,19 +62,10 @@
<Reference Include="ProtoFlux.Nodes.FrooxEngine">
<HintPath>$(ResonitePath)Resonite_Data\Managed\ProtoFlux.Nodes.FrooxEngine.dll</HintPath>
</Reference>
<Reference Include="ResoniteHotReloadLib">
<HintPath>$(ResonitePath)rml_mods\ResoniteHotReloadLib.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="ResoniteHotReloadLibCore">
<HintPath>$(ResonitePath)rml_mods\ResoniteHotReloadLibCore.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(CopyToMods)'=='true'">
<Message Text="Attempting to copy $(TargetFileName) to $(ResonitePath)rml_mods and HotReloadMods" Importance="high" />
<Message Text="Attempting to copy $(TargetFileName) to $(ResonitePath)rml_mods" Importance="high" />
<Copy SourceFiles="$(TargetDir)$(TargetFileName)" DestinationFolder="$(ResonitePath)rml_mods" ContinueOnError="true" />
<Copy SourceFiles="$(TargetDir)$(TargetFileName)" DestinationFolder="$(ResonitePath)rml_mods\HotReloadMods\" ContinueOnError="true" />
</Target>
</Project>

0 comments on commit 846be4b

Please sign in to comment.