Skip to content

Commit 08f74a8

Browse files
committed
Rebuild persistent variable system so that partmodules are responsible for storing their own variables, and global variables are stored in the vesselmodule
1 parent 62313ef commit 08f74a8

File tree

6 files changed

+156
-327
lines changed

6 files changed

+156
-327
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace JSI.Core
8+
{
9+
internal class PersistentVariableCollection
10+
{
11+
Dictionary<string, double> persistentVars = new Dictionary<string, double>();
12+
13+
public double GetPersistentVariable(string name, double defaultValue)
14+
{
15+
double val;
16+
if (persistentVars.TryGetValue(name, out val))
17+
{
18+
19+
}
20+
else
21+
{
22+
val = defaultValue;
23+
persistentVars[name] = defaultValue;
24+
}
25+
26+
return val;
27+
}
28+
29+
public bool GetPersistentVariable(string name, bool defaultValue)
30+
{
31+
double val = GetPersistentVariable(name, defaultValue ? 1 : 0);
32+
33+
// HACK: if someone had tried to access this persistent var, it will have defaulted to -1 which would be "true"
34+
if (val == -1.0)
35+
{
36+
SetPersistentVariable(name, defaultValue ? 1 : 0);
37+
return defaultValue;
38+
}
39+
return val != 0;
40+
}
41+
42+
public bool HasPersistentVariable(string name)
43+
{
44+
return persistentVars.ContainsKey(name);
45+
}
46+
47+
// returns whether the value changed
48+
public bool SetPersistentVariable(string name, double value)
49+
{
50+
if (name.Trim().Length == 0)
51+
{
52+
JUtil.LogErrorMessage(this, "Trying to set an empty variable name!");
53+
return false;
54+
}
55+
56+
bool valueChanged = true;
57+
58+
if (persistentVars.TryGetValue(name, out double oldValue))
59+
{
60+
valueChanged = JUtil.ValueChanged(oldValue, value);
61+
}
62+
63+
persistentVars[name] = value;
64+
return valueChanged;
65+
}
66+
67+
// returns whether the value changed
68+
public bool SetPersistentVariable(string name, bool value)
69+
{
70+
return SetPersistentVariable(name, value ? 1 : 0);
71+
}
72+
73+
public void Load(ConfigNode baseNode)
74+
{
75+
var varNode = baseNode.GetNode("PERSISTENT_VARS");
76+
if (varNode != null)
77+
{
78+
foreach (var value in varNode.values.values)
79+
{
80+
if (double.TryParse(value.value, out double dblValue))
81+
{
82+
persistentVars.Add(value.name, dblValue);
83+
}
84+
else
85+
{
86+
JUtil.LogErrorMessage(null, "Failed to parse {0} = {1} as a double when loading persistent variables", value.name, value.value);
87+
}
88+
}
89+
}
90+
}
91+
92+
public void Save(ConfigNode baseNode)
93+
{
94+
var varNode = baseNode.AddNode("PERSISTENT_VARS");
95+
96+
foreach (var variable in persistentVars)
97+
{
98+
varNode.AddValue(variable.Key, variable.Value);
99+
}
100+
}
101+
}
102+
}

RasterPropMonitor/Core/RPMCPersistence.cs

Lines changed: 39 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -20,120 +20,72 @@
2020
****************************************************************************/
2121
using System;
2222
using System.Collections.Generic;
23+
using JSI.Core;
2324
using UnityEngine;
2425

2526
namespace JSI
2627
{
2728
public partial class RasterPropMonitorComputer : PartModule
2829
{
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();
3431

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)
4633
{
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+
}
6336

64-
return val;
37+
internal double GetPersistentVariable(string name, double defaultValue, bool global)
38+
{
39+
return GetVariableCollection(global).GetPersistentVariable(name, defaultValue);
6540
}
6641

67-
internal bool GetPersistentVariable(string name, bool defaultValue, bool broadcast)
42+
internal bool GetPersistentVariable(string name, bool defaultValue, bool global)
6843
{
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);
7845
}
7946

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)
8848
{
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);
10250
}
10351

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)
11153
{
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;
11857

119-
RPMVesselComputer comp = RPMVesselComputer.Instance(vessel);
58+
// we need to go update the variableCollections....
12059

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)
12464
{
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+
}
12675
}
127-
128-
if(broadcast)
76+
else
12977
{
130-
comp.SetPersistentVariable(name, value);
78+
var vc = variableCollection.GetVariable(varName);
79+
if (vc != null)
80+
{
81+
vc.Update(vesselComp);
82+
}
13183
}
13284
}
13385

