Skip to content

Commit 39dfbb6

Browse files
committed
update to GaldrJson, cleanup, minor bug fixes
1 parent ac34fd9 commit 39dfbb6

File tree

11 files changed

+82
-71
lines changed

11 files changed

+82
-71
lines changed

Config.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Runtime.InteropServices;
4-
using System.Text.Json;
4+
using GaldrJson;
55
using Microsoft.Win32;
66
using Scum_Bag.DataAccess.Data;
77

@@ -11,6 +11,7 @@ internal sealed class Config
1111
{
1212
#region Fields
1313

14+
private readonly IGaldrJsonSerializer _jsonSerializer;
1415
private readonly string _dataDir;
1516
private readonly string _savesPath;
1617
private readonly string _latestScreenshotName;
@@ -23,8 +24,10 @@ internal sealed class Config
2324

2425
#region Constructor
2526

26-
public Config()
27+
public Config(IGaldrJsonSerializer jsonSerializer)
2728
{
29+
_jsonSerializer = jsonSerializer;
30+
2831
_dataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Scum Bag");
2932
_backupsDirectory = _dataDir;
3033
_savesPath = Path.Combine(_dataDir, "saves.json");
@@ -95,7 +98,7 @@ public Settings GetSettings()
9598

9699
if (File.Exists(_settingsPath))
97100
{
98-
settings = JsonSerializer.Deserialize(File.ReadAllText(_settingsPath), SaveDataJsonSerializerContext.Default.Settings);
101+
settings = _jsonSerializer.Deserialize<Settings>(File.ReadAllText(_settingsPath));
99102
}
100103

101104
return settings;
@@ -105,7 +108,7 @@ public void SaveSettings(Settings settings)
105108
{
106109
_backupsDirectory = settings.BackupsDirectory;
107110
_steamExePath = settings.SteamExePath;
108-
string settingsFileContent = JsonSerializer.Serialize(settings, SaveDataJsonSerializerContext.Default.Settings);
111+
string settingsFileContent = _jsonSerializer.Serialize(settings);
109112
File.WriteAllText(_settingsPath, settingsFileContent);
110113
}
111114

@@ -117,7 +120,7 @@ private void Initialize()
117120
{
118121
if (File.Exists(_settingsPath))
119122
{
120-
Settings settings = JsonSerializer.Deserialize(File.ReadAllText(_settingsPath), SaveDataJsonSerializerContext.Default.Settings);
123+
Settings settings = _jsonSerializer.Deserialize<Settings>(File.ReadAllText(_settingsPath));
121124
_backupsDirectory = settings.BackupsDirectory;
122125
_steamExePath = settings.SteamExePath;
123126
}
@@ -126,7 +129,7 @@ private void Initialize()
126129
{
127130
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
128131
{
129-
_steamExePath = Registry.CurrentUser.OpenSubKey("Software\\Valve\\Steam").GetValue("SteamExe").ToString();
132+
_steamExePath = Registry.CurrentUser.OpenSubKey("Software\\Valve\\Steam")?.GetValue("SteamExe")?.ToString();
130133
}
131134
else
132135
{

DataAccess/Data/SaveGame.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using GaldrJson;
34

45
namespace Scum_Bag.DataAccess.Data;
56

7+
[GaldrJsonSerializable]
68
internal sealed class SaveGame
79
{
810
public Guid Id { get; set; }

DataAccess/Data/Settings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using GaldrJson;
2+
13
namespace Scum_Bag.DataAccess.Data;
24

5+
[GaldrJsonSerializable]
36
internal sealed class Settings
47
{
58
public string Theme { get; set; }

Program.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,16 @@ internal class Program
1414
[STAThread]
1515
static void Main()
1616
{
17-
Config config = new();
18-
using LoggingService loggingService = new(config);
17+
using LoggingService loggingService = new();
1918

2019
try
2120
{
22-
//if (!OperatingSystem.IsWindows())
23-
//{
24-
// Galdr.Native.Generated.GaldrGeneratedInitializer.Initialize();
25-
//}
26-
2721
GaldrBuilder builder = new GaldrBuilder()
2822
.SetTitle("Scum Bag - Save Manager")
2923
.SetSize(1100, 775)
3024
.SetMinSize(800, 600)
31-
.AddSingleton(config)
3225
.AddSingleton(loggingService)
26+
.AddSingleton<Config>()
3327
.AddSingleton<BackupService>()
3428
.AddSingleton<GameService>()
3529
.AddSingleton<SaveService>()
@@ -38,10 +32,6 @@ static void Main()
3832
.AddSingleton<FileService>()
3933
.AddSingleton<IShutterService, ShutterService>();
4034
#if DEBUG
41-
//EmbeddedContent embeddedContent = new(embeddedNamespace: "Scum_Bag");
42-
//builder.SetContentProvider(embeddedContent);
43-
//builder.SetDebug(true);
44-
4535
int port = 1314;
4636
UrlContent urlContent = new($"http://localhost:{port}");
4737
builder.SetContentProvider(urlContent);
@@ -51,7 +41,6 @@ static void Main()
5141
EmbeddedContent embeddedContent = new(embeddedNamespace: "Scum_Bag");
5242
builder.SetContentProvider(embeddedContent);
5343
#endif
54-
5544
AddSaveGameCommands(builder);
5645
AddGameCommands(builder);
5746
AddDialogCommands(builder);

SaveDataJsonSerializerContext.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

Scum Bag.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Galdr.Native" Version="1.1.1" />
11+
<PackageReference Include="Galdr.Native" Version="1.1.2" />
1212
<PackageReference Include="Gameloop.Vdf" Version="0.6.2" />
1313
<PackageReference Include="Shutter" Version="1.0.0" />
1414
</ItemGroup>

Services/BackupService.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5-
using System.Text.Json;
65
using System.Timers;
76
using Galdr.Native;
7+
using GaldrJson;
88
using Scum_Bag.DataAccess.Data;
99

1010
namespace Scum_Bag.Services;
@@ -20,19 +20,22 @@ internal sealed class BackupService
2020
private readonly ScreenshotService _screenshotService;
2121
private readonly Dictionary<Guid, Timer> _backupTimers = new();
2222
private readonly Dictionary<Guid, ElapsedEventHandler> _backupTimerEvents = new();
23+
private readonly IGaldrJsonSerializer _jsonSerializer;
2324

2425
#endregion
2526

2627
#region Constructor
2728

2829
public BackupService(Config config, EventService eventService, LoggingService loggingService,
29-
FileService fileService, ScreenshotService screenshotService)
30+
FileService fileService, ScreenshotService screenshotService,
31+
IGaldrJsonSerializer jsonSerializer)
3032
{
3133
_config = config;
3234
_eventService = eventService;
3335
_loggingService = loggingService;
3436
_fileService = fileService;
3537
_screenshotService = screenshotService;
38+
_jsonSerializer = jsonSerializer;
3639

3740
Initialize();
3841
}
@@ -252,7 +255,7 @@ private bool CreateBackup(Guid id)
252255

253256
// actual count is dirs length + 1 because a new backup was just made,
254257
// and don't count any favorites toward the max backups
255-
int totalBackups = dirs.Length + 1 - saveGame.BackupMetadata.Where(x => x.Value.IsFavorite).Count();
258+
int totalBackups = dirs.Length + 1 - saveGame.BackupMetadata?.Where(x => x.Value.IsFavorite)?.Count() ?? 0;
256259

257260
while (totalBackups > saveGame.MaxBackups)
258261
{
@@ -286,7 +289,7 @@ private void DeleteOldestBackup(SaveGame saveGame)
286289
{
287290
bool isFavorite = false;
288291

289-
if (saveGame.BackupMetadata.TryGetValue(dir.FullName, out BackupMetadata backupMetadata))
292+
if (saveGame.BackupMetadata?.TryGetValue(dir.FullName, out BackupMetadata backupMetadata) == true)
290293
{
291294
isFavorite = backupMetadata.IsFavorite;
292295
}
@@ -318,7 +321,7 @@ private List<SaveGame> GetSaveGames()
318321

319322
try
320323
{
321-
saveGames = JsonSerializer.Deserialize(File.ReadAllText(_config.SavesPath), SaveDataJsonSerializerContext.Default.ListSaveGame);
324+
saveGames = _jsonSerializer.Deserialize<List<SaveGame>>(File.ReadAllText(_config.SavesPath));
322325
}
323326
catch (Exception e)
324327
{

Services/LoggingService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ internal sealed class LoggingService : IDisposable
1818

1919
#region Constructor
2020

21-
public LoggingService(Config config)
21+
public LoggingService()
2222
{
23-
_logFileLocation = Path.Combine(config.DataDirectory, "logs.txt");
23+
_logFileLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Scum Bag", "logs.txt");
2424
_logQueue = new();
2525
_logQueueTokenSource = new();
2626

0 commit comments

Comments
 (0)