Skip to content

Commit

Permalink
Fixed exceptions when accessing the game registry on a new install.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkDaskin committed Mar 29, 2024
1 parent 80b4145 commit 2a39549
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions UnityModStudio.Common/Options/GameRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public bool WatchForChanges

public GameRegistry()
{
_fsWatcher = new FileSystemWatcher(Path.GetDirectoryName(_storePath)!, Path.GetFileName(_storePath))
var directoryName = Path.GetDirectoryName(_storePath)!;
Directory.CreateDirectory(directoryName);

_fsWatcher = new FileSystemWatcher(directoryName, Path.GetFileName(_storePath))
{
NotifyFilter = NotifyFilters.LastWrite,
EnableRaisingEvents = false,
Expand Down Expand Up @@ -94,7 +97,7 @@ public async Task LoadAsync()
try
{
using var stream = File.OpenRead(_storePath);
foreach (var game in await JsonSerializer.DeserializeAsync<Game[]>(stream) ?? Array.Empty<Game>())
foreach (var game in await JsonSerializer.DeserializeAsync<Game[]>(stream) ?? [])
AddGame(game);
}
catch (Exception exception)
Expand All @@ -107,9 +110,7 @@ public async Task SaveAsync()
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(_storePath)!);

using var stream = File.Open(_storePath, FileMode.Truncate);
using var stream = File.Open(_storePath, FileMode.Create);
await JsonSerializer.SerializeAsync(stream, Games);
}
catch (Exception exception)
Expand Down

0 comments on commit 2a39549

Please sign in to comment.