134-
internal void SetPersistentVariable(string name, bool value, bool broadcast)
86+
internal void SetPersistentVariable(string name, bool value, bool global)
13587
{
136-
SetPersistentVariable(name, value ? 1 : 0, broadcast);
88+
SetPersistentVariable(name, value ? 1 : 0, global);
13789
}
13890
}
13991
}

RasterPropMonitor/Core/RPMVCEvaluators.cs

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -19,88 +19,14 @@
1919
* along with RasterPropMonitor. If not, see <http://www.gnu.org/licenses/>.
2020
****************************************************************************/
2121
using System;
22+
using JSI.Core;
2223
using UnityEngine;
2324

2425
namespace JSI
2526
{
2627
public partial class RPMVesselComputer : VesselModule
2728
{
28-
/// <summary>
29-
/// Do a "broadcast" GetPersistentVariable, where we iterate over every
30-
/// part in the craft and find each RasterPropMonitorComputer. Return
31-
/// the value from the first one that contains the variable, or the
32-
/// default value otherwise.
33-
/// </summary>
34-
/// <param name="name"></param>
35-
/// <param name="defaultValue"></param>
36-
/// <returns></returns>
37-
internal double GetPersistentVariable(string name, double defaultValue)
38-
{
39-
if (vessel != null)
40-
{
41-
for (int partIdx = 0; partIdx < vessel.parts.Count; ++partIdx)
42-
{
43-
RasterPropMonitorComputer rpmc = vessel.parts[partIdx].FindModuleImplementing<RasterPropMonitorComputer>();
44-
if (rpmc != null)
45-
{
46-
if (rpmc.HasPersistentVariable(name, false))
47-
{
48-
return rpmc.GetPersistentVariable(name, defaultValue, false);
49-
}
50-
}
51-
}
52-
}
53-
54-
return defaultValue;
55-
}
56-
57-
/// <summary>
58-
/// Iterate over every part in the craft to see if any
59-
/// RasterPropMonitorComputer instances have the specified persistent
60-
/// variable.
61-
/// </summary>
62-
/// <param name="name"></param>
63-
/// <returns></returns>
64-
internal bool HasPersistentVariable(string name)
65-
{
66-
if (vessel != null)
67-
{
68-
for (int partIdx = 0; partIdx < vessel.parts.Count; ++partIdx)
69-
{
70-
RasterPropMonitorComputer rpmc = vessel.parts[partIdx].FindModuleImplementing<RasterPropMonitorComputer>();
71-
if (rpmc != null)
72-
{
73-
if (rpmc.HasPersistentVariable(name, false))
74-
{
75-
return true;
76-
}
77-
}
78-
}
79-
}
80-
81-
return false;
82-
}
83-
84-
/// <summary>
85-
/// Iterate over all RPMC objects in the craft and set the specified
86-
/// persistent var to the same value.
87-
/// </summary>
88-
/// <param name="name"></param>
89-
/// <param name="value"></param>
90-
internal void SetPersistentVariable(string name, double value)
91-
{
92-
if (vessel != null)
93-
{
94-
for (int partIdx = 0; partIdx < vessel.parts.Count; ++partIdx)
95-
{
96-
RasterPropMonitorComputer rpmc = vessel.parts[partIdx].FindModuleImplementing<RasterPropMonitorComputer>();
97-
if (rpmc != null)
98-
{
99-
rpmc.SetPersistentVariable(name, value, false);
100-
}
101-
}
102-
}
103-
}
29+
internal readonly PersistentVariableCollection PersistentVariables = new PersistentVariableCollection();
10430

10531
//--- Fallback evaluators
10632
#region FallbackEvaluators

0 commit comments

Comments
 (0)