From 5a16591bbd42a98b5725aa28876428864125d131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bure=C5=A1?= Date: Fri, 12 Jan 2024 19:20:33 +0100 Subject: [PATCH 1/7] Use the new Patcher API so that the mod can be fully disabled --- .../Directory.Build.props | 11 ----------- .../PatchManager.PreloadPatcher.csproj | 1 + src/PatchManager.PreloadPatcher/Patcher.cs | 17 ++++++++--------- 3 files changed, 9 insertions(+), 20 deletions(-) delete mode 100644 src/PatchManager.PreloadPatcher/Directory.Build.props diff --git a/src/PatchManager.PreloadPatcher/Directory.Build.props b/src/PatchManager.PreloadPatcher/Directory.Build.props deleted file mode 100644 index fd356c8..0000000 --- a/src/PatchManager.PreloadPatcher/Directory.Build.props +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - false - $(PatcherBinPath)/$(MSBuildProjectName) - $(SolutionDir)build/obj/patcher/$(MSBuildProjectName) - - \ No newline at end of file diff --git a/src/PatchManager.PreloadPatcher/PatchManager.PreloadPatcher.csproj b/src/PatchManager.PreloadPatcher/PatchManager.PreloadPatcher.csproj index fb2daa2..c624071 100644 --- a/src/PatchManager.PreloadPatcher/PatchManager.PreloadPatcher.csproj +++ b/src/PatchManager.PreloadPatcher/PatchManager.PreloadPatcher.csproj @@ -5,5 +5,6 @@ + \ No newline at end of file diff --git a/src/PatchManager.PreloadPatcher/Patcher.cs b/src/PatchManager.PreloadPatcher/Patcher.cs index 9999bb0..95546b2 100644 --- a/src/PatchManager.PreloadPatcher/Patcher.cs +++ b/src/PatchManager.PreloadPatcher/Patcher.cs @@ -1,23 +1,23 @@ using BepInEx; -using BepInEx.Logging; using JetBrains.Annotations; using Mono.Cecil; using Mono.Cecil.Cil; +using SpaceWarp.Preload.API; namespace PatchManager.PreloadPatcher; /// /// Preload patcher for the game's AssetProvider. /// -public static class Patcher +[UsedImplicitly] +internal class Patcher : BasePatcher { - [UsedImplicitly] - public static IEnumerable TargetDLLs { get; } = new[] + public override IEnumerable DLLsToPatch { get; } = new[] { "Assembly-CSharp.dll" }; - private static MethodReference MakeGeneric(this MethodReference method, params GenericParameter[] args) + private static MethodReference MakeGeneric(MethodReference method, params GenericParameter[] args) { if (args.Length == 0) { @@ -43,8 +43,7 @@ private static MethodReference MakeGeneric(this MethodReference method, params G /// /// Game assembly containing the AssetProvider class. /// Thrown if the assembly with the replacement method cannot be found. - [UsedImplicitly] - public static void Patch(ref AssemblyDefinition assemblyDefinition) + public override void ApplyPatch(ref AssemblyDefinition assemblyDefinition) { // Now we need to get the assembly with the replacement method AssemblyDefinition coreAssembly = null; @@ -67,7 +66,7 @@ public static void Patch(ref AssemblyDefinition assemblyDefinition) // Remove every single instruction from the body of the methods // Emit call to our extracted method var methodInModule = targetMethod.Module.ImportReference(extractedMethod); - var generic = methodInModule.MakeGeneric(targetMethod.GenericParameters.ToArray()); + var generic = MakeGeneric(methodInModule, targetMethod.GenericParameters.ToArray()); targetMethod.Body.Instructions.Clear(); targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_1)); targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_2)); @@ -75,6 +74,6 @@ public static void Patch(ref AssemblyDefinition assemblyDefinition) targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Call, generic)); targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); - Logger.CreateLogSource("Patch Manager Preload").LogInfo("Pre-patching complete!"); + Logger.LogInfo("Pre-patching complete!"); } } \ No newline at end of file From ab1a50d41baefbf125787ebd05f2555a84a93a06 Mon Sep 17 00:00:00 2001 From: Lexi Date: Sun, 14 Jan 2024 11:14:35 -0500 Subject: [PATCH 2/7] Premonition time --- PatchManager.sln | 10 --- .../PatchManager.Core.csproj | 1 + .../Patches/Preload/AssetProviderPatch.cs | 5 ++ .../PatchManager.PreloadPatcher.csproj | 10 --- src/PatchManager.PreloadPatcher/Patcher.cs | 79 ------------------- .../Properties/AssemblyInfo.cs | 3 - 6 files changed, 6 insertions(+), 102 deletions(-) delete mode 100644 src/PatchManager.PreloadPatcher/PatchManager.PreloadPatcher.csproj delete mode 100644 src/PatchManager.PreloadPatcher/Patcher.cs delete mode 100644 src/PatchManager.PreloadPatcher/Properties/AssemblyInfo.cs diff --git a/PatchManager.sln b/PatchManager.sln index de10baa..2ceb8e6 100644 --- a/PatchManager.sln +++ b/PatchManager.sln @@ -4,8 +4,6 @@ Project("{5F1D0BAA-A518-423E-B128-A97490E64B50}") = "PatchManager", "src\PatchMa EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PatchManager.Core", "src\PatchManager.Core\PatchManager.Core.csproj", "{F14D175D-865C-47E8-BCE8-AA14A39FB084}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PatchManager.PreloadPatcher", "src\PatchManager.PreloadPatcher\PatchManager.PreloadPatcher.csproj", "{8BD0F66F-7C47-477D-B519-0118A9F55428}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PatchManager.SassyPatching", "src\PatchManager.SassyPatching\PatchManager.SassyPatching.csproj", "{38D713EA-A57C-4C0E-9B72-09346D3034EE}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PatchManager.SassyPatching.Tests", "src\PatchManager.SassyPatching.Tests\PatchManager.SassyPatching.Tests.csproj", "{95F3322B-A566-4BAB-8370-20C92EACDBC1}" @@ -48,14 +46,6 @@ Global {F14D175D-865C-47E8-BCE8-AA14A39FB084}.Deploy|Any CPU.Build.0 = Deploy|Any CPU {F14D175D-865C-47E8-BCE8-AA14A39FB084}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU {F14D175D-865C-47E8-BCE8-AA14A39FB084}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU - {8BD0F66F-7C47-477D-B519-0118A9F55428}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8BD0F66F-7C47-477D-B519-0118A9F55428}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8BD0F66F-7C47-477D-B519-0118A9F55428}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8BD0F66F-7C47-477D-B519-0118A9F55428}.Release|Any CPU.Build.0 = Release|Any CPU - {8BD0F66F-7C47-477D-B519-0118A9F55428}.Deploy|Any CPU.ActiveCfg = Deploy|Any CPU - {8BD0F66F-7C47-477D-B519-0118A9F55428}.Deploy|Any CPU.Build.0 = Deploy|Any CPU - {8BD0F66F-7C47-477D-B519-0118A9F55428}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU - {8BD0F66F-7C47-477D-B519-0118A9F55428}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU {38D713EA-A57C-4C0E-9B72-09346D3034EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {38D713EA-A57C-4C0E-9B72-09346D3034EE}.Debug|Any CPU.Build.0 = Debug|Any CPU {38D713EA-A57C-4C0E-9B72-09346D3034EE}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/src/PatchManager.Core/PatchManager.Core.csproj b/src/PatchManager.Core/PatchManager.Core.csproj index f7dd492..06bd812 100644 --- a/src/PatchManager.Core/PatchManager.Core.csproj +++ b/src/PatchManager.Core/PatchManager.Core.csproj @@ -4,6 +4,7 @@ + diff --git a/src/PatchManager.Core/Patches/Preload/AssetProviderPatch.cs b/src/PatchManager.Core/Patches/Preload/AssetProviderPatch.cs index 7dd360b..59f8582 100644 --- a/src/PatchManager.Core/Patches/Preload/AssetProviderPatch.cs +++ b/src/PatchManager.Core/Patches/Preload/AssetProviderPatch.cs @@ -2,6 +2,7 @@ using KSP.Assets; using KSP.Game; using PatchManager.Core.Assets; +using Premonition.Core.Attributes; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement.AsyncOperations; @@ -9,8 +10,12 @@ namespace PatchManager.Core.Patches.Preload; +[PremonitionAssembly("Assembly-CSharp")] +[PremonitionType("KSP.Assets.AssetProvider")] internal static class AssetProviderPatch { + [PremonitionMethod("LoadByLabel")] + [PremonitionTrampoline] [UsedImplicitly] public static void LoadByLabel( string label, diff --git a/src/PatchManager.PreloadPatcher/PatchManager.PreloadPatcher.csproj b/src/PatchManager.PreloadPatcher/PatchManager.PreloadPatcher.csproj deleted file mode 100644 index c624071..0000000 --- a/src/PatchManager.PreloadPatcher/PatchManager.PreloadPatcher.csproj +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/src/PatchManager.PreloadPatcher/Patcher.cs b/src/PatchManager.PreloadPatcher/Patcher.cs deleted file mode 100644 index 95546b2..0000000 --- a/src/PatchManager.PreloadPatcher/Patcher.cs +++ /dev/null @@ -1,79 +0,0 @@ -using BepInEx; -using JetBrains.Annotations; -using Mono.Cecil; -using Mono.Cecil.Cil; -using SpaceWarp.Preload.API; - -namespace PatchManager.PreloadPatcher; - -/// -/// Preload patcher for the game's AssetProvider. -/// -[UsedImplicitly] -internal class Patcher : BasePatcher -{ - public override IEnumerable DLLsToPatch { get; } = new[] - { - "Assembly-CSharp.dll" - }; - - private static MethodReference MakeGeneric(MethodReference method, params GenericParameter[] args) - { - if (args.Length == 0) - { - return method; - } - - if (method.GenericParameters.Count != args.Length) - { - throw new ArgumentException("Invalid number of generic type arguments supplied"); - } - - var genericTypeRef = new GenericInstanceMethod(method); - foreach (var arg in args) - { - genericTypeRef.GenericArguments.Add(arg); - } - - return genericTypeRef; - } - - /// - /// Replace the generic AssetProvider.LoadAssetsAsync method with our own. - /// - /// Game assembly containing the AssetProvider class. - /// Thrown if the assembly with the replacement method cannot be found. - public override void ApplyPatch(ref AssemblyDefinition assemblyDefinition) - { - // Now we need to get the assembly with the replacement method - AssemblyDefinition coreAssembly = null; - var dir = new DirectoryInfo(Paths.PluginPath); - foreach (var file in dir.EnumerateFiles("PatchManager.Core.dll", SearchOption.AllDirectories)) - { - coreAssembly = AssemblyDefinition.ReadAssembly(file.FullName); - } - - if (coreAssembly == null) - { - throw new Exception("Could not find PatchManager Core"); - } - - var coreType = coreAssembly.MainModule.Types.First(t => t.Name == "AssetProviderPatch"); - var extractedMethod = coreType.Methods.First(m => m.Name == "LoadByLabel"); - - var targetType = assemblyDefinition.MainModule.Types.Single(t => t.Name == "AssetProvider"); - var targetMethod = targetType.Methods.Single(m => m.Name == "LoadByLabel" && m.HasGenericParameters); - // Remove every single instruction from the body of the methods - // Emit call to our extracted method - var methodInModule = targetMethod.Module.ImportReference(extractedMethod); - var generic = MakeGeneric(methodInModule, targetMethod.GenericParameters.ToArray()); - targetMethod.Body.Instructions.Clear(); - targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_1)); - targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_2)); - targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_3)); - targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Call, generic)); - targetMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); - - Logger.LogInfo("Pre-patching complete!"); - } -} \ No newline at end of file diff --git a/src/PatchManager.PreloadPatcher/Properties/AssemblyInfo.cs b/src/PatchManager.PreloadPatcher/Properties/AssemblyInfo.cs deleted file mode 100644 index 67741a3..0000000 --- a/src/PatchManager.PreloadPatcher/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,3 +0,0 @@ -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("PatchManager")] \ No newline at end of file From 4ad9cdee25e2f151ae7dd5386749a6c44bde4c8a Mon Sep 17 00:00:00 2001 From: Lexi Date: Sun, 14 Jan 2024 20:09:42 -0500 Subject: [PATCH 3/7] Fix implicit subtraction --- src/PatchManager.Core/Assets/PatchingManager.cs | 2 +- .../Nodes/Expressions/Unary/ImplicitSubtract.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PatchManager.Core/Assets/PatchingManager.cs b/src/PatchManager.Core/Assets/PatchingManager.cs index 29aeec6..faa1e22 100644 --- a/src/PatchManager.Core/Assets/PatchingManager.cs +++ b/src/PatchManager.Core/Assets/PatchingManager.cs @@ -188,7 +188,7 @@ private static AsyncOperationHandle> RebuildCache(string label) { var patchedText = PatchJson(label, name, text); if (patchedText == "") continue; - archiveFiles[name] = text; + archiveFiles[name] = patchedText; labelCacheEntry.Assets.Add(name); assetsCacheEntries.Add(name, new CacheEntry { diff --git a/src/PatchManager.SassyPatching/Nodes/Expressions/Unary/ImplicitSubtract.cs b/src/PatchManager.SassyPatching/Nodes/Expressions/Unary/ImplicitSubtract.cs index 70f420f..591227b 100644 --- a/src/PatchManager.SassyPatching/Nodes/Expressions/Unary/ImplicitSubtract.cs +++ b/src/PatchManager.SassyPatching/Nodes/Expressions/Unary/ImplicitSubtract.cs @@ -15,7 +15,7 @@ internal override DataValue GetResult(DataValue leftHandSide, DataValue rightHan { try { - return leftHandSide.Real - rightHandSide.Integer; + return leftHandSide - rightHandSide; } catch (DataValueOperationException) { From 8125298b77506de43ddf7e32d52f6498478611ae Mon Sep 17 00:00:00 2001 From: Lexi Date: Mon, 15 Jan 2024 11:01:20 -0500 Subject: [PATCH 4/7] Fix disabled mods patches still being applied ... pesky newlines --- src/PatchManager.Core/CoreModule.cs | 8 ++++---- src/PatchManager.Core/PatchManager.Core.csproj | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/PatchManager.Core/CoreModule.cs b/src/PatchManager.Core/CoreModule.cs index c2ac54c..e2b93c9 100644 --- a/src/PatchManager.Core/CoreModule.cs +++ b/src/PatchManager.Core/CoreModule.cs @@ -27,14 +27,14 @@ public class CoreModule : BaseModule private bool _wasCacheInvalidated; - private static bool ShouldLoad(string[] disabled, string modInfoLocation) + private static bool ShouldLoad(IEnumerable disabled, string modInfoLocation) { if (!File.Exists(modInfoLocation)) return false; try { var metadata = JsonConvert.DeserializeObject(File.ReadAllText(modInfoLocation)); - return metadata.ModID == null || !disabled.Contains(metadata.ModID); + return metadata.ModID == null || disabled.All(x => x != metadata.ModID); } catch { @@ -90,8 +90,8 @@ public override void PreLoad() var gameDataModsExists = Directory.Exists(Path.Combine(Paths.GameRootPath, "GameData/Mods")); // Go here instead so that the static constructor recognizes everything - var disabledPlugins = File.ReadAllText(Path.Combine(Paths.BepInExRootPath, "disabled_plugins.cfg")) - .Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); + var disabledPlugins = File.ReadAllText(SpaceWarp.Preload.API.CommonPaths.DisabledPluginsFilepath) + .Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList(); var modFolders = Directory.GetDirectories(Paths.PluginPath, "*", SearchOption.AllDirectories) .Where(dir => ShouldLoad(disabledPlugins, Path.Combine(dir, "swinfo.json"))) diff --git a/src/PatchManager.Core/PatchManager.Core.csproj b/src/PatchManager.Core/PatchManager.Core.csproj index 06bd812..731c350 100644 --- a/src/PatchManager.Core/PatchManager.Core.csproj +++ b/src/PatchManager.Core/PatchManager.Core.csproj @@ -5,7 +5,7 @@ - + From 97ff3bb7a8f069b4c85fab933daae9fd9fe9f5ec Mon Sep 17 00:00:00 2001 From: cheese3660 Date: Mon, 15 Jan 2024 11:01:56 -0500 Subject: [PATCH 5/7] Update swinfo.json --- plugin_template/swinfo.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin_template/swinfo.json b/plugin_template/swinfo.json index a8ee0bc..7932ada 100644 --- a/plugin_template/swinfo.json +++ b/plugin_template/swinfo.json @@ -5,7 +5,7 @@ "name": "Patch Manager", "description": "A mod for generic patching needs similar to KSP 1's Module Manager.", "source": "https://github.com/KSP2Community/PatchManager", - "version": "0.9.2", + "version": "0.9.3", "version_check": "https://raw.githubusercontent.com/KSP2Community/PatchManager/main/plugin_template/swinfo.json", "ksp2_version": { "min": "0.2.0", From a933b0d62e078e698737fc506f5e031e1431cd56 Mon Sep 17 00:00:00 2001 From: cheese3660 Date: Mon, 15 Jan 2024 11:03:42 -0500 Subject: [PATCH 6/7] Update swinfo.json --- plugin_template/swinfo.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plugin_template/swinfo.json b/plugin_template/swinfo.json index 7932ada..4390c5f 100644 --- a/plugin_template/swinfo.json +++ b/plugin_template/swinfo.json @@ -15,7 +15,14 @@ { "id": "com.github.x606.spacewarp", "version": { - "min": "1.7.0", + "min": "1.8.0", + "max": "*" + } + }, + { + "id": "Premonition", + "version": { + "min": "0.2.0", "max": "*" } } From 027d0f1f1271966f09e731f4ccf08066bb29f4b0 Mon Sep 17 00:00:00 2001 From: cheese3660 Date: Mon, 15 Jan 2024 11:04:02 -0500 Subject: [PATCH 7/7] Update swinfo.json --- plugin_template/swinfo.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin_template/swinfo.json b/plugin_template/swinfo.json index 4390c5f..25c1368 100644 --- a/plugin_template/swinfo.json +++ b/plugin_template/swinfo.json @@ -20,7 +20,7 @@ } }, { - "id": "Premonition", + "id": "premonition", "version": { "min": "0.2.0", "max": "*"