|
| 1 | +// COPYRIGHT 2016 by the Open Rails project. |
| 2 | +// |
| 3 | +// This file is part of Open Rails. |
| 4 | +// |
| 5 | +// Open Rails is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// Open Rails is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with Open Rails. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +using Newtonsoft.Json; |
| 19 | +using Orts.Formats.Msts; |
| 20 | +using Orts.Formats.OR; |
| 21 | +using Orts.Parsers.Msts; |
| 22 | +using System; |
| 23 | +using System.Collections.Generic; |
| 24 | +using System.IO; |
| 25 | +using System.Linq; |
| 26 | +using System.Text; |
| 27 | +using System.Xml.Linq; |
| 28 | + |
| 29 | +namespace Orts.DataConverter |
| 30 | +{ |
| 31 | + class ClockConverter : IDataConverter |
| 32 | + { |
| 33 | + public ClockConverter() |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + public void ShowConversions() |
| 38 | + { |
| 39 | + // "1234567890123456789012345678901234567890123456789012345678901234567890123456789" |
| 40 | + // " Input Output Description" |
| 41 | + Console.WriteLine("openrails/clocks.dat animated.clocks-or Converts from STF to JSON"); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Not clear what happens if conversion fails. |
| 46 | + /// </summary> |
| 47 | + /// <param name="conversion"></param> |
| 48 | + /// <returns></returns> |
| 49 | + public bool DoConversion(DataConversion conversion) |
| 50 | + { |
| 51 | + // We can convert from .dat files. |
| 52 | + if (conversion.Input.EndsWith(@"openrails\clocks.dat") == false) |
| 53 | + { |
| 54 | + return false; |
| 55 | + } |
| 56 | + // We can convert to .clocks-or files. |
| 57 | + if (conversion.Output.Any(r => r.EndsWith("clocks-or")) == false |
| 58 | + || conversion.Output.Count != 1) |
| 59 | + { |
| 60 | + return false; |
| 61 | + } |
| 62 | + if (File.Exists(conversion.Input) == false) |
| 63 | + { |
| 64 | + throw new FileNotFoundException("", conversion.Input); |
| 65 | + } |
| 66 | + |
| 67 | + var clockLists = new List<ClockList>(); |
| 68 | + try |
| 69 | + { |
| 70 | + var inFilePath = Path.GetFullPath(conversion.Input); |
| 71 | + |
| 72 | + // Shorten the path to get the route |
| 73 | + var routeLength = inFilePath.Length - @"openrails\clocks.dat".Length; |
| 74 | + var routePath = inFilePath.Substring(0, routeLength); |
| 75 | + |
| 76 | + clockLists = ParseInput(conversion, routePath); |
| 77 | + } |
| 78 | + catch (Orts.Parsers.Msts.STFException e) |
| 79 | + { |
| 80 | + Console.WriteLine("Parse error in " + conversion.Input + e.Message); |
| 81 | + throw; // re-throw exception without losing the stack trace |
| 82 | + } |
| 83 | + |
| 84 | + var jso = new List<ClockShape>(); |
| 85 | + |
| 86 | + try |
| 87 | + { |
| 88 | + GenerateOutput(conversion, clockLists, jso); |
| 89 | + } |
| 90 | + catch (Exception e) |
| 91 | + { |
| 92 | + Console.WriteLine("Error generating " + conversion.Output + e.Message); |
| 93 | + throw; // re-throw exception without losing the stack trace |
| 94 | + } |
| 95 | + |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + private static List<ClockList> ParseInput(DataConversion conversion, string routePath) |
| 100 | + { |
| 101 | + var clockLists = new List<ClockList>(); |
| 102 | + { |
| 103 | + using (STFReader stf = new STFReader(conversion.Input, false)) |
| 104 | + { |
| 105 | + var clockBlock = new ClockBlock(stf, routePath + @"shapes\", clockLists, "Default"); |
| 106 | + } |
| 107 | + } |
| 108 | + return clockLists; |
| 109 | + } |
| 110 | + |
| 111 | + private static void GenerateOutput(DataConversion conversion, List<ClockList> clockLists, List<ClockShape> jso) |
| 112 | + { |
| 113 | + var clockList = clockLists.First(); |
| 114 | + for (var i = 0; i < clockList.ClockType.Count(); i++) |
| 115 | + { |
| 116 | + var item = new ClockShape(clockList.ShapeNames[i], clockList.ClockType[i]); |
| 117 | + jso.Add(item); |
| 118 | + } |
| 119 | + |
| 120 | + File.WriteAllText(conversion.Output.First(), JsonConvert.SerializeObject(jso, Formatting.Indented)); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments