Пример #1
0
        public Form OpenFile(byte[] File, string name)
        {
            BinaryDataReader bin   = new BinaryDataReader(new MemoryStream(File));
            string           Magic = bin.ReadString(4);

            if (Magic == "Yaz0")
            {
                return(OpenFile(ManagedYaz0.Decompress(File), name));
            }
            else if (Magic == "SARC")
            {
                var f = new EditorForms.SzsEditor(SARCExt.SARC.UnpackRamN(File), this);
                f.Text = name;
                OpenForm(f);
                return(f);
            }
            else if (Magic == "FLYT")
            {
                EditorView editorView = new EditorView(new BflytFile(File));
                editorView.Text = name;
                OpenForm(editorView);
                return(editorView);
            }
            else if (Magic == "FLAN")
            {
                var editor = new BflanEditor(new BflanFile(File));
                OpenForm(editor);
                return(editor);
            }
            return(null);
        }
Пример #2
0
        public Form OpenFile(byte[] File, IFileWriter saveTo)
        {
            string Magic  = Encoding.ASCII.GetString(File, 0, 4);
            Form   result = null;

            if (Magic == "Yaz0")
            {
                return(OpenFile(ManagedYaz0.Decompress(File), saveTo));
            }
            else if (Magic == "SARC")
            {
                result = new EditorForms.SzsEditor(SARCExt.SARC.Unpack(File), saveTo, this);
            }
            else if (Magic == "FLYT")
            {
                result = new EditorView(new BflytFile(File), saveTo);
            }
            else if (Magic == "FLAN")
            {
                result = new BflanEditor(new BflanFile(File), saveTo);
            }

            if (result != null)
            {
                OpenForm(result);
            }

            return(result);
        }
Пример #3
0
 public static void AutoThemeFileUploaded(Uint8Array arr)
 {
     if (!ValidAutoThemeParts.ContainsStr(AutoThemePartName))
     {
         Window.Alert("An invalid part name has been selected");
         return;
     }
     DoActionWithloading(() =>
     {
         byte[] szs   = arr.ToArray();
         byte[] sarc  = ManagedYaz0.Decompress(szs);
         var szsData  = SARCExt.SARC.UnpackRamN(sarc);
         sarc         = null;
         var detected = SwitchThemesCommon.DetectSarc(szsData, DefaultTemplates.templates);
         if (detected == null)
         {
             Window.Alert("This is not a valid theme file, if it's from a newer firmware it's not compatible with this tool yet");
             return;
         }
         if (!detected.TemplateName.Contains(AutoThemePartName))
         {
             Window.Alert("This szs is valid but it doesn't look like the right one, you can keep it but it might generate themes that affect the wrong parts of the menu");
         }
         Window.LocalStorage.SetItem(AutoThemePartName, Convert.ToBase64String(szs));
         Window.LocalStorage.SetItem(AutoThemePartName + "Name", detected.TemplateName + " " + detected.FirmName);
         LoadAutoThemeState();
     });
 }
Пример #4
0
 public static void UploadSZS(Uint8Array arr)         //called from js
 {
     DoActionWithloading(() =>
     {
         byte[] sarc = ManagedYaz0.Decompress(arr.ToArray());
         CommonSzs   = SARCExt.SARC.UnpackRamN(sarc);
         sarc        = null;
         while (LayoutsComboBox.LastChild.TextContent != "Don't patch")
         {
             LayoutsComboBox.RemoveChild(LayoutsComboBox.LastChild);
         }
         targetPatch = SwitchThemesCommon.DetectSarc(CommonSzs, DefaultTemplates.templates);
         if (targetPatch == null)
         {
             Window.Alert("This is not a valid theme file, if it's from a newer firmware it's not compatible with this tool yet");
             CommonSzs               = null;
             targetPatch             = null;
             lblDetected.TextContent = "";
             return;
         }
         lblDetected.TextContent = "Detected " + targetPatch.TemplateName + " " + targetPatch.FirmName;
         for (int i = 0; i < layoutPatches.Length; i++)
         {
             if (layoutPatches[i] != null && layoutPatches[i].IsCompatible(CommonSzs))
             {
                 LayoutsComboBox.Add(new HTMLOptionElement()
                 {
                     TextContent = layoutPatches[i].ToString(), Value = i.ToString()
                 });
             }
         }
     });
 }
Пример #5
0
        static bool DiffSzs(string[] args)
        {
            if (args.Length != 4)
            {
                Console.Error.WriteLine("Error: Wrong number of arguments.");
                return(false);
            }

            var original = args[1];
            var edited   = args[2];
            var outName  = args[3];

            var options = new LayoutDiff.DiffOptions {
                HideOnlineButton = args.Any(x => x == "--hide-online")
            };

            try
            {
                var res = LayoutDiff.Diff(
                    SARC.Unpack(ManagedYaz0.Decompress(File.ReadAllBytes(original))),
                    SARC.Unpack(ManagedYaz0.Decompress(File.ReadAllBytes(edited))),
                    options
                    );

                File.WriteAllBytes(outName, res.Item1.AsByteArray());

                Console.WriteLine(res.Item2);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"There was an error:\r\n{ex}");
            }

            return(true);
        }
