From ea3d52ba4159a0bd74cd7761aee7f8b51b647146 Mon Sep 17 00:00:00 2001 From: Andrew Cassidy Date: Sun, 17 Nov 2024 23:13:43 -0800 Subject: [PATCH] make use of logging utilities in KSPBuildTools --- Source/IconMaterialPatch.cs | 7 +++--- Source/MaterialDef.cs | 26 ++++++++------------- Source/MaterialReplacement.cs | 7 +++--- Source/ModelFilter.cs | 3 ++- Source/ShabLoader.cs | 7 +++--- Source/Shabby.cs | 44 ++++++++--------------------------- Source/Shabby.csproj | 6 ++++- 7 files changed, 39 insertions(+), 61 deletions(-) diff --git a/Source/IconMaterialPatch.cs b/Source/IconMaterialPatch.cs index b689852..dda02b1 100644 --- a/Source/IconMaterialPatch.cs +++ b/Source/IconMaterialPatch.cs @@ -21,6 +21,7 @@ You should have received a copy of the GNU General Public License using System.Reflection; using System.Reflection.Emit; using HarmonyLib; +using KSPBuildTools; using UnityEngine; namespace Shabby @@ -36,7 +37,7 @@ class SetPartIconMaterialsPatch static Shader FindOverrideIconShader(Material material) { if (Shabby.iconShaders.TryGetValue(material.shader.name, out var shader)) { - Shabby.LogDebug($"custom icon shader {material.shader.name} -> {shader.name}"); + Log.Debug($"custom icon shader {material.shader.name} -> {shader.name}"); return shader; } @@ -80,12 +81,12 @@ static IEnumerable Transpiler(IEnumerable inst code[i].opcode = OpCodes.Ldloc_S; code[i].operand = locMaterial; code[i + 1].operand = mInfo_FindOverrideIconShader; - Shabby.LogDebug("patched part icon shader replacement"); + Log.Debug("patched part icon shader replacement"); return code; } } - Shabby.LogError("failed to patch part icon shader replacement"); + Log.Error("failed to patch part icon shader replacement"); return code; } } diff --git a/Source/MaterialDef.cs b/Source/MaterialDef.cs index fa0360e..a383052 100644 --- a/Source/MaterialDef.cs +++ b/Source/MaterialDef.cs @@ -21,6 +21,7 @@ You should have received a copy of the GNU General Public License using System.Runtime.CompilerServices; using HarmonyLib; using UnityEngine; +using KSPBuildTools; namespace Shabby { @@ -33,7 +34,7 @@ public static void Load() foreach (var node in GameDatabase.Instance.GetConfigNodes("SHABBY_MATERIAL_DEF")) { var def = new MaterialDef(node); if (string.IsNullOrEmpty(def.name) || !def.isValid) { - Shabby.LogError($"[MaterialDef {def.name}] removing invalid definition"); + Log.Error($"[MaterialDef {def.name}] removing invalid definition"); } else { items[def.name] = def; } @@ -41,7 +42,7 @@ public static void Load() } } - public class MaterialDef + public class MaterialDef : ILogContextProvider { [Persistent] public string name; @@ -76,13 +77,13 @@ public MaterialDef(ConfigNode node) if (shaderName != null) { shader = Shabby.FindShader(shaderName); if (shader == null) { - LogError($"failed to find shader {shaderName}"); + this.LogError($"failed to find shader {shaderName}"); isValid = false; } } if (!updateExisting && shader == null) { - LogError($"from-scratch material must define a valid shader"); + this.LogError($"from-scratch material must define a valid shader"); isValid = false; } @@ -113,12 +114,12 @@ Dictionary LoadDictionary(ConfigNode defNode, string propKind, Fun if (value is T parsed) { items[item.name] = parsed; } else { - LogError( + this.LogError( $"failed to load {propKind} property {item.name} = {item.value}"); } } - Log($"loaded {items.Count} {propKind} properties"); + this.LogMessage($"loaded {items.Count} {propKind} properties"); return items; } @@ -132,7 +133,7 @@ public static bool ParseColor(string value, out Color color) static bool CheckProperty(Material mat, string propName) { var exists = mat.HasProperty(propName); - if (!exists) Shabby.LogWarning($"shader {mat.shader.name} does not have property {propName}"); + if (!exists) Log.Warning($"shader {mat.shader.name} does not have property {propName}"); return exists; } @@ -181,16 +182,9 @@ public Material Instantiate(Material referenceMaterial) return material; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void Log(string message) + public string context() { - Debug.Log(logPrefix + message); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void LogError(string message) - { - Debug.LogError(logPrefix + message); + return name; } } } \ No newline at end of file diff --git a/Source/MaterialReplacement.cs b/Source/MaterialReplacement.cs index a1506ab..4ae63d2 100644 --- a/Source/MaterialReplacement.cs +++ b/Source/MaterialReplacement.cs @@ -19,6 +19,7 @@ You should have received a copy of the GNU General Public License using System.Collections.Generic; using System.Linq; using HarmonyLib; +using KSPBuildTools; using UnityEngine; namespace Shabby @@ -32,12 +33,12 @@ public MaterialReplacement(ConfigNode node) : base(node) { var defName = node.GetValue("materialDef"); if (string.IsNullOrEmpty(defName)) { - Shabby.LogError("material replacement must reference a material definition"); + Log.Error("material replacement must reference a material definition"); return; } if (!MaterialDefLibrary.items.TryGetValue(defName, out materialDef)) { - Shabby.LogError($"failed to find valid material definition {defName}"); + Log.Error($"failed to find valid material definition {defName}"); } } @@ -93,7 +94,7 @@ static void Postfix(ref GameObject __result, ConfigNode partCfg) } var replacementNames = string.Join(", ", replacements.Select(rep => rep.materialDef.name)); - Shabby.LogDebug($"applied material replacements {replacementNames}"); + Log.Debug($"applied material replacements {replacementNames}"); } } } \ No newline at end of file diff --git a/Source/ModelFilter.cs b/Source/ModelFilter.cs index 2782832..418b68f 100644 --- a/Source/ModelFilter.cs +++ b/Source/ModelFilter.cs @@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License using System.Collections.Generic; using System.Linq; +using KSPBuildTools; using UnityEngine; namespace Shabby @@ -36,7 +37,7 @@ public ModelFilter(ConfigNode node) targetTransforms = node.GetValuesList("targetTransform").ToHashSet(); if (targetMaterials.Count > 0 && targetTransforms.Count > 0) { - Shabby.LogError("model filter may not specify both materials and transforms"); + Log.Error("model filter may not specify both materials and transforms"); targetTransforms.Clear(); } diff --git a/Source/ShabLoader.cs b/Source/ShabLoader.cs index f9671af..a0efbd6 100644 --- a/Source/ShabLoader.cs +++ b/Source/ShabLoader.cs @@ -20,6 +20,7 @@ You should have received a copy of the GNU General Public License using System.IO; using System.Collections; using System.Collections.Generic; +using KSPBuildTools; using UnityEngine; namespace Shabby @@ -29,14 +30,14 @@ public class DatabaseLoaderTexture_SHAB : DatabaseLoader(); foreach (Shader shader in shaders) { - Shabby.LogDebug($"adding {shader.name}"); + Log.Debug($"adding {shader.name}"); Shabby.AddShader(shader); } } diff --git a/Source/Shabby.cs b/Source/Shabby.cs index b4ce4db..dec6556 100644 --- a/Source/Shabby.cs +++ b/Source/Shabby.cs @@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License using System.Runtime.CompilerServices; using UnityEngine; using HarmonyLib; +using KSPBuildTools; using Mono.Cecil; using Mono.Cecil.Cil; using Mono.Cecil.Rocks; @@ -64,7 +65,7 @@ static Shader FindLoadedShader(string shaderName) { Shader shader; if (loadedShaders.TryGetValue(shaderName, out shader)) { - Log($"custom shader: {shader.name}"); + Log.Message($"custom shader: {shader.name}"); return shader; } @@ -82,7 +83,7 @@ public static Shader FindShader(string shaderName) shader = FindLoadedShader(replacement.shader); if (shader == null) { - LogError($"failed to find shader {replacement.shader} to replace {shaderName}"); + Log.Error($"failed to find shader {replacement.shader} to replace {shaderName}"); } } @@ -107,7 +108,7 @@ public static void MMPostLoadCallback() var iconShaderName = iconNode.GetValue("iconShader"); var iconShader = FindShader(iconShaderName ?? ""); if (string.IsNullOrEmpty(shader) || iconShader == null) { - LogError($"invalid icon shader specification {shader} -> {iconShaderName}"); + Log.Error($"invalid icon shader specification {shader} -> {iconShaderName}"); } else { iconShaders[shader] = iconShader; } @@ -119,13 +120,14 @@ public static void MMPostLoadCallback() void Awake() { + Debug.Log("Test context (shibboleth)", this); if (loadedShaders == null) { loadedShaders = new Dictionary(); harmony = new Harmony("Shabby"); harmony.PatchAll(Assembly.GetExecutingAssembly()); - LogDebug("hooked"); + Log.Debug("hooked"); // Register as an explicit MM callback such that it is run before all reflected // callbacks (as used by most mods), which may wish to access the MaterialDef library. @@ -149,7 +151,7 @@ private void Start() List callSites = new List(); - LogDebug("Beginning search for callsites"); + Log.Debug("Beginning search for callsites"); // Don't use appdomain, we don't want to accidentally patch Unity itself and this avoid // having to iterate on the BCL and Unity assemblies. @@ -190,7 +192,7 @@ private void Start() if (assemblyDef == null) throw new FileLoadException($"Couldn't read assembly \"{kspAssembly.assembly.Location}\""); } catch (Exception e) { - LogWarning($"Replace failed for assembly {kspAssembly.name}\n{e}"); + Log.Warning($"Replace failed for assembly {kspAssembly.name}\n{e}"); continue; } @@ -211,7 +213,7 @@ private void Start() if (callSite == null) throw new MemberAccessException(); } catch { - LogWarning( + Log.Warning( $"Failed to patch method {assemblyDef.Name}::{typeDef.Name}.{methodDef.Name}"); break; } @@ -234,7 +236,7 @@ private void Start() if (callSite == mInfo_ShaderFind_Replacement) continue; - Log( + Log.Debug( $"Patching call site : {callSite.DeclaringType.Assembly.GetName().Name}::{callSite.DeclaringType}.{callSite.Name}"); harmony.Patch(callSite, null, null, new HarmonyMethod(callSiteTranspiler)); } @@ -250,31 +252,5 @@ static IEnumerable CallSiteTranspiler(IEnumerable - + all runtime; build; native; contentfiles; analyzers @@ -11,6 +11,10 @@ + + portable + + net48 7.3