-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow icon shaders to be replaced
- Loading branch information
Showing
5 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
This file is part of Shabby. | ||
Shabby is free software: you can redistribute it and/or | ||
modify it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
Shabby is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with Shabby. If not, see | ||
<http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using UnityEngine; | ||
|
||
namespace Shabby | ||
{ | ||
|
||
[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 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"); | ||
} | ||
|
||
/// <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 loc_material = 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 (loc_material == 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. | ||
loc_material = 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 (loc_material == null) break; | ||
code[i].opcode = OpCodes.Ldloc_S; | ||
code[i].operand = loc_material; | ||
code[i+1].operand = mInfo_FindOverrideIconShader; | ||
Debug.Log("[Shabby] patched part icon shader replacement"); | ||
return code; | ||
} | ||
} | ||
|
||
Debug.Log("[Shabby] failed to patch part icon shader replacement"); | ||
return code; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters