Skip to content

Commit

Permalink
Game settings now use source generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Apostolique committed Apr 23, 2023
1 parent d756bf4 commit e9e7947
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
19 changes: 10 additions & 9 deletions Source/Full/Game/Layer1/GameRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Apos.Input;
using System.Text.Json.Serialization.Metadata;

namespace GameProject {
public class GameRoot : Game {
Expand All @@ -14,7 +15,7 @@ public GameRoot() {
IsMouseVisible = true;
Content.RootDirectory = "Content";

_settings = EnsureJson<Settings>("Settings.json");
_settings = EnsureJson<Settings>("Settings.json", SettingsContext.Default.Settings);
}

protected override void Initialize() {
Expand Down Expand Up @@ -46,7 +47,7 @@ protected override void UnloadContent() {
SaveWindow();
}

SaveJson<Settings>("Settings.json", _settings);
SaveJson<Settings>("Settings.json", _settings, SettingsContext.Default.Settings);

base.UnloadContent();
}
Expand Down Expand Up @@ -99,32 +100,32 @@ private void ToggleBorderless() {
}

public static string GetPath(string name) => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, name);
public static T LoadJson<T>(string name) where T : new() {
public static T LoadJson<T>(string name, JsonTypeInfo<T> typeInfo) where T : new() {
T json;
string jsonPath = GetPath(name);

if (File.Exists(jsonPath)) {
json = JsonSerializer.Deserialize<T>(File.ReadAllText(jsonPath), _options);
json = JsonSerializer.Deserialize<T>(File.ReadAllText(jsonPath), typeInfo);
} else {
json = new T();
}

return json;
}
public static void SaveJson<T>(string name, T json) {
public static void SaveJson<T>(string name, T json, JsonTypeInfo<T> typeInfo) {
string jsonPath = GetPath(name);
string jsonString = JsonSerializer.Serialize(json, _options);
string jsonString = JsonSerializer.Serialize(json, typeInfo);
File.WriteAllText(jsonPath, jsonString);
}
public static T EnsureJson<T>(string name) where T : new() {
public static T EnsureJson<T>(string name, JsonTypeInfo<T> typeInfo) where T : new() {
T json;
string jsonPath = GetPath(name);

if (File.Exists(jsonPath)) {
json = JsonSerializer.Deserialize<T>(File.ReadAllText(jsonPath), _options);
json = JsonSerializer.Deserialize<T>(File.ReadAllText(jsonPath), typeInfo);
} else {
json = new T();
string jsonString = JsonSerializer.Serialize(json, _options);
string jsonString = JsonSerializer.Serialize(json, typeInfo);
File.WriteAllText(jsonPath, jsonString);
}

Expand Down
8 changes: 8 additions & 0 deletions Source/Full/Game/Layer1/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.Json.Serialization;

namespace GameProject {
public class Settings {
public int X { get; set; } = 320;
Expand All @@ -9,4 +11,10 @@ public class Settings {
public bool IsFullscreen { get; set; } = false;
public bool IsBorderless { get; set; } = false;
}

[JsonSourceGenerationOptionsAttribute(
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
WriteIndented = true)]
[JsonSerializable(typeof(Settings))]
internal partial class SettingsContext : JsonSerializerContext { }
}
19 changes: 10 additions & 9 deletions Source/Simple/Game/GameRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Apos.Input;
using System.Text.Json.Serialization.Metadata;

namespace GameProject {
public class GameRoot : Game {
Expand All @@ -14,7 +15,7 @@ public GameRoot() {
IsMouseVisible = true;
Content.RootDirectory = "Content";

_settings = EnsureJson<Settings>("Settings.json");
_settings = EnsureJson<Settings>("Settings.json", SettingsContext.Default.Settings);
}

protected override void Initialize() {
Expand Down Expand Up @@ -46,7 +47,7 @@ protected override void UnloadContent() {
SaveWindow();
}

SaveJson<Settings>("Settings.json", _settings);
SaveJson<Settings>("Settings.json", _settings, SettingsContext.Default.Settings);

base.UnloadContent();
}
Expand Down Expand Up @@ -99,32 +100,32 @@ private void ToggleBorderless() {
}

public static string GetPath(string name) => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, name);
public static T LoadJson<T>(string name) where T : new() {
public static T LoadJson<T>(string name, JsonTypeInfo<T> typeInfo) where T : new() {
T json;
string jsonPath = GetPath(name);

if (File.Exists(jsonPath)) {
json = JsonSerializer.Deserialize<T>(File.ReadAllText(jsonPath), _options);
json = JsonSerializer.Deserialize<T>(File.ReadAllText(jsonPath), typeInfo);
} else {
json = new T();
}

return json;
}
public static void SaveJson<T>(string name, T json) {
public static void SaveJson<T>(string name, T json, JsonTypeInfo<T> typeInfo) {
string jsonPath = GetPath(name);
string jsonString = JsonSerializer.Serialize(json, _options);
string jsonString = JsonSerializer.Serialize(json, typeInfo);
File.WriteAllText(jsonPath, jsonString);
}
public static T EnsureJson<T>(string name) where T : new() {
public static T EnsureJson<T>(string name, JsonTypeInfo<T> typeInfo) where T : new() {
T json;
string jsonPath = GetPath(name);

if (File.Exists(jsonPath)) {
json = JsonSerializer.Deserialize<T>(File.ReadAllText(jsonPath), _options);
json = JsonSerializer.Deserialize<T>(File.ReadAllText(jsonPath), typeInfo);
} else {
json = new T();
string jsonString = JsonSerializer.Serialize(json, _options);
string jsonString = JsonSerializer.Serialize(json, typeInfo);
File.WriteAllText(jsonPath, jsonString);
}

Expand Down
8 changes: 8 additions & 0 deletions Source/Simple/Game/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.Json.Serialization;

namespace GameProject {
public class Settings {
public int X { get; set; } = 320;
Expand All @@ -9,4 +11,10 @@ public class Settings {
public bool IsFullscreen { get; set; } = false;
public bool IsBorderless { get; set; } = false;
}

[JsonSourceGenerationOptionsAttribute(
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
WriteIndented = true)]
[JsonSerializable(typeof(Settings))]
internal partial class SettingsContext : JsonSerializerContext { }
}

0 comments on commit e9e7947

Please sign in to comment.