forked from Whiplash141/SpaceEngineersScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminalPropertiesHelper.cs
More file actions
76 lines (62 loc) · 2.38 KB
/
Copy pathTerminalPropertiesHelper.cs
File metadata and controls
76 lines (62 loc) · 2.38 KB
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
static class TerminalPropertiesHelper
{
static Dictionary<Type, Dictionary<string, ITerminalAction>> _terminalActionDict = new Dictionary<Type, Dictionary<string, ITerminalAction>>();
static Dictionary<Type, Dictionary<string, ITerminalProperty>> _terminalPropertyDict = new Dictionary<Type, Dictionary<string, ITerminalProperty>>();
public static ITerminalAction GetAction(IMyTerminalBlock block, string actionName)
{
Type type = block.GetType();
Dictionary<string, ITerminalAction> dict;
ITerminalAction act;
if (!_terminalActionDict.TryGetValue(type, out dict))
{
dict = new Dictionary<string, ITerminalAction>();
}
if (dict.TryGetValue(actionName, out act))
{
return act;
}
act = block.GetActionWithName(actionName);
dict[actionName] = act;
_terminalActionDict[type] = dict;
return act;
}
public static void ApplyAction(IMyTerminalBlock block, string actionName)
{
ITerminalAction act = GetAction(block, actionName);
if (act != null)
act.Apply(block);
}
public static ITerminalProperty<T> GetProperty<T>(IMyTerminalBlock block, string propertyName)
{
Type type = block.GetType();
Dictionary<string, ITerminalProperty> dict;
ITerminalProperty prop;
if (!_terminalPropertyDict.TryGetValue(type, out dict))
{
dict = new Dictionary<string, ITerminalProperty>();
}
if (dict.TryGetValue(propertyName, out prop))
{
return prop.Cast<T>();
}
prop = block.GetProperty(propertyName);
dict[propertyName] = prop;
_terminalPropertyDict[type] = dict;
if (prop == null)
return null;
return prop.Cast<T>();
}
public static void SetValue<T>(IMyTerminalBlock block, string propertyName, T value)
{
ITerminalProperty<T> prop = GetProperty<T>(block, propertyName);
if (prop != null)
prop.SetValue(block, value);
}
public static T GetValue<T>(IMyTerminalBlock block, string propertyName)
{
ITerminalProperty<T> prop = GetProperty<T>(block, propertyName);
if (prop != null)
return prop.GetValue(block);
return default(T);
}
}