Skip to content

Commit 6a9f79f

Browse files
committed
"better" logging
Got don't get mad at me
1 parent 781ca4f commit 6a9f79f

File tree

7 files changed

+73
-29
lines changed

7 files changed

+73
-29
lines changed

Source/IconMaterialPatch.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SetPartIconMaterialsPatch
3636
static Shader FindOverrideIconShader(Material material)
3737
{
3838
if (Shabby.iconShaders.TryGetValue(material.shader.name, out var shader)) {
39-
Debug.Log($"[Shabby] custom icon shader {material.shader.name} -> {shader.name}");
39+
Shabby.LogDebug($"custom icon shader {material.shader.name} -> {shader.name}");
4040
return shader;
4141
}
4242

@@ -80,12 +80,12 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
8080
code[i].opcode = OpCodes.Ldloc_S;
8181
code[i].operand = locMaterial;
8282
code[i + 1].operand = mInfo_FindOverrideIconShader;
83-
Debug.Log("[Shabby] patched part icon shader replacement");
83+
Shabby.LogDebug("patched part icon shader replacement");
8484
return code;
8585
}
8686
}
8787

88-
Debug.LogError("[Shabby] failed to patch part icon shader replacement");
88+
Shabby.LogError("failed to patch part icon shader replacement");
8989
return code;
9090
}
9191
}

Source/MaterialDef.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License
1818

1919
using System;
2020
using System.Collections.Generic;
21+
using System.Runtime.CompilerServices;
2122
using HarmonyLib;
2223
using UnityEngine;
2324

@@ -32,7 +33,7 @@ public static void Load()
3233
foreach (var node in GameDatabase.Instance.GetConfigNodes("SHABBY_MATERIAL_DEF")) {
3334
var def = new MaterialDef(node);
3435
if (string.IsNullOrEmpty(def.name) || !def.isValid) {
35-
Debug.LogError($"[Shabby][MaterialDef {def.name}] removing invalid definition");
36+
Shabby.LogError($"[MaterialDef {def.name}] removing invalid definition");
3637
} else {
3738
items[def.name] = def;
3839
}
@@ -64,20 +65,24 @@ public class MaterialDef
6465

6566
public readonly bool isValid = true;
6667

68+
internal readonly string logPrefix;
69+
6770
public MaterialDef(ConfigNode node)
6871
{
6972
ConfigNode.LoadObjectFromConfig(this, node);
7073

74+
logPrefix = $"[Shabby][MaterialDef {name}] ";
75+
7176
if (shaderName != null) {
7277
shader = Shabby.FindShader(shaderName);
7378
if (shader == null) {
74-
Debug.LogError($"[Shabby][MaterialDef {name}] failed to find shader {shaderName}");
79+
LogError($"failed to find shader {shaderName}");
7580
isValid = false;
7681
}
7782
}
7883

7984
if (!updateExisting && shader == null) {
80-
Debug.LogError($"[Shabby][MaterialDef {name}] from-scratch material must define a valid shader");
85+
LogError($"from-scratch material must define a valid shader");
8186
isValid = false;
8287
}
8388

@@ -108,12 +113,12 @@ Dictionary<string, T> LoadDictionary<T>(ConfigNode defNode, string propKind, Fun
108113
if (value is T parsed) {
109114
items[item.name] = parsed;
110115
} else {
111-
Debug.LogError(
112-
$"[Shabby][MaterialDef {name}] failed to load {propKind} property {item.name} = {item.value}");
116+
LogError(
117+
$"failed to load {propKind} property {item.name} = {item.value}");
113118
}
114119
}
115120

116-
Debug.Log($"[Shabby][MaterialDef {name}] loaded {items.Count} {propKind} properties");
121+
Log($"loaded {items.Count} {propKind} properties");
117122
return items;
118123
}
119124

@@ -127,7 +132,7 @@ public static bool ParseColor(string value, out Color color)
127132
static bool CheckProperty(Material mat, string propName)
128133
{
129134
var exists = mat.HasProperty(propName);
130-
if (!exists) Debug.LogWarning($"[Shabby] shader {mat.shader.name} does not have property {propName}");
135+
if (!exists) Shabby.LogWarning($"shader {mat.shader.name} does not have property {propName}");
131136
return exists;
132137
}
133138

@@ -175,5 +180,17 @@ public Material Instantiate(Material referenceMaterial)
175180

176181
return material;
177182
}
183+
184+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
185+
private void Log(string message)
186+
{
187+
Debug.Log(logPrefix + message);
188+
}
189+
190+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
191+
private void LogError(string message)
192+
{
193+
Debug.LogError(logPrefix + message);
194+
}
178195
}
179196
}

