public void ReadFromCsvFile(ref SpellBook spellBook, string spellBookName = masterSpellFile) { var csvReader = new CsvReader(new StreamReader(spellBookName)); var records = csvReader.GetRecords<Spell>(); foreach (var record in records.OrderBy(x => x.name)) { spellBook.spells.Add(record); } }
public void ReadBinaryFile(string spellBookName, ref SpellBook spellBook) { if (spellBook == null) { throw new ArgumentNullException("spellBook"); } using (var stream = File.OpenRead(spellBookName)) { var newObj = (SpellBook)new BinaryFormatter().Deserialize(stream); spellBook = newObj; } }
/// <summary> /// selects a spell book to load /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SelectSpellBookToolStripMenuItemClick(object sender, EventArgs e) { var ofd = new OpenFileDialog { Title = "Open Spell Book", Filter = "SpellBook Files | *.sb" }; try { DialogResult result = ofd.ShowDialog(); if (result == DialogResult.OK) { PlayerSpellBook = new SpellBook(); _player = new PlayerBook(); PlayerSpellBook.BookName = ofd.SafeFileName; _handler.ReadBinaryFile(PlayerSpellBook.BookName, ref PlayerSpellBook); PlayerBook.PlayerSpellBook = PlayerSpellBook; PreparePlayerForm(); _player.Show(); } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.ToString()); } }
/// <summary> /// Creates a new spell book /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void NewSpellBookToolStripMenuItemClick(object sender, EventArgs e) { var newSpellBookDialog = new CreateSpellBook(); if (newSpellBookDialog.ShowDialog(this) == DialogResult.OK) { PlayerSpellBook = new SpellBook { BookName = newSpellBookDialog.BookName }; _player = new PlayerBook(); PlayerBook.PlayerSpellBook = PlayerSpellBook; PreparePlayerForm(); _player.Show(); } newSpellBookDialog.Dispose(); }
public void WriteBinaryFile(string fileName, SpellBook spellbook) { using (var stream = File.OpenWrite(fileName + FileExtention)) { new BinaryFormatter().Serialize(stream, spellbook); } }