Пример #6
0
        private void materialRaisedButton4_Click(object sender, EventArgs e)
        {
            if (CommonSzs == null)
            {
                return;
            }
            var opn = new OpenFileDialog();

            if (opn.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            var szs2         = SARCExt.SARC.UnpackRamN(ManagedYaz0.Decompress(File.ReadAllBytes(opn.FileName)));
            var filesIn1     = CommonSzs.Files.Keys.ToList();
            var filesIn2     = szs2.Files.Keys.ToList();
            var common       = filesIn1.Intersect(filesIn2);
            var filesOnlyIn1 = filesIn1.Except(common);
            var filesOnlyIn2 = filesIn2.Except(common);

            filesIn1 = null;
            filesIn2 = null;
            common   = null;
            if (CommonSzs.Files.Count != 0)
            {
                MessageBox.Show($"Files only in 1 : \r\n {string.Join("\r\n", filesOnlyIn1)}\r\n\r\nFiles only in 2: \r\n {string.Join("\r\n", filesOnlyIn2)}");
            }
        }
Пример #7
0
        public static void ExtractNxTheme(string theme, string path)
        {
            var data = SARC.UnpackRamN(ManagedYaz0.Decompress(File.ReadAllBytes(theme)));

            foreach (var f in data.Files.Where(x => x.Key != "info.json"))
            {
                File.WriteAllBytes(Path.Combine(path, f.Key), f.Value);
            }
        }
Пример #8
0
        public void CompressionDecompression()
        {
            var data = MakeData();
            var dec  = ManagedYaz0.Decompress(ManagedYaz0.Compress(data, 9));

            if (!data.SequenceEqual(dec))
            {
                throw new Exception();
            }
        }
Пример #9
0
        private void OpenSzsButton(object sender, EventArgs e)
        {
            OpenFileDialog opn = new OpenFileDialog()
            {
                Title  = "open szs",
                Filter = "szs file|*.szs|all files|*.*",
            };

            if (opn.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            if (!File.Exists(opn.FileName))
            {
                MessageBox.Show("Could not open file");
                return;
            }

            targetPatch = null;
            LayoutPatchList.Items.Clear();
            LayoutPatchList.Items.Add("Don't patch");

            CommonSzs   = SARCExt.SARC.UnpackRamN(ManagedYaz0.Decompress(File.ReadAllBytes(opn.FileName)));
            targetPatch = SwitchThemesCommon.DetectSarc(CommonSzs, Templates);

            if (targetPatch == null)
            {
                if (Advanced)
                {
                    AdvancedUpdate();
                    lblDetected.Text = "Unknown szs file";
                    return;
                }

                MessageBox.Show("This is not a valid theme file, if it's from a newer firmware it's not compatible with this tool yet");
                CommonSzs        = null;
                targetPatch      = null;
                lblDetected.Text = "";
                return;
            }

            AdvancedUpdate();
            lblDetected.Text = "Detected " + targetPatch.TemplateName + " " + targetPatch.FirmName;

            foreach (var l in Layouts.Values)
            {
                if (l.IsCompatible(CommonSzs))
                {
                    LayoutPatchList.Items.Add(l);
                }
            }
            LayoutPatchList.Items.Add("Open from file...");
            LayoutPatchList.SelectedIndex = 0;
        }
Пример #10
0
        private void materialRaisedButton6_Click(object sender, EventArgs e)
        {
            if (CommonSzs == null)
            {
                MessageBox.Show("Open the modded file via SZS PATCHING>OPEN SZS first");
                return;
            }
            OpenFileDialog opn = new OpenFileDialog()
            {
                Title  = "Open the original SZS to diff",
                Filter = "SZS file|*.szs"
            };

            if (opn.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            var         originalSzs = SARCExt.SARC.UnpackRamN(ManagedYaz0.Decompress(File.ReadAllBytes(opn.FileName)));
            LayoutPatch res         = null;

#if !DEBUG
            try
            {
#endif
            string msg;
            (res, msg) = LayoutDiff.Diff(originalSzs, CommonSzs);
            if (msg != null)
            {
                MessageBox.Show(msg);
            }
#if !DEBUG
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }
#endif

            SaveFileDialog sav = new SaveFileDialog()
            {
                Title  = "save the patch file",
                Filter = "json patch file|*.json"
            };
            if (sav.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            File.WriteAllText(sav.FileName, res.AsJson());
        }
Пример #11
0
        private void materialRaisedButton6_Click(object sender, EventArgs e)
        {
            if (CommonSzs == null)
            {
                MessageBox.Show("Open the modded file via SZS PATCHING>OPEN SZS first");
                return;
            }
            OpenFileDialog opn = new OpenFileDialog()
            {
                Title  = "Open the original SZS to diff",
                Filter = "SZS file|*.szs"
            };

            if (opn.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            var originalSzs = SARCExt.SARC.UnpackRamN(ManagedYaz0.Decompress(File.ReadAllBytes(opn.FileName)));
            var res         = LayoutDiff.Diff(originalSzs, CommonSzs);

            if (res == null)
            {
                return;
            }
            res.TargetName = targetPatch?.szsName;

            //if (targetPatch == null || targetPatch.NXThemeName == "home")
            //if (MessageBox.Show(
            //	"Do you want to patch the applet buttons color property in the bntx ? This allow you to properly change their color via a layout, select no if you did not edit them.\r\n" +
            //	"This feature is only for the home menu.", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
            //	res.PatchAppletColorAttrib = true;

            SaveFileDialog sav = new SaveFileDialog()
            {
                Title  = "save the patch file",
                Filter = "json patch file|*.json"
            };

            if (sav.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            File.WriteAllText(sav.FileName, res.AsJson());
        }
Пример #12
0
        private void materialRaisedButton1_Click(object sender, EventArgs e)
        {
            if (CommonSzs == null)
            {
                return;
            }
            SaveFileDialog sav = new SaveFileDialog()
            {
                Filter = "szs file|*.szs",
            };

            if (sav.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            var sarc = SARC.PackN(CommonSzs);

            File.WriteAllBytes(sav.FileName, ManagedYaz0.Compress(sarc.Item2, 3, (int)sarc.Item1));
        }
Пример #13
0
        private void MaterialTexturesAssumption(string name)
        {
            SarcData src = SARC.Unpack(ManagedYaz0.Decompress(System.IO.File.ReadAllBytes(name)));

            foreach (var val in src.Files.Where(x => x.Key.EndsWith(".bflyt")))
            {
                var(n, f) = val;

                SwitchThemes.Common.Bflyt.BflytFile ff = new SwitchThemes.Common.Bflyt.BflytFile(f);

                if (ff.Mat1 != null)
                {
                    foreach (var m in ff.Mat1.Materials)
                    {
                        Assert.AreEqual(m.Textures.Length, m.TextureTransformations.Length);
                    }
                }
            }
        }
Пример #14
0
 private void button3_Click(object sender, EventArgs e)
 {
     if (Original == null)
     {
         Original = SARCExt.SARC.UnpackRamN(ManagedYaz0.Decompress(File.ReadAllBytes(textBox1.Text)));
     }
     if (Edited == null)
     {
         Edited = SARCExt.SARC.UnpackRamN(ManagedYaz0.Decompress(File.ReadAllBytes(textBox2.Text)));
     }
     try
     {
         var(res, msg) = SwitchThemes.Common.LayoutDiff.Diff(Original, Edited);
         if (msg != null)
         {
             MessageBox.Show(msg);
         }
         if (res != null)
         {
             SaveFileDialog sav = new SaveFileDialog()
             {
                 Filter = "json file|*.json"
             };
             if (sav.ShowDialog() != DialogResult.OK)
             {
                 return;
             }
             File.WriteAllText(sav.FileName, res.AsJson());
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
     if (ClearEdited)
     {
         Edited = null;
     }
     if (ClearOriginal)
     {
         Original = null;
     }
 }
Пример #15
0
        SarcData SelectFile(ref TextBox target, string name = null)
        {
            if (name is null)
            {
                OpenFileDialog opn = new OpenFileDialog()
                {
                    Filter = "szs files|*.szs"
                };
                if (opn.ShowDialog() != DialogResult.OK)
                {
                    return(null);
                }
                name = opn.FileName;
            }
            target.Text = name;
            var sarc = SARC.Unpack(ManagedYaz0.Decompress(File.ReadAllBytes(target.Text)));

            EnableResidentMenuCheckbox(sarc);
            return(sarc);
        }
Пример #16
0
        public static byte[] Make(SarcData input, DDSLoadResult dds, PatchTemplate targetPatch, LayoutPatch layout)
        {
            if (layout != null)
            {
                var layoutres = SwitchThemesCommon.PatchLayouts(input, layout.Files);
                if (layoutres == BflytFile.PatchResult.Fail)
                {
                    Window.Alert("One of the target files for the selected layout patch is missing in the SZS, you are probably using an already patched SZS or the wrong layout");
                    return(null);
                }
                else if (layoutres == BflytFile.PatchResult.CorruptedFile)
                {
                    Window.Alert("A layout in this SZS is missing a pane required for the selected layout patch, you are probably using an already patched SZS or the wrong layout");
                    return(null);
                }
            }

            if (SwitchThemesCommon.PatchBntx(input, dds, targetPatch) == BflytFile.PatchResult.Fail)
            {
                Window.Alert(
                    "Can't build this theme: the szs you opened doesn't contain some information needed to patch the bntx," +
                    "without this information it is not possible to rebuild the bntx." +
                    "You should use an original or at least working szs");
                return(null);
            }
            var res = SwitchThemesCommon.PatchBgLayouts(input, targetPatch);

            if (res == BflytFile.PatchResult.Fail)
            {
                Window.Alert("Couldn't patch this file, it might have been already modified or it's from an unsupported system version.");
                return(null);
            }
            else if (res == BflytFile.PatchResult.CorruptedFile)
            {
                Window.Alert("This file has been already patched with another tool and is not compatible, you should get an unmodified layout.");
                return(null);
            }
            var sarc = SARC.PackN(input);

            return(ManagedYaz0.Compress(sarc.Item2, 1, (int)sarc.Item1));
        }
Пример #17
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.LoadTemplate(lyt);
                patcher.PatchLayouts(l);
            }

            var final = patcher.GetFinalSarc();

            CompareSarc(final, exp);
        }
Пример #18
0
        private void materialRaisedButton6_Click(object sender, EventArgs e)
        {
            if (CommonSzs == null)
            {
                MessageBox.Show("Open the modded file from Open szs first");
                return;
            }
            OpenFileDialog opn = new OpenFileDialog()
            {
                Title  = "open the original szs to diff",
                Filter = "szs file|*.szs"
            };

            if (opn.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            var originalSzs = SARCExt.SARC.UnpackRamN(ManagedYaz0.Decompress(File.ReadAllBytes(opn.FileName)));
            var res         = LayoutDiff.Diff(originalSzs, CommonSzs);

            if (res == null)
            {
                return;
            }
            res.TargetName = targetPatch?.szsName;
            SaveFileDialog sav = new SaveFileDialog()
            {
                Title  = "save the patch file",
                Filter = "json patch file|*.json"
            };

            if (sav.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            File.WriteAllText(sav.FileName, res.AsJson());
        }
Пример #19
0
        private void materialRaisedButton7_Click(object sender, EventArgs e)
        {
            if (CommonSzs == null)
            {
                return;
            }
            OpenFileDialog opn = new OpenFileDialog();

            if (opn.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            var           originalSzs = SARCExt.SARC.UnpackRamN(ManagedYaz0.Decompress(File.ReadAllBytes(opn.FileName)));
            List <string> diffFiles   = new List <string>();

            foreach (string f in originalSzs.Files.Keys)
            {
                if (!originalSzs.Files[f].SequenceEqual(CommonSzs.Files[f]))
                {
                    diffFiles.Add(f);
                }
            }
            MessageBox.Show(string.Join("\r\n", diffFiles.ToArray()));
        }
Пример #20
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    CommonSzs   = SARCExt.SARC.Unpack(ManagedYaz0.Decompress(File.ReadAllBytes(Target)));
            var    targetPatch = DefaultTemplates.GetFor(CommonSzs);

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

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

            if (Image == null || !File.Exists(Image))
            {
                Console.WriteLine("No image file !\r\nNote that only dds files are supported for szs themes.");
                return(false);
            }
            string Layout = args.Where(x => x.EndsWith(".json")).FirstOrDefault();

            string Output = GetArg("out");

            if (Output == null || Output == "")
            {
                return(false);
            }

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

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

                if (Image != null)
                {
                    res = Patcher.PatchMainBG(File.ReadAllBytes(Image));
                    if (!res)
                    {
                        Console.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.EndsWith(".dds"))
                        {
                            Console.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.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.LoadTemplate(File.ReadAllText(Layout));
                    var layoutres = Patcher.PatchLayouts(l);
                    if (!layoutres)
                    {
                        Console.WriteLine("One of the target files for the selected layout patch is missing in the SZS, you are probably using an already patched SZS");
                        return(false);
                    }
                }

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

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

                if (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.WriteLine("Error: " + ex.Message);
            }

            return(true);
        }
Пример #21
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    CommonSzs   = SARCExt.SARC.UnpackRamN(ManagedYaz0.Decompress(File.ReadAllBytes(Target)));
            var    targetPatch = SzsPatcher.DetectSarc(CommonSzs, DefaultTemplates.templates);

            if (targetPatch == null)
            {
                Console.WriteLine("Unknown szs file");
                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.WriteLine("No image file !");
                return(false);
            }
            string Layout = args.Where(x => x.EndsWith(".json")).FirstOrDefault();

            string Output = GetArg("out");

            if (Output == null || Output == "")
            {
                return(false);
            }

            if (!Image.EndsWith(".dds"))
            {
                if (Form1.ImageToDDS(Image, Path.GetTempPath()))
                {
                    Image = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Image) + ".dds");
                }
                else
                {
                    return(false);
                }
            }

            try
            {
                var res     = true;
                var Patcher = new SzsPatcher(CommonSzs, DefaultTemplates.templates);

                if (Image != null)
                {
                    res = Patcher.PatchMainBG(File.ReadAllBytes(Image));
                    if (!res)
                    {
                        Console.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.EndsWith(".dds") && !Form1.IcontoDDS(ref path))
                        {
                            path = null;
                        }
                        if (path != null)
                        {
                            if (!Patcher.PatchAppletIcon(File.ReadAllBytes(path), a.NxThemeName))
                            {
                                Console.WriteLine($"Applet icon patch for {a.NxThemeName} failed");
                            }
                        }
                    }
                }

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

                if (Layout != null)
                {
                    Patcher.EnableAnimations = true;
                    var l         = LayoutPatch.LoadTemplate(File.ReadAllText(Layout));
                    var layoutres = Patcher.PatchLayouts(l, targetPatch.NXThemeName, targetPatch);
                    if (!layoutres)
                    {
                        Console.WriteLine("One of the target files for the selected layout patch is missing in the SZS, you are probably using an already patched SZS");
                        return(false);
                    }
                    layoutres = Patcher.PatchAnimations(l.Anims);
                }

                CommonSzs = Patcher.GetFinalSarc();
                var sarc = SARC.PackN(CommonSzs);

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

                if (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.WriteLine("Error: " + ex.Message);
            }

            return(true);
        }
Пример #22
0
        private void PatchButtonClick(object sender, EventArgs e)
        {
            if (CommonSzs == null || targetPatch == null)
            {
                MessageBox.Show("Open a valid theme first !");
                return;
            }
            if (tbBntxFile.Text.Trim() == "")
            {
                if (LayoutPatchList.SelectedIndex <= 0)
                {
                    MessageBox.Show("There is nothing to patch");
                    return;
                }
                if (MessageBox.Show("Are you sure you want to continue without selecting a bntx ?", "", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
            }
            else if (!File.Exists(tbBntxFile.Text))
            {
                MessageBox.Show($"{tbBntxFile.Text} not found !");
                return;
            }

            SaveFileDialog sav = new SaveFileDialog()
            {
                Filter   = "szs file|*.szs",
                FileName = targetPatch.szsName
            };

            if (sav.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            var res = BflytFile.PatchResult.OK;

            if (tbBntxFile.Text.Trim() != "")
            {
                if (!BgImageCheck(true))
                {
                    return;
                }

                if (SwitchThemesCommon.PatchBntx(CommonSzs, File.ReadAllBytes(tbBntxFile.Text), targetPatch) == BflytFile.PatchResult.Fail)
                {
                    MessageBox.Show(
                        "Can't build this theme: the szs you opened doesn't contain some information needed to patch the bntx," +
                        "without this information it is not possible to rebuild the bntx." +
                        "You should use an original or at least working szs", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (targetPatch.NXThemeName == "home" && AlbumIcon != null)
                {
                    if (!AlbumIcon.EndsWith(".dds") && !AlbumIcontoDDS())
                    {
                        return;
                    }
                    SwitchThemesCommon.PatchBntxTexture(CommonSzs, File.ReadAllBytes(AlbumIcon), "RdtIcoPvr_00^s", 0x02000000);
                }

                res = SwitchThemesCommon.PatchBgLayouts(CommonSzs, targetPatch);

                if (res == BflytFile.PatchResult.Fail)
                {
                    MessageBox.Show("Couldn't patch this file, it might have been already modified or it's from an unsupported system version.");
                    return;
                }
                else if (res == BflytFile.PatchResult.CorruptedFile)
                {
                    MessageBox.Show("This file has been already patched with another tool and is not compatible, you should get an unmodified layout.");
                    return;
                }
            }

            if (LayoutPatchList.SelectedIndex != 0)
            {
                var layoutres = SwitchThemesCommon.PatchLayouts(CommonSzs, LayoutPatchList.SelectedItem as LayoutPatch, targetPatch.NXThemeName == "home", !UseAnim.Checked);
                if (layoutres == BflytFile.PatchResult.Fail)
                {
                    MessageBox.Show("One of the target files for the selected layout patch is missing in the SZS, you are probably using an already patched SZS");
                    return;
                }
                else if (layoutres == BflytFile.PatchResult.CorruptedFile)
                {
                    MessageBox.Show("A layout in this SZS is missing a pane required for the selected layout patch, you are probably using an already patched SZS");
                    return;
                }
                layoutres = SwitchThemesCommon.PatchAnimations(CommonSzs, (LayoutPatchList.SelectedItem as LayoutPatch).Anims);
                if (layoutres != BflytFile.PatchResult.OK)
                {
                    MessageBox.Show("Error while patching the animations !");
                    return;
                }
            }

            var sarc = SARC.PackN(CommonSzs);

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

            if (res == BflytFile.PatchResult.AlreadyPatched)
            {
                MessageBox.Show("Done, This file has already been patched before.\r\nIf you have issues try with an unmodified file");
            }
            else
            {
                MessageBox.Show("Done");
            }
        }
Пример #23
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    CommonSzs   = SARCExt.SARC.UnpackRamN(ManagedYaz0.Decompress(File.ReadAllBytes(Target)));
            var    targetPatch = SwitchThemesCommon.DetectSarc(CommonSzs, DefaultTemplates.templates);

            if (targetPatch == null)
            {
                Console.WriteLine("Unknown szs file");
                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.WriteLine("No image file !");
                return(false);
            }
            string Layout = args.Where(x => x.EndsWith(".json")).FirstOrDefault();

            string Output = GetArg("out");

            if (Output == null || Output == "")
            {
                return(false);
            }

            if (!Image.EndsWith(".dds"))
            {
                if (Form1.ImageToDDS(Image, Path.GetTempPath()))
                {
                    Image = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Image) + ".dds");
                }
                else
                {
                    return(false);
                }
            }

            string album = GetArg("ablum");

            if (album != null && !album.EndsWith(".dds"))
            {
                if (Form1.ImageToDDS(album, Path.GetTempPath(), "DXT5", true))
                {
                    album = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(album) + ".dds");
                }
                else
                {
                    return(false);
                }
            }

            try
            {
                var res = BflytFile.PatchResult.OK;

                if (Image != null)
                {
                    if (SwitchThemesCommon.PatchBntx(CommonSzs, File.ReadAllBytes(Image), targetPatch) == BflytFile.PatchResult.Fail)
                    {
                        Console.WriteLine(
                            "Can't build this theme: the szs you opened doesn't contain some information needed to patch the bntx," +
                            "without this information it is not possible to rebuild the bntx." +
                            "You should use an original or at least working szs", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return(false);
                    }
                }

                if (album != null && targetPatch.szsName == "ResidentMenu.szs")
                {
                    SwitchThemesCommon.PatchBntxTexture(CommonSzs, File.ReadAllBytes(album), "RdtIcoPvr_00^s", 0x02000000);
                }

                res = SwitchThemesCommon.PatchBgLayouts(CommonSzs, targetPatch);

                if (res == BflytFile.PatchResult.Fail)
                {
                    Console.WriteLine("Couldn't patch this file, it might have been already modified or it's from an unsupported system version.");
                    return(false);
                }
                else if (res == BflytFile.PatchResult.CorruptedFile)
                {
                    Console.WriteLine("This file has been already patched with another tool and is not compatible, you should get an unmodified layout.");
                    return(false);
                }

                if (Layout != null)
                {
                    var layoutres = SwitchThemesCommon.PatchLayouts(CommonSzs, LayoutPatch.LoadTemplate(File.ReadAllText(Layout)), targetPatch.NXThemeName == "home", true);
                    if (layoutres == BflytFile.PatchResult.Fail)
                    {
                        Console.WriteLine("One of the target files for the selected layout patch is missing in the SZS, you are probably using an already patched SZS");
                        return(false);
                    }
                    else if (layoutres == BflytFile.PatchResult.CorruptedFile)
                    {
                        Console.WriteLine("A layout in this SZS is missing a pane required for the selected layout patch, you are probably using an already patched SZS");
                        return(false);
                    }
                }

                var sarc = SARC.PackN(CommonSzs);

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

                if (res == BflytFile.PatchResult.AlreadyPatched)
                {
                    Console.WriteLine("Done, This file has already been patched before.\r\nIf you have issues try with an unmodified file");
                }
                else
                {
                    Console.WriteLine("Done");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
            }

            return(true);
        }
Пример #24
0
        private void PatchButtonClick(object sender, EventArgs e)
        {
            if (CommonSzs == null || targetPatch == null)
            {
                MessageBox.Show("Open a valid SZS first");
                return;
            }
            if (tbImageFile.Text.Trim() == "")
            {
                if (LayoutPatchList.SelectedIndex <= 0)
                {
                    MessageBox.Show("There is nothing to patch");
                    return;
                }
                if (MessageBox.Show("Are you sure you want to continue without selecting a background?", "", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
            }
            else if (!File.Exists(tbImageFile.Text))
            {
                MessageBox.Show($"{tbImageFile.Text} not found!");
                return;
            }

            SaveFileDialog sav = new SaveFileDialog()
            {
                Filter   = "SZS file|*.szs",
                FileName = targetPatch.szsName
            };

            if (sav.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            SzsPatcher Patcher = new SzsPatcher(CommonSzs, Templates);

            var res = BflytFile.PatchResult.OK;

            if (tbImageFile.Text.Trim() != "")
            {
                if (!BgImageCheck(true))
                {
                    return;
                }

                res = Patcher.PatchMainBG(File.ReadAllBytes(tbImageFile.Text));
                if (res == BflytFile.PatchResult.Fail)
                {
                    MessageBox.Show("Couldn't patch this file, it might have been already modified or it's from an unsupported system version.");
                    return;
                }
                else if (res == BflytFile.PatchResult.CorruptedFile)
                {
                    MessageBox.Show("This file has been already patched with another tool and is not compatible, you should get an unmodified layout.");
                    return;
                }
            }

            if (LayoutPatchList.SelectedIndex != 0)
            {
                Patcher.EnableAnimations = !UseAnim.Checked;
                var layoutres = Patcher.PatchLayouts(LayoutPatchList.SelectedItem as LayoutPatch, targetPatch.NXThemeName, targetPatch.NXThemeName == "home");
                if (layoutres == BflytFile.PatchResult.Fail)
                {
                    MessageBox.Show("One of the target files for the selected layout patch is missing in the SZS, you are probably using an already patched SZS");
                    return;
                }
                else if (layoutres == BflytFile.PatchResult.CorruptedFile)
                {
                    MessageBox.Show("A layout in this SZS is missing a pane required for the selected layout patch, you are probably using an already patched SZS");
                    return;
                }
                layoutres = Patcher.PatchAnimations((LayoutPatchList.SelectedItem as LayoutPatch).Anims);
                if (layoutres != BflytFile.PatchResult.OK)
                {
                    MessageBox.Show("Error while patching the animations!");
                    return;
                }
            }

            if (targetPatch.NXThemeName == "home")
            {
                foreach (var n in TextureReplacement.NxNameToList[targetPatch.NXThemeName])
                {
                    if (HomeAppletIcons[n.NxThemeName] == null)
                    {
                        continue;
                    }
                    string path = HomeAppletIcons[n.NxThemeName];
                    if (!path.EndsWith(".dds") && !IcontoDDS(ref path))
                    {
                        return;
                    }
                    HomeAppletIcons[n.NxThemeName] = path;
                    Patcher.PatchAppletIcon(File.ReadAllBytes(path), n.NxThemeName);
                }
            }
            else if (targetPatch.NXThemeName == "lock" && LockCustomIcon != null)
            {
                if (!LockCustomIcon.EndsWith(".dds") && !IcontoDDS(ref LockCustomIcon))
                {
                    return;
                }
                Patcher.PatchAppletIcon(File.ReadAllBytes(LockCustomIcon), TextureReplacement.Entrance[0].NxThemeName);
            }

            CommonSzs = Patcher.GetFinalSarc();
            var sarc = SARC.PackN(CommonSzs);

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

            if (res == BflytFile.PatchResult.AlreadyPatched)
            {
                MessageBox.Show("Done, This file has already been patched before.\r\nIf you have issues try with an unmodified file");
            }
            else
            {
                MessageBox.Show("Done");
            }
        }
Пример #25
0
 // Not an important test, just to detect changes in the behavior
 public void ConsistentCompression()
 {
     Assert.AreEqual(hash.StringHash(ManagedYaz0.Compress(MakeData(), 9)), "7865BE4B54FBFE3ED21DEA9CB1E184F0F305404251203AD9EFDFA264280CD0FD");
 }
Пример #26
0
        private void PatchButtonClick(object sender, EventArgs e)
        {
            if (CommonSzs == null || targetPatch == null)
            {
                MessageBox.Show("Open a valid SZS first");
                return;
            }
            if (tbImageFile.Text.Trim() == "")
            {
                if (LayoutPatchList.SelectedIndex <= 0)
                {
                    MessageBox.Show("There is nothing to patch");
                    return;
                }

                if (MessageBox.Show("Are you sure you want to continue without selecting a background image ?", "", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
            }
            else if (!File.Exists(tbImageFile.Text))
            {
                MessageBox.Show($"{tbImageFile.Text} not found!");
                return;
            }

            SaveFileDialog sav = new SaveFileDialog()
            {
                Filter   = "SZS file|*.szs",
                FileName = targetPatch.szsName
            };

            if (sav.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            SzsPatcher Patcher = new SzsPatcher(CommonSzs, Templates);

            var res = true;

            if (tbImageFile.Text.Trim() != "")
            {
                if (!BgImageCheck(true))
                {
                    return;
                }

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

            if (targetPatch.NXThemeName == "home")
            {
                foreach (var n in TextureReplacement.NxNameToList[targetPatch.NXThemeName])
                {
                    if (HomeAppletIcons[n.NxThemeName] == null)
                    {
                        continue;
                    }
                    string path = HomeAppletIcons[n.NxThemeName];
                    if (!path.EndsWith(".dds") && !IcontoDDS(ref path))
                    {
                        return;
                    }
                    HomeAppletIcons[n.NxThemeName] = path;
                    if (!Patcher.PatchAppletIcon(File.ReadAllBytes(path), n.NxThemeName))
                    {
                        MessageBox.Show($"Failed applet icon patch for {n.NxThemeName}");
                        return;
                    }
                }
            }
            else if (targetPatch.NXThemeName == "lock" && LockCustomIcon != null)
            {
                if (!LockCustomIcon.EndsWith(".dds") && !IcontoDDS(ref LockCustomIcon))
                {
                    return;
                }
                Patcher.PatchAppletIcon(File.ReadAllBytes(LockCustomIcon), TextureReplacement.Entrance[0].NxThemeName);
            }

            if (LayoutPatchList.SelectedIndex != 0)
            {
                Patcher.EnableAnimations = !UseAnim.Checked;
                var layoutres = Patcher.PatchLayouts(LayoutPatchList.SelectedItem as LayoutPatch, targetPatch.NXThemeName, targetPatch);
                if (!layoutres)
                {
                    MessageBox.Show("One of the target files for the selected layout patch is missing in the SZS, you are probably using an already patched SZS");
                    return;
                }
                layoutres = Patcher.PatchAnimations((LayoutPatchList.SelectedItem as LayoutPatch).Anims);
                if (!layoutres)
                {
                    MessageBox.Show("Error while patching the animations !");
                    return;
                }
            }

            CommonSzs = Patcher.GetFinalSarc();
            var sarc = SARC.PackN(CommonSzs);

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

            if (Patcher.PatchTemplate.RequiresCodePatch)
            {
                MessageBox.Show("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
            {
                MessageBox.Show("Done");
            }
        }
Пример #27
0
        static void DoAutoTheme(string type, string url, string layout)
        {
            cardLoad = Document.GetElementById <HTMLDivElement>("CardLoad");
            Document.GetElementById <HTMLDivElement>("CardTutorial").Hidden = true;
            string themeTarget = "<br/><br/>This theme is for " + Window.LocalStorage.GetItem(type + "Name") as string + "<br/>To change the target version upload another szs for Auto-Theme on the <a href=\"index.html\">Home page</a>";

            cardLoad.InnerHTML = "Wait while your theme is being generated.... " + themeTarget;
            cardLoad.Hidden    = false;
            StartLoading();

            DDSEncoder.DDSLoadResult LoadedDDS;
            SarcData      CommonSzs;
            PatchTemplate targetPatch;
            LayoutPatch   targetLayout = null;

            void BuildTheme()
            {
                var yaz0 = Theme.Make(CommonSzs, LoadedDDS, targetPatch, targetLayout);

                if (yaz0 == null)
                {
                    endWithError("Theme.Make() failed :(");
                    return;
                }
                Uint8Array dwn           = new Uint8Array(yaz0);
                string     DownloadFname = targetPatch.szsName;

                Script.Write("downloadBlob(dwn,DownloadFname,'application/octet-stream');");
                Document.GetElementById <HTMLDivElement>("CardLoad").InnerHTML = "Your theme has been generated !" + themeTarget;
                EndLoading();
            }

            void DDSDownloaded(Uint8Array arr)
            {
                LoadedDDS = DDSEncoder.LoadDDS(arr.ToArray());
                arr       = null;
                CommonSzs = SARC.UnpackRamN(
                    ManagedYaz0.Decompress(
                        Convert.FromBase64String(
                            Window.LocalStorage.GetItem(type) as string)));

                targetPatch = SwitchThemesCommon.DetectSarc(CommonSzs, DefaultTemplates.templates);

                if (layout == null)
                {
                    BuildTheme();
                }
                else
                {
                    HttpRequest(layout, LayoutDownloaded, "Layout");
                }
            }

            void LayoutDownloaded(string req)
            {
                targetLayout = LayoutPatch.LoadTemplate(req);
                BuildTheme();
            }

            HttpRequest(url, DDSDownloaded, "DDS");
        }
Пример #28
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	CommonSzs = SARCExt.SARC.UnpackRamN(ManagedYaz0.Decompress(File.ReadAllBytes(Target)));
			var targetPatch = SzsPatcher.DetectSarc(CommonSzs, DefaultTemplates.templates);

			if (targetPatch == null)
			{
				Console.WriteLine("Unknown szs file");
				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.WriteLine("No image file !");
				return false;
			}
			string Layout = args.Where(x => x.EndsWith(".json")).FirstOrDefault();
			
			string Output = GetArg("out");

			if (Output == null || Output == "")
				return false;

			if (!Image.EndsWith(".dds"))
			{
				if (Form1.ImageToDDS(Image, Path.GetTempPath()))
					Image = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Image) + ".dds");
				else return false;
			}		

			try
			{				
				var res = BflytFile.PatchResult.OK;
				var Patcher = new SzsPatcher(CommonSzs, DefaultTemplates.templates);

				if (Image != null)
				{
					res = Patcher.PatchMainBG(File.ReadAllBytes(Image));
					if (res == BflytFile.PatchResult.Fail)
					{
						Console.WriteLine("Couldn't patch this file, it might have been already modified or it's from an unsupported system version.");
						return false;
					}
					else if (res == BflytFile.PatchResult.CorruptedFile)
					{
						Console.WriteLine("This file has been already patched with another tool and is not compatible, you should get an unmodified layout.");
						return false;
					}
				}				

				if (Layout != null)
				{
					Patcher.EnableAnimations = true;
					var l = LayoutPatch.LoadTemplate(File.ReadAllText(Layout));
					var layoutres = Patcher.PatchLayouts(l, targetPatch.NXThemeName, targetPatch.NXThemeName == "home");
					if (layoutres == BflytFile.PatchResult.Fail)
					{
						Console.WriteLine("One of the target files for the selected layout patch is missing in the SZS, you are probably using an already patched SZS");
						return false;
					}
					else if (layoutres == BflytFile.PatchResult.CorruptedFile)
					{
						Console.WriteLine("A layout in this SZS is missing a pane required for the selected layout patch, you are probably using an already patched SZS");
						return false;
					}
					layoutres = Patcher.PatchAnimations(l.Anims);
				}

				void ProcessAppletIcons(List<TextureReplacement> l)
				{
					foreach (var a in l)
					{
						string path = GetArg(a.NxThemeName);
						if (!path.EndsWith(".dds") && !Form1.IcontoDDS(ref path))
							path = null;
						if (path != null)
							Patcher.PatchAppletIcon(File.ReadAllBytes(path), a.NxThemeName);
					}
				}

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

				CommonSzs = Patcher.GetFinalSarc();
				var sarc = SARC.PackN(CommonSzs);

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

				if (res == BflytFile.PatchResult.AlreadyPatched)
					Console.WriteLine("Done, This file has already been patched before.\r\nIf you have issues try with an unmodified file");
				else
					Console.WriteLine("Done");
			}
			catch (Exception ex)
			{
				Console.WriteLine("ERROR: " + ex.Message);
			}

			return true;
		}