Skip to content

Commit

Permalink
"better" logging
Browse files Browse the repository at this point in the history
Got don't get mad at me
  • Loading branch information
drewcassidy committed Oct 22, 2024
1 parent 781ca4f commit 6a9f79f
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 29 deletions.
6 changes: 3 additions & 3 deletions Source/IconMaterialPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -80,12 +80,12 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> 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;
}
}
Expand Down
31 changes: 24 additions & 7 deletions Source/MaterialDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -108,12 +113,12 @@ Dictionary<string, T> LoadDictionary<T>(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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
}
}
6 changes: 3 additions & 3 deletions Source/MaterialReplacement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}

Expand Down Expand Up @@ -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}");
}
}
}
2 changes: 1 addition & 1 deletion Source/ModelFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
6 changes: 3 additions & 3 deletions Source/ShabLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public class DatabaseLoaderTexture_SHAB : DatabaseLoader<GameDatabase.TextureInf
{
public override IEnumerator Load(UrlDir.UrlFile urlFile, FileInfo file)
{
Debug.Log($"[Shabby] `{urlFile.fullPath}'");
Shabby.Log($"loading '{urlFile.fullPath}'");
var bundle = AssetBundle.LoadFromFile(urlFile.fullPath);
if (!bundle) {
Debug.Log($"[Shabby] could not load {urlFile.fullPath}");
Shabby.LogWarning($"could not load {urlFile.fullPath}");
} else {
Shader[] shaders = bundle.LoadAllAssets<Shader>();
foreach (Shader shader in shaders) {
Debug.Log($"[Shabby] adding {shader.name}");
Shabby.LogDebug($"adding {shader.name}");
Shabby.AddShader(shader);
}
}
Expand Down
49 changes: 38 additions & 11 deletions Source/Shabby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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}");
}
}

Expand All @@ -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;
}
Expand All @@ -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.
Expand All @@ -148,7 +149,7 @@ private void Start()

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

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.
Expand All @@ -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}");
}
}
}
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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));
}
}
Expand All @@ -249,5 +250,31 @@ static IEnumerable<CodeInstruction> CallSiteTranspiler(IEnumerable<CodeInstructi
yield return instruction;
}
}

internal const string LogPrefix = "[Shabby] ";

[System.Diagnostics.Conditional("DEBUG")]
internal static void LogDebug(string message)
{
Debug.Log(LogPrefix + message);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Log(string message)
{
Debug.Log(LogPrefix + message);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void LogWarning(string message)
{
Debug.LogWarning(LogPrefix + message);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void LogError(string message)
{
Debug.LogError(LogPrefix + message);
}
}
}
2 changes: 1 addition & 1 deletion Source/Shabby.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<!-- MSBuild Dependencies -->
<ItemGroup>
<PackageReference Include="KSPBuildTools" Version="0.0.2-alpha.4" />
<PackageReference Include="KSPBuildTools" Version="0.0.2-alpha.7" />
<PackageReference Include="MinVer" Version="5.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down

0 comments on commit 6a9f79f

Please sign in to comment.