summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ConfigFile.cs91
-rw-r--r--EmbeddedResources.cs16
-rw-r--r--Extensions.cs40
3 files changed, 147 insertions, 0 deletions
diff --git a/ConfigFile.cs b/ConfigFile.cs
new file mode 100644
index 0000000..5ed1054
--- /dev/null
+++ b/ConfigFile.cs
@@ -0,0 +1,91 @@
+namespace SarahEngine;
+
+public class ConfigFile
+{
+ public readonly string Path;
+ readonly Dictionary<string, string> ConfigOptions = [];
+ bool OptionChanged = false;
+ const string TopLine = "# OnekoOnline Config File";
+
+ public ConfigFile(string path)
+ {
+ Path = path;
+
+ if (!File.Exists(Path)) {
+ Console.WriteLine("Config file does not exist, creating one.");
+ File.WriteAllText(Path, TopLine);
+ OptionChanged = true;
+ SaveFileDelayed(3000);
+ return;
+ }
+
+ string[] fileContents = File.ReadAllLines(Path);
+
+ foreach (string line in fileContents) {
+ if (line[0] == '#' || !line.Contains('=') || line.Length < 3) continue;
+
+ string key = line[0..line.IndexOf('=')];
+ string value = line[(line.IndexOf('=')+1)..];
+ ConfigOptions.Add(key, value);
+ }
+ }
+
+ public async void SaveFileDelayed(int delayInMilliseconds)
+ {
+ await Task.Delay(delayInMilliseconds);
+ SaveFile();
+ }
+
+ public void SaveFile()
+ {
+ if (!OptionChanged) return;
+
+ List<string> linesToWrite = [TopLine];
+ foreach (KeyValuePair<string, string> pair in ConfigOptions.OrderBy(k => k.Key)) linesToWrite.Add(pair.Key + "=" + pair.Value);
+ File.WriteAllLines(Path, linesToWrite);
+
+ OptionChanged = false;
+ }
+
+ public T GetValue<T>(string Key, T defaultValue) where T : IConvertible
+ {
+ if (ConfigOptions.TryGetValue(Key, out string? value)) {
+ try {
+ return (T)Convert.ChangeType(value, typeof(T));
+ } catch {
+ Console.WriteLine($"Config option {Key} is invalid, setting to default.");
+ }
+ }
+
+ SetValue(Key, defaultValue);
+ return defaultValue;
+ }
+
+ public string GetValue(string Key, string defaultValue)
+ {
+ if (ConfigOptions.TryGetValue(Key, out string? value)) {
+ return value;
+ }
+
+ SetValue(Key, defaultValue);
+ return defaultValue;
+ }
+
+ public void SetValue<T>(string Key, T Value) where T : IConvertible
+ {
+ string? ValueString = Value.ToString();
+ if (ValueString is null) return;
+
+ SetValue(Key, ValueString);
+ }
+
+ public void SetValue(string Key, string Value)
+ {
+ if (ConfigOptions.TryAdd(Key, Value)) {
+ OptionChanged = true;
+ } else if (ConfigOptions[Key] != Value) {
+ ConfigOptions[Key] = Value;
+ OptionChanged = true;
+ }
+ }
+} \ No newline at end of file
diff --git a/EmbeddedResources.cs b/EmbeddedResources.cs
new file mode 100644
index 0000000..61507a5
--- /dev/null
+++ b/EmbeddedResources.cs
@@ -0,0 +1,16 @@
+using System.Reflection;
+
+namespace SarahEngine;
+
+static class EmbeddedResources
+{
+ static readonly Assembly assembly = Assembly.GetExecutingAssembly();
+ static readonly string assemblyName = assembly.GetName().Name ?? "";
+
+ public static byte[] GetResource(string name)
+ {
+ using Stream EmbeddedFile = assembly.GetManifestResourceStream($"{assemblyName}.{name}")!;
+ using BinaryReader reader = new(EmbeddedFile);
+ return reader.ReadBytes((int)EmbeddedFile.Length);
+ }
+} \ No newline at end of file
diff --git a/Extensions.cs b/Extensions.cs
new file mode 100644
index 0000000..40eb72d
--- /dev/null
+++ b/Extensions.cs
@@ -0,0 +1,40 @@
+using System.Numerics;
+
+namespace SarahEngine;
+
+public static class MathExtensions
+{
+ public static Vector2 LimitLength(this Vector2 toLimit, float lengthLimit)
+ {
+ float length = toLimit.Length();
+ if (toLimit == Vector2.Zero) return Vector2.Zero;
+ return Vector2.Normalize(toLimit) * MathF.Min(length, lengthLimit);
+ }
+
+ public static Vector2 Round(this Vector2 toRound)
+ {
+ return new(MathF.Round(toRound.X), MathF.Round(toRound.Y));
+ }
+}
+
+public static class StringExtensions
+{
+ public static string LimitLength(this string value, int maxLength)
+ {
+ if (string.IsNullOrEmpty(value)) return value;
+ return value.Length <= maxLength ? value : value[..maxLength];
+ }
+}
+
+public static class Directions
+{
+ public static readonly Vector2 Up = new(0,-1);
+ public static readonly Vector2 Down = new(0,1);
+ public static readonly Vector2 Left = new(-1,0);
+ public static readonly Vector2 Right = new(1,0);
+
+ public static readonly Vector2 UpLeft = Vector2.Normalize(Up+Left);
+ public static readonly Vector2 UpRight = Vector2.Normalize(Up+Right);
+ public static readonly Vector2 DownLeft = Vector2.Normalize(Down+Left);
+ public static readonly Vector2 DownRight = Vector2.Normalize(Down+Right);
+} \ No newline at end of file