Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit b62a7ad

Browse files
authored
Merge pull request #4 from Exund/developer
Mod early_load
2 parents f889c40 + 2ec7fcc commit b62a7ad

4 files changed

Lines changed: 72 additions & 77 deletions

File tree

ModLoader/Console.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ private void Awake()
3030
this._consoleGui = Console.root.GetComponent<ConsoleGUI>();
3131
string date = string.Format("{0:yyyy-MM-dd}", DateTime.UtcNow);
3232
this.logFile = FileLocations.BaseFolder.Extend("logs").CreateFolder().ExtendToFile(date+".txt");
33-
}
33+
// Clear log content on startup
34+
logFile.WriteText("");
35+
}
3436

3537
private void OnEnable()
3638
{

ModLoader/Loader.cs

Lines changed: 54 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
using HarmonyLib;
1+
using HarmonyLib;
22
using UnityEngine;
33
using SFS.IO;
44
using System.Collections.Generic;
5+
using System.Linq;
56
using System;
67
using System.Reflection;
78
using UnityEngine.SceneManagement;
89
using UnityEngine.Events;
910
using System.IO;
1011
using System.Text.RegularExpressions;
11-
using SFS.UI;
12-
using SFS.Translations;
13-
using SFS.Input;
1412

1513
namespace ModLoader
1614
{
@@ -19,7 +17,6 @@ namespace ModLoader
1917
/// </summary>
2018
public class Loader : MonoBehaviour
2119
{
22-
2320
// This save Loader instance
2421
public static Loader main;
2522
//private Console _console;
@@ -34,39 +31,29 @@ public class Loader : MonoBehaviour
3431

3532
public static FolderPath ModsFolder
3633
{
37-
get
38-
{
39-
return _modsFolder;
40-
}
34+
get { return _modsFolder; }
4135
}
4236

43-
private List<string> _modsLoaded;
37+
private List<SFSMod> _modsLoaded = new List<SFSMod>();
4438

4539
// List of all mods loaded in the MODS folder
4640
private Dictionary<string, SFSMod> _modList;
4741

4842
/// <summary>
49-
/// first executed method. Save the loader instance to static var and subscribe to the scene event.
43+
/// First executed method. Save the loader instance to static var, subscribe to the scene event and load mods for early patches.
5044
/// </summary>
5145
private void Awake()
5246
{
5347
Loader.main = this;
5448
SceneManager.sceneLoaded += this.OnSceneLoaded;
5549
_modsFolder = FileLocations.BaseFolder.Extend("MODS").CreateFolder();
56-
}
5750

58-
/// <summary>
59-
/// This method starts reading mods and runs automatically when this class is created after the Awake method.
60-
/// </summary>
61-
private void Start()
62-
{
63-
Debug.Log("Reading mods");
51+
Debug.Log("Early loading mods");
6452
this._modList = this.getModList();
65-
this._modsLoaded = new List<string>();
66-
53+
6754
foreach (SFSMod mod in this._modList.Values)
6855
{
69-
if(this._modsLoaded.Contains(mod.ModId))
56+
if (this._modsLoaded.Any(m => m.ModId == mod.ModId))
7057
{
7158
continue;
7259
}
@@ -80,8 +67,23 @@ private void Start()
8067
Debug.LogException(e);
8168
}
8269
}
83-
Debug.Log("Already");
84-
}
70+
71+
Debug.Log("Early load finished");
72+
}
73+
74+
/// <summary>
75+
/// This method starts reading mods and runs automatically when this class is created after the Awake method.
76+
/// </summary>
77+
private void Start()
78+
{
79+
Debug.Log("Loading mods");
80+
foreach (SFSMod mod in _modsLoaded)
81+
{
82+
mod.load();
83+
}
84+
85+
Debug.Log("Loading finished");
86+
}
8587

8688
/// <summary>
8789
/// get mod instance.
@@ -90,31 +92,23 @@ private void Start()
9092
/// <returns>instance mod</returns>
9193
public SFSMod getMod(string modId)
9294
{
93-
if (this._modList.ContainsKey(modId))
94-
{
95-
return this._modList[modId];
96-
}
97-
return null;
95+
this._modList.TryGetValue(modId, out SFSMod result);
96+
return result;
9897
}
9998

10099
/// <summary>
101100
/// Get a list of all loaded mods
102101
/// </summary>
103-
/// <returns> loaded mods </returns>
102+
/// <returns> Loaded mods </returns>
104103
public SFSMod[] getMods()
105104
{
106-
List<SFSMod>mods = new List<SFSMod>();
107-
foreach(SFSMod mod in this._modList.Values)
108-
{
109-
mods.Add(mod);
110-
}
111-
return mods.ToArray();
105+
return _modList.Values.ToArray();
112106
}
113107

114108
/// <summary>
115109
/// This method reads the MODS folder and identifies what the entry point is for each folder.
116110
/// </summary>
117-
/// <returns> dictionary with modid as key and SFSMod instance as value</returns>
111+
/// <returns> Dictionary with modId as key and SFSMod instance as value</returns>
118112
private Dictionary<string, SFSMod> getModList()
119113
{
120114
Dictionary<string, SFSMod> modList = new Dictionary<string, SFSMod>();
@@ -135,23 +129,21 @@ private Dictionary<string, SFSMod> getModList()
135129
// check if you have SFSMod class
136130
foreach (Type typeClass in assembly.GetTypes())
137131
{
138-
if (typeClass.IsSubclassOf(typeof(SFSMod)))
132+
if (typeof(SFSMod).IsAssignableFrom(typeClass))
139133
{
140134
mod = (Activator.CreateInstance(typeClass) as SFSMod);
141135
break;
142136
}
143-
144137
}
145138

146139
if (mod == null)
147140
{
148-
throw new Exception(folder.FolderName+" entry point not found");
149-
141+
throw new Exception(folder.FolderName + " entry point not found");
150142
}
151143

152144
if (modList.ContainsKey(mod.ModId))
153145
{
154-
throw new Exception("There is already another mod with id 12 " + mod.ModId);
146+
throw new Exception("There is already another mod with id " + mod.ModId);
155147
}
156148

157149
modList.Add(mod.ModId, mod);
@@ -160,33 +152,29 @@ private Dictionary<string, SFSMod> getModList()
160152
{
161153
Debug.LogException(e);
162154
}
163-
164155
}
156+
165157
return modList;
166158
}
167159

