Created
July 31, 2023 23:43
-
-
Save thatcosmonaut/b841d01c6026edcc1f89a646ed43fce4 to your computer and use it in GitHub Desktop.
Samurai Gunn 2 Settings Loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Text.Json; | |
using System.Text.Json.Serialization; | |
using MoonWorks; | |
namespace SamuraiGunn2 | |
{ | |
[JsonSerializable(typeof(VideoSettings))] | |
internal partial class VideoSettingsContext : JsonSerializerContext | |
{ | |
} | |
[JsonSerializable(typeof(ControlBindings))] | |
internal partial class ControlBindingsContext : JsonSerializerContext | |
{ | |
} | |
public static class SettingsHelper | |
{ | |
#if !NATIVEAOT_CONSOLE | |
static string VideoSettingsFilePath = Path.Combine(Title.UserDataDirectory, "video_settings.json"); | |
static string ControlSettingsFilePath = Path.Combine(Title.UserDataDirectory, "control_settings.json"); | |
#else | |
static string VideoSettingsFilePath = ""; | |
static string ControlSettingsFilePath = ""; | |
#endif | |
static JsonSerializerOptions videoSerializerOptions = new JsonSerializerOptions | |
{ | |
IncludeFields = true, | |
WriteIndented = true | |
}; | |
static JsonSerializerOptions controlSerializerOptions = new JsonSerializerOptions | |
{ | |
IncludeFields = true, | |
WriteIndented = true | |
}; | |
static VideoSettingsContext videoSettingsContext = new VideoSettingsContext(videoSerializerOptions); | |
static ControlBindingsContext controlBindingsContext = new ControlBindingsContext(controlSerializerOptions); | |
public static void WriteVideoSettings(VideoSettings videoSettings) | |
{ | |
#if !NATIVEAOT_CONSOLE | |
var jsonString = JsonSerializer.Serialize(videoSettings, typeof(VideoSettings), videoSettingsContext); | |
System.IO.File.WriteAllText(VideoSettingsFilePath, jsonString); | |
#endif | |
} | |
public static VideoSettings ReadVideoSettings() | |
{ | |
if (!File.Exists(VideoSettingsFilePath)) | |
{ | |
WriteVideoSettings(VideoSettings.Default); | |
return VideoSettings.Default; | |
} | |
try | |
{ | |
var settings = (VideoSettings) JsonSerializer.Deserialize(File.ReadAllText(VideoSettingsFilePath), typeof(VideoSettings), videoSettingsContext); | |
return settings; | |
} | |
catch (JsonException e) | |
{ | |
Logger.WriteLine(e.Message); | |
Logger.WriteLine("Falling back to default video settings..."); | |
WriteVideoSettings(VideoSettings.Default); | |
return VideoSettings.Default; | |
} | |
} | |
public static void WriteControlSettings(ControlBindings bindings) | |
{ | |
#if !NATIVEAOT_CONSOLE | |
var jsonString = JsonSerializer.Serialize(bindings, typeof(ControlBindings), controlBindingsContext); | |
System.IO.File.WriteAllText(ControlSettingsFilePath, jsonString); | |
#endif | |
} | |
public static ControlBindings ReadControlBindings() | |
{ | |
if (!File.Exists(ControlSettingsFilePath)) | |
{ | |
WriteControlSettings(ControlBindings.Default); | |
return ControlBindings.Default; | |
} | |
try | |
{ | |
var settings = (ControlBindings) JsonSerializer.Deserialize(File.ReadAllText(ControlSettingsFilePath), typeof(ControlBindings), controlBindingsContext); | |
return settings; | |
} | |
catch (JsonException e) | |
{ | |
Logger.WriteLine(e.Message); | |
Logger.WriteLine("Falling back to default controls..."); | |
WriteControlSettings(ControlBindings.Default); | |
return ControlBindings.Default; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MoonWorks; | |
using System.Text.Json.Serialization; | |
namespace SamuraiGunn2 | |
{ | |
// Used to read/write video settings from file. | |
public struct VideoSettings | |
{ | |
public static VideoSettings Default = new VideoSettings | |
{ | |
WindowMultiplier = 1, | |
ScreenMode = ScreenMode.BorderlessFullscreen, | |
UseLighting = true, | |
UseInterpolation = false | |
}; | |
[JsonRequiredAttribute] | |
public uint WindowMultiplier; | |
[JsonRequiredAttribute] | |
public ScreenMode ScreenMode; | |
[JsonRequiredAttribute] | |
public bool UseLighting; | |
[JsonRequiredAttribute] | |
public bool UseInterpolation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment