-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCTPersistantFIeld.cs
82 lines (66 loc) · 1.71 KB
/
CTPersistantFIeld.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
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Reflection;
namespace CameraTools
{
[AttributeUsage(AttributeTargets.Field)]
public class CTPersistantField : Attribute
{
public static string settingsURL = "GameData/CameraTools/settings.cfg";
public CTPersistantField()
{
}
public static void Save()
{
ConfigNode fileNode = ConfigNode.Load(settingsURL);
ConfigNode settings = fileNode.GetNode("CToolsSettings");
foreach(var field in typeof(CamTools).GetFields())
{
if(!field.IsDefined(typeof(CTPersistantField), false)) continue;
settings.SetValue(field.Name, field.GetValue(CamTools.fetch).ToString(), true);
}
fileNode.Save(settingsURL);
}
public static void Load()
{
ConfigNode fileNode = ConfigNode.Load(settingsURL);
ConfigNode settings = fileNode.GetNode("CToolsSettings");
foreach(var field in typeof(CamTools).GetFields())
{
if(!field.IsDefined(typeof(CTPersistantField), false)) continue;
if(settings.HasValue(field.Name))
{
object parsedValue = ParseValue(field.FieldType, settings.GetValue(field.Name));
if(parsedValue != null)
{
field.SetValue(CamTools.fetch, parsedValue);
}
}
}
}
public static object ParseValue(Type type, string value)
{
if(type == typeof(string))
{
return value;
}
if(type == typeof(bool))
{
return bool.Parse(value);
}
else if(type.IsEnum)
{
return Enum.Parse(type, value);
}
else if(type == typeof(float))
{
return float.Parse(value);
}
else if(type == typeof(Single))
{
return Single.Parse(value);
}
UnityEngine.Debug.LogError("CameraTools failed to parse settings field of type "+type.ToString()+" and value "+value);
return null;
}
}
}