Skip to content

Commit db05b49

Browse files
committed
Automatic merge of T1.5.1-791-gffaeec028 and 13 pull requests
- Pull request #570 at 3539862: Experimental glTF 2.0 support with PBR lighting - Pull request #839 at d00beb9: First phase of https://blueprints.launchpad.net/or/+spec/additional-cruise-control-parameters - Pull request #876 at f92de76: docs: add source for documents previously on website to source Documentation folder - Pull request #882 at a055bca: Blueprint/train car operations UI window - Pull request #885 at d9ce84b: feat: Add notifications to Menu - Pull request #886 at 6c0785b: Scene viewer extension to TrackViewer - Pull request #892 at 1f5ba4c: Signal Function OPP_SIG_ID_TRAINPATH - Pull request #893 at bf8876b: Signal errors - Pull request #896 at 5866028: First implementation of https://blueprints.launchpad.net/or/+spec/specific-sounds-for-ai-trains - Pull request #897 at 0a9d939: feat: Improved system information collection - Pull request #903 at d5b3639: Downloading route content from Github - Pull request #906 at 0e0aa8b: Bug fix for https://bugs.launchpad.net/or/+bug/2047299 Crash loading a 3Dcab-only loco - Pull request #907 at 9b0b04f: Bug fix for https://bugs.launchpad.net/or/+bug/2047300 Dynamic tracks disappear after long tunnel
15 parents 0fd8912 + ffaeec0 + 3539862 + d00beb9 + f92de76 + a055bca + d9ce84b + 6c0785b + 1f5ba4c + bf8876b + 5866028 + 0a9d939 + d5b3639 + 0e0aa8b + 9b0b04f commit db05b49

File tree

4 files changed

+186
-77
lines changed

4 files changed

+186
-77
lines changed

Source/Menu/DownloadContentForm.cs

Lines changed: 88 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
using System.Linq;
2727
using System.Diagnostics;
2828
using System.Threading;
29+
using System.ComponentModel;
2930

