15
15
using System . IO ;
16
16
using System . Linq ;
17
17
using System . Net . Http ;
18
- using System . Text . Json ;
19
18
using System . Threading ;
20
19
using System . Threading . Tasks ;
21
20
@@ -50,11 +49,10 @@ public class ObjectEditorModel
50
49
public const string ApplicationName = "OpenLoco Object Editor" ;
51
50
public const string LoggingFileName = "objectEditor.log" ;
52
51
53
- public static string SettingsPath => Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , ApplicationName ) ;
54
-
55
- public static string SettingsFile => Path . Combine ( SettingsPath , Environment . GetEnvironmentVariable ( "ENV_SETTINGS_FILE" ) ?? "settings.json" ) ; // "settings-dev.json" for dev, "settings.json" for prod
56
-
57
- public static string LoggingFile => Path . Combine ( SettingsPath , LoggingFileName ) ;
52
+ // stores settings.json, objectEditor.log, etc
53
+ public static string ProgramDataPath => Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , ApplicationName ) ;
54
+ public static string SettingsFile => Path . Combine ( ProgramDataPath , Environment . GetEnvironmentVariable ( "ENV_SETTINGS_FILE" ) ?? EditorSettings . DefaultFileName ) ;
55
+ public static string LoggingFile => Path . Combine ( ProgramDataPath , LoggingFileName ) ;
58
56
59
57
public ObservableCollection < LogLine > LoggerObservableLogs = [ ] ;
60
58
@@ -65,9 +63,9 @@ public ObjectEditorModel()
65
63
Logger = new Logger ( ) ;
66
64
LoggerObservableLogs = [ ] ;
67
65
Logger . LogAdded += ( sender , laea ) => Dispatcher . UIThread . Post ( ( ) => LoggerObservableLogs . Insert ( 0 , laea . Log ) ) ;
68
- //Logger.LogAdded += (sender, laea) => File.AppendAllLines(LoggingFile, [laea.Log.ToString()]);
69
66
70
67
LoadSettings ( ) ;
68
+ InitialiseDownloadDirectory ( ) ;
71
69
72
70
var serverAddress = Settings ! . UseHttps ? Settings . ServerAddressHttps : Settings . ServerAddressHttp ;
73
71
@@ -82,38 +80,26 @@ public ObjectEditorModel()
82
80
}
83
81
}
84
82
85
- public void LoadSettings ( )
83
+ void LoadSettings ( )
86
84
{
87
- if ( ! File . Exists ( SettingsFile ) )
88
- {
89
- Logger . Info ( $ "Settings file doesn't exist; creating now at \" { SettingsFile } \" ") ;
90
- Settings = new ( ) ;
91
- SaveSettings ( ) ;
92
- return ;
93
- }
94
-
95
- var text = File . ReadAllText ( SettingsFile ) ;
96
- var settings = JsonSerializer . Deserialize < EditorSettings > ( text , options : new ( ) { WriteIndented = true } ) ; // todo: try-catch this for invalid settings files
97
- ArgumentNullException . ThrowIfNull ( settings ) ;
98
-
99
- Settings = settings ! ;
100
- InitialiseDownloadDirectory ( ) ;
85
+ Settings = EditorSettings . Load ( SettingsFile , Logger ) ;
101
86
102
- if ( ValidateSettings ( Settings , Logger ) && File . Exists ( IndexFilename ) )
87
+ if ( Settings . Validate ( Logger ) )
103
88
{
104
89
Logger . Info ( "Settings loaded and validated successfully." ) ;
105
90
}
106
91
else
107
92
{
108
93
Logger . Error ( "Unable to validate settings file - please delete it and it will be recreated on next editor start-up." ) ;
109
94
}
95
+
110
96
}
111
97
112
98
void InitialiseDownloadDirectory ( )
113
99
{
114
100
if ( string . IsNullOrEmpty ( Settings . DownloadFolder ) )
115
101
{
116
- Settings . DownloadFolder = Path . Combine ( SettingsPath , "downloads" ) ;
102
+ Settings . DownloadFolder = Path . Combine ( ProgramDataPath , "downloads" ) ;
117
103
}
118
104
119
105
if ( ! Directory . Exists ( Settings . DownloadFolder ) )
@@ -123,52 +109,6 @@ void InitialiseDownloadDirectory()
123
109
}
124
110
}
125
111
126
- public string IndexFilename
127
- => Settings . GetObjDataFullPath ( Settings . IndexFileName ) ;
128
-
129
- static bool ValidateSettings ( EditorSettings settings , ILogger ? logger )
130
- {
131
- if ( settings == null )
132
- {
133
- logger ? . Error ( "Invalid settings file: Unable to deserialise settings file" ) ;
134
- return false ;
135
- }
136
-
137
- if ( string . IsNullOrEmpty ( settings . ObjDataDirectory ) )
138
- {
139
- logger ? . Warning ( "Invalid settings file: Object directory was null or empty" ) ;
140
- return false ;
141
- }
142
-
143
- if ( ! Directory . Exists ( settings . ObjDataDirectory ) )
144
- {
145
- logger ? . Warning ( $ "Invalid settings file: Directory \" { settings . ObjDataDirectory } \" does not exist") ;
146
- return false ;
147
- }
148
-
149
- return true ;
150
- }
151
-
152
- public void SaveSettings ( )
153
- {
154
- var text = JsonSerializer . Serialize ( Settings , options : new ( ) { WriteIndented = true } ) ;
155
-
156
- var parentDir = Path . GetDirectoryName ( SettingsFile ) ;
157
- if ( parentDir != null && ! Directory . Exists ( parentDir ) )
158
- {
159
- _ = Directory . CreateDirectory ( parentDir ) ;
160
- }
161
-
162
- try
163
- {
164
- File . WriteAllText ( SettingsFile , text ) ;
165
- }
166
- catch ( Exception ex )
167
- {
168
- Logger . Error ( ex ) ;
169
- }
170
- }
171
-
172
112
public bool TryLoadObject ( FileSystemItem filesystemItem , out UiDatLocoFile ? uiLocoFile )
173
113
{
174
114
if ( string . IsNullOrEmpty ( filesystemItem . Filename ) )
@@ -309,59 +249,6 @@ public bool TryLoadObject(FileSystemItem filesystemItem, out UiDatLocoFile? uiLo
309
249
return true ;
310
250
}
311
251
312
- public bool LoadDataDirectory ( string directory )
313
- {
314
- if ( ! Directory . Exists ( directory ) )
315
- {
316
- Logger . Warning ( "Invalid directory: doesn't exist" ) ;
317
- return false ;
318
- }
319
-
320
- Settings . DataDirectory = directory ;
321
-
322
- var allDataFiles = Directory . GetFiles ( Settings . DataDirectory ) . Select ( f => Path . GetFileName ( f ) . ToLower ( ) ) . ToHashSet ( ) ;
323
-
324
- void LoadKnownData ( HashSet < string > allFilesInDir , HashSet < string > knownFilenames , Dictionary < string , byte [ ] > dict )
325
- {
326
- dict . Clear ( ) ;
327
- var expectedMusicFiles = knownFilenames . Select ( f => f . ToLower ( ) ) ;
328
- foreach ( var music in expectedMusicFiles )
329
- {
330
- var matching = allFilesInDir . Where ( f => f . EndsWith ( music ) ) ;
331
- if ( matching . Any ( ) )
332
- {
333
- dict . Add ( music , File . ReadAllBytes ( Path . Combine ( Settings . DataDirectory , music ) ) ) ;
334
- _ = allFilesInDir . RemoveWhere ( f => f . EndsWith ( music ) ) ;
335
- }
336
- }
337
- }
338
-
339
- LoadKnownData ( allDataFiles , [ .. OriginalDataFiles . Music . Keys ] , Music ) ;
340
- LoadKnownData ( allDataFiles , [ .. OriginalDataFiles . MiscellaneousTracks . Keys ] , MiscellaneousTracks ) ;
341
- LoadKnownData ( allDataFiles , [ OriginalDataFiles . SoundEffect ] , SoundEffects ) ;
342
- LoadKnownData ( allDataFiles , OriginalDataFiles . Tutorials , Tutorials ) ;
343
-
344
- //MiscFiles = [.. allDataFiles];
345
-
346
- // load G1 only for now since we need it for palette
347
- //G1 = SawyerStreamReader.LoadG1(Settings.GetDataFullPath(Settings.G1DatFileName));
348
-
349
- //LoadPalette(); // update palette from g1
350
-
351
- //await SaveSettings();
352
-
353
- return true ;
354
- }
355
-
356
- // this method will load any supported file type
357
- //public void LoadDirectory(string directory)
358
- //{
359
- // var allFiles = Directory.GetFiles(directory, "*.dat|*.sv5|*.sc5", SearchOption.AllDirectories);
360
- //}
361
-
362
- //public void LoadObjDirectory(string directory)
363
- // => LoadObjDirectory(directory, new Progress<float>(), true);
364
-
365
252
static Task ? indexerTask ;
366
253
static readonly SemaphoreSlim taskLock = new ( 1 , 1 ) ;
367
254
@@ -393,15 +280,15 @@ async Task LoadObjDirectoryAsyncCore(string directory, IProgress<float> progress
393
280
}
394
281
395
282
Settings . ObjDataDirectory = directory ;
396
- SaveSettings ( ) ;
283
+ Settings . Save ( SettingsFile , Logger ) ;
397
284
398
- if ( useExistingIndex && File . Exists ( IndexFilename ) )
285
+ if ( useExistingIndex && File . Exists ( Settings . IndexFileName ) )
399
286
{
400
287
var exception = false ;
401
288
402
289
try
403
290
{
404
- ObjectIndex = await ObjectIndex . LoadIndexAsync ( IndexFilename ) ?? ObjectIndex ;
291
+ ObjectIndex = await ObjectIndex . LoadIndexAsync ( Settings . IndexFileName ) ?? ObjectIndex ;
405
292
}
406
293
catch ( Exception ex )
407
294
{
@@ -433,7 +320,7 @@ async Task RecreateIndex(string directory, IProgress<float> progress)
433
320
{
434
321
Logger . Info ( "Recreating index file" ) ;
435
322
ObjectIndex = await ObjectIndex . CreateIndexAsync ( directory , Logger , progress ) ;
436
- ObjectIndex ? . SaveIndex ( IndexFilename ) ;
323
+ ObjectIndex ? . SaveIndex ( Settings . IndexFileName ) ;
437
324
}
438
325
}
439
326
0 commit comments