168160
/// <summary>
169-
///Load the mod dependencies and check if it is already loaded and check its version.
161+
/// Load the mod dependencies and check if it is already loaded and check its version.
170162
/// </summary>
171163
/// <param name="dependencies"> lista de dependencias que necesitas cargar primero</param>
172164
/// <returns> verdadero si se han cargado todas las dependencias</returns>
173165
private bool loadDependencies(Dictionary<string, string[]> dependencies)
174166
{
175-
176167
// for each mod dependency
177168
foreach (var item in dependencies)
178169
{
179-
// is there a mod in the list?
180-
if (this._modList.ContainsKey(item.Key))
170+
// is the dependency present
171+
if (this._modList.TryGetValue(item.Key, out var dependency))
181172
{
182-
// get mod dependencies
183-
SFSMod dependencieMod = this._modList[item.Key];
184-
185173
// check if the dependency version is the same as the one needed by the mod
186174
bool versionFlag = false;
187-
foreach(string version in item.Value)
175+
foreach (string version in item.Value)
188176
{
189-
if(verifyVersion(dependencieMod.Version, version))
177+
if (verifyVersion(dependency.Version, version))
190178
{
191179
versionFlag = true;
192180
break;
@@ -197,30 +185,31 @@ private bool loadDependencies(Dictionary<string, string[]> dependencies)
197185
if (versionFlag)
198186
{
199187
// start load dependency first
200-
this.loadMod(dependencieMod);
188+
this.loadMod(dependency);
201189
continue;
202190
}
203191
}
192+
204193
// dependency does not exist or is a different version
205-
throw new Exception("Is necesary install " + item.Key +" "+ string.Join(", ", item.Value));
194+
throw new Exception("Is necesary install " + item.Key + " " + string.Join(", ", item.Value));
206195
}
196+
207197
return true;
208198
}
209199

210200
/// <summary>
211-
/// start checking mod version and run load method
201+
/// Start checking mod version and run load method
212202
/// </summary>
213-
/// <param name="mod">mod to start load</param>
203+
/// <param name="mod">Mod to start load</param>
214204
private void loadMod(SFSMod mod)
215205
{
216206
// has this mod been loaded?, if it does not start loading
217-
if (this._modsLoaded.Contains(mod.ModId))
218-
{
207+
if (this._modsLoaded.Any(m => m.ModId == mod.ModId))
208+
{
219209
return;
220210
}
221211

222212
Debug.Log("Loading " + mod.Name);
223-
this._modsLoaded.Add(mod.ModId);
224213

225214
// check if the version is valid for this modloader version
226215
if (verifyVersion(mod.ModLoderVersion, modLoderVersion))
@@ -231,22 +220,23 @@ private void loadMod(SFSMod mod)
231220
// charge them
232221
this.loadDependencies(mod.Dependencies);
233222
}
234-
223+
235224
mod.loadAssets();
236-
mod.load();
225+
mod.early_load();
226+
this._modsLoaded.Add(mod);
237227
return;
238228
}
239229

240230
//mod loader version is invalid
241-
throw new Exception(mod.Name + " need ModLoader " + mod.Version);
231+
throw new Exception(mod.Name + " need ModLoader " + mod.ModLoderVersion);
242232
}
243233

244234
/// <summary>
245-
/// check the string of two versions to identify if they are the same
235+
/// Check the string of two versions to identify if they are the same
246236
/// </summary>
247237
/// <param name="version1"> version to check</param>
248238
/// <param name="version2"> verison to check</param>
249-
/// <returns>true if they are valid versions</returns>
239+
/// <returns>True if they are valid versions</returns>
250240
private bool verifyVersion(string version1, string version2)
251241
{
252242
Regex rx = new Regex(@"\bv([0-9]|[1-9][0-9]).([0-9]|[1-9][0-9]|x).([0-9]|[1-9][0-9]|x)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
@@ -280,7 +270,7 @@ private bool verifyVersion(string version1, string version2)
280270

281271
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
282272
{
283-
Debug.Log("Scene change to "+scene.name);
273+
Debug.Log("Scene change to " + scene.name);
284274
}
285275

286276
/// <summary>
@@ -293,5 +283,4 @@ public static void Main(string[] args)
293283
patcher.PatchAll();
294284
}
295285
}
296-
297286
}