3031
namespace ORTS
3132
{
3233
public partial class DownloadContentForm : Form
3334
{
34-
private readonly GettextResourceManager Catalog = new GettextResourceManager("Menu");
35+
private readonly GettextResourceManager Catalog;
3536
private readonly UserSettings Settings;
3637
private readonly IDictionary<string, RouteSettings.Route> Routes;
3738

@@ -41,35 +42,23 @@ public DownloadContentForm(UserSettings settings)
4142
{
4243
InitializeComponent();
4344

45+
Catalog = new GettextResourceManager("Menu");
4446
Settings = settings;
45-
Routes = settings.Routes.Routes;
4647

48+
Settings.Routes.LoadContentAndInstalled();
49+
Routes = settings.Routes.Routes;
4750
for (int index = 0; index < Routes.Count; index++)
4851
{
4952
string routeName = Routes.ElementAt(index).Key;
5053
RouteSettings.Route route = Routes.ElementAt(index).Value;
5154
dataGridViewDownloadContent.Rows.Add(new string[] { routeName, route.DateInstalled, route.Url });
5255
}
5356

54-
Add("OR CPV", "https://github.com/cpvirtual/OR_CPV.git", "");
55-
Add("NewForest Route V3", "https://github.com/rickloader/NewForestRouteV3.git", "");
56-
Add("MidEast Coast", "https://github.com/MECoast/MECoast.git", "");
57-
Add("Demo Model 1", "https://github.com/cjakeman/Demo-Model-1.git", "Demo Model 1");
58-
Add("Chiltern Route V2", "https://github.com/DocMartin7644/Chiltern-Route-v2.git", "");
57+
dataGridViewDownloadContent.Sort(dataGridViewDownloadContent.Columns[0], ListSortDirection.Ascending);
5958

6059
InstallPathTextBox.Text = settings.Content.InstallPath;
6160
}
6261

63-
public void Add(string routeName, string url, string subDirectory)
64-
{
65-
RouteSettings.Route route = Routes.ContainsKey(routeName) ? Routes[routeName] : null;
66-
if (route == null)
67-
{
68-
dataGridViewDownloadContent.Rows.Add(new string[] { routeName, "", url });
69-
Routes.Add(routeName, new RouteSettings.Route("", url, subDirectory));
70-
}
71-
}
72-
7362
void dataGridViewDownloadContent_SelectionChanged(object sender, EventArgs e)
7463
{
7564
RouteName = dataGridViewDownloadContent.CurrentRow.Cells[0].Value.ToString();
@@ -82,7 +71,7 @@ private void InstallPathButton_Click(object sender, EventArgs e)
8271
using (FolderBrowserDialog folderBrowser = new FolderBrowserDialog())
8372
{
8473
folderBrowser.SelectedPath = InstallPathTextBox.Text;
85-
folderBrowser.Description = "Main Path where route is to be installed";
74+
folderBrowser.Description = Catalog.GetString("Main Path where route is to be installed");
8675
folderBrowser.ShowNewFolderButton = true;
8776
if (folderBrowser.ShowDialog(this) == DialogResult.OK)
8877
{
@@ -141,10 +130,6 @@ private void DownloadContentButton_Click(object sender, EventArgs e)
141130

142131
Settings.Content.InstallPath = installPath;
143132

144-
// check if filesystem is case sensitive
145-
// ok, this check will be wrong if both upper- and lowercase named route directories exist
146-
bool directoryCaseInsensitive = Directory.Exists(installPathRoute.ToUpper()) && Directory.Exists(installPathRoute.ToLower());
147-
148133
// set json route filename
149134

150135
Settings.Content.RouteJsonName = Path.Combine(installPath, "ORRoute.json");
@@ -156,58 +141,16 @@ private void DownloadContentButton_Click(object sender, EventArgs e)
156141
dataGridViewDownloadContent.CurrentRow.Cells[1].Value = Catalog.GetString("Installing...");
157142
Refresh();
158143

159-
// actual download
160-
161144
if (!downloadRoute(installPathRoute))
162145
{
163146
return;
164147
}
165148

166149
// insert row in Options, tab Content
167150

168-
if (!string.IsNullOrWhiteSpace(Routes[RouteName].SubDirectory))
169-
{
170-
installPathRoute = Path.Combine(installPathRoute, Routes[RouteName].SubDirectory);
171-
}
172-
173-
bool updated = false;
174-
int index = 0;
175-
while (!updated)
151+
if (!insertRowInOptions(installPathRoute))
176152
{
177-
string routeName = "";
178-
bool routeNameFound = false;
179-
foreach (KeyValuePair<string, string> folderSetting in Settings.Folders.Folders)
180-
{
181-
if (index == 0) {
182-
routeName = RouteName;
183-
} else
184-
{
185-
routeName = string.Format("{0} ({1})", RouteName, index);
186-
}
187-
if (folderSetting.Key == routeName) {
188-
if (folderSetting.Value.Equals(installPathRoute,
189-
directoryCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal))
190-
{
191-
updated = true;
192-
}
193-
else
194-
{
195-
routeNameFound = true;
196-
}
197-
}
198-
}
199-
if (!updated)
200-
{
201-
if (routeNameFound)
202-
{
203-
index++;
204-
}
205-
else
206-
{
207-
Settings.Folders.Folders[routeName] = installPathRoute;
208-
updated = true;
209-
}
210-
}
153+
return;
211154
}
212155

213156
string dateTimeNowStr = DateTime.Now.ToString(CultureInfo.CurrentCulture.DateTimeFormat);
@@ -275,14 +218,92 @@ private void sumMB(string path)
275218
{
276219
foreach (string fileName in Directory.GetFiles(path))
277220
{
278-
TotalBytes += new System.IO.FileInfo(fileName).Length;
221+
TotalBytes += new FileInfo(fileName).Length;
279222
}
280223

281224
foreach (string directoryName in Directory.GetDirectories(path))
282225
{
283226
sumMB(directoryName);
284227
}
285228
}
229+
230+
private bool insertRowInOptions(string installPathRoute)
231+
{
232+
// check if filesystem is case sensitive
233+
// ok, this check will be wrong if both upper- and lowercase named route directories exist
234+
bool directoryCaseInsensitive = Directory.Exists(installPathRoute.ToUpper()) && Directory.Exists(installPathRoute.ToLower());
235+
236+
// sometimes the route is located one directory level deeper, determine real installPathRoute
237+
string installPathRouteReal;
238+
239+
if (Directory.Exists(Path.Combine(installPathRoute, "routes"))) {
240+
installPathRouteReal = installPathRoute;
241+
}
242+
else
243+
{
244+
string[] directories = Directory.GetDirectories(installPathRoute);
245+
int indexDirectories = 0;
246+
while ((indexDirectories < directories.Length) &&
247+
!Directory.Exists(Path.Combine(directories[indexDirectories], "routes")))
248+
{
249+
indexDirectories++;
250+
}
251+
if (indexDirectories < directories.Length)
252+
{
253+
installPathRouteReal = Path.Combine(installPathRoute, directories[indexDirectories]);
254+
}
255+
else
256+
{
257+
string message = Catalog.GetString("Incorrect route, directory \"routes\" not found");
258+
MessageBox.Show(message, Catalog.GetString("Attention"), MessageBoxButtons.OK, MessageBoxIcon.Error);
259+
return false;
260+
}
261+
}
262+
263+
bool updated = false;
264+
int index = 0;
265+
while (!updated)
266+
{
267+
string routeName = "";
268+
bool routeNameFound = false;
269+
foreach (KeyValuePair<string, string> folderSetting in Settings.Folders.Folders)
270+
{
271+
if (index == 0)
272+
{
273+
routeName = RouteName;
274+
}
275+
else
276+
{
277+
routeName = string.Format("{0} ({1})", RouteName, index);
278+
}
279+
if (folderSetting.Key == routeName)
280+
{
281+
if (folderSetting.Value.Equals(installPathRouteReal,
282+
directoryCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal))
283+
{
284+
updated = true;
285+
}
286+
else
287+
{
288+
routeNameFound = true;
289+
}
290+
}
291+
}
292+
if (!updated)
293+
{
294+
if (routeNameFound)
295+
{
296+
index++;
297+
}
298+
else
299+
{
300+
Settings.Folders.Folders[routeName] = installPathRouteReal;
301+
updated = true;
302+
}
303+
}
304+
}
305+
return true;
306+
}
286307
}
287308

288309
}

Source/Menu/MainForm.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,11 +572,8 @@ void buttonOptions_Click(object sender, EventArgs e)
572572

573573
void buttonDownloadContent_Click(object sender, EventArgs e)
574574
{
575-
SaveOptions();
576-
577575
using (var form = new DownloadContentForm(Settings))
578576
{
579-
Cursor.Current = Cursors.WaitCursor;
580577
form.ShowDialog(this);
581578
}
582579
}

Source/ORTS.Settings/ORTS.Settings.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<ProjectReference Include="..\ORTS.Common\ORTS.Common.csproj" />
2727
</ItemGroup>
2828
<ItemGroup>
29+
<PackageReference Include="LibGit2Sharp" Version="0.29.0" />
2930
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.4.355802">
3031
<PrivateAssets>all</PrivateAssets>
3132
</PackageReference>

Source/ORTS.Settings/RouteSettings.cs

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21+
using System.Diagnostics;
2122
using System.IO;
2223
using System.Linq;
2324
using Newtonsoft.Json;
25+
using Newtonsoft.Json.Linq;
26+
using LibGit2Sharp;
27+
using static ORTS.Settings.RouteSettings;
28+
using System.Windows.Forms;
2429

2530
namespace ORTS.Settings
2631
{
@@ -30,35 +35,120 @@ public class Route
3035
{
3136
public string DateInstalled { get; set; }
3237
public string Url { get; set; }
33-
public string SubDirectory { get; set; }
3438

35-
public Route(string dateInstalled, string url, string subDirectory)
39+
public Route(string dateInstalled, string url)
3640
{
3741
DateInstalled = dateInstalled;
3842
Url = url;
39-
SubDirectory = subDirectory;
4043
}
4144
}
4245

43-
private ContentSettings Content;
46+
private readonly ContentSettings Content;
4447

45-
public IDictionary<string, Route> Routes = new Dictionary<string, Route>();
48+
public IDictionary<string, Route> Routes { get; private set; }
4649

4750
public RouteSettings(ContentSettings content)
4851
{
4952
Content = content;
53+
Routes = new Dictionary<string, Route>();
54+
5055
Load();
5156
}
5257

5358
public void Load()
59+
{
60+
// left empty
61+
}
62+
63+
public void LoadContentAndInstalled()
5464
{
5565
if (!string.IsNullOrWhiteSpace(Content.RouteJsonName))
5666
{
5767
if (File.Exists(Content.RouteJsonName))
5868
{
59-
string json = File.ReadAllText(Content.RouteJsonName);
60-
Routes = JsonConvert.DeserializeObject<IDictionary<string, Route>>(json);
69+
try
70+
{
71+
string json = File.ReadAllText(Content.RouteJsonName);
72+
Routes = JsonConvert.DeserializeObject<IDictionary<string, Route>>(json);
73+
}
74+
catch (Exception error)
75+
{
76+
throw new Exception("Error during reading " + Content.RouteJsonName + ": " + error.Message, error);
77+
}
78+
}
79+
}
80+
81+
string definedContentJsonName = @"d:\content\routes.json";
82+
string definedContentJsonDirectoryName = Path.Combine(UserSettings.UserDataFolder, "ContentJson");
83+
string githubUrl = "https://github.com/openrails/content.git";
84+
85+
if (Environment.GetEnvironmentVariable("TstLoadContentAndInstalled") == null)
86+
{
87+
try
88+
{
89+
// normal non test behaviour, retrieve json file from github
90+
91+
directoryDelete(definedContentJsonDirectoryName);
92+
93+
Repository.Clone(githubUrl, definedContentJsonDirectoryName);
94+
95+
definedContentJsonName = Path.Combine(definedContentJsonDirectoryName, "routes.json");
6196
}
97+
catch (Exception error)
98+
{
99+
throw new Exception("Error during retrieving routes.json from \"" + githubUrl + "\":" + error.Message, error);
100+
}
101+
}
102+
103+
if (File.Exists(definedContentJsonName))
104+
{
105+
try
106+
{
107+
var json = File.ReadAllText(definedContentJsonName);
108+
109+
IList<JToken> results = JsonConvert.DeserializeObject<JToken>(json) as IList<JToken>;
110+
foreach (JToken result in results)
111+
{
112+
if (result["url"].ToString().EndsWith(".git"))
113+
{
114+
string routeName = result["name"].ToString();
115+
if (!Routes.ContainsKey(routeName))
116+
{
117+
Routes.Add(routeName, new RouteSettings.Route("", result["url"].ToString()));
118+
}
119+
}
120+
}
121+
122+
directoryDelete(definedContentJsonDirectoryName);
123+
}
124+
catch (Exception error)
125+
{
126+
throw new Exception("Error during reading \"" + definedContentJsonName + "\": " + error.Message, error);
127+
}
128+
}
129+
130+
return;
131+
}
132+
133+
private void directoryDelete(string directoryName)
134+
{
135+
if (Directory.Exists(directoryName))
136+
{
137+
directoryRemoveReadOnlyFlags(directoryName);
138+
Directory.Delete(directoryName, true);
139+
}
140+
}
141+
142+
private void directoryRemoveReadOnlyFlags(string directoryName)
143+
{
144+
foreach (string filename in Directory.GetFiles(directoryName))
145+
{
146+
FileInfo file = new FileInfo(filename);
147+
file.IsReadOnly = false;
148+
}
149+
foreach (string subDirectoryName in Directory.GetDirectories(directoryName))
150+
{
151+
directoryRemoveReadOnlyFlags(subDirectoryName);
62152
}
63153
}
64154

0 commit comments

Comments
 (0)