From 3d90ccaf54b7ae83dc0fbcb5a494c07683e465d5 Mon Sep 17 00:00:00 2001
From: bionic85 <144353436+bionic85@users.noreply.github.com>
Date: Mon, 6 Jan 2025 17:21:11 +0100
Subject: [PATCH] smoll update to UserDataManager class

---
 PuzzlePlayer/UserDataManager.cs | 67 ++++++++++++++++++++++-----------
 1 file changed, 44 insertions(+), 23 deletions(-)

diff --git a/PuzzlePlayer/UserDataManager.cs b/PuzzlePlayer/UserDataManager.cs
index 15c01a4..cefa354 100644
--- a/PuzzlePlayer/UserDataManager.cs
+++ b/PuzzlePlayer/UserDataManager.cs
@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Drawing;
 using System.IO;
+using System.Reflection;
 using PuzzlePlayer_Namespace.Properties;    // for accessing user settings
 
 namespace PuzzlePlayer_Namespace
@@ -39,6 +40,11 @@ namespace PuzzlePlayer_Namespace
                 return null;
             }
         }
+
+        public void Unlock()
+        {
+            unlocked = true;
+        }
     }
 
     // Class for managing and storing user data
@@ -49,37 +55,33 @@ namespace PuzzlePlayer_Namespace
         public Font MainFont { get { return GetMainFont(); } }
         public Theme Theme { get { return GetCurrentTheme(); } }
         
-        private List<(Theme,bool)> unlockedThemes;
+        private List<Theme> allThemes;
 
-        private const string themeFilePath = "Themes.txt";
+        private const string themeFilePath = "PuzzlePlayer_Namespace.Resources.Themes.txt";
 
         public UserDataManager()
         {
-            unlockedThemes = GetUnlockedThemes();
+            allThemes = GetThemes();
         }
 
-        private List<(Theme,bool)> GetUnlockedThemes()
+        private List<Theme> GetThemes()
         {
-            List<(Theme,bool)> result = new List<(Theme, bool)>();
-
-            if (!File.Exists(themeFilePath))
-                throw new Exception("blijf van de theme file af kkr djalla");
+            List<Theme> result = new List<Theme>();
 
-            using(StreamReader sr = new StreamReader(themeFilePath))
+            using(Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(themeFilePath))
             {
-                while (!sr.EndOfStream)
+                using(StreamReader sr = new StreamReader(stream))
                 {
-                    Theme? t = Theme.FromString(sr.ReadLine());
-                    if(t == null)
-                        throw new Exception("blijf van de theme file af kkr djalla, ga t zelf maar fixen");
-                    else
+                    while (!sr.EndOfStream)
                     {
-                        Theme theme = (Theme)t;
-                        result.Add((theme, theme.unlocked));
-                    }  
+                        Theme? t = Theme.FromString(sr.ReadLine());
+                        if (t == null)
+                            throw new Exception("blijf van de theme file af kkr djalla, ga t zelf maar fixen");
+                        else
+                            result.Add((Theme)t);
+                    }
                 }
             }
-
             return result;
         }
 
@@ -110,19 +112,19 @@ namespace PuzzlePlayer_Namespace
         private Theme GetCurrentTheme()
         {
             string currentThemeName = Settings.Default.ThemeName;
-            foreach ((Theme,bool) tb in unlockedThemes)
+            foreach (Theme t in allThemes)
             {
-                if (tb.Item2 && currentThemeName == tb.Item1.name)
-                    return tb.Item1;
+                if (t.unlocked && currentThemeName == t.name)
+                    return t;
             }
             throw new Exception("can't find the selected theme in unlocked themes");
         }
 
         public void SetCurrentTheme(string newThemeName)
         {
-            foreach ((Theme, bool) tb in unlockedThemes)
+            foreach (Theme t in allThemes)
             {
-                if(tb.Item2 && newThemeName == tb.Item1.name)
+                if(t.unlocked && newThemeName == t.name)
                 {
                     Settings.Default.ThemeName = newThemeName;
                     Settings.Default.Save();
@@ -130,5 +132,24 @@ namespace PuzzlePlayer_Namespace
                 }
             }
         }
+
+        public void UnlockTheme(string ThemeToUnlock)
+        {
+            // update the unlockedThemes list
+            for (int i = 0; i < allThemes.Count; i++)
+            {
+                if (allThemes[i].name == ThemeToUnlock)
+                {
+                    allThemes[i].Unlock();
+                    return;
+                }
+            }
+
+            // write the data back to the file
+            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(themeFilePath))
+                using (StreamWriter writer = new StreamWriter(stream))
+                    foreach(Theme t in allThemes)
+                        writer.WriteLine(t.ToString());
+        }
     }
 }
-- 
GitLab