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); } } }
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); }
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); } }