private static void WriteDod(DodJson dod) { try { var ser = new DataContractJsonSerializer(typeof(DodJson)); var s = new MemoryStream(); ser.WriteObject(s, dod); File.WriteAllBytes(dod.filepath, s.ToArray()); } catch (Exception e) { throw new Exception("Error saving trainer: " + dod.filepath + "\r\n" + e.Message); } }
private void NewTrainerBT_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(working_dir_) || dods_.Count == 0) { return; } int id = 0; using (var dialog = new NewIDDialog("New Trainer", 2047)) { if (dialog.ShowDialog() != DialogResult.OK) { return; } id = dialog.ID; } var path = working_dir_ + (is_ynk_ ? "/gn_dat5.arc/script/dollOperator/" : "/gn_dat3.arc/script/dollOperator/") + id.ToString("D4") + ".dod"; if (File.Exists(path)) { ErrMsg("Trainer ID already exists!"); return; } try { byte[] buf = new byte[0x42A]; File.WriteAllBytes(path, buf); } catch (Exception ex) { ErrMsg("Failed to write to file: " + path + "\r\n" + ex.Message); return; } var dod = new DodJson(); dod.filepath = path.Replace(".dod", ".json"); dod.id = id; dod.trainer_name = "New Trainer"; dod.trainer_title = "New Trainer"; dods_.Add(dod); TrainerLB.Items.Add(dod.trainer_name); TrainerLB.SelectedIndex = TrainerLB.Items.Count - 1; WriteDod(dod); }