Skip to content

Commit 82491ca

Browse files
author
Balint66
committed
Merge experimental fixes into main
2 parents 2ed891e + 28a4808 commit 82491ca

File tree

12 files changed

+128
-52
lines changed

12 files changed

+128
-52
lines changed

GoingMedievalModLauncher/GoingMedievalModLauncher.csproj

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
<ItemGroup>
2323
<Compile Include="Properties\AssemblyInfo.cs" />
2424
<Compile Include="src\buildings\BuildingBase.cs" />
25-
<Compile Include="src\engine\DebbugingPatches.cs" />
2625
<Compile Include="src\engine\DynamicRepositoryBuilder.cs" />
2726
<Compile Include="src\engine\EngineLauncher.cs" />
2827
<Compile Include="src\engine\LocalizationControllerPatch.cs" />
@@ -51,13 +50,6 @@
5150
<ItemGroup>
5251
<Folder Include="dist\mods" />
5352
</ItemGroup>
54-
<ItemGroup>
55-
<None Include="packages.config" />
56-
</ItemGroup>
57-
<ItemGroup>
58-
<PackageReference Include="NLog" Version="4.7.10" />
59-
<PackageReference Include="Lib.Harmony" Version="2.1.0" />
60-
</ItemGroup>
6153
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6254
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
6355
Other similar extension points exist, see Microsoft.Common.targets.
-752 KB
Binary file not shown.

GoingMedievalModLauncher/packages.config

Lines changed: 0 additions & 4 deletions
This file was deleted.

GoingMedievalModLauncher/src/Launcher.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using HarmonyLib;
1111
using NSEipix.Base;
1212
using NSEipix.Repository;
13+
using NSMedieval;
1314
using NSMedieval.Almanac;
1415
using NSMedieval.Crops;
1516
using NSMedieval.GameEventSystem;
@@ -44,12 +45,12 @@ public static JsonRepository<T, M> getRepository<T, M>() where T : JsonRepositor
4445
where M : Model
4546
{
4647
object it = Repositories[typeof(T)];
47-
if ( it as T == null )
48+
if ( it is JsonRepository<T, M> repo )
4849
{
49-
return null;
50+
return repo;
5051
}
5152

52-
return it as JsonRepository<T, M>;
53+
return null;
5354
}
5455

