Skip to content

Commit

Permalink
Merge pull request #60 from Nytra/toggleCoreAssembly
Browse files Browse the repository at this point in the history
Add plugin settings to toggle loading the plugin when starting new sessions
  • Loading branch information
Nytra authored Nov 29, 2024
2 parents 0076a04 + 17904aa commit 58ccc19
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
4 changes: 2 additions & 2 deletions ProjectObsidian/Injection/Injection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ static ExecutionHook()
{
Engine.Current.OnReady += () =>
{
ShaderInjection.AppendShaders();

ShaderInjection.AppendShaders();
Settings.GetActiveSetting<PluginSettings>();
};
}
catch (Exception e)
Expand Down
1 change: 0 additions & 1 deletion ProjectObsidian/Settings/MIDI_Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ protected override void OnStart()
_localeData.LocaleCode = "en";
_localeData.Authors = new List<string>() { "Nytra" };
_localeData.Messages = new Dictionary<string, string>();
_localeData.Messages.Add("Settings.Category.Obsidian", "Obsidian");
_localeData.Messages.Add("Settings.MIDI_Settings", "MIDI Settings");
_localeData.Messages.Add("Settings.MIDI_Settings.RefreshDeviceLists", "Refresh Devices");
_localeData.Messages.Add("Settings.MIDI_Settings.InputDevices", "Input Devices");
Expand Down
83 changes: 83 additions & 0 deletions ProjectObsidian/Settings/PluginSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FrooxEngine;
using Elements.Core;
using Elements.Assets;
using Commons.Music.Midi;
using System.Reflection;

namespace Obsidian;

[SettingCategory("Obsidian")]
public class PluginSettings : SettingComponent<PluginSettings>
{
public override bool UserspaceOnly => true;

[NonPersistent]
[SettingIndicatorProperty(null, null, null, null, false, 0L)]
public readonly Sync<bool> CoreAssemblyLoaded;

private LocaleData _localeData;

private static AssemblyTypeRegistry obsidianRegistry;

protected override void OnStart()
{
base.OnStart();
_localeData = new LocaleData();
_localeData.LocaleCode = "en";
_localeData.Authors = new List<string>() { "Nytra" };
_localeData.Messages = new Dictionary<string, string>();
_localeData.Messages.Add("Settings.Category.Obsidian", "Obsidian");
_localeData.Messages.Add("Settings.PluginSettings", "Plugin Settings");
_localeData.Messages.Add("Settings.PluginSettings.CoreAssemblyLoaded", "Core Assembly Loaded");
_localeData.Messages.Add("Settings.PluginSettings.ToggleCoreAssembly", "Toggle Core Assembly");

CoreAssemblyLoaded.Value = true;

// Sometimes the locale is null in here, so wait a bit I guess

RunInUpdates(15, () =>
{
UpdateLocale();
Settings.RegisterValueChanges<LocaleSettings>(UpdateLocale);
});
}

protected override void OnDispose()
{
base.OnDispose();
Settings.UnregisterValueChanges<LocaleSettings>(UpdateLocale);
}

private void UpdateLocale(LocaleSettings settings = null)
{
this.GetCoreLocale()?.Asset?.Data.LoadDataAdditively(_localeData);
}

[SettingProperty(null, null, null, false, 0L, null, null)]
[SyncMethod(typeof(Action), new string[] { })]
public void ToggleCoreAssembly()
{
UniLog.Log("Toggle pressed");
var glob = (List<AssemblyTypeRegistry>)typeof(GlobalTypeRegistry).GetField("_coreAssemblies", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null);
if (CoreAssemblyLoaded)
{
foreach (var thing in glob.ToList())
{
if (thing.Assembly == Assembly.GetExecutingAssembly())
{
obsidianRegistry = thing;
glob.Remove(thing);
}
}
CoreAssemblyLoaded.Value = false;
}
else
{
glob.Add(obsidianRegistry);
CoreAssemblyLoaded.Value = true;
}
}
}

0 comments on commit 58ccc19

Please sign in to comment.