우선순위 속성 - solaris0115/RimWorldModGuide GitHub Wiki

동일한 함수에 대한 패치의 순서는 선형적으로 진행되지 않습니다. (모드 전체 우선순위가 아님. 패치 함수 하나)
각 모드별 플러그인들은 필요하다면 먼저 실행될 수 있습니다.
아래의 속성을 통해 우선순위를 부여할 수 있습니다.

  • [HarmonyPriority(int)] Prefix/Postfix의 우선순위를 결정합니다. 기본 우선순위값은 400입니다. 높을수록 먼저 실행됩니다. Priority을 사용하세요.
  • [HarmonyBefore(string[])] Prefix/Postfix가 특정 모드 ID들 이전에 실행되도록 합니다.
  • [HarmonyAfter(string[])] Prefix/Postfix가 특정 모드 ID들 이후에 실행되도록 합니다.

예제

class Foo
{
	static string Bar()
	{
		return "secret";
	}
}

플러그인 1

var harmony = HarmonyInstance.Create("net.example.plugin1");
harmony.PatchAll(Assembly.GetExecutingAssembly());

[HarmonyPatch(typeof(Foo))]
[HarmonyPatch("Bar")]
class MyPatch
{
	static void Postfix(ref result)
	{
		result = "new secret 1";
	}
}

플러그인 2

var harmony = HarmonyInstance.Create("net.example.plugin2");
harmony.PatchAll(Assembly.GetExecutingAssembly());

[HarmonyPatch(typeof(Foo))]
[HarmonyPatch("Bar")]
class MyPatch
{
	static void Postfix(ref result)
	{
		result = "new secret 2";
	}
}

여기서 Foo.Bar()를 호출하면 new secret 2가 출력됩니다. 왜냐하면 두번째 Postfix때문에 첫번째 플러그인의 결과가 덮어씌워졌기 때문이죠. 만약 'new secret 1'이 출력되기를 원한다면 다음과 같이 작성 하시면 됩니다.

var harmony = HarmonyInstance.Create("net.example.plugin1");
harmony.PatchAll(Assembly.GetExecutingAssembly());

[HarmonyPatch(typeof(Foo))]
[HarmonyPatch("Bar")]
class MyPatch
{
	[HarmonyAfter(new string[] { "net.example.plugin2" })]
	static void Postfix(ref result)
	{
		result = "new secret 1";
	}
}

혹은

var harmony = HarmonyInstance.Create("net.example.plugin1");
harmony.PatchAll(Assembly.GetExecutingAssembly());

[HarmonyPatch(typeof(Foo))]
[HarmonyPatch("Bar")]
class MyPatch
{
        [HarmonyPriority(Priority.High)]
	static void Postfix(ref result)
	{
		result = "new secret 1";
	}
}

적용 규칙

아래 예시는 모두 Core>Hugslib>A>B>C>D의 모드 호출순으로 설명합니다.

  • 모드는 일반적으로 Core와 가까운 순서대로 적용됩니다. (위에서부터 아래로 순차적)

  • [HarmonyPriority(int)]속성은 동일 범주 내에서 우선순위를 제공합니다.
    예시대로라면 Core>A>B>C>D순으로 호출됩니다.
    하지만 D모드에서 [HarmonyAfter("A.ID")][HarmonyPriority(Priority.High)]로 우선순위를 결정했다면
    A이후 모드들 중 BCD에서 D모드가 우선순위를 먼저 잡게 됩니다.Core>A>D>B>C

  • [HarmonyAfter(string[])][HarmonyBefore(string[])]은 해당 모드가 먼저 로드되어 있어야 합니다.
    예를들어 D모드에서 [HarmonyAfter("A.ID")]속성을 사용하고 싶다면 D는 반드시 A모드 이후에 로드 되어야합니다. 만약 D>A>B>C순으로 로드 되었다면 결과 또한 D>A>B>C순으로 나올 것입니다.

다음: 유틸리티