Skip to content

Commit

Permalink
Added [ManualPatch] attribute, allow to define a BasePatch that can b…
Browse files Browse the repository at this point in the history
…e called manually. Main intended usage is for patches requiring to be applied before ModuleManagerPostLoad.
  • Loading branch information
gotmachine committed Oct 19, 2024
1 parent c2dcbf3 commit 22e8469
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
13 changes: 12 additions & 1 deletion KSPCommunityFixes/BasePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ internal class TranspileInDebugAttribute : Attribute
{
}

/// <summary>
/// When applied to a class derived from <see cref="BasePatch"/>, the patch won't be automatically applied.<para/>
/// To apply the patch, call <see cref="BasePatch.Patch"/>. Note that if that call happens before ModuleManager
/// has patched the configs (ie, before part compilation), <see cref="BasePatch.IgnoreConfig"/> must be overriden
/// to return <see langword="true"/>, or the patch won't be applied.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
internal class ManualPatchAttribute : Attribute
{
}

public abstract class BasePatch
{
public static readonly string pluginData = "PluginData";
Expand All @@ -64,7 +75,7 @@ public static void Patch(Type patchType)

if (!patch.IgnoreConfig && !KSPCommunityFixes.enabledPatches.Contains(patchType.Name))
{
Debug.Log($"[KSPCommunityFixes] Patch {patchType.Name} not applied (disabled in Settings.cfg)");
Debug.Log($"[KSPCommunityFixes] Patch {patchType.Name} not applied (not enabled in Settings.cfg)");
return;
}

Expand Down
17 changes: 9 additions & 8 deletions KSPCommunityFixes/KSPCommunityFixes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class KSPCommunityFixes : MonoBehaviour

public static string LOC_KSPCF_Title = "KSP Community Fixes";


public static Harmony Harmony { get; private set; }

public static HashSet<string> enabledPatches = new HashSet<string>();
Expand Down Expand Up @@ -50,6 +49,14 @@ public static Version KspVersion
}
}

static KSPCommunityFixes()
{
Harmony = new Harmony("KSPCommunityFixes");
#if DEBUG
Harmony.DEBUG = true;
#endif
}

public static T GetPatchInstance<T>() where T : BasePatch
{
if (!patchInstances.TryGetValue(typeof(T), out BasePatch instance))
Expand All @@ -72,11 +79,6 @@ void Start()
DontDestroyOnLoad(this);
}

Harmony = new Harmony("KSPCommunityFixes");

#if DEBUG
Harmony.DEBUG = true;
#endif
LocalizationUtils.GenerateLocTemplateIfRequested();
LocalizationUtils.ParseLocalization();

Expand Down Expand Up @@ -113,10 +115,9 @@ public void MMPostLoadCallback()
List<Type> patchesTypes = new List<Type>();
foreach (Type type in Assembly.GetAssembly(basePatchType).GetTypes())
{
if (!type.IsAbstract && type.IsSubclassOf(basePatchType))
if (!type.IsAbstract && type.IsSubclassOf(basePatchType) && type.GetCustomAttribute<ManualPatchAttribute>() == null)
{
patchesTypes.Add(type);

}
}

Expand Down
2 changes: 1 addition & 1 deletion KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Krafs.Publicizer.2.2.1\build\Krafs.Publicizer.props" Condition="Exists('..\packages\Krafs.Publicizer.2.2.1\build\Krafs.Publicizer.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
Expand Down

0 comments on commit 22e8469

Please sign in to comment.