@@ -25,67 +25,68 @@ You should have received a copy of the GNU General Public License
25
25
26
26
namespace Shabby
27
27
{
28
+ [ HarmonyPatch ( typeof ( PartLoader ) , "SetPartIconMaterials" ) ]
29
+ class SetPartIconMaterialsPatch
30
+ {
31
+ static MethodInfo mInfo_ShaderFind = AccessTools . Method ( typeof ( Shader ) , nameof ( Shader . Find ) ) ;
28
32
29
- [ HarmonyPatch ( typeof ( PartLoader ) , "SetPartIconMaterials" ) ]
30
- class SetPartIconMaterialsPatch
31
- {
32
- static MethodInfo mInfo_ShaderFind = AccessTools . Method ( typeof ( Shader ) , nameof ( Shader . Find ) ) ;
33
- static MethodInfo mInfo_FindOverrideIconShader = AccessTools . Method ( typeof ( SetPartIconMaterialsPatch ) , nameof ( FindOverrideIconShader ) ) ;
33
+ static MethodInfo mInfo_FindOverrideIconShader =
34
+ AccessTools . Method ( typeof ( SetPartIconMaterialsPatch ) , nameof ( FindOverrideIconShader ) ) ;
34
35
35
- static Shader FindOverrideIconShader ( Material material )
36
- {
37
- if ( Shabby . iconShaders . TryGetValue ( material . shader . name , out var shader ) ) {
38
- Debug . Log ( $ "[Shabby] custom icon shader { material . shader . name } -> { shader . name } ") ;
39
- return shader ;
36
+ static Shader FindOverrideIconShader ( Material material )
37
+ {
38
+ if ( Shabby . iconShaders . TryGetValue ( material . shader . name , out var shader ) ) {
39
+ Debug . Log ( $ "[Shabby] custom icon shader { material . shader . name } -> { shader . name } ") ;
40
+ return shader ;
41
+ }
42
+
43
+ return Shabby . FindShader ( "KSP/ScreenSpaceMask" ) ;
40
44
}
41
- return Shabby . FindShader ( "KSP/ScreenSpaceMask" ) ;
42
- }
43
45
44
- /// <summary>
45
- /// The stock method iterates through every material in the icon prefab and replaces some
46
- /// stock shaders with 'ScreenSpaceMask'-prefixed ones. All shaders not explicitly checked,
47
- /// including custom shaders, are replaced with 'KSP/ScreenSpaceMask'.
48
- /// This transpiler inserts logic to check for additional replacements.
49
- /// </summary>
50
- static IEnumerable < CodeInstruction > Transpiler ( IEnumerable < CodeInstruction > instructions )
51
- {
52
- var code = instructions . ToList ( ) ;
53
- object locMaterial = null ;
46
+ /// <summary>
47
+ /// The stock method iterates through every material in the icon prefab and replaces some
48
+ /// stock shaders with 'ScreenSpaceMask'-prefixed ones. All shaders not explicitly checked,
49
+ /// including custom shaders, are replaced with 'KSP/ScreenSpaceMask'.
50
+ /// This transpiler inserts logic to check for additional replacements.
51
+ /// </summary>
52
+ static IEnumerable < CodeInstruction > Transpiler ( IEnumerable < CodeInstruction > instructions )
53
+ {
54
+ var code = instructions . ToList ( ) ;
55
+ object locMaterial = null ;
54
56
55
- for ( var i = 0 ; i < code . Count ; ++ i ) {
56
- // Material material = sharedMaterials[j];
57
- // IL_002C ldloc.3
58
- // IL_002D ldloc.s 4
59
- // IL_002F ldelem.ref
60
- // IL_0030 stloc.s 6
61
- if ( locMaterial == null
62
- && code [ i ] . opcode == OpCodes . Ldloc_3
63
- && code [ i + 1 ] . opcode == OpCodes . Ldloc_S
64
- && code [ i + 2 ] . opcode == OpCodes . Ldelem_Ref
65
- && code [ i + 3 ] . opcode == OpCodes . Stloc_S ) {
66
- // Extract the stack index of the material local.
67
- locMaterial = code [ i + 3 ] . operand ;
68
- }
57
+ for ( var i = 0 ; i < code . Count ; ++ i ) {
58
+ // Material material = sharedMaterials[j];
59
+ // IL_002C ldloc.3
60
+ // IL_002D ldloc.s 4
61
+ // IL_002F ldelem.ref
62
+ // IL_0030 stloc.s 6
63
+ if ( locMaterial == null
64
+ && code [ i ] . opcode == OpCodes . Ldloc_3
65
+ && code [ i + 1 ] . opcode == OpCodes . Ldloc_S
66
+ && code [ i + 2 ] . opcode == OpCodes . Ldelem_Ref
67
+ && code [ i + 3 ] . opcode == OpCodes . Stloc_S ) {
68
+ // Extract the stack index of the material local.
69
+ locMaterial = code [ i + 3 ] . operand ;
70
+ }
69
71
70
- // material2 = new Material(Shader.Find("KSP/ScreenSpaceMask"));
71
- // IL_0191 ldstr "KSP/ScreenSpaceMask"
72
- // IL_0196 call class UnityEngine.Shader UnityEngine.Shader::Find(string)
73
- // IL_019D newobj instance void UnityEngine.Material::.ctor(class UnityEngine.Shader)
74
- // IL_01A2 stloc.s 7
75
- if ( code [ i ] . Is ( OpCodes . Ldstr , "KSP/ScreenSpaceMask" ) && code [ i + 1 ] . Calls ( mInfo_ShaderFind ) ) {
76
- // Replace the call to Shader.Find with FindOverrideIconShader(material).
77
- if ( locMaterial == null ) break ;
78
- code [ i ] . opcode = OpCodes . Ldloc_S ;
79
- code [ i ] . operand = locMaterial ;
80
- code [ i + 1 ] . operand = mInfo_FindOverrideIconShader ;
81
- Debug . Log ( "[Shabby] patched part icon shader replacement" ) ;
82
- return code ;
72
+ // material2 = new Material(Shader.Find("KSP/ScreenSpaceMask"));
73
+ // IL_0191 ldstr "KSP/ScreenSpaceMask"
74
+ // IL_0196 call class UnityEngine.Shader UnityEngine.Shader::Find(string)
75
+ // IL_019D newobj instance void UnityEngine.Material::.ctor(class UnityEngine.Shader)
76
+ // IL_01A2 stloc.s 7
77
+ if ( code [ i ] . Is ( OpCodes . Ldstr , "KSP/ScreenSpaceMask" ) && code [ i + 1 ] . Calls ( mInfo_ShaderFind ) ) {
78
+ // Replace the call to Shader.Find with FindOverrideIconShader(material).
79
+ if ( locMaterial == null ) break ;
80
+ code [ i ] . opcode = OpCodes . Ldloc_S ;
81
+ code [ i ] . operand = locMaterial ;
82
+ code [ i + 1 ] . operand = mInfo_FindOverrideIconShader ;
83
+ Debug . Log ( "[Shabby] patched part icon shader replacement" ) ;
84
+ return code ;
85
+ }
83
86
}
84
- }
85
87
86
- Debug . LogError ( "[Shabby] failed to patch part icon shader replacement" ) ;
87
- return code ;
88
+ Debug . LogError ( "[Shabby] failed to patch part icon shader replacement" ) ;
89
+ return code ;
90
+ }
88
91
}
89
- }
90
-
91
- }
92
+ }
0 commit comments