Пример #1
0
        public static List <string> GenerateCommonThemeFiles(ApplicationMode mode, string 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(PlaynitePaths.EngLocSourceFileName) == true);

            langElem.Attribute("Source").Value = PlaynitePaths.EngLocSourceFileName;

            // 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")));
            }

            appXaml.Save(Path.Combine(outDir, Themes.AppXamlName));
            csproj.Save(Path.Combine(outDir, Themes.ThemeProjName));

            FileSystem.CopyFile(Paths.GetThemeTemplatePath(PlaynitePaths.EngLocSourceFileName), Path.Combine(outDir, PlaynitePaths.EngLocSourceFileName));
            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);
                }
            }

            return(defaultThemeXamlFiles);
        }
Пример #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 = 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);
        }