diff --git a/PuzzlePlayer/UserDataManager.cs b/PuzzlePlayer/UserDataManager.cs index 15c01a4bde5026e461e4f74be416c58fafb0cd1c..cefa35482a4f497c56b4d735851e0d91b231538d 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()); + } } }