コード例 #1
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);
        }
コード例 #2
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);
        }
コード例 #3
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;
		}
コード例 #4
0
ファイル: Form1.cs プロジェクト: gfdac/SwitchThemeInjector
        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");
            }
        }
コード例 #5
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);
        }
コード例 #6
0
ファイル: Form1.cs プロジェクト: wdnmd114/SwitchThemeInjector
        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");
            }
        }