1- using HarmonyLib ;
1+ using HarmonyLib ;
22using UnityEngine ;
33using SFS . IO ;
44using System . Collections . Generic ;
5+ using System . Linq ;
56using System ;
67using System . Reflection ;
78using UnityEngine . SceneManagement ;
89using UnityEngine . Events ;
910using System . IO ;
1011using System . Text . RegularExpressions ;
11- using SFS . UI ;
12- using SFS . Translations ;
13- using SFS . Input ;
1412
1513namespace 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}
0 commit comments