Skip to content

Commit

Permalink
Slight changes to make patches easier to handle
Browse files Browse the repository at this point in the history
- Patches will now load automatically without having to reference them in a list.

- The game will now check if the required patches folder exists before loading.

- Paths have been moved to the Master class to keep consistency with the rest of the code.
  • Loading branch information
Byte-Nova committed Sep 6, 2024
1 parent 8a58643 commit c90933f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 55 deletions.
52 changes: 0 additions & 52 deletions Source/Client/Core/LoadAllMods.cs

This file was deleted.

7 changes: 6 additions & 1 deletion Source/Client/Core/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public static class RimworldTogether
static RimworldTogether()
{
ApplyHarmonyPathches();
LoadAllMods.LoadAllPatchAssemblies();

PrepareCulture();
PreparePaths();
Expand All @@ -29,6 +28,8 @@ static RimworldTogether()
FactionValues.SetPlayerFactionDefs();
CaravanManagerHelper.SetCaravanDefs();
PreferenceManager.LoadClientPreferences();

CompatibilityManager.LoadAllPatchedAssemblies();
}
}

Expand All @@ -50,13 +51,17 @@ public static void PreparePaths()
{
Master.mainPath = GenFilePaths.SaveDataFolderPath;
Master.modFolderPath = Path.Combine(Master.mainPath, "RimWorld Together");

Master.modAssemblyPath = Directory.GetParent(Assembly.GetExecutingAssembly().Location).ToString();
Master.compatibilityPatchesFolderPath = Path.Combine(Master.modAssemblyPath, "Patches");

Master.connectionDataPath = Path.Combine(Master.modFolderPath, "ConnectionData.json");
Master.clientPreferencesPath = Path.Combine(Master.modFolderPath, "Preferences.json");
Master.loginDataPath = Path.Combine(Master.modFolderPath, "LoginData.json");
Master.savesFolderPath = GenFilePaths.SavedGamesFolderPath;

if (!Directory.Exists(Master.modFolderPath)) Directory.CreateDirectory(Master.modFolderPath);
if (!Directory.Exists(Master.compatibilityPatchesFolderPath)) Directory.CreateDirectory(Master.compatibilityPatchesFolderPath);
}

public static void CreateUnityDispatcher()
Expand Down
6 changes: 5 additions & 1 deletion Source/Client/Core/Master.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ public static class Master

public static ModConfigs modConfigs = new ModConfigs();

public static Dictionary<string,Assembly> loadedPatches = new Dictionary<string,Assembly>();
public static Dictionary<string, Assembly> loadedCompatibilityPatches = new Dictionary<string,Assembly>();

//Paths

public static string mainPath;

public static string modFolderPath;

public static string modAssemblyPath;

public static string compatibilityPatchesFolderPath;

public static string connectionDataPath;

public static string loginDataPath;
Expand Down
56 changes: 56 additions & 0 deletions Source/Client/Managers/CompatibilityManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Verse;

namespace GameClient
{
public static class CompatibilityManager
{
public static void LoadAllPatchedAssemblies()
{
string[] allCompatibilitiesToLoad = CompatibilityManagerHelper.GetAllPatchedMods();

foreach (string compatibility in allCompatibilitiesToLoad)
{
string compatibilityName = Path.GetFileNameWithoutExtension(compatibility);

if (LoadedModManager.RunningModsListForReading.Any(mod => mod.Name == compatibilityName))
{
try
{
Assembly assembly = Assembly.LoadFrom(compatibility);
Type toUse = typeof(CompatibilityManager);

MethodInfo methodInfo = toUse.GetMethod(compatibilityName, BindingFlags.NonPublic | BindingFlags.Static);
methodInfo.Invoke(compatibilityName, null);

Master.loadedCompatibilityPatches.Add(compatibilityName, assembly);
Logger.Message($"Loaded patch for '{compatibilityName}'");
}
catch (Exception ex){ Logger.Error($"Failed to load patch for '{compatibilityName}' because :\n{ex}"); }
}
}
}

//Entry point for the soon-to-come SOS2 patch

private static void SOS2Patch()
{
Logger.Warning("Loaded!");
}
}

public static class CompatibilityManagerHelper
{
public static readonly string fileExtension = ".dll";

public static string[] GetAllPatchedMods()
{
return Directory.GetFiles(Master.compatibilityPatchesFolderPath)
.Where(fetch => fetch.EndsWith(fileExtension)).ToArray();
}
}
}
2 changes: 1 addition & 1 deletion Source/Shared/Misc/CommonValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Shared
{
public static class CommonValues
{
public readonly static string executableVersion = "24.9.1.1";
public readonly static string executableVersion = "dev";
}
}

0 comments on commit c90933f

Please sign in to comment.