ModLoader/Patcher.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using HarmonyLib;
1+
using HarmonyLib;
22
using SFS;
33
using System;
44
using UnityEngine;
@@ -19,17 +19,16 @@ public static void Postfix(BaseAssigner __instance)
1919
GameObject loader = new GameObject("ModLoader");
2020
Loader.root = loader;
2121
Console.root = modConsole;
22-
loader.AddComponent<Loader>();
2322
modConsole.AddComponent<Console>();
23+
modConsole.SetActive(true);
24+
loader.AddComponent<Loader>();
2425
modConsole.AddComponent<Helper>();
2526
//modConsole.AddComponent<ModsMenu>();
2627

2728
UnityEngine.Object.DontDestroyOnLoad(modConsole);
2829
UnityEngine.Object.DontDestroyOnLoad(loader);
2930

3031
loader.SetActive(true);
31-
modConsole.SetActive(true);
3232
}
33-
3433
}
3534
}

ModLoader/SFSMod.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public string Description
6969
/// <summary>
7070
/// Get the list of mods you need to run.
7171
/// </summary>
72-
public Dictionary<string, string[] > Dependencies
72+
public Dictionary<string, string[]> Dependencies
7373
{
7474
get { return this._dependencies; }
7575
}
@@ -82,8 +82,7 @@ public AssetBundle Assets
8282
get { return this._assets; }
8383
}
8484

85-
86-
protected SFSMod(string id,string name, string author, string modLoderVersion, string version, string description = "", string assetsFilename = null , Dictionary<string, string[]> dependencies = null )
85+
protected SFSMod(string id, string name, string author, string modLoderVersion, string version, string description = "", string assetsFilename = null, Dictionary<string, string[]> dependencies = null)
8786
{
8887
_modId = id;
8988
_name = name;
@@ -100,7 +99,7 @@ protected SFSMod(string id,string name, string author, string modLoderVersion, s
10099
/// </summary>
101100
public void loadAssets()
102101
{
103-
if(this._assetsFilename == null || this._assetsFilename == "")
102+
if (string.IsNullOrEmpty(this._assetsFilename))
104103
{
105104
return;
106105
}
@@ -111,11 +110,18 @@ public void loadAssets()
111110
{
112111
throw new Exception("Assets file not found");
113112
}
113+
114114
this._assets = assets;
115-
116-
Debug.Log(this._name+" assets loaded!");
115+
116+
Debug.Log(this._name + " assets loaded!");
117117
}
118118

119+
/// <summary>
120+
/// This method is called before Base's components are fully initialized allowing for hooking in their initialization
121+
/// </summary>
122+
public virtual void early_load()
123+
{
124+
}
119125

120126
/// <summary>
121127
/// This method is called for modloader to start your mod
@@ -126,6 +132,5 @@ public void loadAssets()
126132
/// This method is called for modloader to remove your mod.
127133
/// </summary>
128134
public abstract void unload();
129-
130135
}
131136
}

0 commit comments

Comments
 (0)