|
| 1 | +// COPYRIGHT 2023 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 System; |
| 19 | +using System.Collections.Generic; |
| 20 | +using System.Diagnostics; |
| 21 | +using System.IO; |
| 22 | +using System.Linq; |
| 23 | +using System.Text; |
| 24 | + |
| 25 | +namespace ORTS.Common |
| 26 | +{ |
| 27 | + /// <summary> |
| 28 | + /// Utility functions to procedurally generate con and eng files to display the Khronos glTF-Sample-Assets for testing purposes. |
| 29 | + /// For this to work 'git clone https://github.com/KhronosGroup/glTF-Sample-Assets.git' to the MSTS/TRAINS/TRAINSET folder, |
| 30 | + /// so that the models will be available in e.g. MSTS/TRAINS/TRAINSET/glTF-Sample-Assets/Models/... folder. Then start like: |
| 31 | + /// RunActivity.exe -start -explorer "C:\Devel\MSTS\ROUTES\USA2\PATHS\tut6path.pat" "glTF-Sample-Assets" 12:00 1 0 |
| 32 | + /// RunActivity.exe -start -explorer "C:\Devel\MSTS\ROUTES\USA2\PATHS\tut6path.pat" "glTF-Sample-Assets&AnimatedTriangle" 12:00 1 0 |
| 33 | + /// RunActivity.exe -start -explorer "C:\Devel\MSTS\ROUTES\USA2\PATHS\tut6path.pat" "glTF-Sample-Assets#2" 12:00 1 0 |
| 34 | + /// </summary> |
| 35 | + public class ConsistGenerator |
| 36 | + { |
| 37 | + /// <summary> |
| 38 | + /// Indicates if the current run is a glTF visual test only. It is possible to generate an on-the-fly consist of all the Khronos test models. |
| 39 | + /// </summary> |
| 40 | + public static bool GltfVisualTestRun; |
| 41 | + |
| 42 | + const string ConsistTemplateStart = |
| 43 | + @"Train ( |
| 44 | + TrainCfg ( ""trainname"" |
| 45 | + Name ( ""trainname"" ) |
| 46 | + MaxVelocity ( 50 1 ) |
| 47 | + "; |
| 48 | + const string ConsistTemplateRecord = @"Engine ( UID ( xx ) EngineData ( ""engfile"" ""trainsetdir"" ) )" + "\r\n"; |
| 49 | + const string ConsistTemplateEnd = ")\r\n)"; |
| 50 | + const string EngineTemplate = |
| 51 | + @"Wagon ( ""enginname"" |
| 52 | + Type ( Engine ) |
| 53 | + WagonShape ( ""shapefilename"" ) |
| 54 | + Size (100m 7m 100m ) |
| 55 | + Lights ( 1 |
| 56 | + Light ( |
| 57 | + Type ( 1 ) |
| 58 | + ShapeHierarchy ( Headlight ) |
| 59 | + Conditions ( |
| 60 | + Headlight ( 3 ) |
| 61 | + Unit ( 2 ) |
| 62 | + ) |
| 63 | + Cycle ( 0 ) |
| 64 | + FadeIn ( 1 ) |
| 65 | + FadeOut ( 1 ) |
| 66 | + States ( 1 |
| 67 | + State ( |
| 68 | + Duration ( 0.0 ) |
| 69 | + LightColour ( ffffffff ) |
| 70 | + Position ( 0 3.5 0 ) |
| 71 | + Transition ( 0 ) |
| 72 | + Radius ( 400 ) |
| 73 | + Angle ( 15 ) |
| 74 | + ) |
| 75 | + ) |
| 76 | + ) |
| 77 | + ) |
| 78 | + ) |
| 79 | + Engine ( ""enginname"" |
| 80 | + Wagon ( ""enginname"" ) |
| 81 | + Type ( Electric ) |
| 82 | + CabView ( dummy.cvf ) |
| 83 | + Name ( ""enginname"" ) |
| 84 | + )"; |
| 85 | + |
| 86 | + static readonly Dictionary<string, string> Wagons = new Dictionary<string, string>(); |
| 87 | + static readonly Dictionary<string, string> SubDirectories = new Dictionary<string, string> |
| 88 | + { |
| 89 | + { "glTF-Sample-Assets", "glTF"}, |
| 90 | + { "glTF-Sample-Assets-Binary", "glTF-Binary"}, |
| 91 | + { "glTF-Sample-Assets-Embedded", "glTF-Embedded"}, |
| 92 | + { "glTF-Sample-Assets-Draco", "glTF-Draco"}, |
| 93 | + { "glTF-Sample-Assets-Quantized", "glTF-Quantized"}, |
| 94 | + }; |
| 95 | + |
| 96 | + static string GltfExtension(string keyword) => keyword.EndsWith("-Binary") ? ".glb" : ".gltf"; |
| 97 | + static string RequestedType(string requestedPath) => SubDirectories.FirstOrDefault(ext => ext.Key == Path.GetFileNameWithoutExtension(requestedPath).Split('#', '&').FirstOrDefault()).Key; |
| 98 | + |
| 99 | + public static bool IsConsistRecognized(string requestedPath) => RequestedType(requestedPath) != null; |
| 100 | + public static bool IsWagonRecognized(string requestedPath) => Wagons.ContainsKey(Path.GetFileName(requestedPath)); |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Provides a procedurally generated consist of the requested type of models |
| 104 | + /// </summary> |
| 105 | + /// <param name="requestedPath"></param> |
| 106 | + /// <returns></returns> |
| 107 | + public static Stream GetConsist(string requestedPath) |
| 108 | + { |
| 109 | + var trainsetDir = "glTF-Sample-Assets"; |
| 110 | + var baseDir = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(requestedPath)), "TRAINSET", trainsetDir); |
| 111 | + if (!Directory.Exists(baseDir)) |
| 112 | + { |
| 113 | + Console.WriteLine($"Cannot find the trainset directory {trainsetDir}."); |
| 114 | + return Stream.Null; |
| 115 | + } |
| 116 | + |
| 117 | + var keyword = RequestedType(requestedPath); |
| 118 | + Debug.Assert(keyword != null); |
| 119 | + |
| 120 | + var consist = ConsistTemplateStart.Replace("trainname", keyword); |
| 121 | + |
| 122 | + if (keyword.StartsWith("glTF")) |
| 123 | + { |
| 124 | + GltfVisualTestRun = true; |
| 125 | + |
| 126 | + var models = Directory.EnumerateFileSystemEntries(baseDir, "*.*", SearchOption.AllDirectories) |
| 127 | + .Where(f => !f.Contains(Path.Combine(baseDir, "Models")) && (f.EndsWith(".gltf") || f.EndsWith(".glb"))) |
| 128 | + .Select(f => Path.GetFileNameWithoutExtension(f)) |
| 129 | + .Concat(Directory.EnumerateDirectories(Path.Combine(baseDir, "Models"))) |
| 130 | + .Where((m, n) => Path.GetFileName(m) == (Path.GetFileNameWithoutExtension(requestedPath).Split('&').ElementAtOrDefault(1) ?? Path.GetFileName(m)) && |
| 131 | + n == (int.TryParse(Path.GetFileNameWithoutExtension(requestedPath).Split('#').ElementAtOrDefault(1), out var requestedModelNumber) ? requestedModelNumber : n)); |
| 132 | + |
| 133 | + var uid = 0; |
| 134 | + foreach (var model in models) |
| 135 | + { |
| 136 | + var dir = Path.Combine(model, SubDirectories[keyword]); |
| 137 | + var file = (Directory.Exists(dir) |
| 138 | + ? Directory.EnumerateFiles(dir).FirstOrDefault(f => f.EndsWith(GltfExtension(keyword))) |
| 139 | + : Directory.EnumerateFiles(baseDir).FirstOrDefault(f => f.Contains(model))) |
| 140 | + ?.Substring(baseDir.Length + 1) |
| 141 | + ?.Replace(@"\", "/"); |
| 142 | + |
| 143 | + if (file == null) |
| 144 | + { |
| 145 | + // When gltf files got dropped into the sample directory, show that ones too. |
| 146 | + file = Directory.GetFiles(baseDir, Path.GetFileNameWithoutExtension(model) + "*.gl*", SearchOption.AllDirectories) |
| 147 | + .Where(f => !f.Contains(Path.Combine(baseDir, "Models")) && (f.EndsWith(".gltf") || f.EndsWith(".glb"))) |
| 148 | + .FirstOrDefault() |
| 149 | + ?.Substring(baseDir.Length + 1) |
| 150 | + ?.Replace(@"\", "/"); |
| 151 | + if (file == null) |
| 152 | + continue; |
| 153 | + } |
| 154 | + |
| 155 | + var eng = $"{keyword}_{Path.GetFileNameWithoutExtension(file)}.eng"; |
| 156 | + Wagons.Add(eng, EngineTemplate |
| 157 | + .Replace("shapefilename", file) |
| 158 | + .Replace("enginname", Path.GetFileNameWithoutExtension(file))); |
| 159 | + |
| 160 | + consist += ConsistTemplateRecord |
| 161 | + .Replace("xx", uid.ToString()) |
| 162 | + .Replace("engfile", Path.GetFileNameWithoutExtension(eng)) |
| 163 | + .Replace("trainsetdir", trainsetDir); |
| 164 | + uid++; |
| 165 | + } |
| 166 | + } |
| 167 | + consist += ConsistTemplateEnd; |
| 168 | + |
| 169 | + var stream = new MemoryStream(); |
| 170 | + using (var writer = new StreamWriter(stream, Encoding.UTF8, Encoding.UTF8.GetByteCount(consist), true)) |
| 171 | + { |
| 172 | + writer.Write(consist); |
| 173 | + writer.Flush(); |
| 174 | + } |
| 175 | + stream.Position = 0; |
| 176 | + return stream; |
| 177 | + } |
| 178 | + |
| 179 | + public static Stream GetWagon(string requestedEngine) |
| 180 | + { |
| 181 | + if (!Wagons.ContainsKey(Path.GetFileName(requestedEngine))) |
| 182 | + { |
| 183 | + Console.WriteLine($"Cannot find the requested procedurally generated engine {requestedEngine}."); |
| 184 | + return Stream.Null; |
| 185 | + } |
| 186 | + |
| 187 | + var wagon = Wagons[Path.GetFileName(requestedEngine)]; |
| 188 | + var stream = new MemoryStream(); |
| 189 | + using (var writer = new StreamWriter(stream, Encoding.UTF8, Encoding.UTF8.GetByteCount(wagon), true)) |
| 190 | + { |
| 191 | + writer.Write(wagon); |
| 192 | + writer.Flush(); |
| 193 | + } |
| 194 | + stream.Position = 0; |
| 195 | + return stream; |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments