Skip to content

Commit

Permalink
Merge pull request #8 from sctanf/master
Browse files Browse the repository at this point in the history
Fix longstanding issue where F8 resets all settings.
Patch provided by @sctanf
  • Loading branch information
BlueCyro authored Jan 20, 2023
2 parents 4be6a9f + 263a540 commit 9871c0f
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions PhotonicFreedom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public static List<Type> Types
private static JsonSerializerSettings? serializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, TypeNameHandling = TypeNameHandling.Auto };
public static void Bootstrap()
{
Slot UserspacePhotonicFreedom = Userspace.UserspaceWorld.RootSlot.Find("PhotonicFreedom");
if (UserspacePhotonicFreedom != null)
{
UserspacePhotonicFreedom.Destroy();
}
Userspace.UserspaceWorld.RootSlot.AddSlot("PhotonicFreedom", false);
UnityEngine.Camera mainCam = UnityEngine.Camera.main;
PostProcessLayer layer = mainCam.GetComponent<PostProcessLayer>();
if(layer == null)
Expand Down Expand Up @@ -128,7 +134,7 @@ public static void WriteCurrentSettings(Type type, object comp)
public static void ReadAllSettings(Type type, object comp)
{
ClassSerializer loaded = JsonConvert.DeserializeObject<ClassSerializer>(File.ReadAllText($"nml_mods/Photonic_Settings/{type.Name}.json"), serializerSettings);
Slot UserspaceRoot = Userspace.UserspaceWorld.RootSlot;
Slot UserspacePhotonicFreedom = Userspace.UserspaceWorld.RootSlot.Find("PhotonicFreedom");
foreach (var field in FieldHolders[type].Fields)
{
IFieldValuePair pair = loaded.defaultFieldValues.FirstOrDefault(pair => pair.fieldName == field.Name);
Expand All @@ -137,7 +143,7 @@ public static void ReadAllSettings(Type type, object comp)
{
field.SetRealValue(comp, pair.BoxedValue);
object? SanitizedValue = pair.BoxedValue.GetType() == typeof(UnityEngine.Color) ? UnityNeos.Conversions.ToNeos((UnityEngine.Color)pair.BoxedValue) : pair.BoxedValue;
var DynValue = UserspaceRoot.AttachComponent(typeof(DynamicValueVariable<>).MakeGenericType(SanitizedValue.GetType()));
var DynValue = UserspacePhotonicFreedom.AttachComponent(typeof(DynamicValueVariable<>).MakeGenericType(SanitizedValue.GetType()));
if (DynValue == null)
return;

Expand Down Expand Up @@ -173,7 +179,7 @@ public static void ReadAllSettings(Type type, object comp)
{
property.SetRealValue(comp, pair.BoxedValue);
object? SanitizedValue = pair.BoxedValue.GetType() == typeof(UnityEngine.Color) ? UnityNeos.Conversions.ToNeos((UnityEngine.Color)pair.BoxedValue) : pair.BoxedValue;
var DynValue = UserspaceRoot.AttachComponent(typeof(DynamicValueVariable<>).MakeGenericType(SanitizedValue.GetType()));
var DynValue = UserspacePhotonicFreedom.AttachComponent(typeof(DynamicValueVariable<>).MakeGenericType(SanitizedValue.GetType()));
if (DynValue == null)
{
return;
Expand Down Expand Up @@ -451,11 +457,13 @@ public static IEnumerable<CodeInstruction> OnAttach_Transpiler(IEnumerable<CodeI
[HarmonyPatch]
public static class Userspace_Patch
{
private static bool OutputDeviceChanged = false;
private static UnityEngine.Camera? mainCam;
[HarmonyPatch(typeof(Userspace), "SaveAllSettings")]
[HarmonyPrefix]
public static void SaveAllSettings_Prefix(Userspace __instance)
{
UnityEngine.Camera mainCam = UnityEngine.Camera.main;
mainCam = UnityEngine.Camera.main;
if (mainCam == null)
{
return;
Expand All @@ -472,11 +480,33 @@ public static void SaveAllSettings_Prefix(Userspace __instance)
WriteCurrentSettings(type, comp);
});
}
public static void VRActiveChangedEvent(bool a)
{
OutputDeviceChanged = true;
}
[HarmonyPatch(typeof(Userspace), "OnAttach")]
[HarmonyPostfix]
public static void OnAttach_Postfix(Userspace __instance)
{
mainCam = UnityEngine.Camera.main;
Bootstrap();
__instance.InputInterface.VRActiveChanged += VRActiveChangedEvent;
}
[HarmonyPatch(typeof(ScreenModeController), "OnCommonUpdate")]
[HarmonyPostfix]
public static void OnCommonUpdate_Postfix(ScreenModeController __instance)
{
if (OutputDeviceChanged == true)
{
Msg("Waiting for changed camera");
if (mainCam != UnityEngine.Camera.main && UnityEngine.Camera.main != null)
{
Msg("Resetting Camera");
mainCam = UnityEngine.Camera.main;
OutputDeviceChanged = false;
Bootstrap();
}
}
}
}
}

0 comments on commit 9871c0f

Please sign in to comment.