|
20 | 20 | ****************************************************************************/ |
21 | 21 | using System; |
22 | 22 | using System.Collections.Generic; |
| 23 | +using JSI.Core; |
23 | 24 | using UnityEngine; |
24 | 25 |
|
25 | 26 | namespace JSI |
26 | 27 | { |
27 | 28 | public partial class RasterPropMonitorComputer : PartModule |
28 | 29 | { |
29 | | - /// <summary> |
30 | | - /// Per-pod persistence. This code was devolved from RPMVC due to |
31 | | - /// difficulties handling docking and undocking. |
32 | | - /// </summary> |
33 | | - internal Dictionary<string, double> persistentVars = new Dictionary<string, double>(); |
| 30 | + internal readonly PersistentVariableCollection m_persistentVariables = new PersistentVariableCollection(); |
34 | 31 |
|
35 | | - /// <summary> |
36 | | - /// Returns the named persistent value, or the default provided if |
37 | | - /// it's not set. The persistent value is initialized to the default |
38 | | - /// if the default is used. If 'broadcast' is set, other RPMC on the |
39 | | - /// same vessel are queried, as well. |
40 | | - /// </summary> |
41 | | - /// <param name="name">Name of the persistent</param> |
42 | | - /// <param name="defaultValue">The default value</param> |
43 | | - /// <param name="broadcast">Broadcast the request to other parts of the same craft?</param> |
44 | | - /// <returns></returns> |
45 | | - internal double GetPersistentVariable(string name, double defaultValue, bool broadcast) |
| 32 | + PersistentVariableCollection GetVariableCollection(bool global) |
46 | 33 | { |
47 | | - double val; |
48 | | - if (persistentVars.TryGetValue(name, out val)) |
49 | | - { |
50 | | - |
51 | | - } |
52 | | - else if (broadcast) |
53 | | - { |
54 | | - RPMVesselComputer comp = RPMVesselComputer.Instance(vessel); |
55 | | - val = comp.GetPersistentVariable(name, defaultValue); |
56 | | - persistentVars[name] = val; |
57 | | - } |
58 | | - else |
59 | | - { |
60 | | - val = defaultValue; |
61 | | - persistentVars[name] = defaultValue; |
62 | | - } |
| 34 | + return global ? RPMVesselComputer.Instance(vessel).PersistentVariables : m_persistentVariables; |
| 35 | + } |
63 | 36 |
|
64 | | - return val; |
| 37 | + internal double GetPersistentVariable(string name, double defaultValue, bool global) |
| 38 | + { |
| 39 | + return GetVariableCollection(global).GetPersistentVariable(name, defaultValue); |
65 | 40 | } |
66 | 41 |
|
67 | | - internal bool GetPersistentVariable(string name, bool defaultValue, bool broadcast) |
| 42 | + internal bool GetPersistentVariable(string name, bool defaultValue, bool global) |
68 | 43 | { |
69 | | - double val = GetPersistentVariable(name, defaultValue ? 1 : 0, broadcast); |
70 | | - |
71 | | - // HACK: if someone tried to access this persistent var, it will have defaulted to -1 which would be "true" |
72 | | - if (val == -1.0) |
73 | | - { |
74 | | - SetPersistentVariable(name, defaultValue ? 1 : 0, broadcast); |
75 | | - return defaultValue; |
76 | | - } |
77 | | - return val != 0; |
| 44 | + return GetVariableCollection(global).GetPersistentVariable(name, defaultValue); |
78 | 45 | } |
79 | 46 |
|
80 | | - /// <summary> |
81 | | - /// Indicates whether the named persistent variable is present in the |
82 | | - /// dictionary. |
83 | | - /// </summary> |
84 | | - /// <param name="name"></param> |
85 | | - /// <param name="broadcast">Broadcast the request to other parts of the same craft?</param> |
86 | | - /// <returns></returns> |
87 | | - internal bool HasPersistentVariable(string name, bool broadcast) |
| 47 | + internal bool HasPersistentVariable(string name, bool global) |
88 | 48 | { |
89 | | - if(persistentVars.ContainsKey(name)) |
90 | | - { |
91 | | - return true; |
92 | | - } |
93 | | - else if(broadcast) |
94 | | - { |
95 | | - RPMVesselComputer comp = RPMVesselComputer.Instance(vessel); |
96 | | - return comp.HasPersistentVariable(name); |
97 | | - } |
98 | | - else |
99 | | - { |
100 | | - return false; |
101 | | - } |
| 49 | + return GetVariableCollection(global).HasPersistentVariable(name); |
102 | 50 | } |
103 | 51 |
|
104 | | - /// <summary> |
105 | | - /// Set the named persistent variable to the value provided. |
106 | | - /// </summary> |
107 | | - /// <param name="name"></param> |
108 | | - /// <param name="value"></param> |
109 | | - /// <param name="broadcast">Broadcast the request to other parts of the same craft?</param> |
110 | | - internal void SetPersistentVariable(string name, double value, bool broadcast) |
| 52 | + internal void SetPersistentVariable(string name, double value, bool global) |
111 | 53 | { |
112 | | - if (name.Trim().Length == 0) |
113 | | - { |
114 | | - JUtil.LogErrorMessage(this, "Trying to set an empty variable name!"); |
115 | | - return; |
116 | | - } |
117 | | - persistentVars[name] = value; |
| 54 | + bool valueChanged = GetVariableCollection(global).SetPersistentVariable(name, value); |
| 55 | + |
| 56 | + if (!valueChanged) return; |
118 | 57 |
|
119 | | - RPMVesselComputer comp = RPMVesselComputer.Instance(vessel); |
| 58 | + // we need to go update the variableCollections.... |
120 | 59 |
|
121 | | - // TEMP: how can we avoid this string concat? |
122 | | - var vc = variableCollection.GetVariable("PERSISTENT_" + name); |
123 | | - if (vc != null) |
| 60 | + RPMVesselComputer vesselComp = RPMVesselComputer.Instance(vessel); |
| 61 | + string varName = "PERSISTENT_" + name; |
| 62 | + |
| 63 | + if (global) |
124 | 64 | { |
125 | | - vc.Update(comp); |
| 65 | + // TODO: might want to cache this list in the vesselmodule? |
| 66 | + foreach (var part in vessel.parts) |
| 67 | + { |
| 68 | + var rpmc = part.FindModuleImplementing<RasterPropMonitorComputer>(); |
| 69 | + if (rpmc != null) |
| 70 | + { |
| 71 | + var vc = rpmc.variableCollection.GetVariable(varName); |
| 72 | + vc.Update(vesselComp); |
| 73 | + } |
| 74 | + } |
126 | 75 | } |
127 | | - |
128 | | - if(broadcast) |
| 76 | + else |
129 | 77 | { |
130 | | - comp.SetPersistentVariable(name, value); |
| 78 | + var vc = variableCollection.GetVariable(varName); |
| 79 | + if (vc != null) |
| 80 | + { |
| 81 | + vc.Update(vesselComp); |
| 82 | + } |
131 | 83 | } |
132 | 84 | } |
133 | 85 |
|
134 | | - internal void SetPersistentVariable(string name, bool value, bool broadcast) |
| 86 | + internal void SetPersistentVariable(string name, bool value, bool global) |
135 | 87 | { |
136 | | - SetPersistentVariable(name, value ? 1 : 0, broadcast); |
| 88 | + SetPersistentVariable(name, value ? 1 : 0, global); |
137 | 89 | } |
138 | 90 | } |
139 | 91 | } |
0 commit comments