Skip to content

Commit ea3d52b

Browse files
committed
make use of logging utilities in KSPBuildTools
1 parent 23b17cb commit ea3d52b

File tree

7 files changed

+39
-61
lines changed

7 files changed

+39
-61
lines changed

Source/IconMaterialPatch.cs

Lines changed: 4 additions & 3 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.Reflection;
2222
using System.Reflection.Emit;
2323
using HarmonyLib;
24+
using KSPBuildTools;
2425
using UnityEngine;
2526

2627
namespace Shabby
@@ -36,7 +37,7 @@ class SetPartIconMaterialsPatch
3637
static Shader FindOverrideIconShader(Material material)
3738
{
3839
if (Shabby.iconShaders.TryGetValue(material.shader.name, out var shader)) {
39-
Shabby.LogDebug($"custom icon shader {material.shader.name} -> {shader.name}");
40+
Log.Debug($"custom icon shader {material.shader.name} -> {shader.name}");
4041
return shader;
4142
}
4243

@@ -80,12 +81,12 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
8081
code[i].opcode = OpCodes.Ldloc_S;
8182
code[i].operand = locMaterial;
8283
code[i + 1].operand = mInfo_FindOverrideIconShader;
83-
Shabby.LogDebug("patched part icon shader replacement");
84+
Log.Debug("patched part icon shader replacement");
8485
return code;
8586
}
8687
}
8788

88-
Shabby.LogError("failed to patch part icon shader replacement");
89+
Log.Error("failed to patch part icon shader replacement");
8990
return code;
9091
}
9192
}

Source/MaterialDef.cs

Lines changed: 10 additions & 16 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.Runtime.CompilerServices;
2222
using HarmonyLib;
2323
using UnityEngine;
24+
using KSPBuildTools;
2425

2526
namespace Shabby
2627
{
@@ -33,15 +34,15 @@ public static void Load()
3334
foreach (var node in GameDatabase.Instance.GetConfigNodes("SHABBY_MATERIAL_DEF")) {
3435
var def = new MaterialDef(node);
3536
if (string.IsNullOrEmpty(def.name) || !def.isValid) {
36-
Shabby.LogError($"[MaterialDef {def.name}] removing invalid definition");
37+
Log.Error($"[MaterialDef {def.name}] removing invalid definition");
3738
} else {
3839
items[def.name] = def;
3940
}
4041
}
4142
}
4243
}
4344

44-
public class MaterialDef
45+
public class MaterialDef : ILogContextProvider
4546
{
4647
[Persistent] public string name;
4748

@@ -76,13 +77,13 @@ public MaterialDef(ConfigNode node)
7677
if (shaderName != null) {
7778
shader = Shabby.FindShader(shaderName);
7879
if (shader == null) {
79-
LogError($"failed to find shader {shaderName}");
80+
this.LogError($"failed to find shader {shaderName}");
8081
isValid = false;
8182
}
8283
}
8384

8485
if (!updateExisting && shader == null) {
85-
LogError($"from-scratch material must define a valid shader");
86+
this.LogError($"from-scratch material must define a valid shader");
8687
isValid = false;
8788
}
8889

@@ -113,12 +114,12 @@ Dictionary<string, T> LoadDictionary<T>(ConfigNode defNode, string propKind, Fun
113114
if (value is T parsed) {
114115
items[item.name] = parsed;
115116
} else {
116-
LogError(
117+
this.LogError(
117118
$"failed to load {propKind} property {item.name} = {item.value}");
118119
}
119120
}
120121

121-
Log($"loaded {items.Count} {propKind} properties");
122+
this.LogMessage($"loaded {items.Count} {propKind} properties");
122123
return items;
123124
}
124125

@@ -132,7 +133,7 @@ public static bool ParseColor(string value, out Color color)
132133
static bool CheckProperty(Material mat, string propName)
133134
{
134135
var exists = mat.HasProperty(propName);
135-
if (!exists) Shabby.LogWarning($"shader {mat.shader.name} does not have property {propName}");
136+
if (!exists) Log.Warning($"shader {mat.shader.name} does not have property {propName}");
136137
return exists;
137138
}
138139

@@ -181,16 +182,9 @@ public Material Instantiate(Material referenceMaterial)
181182
return material;
182183
}
183184

