|
| 1 | +//////////////////////////////////////////////////////////////////////////////// |
| 2 | +// The MIT License (MIT) |
| 3 | +// |
| 4 | +// Copyright (c) 2019 Tim Stair |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | +//////////////////////////////////////////////////////////////////////////////// |
| 24 | + |
| 25 | +using System; |
| 26 | +using System.Collections.Generic; |
| 27 | +using System.IO; |
| 28 | +using System.Windows.Forms; |
| 29 | +using Support.UI; |
| 30 | + |
| 31 | +namespace KeyCap.Format |
| 32 | +{ |
| 33 | + public class ConfigFileManager |
| 34 | + { |
| 35 | + private static readonly int FILE_DATA_PREFIX = (int)0x0E0CA000; |
| 36 | + private static readonly int DATA_FORMAT_VERSION = (int)0x1; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Saves the remap entries to a versioned file format |
| 40 | + /// </summary> |
| 41 | + /// <param name="listRemapEntries">The entries to persist</param> |
| 42 | + /// <param name="sFileName">The name of the file to save to</param> |
| 43 | + public void SaveFile(List<RemapEntry> listRemapEntries, string sFileName) |
| 44 | + { |
| 45 | + var zFileStream = new FileStream(sFileName, FileMode.Create, FileAccess.Write, FileShare.None); |
| 46 | + StreamUtil.WriteIntToStream(zFileStream, FILE_DATA_PREFIX); |
| 47 | + StreamUtil.WriteIntToStream(zFileStream, DATA_FORMAT_VERSION); |
| 48 | + listRemapEntries.ForEach(zEntry => |
| 49 | + { |
| 50 | + var arrayBytes = zEntry.SerializeToBytes(); |
| 51 | + zFileStream.Write(arrayBytes, 0, arrayBytes.Length); |
| 52 | + }); |
| 53 | + zFileStream.Close(); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Loads the remap entries from the specified file |
| 58 | + /// </summary> |
| 59 | + /// <param name="sFileName"></param> |
| 60 | + /// <returns></returns> |
| 61 | + public List<RemapEntry> LoadFile(string sFileName) |
| 62 | + { |
| 63 | + FileStream zFileStream = null; |
| 64 | + var listConfigs = new List<RemapEntry>(); |
| 65 | + try |
| 66 | + { |
| 67 | + zFileStream = new FileStream(sFileName, FileMode.Open, FileAccess.Read, FileShare.Read); |
| 68 | + var nPrefix = StreamUtil.ReadIntFromStream(zFileStream); |
| 69 | + if (nPrefix != FILE_DATA_PREFIX) |
| 70 | + { |
| 71 | + throw new Exception("{} does not have the correct data prefix. This is likely an unsupported format.".FormatString(sFileName)); |
| 72 | + } |
| 73 | + |
| 74 | + var nDataFormatVersion = StreamUtil.ReadIntFromStream(zFileStream); |
| 75 | + if (nDataFormatVersion != DATA_FORMAT_VERSION) |
| 76 | + { |
| 77 | + throw new Exception("{} indicates an unsupported data format.".FormatString(sFileName)); |
| 78 | + } |
| 79 | + |
| 80 | + while (zFileStream.Position < zFileStream.Length) |
| 81 | + { |
| 82 | + listConfigs.Add(new RemapEntry(zFileStream)); |
| 83 | + } |
| 84 | + } |
| 85 | + catch (Exception e) |
| 86 | + { |
| 87 | + MessageBox.Show(e.Message); |
| 88 | + } |
| 89 | + finally |
| 90 | + { |
| 91 | + zFileStream?.Close(); |
| 92 | + } |
| 93 | + |
| 94 | + return listConfigs; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments