From 6a9f79fa836eb6a028779214f5cf921f3b0e73f7 Mon Sep 17 00:00:00 2001 From: Andrew Cassidy Date: Mon, 21 Oct 2024 23:38:57 -0700 Subject: [PATCH] "better" logging Got don't get mad at me --- Source/IconMaterialPatch.cs | 6 ++--- Source/MaterialDef.cs | 31 +++++++++++++++++----- Source/MaterialReplacement.cs | 6 ++--- Source/ModelFilter.cs | 2 +- Source/ShabLoader.cs | 6 ++--- Source/Shabby.cs | 49 +++++++++++++++++++++++++++-------- Source/Shabby.csproj | 2 +- 7 files changed, 73 insertions(+), 29 deletions(-) diff --git a/Source/IconMaterialPatch.cs b/Source/IconMaterialPatch.cs index 3fa4371..b689852 100644 --- a/Source/IconMaterialPatch.cs +++ b/Source/IconMaterialPatch.cs @@ -36,7 +36,7 @@ class SetPartIconMaterialsPatch 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}"); + Shabby.LogDebug($"custom icon shader {material.shader.name} -> {shader.name}"); return shader; } @@ -80,12 +80,12 @@ static IEnumerable Transpiler(IEnumerable inst code[i].opcode = OpCodes.Ldloc_S; code[i].operand = locMaterial; code[i + 1].operand = mInfo_FindOverrideIconShader; - Debug.Log("[Shabby] patched part icon shader replacement"); + Shabby.LogDebug("patched part icon shader replacement"); return code; } } - Debug.LogError("[Shabby] failed to patch part icon shader replacement"); + Shabby.LogError("failed to patch part icon shader replacement"); return code; } } diff --git a/Source/MaterialDef.cs b/Source/MaterialDef.cs index 7967c8e..fa0360e 100644 --- a/Source/MaterialDef.cs +++ b/Source/MaterialDef.cs @@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License using System; using System.Collections.Generic; +using System.Runtime.CompilerServices; using HarmonyLib; using UnityEngine; @@ -32,7 +33,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) { - Debug.LogError($"[Shabby][MaterialDef {def.name}] removing invalid definition"); + Shabby.LogError($"[MaterialDef {def.name}] removing invalid definition"); } else { items[def.name] = def; } @@ -64,20 +65,24 @@ public class MaterialDef public readonly bool isValid = true; + internal readonly string logPrefix; + public MaterialDef(ConfigNode node) { ConfigNode.LoadObjectFromConfig(this, node); + logPrefix = $"[Shabby][MaterialDef {name}] "; + if (shaderName != null) { shader = Shabby.FindShader(shaderName); if (shader == null) { - Debug.LogError($"[Shabby][MaterialDef {name}] failed to find shader {shaderName}"); + LogError($"failed to find shader {shaderName}"); isValid = false; } } if (!updateExisting && shader == null) { - Debug.LogError($"[Shabby][MaterialDef {name}] from-scratch material must define a valid shader"); + LogError($"from-scratch material must define a valid shader"); isValid = false; } @@ -108,12 +113,12 @@ Dictionary LoadDictionary(ConfigNode defNode, string propKind, Fun if (value is T parsed) { items[item.name] = parsed; } else { - Debug.LogError( - $"[Shabby][MaterialDef {name}] failed to load {propKind} property {item.name} = {item.value}"); + LogError( + $"failed to load {propKind} property {item.name} = {item.value}"); } } - Debug.Log($"[Shabby][MaterialDef {name}] loaded {items.Count} {propKind} properties"); + Log($"loaded {items.Count} {propKind} properties"); return items; } @@ -127,7 +132,7 @@ public static bool ParseColor(string value, out Color color) static bool CheckProperty(Material mat, string propName) { var exists = mat.HasProperty(propName); - if (!exists) Debug.LogWarning($"[Shabby] shader {mat.shader.name} does not have property {propName}"); + if (!exists) Shabby.LogWarning($"shader {mat.shader.name} does not have property {propName}"); return exists; } @@ -175,5 +180,17 @@ public Material Instantiate(Material referenceMaterial) return material; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void Log(string message) + { + Debug.Log(logPrefix + message); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void LogError(string message) + { + Debug.LogError(logPrefix + message); + } } } \ No newline at end of file diff --git a/Source/MaterialReplacement.cs b/Source/MaterialReplacement.cs index ad3176b..a1506ab 100644 --- a/Source/MaterialReplacement.cs +++ b/Source/MaterialReplacement.cs @@ -32,12 +32,12 @@ public MaterialReplacement(ConfigNode node) : base(node) { var defName = node.GetValue("materialDef"); if (string.IsNullOrEmpty(defName)) { - Debug.LogError("[Shabby] material replacement must reference a material definition"); + Shabby.LogError("material replacement must reference a material definition"); return; } if (!MaterialDefLibrary.items.TryGetValue(defName, out materialDef)) { - Debug.LogError($"[Shabby] failed to find valid material definition {defName}"); + Shabby.LogError($"failed to find valid material definition {defName}"); } } @@ -93,7 +93,7 @@ static void Postfix(ref GameObject __result, ConfigNode partCfg) } var replacementNames = string.Join(", ", replacements.Select(rep => rep.materialDef.name)); - Debug.Log($"[Shabby] applied material replacements {replacementNames}"); + Shabby.LogDebug($"applied material replacements {replacementNames}"); } } } \ No newline at end of file diff --git a/Source/ModelFilter.cs b/Source/ModelFilter.cs index 5c890e8..2782832 100644 --- a/Source/ModelFilter.cs +++ b/Source/ModelFilter.cs @@ -36,7 +36,7 @@ public ModelFilter(ConfigNode node) targetTransforms = node.GetValuesList("targetTransform").ToHashSet(); if (targetMaterials.Count > 0 && targetTransforms.Count > 0) { - Debug.LogError("[Shabby] model filter may not specify both materials and transforms"); + Shabby.LogError("model filter may not specify both materials and transforms"); targetTransforms.Clear(); } diff --git a/Source/ShabLoader.cs b/Source/ShabLoader.cs index 032646a..f9671af 100644 --- a/Source/ShabLoader.cs +++ b/Source/ShabLoader.cs @@ -29,14 +29,14 @@ public class DatabaseLoaderTexture_SHAB : DatabaseLoader(); foreach (Shader shader in shaders) { - Debug.Log($"[Shabby] adding {shader.name}"); + Shabby.LogDebug($"adding {shader.name}"); Shabby.AddShader(shader); } } diff --git a/Source/Shabby.cs b/Source/Shabby.cs index 6d95e65..b4ce4db 100644 --- a/Source/Shabby.cs +++ b/Source/Shabby.cs @@ -21,6 +21,7 @@ You should have received a copy of the GNU General Public License using System.Collections.Generic; using System.Reflection; using System.Reflection.Emit; +using System.Runtime.CompilerServices; using UnityEngine; using HarmonyLib; using Mono.Cecil; @@ -63,7 +64,7 @@ static Shader FindLoadedShader(string shaderName) { Shader shader; if (loadedShaders.TryGetValue(shaderName, out shader)) { - Debug.Log($"[Shabby] custom shader: {shader.name}"); + Log($"custom shader: {shader.name}"); return shader; } @@ -81,7 +82,7 @@ public static Shader FindShader(string shaderName) shader = FindLoadedShader(replacement.shader); if (shader == null) { - Debug.LogError($"[Shabby] failed to find shader {replacement.shader} to replace {shaderName}"); + LogError($"failed to find shader {replacement.shader} to replace {shaderName}"); } } @@ -106,7 +107,7 @@ public static void MMPostLoadCallback() var iconShaderName = iconNode.GetValue("iconShader"); var iconShader = FindShader(iconShaderName ?? ""); if (string.IsNullOrEmpty(shader) || iconShader == null) { - Debug.LogError($"[Shabby] invalid icon shader specification {shader} -> {iconShaderName}"); + LogError($"invalid icon shader specification {shader} -> {iconShaderName}"); } else { iconShaders[shader] = iconShader; } @@ -124,7 +125,7 @@ void Awake() harmony = new Harmony("Shabby"); harmony.PatchAll(Assembly.GetExecutingAssembly()); - Debug.Log($"[Shabby] hooked"); + LogDebug("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. @@ -148,7 +149,7 @@ private void Start() List callSites = new List(); - Debug.Log($"[Shabby]: Beginning search for callsites"); + LogDebug("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. @@ -174,7 +175,7 @@ private void Start() } } } catch (Exception ex) { - Debug.LogError($"[Shabby] excpetion while patching {method.Name}: {ex}"); + LogError($"exception while patching {method.Name}: {ex}"); } } } @@ -189,7 +190,7 @@ private void Start() if (assemblyDef == null) throw new FileLoadException($"Couldn't read assembly \"{kspAssembly.assembly.Location}\""); } catch (Exception e) { - Debug.LogWarning($"[Shabby] Replace failed for assembly {kspAssembly.name}\n{e}"); + LogWarning($"Replace failed for assembly {kspAssembly.name}\n{e}"); continue; } @@ -210,8 +211,8 @@ private void Start() if (callSite == null) throw new MemberAccessException(); } catch { - Debug.LogWarning( - $"[Shabby] Failed to patch method {assemblyDef.Name}::{typeDef.Name}.{methodDef.Name}"); + LogWarning( + $"Failed to patch method {assemblyDef.Name}::{typeDef.Name}.{methodDef.Name}"); break; } @@ -233,8 +234,8 @@ private void Start() if (callSite == mInfo_ShaderFind_Replacement) continue; - Debug.Log( - $"[Shabby] Patching call site : {callSite.DeclaringType.Assembly.GetName().Name}::{callSite.DeclaringType}.{callSite.Name}"); + Log( + $"Patching call site : {callSite.DeclaringType.Assembly.GetName().Name}::{callSite.DeclaringType}.{callSite.Name}"); harmony.Patch(callSite, null, null, new HarmonyMethod(callSiteTranspiler)); } } @@ -249,5 +250,31 @@ static IEnumerable CallSiteTranspiler(IEnumerable - + all runtime; build; native; contentfiles; analyzers