Skip to content

Commit

Permalink
Reformat to match editor config
Browse files Browse the repository at this point in the history
  • Loading branch information
drewcassidy committed Oct 22, 2024
1 parent c1f2a03 commit 781ca4f
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 279 deletions.
111 changes: 56 additions & 55 deletions Source/IconMaterialPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,67 +25,68 @@ You should have received a copy of the GNU General Public License

namespace Shabby
{
[HarmonyPatch(typeof(PartLoader), "SetPartIconMaterials")]
class SetPartIconMaterialsPatch
{
static MethodInfo mInfo_ShaderFind = AccessTools.Method(typeof(Shader), nameof(Shader.Find));

[HarmonyPatch(typeof(PartLoader), "SetPartIconMaterials")]
class SetPartIconMaterialsPatch
{
static MethodInfo mInfo_ShaderFind = AccessTools.Method(typeof(Shader), nameof(Shader.Find));
static MethodInfo mInfo_FindOverrideIconShader = AccessTools.Method(typeof(SetPartIconMaterialsPatch), nameof(FindOverrideIconShader));
static MethodInfo mInfo_FindOverrideIconShader =
AccessTools.Method(typeof(SetPartIconMaterialsPatch), nameof(FindOverrideIconShader));

static Shader FindOverrideIconShader(Material material)
{
if (Shabby.iconShaders.TryGetValue(material.shader.name, out var shader)) {
Debug.Log($"[Shabby] custom icon shader {material.shader.name} -> {shader.name}");
return shader;
static Shader FindOverrideIconShader(Material material)
{
if (Shabby.iconShaders.TryGetValue(material.shader.name, out var shader)) {
Debug.Log($"[Shabby] custom icon shader {material.shader.name} -> {shader.name}");
return shader;
}

return Shabby.FindShader("KSP/ScreenSpaceMask");
}
return Shabby.FindShader("KSP/ScreenSpaceMask");
}

/// <summary>
/// The stock method iterates through every material in the icon prefab and replaces some
/// stock shaders with 'ScreenSpaceMask'-prefixed ones. All shaders not explicitly checked,
/// including custom shaders, are replaced with 'KSP/ScreenSpaceMask'.
/// This transpiler inserts logic to check for additional replacements.
/// </summary>
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
var code = instructions.ToList();
object locMaterial = null;
/// <summary>
/// The stock method iterates through every material in the icon prefab and replaces some
/// stock shaders with 'ScreenSpaceMask'-prefixed ones. All shaders not explicitly checked,
/// including custom shaders, are replaced with 'KSP/ScreenSpaceMask'.
/// This transpiler inserts logic to check for additional replacements.
/// </summary>
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
var code = instructions.ToList();
object locMaterial = null;

for (var i = 0; i < code.Count; ++i) {
// Material material = sharedMaterials[j];
// IL_002C ldloc.3
// IL_002D ldloc.s 4
// IL_002F ldelem.ref
// IL_0030 stloc.s 6
if (locMaterial == null
&& code[i].opcode == OpCodes.Ldloc_3
&& code[i+1].opcode == OpCodes.Ldloc_S
&& code[i+2].opcode == OpCodes.Ldelem_Ref
&& code[i+3].opcode == OpCodes.Stloc_S) {
// Extract the stack index of the material local.
locMaterial = code[i+3].operand;
}
for (var i = 0; i < code.Count; ++i) {
// Material material = sharedMaterials[j];
// IL_002C ldloc.3
// IL_002D ldloc.s 4
// IL_002F ldelem.ref
// IL_0030 stloc.s 6
if (locMaterial == null
&& code[i].opcode == OpCodes.Ldloc_3
&& code[i + 1].opcode == OpCodes.Ldloc_S
&& code[i + 2].opcode == OpCodes.Ldelem_Ref
&& code[i + 3].opcode == OpCodes.Stloc_S) {
// Extract the stack index of the material local.
locMaterial = code[i + 3].operand;
}

// material2 = new Material(Shader.Find("KSP/ScreenSpaceMask"));
// IL_0191 ldstr "KSP/ScreenSpaceMask"
// IL_0196 call class UnityEngine.Shader UnityEngine.Shader::Find(string)
// IL_019D newobj instance void UnityEngine.Material::.ctor(class UnityEngine.Shader)
// IL_01A2 stloc.s 7
if (code[i].Is(OpCodes.Ldstr, "KSP/ScreenSpaceMask") && code[i+1].Calls(mInfo_ShaderFind)) {
// Replace the call to Shader.Find with FindOverrideIconShader(material).
if (locMaterial == null) break;
code[i].opcode = OpCodes.Ldloc_S;
code[i].operand = locMaterial;
code[i+1].operand = mInfo_FindOverrideIconShader;
Debug.Log("[Shabby] patched part icon shader replacement");
return code;
// material2 = new Material(Shader.Find("KSP/ScreenSpaceMask"));
// IL_0191 ldstr "KSP/ScreenSpaceMask"
// IL_0196 call class UnityEngine.Shader UnityEngine.Shader::Find(string)
// IL_019D newobj instance void UnityEngine.Material::.ctor(class UnityEngine.Shader)
// IL_01A2 stloc.s 7
if (code[i].Is(OpCodes.Ldstr, "KSP/ScreenSpaceMask") && code[i + 1].Calls(mInfo_ShaderFind)) {
// Replace the call to Shader.Find with FindOverrideIconShader(material).
if (locMaterial == null) break;
code[i].opcode = OpCodes.Ldloc_S;
code[i].operand = locMaterial;
code[i + 1].operand = mInfo_FindOverrideIconShader;
Debug.Log("[Shabby] patched part icon shader replacement");
return code;
}
}
}

Debug.LogError("[Shabby] failed to patch part icon shader replacement");
return code;
Debug.LogError("[Shabby] failed to patch part icon shader replacement");
return code;
}
}
}

}
}
Loading

0 comments on commit 781ca4f

Please sign in to comment.