Skip to content

Commit b68d778

Browse files
committed
remove index version, add more async methods
1 parent abd7cac commit b68d778

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

AvaGui/Models/ObjectEditorModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ async Task LoadObjDirectoryAsyncCore(string directory, IProgress<float> progress
348348

349349
try
350350
{
351-
ObjectIndex = ObjectIndex.LoadIndex(IndexFilename) ?? ObjectIndex;
351+
ObjectIndex = await ObjectIndex.LoadIndexAsync(IndexFilename) ?? ObjectIndex;
352352
}
353353
catch (Exception ex)
354354
{

Dat/ObjectIndex.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
using OpenLoco.Dat.FileParsing;
33
using OpenLoco.Dat.Objects;
44
using System.Collections.Concurrent;
5+
using System.Text;
56
using System.Text.Json;
67

78
namespace Dat
89
{
910
public class ObjectIndex
1011
{
11-
public const int JsonVersion = 1; // change this every time this format changes
12-
public int Version => JsonVersion;
13-
1412
public required IList<ObjectIndexEntry> Objects { get; set; } = [];
1513

1614
public required IList<ObjectIndexFailedEntry> ObjectsFailed { get; set; } = [];
@@ -63,13 +61,24 @@ public void SaveIndex(string indexFile)
6361
public void SaveIndex(string indexFile, JsonSerializerOptions options)
6462
=> File.WriteAllText(indexFile, JsonSerializer.Serialize(this, options));
6563

66-
public static ObjectIndex LoadIndex(string indexFile)
64+
public static ObjectIndex? LoadIndex(string indexFile)
6765
=> JsonSerializer.Deserialize<ObjectIndex>(File.ReadAllText(indexFile));
6866

69-
public static ObjectIndex LoadIndex(string indexFile, JsonSerializerOptions options)
67+
public static ObjectIndex? LoadIndex(string indexFile, JsonSerializerOptions options)
7068
=> JsonSerializer.Deserialize<ObjectIndex>(File.ReadAllText(indexFile), options);
7169

72-
public static ObjectIndex LoadOrCreateIndex(string directory)
70+
public static async Task<ObjectIndex?> LoadIndexAsync(string indexFile)
71+
{
72+
await using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(await File.ReadAllTextAsync(indexFile))))
73+
{
74+
return await JsonSerializer.DeserializeAsync<ObjectIndex>(stream);
75+
}
76+
}
77+
78+
public static ObjectIndex? LoadOrCreateIndex(string directory)
79+
=> LoadOrCreateIndexAsync(directory).Result;
80+
81+
public static async Task<ObjectIndex?> LoadOrCreateIndexAsync(string directory)
7382
{
7483
var indexPath = Path.Combine(directory, "objectIndex.json");
7584
ObjectIndex? index;
@@ -80,7 +89,7 @@ public static ObjectIndex LoadOrCreateIndex(string directory)
8089
else
8190
{
8291
var fileArr = SawyerStreamUtils.GetDatFilesInDirectory(directory).ToArray();
83-
index = CreateIndexAsync(directory, fileArr, null).Result;
92+
index = await CreateIndexAsync(directory, fileArr, null);
8493
index.SaveIndex(indexPath);
8594
}
8695

DatabaseSeeder/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ static void SeedDb(LocoDb db, bool deleteExisting)
151151
Authors = authors ?? [],
152152
CreationDate = creationTime,
153153
LastEditDate = null,
154+
UploadDate = DateTimeOffset.Now,
154155
Tags = tags ?? [],
155156
Modpacks = modpacks ?? [],
156157
Availability = ObjectAvailability.NewGames,

WinGui/MainFormModel.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,10 @@ public void LoadObjDirectory(string directory, IProgress<float>? progress, bool
254254

255255
Settings.ObjDataDirectory = directory;
256256
var allFiles = Directory.GetFiles(directory, "*.dat", SearchOption.AllDirectories);
257-
if (useExistingIndex && File.Exists(Settings.GetObjDataFullPath(Settings.IndexFileName)))
257+
var indexFileName = Settings.GetObjDataFullPath(Settings.IndexFileName);
258+
if (useExistingIndex && File.Exists(indexFileName))
258259
{
259-
ObjectIndex = DeserialiseHeaderIndexFromFile(Settings.GetObjDataFullPath(Settings.IndexFileName)) ?? ObjectIndex;
260+
ObjectIndex = ObjectIndex.LoadIndex(indexFileName) ?? ObjectIndex;
260261

261262
var filenames = ObjectIndex.Objects.Select(x => x.Filename).Concat(ObjectIndex.ObjectsFailed.Select(x => x.Filename));
262263
var a = filenames.Except(allFiles);
@@ -354,17 +355,6 @@ static void SerialiseHeaderIndexToFile(string filename, ObjectIndex headerIndex,
354355
headerIndex.SaveIndex(filename, options);
355356
}
356357

357-
static ObjectIndex? DeserialiseHeaderIndexFromFile(string filename, ILogger? logger = null)
358-
{
359-
if (!File.Exists(filename))
360-
{
361-
logger?.Info($"Settings file {filename} does not exist");
362-
return null;
363-
}
364-
logger?.Info($"Loading settings from {filename}");
365-
return ObjectIndex.LoadIndex(filename);
366-
}
367-
368358
public UiLocoObject? LoadAndCacheObject(string filename)
369359
{
370360
if (string.IsNullOrEmpty(filename) || !filename.EndsWith(".dat", StringComparison.InvariantCultureIgnoreCase) || !File.Exists(filename))

0 commit comments

Comments
 (0)