Skip to content

Commit c296b32

Browse files
Options downloaded routes now stored in registry like other options.
1 parent 4742fa3 commit c296b32

File tree

4 files changed

+74
-108
lines changed

4 files changed

+74
-108
lines changed

Source/Menu/DownloadContentForm.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public DownloadContentForm(UserSettings settings)
5858
Catalog = new GettextResourceManager("Menu");
5959
Settings = settings;
6060

61-
Settings.Routes.LoadContentAndInstalled();
62-
Routes = settings.Routes.Routes;
61+
Settings.Content.ContentRouteSettings.LoadContent();
62+
Routes = Settings.Content.ContentRouteSettings.Routes;
6363
for (int index = 0; index < Routes.Count; index++)
6464
{
6565
string routeName = Routes.ElementAt(index).Key;
@@ -79,7 +79,12 @@ public DownloadContentForm(UserSettings settings)
7979
cell.ToolTipText = cell.Value.ToString();
8080
}
8181

82-
InstallPathTextBox.Text = settings.Content.InstallPath;
82+
if (string.IsNullOrEmpty(settings.ContentInstallPath))
83+
{
84+
settings.ContentInstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Open Rails", "Content");
85+
}
86+
87+
InstallPathTextBox.Text = settings.ContentInstallPath;
8388