184-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
185-
private void Log(string message)
185+
public string context()
186186
{
187-
Debug.Log(logPrefix + message);
188-
}
189-
190-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
191-
private void LogError(string message)
192-
{
193-
Debug.LogError(logPrefix + message);
187+
return name;
194188
}
195189
}
196190
}

Source/MaterialReplacement.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ You should have received a copy of the GNU General Public License
1919
using System.Collections.Generic;
2020
using System.Linq;
2121
using HarmonyLib;
22+
using KSPBuildTools;
2223
using UnityEngine;
2324

2425
namespace Shabby
@@ -32,12 +33,12 @@ public MaterialReplacement(ConfigNode node) : base(node)
3233
{
3334
var defName = node.GetValue("materialDef");
3435
if (string.IsNullOrEmpty(defName)) {
35-
Shabby.LogError("material replacement must reference a material definition");
36+
Log.Error("material replacement must reference a material definition");
3637
return;
3738
}
3839

3940
if (!MaterialDefLibrary.items.TryGetValue(defName, out materialDef)) {
40-
Shabby.LogError($"failed to find valid material definition {defName}");
41+
Log.Error($"failed to find valid material definition {defName}");
4142
}
4243
}
4344

@@ -93,7 +94,7 @@ static void Postfix(ref GameObject __result, ConfigNode partCfg)
9394
}
9495

9596
var replacementNames = string.Join(", ", replacements.Select(rep => rep.materialDef.name));
96-
Shabby.LogDebug($"applied material replacements {replacementNames}");
97+
Log.Debug($"applied material replacements {replacementNames}");
9798
}
9899
}
99100
}

Source/ModelFilter.cs

Lines changed: 2 additions & 1 deletion
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.Collections.Generic;
2020
using System.Linq;
21+
using KSPBuildTools;
2122
using UnityEngine;
2223

2324
namespace Shabby
@@ -36,7 +37,7 @@ public ModelFilter(ConfigNode node)
3637
targetTransforms = node.GetValuesList("targetTransform").ToHashSet();
3738

3839
if (targetMaterials.Count > 0 && targetTransforms.Count > 0) {
39-
Shabby.LogError("model filter may not specify both materials and transforms");
40+
Log.Error("model filter may not specify both materials and transforms");
4041
targetTransforms.Clear();
4142
}
4243

Source/ShabLoader.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ You should have received a copy of the GNU General Public License
2020
using System.IO;
2121
using System.Collections;
2222
using System.Collections.Generic;
23+
using KSPBuildTools;
2324
using UnityEngine;
2425

