Skip to content

Commit 7cd72de

Browse files
committed
change MaterialDef nodes to all caps and better diagnostics
1 parent 5d76876 commit 7cd72de

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

Source/MaterialDef.cs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,35 +79,38 @@ public MaterialDef(ConfigNode node)
7979
isValid = false;
8080
}
8181

82-
keywords = LoadDictionary<bool>(node.GetNode("Keyword"));
83-
floats = LoadDictionary<float>(node.GetNode("Float"));
82+
keywords = LoadDictionary<bool>(node, "KEYWORD");
83+
floats = LoadDictionary<float>(node, "FLOAT");
8484
colors = LoadDictionary<Color>(
85-
node.GetNode("Color"),
85+
node, "COLOR",
8686
value => ParseColor(value, out var color) ? (object)color : null);
87-
vectors = LoadDictionary<Vector4>(node.GetNode("Vector"));
87+
vectors = LoadDictionary<Vector4>(node, "VECTOR");
8888
textures = LoadDictionary<Texture>(
89-
node.GetNode("Texture"),
89+
node, "TEXTURE",
9090
value => GameDatabase.Instance.GetTexture(value, asNormalMap: false));
9191
}
9292

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

97-
Dictionary<string, T> LoadDictionary<T>(ConfigNode node, Func<string, object> parser = null)
97+
Dictionary<string, T> LoadDictionary<T>(ConfigNode defNode, string propKind, Func<string, object> parser = null)
9898
{
9999
var items = new Dictionary<string, T>();
100-
if (node == null) return items;
101100

102-
foreach (ConfigNode.Value item in node.values) {
101+
var propNode = defNode.GetNode(propKind);
102+
if (propNode == null) return items;
103+
104+
foreach (ConfigNode.Value item in propNode.values) {
103105
object value = parser != null ? parser(item.value) : ReadValue(typeof(T), item.value);
104106
if (value is T parsed) {
105107
items[item.name] = parsed;
106108
} else {
107-
Debug.LogError($"[Shabby][MaterialDef {name}] failed to load {typeof(T).Name} property {item.name} = {item.value}");
109+
Debug.LogError($"[Shabby][MaterialDef {name}] failed to load {propKind} property {item.name} = {item.value}");
108110
}
109111
}
110112

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

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

124+
static bool CheckProperty(Material mat, string propName)
125+
{
126+
var exists = mat.HasProperty(propName);
127+
if (!exists) Debug.LogWarning($"[Shabby] shader {mat.shader.name} does not have property {propName}");
128+
return exists;
129+
}
130+
121131
/// <summary>
122132
/// Create a new material based on this definition. The material name is copied from the
123133
/// passed reference material. In update-existing mode, all properties are also copied from
@@ -139,17 +149,26 @@ public Material Instantiate(Material referenceMaterial)
139149
if (preserveRenderQueue) material.renderQueue = referenceMaterial.renderQueue;
140150

141151
foreach (var kvp in keywords) {
152+
if (!CheckProperty(material, kvp.Key)) continue;
142153
if (kvp.Value) material.EnableKeyword(kvp.Key);
143154
else material.DisableKeyword(kvp.Key);
144155
}
145156

146-
foreach (var kvp in floats) material.SetFloat(kvp.Key, kvp.Value);
157+
foreach (var kvp in floats) {
158+
if (CheckProperty(material, kvp.Key)) material.SetFloat(kvp.Key, kvp.Value);
159+
}
147160

148-
foreach (var kvp in colors) material.SetColor(kvp.Key, kvp.Value);
161+
foreach (var kvp in colors) {
162+
if (CheckProperty(material, kvp.Key)) material.SetColor(kvp.Key, kvp.Value);
163+
}
149164

150-
foreach (var kvp in vectors) material.SetVector(kvp.Key, kvp.Value);
165+
foreach (var kvp in vectors) {
166+
if (CheckProperty(material, kvp.Key)) material.SetVector(kvp.Key, kvp.Value);
167+
}
151168

152-
foreach (var kvp in textures) material.SetTexture(kvp.Key, kvp.Value);
169+
foreach (var kvp in textures) {
170+
if (CheckProperty(material, kvp.Key)) material.SetTexture(kvp.Key, kvp.Value);
171+
}
153172

154173
return material;
155174
}

Source/MaterialReplacement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public MaterialReplacement(ConfigNode node) : base(node)
3737
return;
3838
}
3939
if (!MaterialDefLibrary.items.TryGetValue(defName, out materialDef)) {
40-
Debug.LogError($"[Shabby] failed to find material definition {defName}");
40+
Debug.LogError($"[Shabby] failed to find valid material definition {defName}");
4141
}
4242
}
4343

Source/ModelFilter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ You should have received a copy of the GNU General Public License
2222

2323
namespace Shabby
2424
{
25+
2526
public class ModelFilter
2627
{
2728
public readonly HashSet<string> targetMaterials;

Source/Shabby.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static void MMPostLoadCallback()
104104
var iconShaderName = iconNode.GetValue("iconShader");
105105
var iconShader = FindShader(iconShaderName ?? "");
106106
if (string.IsNullOrEmpty(shader) || iconShader == null) {
107-
Debug.Log($"[Shabby] invalid icon shader specification {shader} -> {iconShaderName}");
107+
Debug.LogError($"[Shabby] invalid icon shader specification {shader} -> {iconShaderName}");
108108
} else {
109109
iconShaders[shader] = iconShader;
110110
}

0 commit comments

Comments
 (0)