8489
ImageTempFilename = Path.GetTempFileName();
8590
InfoTempFilename = Path.GetTempFileName();
@@ -152,7 +157,7 @@ private void dataGridViewDownloadContent_SelectionChanged(object sender, EventAr
152157
#region InstallPathTextBox
153158
private void InstallPathTextBox_TextChanged(object sender, EventArgs e)
154159
{
155-
Settings.Content.InstallPath = InstallPathTextBox.Text;
160+
Settings.ContentInstallPath = InstallPathTextBox.Text;
156161
}
157162
#endregion
158163

@@ -296,7 +301,7 @@ private async void DownloadContentButton_Click(object sender, EventArgs e)
296301
route.DirectoryInstalledIn = installPathRoute;
297302

298303
Settings.Folders.Save();
299-
Settings.Routes.Save();
304+
Settings.Content.Save();
300305

301306
if (!string.IsNullOrWhiteSpace(route.Start.Route))
302307
{
@@ -957,17 +962,23 @@ private async void DeleteButton_Click(object sender, EventArgs e)
957962
await Task.Run(() => deleteRoute(route.DirectoryInstalledIn));
958963
}
959964

960-
if (Settings.Folders.Folders[route.ContentName] == route.ContentDirectory)
965+
if (Settings.Folders.Folders.ContainsKey(route.ContentName))
961966
{
962-
Settings.Folders.Folders.Remove(route.ContentName);
967+
if (Settings.Folders.Folders[route.ContentName] == route.ContentDirectory)
968+
{
969+
Settings.Folders.Folders.Remove(route.ContentName);
970+
}
971+
Settings.Folders.Save();
963972
}
964-
Settings.Folders.Save();
965973

966974
route.Installed = false;
967975
route.DateInstalled = DateTime.MinValue;
968976
route.DirectoryInstalledIn = "";
969977

970-
Settings.Routes.Save();
978+
// remove from registry, but leave entry in Routes for this route name
979+
ContentRouteSettings.Route routeSaved = route;
980+
Settings.Content.Save();
981+
Routes[RouteName] = routeSaved;
971982

972983
dataGridViewDownloadContent.CurrentRow.Cells[1].Value = "";
973984

@@ -1053,8 +1064,10 @@ private bool doThePull(ContentRouteSettings.Route route) {
10531064
{
10541065
using (var repo = new Repository(route.DirectoryInstalledIn))
10551066
{
1056-
LibGit2Sharp.PullOptions options = new LibGit2Sharp.PullOptions();
1057-
options.FetchOptions = new FetchOptions();
1067+
LibGit2Sharp.PullOptions options = new LibGit2Sharp.PullOptions
1068+
{
1069+
FetchOptions = new FetchOptions()
1070+
};
10581071

10591072
// User information to create a merge commit
10601073
var signature = new LibGit2Sharp.Signature(

Source/ORTS.Settings/ContentRouteSettings.cs

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
using System;
2020
using System.Collections.Generic;
2121
using System.IO;
22-
using System.Linq;
2322
using Newtonsoft.Json;
2423
using Newtonsoft.Json.Linq;
2524
using LibGit2Sharp;
26-
using System.Windows.Forms;
2725

2826
namespace ORTS.Settings
2927
{
@@ -112,51 +110,18 @@ public DownloadType getDownloadType ()
112110
// RouteSettings are presented to the user in the menu DownloadContent form
113111
//
114112
// lines are a mix from:
115-
// - routes already downloaded, stored in {ApplicationData}\{ProductName\Settings\ORRoute.json",
116-
// eg: "C:\\Users\\Siebren\\AppData\\Roaming\\Open Rails\\Settings\ORRoute.json"
113+
// - routes already downloaded, stored in registry Computer\HKEY_CURRENT_USER\SOFTWARE\OpenRails\ORTS\ContentRoutes
117114
// - routes which can be downloaded, stored in GitHub "https://github.com/openrails/content.git" file routes.json
118115
//
119-
// this .cs file is not like all the other *Settings.cs files, which are derived from class SettingsBase
120-
// and store their settings in the registry
121-
// but since this class stores settings also, be it in a file, it looks like a good place
122-
//
123116
// where above the word route is mentioned, "Installation profile" is ment as can be found in OR's Main menu
124117

125118
public ContentRouteSettings()
126119
{
127120
Routes = new Dictionary<string, Route>();
128121
}
129122

130-
private string RouteJsonName;
131-
132-
public void LoadContentAndInstalled()
123+
public void LoadContent()
133124
{
134-
// set json route filename
135-
136-
string userDataSettingsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Application.ProductName, "Settings");
137-
if (!Directory.Exists(userDataSettingsFolder))
138-
{
139-
Directory.CreateDirectory(userDataSettingsFolder);
140-
}
141-
142-
RouteJsonName = Path.Combine(userDataSettingsFolder, "ORRoute.json");
143-
144-
if (!string.IsNullOrWhiteSpace(RouteJsonName))
145-
{
146-
if (File.Exists(RouteJsonName))
147-
{
148-
try
149-
{
150-
string json = File.ReadAllText(RouteJsonName);
151-
Routes = JsonConvert.DeserializeObject<IDictionary<string, Route>>(json);
152-
}
153-
catch (Exception error)
154-
{
155-
throw new Exception("Error during reading " + RouteJsonName + ": " + error.Message, error);
156-
}
157-
}
158-
}
159-
160125
// only for debug purposes
161126
string definedContentJsonName = @"c:\content\routes.json";
162127

@@ -310,21 +275,5 @@ private static void directoryRemoveReadOnlyFlags(string directoryName)
310275
directoryRemoveReadOnlyFlags(subDirectoryName);
311276
}
312277
}
313-
314-
public void Save()
315-
{
316-
IDictionary<string, Route> routes = new Dictionary<string, Route>();
317-
318-
for (int index = 0; index < Routes.Count; index++)
319-
{
320-
// only save the installed routes
321-
if (Routes.ElementAt(index).Value.Installed)
322-
{
323-
routes.Add(Routes.ElementAt(index));
324-
}
325-
}
326-
string json = JsonConvert.SerializeObject(routes, Formatting.Indented);
327-
File.WriteAllText(RouteJsonName, json);
328-
}
329278
}
330279
}

Source/ORTS.Settings/ContentSettings.cs

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,83 +15,85 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717

18-
using System;
1918
using System.Collections.Generic;
20-
using System.IO;
21-
using System.Reflection;
19+
using Newtonsoft.Json;
2220
using ORTS.Common;
21+
using static ORTS.Settings.ContentRouteSettings;
2322

2423
namespace ORTS.Settings
2524
{
2625
public class ContentSettings : SettingsBase
2726
{
2827
#region User Settings
29-
30-
[Default("")]
31-
public string InstallPath { get; set; }
32-
28+
public ContentRouteSettings ContentRouteSettings;
3329
#endregion
3430

3531
public ContentSettings(IEnumerable<string> options)
36-
: base(SettingsStore.GetSettingStore(UserSettings.SettingsFilePath, UserSettings.RegistryKey, "Content"))
32+
: base(SettingsStore.GetSettingStore(UserSettings.SettingsFilePath, UserSettings.RegistryKey, "ContentRoutes"))
3733
{
34+
ContentRouteSettings = new ContentRouteSettings();
3835
Load(options);
39-
if (string.IsNullOrEmpty(InstallPath))
40-
{
41-
InstallPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) , "Open Rails", "Content");
42-
}
4336
}
4437

4538
public override object GetDefaultValue(string name)
4639
{
47-
var property = GetType().GetProperty(name);
48-
49-
if (property.GetCustomAttributes(typeof(DefaultAttribute), false).Length > 0)
50-
return (property.GetCustomAttributes(typeof(DefaultAttribute), false)[0] as DefaultAttribute).Value;
51-
52-
throw new InvalidDataException(String.Format("UserSetting {0} has no default value.", property.Name));
40+
return "";
5341
}
54-
public override void Reset()
42+
43+
protected override object GetValue(string name)
5544
{
56-
throw new System.NotImplementedException();
45+
return JsonConvert.SerializeObject(ContentRouteSettings.Routes[name], Formatting.Indented);
5746
}
5847

59-
public override void Save()
48+
protected override void SetValue(string name, object value)
6049
{
61-
foreach (var property in GetProperties())
62-
if (property.GetCustomAttributes(typeof(DoNotSaveAttribute), false).Length == 0)
63-
Save(property.Name, property.PropertyType);
50+
ContentRouteSettings.Routes[name] = JsonConvert.DeserializeObject<Route>((string)value);
6451
}
6552

66-
public override void Save(string name)
53+
protected override void Load(Dictionary<string, string> optionsDictionary)
6754
{
68-
throw new System.NotImplementedException();
55+
foreach (var name in SettingStore.GetUserNames())
56+
{
57+
Load(optionsDictionary, name, typeof(string));
58+
}
6959
}
7060

71-
protected override object GetValue(string name)
61+
public override void Save()
7262
{
73-
return GetProperty(name).GetValue(this, null);
74-
}
63+
foreach (var name in ContentRouteSettings.Routes.Keys)
64+
{
65+
if (ContentRouteSettings.Routes.ContainsKey(name))
66+
{
67+
if (ContentRouteSettings.Routes[name].Installed)
68+
{
69+
Save(name);
70+
}
71+
}
72+
}
7573

76-
PropertyInfo[] GetProperties()
77-
{
78-
return GetType().GetProperties();
79-
}
74+
foreach (var name in SettingStore.GetUserNames())
75+
{
76+
if (!ContentRouteSettings.Routes.ContainsKey(name) ||
77+
(!ContentRouteSettings.Routes[name].Installed))
78+
{
79+
var route = ContentRouteSettings.Routes[name];
80+
// remove from registry
81+
Reset(name);
82+
// ContentRouteSettings.Routes.Add(name, route);
83+
}
84+
85+
}
8086

81-
protected override void Load(Dictionary<string, string> optionsDictionary)
82-
{
83-
foreach (var property in GetProperties())
84-
Load(optionsDictionary, property.Name, property.PropertyType);
8587
}
8688

87-
PropertyInfo GetProperty(string name)
89+
public override void Save(string name)
8890
{
89-
return GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
91+
Save(name, typeof(string));
9092
}
9193

92-
protected override void SetValue(string name, object value)
94+
public override void Reset()
9395
{
94-
GetProperty(name).SetValue(this, value, null);
96+
throw new System.NotImplementedException();
9597
}
9698
}
9799
}

Source/ORTS.Settings/UserSettings.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,16 @@ public string DirectXFeatureLevel
436436
[Default(0)] // TrackMonitor.DisplayMode.All
437437
public int TrackMonitorDisplayMode { get; set; }
438438

439+
// Content form settings
440+
[Default("")]
441+
public string ContentInstallPath { get; set; }
442+
439443
#endregion
440444

441445
public FolderSettings Folders { get; private set; }
442446
public InputSettings Input { get; private set; }
443447
public RailDriverSettings RailDriver { get; private set; }
444-
public ContentSettings Content { get; private set; }
445-
public ContentRouteSettings Routes { get; private set; }
448+
public ContentSettings Content { get; private set; }
446449

447450
public UserSettings(IEnumerable<string> options)
448451
: base(SettingsStore.GetSettingStore(SettingsFilePath, RegistryKey, null))
@@ -455,7 +458,6 @@ public UserSettings(IEnumerable<string> options)
455458
Input = new InputSettings(options);
456459
RailDriver = new RailDriverSettings(options);
457460
Content = new ContentSettings(options);
458-
Routes = new ContentRouteSettings();
459461
}
460462

461463
/// <summary>

0 commit comments

Comments
 (0)