5556
// the entry point of the code execution
@@ -64,11 +65,16 @@ private static void ReplaceComponent<T, M>(FieldInfo[] fieldToCopy = null)
6465
where T : JsonRepository<T, M>
6566
where M : Model
6667
{
68+
var comp = Object.FindObjectOfType<T>();
69+
if ( comp == null )
70+
{
71+
LOGGER.Info($"Unable to find component: {typeof(T).Name}");
72+
return;
73+
}
6774
TypeBuilder b = DynamicRepositoryBuilder<T, M>.GetTypeBuilder(
6875
$"Patched_{typeof(T).Name}");
6976
DynamicRepositoryBuilder<T, M>.OverrideDeserialize(b);
7077
Type t = DynamicRepositoryBuilder<T, M>.CompileResultType(b);
71-
var comp = Object.FindObjectOfType<T>();
7278
object[] variables = new object[0];
7379
if ( fieldToCopy != null )
7480
{
@@ -155,7 +161,7 @@ private static void Startup(Scene arg0, LoadSceneMode arg1)
155161
{
156162
var harmony = new Harmony("com.modloader.nsmeadival");
157163

158-
DebbugingPatches.ApplyPatches(harmony);
164+
//DebbugingPatches.ApplyPatches(harmony);
159165
MainMenuPatch.ApplyPatch(harmony);
160166
RoomTypePatch.ApplyPatch(harmony);
161167
LocalizationControllerPatch.ApplyPatch(harmony);
@@ -173,7 +179,7 @@ private static void Startup(Scene arg0, LoadSceneMode arg1)
173179

174180
LOGGER.Info("Mod-loader is running ...");
175181

176-
Singleton<PluginManager>.Instance.loadAssemblies();
182+
Singleton<PluginManager>.Instance.LoadAssemblies();
177183

178184
// create a gameObject which we can use as a root reference to the scene-graph
179185
var modLoaderObject = new GameObject {name = "ModLoader"};
@@ -189,6 +195,8 @@ private static void Startup(Scene arg0, LoadSceneMode arg1)
189195
throw;
190196
}
191197
//For replacements that are required before the game map
198+
ReplaceComponent<MapRepository, Map>();
199+
LOGGER.Info("The Maptype repository was replaced with a patched one.");
192200
ReplaceComponent<MapSizeRepository, MapSize>();
193201
LOGGER.Info("The Mapsize repository was replaced with a patched one.");
194202
ReplaceComponent<VillageNameRepository, VillageNames>();

GoingMedievalModLauncher/src/MonoScripts/BarrelBuildableView.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
namespace GoingMedievalModLauncher.MonoScripts
88
{
9+
10+
/// <summary>
11+
/// An example class to add buildable objects. WIP
12+
/// </summary>
913
public class BarrelBuildableView : BaseBuildableView<BuildingInstance>, IHideObject
1014
{
1115

GoingMedievalModLauncher/src/engine/EngineLauncher.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ namespace GoingMedievalModLauncher.Engine
1111

1212
// The EngineLauncher gets loads any provided mod from the /mods directory and instantiates the lifecycle of each
1313
// plugin within the unity-engine.
14-
public class EngineLauncher : MonoBehaviour
14+
public class EngineLauncher : MonoSingleton<EngineLauncher>
1515
{
16+
17+
public ModManagerWindow modManagerWindow = null;
18+
1619
public void Awake()
1720
{
1821

@@ -22,7 +25,7 @@ public void Awake()
2225
t.Field("suffix").SetValue(" - mods active");
2326
t.Method("Start").GetValue();
2427

25-
// dont destroy the engines object when loading another scene, etc.
28+
// don't destroy the engines object when loading another scene, etc.
2629
DontDestroyOnLoad(this);
2730

2831
// Load all the mods / plugins / assemblies from the mods directory
@@ -40,7 +43,7 @@ public void Awake()
4043

4144
// Show a fancy ui to display and control every loaded mod at runtime
4245
Launcher.LOGGER.Info("Showing mod-manager window...");
43-
var modManagerWindow = gameObject.AddComponent<ModManagerWindow>();
46+
modManagerWindow = gameObject.AddComponent<ModManagerWindow>();
4447

4548
}
4649

GoingMedievalModLauncher/src/engine/LocalizationControllerPatch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class LocalizationControllerPatch
1010

1111
private static void GetTextPre(ref string key)
1212
{
13-
if ( key.Contains(":") )
13+
if ( key != null && key.Contains(":") )
1414
{
1515
var modS = key.Split('#', ':');
1616
if ( modS.Length == 3 )

GoingMedievalModLauncher/src/plugins/PluginComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace GoingMedievalModLauncher.plugins
44
{
5-
// The PluginComponent is a simple monobehavior which contains a reference to an IPlugin-Instance -
5+
// The PluginComponent is a simple monobehavior which contains a reference to an IPluginContainer-Instance -
66
// this way, a plugin / mod can make use of the native lifecycle of an object within the unity-engine.
77
//
88
// See the IPluginContainer-Class for more detailed information about each method.

GoingMedievalModLauncher/src/plugins/PluginContainer.cs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
using System.Reflection;
66
using GoingMedievalModLauncher.Engine;
77
using GoingMedievalModLauncher.MonoScripts;
8+
using GoingMedievalModLauncher.ui;
89
using I2.Loc;
910
using Newtonsoft.Json;
1011
using NSEipix.Base;
1112
using NSEipix.ObjectMapper;
1213
using NSEipix.Repository;
14+
using NSMedieval;
1315
using NSMedieval.Construction;
1416
using NSMedieval.Crops;
1517
using NSMedieval.Model;
@@ -98,6 +100,9 @@ ContainerState State
98100
/// </summary>
99101
bool CodeOnly { get; }
100102

103+
/// <summary>
104+
/// Indicates that the launcher should not search in the code directory
105+
/// </summary>
101106
bool NoCode { get; }
102107

103108
void Init();
@@ -165,14 +170,13 @@ public ContainerState State
165170
}
166171
}
167172

168-
public bool NoCode { get; }
169-
170173
/// <summary>
171174
/// The backing variable of the State property.
172175
/// </summary>
173176
private ContainerState _state;
174177

175178
public bool CodeOnly { get; }
179+
public bool NoCode { get; }
176180

177181
private event Action<MonoBehaviour> OnDeinit;
178182
private event Action OnInit;
@@ -217,6 +221,7 @@ private PluginContainer(DirectoryInfo path, ManifestClass manifest)
217221

218222
if ( !CodeOnly )
219223
{
224+
RegisterJsonRepositoryLoader<MapRepository, Map>();
220225
RegisterJsonRepositoryLoader<MapSizeRepository, MapSize>();
221226

222227
RegisterJsonRepositoryLoaderWithIdField<ResourceRepository, Resource>("resourceId");
@@ -240,7 +245,7 @@ private PluginContainer(DirectoryInfo path, ManifestClass manifest)
240245
assemblies.Add(assembly);
241246
}
242247

243-
// Check if the assembly is valid (we don't want to load any dll within the mods-directory
248+
// Check if the assembly is valid (we don't want to load any dll within the mods-directory)
244249
Type pluginType = typeof(IPlugin);
245250
List<Type> validPlugins = new List<Type>();
246251

@@ -250,7 +255,7 @@ private PluginContainer(DirectoryInfo path, ManifestClass manifest)
250255
{
251256
if ( assembly != null )
252257
{
253-
Type[] types = new Type[0];
258+
Type[] types = Type.EmptyTypes;
254259
try
255260
{
256261
types = assembly.GetTypes();
@@ -287,12 +292,11 @@ private PluginContainer(DirectoryInfo path, ManifestClass manifest)
287292
}
288293
catch (Exception e)
289294
{
290-
Launcher.LOGGER.Info("Unable to get Plugin's type. Maybe the code is referencing an older launcher?");
291-
Launcher.LOGGER.Info(e.ToString());
292-
295+
Launcher.LOGGER.Error(e,
296+
"Unable to get Plugin's type. Maybe the code is referencing an older launcher?");
293297
}
294298

295-
//TODO: more erros?
299+
//TODO: more errors?
296300
if ( validPlugins.Count == 0 )
297301
{
298302
Launcher.LOGGER.Info("No valid plugin were found for this directory.");
@@ -320,7 +324,7 @@ void I()
320324
}
321325
catch (Exception e)
322326
{
323-
Launcher.LOGGER.Info($"Unable to initalize plugin {Name}: {e}");
327+
Launcher.LOGGER.Error(e,$"Unable to initalize plugin {Name}");
324328
}
325329
}
326330

@@ -329,11 +333,11 @@ void D(MonoBehaviour o)
329333
try
330334
{
331335
instance.disable(o);
332-
// TODO: plugin.start(doorstepGameObject);
336+
// TODO: plugin.stop(doorstepGameObject);
333337
}
334338
catch (Exception e)
335339
{
336-
Launcher.LOGGER.Info($"Unable to disbale plugin {Name}: {e}");
340+
Launcher.LOGGER.Error(e,$"Unable to disbale plugin {Name}");
337341
}
338342
}
339343

@@ -342,7 +346,7 @@ void D(MonoBehaviour o)
342346
}
343347
catch (Exception e)
344348
{
345-
Launcher.LOGGER.Info("An error happened instantiating a plugin!\n" + e);
349+
Launcher.LOGGER.Error(e,"An error happened instantiating a plugin!\n");
346350
}
347351
finally
348352
{
@@ -525,8 +529,9 @@ void Del(T repo)
525529
Launcher.LOGGER.Info($"Added item {item.GetID()} to repository {typeof(T).Name}");
526530
repo.Add(item);
527531
}
528-
529-
repo.Reload();
532+
533+
//DO NOT! RECURSION ALERT!
534+
//repo.Reload();
530535
}
531536

532537
}
@@ -711,7 +716,19 @@ public void Deinit(MonoBehaviour o)
711716

712717
}
713718

714-
private ModLoaderPluginContainer(){}
719+
private ModLoaderPluginContainer()
720+
{
721+
MainMenuPatch.OnMenuStartPost += () =>
722+
{
723+
MainMenuPatch.AddMainMenuButton("ModsButton", "Mods", () =>
724+
{
725+
if(EngineLauncher.Instance.modManagerWindow == null) return;
726+
727+
EngineLauncher.Instance.modManagerWindow.shown = !EngineLauncher.Instance.modManagerWindow.shown;
728+
729+
}, 15);
730+
};
731+
}
715732

716733
}
717734

GoingMedievalModLauncher/src/plugins/PluginManager.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ICollection<PluginContainer> GetPlugins()
2424
/**
2525
* Loads all assemblies by a given path
2626
*/
27-
public ICollection<PluginContainer> loadAssemblies()
27+
public ICollection<PluginContainer> LoadAssemblies()
2828
{
2929
// The directory where all assemblies / plugins / mods have to be stored
3030
DirectoryInfo dir = new DirectoryInfo(@"./mods");
@@ -65,14 +65,13 @@ private void ValidateRequirements()
6565
var p = pc.ToList();
6666
foreach ( var container in p )
6767
{
68-
if ( leaf.value.ID == container.Requirement )
69-
{
70-
tree.Add(new PluginTree.TreeNode(leaf, container));
71-
pc.Remove(container);
72-
}
68+
if ( leaf.value.ID != container.Requirement ) continue;
69+
70+
tree.Add(new PluginTree.TreeNode(leaf, container));
71+
pc.Remove(container);
7372
}
7473
}
75-
} while ( leaves.Equals(tree.leaves));
74+
} while ( !leaves.Equals(tree.leaves));
7675

7776
foreach ( PluginContainer container in pc )
7877
{

0 commit comments

Comments
 (0)