private void btnSyncScenario_Click(object sender, RoutedEventArgs e) { String scenariosPath = @"Content\Data\Scenario\Scenarios.json"; MessageBoxResult result = MessageBox.Show("更新" + scenariosPath + "檔案,使遊戲能辨認劇本資料夾裡的劇本。是否繼續?", "更新Scenarios.json", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { List <GameManager.Scenario> scesList = new List <GameManager.Scenario>(); FileInfo[] files = new DirectoryInfo(@"Content\Data\Scenario").GetFiles("*.json", SearchOption.TopDirectoryOnly); foreach (FileInfo file in files) { if (file.Name.Equals("Scenarios.json")) { continue; } GameScenario s = WorldOfTheThreeKingdoms.GameScreens.MainGameScreen.LoadScenarioData(file.FullName, true, null, true); GameManager.Scenario s1 = createScenarioObject(s, file.Name); if (s1 != null) { scesList.Add(s1); } } string s2 = Newtonsoft.Json.JsonConvert.SerializeObject(scesList, Newtonsoft.Json.Formatting.Indented); File.WriteAllText(scenariosPath, s2); MessageBox.Show("已更新" + scenariosPath); } }
private void SaveCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) { if (scenLoaded) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "劇本檔 (*.json)|*.json"; saveFileDialog.InitialDirectory = @"Content\Data\Scenario"; if (saveFileDialog.ShowDialog() == true) { String filename = saveFileDialog.FileName; String scenName = filename.Substring(filename.LastIndexOf(@"\") + 1, filename.LastIndexOf(".") - filename.LastIndexOf(@"\") - 1); String scenPath = filename.Substring(0, filename.LastIndexOf(@"\")); scen.SaveGameScenario(filename, true, false, false, false, true, true); // GameCommonData.json String commonPath = @"Content\Data\Common\CommonData.json"; saveGameCommonData(commonPath); // Scenarios.json String scenariosPath = scenPath + @"\Scenarios.json"; List <GameManager.Scenario> scesList = null; if (File.Exists(scenariosPath)) { scesList = SimpleSerializer.DeserializeJsonFile <List <GameManager.Scenario> >(scenariosPath, false).NullToEmptyList(); } if (scesList == null) { scesList = new List <GameManager.Scenario>(); } GameManager.Scenario s1 = createScenarioObject(scen, scenName); if (s1 != null) { int index = scesList.FindIndex(x => x.Name == scenName); if (index >= 0) { scesList[index] = s1; } else { scesList.Add(s1); } } string s2 = Newtonsoft.Json.JsonConvert.SerializeObject(scesList, Newtonsoft.Json.Formatting.Indented); File.WriteAllText(scenariosPath, s2); MessageBox.Show("劇本已儲存為" + filename); } } else { // GameCommonData.json String commonPath = @"Content\Data\Common\CommonData.json"; saveGameCommonData(commonPath); MessageBox.Show("CommonData已儲存為" + commonPath); } }
private void SaveCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "劇本檔 (*.json)|*.json"; saveFileDialog.InitialDirectory = @"Content\Data\Scenario"; if (saveFileDialog.ShowDialog() == true) { String filename = saveFileDialog.FileName; String scenName = filename.Substring(filename.LastIndexOf(@"\") + 1, filename.LastIndexOf(".") - filename.LastIndexOf(@"\") - 1); String scenPath = filename.Substring(0, filename.LastIndexOf(@"\")); scen.SaveGameScenario(filename, true, false, false, false, true, true); // GameCommonData.json String commonPath = @"Content\Data\Common\CommonData.json"; GameScenario.SaveGameCommonData(scen); string ss1 = ""; System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(CommonData)); using (MemoryStream stream = new MemoryStream()) { //lock (Platform.SerializerLock) { serializer.WriteObject(stream, scen.GameCommonData); } var array = stream.ToArray(); ss1 = Encoding.UTF8.GetString(array, 0, array.Length); } ss1 = ss1.Replace("{\"", "{\r\n\""); ss1 = ss1.Replace("[{", "[\r\n{"); ss1 = ss1.Replace(",\"", ",\r\n\""); ss1 = ss1.Replace("}", "\r\n}"); ss1 = ss1.Replace("},{", "},\r\n{"); ss1 = ss1.Replace("}]", "}\r\n]"); File.WriteAllText(commonPath, ss1); // Scenarios.json String scenariosPath = scenPath + @"\Scenarios.json"; List <GameManager.Scenario> scesList = null; if (File.Exists(scenariosPath)) { scesList = SimpleSerializer.DeserializeJsonFile <List <GameManager.Scenario> >(scenariosPath, false).NullToEmptyList(); } if (scesList == null) { scesList = new List <GameManager.Scenario>(); } string time = scen.Date.Year + "-" + scen.Date.Month + "-" + scen.Date.Day; GameManager.Scenario s1 = new GameManager.Scenario() { Create = DateTime.Now.ToSeasonDateTime(), Desc = scen.ScenarioDescription, First = StaticMethods.SaveToString(scen.ScenarioMap.JumpPosition), IDs = scen.Factions.GameObjects.Select(x => x.ID.ToString()).Aggregate((a, b) => a + "," + b), Info = "电脑", Name = scenName, Names = scen.Factions.GameObjects.Select(x => x.Name).Aggregate((a, b) => a + "," + b), // Path = "", // PlayTime = scenario.GameTime.ToString(), // Player = "", // Players = String.Join(",", scenario.PlayerList.NullToEmptyList()), Time = time.ToSeasonDate(), Title = scen.ScenarioTitle }; int index = scesList.FindIndex(x => x.Name == scenName); if (index >= 0) { scesList[index] = s1; } else { scesList.Add(s1); } string s2 = Newtonsoft.Json.JsonConvert.SerializeObject(scesList, Newtonsoft.Json.Formatting.Indented); File.WriteAllText(scenariosPath, s2); MessageBox.Show("劇本已儲存為" + filename); } }