couple more optimisations and readability changes

This commit is contained in:
Jack Baron 2020-02-23 15:23:04 +00:00
parent f46fd8c92a
commit 757b38c186
No known key found for this signature in database
GPG key ID: CD10BCEEC646C064

View file

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
@ -19,14 +19,14 @@ namespace ModAssistant
public static List<string> LoadedThemes { get => loadedThemes.Keys.ToList(); } public static List<string> LoadedThemes { get => loadedThemes.Keys.ToList(); }
public static string ThemeDirectory => $"{Environment.CurrentDirectory}/Themes"; public static string ThemeDirectory => $"{Environment.CurrentDirectory}/Themes";
/// <summary> /// <summary>
/// Local dictionary of Resource Dictionaries mapped by their names. /// Local dictionary of Resource Dictionaries mapped by their names.
/// </summary> /// </summary>
private static Dictionary<string, Theme> loadedThemes = new Dictionary<string, Theme>(); private static Dictionary<string, Theme> loadedThemes = new Dictionary<string, Theme>();
private static List<string> preInstalledThemes = new List<string> { "Light", "Dark", "Light Pink" }; private static List<string> preInstalledThemes = new List<string> { "Light", "Dark", "Light Pink" };
/// <summary> /// <summary>
/// Index of "LoadedTheme" in App.xaml /// Index of "LoadedTheme" in App.xaml
/// </summary> /// </summary>
private static readonly int LOADED_THEME_INDEX = 4; private static readonly int LOADED_THEME_INDEX = 4;
@ -48,8 +48,10 @@ namespace ModAssistant
{ {
string location = $"Themes/{localTheme}.xaml"; string location = $"Themes/{localTheme}.xaml";
Uri local = new Uri(location, UriKind.Relative); Uri local = new Uri(location, UriKind.Relative);
ResourceDictionary localDictionary = new ResourceDictionary(); ResourceDictionary localDictionary = new ResourceDictionary();
localDictionary.Source = local; localDictionary.Source = local;
Theme theme = new Theme(localTheme, localDictionary); Theme theme = new Theme(localTheme, localDictionary);
loadedThemes.Add(localTheme, theme); loadedThemes.Add(localTheme, theme);
} }
@ -66,6 +68,7 @@ namespace ModAssistant
{ {
Theme theme = LoadZipTheme(ThemeDirectory, name, ".mat"); Theme theme = LoadZipTheme(ThemeDirectory, name, ".mat");
if (theme is null) continue; if (theme is null) continue;
AddOrModifyTheme(name, theme); AddOrModifyTheme(name, theme);
} }
} }
@ -75,6 +78,7 @@ namespace ModAssistant
{ {
string name = directory.Split('\\').Last(); string name = directory.Split('\\').Last();
Theme theme = LoadTheme(directory, name); Theme theme = LoadTheme(directory, name);
if (theme is null) continue; if (theme is null) continue;
AddOrModifyTheme(name, theme); AddOrModifyTheme(name, theme);
} }
@ -99,6 +103,7 @@ namespace ModAssistant
Themes.ApplyWindowsTheme(); Themes.ApplyWindowsTheme();
return; return;
} }
try try
{ {
Themes.ApplyTheme(savedTheme, false); Themes.ApplyTheme(savedTheme, false);
@ -122,18 +127,22 @@ namespace ModAssistant
LoadedTheme = theme; LoadedTheme = theme;
MainWindow.Instance.BackgroundVideo.Pause(); MainWindow.Instance.BackgroundVideo.Pause();
MainWindow.Instance.BackgroundVideo.Visibility = Visibility.Hidden; MainWindow.Instance.BackgroundVideo.Visibility = Visibility.Hidden;
if (newTheme.ThemeDictionary != null) if (newTheme.ThemeDictionary != null)
{ {
// TODO: Search by name // TODO: Search by name
Application.Current.Resources.MergedDictionaries.RemoveAt(LOADED_THEME_INDEX); Application.Current.Resources.MergedDictionaries.RemoveAt(LOADED_THEME_INDEX);
Application.Current.Resources.MergedDictionaries.Insert(LOADED_THEME_INDEX, newTheme.ThemeDictionary); Application.Current.Resources.MergedDictionaries.Insert(LOADED_THEME_INDEX, newTheme.ThemeDictionary);
} }
Properties.Settings.Default.SelectedTheme = theme; Properties.Settings.Default.SelectedTheme = theme;
Properties.Settings.Default.Save(); Properties.Settings.Default.Save();
if (sendMessage) if (sendMessage)
{ {
MainWindow.Instance.MainText = string.Format((string)Application.Current.FindResource("Themes:ThemeSet"), theme); MainWindow.Instance.MainText = string.Format((string)Application.Current.FindResource("Themes:ThemeSet"), theme);
} }
ApplyWaifus(); ApplyWaifus();
if (File.Exists(newTheme.VideoLocation)) if (File.Exists(newTheme.VideoLocation))
@ -147,8 +156,10 @@ namespace ModAssistant
MainWindow.Instance.BackgroundVideo.Stop(); MainWindow.Instance.BackgroundVideo.Stop();
MainWindow.Instance.BackgroundVideo.Source = videoUri; MainWindow.Instance.BackgroundVideo.Source = videoUri;
} }
MainWindow.Instance.BackgroundVideo.Play(); MainWindow.Instance.BackgroundVideo.Play();
} }
ReloadIcons(); ReloadIcons();
} }
else else
@ -163,16 +174,10 @@ namespace ModAssistant
/// <param name="themeName">Name of local theme.</param> /// <param name="themeName">Name of local theme.</param>
public static void WriteThemeToDisk(string themeName) public static void WriteThemeToDisk(string themeName)
{ {
if (!Directory.Exists(ThemeDirectory)) Directory.CreateDirectory(ThemeDirectory);
{ Directory.CreateDirectory($"{ThemeDirectory}\\{themeName}");
Directory.CreateDirectory(ThemeDirectory);
}
if (!Directory.Exists($"{ThemeDirectory}\\{themeName}"))
{
Directory.CreateDirectory($"{ThemeDirectory}\\{themeName}");
}
if (!File.Exists($@"{ThemeDirectory}\\{themeName}.xaml")) if (File.Exists($@"{ThemeDirectory}\\{themeName}.xaml") == false)
{ {
/* /*
* Any theme that you want to write must be set as an Embedded Resource instead of the default Page. * Any theme that you want to write must be set as an Embedded Resource instead of the default Page.
@ -215,6 +220,7 @@ namespace ModAssistant
return; return;
} }
} }
ApplyTheme("Light", false); ApplyTheme("Light", false);
} }
} }
@ -229,6 +235,7 @@ namespace ModAssistant
{ {
Theme theme = new Theme(name, null); Theme theme = new Theme(name, null);
theme.Waifus = new Waifus(); theme.Waifus = new Waifus();
foreach (string file in Directory.EnumerateFiles(directory).OrderBy(x => x)) foreach (string file in Directory.EnumerateFiles(directory).OrderBy(x => x))
{ {
FileInfo info = new FileInfo(file); FileInfo info = new FileInfo(file);
@ -253,6 +260,7 @@ namespace ModAssistant
dictionary.Source = resourceSource; dictionary.Source = resourceSource;
theme.ThemeDictionary = dictionary; theme.ThemeDictionary = dictionary;
} }
if (supportedVideoExtensions.Contains(info.Extension)) if (supportedVideoExtensions.Contains(info.Extension))
{ {
if (info.Name != $"_{name}{info.Extension}" || theme.VideoLocation is null) if (info.Name != $"_{name}{info.Extension}" || theme.VideoLocation is null)
@ -261,6 +269,7 @@ namespace ModAssistant
} }
} }
} }
return theme; return theme;
} }
@ -277,20 +286,26 @@ namespace ModAssistant
{ {
loadedThemes[name].ThemeDictionary = theme.ThemeDictionary; loadedThemes[name].ThemeDictionary = theme.ThemeDictionary;
} }
if (theme.Waifus?.Background != null) if (theme.Waifus?.Background != null)
{ {
loadedThemes[name].Waifus.Background = theme.Waifus.Background; loadedThemes[name].Waifus.Background = theme.Waifus.Background;
} }
if (theme.Waifus?.Sidebar != null) if (theme.Waifus?.Sidebar != null)
{ {
loadedThemes[name].Waifus.Sidebar = theme.Waifus.Sidebar; loadedThemes[name].Waifus.Sidebar = theme.Waifus.Sidebar;
} }
if (!string.IsNullOrEmpty(theme.VideoLocation)) if (!string.IsNullOrEmpty(theme.VideoLocation))
{ {
loadedThemes[name].VideoLocation = theme.VideoLocation; loadedThemes[name].VideoLocation = theme.VideoLocation;
} }
} }
else loadedThemes.Add(name, theme); else
{
loadedThemes.Add(name, theme);
}
} }
/// <summary> /// <summary>
@ -303,12 +318,13 @@ namespace ModAssistant
{ {
Waifus waifus = new Waifus(); Waifus waifus = new Waifus();
ResourceDictionary dictionary = null; ResourceDictionary dictionary = null;
using (FileStream stream = new FileStream(Path.Combine(directory, name + extension), FileMode.Open)) using (FileStream stream = new FileStream(Path.Combine(directory, name + extension), FileMode.Open))
using (ZipArchive archive = new ZipArchive(stream)) using (ZipArchive archive = new ZipArchive(stream))
{ {
foreach (ZipArchiveEntry file in archive.Entries) foreach (ZipArchiveEntry file in archive.Entries)
{ {
bool isPng = file.Name.EndsWith(".png", StringComparison.OrdinalIgnoreCase); bool isPng = file.Name.EndsWith(".png", StringComparison.OrdinalIgnoreCase);
bool isSidePng = file.Name.EndsWith(".side.png", StringComparison.OrdinalIgnoreCase); bool isSidePng = file.Name.EndsWith(".side.png", StringComparison.OrdinalIgnoreCase);
bool isXaml = file.Name.EndsWith(".xaml", StringComparison.OrdinalIgnoreCase); bool isXaml = file.Name.EndsWith(".xaml", StringComparison.OrdinalIgnoreCase);
@ -321,15 +337,14 @@ namespace ModAssistant
{ {
waifus.Sidebar = GetImageFromStream(Utils.StreamToArray(file.Open())); waifus.Sidebar = GetImageFromStream(Utils.StreamToArray(file.Open()));
} }
string videoExtension = $".{file.Name.Split('.').Last()}"; string videoExtension = $".{file.Name.Split('.').Last()}";
if (supportedVideoExtensions.Contains(videoExtension)) if (supportedVideoExtensions.Contains(videoExtension))
{ {
string videoName = $"{ThemeDirectory}\\{name}\\_{name}{videoExtension}"; string videoName = $"{ThemeDirectory}\\{name}\\_{name}{videoExtension}";
if (!Directory.Exists($"{ThemeDirectory}\\{name}")) Directory.CreateDirectory($"{ThemeDirectory}\\{name}");
{
Directory.CreateDirectory($"{ThemeDirectory}\\{name}"); if (File.Exists(videoName) == false)
}
if (!File.Exists(videoName))
{ {
file.ExtractToFile(videoName, false); file.ExtractToFile(videoName, false);
} }
@ -350,13 +365,13 @@ namespace ModAssistant
if (isXaml && loadedThemes.ContainsKey(name) == false) if (isXaml && loadedThemes.ContainsKey(name) == false)
{ {
try try
{ {
dictionary = (ResourceDictionary)XamlReader.Load(file.Open()); dictionary = (ResourceDictionary)XamlReader.Load(file.Open());
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show($"Could not load {name}.\n\n{ex.Message}\n\nIgnoring..."); MessageBox.Show($"Could not load {name}.\n\n{ex.Message}\n\nIgnoring...");
} }
} }
} }
@ -364,6 +379,7 @@ namespace ModAssistant
Theme theme = new Theme(name, dictionary); Theme theme = new Theme(name, dictionary);
theme.Waifus = waifus; theme.Waifus = waifus;
return theme; return theme;
} }
@ -381,8 +397,8 @@ namespace ModAssistant
image.CacheOption = BitmapCacheOption.OnLoad; image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = mStream; image.StreamSource = mStream;
image.EndInit(); image.EndInit();
if (image.CanFreeze) if (image.CanFreeze) image.Freeze();
image.Freeze();
return image; return image;
} }
} }
@ -393,6 +409,7 @@ namespace ModAssistant
private static void ApplyWaifus() private static void ApplyWaifus()
{ {
Waifus waifus = loadedThemes[LoadedTheme].Waifus; Waifus waifus = loadedThemes[LoadedTheme].Waifus;
if (waifus?.Background is null) if (waifus?.Background is null)
{ {
MainWindow.Instance.BackgroundImage.Opacity = 0; MainWindow.Instance.BackgroundImage.Opacity = 0;