public static void ToFile(string file, MenuFile data) { using(var s= File.OpenWrite(file)) using(var w= new BinaryWriter(s)) { WriteString(w, data.Magic); w.Write(data.Version); Write(w, WriteString, data.Descriptions); w.Flush(); var pos = w.BaseStream.Position; w.Write(0); foreach(var i in data.Scripts) { w.Write((byte)i.Count); Write(w, WriteString, i); } w.Write((byte)0); w.Flush(); var len = w.BaseStream.Position; w.BaseStream.Position = pos; w.Write((int)(len - pos - 4)); w.BaseStream.SetLength(len); } }
public static MenuFile FromFile(string file) { var data= new MenuFile() { FileName= file }; using(var s= File.OpenRead(file)) using(var r = new BinaryReader(s)) { data.Magic = ReadString(r); data.Version = r.ReadInt32(); data.Descriptions= ReadList(r, 4, ReadString); data.DataSize = r.ReadInt32(); data.Scripts = new List<List<string>>(); var end = r.BaseStream.Position + data.DataSize; try { while(r.BaseStream.Position < end) { var count = r.ReadByte(); var strings = ReadList(r, count, ReadString); data.Scripts.Add(strings); } } catch(EndOfStreamException ex) { System.Diagnostics.Debug.Print(file); System.Diagnostics.Debug.Print(ex.ToString()); } } return data; }
private void Add(MenuFile data) { Menus[Path.GetFileName(data.FileName).ToUpper()] = data; OnDataAdded(new DataFileEventArgs(data)); }
private void SetData(MenuFile value) { if(value == data) return; data = value; UpdateView(); }
private void AddMenu(MenuFile menu) { AddFiles(menu.Scripts .SelectMany(j => j) .Select(j => DataManager.Instance.FindItem(j) as BaseFile) .Where(j => j != null)); }
private void CloneMenuFile(MenuFile file, string newname) { var clone = MenuFile.FromFile(file.FileName); clone.FileName = newname; ReplaceDescription(clone.Descriptions); foreach(var i in clone.Scripts) { for(int j= 0; j < i.Count; ++j) { if(DataManager.Instance.FindItem(i[j]) == null) continue; i[j] = Replacer.ReplaceFileName(i[j]); } } if(ConfirmOverride(clone.FileName) == DialogResult.Yes) { MenuFile.ToFile(clone.FileName, clone); AddCopiedFile(clone); } }
private void tsbLoadBackup_Click(object sender, EventArgs e) { try { var data = MenuFile.FromFile(Data.GetBackupFileName()); data.FileName = Path.ChangeExtension(data.FileName, ".menu"); Data = data; } catch {} }