Skip to content

Commit

Permalink
change MaterialDef nodes to all caps and better diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
al2me6 committed Aug 4, 2024
1 parent 5d76876 commit 7cd72de
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
45 changes: 32 additions & 13 deletions Source/MaterialDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,35 +79,38 @@ public MaterialDef(ConfigNode node)
isValid = false;
}

keywords = LoadDictionary<bool>(node.GetNode("Keyword"));
floats = LoadDictionary<float>(node.GetNode("Float"));
keywords = LoadDictionary<bool>(node, "KEYWORD");
floats = LoadDictionary<float>(node, "FLOAT");
colors = LoadDictionary<Color>(
node.GetNode("Color"),
node, "COLOR",
value => ParseColor(value, out var color) ? (object)color : null);
vectors = LoadDictionary<Vector4>(node.GetNode("Vector"));
vectors = LoadDictionary<Vector4>(node, "VECTOR");
textures = LoadDictionary<Texture>(
node.GetNode("Texture"),
node, "TEXTURE",
value => GameDatabase.Instance.GetTexture(value, asNormalMap: false));
}

static readonly Func<Type, string, object> ReadValue =
AccessTools.MethodDelegate<Func<Type, string, object>>(
AccessTools.DeclaredMethod(typeof(ConfigNode), "ReadValue"));

Dictionary<string, T> LoadDictionary<T>(ConfigNode node, Func<string, object> parser = null)
Dictionary<string, T> LoadDictionary<T>(ConfigNode defNode, string propKind, Func<string, object> parser = null)
{
var items = new Dictionary<string, T>();
if (node == null) return items;

foreach (ConfigNode.Value item in node.values) {
var propNode = defNode.GetNode(propKind);
if (propNode == null) return items;

foreach (ConfigNode.Value item in propNode.values) {
object value = parser != null ? parser(item.value) : ReadValue(typeof(T), item.value);
if (value is T parsed) {
items[item.name] = parsed;
} else {
Debug.LogError($"[Shabby][MaterialDef {name}] failed to load {typeof(T).Name} property {item.name} = {item.value}");
Debug.LogError($"[Shabby][MaterialDef {name}] failed to load {propKind} property {item.name} = {item.value}");
}
}

Debug.Log($"[Shabby][MaterialDef {name}] loaded {items.Count} {propKind} properties");
return items;
}

Expand All @@ -118,6 +121,13 @@ public static bool ParseColor(string value, out Color color)
return false;
}

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}");
return exists;
}

/// <summary>
/// Create a new material based on this definition. The material name is copied from the
/// passed reference material. In update-existing mode, all properties are also copied from
Expand All @@ -139,17 +149,26 @@ public Material Instantiate(Material referenceMaterial)
if (preserveRenderQueue) material.renderQueue = referenceMaterial.renderQueue;

foreach (var kvp in keywords) {
if (!CheckProperty(material, kvp.Key)) continue;
if (kvp.Value) material.EnableKeyword(kvp.Key);
else material.DisableKeyword(kvp.Key);
}

foreach (var kvp in floats) material.SetFloat(kvp.Key, kvp.Value);
foreach (var kvp in floats) {
if (CheckProperty(material, kvp.Key)) material.SetFloat(kvp.Key, kvp.Value);
}

foreach (var kvp in colors) material.SetColor(kvp.Key, kvp.Value);
foreach (var kvp in colors) {
if (CheckProperty(material, kvp.Key)) material.SetColor(kvp.Key, kvp.Value);
}

foreach (var kvp in vectors) material.SetVector(kvp.Key, kvp.Value);
foreach (var kvp in vectors) {
if (CheckProperty(material, kvp.Key)) material.SetVector(kvp.Key, kvp.Value);
}

foreach (var kvp in textures) material.SetTexture(kvp.Key, kvp.Value);
foreach (var kvp in textures) {
if (CheckProperty(material, kvp.Key)) material.SetTexture(kvp.Key, kvp.Value);
}

return material;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/MaterialReplacement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public MaterialReplacement(ConfigNode node) : base(node)
return;
}
if (!MaterialDefLibrary.items.TryGetValue(defName, out materialDef)) {
Debug.LogError($"[Shabby] failed to find material definition {defName}");
Debug.LogError($"[Shabby] failed to find valid material definition {defName}");
}
}

Expand Down
1 change: 1 addition & 0 deletions Source/ModelFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ You should have received a copy of the GNU General Public License

namespace Shabby
{

public class ModelFilter
{
public readonly HashSet<string> targetMaterials;
Expand Down
2 changes: 1 addition & 1 deletion Source/Shabby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static void MMPostLoadCallback()
var iconShaderName = iconNode.GetValue("iconShader");
var iconShader = FindShader(iconShaderName ?? "");
if (string.IsNullOrEmpty(shader) || iconShader == null) {
Debug.Log($"[Shabby] invalid icon shader specification {shader} -> {iconShaderName}");
Debug.LogError($"[Shabby] invalid icon shader specification {shader} -> {iconShaderName}");
} else {
iconShaders[shader] = iconShader;
}
Expand Down

0 comments on commit 7cd72de

Please sign in to comment.