Skip to content

Commit ea16a31

Browse files
authored
Merge pull request #6 from Balint66/main
Add manifest.json for better organization and dependency management. Adding more reliable way to "patch" repositories. Reworked logging and log screen.
2 parents d7273e0 + 43976ef commit ea16a31

25 files changed

Lines changed: 1177 additions & 210 deletions

AdditionalMapSizesPlugin/AdditionalMapSizesPlugin.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using GoingMedievalModLauncher;
2+
using GoingMedievalModLauncher.plugins;
23
using HarmonyLib;
34
using NSEipix.Base;
45
using NSMedieval.Model.MapNew;

BugReportDisablerPlugin/BugReportDisablerPlugin.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using GoingMedievalModLauncher;
3+
using GoingMedievalModLauncher.plugins;
34
using HarmonyLib;
45
using NSMedieval.Tools;
56
using NSMedieval.Tools.Debug;

CameraSettingsPlusPlusPlugin/CameraSettingsPlusPlus.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using GoingMedievalModLauncher;
3+
using GoingMedievalModLauncher.plugins;
34
using HarmonyLib;
45
using NSEipix.Base;
56
using NSMedieval;

DeveloperConsoleEnablerPlugin/DeveloperConsoleEnablerPlugin.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using GoingMedievalModLauncher;
2+
using GoingMedievalModLauncher.plugins;
23
using HarmonyLib;
34
using NSEipix.Base;
45
using NSMedieval.DevConsole;