Source/MaterialReplacement.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public MaterialReplacement(ConfigNode node) : base(node)
3232
{
3333
var defName = node.GetValue("materialDef");
3434
if (string.IsNullOrEmpty(defName)) {
35-
Debug.LogError("[Shabby] material replacement must reference a material definition");
35+
Shabby.LogError("material replacement must reference a material definition");
3636
return;
3737
}
3838

3939
if (!MaterialDefLibrary.items.TryGetValue(defName, out materialDef)) {
40-
Debug.LogError($"[Shabby] failed to find valid material definition {defName}");
40+
Shabby.LogError($"failed to find valid material definition {defName}");
4141
}
4242
}
4343

@@ -93,7 +93,7 @@ static void Postfix(ref GameObject __result, ConfigNode partCfg)
9393
}
9494

9595
var replacementNames = string.Join(", ", replacements.Select(rep => rep.materialDef.name));
96-
Debug.Log($"[Shabby] applied material replacements {replacementNames}");
96+
Shabby.LogDebug($"applied material replacements {replacementNames}");
9797
}
9898
}
9999
}

Source/ModelFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public ModelFilter(ConfigNode node)
3636
targetTransforms = node.GetValuesList("targetTransform").ToHashSet();
3737

3838
if (targetMaterials.Count > 0 && targetTransforms.Count > 0) {
39-
Debug.LogError("[Shabby] model filter may not specify both materials and transforms");
39+
Shabby.LogError("model filter may not specify both materials and transforms");
4040
targetTransforms.Clear();
4141
}
4242

Source/ShabLoader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public class DatabaseLoaderTexture_SHAB : DatabaseLoader<GameDatabase.TextureInf
2929
{
3030
public override IEnumerator Load(UrlDir.UrlFile urlFile, FileInfo file)
3131
{
32-
Debug.Log($"[Shabby] `{urlFile.fullPath}'");
32+
Shabby.Log($"loading '{urlFile.fullPath}'");
3333
var bundle = AssetBundle.LoadFromFile(urlFile.fullPath);
3434
if (!bundle) {
35-
Debug.Log($"[Shabby] could not load {urlFile.fullPath}");
35+
Shabby.LogWarning($"could not load {urlFile.fullPath}");
3636
} else {
3737
Shader[] shaders = bundle.LoadAllAssets<Shader>();
3838
foreach (Shader shader in shaders) {
39-
Debug.Log($"[Shabby] adding {shader.name}");
39+
Shabby.LogDebug($"adding {shader.name}");
4040
Shabby.AddShader(shader);
4141
}
4242
}

Source/Shabby.cs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ You should have received a copy of the GNU General Public License
2121
using System.Collections.Generic;
2222
using System.Reflection;
2323
using System.Reflection.Emit;
24+
using System.Runtime.CompilerServices;
2425
using UnityEngine;
2526
using HarmonyLib;
2627
using Mono.Cecil;
@@ -63,7 +64,7 @@ static Shader FindLoadedShader(string shaderName)
6364
{
6465
Shader shader;
6566
if (loadedShaders.TryGetValue(shaderName, out shader)) {
66-
Debug.Log($"[Shabby] custom shader: {shader.name}");
67+
Log($"custom shader: {shader.name}");
6768
return shader;
6869
}
6970

@@ -81,7 +82,7 @@ public static Shader FindShader(string shaderName)
8182
shader = FindLoadedShader(replacement.shader);
8283

8384
if (shader == null) {
84-
Debug.LogError($"[Shabby] failed to find shader {replacement.shader} to replace {shaderName}");
85+
LogError($"failed to find shader {replacement.shader} to replace {shaderName}");
8586
}
8687
}
8788

@@ -106,7 +107,7 @@ public static void MMPostLoadCallback()
106107
var iconShaderName = iconNode.GetValue("iconShader");
107108
var iconShader = FindShader(iconShaderName ?? "");
108109
if (string.IsNullOrEmpty(shader) || iconShader == null) {
109-
Debug.LogError($"[Shabby] invalid icon shader specification {shader} -> {iconShaderName}");
110+
LogError($"invalid icon shader specification {shader} -> {iconShaderName}");
110111
} else {
111112
iconShaders[shader] = iconShader;
112113
}
@@ -124,7 +125,7 @@ void Awake()
124125
harmony = new Harmony("Shabby");
125126
harmony.PatchAll(Assembly.GetExecutingAssembly());
126127

127-
Debug.Log($"[Shabby] hooked");
128+
LogDebug("hooked");
128129

129130
// Register as an explicit MM callback such that it is run before all reflected
130131
// callbacks (as used by most mods), which may wish to access the MaterialDef library.
@@ -148,7 +149,7 @@ private void Start()
148149

149150
List<MethodBase> callSites = new List<MethodBase>();
150151

151-
Debug.Log($"[Shabby]: Beginning search for callsites");
152+
LogDebug("Beginning search for callsites");
152153

153154
// Don't use appdomain, we don't want to accidentally patch Unity itself and this avoid
154155
// having to iterate on the BCL and Unity assemblies.
@@ -174,7 +175,7 @@ private void Start()
174175
}
175176
}
176177
} catch (Exception ex) {
177-
Debug.LogError($"[Shabby] excpetion while patching {method.Name}: {ex}");
178+
LogError($"exception while patching {method.Name}: {ex}");
178179
}
179180
}
180181
}
@@ -189,7 +190,7 @@ private void Start()
189190
if (assemblyDef == null)
190191
throw new FileLoadException($"Couldn't read assembly \"{kspAssembly.assembly.Location}\"");
191192
} catch (Exception e) {
192-
Debug.LogWarning($"[Shabby] Replace failed for assembly {kspAssembly.name}\n{e}");
193+
LogWarning($"Replace failed for assembly {kspAssembly.name}\n{e}");
193194
continue;
194195
}
195196

