11using System ;
22using System . Reflection ;
3- using System . Threading ;
3+ using System . Reflection . Emit ;
4+ using GoingMedievalModLauncher . Engine ;
5+ using GoingMedievalModLauncher . plugins ;
46using GoingMedievalModLauncher . ui ;
57using HarmonyLib ;
68using NSEipix . Base ;
7- using NSMedieval . Tools . Debug ;
8- using NSMedieval . UI ;
9+ using NSEipix . Repository ;
10+ using NSMedieval . Crops ;
11+ using NSMedieval . Model ;
12+ using NSMedieval . Production ;
13+ using NSMedieval . Repository ;
14+ using NSMedieval . Research ;
915using UnityEngine ;
1016using UnityEngine . SceneManagement ;
17+ using Object = UnityEngine . Object ;
1118
1219namespace GoingMedievalModLauncher
1320{
@@ -18,58 +25,126 @@ namespace GoingMedievalModLauncher
1825 public class Launcher
1926 {
2027 // is the startup of the launcher already done?
21- private static bool startupFinished = false ;
28+ private static bool _startupFinished ;
2229
2330 // the entry point of the code execution
2431 public static void Main ( )
2532 {
2633 // we're hooking into the scene-manager to get a valid callback for executing our code
27- SceneManager . sceneLoaded += startup ;
34+ SceneManager . sceneLoaded += Startup ;
35+ SceneManager . sceneLoaded += delegate ( Scene arg0 , LoadSceneMode mode )
36+ {
37+ if ( arg0 . name == "MainScene" )
38+ {
39+ ReplaceRepositories ( ) ;
40+ }
41+ } ;
42+ }
43+
44+ //Be careful, reflection and lowlevel stuff
45+ private static void ReplaceComponent < T , M > ( FieldInfo [ ] fieldToCopy = null )
46+ where T : JsonRepository < T , M >
47+ where M : Model
48+ {
49+ TypeBuilder b = DynamicRepositoryBuilder < T , M > . GetTypeBuilder (
50+ $ "Patched{ typeof ( T ) . Name } ") ;
51+ DynamicRepositoryBuilder < T , M > . OverrideDeserialize ( b ) ;
52+ Type t = DynamicRepositoryBuilder < T , M > . CompileResultType ( b ) ;
53+ var comp = Object . FindObjectOfType < T > ( ) ;
54+ object [ ] variables = new object [ 0 ] ;
55+ if ( fieldToCopy != null )
56+ {
57+ variables = new object [ fieldToCopy . Length ] ;
58+ for ( int i = 0 ; i < fieldToCopy . Length ; i ++ )
59+ {
60+ variables [ i ] = fieldToCopy [ i ] . GetValue ( comp ) ;
61+ }
62+ }
63+
64+ GameObject go = comp . gameObject ;
65+ Object . DestroyImmediate ( comp ) ;
66+ var newComp = go . AddComponent ( t ) ;
67+
68+ if ( fieldToCopy != null )
69+ for ( int i = 0 ; i < fieldToCopy . Length ; i ++ )
70+ {
71+ fieldToCopy [ i ] . SetValue ( newComp , variables [ i ] ) ;
72+ }
73+
74+ }
75+
76+ //Be careful, reflection and lowlevel stuff
77+ private static void ReplaceRepositories ( )
78+ {
79+
80+ ReplaceComponent < RoomTypeRepository , RoomType > (
81+ new [ ]
82+ {
83+ typeof ( RoomTypeRepository ) . GetField
84+ ( "roomDetectionMaterial" , BindingFlags . Instance | BindingFlags . NonPublic )
85+ } ) ;
86+ Logger . Instance . info ( "The room repository was replaced with a patched one." ) ;
87+ ReplaceComponent < ResearchRepository , ResearchModel > ( ) ;
88+ Logger . Instance . info ( "The research repository was replaced with a patched one." ) ;
89+ ReplaceComponent < CropfieldRepository , Cropfield > ( ) ;
90+ Logger . Instance . info ( "The crop field repository was replaced with a patched one." ) ;
91+ ReplaceComponent < ResourceRepository , Resource > ( ) ;
92+ Logger . Instance . info ( "The resource repository was replaced with a patched one." ) ;
93+ ReplaceComponent < ProductionRepository , Production > ( ) ;
94+ Logger . Instance . info ( "The resource repository was replaced with a patched one." ) ;
2895 }
2996
3097 /*
3198 * The startup of the launcher
3299 * This creates a native unity gameObject
33100 */
34- public static void startup ( Scene arg0 , LoadSceneMode arg1 )
101+ public static void Startup ( Scene arg0 , LoadSceneMode arg1 )
35102 {
36103 Logger . Instance . info ( "Initializing mod-loader!" ) ;
37104
38- if ( startupFinished ) return ;
105+ if ( _startupFinished ) return ;
39106
40- new Thread ( ( ) =>
41- {
42- try
43- {
44-
45- var harmony = new Harmony ( "com.modloader.nsmeadival" ) ;
46-
47- MainMenuPatch . ApplyPatch ( harmony ) ;
107+ //Just be double safe.
108+ SceneManager . sceneLoaded -= Startup ;
48109
49- Singleton < PluginManager > . Instance . loadAssemblies ( ) ;
110+ try
111+ {
112+ var harmony = new Harmony ( "com.modloader.nsmeadival" ) ;
50113
51- // wait a short amount of time in order to let the game initialize itself
52- Thread . Sleep ( 2500 ) ;
53-
114+ DebbugingPatches . ApplyPatches ( harmony ) ;
115+ MainMenuPatch . ApplyPatch ( harmony ) ;
116+ RoomTypePatch . ApplyPatch ( harmony ) ;
117+ LocalizationControllerPatch . ApplyPatch ( harmony ) ;
118+ PrefabRepositoryPatch . ApplyPatch ( harmony ) ;
119+ }
120+ catch ( Exception e )
121+ {
122+ Logger . Instance . info ( "Error happened while loading patches.\n " + e ) ;
123+ throw ;
124+ }
125+
126+ try
127+ {
54128
55- Logger . Instance . info ( "Mod-loader thread is running ..." ) ;
129+ Logger . Instance . info ( "Mod-loader thread is running ..." ) ;
56130
57- // create a gameObject which we can use as a root reference to the scene-graph
58- var modLoaderObject = new GameObject { name = "ModLoader" } ;
59- modLoaderObject . AddComponent < EngineLauncher > ( ) ;
131+ // create a gameObject which we can use as a root reference to the scene-graph
132+ var modLoaderObject = new GameObject { name = "ModLoader" } ;
133+ modLoaderObject . AddComponent < EngineLauncher > ( ) ;
60134
61- // print out a nice little confirmation message that the plugin has been loaded
62- Logger . Instance . info ( "... initialization thread has been finished!" ) ;
63- }
64- catch ( Exception e )
65- {
66- Logger . Instance . info ( "An error occured: \n " ) ;
67- Logger . Instance . info ( e . ToString ( ) ) ;
68- throw ;
69- }
70- } ) . Start ( ) ;
135+ // print out a nice little confirmation message that the plugin has been loaded
136+ Logger . Instance . info ( "... initialization thread has been finished!" ) ;
137+
138+ Singleton < PluginManager > . Instance . loadAssemblies ( ) ;
139+ }
140+ catch ( Exception e )
141+ {
142+ Logger . Instance . info ( "An error occured: \n " ) ;
143+ Logger . Instance . info ( e . ToString ( ) ) ;
144+ throw ;
145+ }
71146
72- startupFinished = true ;
147+ _startupFinished = true ;
73148 }
74149 }
75150}
0 commit comments