Exemple #1
0
 public static void AreEqual(ThemeDescription expected, ThemeDescription actual)
 {
     Assert.AreEqual(expected.Theme, actual.Theme);
     Assert.AreEqual(expected.Stage, actual.Stage);
     Assert.AreEqual(expected.Curriculum, actual.Curriculum);
     AdvAssert.AreEqual(expected.Timelines, actual.Timelines);
 }
Exemple #2
0
        public static string GenerateNewTheme(ApplicationMode mode, string themeName)
        {
            var themeDirName    = Common.Paths.GetSafeFilename(themeName).Replace(" ", string.Empty);
            var defaultThemeDir = Path.Combine(Paths.GetThemesPath(mode), "Default");
            var outDir          = Path.Combine(PlaynitePaths.ThemesProgramPath, mode.GetDescription(), themeDirName);

            if (Directory.Exists(outDir))
            {
                throw new Exception($"Theme directory \"{outDir}\" already exists.");
            }

            FileSystem.CreateDirectory(outDir);
            var defaultThemeXamlFiles = GenerateCommonThemeFiles(mode, outDir);

            CopyThemeDirectory(defaultThemeDir, outDir, defaultThemeXamlFiles.Select(a => Path.Combine(defaultThemeDir, a)).ToList());

            var themeDesc = new ThemeDescription()
            {
                Author          = "Your Name Here",
                Name            = themeName,
                Version         = "1.0",
                Mode            = mode,
                ThemeApiVersion = ThemeManager.GetApiVersion(mode).ToString()
            };

            File.WriteAllText(Path.Combine(outDir, PlaynitePaths.ThemeManifestFileName), Serialization.ToYaml(themeDesc));
            Explorer.NavigateToFileSystemEntry(Path.Combine(outDir, Themes.ThemeSlnName));
            return(outDir);
        }
Exemple #3
0
 public static void AreEqual(IEnumerable <ThemeDescription> expected, IEnumerable <ThemeDescription> actual)
 {
     Assert.AreEqual(expected.ToList().Count, actual.ToList().Count);
     foreach (ThemeDescription exp in expected)
     {
         ThemeDescription act = actual.SingleOrDefault(item => item.Theme == exp.Theme);
         if (act != null)
         {
             AreEqual(exp, act);
         }
         else
         {
             Assert.Fail("Expected theme description with theme={0} doesn't exists in actual collection", exp.Theme);
         }
     }
 }
        public static ThemeDescription InstallExtensionQueue()
        {
            var anyFailed = false;
            ThemeDescription installedTheme = null;

            if (!File.Exists(PlaynitePaths.ExtensionQueueFilePath))
            {
                return(null);
            }

            var queue = Serialization.FromJsonFile <List <ExtensionInstallQueueItem> >(PlaynitePaths.ExtensionQueueFilePath);

            foreach (var queueItem in queue)
            {
                if (queueItem.Path.EndsWith(ThemeManager.PackedThemeFileExtention, StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        installedTheme = ThemeManager.InstallFromPackedFile(queueItem.Path);
                    }
                    catch (Exception e) when(!PlayniteEnvironment.ThrowAllErrors)
                    {
                        anyFailed = true;
                        logger.Error(e, $"Failed to install theme {queueItem}");
                    }
                }
                else
                {
                    logger.Warn($"Uknown extension file format {queueItem}");
                }
            }

            File.Delete(PlaynitePaths.ExtensionQueueFilePath);

            if (anyFailed)
            {
                throw new Exception("Failed to install one or more extensions.");
            }

            return(installedTheme);
        }
Exemple #5
0
        public static string GetFilePath(string relPath, ThemeDescription defaultTheme, ThemeDescription currentTheme)
        {
            var relativePath = Paths.FixSeparators(relPath).TrimStart(new char[] { Path.DirectorySeparatorChar });

            if (currentTheme != null)
            {
                var themePath = Path.Combine(currentTheme.DirectoryPath, relativePath);
                if (File.Exists(themePath))
                {
                    return(themePath);
                }
            }

            if (defaultTheme != null)
            {
                var defaultPath = Path.Combine(defaultTheme.DirectoryPath, relativePath);
                if (File.Exists(defaultPath))
                {
                    return(defaultPath);
                }
            }

            return(null);
        }
Exemple #6
0
        public static void UpdateTheme(string themeDirectory, ApplicationMode mode)
        {
            var themeManifestPath   = Path.Combine(themeDirectory, "theme.yaml");
            var currentThemeMan     = ThemeDescription.FromFile(themeManifestPath);
            var origThemeApiVersion = new Version(currentThemeMan.ThemeApiVersion);

            if (!File.Exists(Path.Combine(themeDirectory, Themes.ThemeProjName)))
            {
                throw new Exception("Cannot update theme that was not generated via Toolbox utility.");
            }

            if (ThemeManager.GetApiVersion(mode) == origThemeApiVersion)
            {
                logger.Warn("Theme is already updated to current API version.");
                return;
            }

            var folder = Paths.GetNextBackupFolder(themeDirectory);

            BackupTheme(themeDirectory, Paths.GetNextBackupFolder(themeDirectory));
            logger.Info($"Current theme backed up into \"{Path.GetFileName(folder)}\" folder.");

            var defaultThemeDir = Path.Combine(Paths.GetThemesPath(mode), "Default");
            var origFilesZip    = Path.Combine(Paths.ChangeLogsDir, currentThemeMan.ThemeApiVersion + ".zip");
            var themeChanges    = Themes.GetThemeChangelog(origThemeApiVersion, mode);

            if (!themeChanges.HasItems())
            {
                logger.Info("No files to update.");
                return;
            }

            // Update files
            var notUpdated = new List <string>();

            using (var origFiles = ZipFile.OpenRead(origFilesZip))
            {
                foreach (var changedFile in themeChanges)
                {
                    var subpath      = Common.Paths.FixSeparators(Regex.Replace(changedFile.Path, ".+Themes/(Desktop|Fullscreen)/Default/", ""));
                    var curThemePath = Path.Combine(themeDirectory, subpath);
                    var defaultPath  = Path.Combine(defaultThemeDir, subpath);
                    if (changedFile.ChangeType == "D")
                    {
                        FileSystem.DeleteFile(curThemePath);
                    }
                    else
                    {
                        var canUpdate = false;
                        if (File.Exists(curThemePath))
                        {
                            var origEntry = origFiles.GetEntry(ThemeManager.GetThemeRootDir(mode) + "\\" + subpath);
                            if (origEntry == null)
                            {
                                canUpdate = false;
                            }
                            else
                            {
                                var origContent = string.Empty;
                                using (var reader = new StreamReader(origEntry.Open()))
                                {
                                    origContent = reader.ReadToEnd();
                                }

                                if (subpath.EndsWith(".xaml", StringComparison.OrdinalIgnoreCase))
                                {
                                    if (Xml.AreEqual(origContent, File.ReadAllText(curThemePath)))
                                    {
                                        canUpdate = true;
                                    }
                                }
                                else
                                {
                                    if (origContent == FileSystem.GetMD5(curThemePath))
                                    {
                                        canUpdate = true;
                                    }
                                }
                            }
                        }
                        else
                        {
                            canUpdate = true;
                        }

                        if (canUpdate)
                        {
                            FileSystem.CopyFile(defaultPath, curThemePath);
                        }
                        else
                        {
                            logger.Debug($"Can't update {subpath}.");
                            notUpdated.Add(subpath);
                        }
                    }
                }
            }

            if (notUpdated.HasItems())
            {
                logger.Warn("Couldn't update some theme files, please update them manually:");
                notUpdated.ForEach(a => logger.Warn(a));
            }

            // Update common files
            GenerateCommonThemeFiles(mode, themeDirectory);

            // Update manifest
            currentThemeMan.ThemeApiVersion = ThemeManager.GetApiVersion(mode).ToString(3);
            File.WriteAllText(themeManifestPath, Serialization.ToYaml(currentThemeMan));
        }
Exemple #7
0
 public static string GetFilePath(string relPath, ThemeDescription defaultTheme)
 {
     return(GetFilePath(relPath, defaultTheme, ThemeManager.CurrentTheme));
 }