GoingMedievalModLauncher.sln

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
Microsoft Visual Studio Solution File, Format Version 12.00
1+
Microsoft Visual Studio Solution File, Format Version 12.00
32
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GoingMedievalModLauncher", "GoingMedievalModLauncher\GoingMedievalModLauncher.csproj", "{91AECC71-AB38-4E60-BC8D-34263B818A39}"
43
EndProject
54
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BugReportDisablerPlugin", "BugReportDisablerPlugin\BugReportDisablerPlugin.csproj", "{6019B364-210A-45AB-9E74-192953A87867}"
@@ -10,8 +9,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeveloperConsoleEnablerPlug
109
EndProject
1110
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdditionalMapSizesPlugin", "AdditionalMapSizesPlugin\AdditionalMapSizesPlugin.csproj", "{E4B8324F-9744-441D-A1CD-D3D74594EFEC}"
1211
EndProject
13-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DummyPlugin", "DummyPlugin\DummyPlugin.csproj", "{7E2FAB97-5DB2-4683-A07D-3C35E5ED97C0}"
14-
EndProject
1512
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExperimentalMod", "ExperimentalMod\ExperimentalMod.csproj", "{AFB3E9BE-3087-4033-8B07-E5F18D99A25F}"
1613
EndProject
1714
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleMenuMod", "ExampleMenuMod\ExampleMenuMod.csproj", "{EDE0D18F-D050-437B-8A12-D6593EE7168E}"
@@ -44,16 +41,14 @@ Global
4441
{E4B8324F-9744-441D-A1CD-D3D74594EFEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
4542
{E4B8324F-9744-441D-A1CD-D3D74594EFEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
4643
{E4B8324F-9744-441D-A1CD-D3D74594EFEC}.Release|Any CPU.Build.0 = Release|Any CPU
47-
{7E2FAB97-5DB2-4683-A07D-3C35E5ED97C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
48-
{7E2FAB97-5DB2-4683-A07D-3C35E5ED97C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
49-
{7E2FAB97-5DB2-4683-A07D-3C35E5ED97C0}.Release|Any CPU.Build.0 = Release|Any CPU
5044
{AFB3E9BE-3087-4033-8B07-E5F18D99A25F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51-
{AFB3E9BE-3087-4033-8B07-E5F18D99A25F}.Debug|Any CPU.Build.0 = Debug|Any CPU
5245
{AFB3E9BE-3087-4033-8B07-E5F18D99A25F}.Release|Any CPU.ActiveCfg = Release|Any CPU
5346
{AFB3E9BE-3087-4033-8B07-E5F18D99A25F}.Release|Any CPU.Build.0 = Release|Any CPU
5447
{EDE0D18F-D050-437B-8A12-D6593EE7168E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
5548
{EDE0D18F-D050-437B-8A12-D6593EE7168E}.Debug|Any CPU.Build.0 = Debug|Any CPU
5649
{EDE0D18F-D050-437B-8A12-D6593EE7168E}.Release|Any CPU.ActiveCfg = Release|Any CPU
5750
{EDE0D18F-D050-437B-8A12-D6593EE7168E}.Release|Any CPU.Build.0 = Release|Any CPU
51+
{7E2FAB97-5DB2-4683-A07D-3C35E5ED97C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
52+
{7E2FAB97-5DB2-4683-A07D-3C35E5ED97C0}.Release|Any CPU.Build.0 = Release|Any CPU
5853
EndGlobalSection
5954
EndGlobal

GoingMedievalModLauncher.sln.DotSettings.user

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=E_003A_005CPrograms_005CGoingMedievalModLauncher_005Clibs_005CUnityEngine_002EInputModule_002Edll/@EntryIndexedValue">True</s:Boolean>
2424
<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=E_003A_005CPrograms_005CGoingMedievalModLauncher_005Clibs_005CUnityEngine_002EUI_002Edll/@EntryIndexedValue">True</s:Boolean>
2525
<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue">&lt;AssemblyExplorer&gt;&#xD;
26-
&lt;Assembly Path="C:\dev\steam\steamapps\common\Going Medieval\Going Medieval_Data\Managed\Assembly-CSharp.dll" /&gt;&#xD;
2726
&lt;Assembly Path="E:\Programs\GoingMedievalModLauncher\GoingMedievalModLauncher\libs\0Harmony.dll" /&gt;&#xD;
28-
&lt;Assembly Path="E:\Programs\GoingMedievalModLauncher\libs\Assembly-CSharp.dll" /&gt;&#xD;
29-
&lt;/AssemblyExplorer&gt;</s:String></wpf:ResourceDictionary>
27+
&lt;Assembly Path="C:\dev\steam\steamapps\common\Going Medieval\Mods\Assembly-CSharp.dll" /&gt;&#xD;
28+
&lt;/AssemblyExplorer&gt;</s:String>
29+
<s:Boolean x:Key="/Default/UnloadedProject/UnloadedProjects/=7e2fab97_002D5db2_002D4683_002Da07d_002D3c35e5ed97c0_0023DummyPlugin/@EntryIndexedValue">True</s:Boolean>
30+
<s:Boolean x:Key="/Default/UnloadedProject/UnloadedProjects/=990eeee2_002Da801_002D4ee9_002Db89a_002De53192219438_0023common/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

GoingMedievalModLauncher/GoingMedievalModLauncher.csproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
4646
<HintPath>$(GameDir)\Going Medieval_Data\Managed\UnityEngine.dll</HintPath>
4747
</Reference>
48+
<Reference Include="UnityEngine.AssetBundleModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
49+
<HintPath>$(GameDir)\Going Medieval_Data\Managed\UnityEngine.AssetBundleModule.dll</HintPath>
50+
</Reference>
4851
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
4952
<HintPath>$(GameDir)\Going Medieval_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
5053
</Reference>
@@ -57,18 +60,30 @@
5760
<Reference Include="UnityEngine.InputModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
5861
<HintPath>$(GameDir)\Going Medieval_Data\Managed\UnityEngine.InputModule.dll</HintPath>
5962
</Reference>
63+
<Reference Include="UnityEngine.PhysicsModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
64+
<HintPath>$(GameDir)\Going Medieval_Data\Managed\UnityEngine.PhysicsModule.dll</HintPath>
65+
</Reference>
6066
<Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
6167
<HintPath>$(GameDir)\Going Medieval_Data\Managed\UnityEngine.UI.dll</HintPath>
6268
</Reference>
6369
</ItemGroup>
6470
<ItemGroup>
6571
<Compile Include="Properties\AssemblyInfo.cs" />
72+
<Compile Include="src\engine\DebbugingPatches.cs" />
73+
<Compile Include="src\engine\DynamicRepositoryBuilder.cs" />
6674
<Compile Include="src\engine\EngineLauncher.cs" />
75+
<Compile Include="src\engine\LocalizationControllerPatch.cs" />
76+
<Compile Include="src\engine\PrefabRepositoryPatch.cs" />
77+
<Compile Include="src\engine\RepositoryPatch.cs" />
78+
<Compile Include="src\engine\RoomTypePatch.cs" />
6779
<Compile Include="src\Launcher.cs" />
80+
<Compile Include="src\MonoScripts\BarrelBuildableView.cs" />
6881
<Compile Include="src\plugins\IPlugin.cs" />
82+
<Compile Include="src\plugins\ManifestClass.cs" />
6983
<Compile Include="src\plugins\PluginComponent.cs" />
7084
<Compile Include="src\plugins\PluginContainer.cs" />
7185
<Compile Include="src\plugins\PluginManager.cs" />
86+
<Compile Include="src\plugins\PluginTree.cs" />
7287
<Compile Include="src\ui\MainMenuPatch.cs" />
7388
<Compile Include="src\ui\ModManagerLogWindow.cs" />
7489
<Compile Include="src\ui\ModManagerWindow.cs" />
@@ -85,6 +100,12 @@
85100
<ItemGroup>
86101
<Folder Include="dist\mods" />
87102
</ItemGroup>
103+
<ItemGroup>
104+
<ProjectReference Include="..\GoingMedievalModLauncherUnityCompat\GoingMedievalModLauncherUnityCompat.csproj">
105+
<Project>{52a9e915-fdd0-4151-8ec6-31672de478bf}</Project>
106+
<Name>GoingMedievalModLauncherUnityCompat</Name>
107+
</ProjectReference>
108+
</ItemGroup>
88109
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
89110
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
90111
Other similar extension points exist, see Microsoft.Common.targets.
Lines changed: 109 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
using System;
22
using System.Reflection;
3-
using System.Threading;
3+
using System.Reflection.Emit;
4+
using GoingMedievalModLauncher.Engine;
5+
using GoingMedievalModLauncher.plugins;
46
using GoingMedievalModLauncher.ui;
57
using HarmonyLib;
68
using 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;
915
using UnityEngine;
1016
using UnityEngine.SceneManagement;
17+
using Object = UnityEngine.Object;
1118

1219
namespace 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

Comments
 (0)