Skip to content

Commit d560b11

Browse files
committed
[UMM] Clean up the UMM plugin class; Fix some minor issues
1 parent 39baf4d commit d560b11

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

RuntimeUnityEditor.UMM/RuntimeUnityEditorUMM.cs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using UnityModManagerNet;
66

77
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
8+
#pragma warning disable IDE0051 // Remove unused private members
89

910
namespace RuntimeUnityEditor.UMM
1011
{
@@ -13,15 +14,20 @@ namespace RuntimeUnityEditor.UMM
1314
/// When referencing RuntimeUnityEditor from other code it's recommended to not reference this assembly and instead reference RuntimeUnityEditorCore directly.
1415
/// You can see if RuntimeUnityEditor has finished loading with <code>RuntimeUnityEditorCore.IsInitialized()</code>.
1516
/// </summary>
16-
[Obsolete("It's recommended to reference RuntimeUnityEditorCore directly")] // TODO: Since core dll is ILMerged and internalized this is impossible to do
17+
[Obsolete("It's recommended to reference RuntimeUnityEditorCore directly")]
1718
public static class RuntimeUnityEditorUMM
1819
{
19-
public static bool Enabled { get; private set; }
20+
public static bool Enabled
21+
{
22+
get => _editorMonoBehaviour?.enabled ?? false;
23+
private set => _editorMonoBehaviour.enabled = value;
24+
}
2025
public static UnityModManager.ModEntry Instance { get; private set; }
2126
public static RuntimeUnityEditorCore CoreInstance { get; private set; }
22-
public static RuntimeUnityEditorSettings Settings;
23-
private static GameObject runtimeUnityEditorGO;
24-
private static MonoBehaviour RuntimeUnityEditor;
27+
public static RuntimeUnityEditorSettings Settings { get; private set; }
28+
29+
private static GameObject _editorGameObject;
30+
private static MonoBehaviour _editorMonoBehaviour;
2531

2632
public static bool Load(UnityModManager.ModEntry modEntry)
2733
{
@@ -33,11 +39,11 @@ public static bool Load(UnityModManager.ModEntry modEntry)
3339

3440
Instance = modEntry;
3541

36-
Settings = RuntimeUnityEditorSettings.Load<RuntimeUnityEditorSettings>(Instance);
42+
Settings = UnityModManager.ModSettings.Load<RuntimeUnityEditorSettings>(Instance);
3743
Settings.Load(Instance);
3844

39-
runtimeUnityEditorGO = new GameObject("RuntimeUnityEditor", typeof(RuntimeUnityEditorBehaviour));
40-
UnityEngine.Object.DontDestroyOnLoad(runtimeUnityEditorGO);
45+
_editorGameObject = new GameObject("RuntimeUnityEditor", typeof(RuntimeUnityEditorBehaviour));
46+
UnityEngine.Object.DontDestroyOnLoad(_editorGameObject);
4147

4248
Instance.OnGUI = OnGUI;
4349
Instance.OnSaveGUI = Settings.Save;
@@ -52,37 +58,27 @@ private static void OnGUI(UnityModManager.ModEntry modEntry)
5258

5359
private class RuntimeUnityEditorBehaviour : MonoBehaviour
5460
{
55-
void Start()
61+
private void Start()
5662
{
57-
RuntimeUnityEditor = this;
63+
_editorMonoBehaviour = this;
5864
CoreInstance = new RuntimeUnityEditorCore(new UMMInitSettings(Instance, Settings));
5965
}
60-
private void Update()
61-
{
62-
CoreInstance.Update();
63-
}
6466

65-
private void LateUpdate()
66-
{
67-
CoreInstance.LateUpdate();
68-
}
67+
private void Update() => CoreInstance.Update();
6968

70-
private void OnGUI()
71-
{
72-
CoreInstance.OnGUI();
73-
}
69+
private void LateUpdate() => CoreInstance.LateUpdate();
70+
71+
private void OnGUI() => CoreInstance.OnGUI();
7472
}
7573

7674
private sealed class UMMInitSettings : InitSettings
7775
{
78-
private readonly UnityModManager.ModEntry _instance;
79-
private RuntimeUnityEditorSettings _settings;
76+
private readonly RuntimeUnityEditorSettings _settings;
8077

8178
public UMMInitSettings(UnityModManager.ModEntry instance, RuntimeUnityEditorSettings settings)
8279
{
83-
_instance = instance;
8480
_settings = settings;
85-
LoggerWrapper = new LoggerUMM(_instance.Logger);
81+
LoggerWrapper = new LoggerUMM(instance.Logger);
8682
}
8783

8884
protected override Action<T> RegisterSetting<T>(string category, string name, T defaultValue, string description, Action<T> onValueUpdated)
@@ -94,11 +90,11 @@ protected override Action<T> RegisterSetting<T>(string category, string name, T
9490
}
9591
setting.OnChanged = onValueUpdated;
9692
onValueUpdated(setting.Value);
97-
93+
9894
return (newValue) => { setting.Value = newValue; onValueUpdated(newValue); };
9995
}
10096

101-
public override MonoBehaviour PluginMonoBehaviour => RuntimeUnityEditor;
97+
public override MonoBehaviour PluginMonoBehaviour => _editorMonoBehaviour;
10298
public override ILoggerWrapper LoggerWrapper { get; }
10399
public override string ConfigPath => Instance.Path;
104100
}
@@ -116,25 +112,33 @@ public void Log(Core.Utils.Abstractions.LogLevel logLevel, object content)
116112
{
117113
switch (logLevel)
118114
{
115+
case LogLevel.All:
119116
case LogLevel.None:
120117
case LogLevel.Fatal:
121-
if (content is Exception) _logger.LogException(content as Exception);
122-
else _logger.Critical(content as string);
118+
if (content is Exception e1) _logger.LogException(e1);
119+
else _logger.Critical(content as string);
123120
break;
121+
124122
case LogLevel.Error:
125-
if (content is Exception) _logger.LogException(content as Exception);
123+
if (content is Exception e2) _logger.LogException(e2);
126124
else _logger.Error(content as string);
127125
break;
126+
128127
case LogLevel.Warning:
129-
if (content is Exception) _logger.LogException(content as Exception);
128+
if (content is Exception e3) _logger.LogException(e3);
130129
else _logger.Warning(content as string);
131130
break;
131+
132132
case LogLevel.Message:
133133
case LogLevel.Info:
134134
case LogLevel.Debug:
135-
if (content is Exception) _logger.LogException(content as Exception);
135+
if (content is Exception e4) _logger.LogException(e4);
136136
else _logger.Log(content as string);
137137
break;
138+
139+
default:
140+
_logger.Warning($"Unknown LogLevel [{logLevel}] for content: {content}");
141+
break;
138142
}
139143
}
140144
}

0 commit comments

Comments
 (0)