@@ -24,77 +24,79 @@ You should have received a copy of the GNU General Public License
2424using KSPBuildTools ;
2525using UnityEngine ;
2626
27- namespace Shabby
28- {
29- [ HarmonyPatch ( typeof ( PartLoader ) , "SetPartIconMaterials" ) ]
30- class SetPartIconMaterialsPatch
31- {
32- static MethodInfo mInfo_ShaderFind = AccessTools . Method ( typeof ( Shader ) , nameof ( Shader . Find ) ) ;
27+ namespace Shabby ;
3328
34- static MethodInfo mInfo_FindOverrideIconShader =
35- AccessTools . Method ( typeof ( SetPartIconMaterialsPatch ) , nameof ( FindOverrideIconShader ) ) ;
29+ [ HarmonyPatch ( typeof ( PartLoader ) , "SetPartIconMaterials" ) ]
30+ internal class SetPartIconMaterialsPatch
31+ {
32+ private static MethodInfo mInfo_ShaderFind =
33+ AccessTools . Method ( typeof ( Shader ) , nameof ( Shader . Find ) ) ;
3634
37- static Shader FindOverrideIconShader ( Material material ) =>
38- Shabby . TryFindIconShader ( material . shader . name ) ?? Shabby . FindShader ( "KSP/ScreenSpaceMask" ) ;
35+ private static MethodInfo mInfo_FindOverrideIconShader =
36+ AccessTools . Method ( typeof ( SetPartIconMaterialsPatch ) , nameof ( FindOverrideIconShader ) ) ;
3937
40- /// <summary>
41- /// The stock method iterates through every material in the icon prefab and replaces some
42- /// stock shaders with 'ScreenSpaceMask'-prefixed ones. All shaders not explicitly checked,
43- /// including custom shaders, are replaced with 'KSP/ScreenSpaceMask'.
44- /// This transpiler inserts logic to check for additional replacements.
45- /// </summary>
46- static IEnumerable < CodeInstruction > Transpiler ( IEnumerable < CodeInstruction > instructions )
47- {
48- var code = instructions . ToList ( ) ;
49- object locMaterial = null ;
38+ private static Shader FindOverrideIconShader ( Material material ) =>
39+ Shabby . TryFindIconShader ( material . shader . name ) ?? Shabby . FindShader ( "KSP/ScreenSpaceMask" ) ;
5040
51- for ( var i = 0 ; i < code . Count ; ++ i ) {
52- // Material material = sharedMaterials[j];
53- // IL_002C ldloc.3
54- // IL_002D ldloc.s 4
55- // IL_002F ldelem.ref
56- // IL_0030 stloc.s 6
57- if ( locMaterial == null
58- && code [ i ] . opcode == OpCodes . Ldloc_3
59- && code [ i + 1 ] . opcode == OpCodes . Ldloc_S
60- && code [ i + 2 ] . opcode == OpCodes . Ldelem_Ref
61- && code [ i + 3 ] . opcode == OpCodes . Stloc_S ) {
62- // Extract the stack index of the material local.
63- locMaterial = code [ i + 3 ] . operand ;
64- }
41+ /// <summary>
42+ /// The stock method iterates through every material in the icon prefab and replaces some
43+ /// stock shaders with 'ScreenSpaceMask'-prefixed ones. All shaders not explicitly checked,
44+ /// including custom shaders, are replaced with 'KSP/ScreenSpaceMask'.
45+ /// This transpiler inserts logic to check for additional replacements.
46+ /// </summary>
47+ private static IEnumerable < CodeInstruction > Transpiler (
48+ IEnumerable < CodeInstruction > instructions )
49+ {
50+ var code = instructions . ToList ( ) ;
51+ object locMaterial = null ;
6552
66- // material2 = new Material(Shader.Find("KSP/ScreenSpaceMask"));
67- // IL_0191 ldstr "KSP/ScreenSpaceMask"
68- // IL_0196 call class UnityEngine.Shader UnityEngine.Shader::Find(string)
69- // IL_019D newobj instance void UnityEngine.Material::.ctor(class UnityEngine.Shader)
70- // IL_01A2 stloc.s 7
71- if ( code [ i ] . Is ( OpCodes . Ldstr , "KSP/ScreenSpaceMask" ) && code [ i + 1 ] . Calls ( mInfo_ShaderFind ) ) {
72- // Replace the call to Shader.Find with FindOverrideIconShader(material).
73- if ( locMaterial == null ) break ;
74- code [ i ] . opcode = OpCodes . Ldloc_S ;
75- code [ i ] . operand = locMaterial ;
76- code [ i + 1 ] . operand = mInfo_FindOverrideIconShader ;
77- return code ;
78- }
53+ for ( var i = 0 ; i < code . Count ; ++ i ) {
54+ // Material material = sharedMaterials[j];
55+ // IL_002C ldloc.3
56+ // IL_002D ldloc.s 4
57+ // IL_002F ldelem.ref
58+ // IL_0030 stloc.s 6
59+ if ( locMaterial == null
60+ && code [ i ] . opcode == OpCodes . Ldloc_3
61+ && code [ i + 1 ] . opcode == OpCodes . Ldloc_S
62+ && code [ i + 2 ] . opcode == OpCodes . Ldelem_Ref
63+ && code [ i + 3 ] . opcode == OpCodes . Stloc_S ) {
64+ // Extract the stack index of the material local.
65+ locMaterial = code [ i + 3 ] . operand ;
7966 }
8067
81- Log . Error ( "failed to patch part icon shader replacement" ) ;
82- return code ;
68+ // material2 = new Material(Shader.Find("KSP/ScreenSpaceMask"));
69+ // IL_0191 ldstr "KSP/ScreenSpaceMask"
70+ // IL_0196 call class UnityEngine.Shader UnityEngine.Shader::Find(string)
71+ // IL_019D newobj instance void UnityEngine.Material::.ctor(class UnityEngine.Shader)
72+ // IL_01A2 stloc.s 7
73+ if ( code [ i ] . Is ( OpCodes . Ldstr , "KSP/ScreenSpaceMask" ) &&
74+ code [ i + 1 ] . Calls ( mInfo_ShaderFind ) ) {
75+ // Replace the call to Shader.Find with FindOverrideIconShader(material).
76+ if ( locMaterial == null ) break ;
77+ code [ i ] . opcode = OpCodes . Ldloc_S ;
78+ code [ i ] . operand = locMaterial ;
79+ code [ i + 1 ] . operand = mInfo_FindOverrideIconShader ;
80+ return code ;
81+ }
8382 }
83+
84+ Log . Error ( "failed to patch part icon shader replacement" ) ;
85+ return code ;
8486 }
87+ }
8588
86- [ HarmonyPatch ( TargetType , "GetIconShader" ) ]
87- class KSPCFGetIconShaderPatch
88- {
89- const string TargetType = "KSPCommunityFixes.Performance.PartParsingPerf" ;
89+ [ HarmonyPatch ( TargetType , "GetIconShader" ) ]
90+ internal class KSPCFGetIconShaderPatch
91+ {
92+ private const string TargetType = "KSPCommunityFixes.Performance.PartParsingPerf" ;
9093
91- static bool Prepare ( ) => AccessTools . TypeByName ( TargetType ) != null ;
94+ private static bool Prepare ( ) => AccessTools . TypeByName ( TargetType ) != null ;
9295
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- }
96+ private static void Postfix ( string materialShaderName , ref Shader __result )
97+ {
98+ if ( __result . name != "KSP/ScreenSpaceMask" ) return ;
99+ if ( Shabby . TryFindIconShader ( materialShaderName ) is not Shader iconShader ) return ;
100+ __result = iconShader ;
99101 }
100- }
102+ }
0 commit comments