Exemplo n.º 1
0
 public void LoadAndOptimizeAll()
 {
     // This generates layouts to load for SwitchThemesNXTests::Layouts::LayoutLoading
     foreach (var f in Directory.EnumerateFiles("../../../../../SwitchThemes/layouts").Where(x => x.EndsWith(".json")))
     {
         File.WriteAllText(
             Path.Combine(Util.GetPath("ParsedLayouts"), Path.GetFileName(f)),
             LayoutPatch.Load(File.ReadAllText(f)).AsJson()
             );
     }
 }
Exemplo n.º 2
0
        private void ProcessSzs(string name)
        {
            SarcData src = SARC.Unpack(ManagedYaz0.Decompress(Util.ReadData($"Source/{name}.szs")));
            SarcData exp = SARC.Unpack(ManagedYaz0.Decompress(Util.ReadData($"Expected/{name}.szs")));

            string lyt = Util.Exists($"Source/{name}.json") ? Util.ReadString($"Source/{name}.json") : null;

            SzsPatcher patcher = new SzsPatcher(src);

            Assert.IsTrue(patcher.PatchMainBG(DDS));

            if (lyt != null)
            {
                var l = LayoutPatch.Load(lyt);
                patcher.PatchLayouts(l);
            }

            var final = patcher.GetFinalSarc();

            CompareSarc(final, exp);
        }
Exemplo n.º 3
0
        static bool NXThemeFromArgs(string[] args)
        {
            if (args.Length < 4)
            {
                return(false);
            }
            string Target = args[1];

            if (!Form1.HomeMenuParts.Values.Contains(Target))
            {
                return(false);
            }

            string Image = args.Where(x => x.EndsWith(".dds") || x.EndsWith(".jpg") || x.EndsWith(".png") || x.EndsWith("jpeg")).FirstOrDefault();

            if (Image != null && !File.Exists(Image))
            {
                Console.Error.WriteLine("No image file!");
                return(false);
            }

            string Layout = args.Where(x => x.EndsWith(".json")).FirstOrDefault();

            if (Image == null && Layout == null)
            {
                Console.Error.WriteLine("You need at least an image or a layout to make a theme.");
                return(false);
            }

            string GetArg(string start)
            {
                var a = args.Where(x => x.StartsWith(start + "=")).FirstOrDefault();

                if (a == null)
                {
                    return(null);
                }
                else
                {
                    return(a.Split('=')[1]);
                }
            }

            string Name        = GetArg("name");
            string Author      = GetArg("author");
            string Output      = GetArg("out");
            string ExtraCommon = GetArg("commonlyt");
            string album       = GetArg("album");

            if (Output == null || Output == "")
            {
                Console.Error.WriteLine("Missing out= arg");
                return(false);
            }

            if (Name == null || Name.Trim() == "")
            {
                (Name, Author) = ThemeInputInfo.Ask();
                if (Name == null)
                {
                    return(true);
                }
            }

            LayoutPatch layout = null;

            if (Layout != null && File.Exists(Layout))
            {
                layout = LayoutPatch.Load(File.ReadAllText(Layout));
            }

            Dictionary <string, string> AppletIcons = new Dictionary <string, string>();

            void PopulateAppletIcons(List <TextureReplacement> l)
            {
                foreach (var a in l)
                {
                    string path = GetArg(a.NxThemeName);
                    AppletIcons.Add(a.NxThemeName, path);
                }
            }

            if (TextureReplacement.NxNameToList.ContainsKey(Target))
            {
                PopulateAppletIcons(TextureReplacement.NxNameToList[Target]);
            }

            try
            {
                var builder = new NXThemeBuilder(Target, Name, Author);

                if (layout != null)
                {
                    builder.AddMainLayout(layout);
                }
                if (Image != null)
                {
                    builder.AddMainBg(File.ReadAllBytes(Image));
                }
                if (ExtraCommon != null)
                {
                    builder.AddCommonLayout(File.ReadAllText(ExtraCommon));
                }

                foreach (var i in AppletIcons)
                {
                    if (i.Value != null)
                    {
                        builder.AddAppletIcon(i.Key, File.ReadAllBytes(i.Value));
                    }
                }

                File.WriteAllBytes(Output, builder.GetNxtheme());
                Console.WriteLine("Done!");
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Error: " + ex.Message);
                return(false);
            }

            return(true);
        }
