From 4bdf5c89aea0cfbfc32c113261d83d2810ada700 Mon Sep 17 00:00:00 2001
From: bionic85 <144353436+bionic85@users.noreply.github.com>
Date: Mon, 6 Jan 2025 15:06:16 +0100
Subject: [PATCH] Simplified the UserDataManager using build in setting thingy
 from .net

not done yet tho
---
 PuzzlePlayer/App.config                      |  24 +++
 PuzzlePlayer/Properties/Settings.Designer.cs |  74 +++++++++
 PuzzlePlayer/Properties/Settings.settings    |  18 +++
 PuzzlePlayer/SettingForm.cs                  |   3 -
 PuzzlePlayer/UserDataManager.cs              | 160 ++++---------------
 5 files changed, 151 insertions(+), 128 deletions(-)
 create mode 100644 PuzzlePlayer/App.config
 create mode 100644 PuzzlePlayer/Properties/Settings.Designer.cs
 create mode 100644 PuzzlePlayer/Properties/Settings.settings

diff --git a/PuzzlePlayer/App.config b/PuzzlePlayer/App.config
new file mode 100644
index 0000000..8a5320f
--- /dev/null
+++ b/PuzzlePlayer/App.config
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+    <configSections>
+        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
+            <section name="PuzzlePlayer_Namespace.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
+        </sectionGroup>
+    </configSections>
+    <userSettings>
+        <PuzzlePlayer_Namespace.Properties.Settings>
+            <setting name="ThemeName" serializeAs="String">
+                <value>Dark</value>
+            </setting>
+            <setting name="FontName" serializeAs="String">
+                <value>Gotham</value>
+            </setting>
+            <setting name="FontSize" serializeAs="String">
+                <value>16</value>
+            </setting>
+            <setting name="Money" serializeAs="String">
+                <value>100</value>
+            </setting>
+        </PuzzlePlayer_Namespace.Properties.Settings>
+    </userSettings>
+</configuration>
\ No newline at end of file
diff --git a/PuzzlePlayer/Properties/Settings.Designer.cs b/PuzzlePlayer/Properties/Settings.Designer.cs
new file mode 100644
index 0000000..b26524e
--- /dev/null
+++ b/PuzzlePlayer/Properties/Settings.Designer.cs
@@ -0,0 +1,74 @@
+//------------------------------------------------------------------------------
+// <auto-generated>
+//     This code was generated by a tool.
+//     Runtime Version:4.0.30319.42000
+//
+//     Changes to this file may cause incorrect behavior and will be lost if
+//     the code is regenerated.
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+namespace PuzzlePlayer_Namespace.Properties {
+    
+    
+    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.11.0.0")]
+    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
+        
+        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
+        
+        public static Settings Default {
+            get {
+                return defaultInstance;
+            }
+        }
+        
+        [global::System.Configuration.UserScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [global::System.Configuration.DefaultSettingValueAttribute("Dark")]
+        public string ThemeName {
+            get {
+                return ((string)(this["ThemeName"]));
+            }
+            set {
+                this["ThemeName"] = value;
+            }
+        }
+        
+        [global::System.Configuration.UserScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [global::System.Configuration.DefaultSettingValueAttribute("Gotham")]
+        public string FontName {
+            get {
+                return ((string)(this["FontName"]));
+            }
+            set {
+                this["FontName"] = value;
+            }
+        }
+        
+        [global::System.Configuration.UserScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [global::System.Configuration.DefaultSettingValueAttribute("16")]
+        public int FontSize {
+            get {
+                return ((int)(this["FontSize"]));
+            }
+            set {
+                this["FontSize"] = value;
+            }
+        }
+        
+        [global::System.Configuration.UserScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [global::System.Configuration.DefaultSettingValueAttribute("100")]
+        public int Money {
+            get {
+                return ((int)(this["Money"]));
+            }
+            set {
+                this["Money"] = value;
+            }
+        }
+    }
+}
diff --git a/PuzzlePlayer/Properties/Settings.settings b/PuzzlePlayer/Properties/Settings.settings
new file mode 100644
index 0000000..62db88e
--- /dev/null
+++ b/PuzzlePlayer/Properties/Settings.settings
@@ -0,0 +1,18 @@
+<?xml version='1.0' encoding='utf-8'?>
+<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="PuzzlePlayer_Namespace.Properties" GeneratedClassName="Settings">
+  <Profiles />
+  <Settings>
+    <Setting Name="ThemeName" Type="System.String" Scope="User">
+      <Value Profile="(Default)">Dark</Value>
+    </Setting>
+    <Setting Name="FontName" Type="System.String" Scope="User">
+      <Value Profile="(Default)">Gotham</Value>
+    </Setting>
+    <Setting Name="FontSize" Type="System.Int32" Scope="User">
+      <Value Profile="(Default)">16</Value>
+    </Setting>
+    <Setting Name="Money" Type="System.Int32" Scope="User">
+      <Value Profile="(Default)">100</Value>
+    </Setting>
+  </Settings>
+</SettingsFile>
\ No newline at end of file
diff --git a/PuzzlePlayer/SettingForm.cs b/PuzzlePlayer/SettingForm.cs
index f51a68f..06f4075 100644
--- a/PuzzlePlayer/SettingForm.cs
+++ b/PuzzlePlayer/SettingForm.cs
@@ -26,7 +26,6 @@ namespace PuzzlePlayer_Namespace
 
         // For storing user settings
         private UserData settings;
-        private static string filePath = "Settings.txt";
 
         public SettingForm()
         {
@@ -42,8 +41,6 @@ namespace PuzzlePlayer_Namespace
             Invalidate();
         }
 
-        
-
         private void SetUpUI()
         {
             this.ClientSize = new Size(1115, 755);
diff --git a/PuzzlePlayer/UserDataManager.cs b/PuzzlePlayer/UserDataManager.cs
index 28494c8..0e24aac 100644
--- a/PuzzlePlayer/UserDataManager.cs
+++ b/PuzzlePlayer/UserDataManager.cs
@@ -2,10 +2,12 @@
 using System.Collections.Generic;
 using System.Drawing;
 using System.IO;
+using PuzzlePlayer_Namespace.Properties;    // for accessing user settings
 
 namespace PuzzlePlayer_Namespace
 {
     // struct for saving settings
+    // saves the current selected theme and the font name and size
     internal struct UserData(string themeName, string fName, int fSize)
     {
         public const string prefix = "SETTINGS";
@@ -76,155 +78,63 @@ namespace PuzzlePlayer_Namespace
     // Class for managing and storing user data
     internal class UserDataManager
     {
-        private int money;
-        public int Money { get { return money; } set { UpdateMoney(value); } }
-
-        private UserData data;
+        // properties for getting various settings
+        public int Money { get { return Settings.Default.Money; } set { SetMoney(value); } }
+        public Font MainFont { get { return GetMainFont(); } }
+        public Theme Theme { get { return GetCurrentTheme(); } }
+        public UserData Data { get {  return GetUserSettings(); } set { SetUserData(value); } }
+        
         private List<(Theme,bool)> unlockedThemes = new List<(Theme, bool)>();
 
-        private static string filePath = "Data.txt";
+        private const string dataFilePath = "Data.txt";
+        private const string themeFilePath = "Themes.txt";
         private const string moneyPrefix = "DO_NOT_TOUCH_THE_MONEY_CHEATER";
 
-        public UserDataManager()
-        {
-            money = GetMoney();
-            data = GetUserSettings();
-        }
-
-        // reads the file and returns a array of strings of each line
-        private string[] GetDataFileContents()
+        private void SetMoney(int newValue)
         {
-            if (!File.Exists(filePath)) // if the file doesn't exists then a new one is created with default settings
-                MakeNewDataFile();
-
-            List<String> result = new List<string>();
-            using (StreamReader reader = new StreamReader(System.IO.File.OpenRead(filePath)))
-            {
-                string s = "";
-                while ((s = reader.ReadLine()) != null)
-                {
-                    result.Add(s);
-                }
-            }
-
-            if (result.Count > 0)
-                return result.ToArray();
-            else
-                MakeNewDataFile();          // is dit clean? of nah?
+            if (newValue < 0)
+                return;
 
-            return GetDataFileContents();   // is dit clean? of nah?
+            Settings.Default.Money = newValue;
+            Settings.Default.Save();
         }
 
-        // writes to the file
-        private void WriteDataFileContent(string[] toWrite)
+        private UserData GetUserSettings()
         {
-            if (!File.Exists(filePath)) // if the file doesn't exists then a new one is created with default settings
-                MakeNewDataFile();
+            string themeName = Settings.Default.ThemeName;
+            string fontName = Settings.Default.FontName;
+            int fontSize = Settings.Default.FontSize;
 
-            using (StreamWriter sw = new StreamWriter(filePath))
-            {
-                foreach(string s in toWrite)
-                    sw.WriteLine(s);
-            }
+            return new UserData(themeName, fontName, fontSize);
         }
 
-        private int GetMoney()
+        private void SetUserData(UserData newUserData)
         {
-            string[] dataFile = GetDataFileContents();
-
-            foreach (string s in dataFile)
-                if(s.StartsWith(moneyPrefix))
-                {
-                    string[] splitted = s.Split('|');
-                    try
-                    {
-                        return int.Parse(splitted[1]);
-                    }
-                    catch (Exception)
-                    {
-                        MakeNewDataFile();
-                        return 100; // default money if the file is broken
-                    }
-                    
-                }
-
-            MakeNewDataFile();
-            return 100; // default money if the file is broken
+            Settings.Default.FontName = newUserData.fontName;
+            Settings.Default.FontSize = newUserData.fontSize;
+            Settings.Default.ThemeName = newUserData.currentThemeName;
+            Settings.Default.Save();
         }
 
-        private void UpdateMoney(int newValue)
+        private Font GetMainFont()
         {
-            if(newValue < 0)
-                return;
-
-            money = newValue;
+            string fontName = Settings.Default.FontName;
+            int fontSize = Settings.Default.FontSize;
 
-            // write the new value to the data file
-            // first read the old file and replace the line containing the information for the money with the new value
-            string[] dataFile = GetDataFileContents();
-            string newString = $"{moneyPrefix}|{newValue}";
-            for (int i = 0; i < dataFile.Length; i++)
-            {
-                if (dataFile[i].StartsWith(moneyPrefix))
-                {
-                    dataFile[i] = newString;
-                    break;
-                }
-            }
-
-            // after modifying write it back
-            WriteDataFileContent(dataFile);
+            return new Font(fontName, fontSize);
         }
 
-        private UserData GetUserSettings()
+        public void SetMainFont(string fontName, int fontSize)
         {
-            string[] dataFile = GetDataFileContents();
-            
-            UserData? newSettings = null;
-            foreach (string s in dataFile)
-                if (s.StartsWith("SETTINGS"))
-                {
-                    newSettings = UserData.FromString(s);
-                    break;
-                }
-
-            if (newSettings != null)
-                return (UserData)newSettings;
-            else
-                MakeNewDataFile();          // if the settings could not be read then a new file must be made
-
-            return GetUserSettings();           // after the new file is made it is save to call getUserSettings again
+            Settings.Default.FontName = fontName;
+            Settings.Default.FontSize = fontSize;
+            Settings.Default.Save();
         }
 
-        private void UpdateUserSettings(UserData newUserData)
+        private Theme GetCurrentTheme()
         {
-            data = newUserData;
-
-            // write the new value to the data file
-            // first read the old file and replace the line containing the information for the money with the new value
-            string[] dataFile = GetDataFileContents();
-
-            for (int i = 0; i < dataFile.Length; i++)
-            {
-                if (dataFile[i].StartsWith(UserData.prefix))
-                {
-                    dataFile[i] = newUserData.ToString();
-                    break;
-                }
-            }
-
-            // after modifying write it back
-            WriteDataFileContent(dataFile);
+            
         }
 
-        // if there is no Settings.txt or it is damaged/modified/unreadable then a new one is created or overwritten
-        private void MakeNewDataFile()
-        {
-            using (StreamWriter writer = File.CreateText(filePath))
-            {
-                writer.WriteLine(SettingForm.defaultSettings.ToString());   // get the default settings from SettingsForm
-                writer.WriteLine($"{moneyPrefix}|100");                       // default money is 100
-            }
-        }
     }
 }
-- 
GitLab