Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Commit 3898cf4

Browse files
Remove all statics from ModLoader
1 parent 7ea0a75 commit 3898cf4

8 files changed

Lines changed: 59 additions & 61 deletions

File tree

Scenes/Prefabs/ModLoader.tscn

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ size_flags_horizontal = 0
1212
size_flags_vertical = 0
1313
theme = ExtResource( 3 )
1414
script = ExtResource( 1 )
15-
__meta__ = {
16-
"_edit_use_anchors_": false
17-
}
1815
NodePathModList = NodePath("VBoxContainer/Mod Loader Info/Mod List/Mod List/MarginContainer/PanelContainer/ScrollContainer/Mods")
1916
NodePathModName = NodePath("VBoxContainer/Mod Loader Info/Mod Info/Mod Information/Mod Name")
2017
NodePathModGameVersions = NodePath("VBoxContainer/Mod Loader Info/Mod Info/Mod Information/MarginContainer2/VBoxContainer/Game Versions")
@@ -219,8 +216,10 @@ scroll_following = true
219216
selection_enabled = true
220217

221218
[node name="FileDialog" type="FileDialog" parent="."]
222-
margin_right = 282.0
223-
margin_bottom = 131.0
219+
margin_left = 1.0
220+
margin_top = 1.0
221+
margin_right = 599.0
222+
margin_bottom = 499.0
224223

225224
[connection signal="pressed" from="VBoxContainer/Mod Loader Info/Mod List/Mod List/HBoxContainer/Refresh" to="." method="_on_Refresh_pressed"]
226225
[connection signal="pressed" from="VBoxContainer/Mod Loader Info/Mod List/Mod List/HBoxContainer/Load Mods" to="." method="_on_Load_Mods_pressed"]

Scenes/Scenes/Menu.tscn

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ anchor_right = 1.0
1515
anchor_bottom = 1.0
1616
theme = ExtResource( 4 )
1717
script = ExtResource( 5 )
18-
__meta__ = {
19-
"_edit_use_anchors_": false
20-
}
2118
NodePathSetupOnlineUsernamePopup = NodePath("Set Online Username Popup")
2219

2320
[node name="PanelContainer" type="PanelContainer" parent="."]
@@ -50,7 +47,7 @@ anchor_right = 0.0
5047
anchor_bottom = 0.0
5148
margin_left = 604.0
5249
margin_right = 1004.0
53-
margin_bottom = 212.0
50+
margin_bottom = 213.0
5451

5552
[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer"]
5653
margin_left = 474.0

Scripts/Msc/UIPopupFileDialogMods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public override void _Ready()
1515

1616
public void Open()
1717
{
18-
FileManager.CurrentDir = ModLoader.ModLoader.PathMods;
18+
FileManager.CurrentDir = GameManager.ModLoader.PathMods;
1919
FileManager.PopupCentered();
2020
}
2121

Scripts/Scenes/Game/SceneGame.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Godot;
2-
using GodotModules.ModLoader;
32
using GodotModules.Netcode;
43
using GodotModules.Netcode.Client;
54
using GodotModules.Netcode.Server;
@@ -37,17 +36,17 @@ public override void _Ready()
3736
AddChild(bullet);
3837

3938
// set game definitions
40-
ModLoader.Script.Globals["Player", "setHealth"] = (Action<int>)Player.SetHealth;
39+
GameManager.ModLoader.Script.Globals["Player", "setHealth"] = (Action<int>)Player.SetHealth;
4140

42-
ModLoader.Call("OnGameInit");
41+
GameManager.ModLoader.Call("OnGameInit");
4342

4443
if (NetworkManager.IsMultiplayer())
4544
InitMultiplayerStuff();
4645
}
4746

