Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual patches #272

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading