Beispiel #1
0
        public static ThemeDescription InstallFromPackedFile(string path)
        {
            logger.Info($"Installing theme extenstion {path}");
            var desc = GetDescriptionFromPackedFile(path);

            if (desc == null)
            {
                throw new FileNotFoundException("Theme manifest not found.");
            }

            var installDir = Paths.GetSafeFilename(desc.Name).Replace(" ", string.Empty) + "_" + (desc.Name + desc.Author).MD5();
            var targetDir  = PlayniteSettings.IsPortable ? PlaynitePaths.ThemesProgramPath : PlaynitePaths.ThemesUserDataPath;

            targetDir = Path.Combine(targetDir, desc.Mode.GetDescription(), installDir);
            var oldBackPath = targetDir + "_old";

            if (Directory.Exists(targetDir))
            {
                logger.Debug($"Replacing existing theme installation: {targetDir}.");
                Directory.Move(targetDir, oldBackPath);
            }

            FileSystem.CreateDirectory(targetDir, true);
            ZipFile.ExtractToDirectory(path, targetDir);

            if (Directory.Exists(oldBackPath))
            {
                Directory.Delete(oldBackPath, true);
            }

            return(ThemeDescription.FromFile(Path.Combine(targetDir, PlaynitePaths.ThemeManifestFileName)));
        }
Beispiel #2
0
        public static void ApplyTheme(Application app, ThemeDescription theme, ApplicationMode mode)
        {
            var allLoaded   = true;
            var loadedXamls = new List <ResourceDictionary>();
            var xamlFiles   = Directory.GetFiles(theme.DirectoryPath, "*.xaml", SearchOption.AllDirectories);

            foreach (var xamlFile in xamlFiles)
            {
                try
                {
                    var xaml = Xaml.FromFile(xamlFile);
                    if (xaml is ResourceDictionary xamlDir)
                    {
                        xamlDir.Source = new Uri(xamlFile, UriKind.Absolute);
                        loadedXamls.Add(xamlDir as ResourceDictionary);
                    }
                    else
                    {
                        logger.Error($"Skipping theme file {xamlFile}, it's not resource dictionary.");
                    }
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, $"Failed to load xaml {xamlFiles}");
                    allLoaded = false;
                    break;
                }
            }

            if (allLoaded)
            {
                loadedXamls.ForEach(a => app.Resources.MergedDictionaries.Add(a));
            }
        }
Beispiel #3
0
        public static bool ApplyTheme(Application app, ThemeDescription theme, ApplicationMode mode)
        {
            if ((new System.Version(theme.ThemeApiVersion).Major != ThemeApiVersion.Major))
            {
                logger.Error($"Failed to apply {theme.Name} theme, unsupported API version {theme.ThemeApiVersion}.");
                return(false);
            }

            var allLoaded       = true;
            var loadedXamls     = new List <ResourceDictionary>();
            var acceptableXamls = new List <string>();
            var defaultRoot     = $"Themes/{mode.GetDescription()}/{DefaultTheme.DirectoryName}/";

            foreach (var dict in app.Resources.MergedDictionaries)
            {
                if (dict.Source.OriginalString.StartsWith("Themes") && dict.Source.OriginalString.EndsWith("xaml"))
                {
                    acceptableXamls.Add(dict.Source.OriginalString.Replace(defaultRoot, "").Replace('/', '\\'));
                }
            }

            foreach (var accXaml in acceptableXamls)
            {
                var xamlPath = Path.Combine(theme.DirectoryPath, accXaml);
                if (!File.Exists(xamlPath))
                {
                    continue;
                }

                try
                {
                    var xaml = Xaml.FromFile(xamlPath);
                    if (xaml is ResourceDictionary xamlDir)
                    {
                        xamlDir.Source = new Uri(xamlPath, UriKind.Absolute);
                        loadedXamls.Add(xamlDir as ResourceDictionary);
                    }
                    else
                    {
                        logger.Error($"Skipping theme file {xamlPath}, it's not resource dictionary.");
                    }
                }
                catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                {
                    logger.Error(e, $"Failed to load xaml {xamlPath}");
                    allLoaded = false;
                    break;
                }
            }

            if (allLoaded)
            {
                loadedXamls.ForEach(a => app.Resources.MergedDictionaries.Add(a));
                return(true);
            }

            return(false);
        }
Beispiel #4
0
        public static List <ThemeDescription> GetAvailableThemes(ApplicationMode mode)
        {
            var modeDir = GetThemeRootDir(mode);
            var added   = new List <string>();
            var themes  = new List <ThemeDescription>();

            var userPath = Path.Combine(PlaynitePaths.ThemesUserDataPath, modeDir);

            if (!PlayniteSettings.IsPortable && Directory.Exists(userPath))
            {
                foreach (var dir in Directory.GetDirectories(userPath))
                {
                    try
                    {
                        var descriptorPath = Path.Combine(dir, PlaynitePaths.ThemeManifestFileName);
                        if (File.Exists(descriptorPath))
                        {
                            var info = new FileInfo(descriptorPath);
                            added.Add(info.Directory.Name);
                            themes.Add(ThemeDescription.FromFile(descriptorPath));
                        }
                    }
                    catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        logger.Error(e, $"Failed to load theme info {dir}");
                    }
                }
            }

            var programPath = Path.Combine(PlaynitePaths.ThemesProgramPath, modeDir);

            if (Directory.Exists(programPath))
            {
                foreach (var dir in Directory.GetDirectories(programPath))
                {
                    try
                    {
                        var descriptorPath = Path.Combine(dir, PlaynitePaths.ThemeManifestFileName);
                        if (File.Exists(descriptorPath))
                        {
                            var info = new FileInfo(descriptorPath);
                            if (!added.Contains(info.Directory.Name))
                            {
                                themes.Add(ThemeDescription.FromFile(descriptorPath));
                            }
                        }
                    }
                    catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        logger.Error(e, $"Failed to load theme info {dir}");
                    }
                }
            }

            return(themes);
        }
Beispiel #5
0
        public static List <ThemeDescription> GetAvailableThemes(ApplicationMode mode)
        {
            var modeDir = GetThemeRootDir(mode);
            var added   = new List <string>();
            var themes  = new List <ThemeDescription>();

            var userPath = Path.Combine(PlaynitePaths.ThemesUserDataPath, modeDir);

            if (!PlayniteSettings.IsPortable && Directory.Exists(userPath))
            {
                foreach (var dir in Directory.GetDirectories(userPath))
                {
                    var descriptorPath = Path.Combine(dir, ThemeManifestFileName);
                    if (File.Exists(descriptorPath))
                    {
                        var info = new FileInfo(descriptorPath);
                        added.Add(info.Directory.Name);
                        themes.Add(ThemeDescription.FromFile(descriptorPath));
                    }
                }
            }

            var programPath = Path.Combine(PlaynitePaths.ThemesProgramPath, modeDir);

            if (Directory.Exists(programPath))
            {
                foreach (var dir in Directory.GetDirectories(programPath))
                {
                    var descriptorPath = Path.Combine(dir, ThemeManifestFileName);
                    if (File.Exists(descriptorPath))
                    {
                        var info = new FileInfo(descriptorPath);
                        if (!added.Contains(info.Directory.Name))
                        {
                            themes.Add(ThemeDescription.FromFile(descriptorPath));
                        }
                    }
                }
            }

            return(themes);
        }
Beispiel #6
0
 public static void SetDefaultTheme(ThemeDescription theme)
 {
     DefaultTheme = theme;
 }
Beispiel #7
0
 public static void SetCurrentTheme(ThemeDescription theme)
 {
     CurrentTheme = theme;
 }