Exemplo n.º 4
0
        static bool SZSFromArgs(string[] args)
        {
            string GetArg(string start)
            {
                var a = args.Where(x => x.StartsWith(start + "=")).FirstOrDefault();

                if (a == null)
                {
                    return(null);
                }
                else
                {
                    return(a.Split('=')[1]);
                }
            }

            if (args.Length < 2)
            {
                return(false);
            }

            string Target      = args[1];
            var    TargetSzs   = SARCExt.SARC.Unpack(ManagedYaz0.Decompress(File.ReadAllBytes(Target)));
            var    targetPatch = DefaultTemplates.GetFor(TargetSzs);

            if (targetPatch == null)
            {
                Console.Error.WriteLine("Unknown SZS file.");
                return(false);
            }

            string Image = args.Where(x => x.ToLower().EndsWith(".dds")).FirstOrDefault();

            if (Image != null && !File.Exists(Image))
            {
                Console.Error.WriteLine("DDS image not found!\r\nNote that only DDS files are supported for szs themes.");
                return(false);
            }

            string Layout = args.Where(x => x.EndsWith(".json")).FirstOrDefault();

            if (Layout != null && !File.Exists(Layout))
            {
                Console.Error.WriteLine("JSON layout not found!");
                return(false);
            }

            string Output = GetArg("out");

            if (Output == null || Output == "")
            {
                Console.Error.WriteLine("No output path supplied! Example: 'out=file.szs'");
                return(false);
            }

            if (Image == null && Layout == null)
            {
                Console.Error.WriteLine("Nothing to do! An image (DDS), layout (JSON), or both should be supplied.");
                return(false);
            }

            try {
                var res     = true;
                var Patcher = new SzsPatcher(TargetSzs);

                if (Image != null)
                {
                    {
                        var dds = Common.Images.Util.ParseDds(File.ReadAllBytes(Image));
                        if (dds.Encoding != "DXT1")
                        {
                            Console.WriteLine("WARNING: the encoding of the selected DDS is not DXT1, it may crash on the switch");
                        }
                        if (dds.Size.Width != 1280 || dds.Size.Height != 720)
                        {
                            Console.WriteLine("WARNING: the selected image is not 720p (1280x720), it may crash on the switch");
                        }
                    }

                    res = Patcher.PatchMainBG(File.ReadAllBytes(Image));
                    if (!res)
                    {
                        Console.Error.WriteLine("Couldn't patch this file, it might have been already modified or it's from an unsupported system version.");
                        return(false);
                    }
                }

                void ProcessAppletIcons(List <TextureReplacement> l)
                {
                    foreach (var a in l)
                    {
                        string path = GetArg(a.NxThemeName);
                        if (path != null && !path.EndsWith(".dds"))
                        {
                            Console.Error.WriteLine($"{path} is not supported, only dds files can be used for szs themes");
                            path = null;
                        }
                        if (path != null)
                        {
                            if (!Patcher.PatchAppletIcon(File.ReadAllBytes(path), a.NxThemeName))
                            {
                                Console.Error.WriteLine($"Applet icon patch for {a.NxThemeName} failed");
                            }
                        }
                    }
                }

                if (TextureReplacement.NxNameToList.ContainsKey(targetPatch.NXThemeName))
                {
                    ProcessAppletIcons(TextureReplacement.NxNameToList[targetPatch.NXThemeName]);
                }

                if (Layout != null)
                {
                    var l         = LayoutPatch.Load(File.ReadAllText(Layout));
                    var layoutres = Patcher.PatchLayouts(l);
                    if (!layoutres)
                    {
                        Console.Error.WriteLine("One or more of the target files for the selected layout patch is missing in the SZS. Either this layout it not meant for this menu or you are using an already patched SZS.");
                        return(false);
                    }
                }

                TargetSzs = Patcher.GetFinalSarc();
                var sarc = SARC.Pack(TargetSzs);

                File.WriteAllBytes(Output, ManagedYaz0.Compress(sarc.Item2, 3, (int)sarc.Item1));
                GC.Collect();

                if (Image != null && Patcher.PatchTemplate.RequiresCodePatch)
                {
                    Console.WriteLine("The file has been patched successfully but due to memory limitations this szs requires an extra code patch to be applied to the home menu, if you use NXThemesInstaller to install this it will be done automatically, otherwise you need to manually copy the patches from https://github.com/exelix11/SwitchThemeInjector/tree/master/SwitchThemesNX/romfs to the exefs patches directory of your cfw");
                }
                else
                {
                    Console.WriteLine("Done!");
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Error: " + ex.Message);
            }

            return(true);
        }