1- using SFS ;
2- using HarmonyLib ;
1+ using HarmonyLib ;
32using UnityEngine ;
43using SFS . IO ;
54using System . Collections . Generic ;
65using System ;
7- using System . Linq . Expressions ;
8- using SFS . Builds ;
96using System . Reflection ;
107using UnityEngine . SceneManagement ;
118using UnityEngine . Events ;
9+ using System . IO ;
10+ using System . Text . RegularExpressions ;
1211
1312namespace ModLoader
1413{
@@ -18,58 +17,68 @@ namespace ModLoader
1817 public class Loader : MonoBehaviour
1918 {
2019
20+ // This save Loader instance
21+ public static Loader main ;
2122
22- // This is de main logger, use this to show message in console
23- public static ModConsole logger ;
23+ // This save the gameObject that implement Loader class
24+ public static GameObject root ;
2425
25- private static Harmony patcher ;
26+ private static FolderPath _modsFolder ;
2627
27- // Thiss save Loader instance
28- public static Loader modLoader ;
28+ private const string modLoderVersion = "v1.1.0" ;
2929
30- // This save the gameObject that implement Loader class
31- public static GameObject root ;
32- public static BaseAssigner baseAssigner ;
30+ public static FolderPath ModsFolder
31+ {
32+ get
33+ {
34+ return _modsFolder ;
35+ }
36+ }
3337
3438 // List of all mods loaded in the folder MODS
35- private SFSMod [ ] modList ;
39+ private SFSMod [ ] _modList ;
40+ /*public SFSMod[] ModList
41+ {
42+ get
43+ }*/
3644
37- public Loader ( )
45+ private void Awake ( )
3846 {
39- Loader . logger = new ModConsole ( ) ;
40- Loader . modLoader = this ;
47+ Loader . main = this ;
48+ this . _modList = new SFSMod [ ] { } ;
49+ this . suscribeOnChangeScene ( this . OnSceneLoaded ) ;
50+ _modsFolder = FileLocations . BaseFolder . Extend ( "MODS" ) . CreateFolder ( ) ;
4151 }
4252
43- public void Awake ( )
53+ private void Start ( )
4454 {
45- this . modList = new SFSMod [ ] { } ;
46- Loader . logger . log ( "Loading Mods" , "ModLoader" ) ;
47-
48- // If not existe is created and pass FolderPath
49- FolderPath modFolder = FileLocations . BaseFolder . Extend ( "MODS" ) . CreateFolder ( ) ;
50-
51- // get list of files into MODS folder
52- IEnumerable < FilePath > files = modFolder . GetFilesInFolder ( false ) ;
53- foreach ( FilePath file in files )
55+ IEnumerable < FolderPath > modsFolders = _modsFolder . GetFoldersInFolder ( false ) ;
56+ string basePath = Path . Combine ( FileLocations . BaseFolder , "MODS" ) ;
57+ Debug . Log ( "Reading Mods" ) ;
58+ Debug . LogError ( new Exception ( "Error custom" ) ) ;
59+
60+ foreach ( FolderPath folder in modsFolders )
5461 {
55- // get only dll files in mods folder
56- if ( file . Extension == "dll" )
62+ string fileModPath = Path . Combine ( basePath , folder . FolderName , folder . FolderName + ".dll" ) ;
63+ try
64+ {
65+ SFSMod mod = this . loadMod ( fileModPath ) ;
66+ this . _modList . AddItem ( mod ) ;
67+ Debug . Log ( mod . Name + " Loaded" ) ;
68+ }
69+ catch ( Exception e )
5770 {
58- Loader . logger . log ( "Reading " + file . FileName , "ModLoader" ) ;
59- // try to load mod
60- this . loadMod ( modFolder . GetRelativePath ( file . FileName ) + "/" + file . FileName ) ;
71+ Debug . LogError ( "Error Reading " + folder . FolderName + " folder" ) ;
72+ Debug . LogError ( e ) ;
6173 }
74+
6275 }
76+
6377 }
6478
65- public SFSMod [ ] getModList ( )
66- {
67- return this . modList ;
68- }
69-
70- void OnEnable ( )
79+ private void OnSceneLoaded ( Scene scene , LoadSceneMode mode )
7180 {
72- SceneManager . sceneLoaded += OnSceneLoaded ;
81+ Debug . Log ( "Scene change to " + scene . name ) ;
7382 }
7483
7584 public bool suscribeOnChangeScene ( UnityAction < Scene , LoadSceneMode > method )
@@ -78,59 +87,80 @@ public bool suscribeOnChangeScene(UnityAction<Scene, LoadSceneMode> method)
7887 return true ;
7988 }
8089
81- void OnSceneLoaded ( Scene scene , LoadSceneMode mode )
82- {
83- logger . log ( "scene change to " + scene . name , "ModLoader" ) ;
84- }
85-
86- private void loadMod ( string path )
90+ private SFSMod loadMod ( string path )
8791 {
8892 Assembly assembly = Assembly . LoadFrom ( path ) ;
8993 SFSMod mod = null ;
90- logger . log ( "Searching SFSMod interface" , "ModLoader" ) ;
91- // we search SFSMod interface in dll file dounede in MODS folder
94+
9295 foreach ( Type typeClass in assembly . GetTypes ( ) )
9396 {
94- Type inteface = typeClass . GetInterface ( typeof ( SFSMod ) . Name ) ;
95- if ( inteface == null )
97+ if ( typeClass . IsSubclassOf ( typeof ( SFSMod ) ) )
9698 {
97- continue ;
99+ mod = ( Activator . CreateInstance ( typeClass ) as SFSMod ) ;
100+ break ;
98101 }
99- mod = ( Activator . CreateInstance ( typeClass ) as SFSMod ) ;
102+
100103 }
101-
102- if ( mod == null )
104+
105+ if ( mod == null )
103106 {
104- logger . log ( "File " + path + " don't have SFSMod interface" , "ModLoader" ) ;
105- return ;
107+ throw new Exception ( "SFSmod class not found" ) ;
106108 }
107109
108- Loader . logger . log ( "SFSMod interface found" , "ModLoader" ) ;
109-
110- try
110+ if ( verifyModLoaderVersion ( mod . ModLoderVersion ) )
111111 {
112- logger . log ( "Loading " + mod . getModName ( ) , mod . getModAuthor ( ) ) ;
113-
114- // execute entry point of mod
112+ mod . loadAssets ( ) ;
115113 mod . load ( ) ;
116- logger . log ( "Loaded " + mod . getModName ( ) , mod . getModAuthor ( ) ) ;
117- this . modList . AddItem ( mod ) ;
114+
115+ return mod ;
118116 }
119- catch ( Exception e )
117+ throw new Exception ( mod . Name + " need ModLoader " + mod . Version ) ;
118+ }
119+
120+ /// <summary>
121+ /// get if the mod have a valid version format and valid version for this modloader version
122+ /// </summary>
123+ /// <param name="version"> mod version</param>
124+ /// <returns>true if valid version</returns>
125+ private bool verifyModLoaderVersion ( string version )
126+ {
127+ 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 ) ;
128+ // have the formal v1.x.x
129+ if ( rx . IsMatch ( version ) )
120130 {
121- logger . log ( "Error loading " + mod . getModName ( ) , mod . getModAuthor ( ) ) ;
122- logger . logError ( e ) ;
131+ string [ ] modVersion = version . Split ( '.' ) ;
132+ string [ ] target = modLoderVersion . Split ( '.' ) ;
133+
134+ if ( modVersion . Length == target . Length )
135+ {
136+ for ( short index = 0 ; index < target . Length ; index ++ )
137+ {
138+ if ( modVersion [ index ] == "x" )
139+ {
140+ continue ;
141+ }
142+ if ( modVersion [ index ] != target [ index ] )
143+ {
144+ return false ;
145+ }
146+ }
147+ // have the format and is valid version for this modloader version
148+ return true ;
149+
150+ }
151+
123152 }
124- }
153+ return false ;
154+ }
125155
126156 /// <summary>
127157 /// This is the mod loader entry point, this is the method execute after be injected in the game
128158 /// </summary>
129159 /// <param name="args"></param>
130160 public static void Main ( string [ ] args )
131161 {
132- Loader . patcher = new Harmony ( "SFS.mod.loader" ) ;
133- Loader . patcher . PatchAll ( ) ;
162+ Harmony patcher = new Harmony ( "SFS.mod.loader" ) ;
163+ patcher . PatchAll ( ) ;
134164 }
135165 }
136166
0 commit comments