4847
public override void _PhysicsProcess(float delta)
4948
{
50-
ModLoader.Call("OnGameUpdate", delta);
49+
GameManager.ModLoader.Call("OnGameUpdate", delta);
5150

5251
if (SceneManager.PrevSceneName == "Menu") // singleplayer
5352
return;

Scripts/Scenes/Main/GameManager.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ namespace GodotModules
88
public class GameManager : Node
99
{
1010
[Export] public readonly NodePath NodePathGameConsole;
11+
public static ModLoader ModLoader { get; set; }
1112
public static UIGameConsole GameConsole { get; set; }
12-
1313
public static GodotCommands GodotCommands { get; set; }
1414

1515
public static string GameName = "Godot Modules";
@@ -21,6 +21,9 @@ public override void _Ready()
2121
GameTree = GetTree();
2222
GameConsole = GetNode<UIGameConsole>(NodePathGameConsole);
2323
GodotCommands = new GodotCommands();
24+
ModLoader = new ModLoader();
25+
ModLoader.Init();
26+
ModLoader.LoadMods();
2427
}
2528

2629
public override async void _Process(float delta)

Scripts/Scenes/Menu/ModLoader.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
using System.IO;
66

7-
namespace GodotModules.ModLoader
7+
namespace GodotModules
88
{
99
public class ModLoader
1010
{
11-
public static List<Mod> LoadedMods = new List<Mod>();
12-
public static Dictionary<string, Mod> ModInfo = new Dictionary<string, Mod>();
13-
private static MoonSharpVsCodeDebugServer DebugServer { get; set; }
14-
public static string PathModsEnabled { get; set; }
15-
public static string PathMods { get; set; }
16-
public static Script Script { get; set; }
17-
public static string LuaScriptsPath = "";
18-
19-
public static void Init()
11+
public List<Mod> LoadedMods = new List<Mod>();
12+
public Dictionary<string, Mod> ModInfo = new Dictionary<string, Mod>();
13+
private MoonSharpVsCodeDebugServer DebugServer { get; set; }
14+
public string PathModsEnabled { get; set; }
15+
public string PathMods { get; set; }
16+
public Script Script { get; set; }
17+
public string LuaScriptsPath = "";
18+
19+
public void Init()
2020
{
2121
PathMods = Path.Combine(GameManager.GetGameDataPath(), "Mods");
2222
PathModsEnabled = Path.Combine(PathMods, "enabled.json");
@@ -33,7 +33,7 @@ public static void Init()
3333
SortMods();
3434
}
3535

36-
public static void SortMods()
36+
public void SortMods()
3737
{
3838
LoadedMods.Clear();
3939
ModInfo = FindAllMods();
@@ -57,7 +57,7 @@ public static void SortMods()
5757
});
5858
}
5959

60-
public static void LoadMods()
60+
public void LoadMods()
6161
{
6262
UIModLoader.Instance.ClearLog();
6363

@@ -83,7 +83,7 @@ public static void LoadMods()
8383
UIModLoader.Instance.Log($"{modLoadedCount} mods have loaded successfully");
8484
}
8585

86-
private static void DisableModsWithLackingDependencies()
86+
private void DisableModsWithLackingDependencies()
8787
{
8888
var modsToDisable = new List<ModInfo>();
8989

@@ -99,7 +99,7 @@ private static void DisableModsWithLackingDependencies()
9999
mod.Enabled = false;
100100
}
101101

102-
private static void CheckAllDependenciesEnabled(ModInfo firstMod, ModInfo modInfo, List<string> checkedMods, ref List<ModInfo> modsToDisable)
102+
private void CheckAllDependenciesEnabled(ModInfo firstMod, ModInfo modInfo, List<string> checkedMods, ref List<ModInfo> modsToDisable)
103103
{
104104
foreach (var dependency in modInfo.Dependencies)
105105
{
@@ -117,7 +117,7 @@ private static void CheckAllDependenciesEnabled(ModInfo firstMod, ModInfo modInf
117117
}
118118
}
119119

120-
public static void Call(string v, params object[] args)
120+
public void Call(string v, params object[] args)
121121
{
122122
try
123123
{
@@ -130,7 +130,7 @@ public static void Call(string v, params object[] args)
130130
}
131131
}
132132

133-
public static void SetModsEnabled()
133+
public void SetModsEnabled()
134134
{
135135
var modsEnabled = new Dictionary<string, bool>();
136136
foreach (var mod in ModInfo)
@@ -139,7 +139,7 @@ public static void SetModsEnabled()
139139
File.WriteAllText(PathModsEnabled, JsonConvert.SerializeObject(modsEnabled, Formatting.Indented));
140140
}
141141

142-
public static void GetModsEnabled()
142+
public void GetModsEnabled()
143143
{
144144
if (!File.Exists(PathModsEnabled))
145145
return;
@@ -152,13 +152,13 @@ public static void GetModsEnabled()
152152
}
153153
}
154154

155-
private static void Log(object obj)
155+
private void Log(object obj)
156156
{
157157
Logger.Log($"[ModLoader]: {obj}");
158158
UIModLoader.Instance.Log($"{obj}");
159159
}
160160

161-
private static void LoadLuaScripts(string directory)
161+
private void LoadLuaScripts(string directory)
162162
{
163163
GodotFileManager.LoadDir(directory, (dir, fileName) =>
164164
{
@@ -182,15 +182,15 @@ private static void LoadLuaScripts(string directory)
182182
});
183183
}
184184

185-
private static void SetupModsEnabled()
185+
private void SetupModsEnabled()
186186
{
187187
if (File.Exists(PathModsEnabled))
188188
return;
189189

190190
File.WriteAllText(PathModsEnabled, "{}");
191191
}
192192

193-
private static Dictionary<string, Mod> FindAllMods()
193+
private Dictionary<string, Mod> FindAllMods()
194194
{
195195
var mods = new Dictionary<string, Mod>();
196196
var modFolders = Directory.GetDirectories(PathMods);
@@ -233,13 +233,13 @@ private static Dictionary<string, Mod> FindAllMods()
233233
return mods;
234234
}
235235

236-
private static void AddDependencyToLoadedMods(int modIndex, string dependency)
236+
private void AddDependencyToLoadedMods(int modIndex, string dependency)
237237
{
238238
if (!LoadedMods.Contains(ModInfo[dependency]))
239239
LoadedMods.Insert(modIndex, ModInfo[dependency]);
240240
}
241241

242-
private static void LoadMod(Mod mod, ref int modLoadedCount)
242+
private void LoadMod(Mod mod, ref int modLoadedCount)
243243
{
244244
try
245245
{
@@ -255,15 +255,15 @@ private static void LoadMod(Mod mod, ref int modLoadedCount)
255255
}
256256
}
257257

258-
private static bool ModHasName(ModInfo modInfo) => modInfo.Name != null;
258+
private bool ModHasName(ModInfo modInfo) => modInfo.Name != null;
259259

260-
private static bool RequiredModFilesExist(string folder, string pathInfo, string pathScriptLua) => File.Exists(pathInfo) && File.Exists(pathScriptLua);
260+
private bool RequiredModFilesExist(string folder, string pathInfo, string pathScriptLua) => File.Exists(pathInfo) && File.Exists(pathScriptLua);
261261

262-
private static bool IsModMissingDependency(string dependency) => !ModInfo.ContainsKey(dependency);
262+
private bool IsModMissingDependency(string dependency) => !ModInfo.ContainsKey(dependency);
263263

264-
private static bool ModHasDependency(string dependency) => ModInfo.ContainsKey(dependency);
264+
private bool ModHasDependency(string dependency) => ModInfo.ContainsKey(dependency);
265265

266-
private static bool ModHasAllDependenciesEnabled(Mod mod)
266+
private bool ModHasAllDependenciesEnabled(Mod mod)
267267
{
268268
var allDependenciesEnabled = true;
269269

Scripts/Scenes/Menu/UIModInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Godot;
22

3-
namespace GodotModules.ModLoader
3+
namespace GodotModules
44
{
55
public class UIModInfo : Control
66
{
@@ -43,7 +43,7 @@ private void _on_Enabled_pressed()
4343
var modName = BtnMod.Text;
4444

4545
BtnModEnabled.Text = Enabled ? "[x]" : "[ ]";
46-
ModLoader.ModInfo[modName].ModInfo.Enabled = Enabled;
46+
GameManager.ModLoader.ModInfo[modName].ModInfo.Enabled = Enabled;
4747

4848
if (DisplayedInDependencies)
4949
UIModLoader.Instance.ModInfoList[modName].SetModEnabled(Enabled);

Scripts/Scenes/Menu/UIModLoader.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System;
33
using System.Diagnostics;
44

5-
namespace GodotModules.ModLoader
5+
namespace GodotModules
66
{
77
public class UIModLoader : Control
88
{
@@ -31,7 +31,7 @@ public class UIModLoader : Control
3131
// logger
3232
private RichTextLabel Logger { get; set; }
3333

34-
public override void _Ready()
34+
public override async void _Ready()
3535
{
3636
Instance = this;
3737
ModList = GetNode<VBoxContainer>(NodePathModList);
@@ -46,8 +46,8 @@ public override void _Ready()
4646
ModDescription.Text = "";
4747
Logger.Clear();
4848

49-
ModLoader.Init();
50-
ModLoader.LoadMods();
49+
while (GameManager.ModLoader == null) // this is ugly
50+
await Task.Delay(1);
5151

5252
DisplayMods();
5353
}
@@ -61,7 +61,7 @@ public void UpdateModInfo(string name)
6161
foreach (Node node in ModDependencies.GetChildren())
6262
node.QueueFree();
6363

64-
var modInfo = ModLoader.ModInfo[name].ModInfo;
64+
var modInfo = GameManager.ModLoader.ModInfo[name].ModInfo;
6565

6666
ModName.Text = name;
6767

@@ -86,24 +86,24 @@ public void UpdateModInfo(string name)
8686

8787
public void DisplayMods()
8888
{
89-
ModLoader.LoadedMods.ForEach(mod =>
89+
GameManager.ModLoader.LoadedMods.ForEach(mod =>
9090
{
9191
var modInfo = CreateModInfoInstance(mod.ModInfo.Name);
9292
ModList.AddChild(modInfo);
9393
ModInfoList[mod.ModInfo.Name] = modInfo;
9494
});
9595

96-
if (ModLoader.LoadedMods.Count > 0)
97-
UpdateModInfo(ModLoader.LoadedMods[0].ModInfo.Name);
96+
if (GameManager.ModLoader.LoadedMods.Count > 0)
97+
UpdateModInfo(GameManager.ModLoader.LoadedMods[0].ModInfo.Name);
9898
}
9999

100100
private UIModInfo CreateModInfoInstance(string modName)
101101
{
102102
var instance = Prefabs.ModInfo.Instance<UIModInfo>();
103103
instance.SetModName(modName);
104104

105-
if (ModLoader.ModInfo[modName].ModInfo.Enabled)
106-
instance.SetModEnabled(ModLoader.ModInfo[modName].ModInfo.Enabled);
105+
if (GameManager.ModLoader.ModInfo[modName].ModInfo.Enabled)
106+
instance.SetModEnabled(GameManager.ModLoader.ModInfo[modName].ModInfo.Enabled);
107107
else
108108
instance.SetModEnabled(false);
109109

@@ -112,12 +112,12 @@ private UIModInfo CreateModInfoInstance(string modName)
112112

113113
private void _on_Refresh_pressed()
114114
{
115-
ModLoader.SetModsEnabled();
115+
GameManager.ModLoader.SetModsEnabled();
116116

117117
foreach (Node node in ModList.GetChildren())
118118
node.QueueFree();
119119

120-
ModLoader.SortMods();
120+
GameManager.ModLoader.SortMods();
121121
DisplayMods();
122122
}
123123

@@ -128,11 +128,11 @@ private void _on_Load_Mods_pressed()
128128
var modName = info.ModName;
129129
var modEnabled = info.Enabled;
130130

131-
ModLoader.ModInfo[modName].ModInfo.Enabled = modEnabled;
131+
GameManager.ModLoader.ModInfo[modName].ModInfo.Enabled = modEnabled;
132132
}
133133

134-
ModLoader.SetModsEnabled();
135-
ModLoader.LoadMods();
134+
GameManager.ModLoader.SetModsEnabled();
135+
GameManager.ModLoader.LoadMods();
136136
}
137137

138138
private void _on_Open_Mods_Folder_pressed()
@@ -145,7 +145,7 @@ private void _on_Open_Mods_Folder_pressed()
145145
{
146146
using (Process myProcess = new Process())
147147
{
148-
myProcess.StartInfo.FileName = ModLoader.PathMods;
148+
myProcess.StartInfo.FileName = GameManager.ModLoader.PathMods;
149149
myProcess.Start();
150150
}
151151
}

0 commit comments

Comments
 (0)