public void Save() { bool fileExists = false; if (s_Path == null) return; if (All.Count == 0) { if (File.Exists(s_Path)) { try { File.Delete(s_Path); } catch { // ... could not delete? } } return; } if (File.Exists(s_Path)) { fileExists = true; File.Copy(s_Path, s_Path + ".bak", true); } try { Tracer.Debug("Saving macros..."); BinaryFileWriter writer = new BinaryFileWriter(s_Path, false); Serialize(writer); writer.Close(); writer = null; } catch (Exception e) { Tracer.Warn(string.Format("Error saving macros: {0}", e.Message)); Tracer.Debug("Attempting to restore old macros..."); if (fileExists) { File.Copy(s_Path + ".bak", s_Path); } Tracer.Debug("Old macros restored."); } }
private void Serialize(BinaryFileWriter writer) { writer.Write(MAGIC); writer.Write((int)0); // version writer.Write((int)All.Count); for (int i = 0; i < All.Count; i++) { writer.Write((ushort)All[i].Keystroke); writer.Write(All[i].Ctrl); writer.Write(All[i].Alt); writer.Write(All[i].Shift); writer.Write(false); writer.Write((ushort)All[i].Macros.Count); for (int j = 0; j < All[i].Macros.Count; j++) { writer.Write((int)All[i].Macros[j].Type); writer.Write((byte)All[i].Macros[j].ValueType); if (All[i].Macros[j].ValueType == Macro.ValueTypes.Integer) { writer.Write((int)All[i].Macros[j].ValueInteger); } else if (All[i].Macros[j].ValueType == Macro.ValueTypes.String) { writer.Write((string)All[i].Macros[j].ValueString); } } } }