-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathConfigTabValueSavedAttribute.cs
68 lines (54 loc) · 2.43 KB
/
ConfigTabValueSavedAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace TechAdvancing
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class ConfigTabValueSavedAttribute : Attribute
{
public string SaveName { get; }
public object DefaultValue { get => attributeDefaultValues[this.SaveName]; }
private bool IsDefaultValueSet => allAttributeSavedNames.Contains(this.SaveName);
internal static readonly Dictionary<string, object> attributeDefaultValues = new Dictionary<string, object>();
private static readonly List<string> allAttributeSavedNames = new List<string>();
private static readonly List<ConfigTabValueSavedAttribute> allAttributes = new List<ConfigTabValueSavedAttribute>();
public ConfigTabValueSavedAttribute(string saveName)
{
this.SaveName = saveName;
if (!allAttributes.Any(x => x.SaveName == saveName))
{
allAttributes.Add(this);
}
}
private static bool defaultValueCacheBuilt = false;
public static void BuildDefaultValueCache()
{
if (!defaultValueCacheBuilt)
{
defaultValueCacheBuilt = true;
RebuildDefaultValueCache();
}
}
public static void RebuildDefaultValueCache()
{
var types = new[] { typeof(TechAdvancing_Config_Tab) };
foreach (var t in types)
{
var props = t.GetProperties().Where(x => x.GetCustomAttribute<ConfigTabValueSavedAttribute>() != null);
foreach (var p in props)
{
var attrib = p.GetCustomAttribute<ConfigTabValueSavedAttribute>();
attributeDefaultValues.Add(attrib.SaveName, p.GetValue(null, null));
allAttributeSavedNames.Add(attrib.SaveName);
}
}
var notFoundAttribs = allAttributes.Where(x => !x.IsDefaultValueSet).ToList();
if (notFoundAttribs.Any())
{
LogOutput.WriteLogMessage(Errorlevel.Error, "Unable to find linked property of one or more attributes. Default config values cannot be determined. Full list:\n"
+ string.Join(", ", notFoundAttribs.Select(x => x.SaveName)));
}
}
}
}