-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConditionSetSelectable.cs
More file actions
151 lines (133 loc) · 5.21 KB
/
ConditionSetSelectable.cs
File metadata and controls
151 lines (133 loc) · 5.21 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using KSP.Game.Missions;
using Newtonsoft.Json.Linq;
using PatchManager.SassyPatching;
using PatchManager.SassyPatching.Interfaces;
using PatchManager.SassyPatching.Modifiables;
using PatchManager.SassyPatching.Selectables;
namespace PatchManager.Missions.Selectables
{
/// <summary>
/// Selectable for the condition set of a mission.
/// </summary>
public sealed class ConditionSetSelectable : BaseSelectable
{
/// <summary>
/// The mission selectable that this condition set is a child of
/// </summary>
public MissionSelectable MissionSelectable;
/// <summary>
/// The condition set that this selectable represents
/// </summary>
public JObject ConditionSet;
/// <summary>
/// The children of this condition set
/// </summary>
private JArray _children;
public void SetModified()
{
MissionSelectable.SetModified();
}
public override bool WasModified => MissionSelectable.WasModified;
public override void ClearModified()
{
}
/// <summary>
/// Create a new condition set selectable
/// </summary>
/// <param name="missionSelectable">Mission selectable that this condition set is a child of</param>
/// <param name="conditionSet">The condition set that this selectable represents</param>
public ConditionSetSelectable(MissionSelectable missionSelectable, JObject conditionSet)
{
MissionSelectable = missionSelectable;
ConditionSet = conditionSet;
_children = (JArray)conditionSet["Children"]!;
Children = new List<ISelectable>();
Classes = new List<string>
{
"ConditionType",
"ConditionMode"
};
// We aren't going to add the condition type as a child, it will still be editable tho
foreach (var child in _children)
{
var condition = (JObject)child;
var type = condition["ConditionType"]!.Value<string>()!;
Classes.Add(type);
if (type == "ConditionSet")
{
Children.Add(new ConditionSetSelectable(missionSelectable, condition));
}
else
{
Children.Add(new JTokenSelectable(
MissionSelectable.SetModified,
condition,
token => ((JObject)token)["ConditionType"]!.Value<string>()!,
type
));
}
}
}
/// <inheritdoc />
public override List<ISelectable> Children { get; }
/// <inheritdoc />
public override string Name => "ConditionSet";
/// <inheritdoc />
public override List<string> Classes { get; }
/// <inheritdoc />
public override bool MatchesClass(string @class, out DataValue classValue)
{
foreach (var child in _children)
{
var condition = (JObject)child;
var type = condition["ConditionType"]!.Value<string>()!;
if (type != @class)
continue;
classValue = DataValue.FromJToken(child);
}
classValue = DataValue.Null;
return false;
}
/// <inheritdoc />
public override bool IsSameAs(ISelectable other) => other is ConditionSetSelectable conditionSetSelectable &&
conditionSetSelectable.ConditionSet == ConditionSet;
/// <inheritdoc />
public override IModifiable OpenModification() => new JTokenModifiable(ConditionSet, SetModified);
/// <inheritdoc />
public override ISelectable AddElement(string elementType)
{
SetModified();
var conditionType = MissionsTypes.Conditions[elementType];
var conditionObject = new JObject()
{
["$type"] = conditionType.AssemblyQualifiedName
};
foreach (var (key, value) in JObject.FromObject(Activator.CreateInstance(conditionType)))
{
conditionObject[key] = value;
}
_children.Add(conditionObject);
if (conditionType == typeof(ConditionSet))
{
var selectable = new ConditionSetSelectable(MissionSelectable, conditionObject);
Children.Add(selectable);
return selectable;
}
else
{
var selectable = new JTokenSelectable(SetModified, conditionObject,
"scriptableCondition", "scriptableCondition");
Children.Add(selectable);
return selectable;
}
}
/// <inheritdoc />
public override string Serialize() => ConditionSet.ToString();
/// <inheritdoc />
public override DataValue GetValue() => DataValue.FromJToken(ConditionSet);
/// <inheritdoc />
public override string ElementType => "ConditionSet";
}
}