Skip to content

Commit 7fb5221

Browse files
committed
fix: icon shader replacement compatibility w/ KSPCF v1.37+
KSPCF rewrote the Part parser, including the icon shader setter. Inject our replacements there too. Fix KSPModdingLibs/KSPCommunityFixes#318 .
1 parent 474e0c2 commit 7fb5221

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

Source/IconMaterialPatch.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,8 @@ class SetPartIconMaterialsPatch
3434
static MethodInfo mInfo_FindOverrideIconShader =
3535
AccessTools.Method(typeof(SetPartIconMaterialsPatch), nameof(FindOverrideIconShader));
3636

37-
static Shader FindOverrideIconShader(Material material)
38-
{
39-
if (Shabby.iconShaders.TryGetValue(material.shader.name, out var shader)) {
40-
Log.Debug($"custom icon shader {material.shader.name} -> {shader.name}");
41-
return shader;
42-
}
43-
44-
return Shabby.FindShader("KSP/ScreenSpaceMask");
45-
}
37+
static Shader FindOverrideIconShader(Material material) =>
38+
Shabby.TryFindIconShader(material.shader.name) ?? Shabby.FindShader("KSP/ScreenSpaceMask");
4639

4740
/// <summary>
4841
/// The stock method iterates through every material in the icon prefab and replaces some
@@ -89,4 +82,19 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
8982
return code;
9083
}
9184
}
92-
}
85+
86+
[HarmonyPatch(TargetType, "GetIconShader")]
87+
class KSPCFGetIconShaderPatch
88+
{
89+
const string TargetType = "KSPCommunityFixes.Performance.PartParsingPerf";
90+
91+
static bool Prepare() => AccessTools.TypeByName(TargetType) != null;
92+
93+
static void Postfix(string materialShaderName, ref Shader __result)
94+
{
95+
if (__result.name != "KSP/ScreenSpaceMask") return;
96+
if (Shabby.TryFindIconShader(materialShaderName) is not Shader iconShader) return;
97+
__result = iconShader;
98+
}
99+
}
100+
}

Source/Shabby.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ public static Shader FindShader(string shaderName)
9595
return shader;
9696
}
9797

98+
public static Shader TryFindIconShader(string shaderName)
99+
{
100+
if (!iconShaders.TryGetValue(shaderName, out var iconShader)) return null;
101+
Log.Debug($"custom icon shader {shaderName} -> {iconShader.name}");
102+
return iconShader;
103+
}
104+
98105
public static void MMPostLoadCallback()
99106
{
100107
var configNodes = GameDatabase.Instance.GetConfigNodes("SHABBY");

0 commit comments

Comments
 (0)