Exemple #8
0
        public static string GenerateNewTheme(ApplicationMode mode, string themeName)
        {
            var themeDirName    = Common.Paths.GetSafeFilename(themeName).Replace(" ", string.Empty);
            var defaultThemeDir = Path.Combine(Paths.GetThemesPath(mode), "Default");
            var outDir          = Path.Combine(PlaynitePaths.ThemesProgramPath, mode.GetDescription(), themeDirName);

            if (Directory.Exists(outDir))
            {
                throw new Exception($"Theme directory \"{outDir}\" already exists.");
            }

            FileSystem.CreateDirectory(outDir);
            var defaultThemeXamlFiles = new List <string>();

            // Modify paths in App.xaml
            var appXaml = XDocument.Load(Paths.GetThemeTemplateFilePath(mode, Themes.AppXamlName));

            foreach (var resDir in appXaml.Descendants().Where(a =>
                                                               a.Name.LocalName == "ResourceDictionary" && a.Attribute("Source")?.Value.StartsWith("Themes") == true))
            {
                var val = resDir.Attribute("Source").Value.Replace($"Themes/{mode.GetDescription()}/Default/", "");
                resDir.Attribute("Source").Value = val;
                defaultThemeXamlFiles.Add(val.Replace('/', '\\'));
            }

            // Change localization file reference
            var langElem = appXaml.Descendants().First(a => a.Attribute("Source")?.Value.EndsWith(Themes.LocSourceName) == true);

            langElem.Attribute("Source").Value = Themes.LocSourceName;

            // Update theme project file
            XNamespace ns        = "http://schemas.microsoft.com/developer/msbuild/2003";
            var        csproj    = XDocument.Load(Paths.GetThemeTemplateFilePath(mode, Themes.ThemeProjName));
            var        groupRoot = new XElement(ns + "ItemGroup");

            csproj.Root.Add(groupRoot);

            foreach (var resDir in appXaml.Descendants().Where(a =>
                                                               a.Name.LocalName == "ResourceDictionary" && a.Attribute("Source") != null))
            {
                groupRoot.Add(new XElement(ns + "Content",
                                           new XAttribute("Include", resDir.Attribute("Source").Value.Replace('/', '\\')),
                                           new XElement(ns + "Generator", "MSBuild:Compile"),
                                           new XElement(ns + "SubType", "Designer")));
            }

            // Copy to output
            CopyThemeDirectory(defaultThemeDir, outDir, defaultThemeXamlFiles.Select(a => Path.Combine(defaultThemeDir, a)).ToList());
            appXaml.Save(Path.Combine(outDir, Themes.AppXamlName));
            csproj.Save(Path.Combine(outDir, Themes.ThemeProjName));

            FileSystem.CopyFile(Paths.GetThemeTemplatePath(Themes.LocSourceName), Path.Combine(outDir, Themes.LocSourceName));
            FileSystem.CopyFile(Paths.GetThemeTemplateFilePath(mode, Themes.GlobalResourcesName), Path.Combine(outDir, Themes.GlobalResourcesName));
            FileSystem.CopyFile(Paths.GetThemeTemplateFilePath(mode, Themes.ThemeSlnName), Path.Combine(outDir, Themes.ThemeSlnName));

            var commonFontsDirs = Paths.GetThemeTemplatePath("Fonts");

            if (Directory.Exists(commonFontsDirs))
            {
                foreach (var fontFile in Directory.GetFiles(commonFontsDirs))
                {
                    var targetPath = Path.Combine(outDir, "Fonts", Path.GetFileName(fontFile));
                    FileSystem.CopyFile(fontFile, targetPath);
                }
            }

            var modeFontDir = Paths.GetThemeTemplateFilePath(mode, "Fonts");

            if (Directory.Exists(modeFontDir))
            {
                foreach (var fontFile in Directory.GetFiles(modeFontDir))
                {
                    var targetPath = Path.Combine(outDir, "Fonts", Path.GetFileName(fontFile));
                    FileSystem.CopyFile(fontFile, targetPath);
                }
            }

            var themeDesc = new ThemeDescription()
            {
                Author  = "Your Name Here",
                Name    = themeName,
                Version = "1.0",
                Mode    = mode
            };

            File.WriteAllText(Path.Combine(outDir, ThemeManager.ThemeManifestFileName), Serialization.ToYaml(themeDesc));
            Explorer.NavigateToFileSystemEntry(Path.Combine(outDir, Themes.ThemeSlnName));
            return(outDir);
        }