2526
namespace Shabby
@@ -29,14 +30,14 @@ public class DatabaseLoaderTexture_SHAB : DatabaseLoader<GameDatabase.TextureInf
2930
{
3031
public override IEnumerator Load(UrlDir.UrlFile urlFile, FileInfo file)
3132
{
32-
Shabby.Log($"loading '{urlFile.fullPath}'");
33+
Log.Message($"loading '{urlFile.fullPath}'");
3334
var bundle = AssetBundle.LoadFromFile(urlFile.fullPath);
3435
if (!bundle) {
35-
Shabby.LogWarning($"could not load {urlFile.fullPath}");
36+
Log.Warning($"could not load {urlFile.fullPath}");
3637
} else {
3738
Shader[] shaders = bundle.LoadAllAssets<Shader>();
3839
foreach (Shader shader in shaders) {
39-
Shabby.LogDebug($"adding {shader.name}");
40+
Log.Debug($"adding {shader.name}");
4041
Shabby.AddShader(shader);
4142
}
4243
}

Source/Shabby.cs

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License
2424
using System.Runtime.CompilerServices;
2525
using UnityEngine;
2626
using HarmonyLib;
27+
using KSPBuildTools;
2728
using Mono.Cecil;
2829
using Mono.Cecil.Cil;
2930
using Mono.Cecil.Rocks;
@@ -64,7 +65,7 @@ static Shader FindLoadedShader(string shaderName)
6465
{
6566
Shader shader;
6667
if (loadedShaders.TryGetValue(shaderName, out shader)) {
67-
Log($"custom shader: {shader.name}");
68+
Log.Message($"custom shader: {shader.name}");
6869
return shader;
6970
}
7071

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

8485
if (shader == null) {
85-
LogError($"failed to find shader {replacement.shader} to replace {shaderName}");
86+
Log.Error($"failed to find shader {replacement.shader} to replace {shaderName}");
8687
}
8788
}
8889

@@ -107,7 +108,7 @@ public static void MMPostLoadCallback()
107108
var iconShaderName = iconNode.GetValue("iconShader");
108109
var iconShader = FindShader(iconShaderName ?? "");
109110
if (string.IsNullOrEmpty(shader) || iconShader == null) {
110-
LogError($"invalid icon shader specification {shader} -> {iconShaderName}");
111+
Log.Error($"invalid icon shader specification {shader} -> {iconShaderName}");
111112
} else {
112113
iconShaders[shader] = iconShader;
113114
}
@@ -119,13 +120,14 @@ public static void MMPostLoadCallback()
119120

120121
void Awake()
121122
{
123+
Debug.Log("Test context (shibboleth)", this);
122124
if (loadedShaders == null) {
123125
loadedShaders = new Dictionary<string, Shader>();
124126

125127
harmony = new Harmony("Shabby");
126128
harmony.PatchAll(Assembly.GetExecutingAssembly());
127129

128-
LogDebug("hooked");
130+
Log.Debug("hooked");
129131

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

150152
List<MethodBase> callSites = new List<MethodBase>();
151153

152-
LogDebug("Beginning search for callsites");
154+
Log.Debug("Beginning search for callsites");
153155

154156
// Don't use appdomain, we don't want to accidentally patch Unity itself and this avoid
155157
// having to iterate on the BCL and Unity assemblies.
@@ -190,7 +192,7 @@ private void Start()
190192
if (assemblyDef == null)
191193
throw new FileLoadException($"Couldn't read assembly \"{kspAssembly.assembly.Location}\"");
192194
} catch (Exception e) {
193-
LogWarning($"Replace failed for assembly {kspAssembly.name}\n{e}");
195+
Log.Warning($"Replace failed for assembly {kspAssembly.name}\n{e}");
194196
continue;
195197
}
196198

@@ -211,7 +213,7 @@ private void Start()
211213
if (callSite == null)
212214
throw new MemberAccessException();
213215
} catch {
214-
LogWarning(
216+
Log.Warning(
215217
$"Failed to patch method {assemblyDef.Name}::{typeDef.Name}.{methodDef.Name}");
216218
break;
217219
}
@@ -234,7 +236,7 @@ private void Start()
234236
if (callSite == mInfo_ShaderFind_Replacement)
235237
continue;
236238

237-
Log(
239+
Log.Debug(
238240
$"Patching call site : {callSite.DeclaringType.Assembly.GetName().Name}::{callSite.DeclaringType}.{callSite.Name}");
239241
harmony.Patch(callSite, null, null, new HarmonyMethod(callSiteTranspiler));
240242
}
@@ -250,31 +252,5 @@ static IEnumerable<CodeInstruction> CallSiteTranspiler(IEnumerable<CodeInstructi
250252
yield return instruction;
251253
}
252254
}
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-
}
279255
}
280256
}

Source/Shabby.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33

44
<!-- MSBuild Dependencies -->
55
<ItemGroup>
6-
<PackageReference Include="KSPBuildTools" Version="0.0.2" />
6+
<PackageReference Include="KSPBuildTools" Version="0.0.3-*"/>
77
<PackageReference Include="MinVer" Version="5.0.0">
88
<PrivateAssets>all</PrivateAssets>
99
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
1010
</PackageReference>
1111
</ItemGroup>
1212

1313
<!-- Static Properties -->
14+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
15+
<DebugType>portable</DebugType>
16+
</PropertyGroup>
17+
1418
<PropertyGroup>
1519
<TargetFramework>net48</TargetFramework>
1620
<LangVersion>7.3</LangVersion>

0 commit comments

Comments
 (0)