-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDialogLoader.cs
231 lines (198 loc) · 7.37 KB
/
DialogLoader.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DComposer
{
public static class DialogLoader
{
public static void Save(DialogPage page, string filename)
{
dialogs dlgs = new dialogs();
translate_page(page, dlgs);
JsonSerializer serializer = new JsonSerializer();
serializer.NullValueHandling = NullValueHandling.Ignore;
serializer.Formatting = Formatting.Indented;
using (StreamWriter sw = new StreamWriter(filename))
{
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, dlgs);
}
}
}
private static void translate_page(DialogPage page, dialogs dlgs)
{
if (page == null) return;
dialog dlg = new dialog();
dlgs.items.Add(dlg);
dlg.name = page.Label;
dlg.starting_text = page.DisplayText;
foreach(var option in page.Options)
{
if (String.IsNullOrWhiteSpace(option.DisplayText)) continue;
choice ch = new choice();
ch.text = option.DisplayText;
ch.tree = option.Label;
if (!String.IsNullOrWhiteSpace(option.Command))
ch.target = option.Command;
else
ch.target = "self";
foreach(var condition in option.Conditions)
{
if ( String.IsNullOrEmpty(condition.Skill) )
{
continue;
}
condition con = new condition();
con.name = condition.Skill;
con.op = condition.TypeString;
con.value = condition.Value;
ch.conditions.Add(con);
}
foreach (var modifier in option.Modifiers)
{
if (String.IsNullOrEmpty(modifier.Skill))
{
continue;
}
modifiers mod = new modifiers();
mod.name = modifier.Skill;
mod.op = modifier.TypeString;
mod.value = modifier.Value;
ch.modifiers.Add(mod);
}
dlg.choices.Add(ch);
translate_page(option.Target, dlgs);
}
}
private static DialogPage translate_dialog(dialog dlg)
{
DialogPage dp = new DialogPage(null);
dp.Label = dlg.name;
dp.DisplayText = dlg.starting_text;
dp.Options = new ObservableCollection<DialogOption>();
foreach( var choice in dlg.choices)
{
DialogOption option = new DialogOption();
option.LabelBinding = choice.tree;
option.DisplayText = choice.text;
if (choice.target != "self")
option.Command = choice.target;
for(int i = 0; i < choice.conditions.Count; i++)
{
option.Conditions[i].Skill = choice.conditions[i].name;
option.Conditions[i].Value = choice.conditions[i].value;
switch( choice.conditions[i].op)
{
case "=":
option.Conditions[i].Type = ConditionTypes.Equal;
break;
case "!":
option.Conditions[i].Type = ConditionTypes.NotEqual;
break;
case ">":
option.Conditions[i].Type = ConditionTypes.GreaterThan;
break;
case "<":
option.Conditions[i].Type = ConditionTypes.LessThan;
break;
}
}
for (int i = 0; i < choice.modifiers.Count; i++)
{
option.Modifiers[i].Skill = choice.modifiers[i].name;
option.Modifiers[i].Value = choice.modifiers[i].value;
switch (choice.modifiers[i].op)
{
case "=":
option.Modifiers[i].Type = ModifierTypes.Equal;
break;
case "+":
option.Modifiers[i].Type = ModifierTypes.Add;
break;
case "-":
option.Modifiers[i].Type = ModifierTypes.Subtract;
break;
case "*":
option.Modifiers[i].Type = ModifierTypes.Multiply;
break;
case "/":
option.Modifiers[i].Type = ModifierTypes.Divide;
break;
case "^":
option.Modifiers[i].Type = ModifierTypes.Execute;
break;
}
}
dp.Options.Add(option);
}
return dp;
}
public static DialogPage Load(string filename)
{
dialogs dlgs = null;
JsonSerializer serializer = new JsonSerializer();
serializer.NullValueHandling = NullValueHandling.Ignore;
serializer.Formatting = Formatting.Indented;
using (StreamReader sr = new StreamReader(filename))
{
using (JsonReader reader = new JsonTextReader(sr))
{
dlgs = serializer.Deserialize<dialogs>(reader);
}
}
Dictionary<string, DialogPage> pages = new Dictionary<string, DialogPage>();
foreach(var dlg in dlgs.items)
{
var page = translate_dialog(dlg);
pages[page.Label] = page;
}
foreach(var page in pages.Values)
{
foreach(var option in page.Options)
{
if (option.LabelBinding != null && pages.ContainsKey(option.LabelBinding))
option.Target = pages[option.LabelBinding];
}
}
var rootPage = pages[dlgs.items[0].name];
return rootPage;
}
}
class dialogs
{
public List<dialog> items = new List<dialog>();
}
class dialog
{
public List<choice> choices = new List<choice>();
public string name;
public string starting_text;
}
class choice
{
public List<condition> conditions = new List<condition>();
public List<modifiers> modifiers = new List<modifiers>();
public string text;
public string callback;
public string target;
public string tree;
}
class condition
{
public string name;
public string op;
public string value;
}
class modifiers
{
public string name;
public string op;
public string value;
}
}