@@ -210,8 +211,8 @@ private void Start()
210211
if (callSite == null)
211212
throw new MemberAccessException();
212213
} catch {
213-
Debug.LogWarning(
214-
$"[Shabby] Failed to patch method {assemblyDef.Name}::{typeDef.Name}.{methodDef.Name}");
214+
LogWarning(
215+
$"Failed to patch method {assemblyDef.Name}::{typeDef.Name}.{methodDef.Name}");
215216
break;
216217
}
217218

@@ -233,8 +234,8 @@ private void Start()
233234
if (callSite == mInfo_ShaderFind_Replacement)
234235
continue;
235236

236-
Debug.Log(
237-
$"[Shabby] Patching call site : {callSite.DeclaringType.Assembly.GetName().Name}::{callSite.DeclaringType}.{callSite.Name}");
237+
Log(
238+
$"Patching call site : {callSite.DeclaringType.Assembly.GetName().Name}::{callSite.DeclaringType}.{callSite.Name}");
238239
harmony.Patch(callSite, null, null, new HarmonyMethod(callSiteTranspiler));
239240
}
240241
}
@@ -249,5 +250,31 @@ static IEnumerable<CodeInstruction> CallSiteTranspiler(IEnumerable<CodeInstructi
249250
yield return instruction;
250251
}
251252
}
253+
254+
internal const string LogPrefix = "[Shabby] ";
255+
256+
[System.Diagnostics.Conditional("DEBUG")]
257+
internal static void LogDebug(string message)
258+
{
259+
Debug.Log(LogPrefix + message);
260+
}
261+
262+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
263+
internal static void Log(string message)
264+
{
265+
Debug.Log(LogPrefix + message);
266+
}
267+
268+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
269+
internal static void LogWarning(string message)
270+
{
271+
Debug.LogWarning(LogPrefix + message);
272+
}
273+
274+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
275+
internal static void LogError(string message)
276+
{
277+
Debug.LogError(LogPrefix + message);
278+
}
252279
}
253280
}

Source/Shabby.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<!-- MSBuild Dependencies -->
55
<ItemGroup>
6-
<PackageReference Include="KSPBuildTools" Version="0.0.2-alpha.4" />
6+
<PackageReference Include="KSPBuildTools" Version="0.0.2-alpha.7" />
77
<PackageReference Include="MinVer" Version="5.0.0">
88
<PrivateAssets>all</PrivateAssets>
99
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>

0 commit comments

Comments
 (0)