public override void OnClick(TreeView treeview) { string filePath = GetFilePath(); if (!romfs.FileDict.ContainsKey($"/{filePath}")) { return; } var file = romfs.FileDict[$"/{filePath}"]; HexEditor editor = (HexEditor)LibraryGUI.GetActiveContent(typeof(HexEditor)); if (editor == null) { editor = new HexEditor(); LibraryGUI.LoadEditor(editor); } editor.Text = Text; editor.Dock = DockStyle.Fill; using (var stream = romfs.OpenFile(file).AsStream()) { var mem = new MemoryStream(); stream.CopyTo(mem); editor.LoadData(mem.ToArray()); } }
private void jSONToolStripMenuItem_Click(object sender, EventArgs e) { if (Control != null) { using (var Rom = Control.OpenSection(0, false, IntegrityCheckLevel.None)) { var Romfs = new Romfs(Rom); var Dialog = folderBrowserDialog1.ShowDialog(); if (Dialog != DialogResult.Cancel) { using (var InFile = Romfs.OpenFile(Romfs.Files.FirstOrDefault(f => f.Name == "control.nacp"))) using (var Read = new BinaryReader(InFile)) { var Nacp = new Nacp(Read); var Settings = new JsonSerializerSettings { Formatting = Formatting.Indented }; File.WriteAllText($"{folderBrowserDialog1.SelectedPath}/{Nca.Header.TitleId:x16}_control.json", JsonConvert.SerializeObject(Nacp, Settings)); } } } } else { MessageBox.Show("Error: No control is present!"); } }
public static byte[] GetFirmwareData(Switch device) { long titleId = 0x0100000000000809; string contentPath = device.System.ContentManager.GetInstalledContentPath(titleId, StorageId.NandSystem, ContentType.Data); if (string.IsNullOrWhiteSpace(contentPath)) { return(null); } string firmwareTitlePath = device.FileSystem.SwitchPathToSystemPath(contentPath); using (FileStream firmwareStream = File.Open(firmwareTitlePath, FileMode.Open, FileAccess.Read)) { Nca firmwareContent = new Nca(device.System.KeySet, firmwareStream.AsStorage(), false); IStorage romFsStorage = firmwareContent.OpenSection(0, false, device.System.FsIntegrityCheckLevel, false); if (romFsStorage == null) { return(null); } Romfs firmwareRomFs = new Romfs(romFsStorage); IStorage firmwareFile = firmwareRomFs.OpenFile("/file"); byte[] data = new byte[firmwareFile.Length]; firmwareFile.Read(data, 0); return(data); } }
private void iconToolStripMenuItem_Click(object sender, EventArgs e) { if (Control != null) { using (var Rom = Control.OpenSection(0, false, IntegrityCheckLevel.None)) { var Romfs = new Romfs(Rom); var Dialog = folderBrowserDialog1.ShowDialog(); if (Dialog != DialogResult.Cancel) { using (var Icon = Romfs.OpenFile(Romfs.Files.FirstOrDefault(f => f.Name.Contains("icon")))) Icon.WriteAllBytes($"{folderBrowserDialog1.SelectedPath}/{Control.Header.TitleId:x16}_icon.jpg"); } } } else if (pictureBox1.Image != null) { var Dialog = folderBrowserDialog1.ShowDialog(); if (Dialog != DialogResult.Cancel) { pictureBox1.Image.Save($"{folderBrowserDialog1.SelectedPath}/{Nca.Header.TitleId:x16}_icon.jpg"); } } else { MessageBox.Show("Error: No control is present and icon is not in the database!"); } }
public void ReadControlData(Nca controlNca) { Romfs controlRomfs = new Romfs(controlNca.OpenSection(0, false, FsIntegrityCheckLevel, true)); IStorage controlFile = controlRomfs.OpenFile("/control.nacp"); ControlData = new Nacp(controlFile.AsStream()); }
private string[] GetTitleMeta(string TitleID = null) { var Info = new string[] { "No title info loaded.", "" }; if (Control != null) { var Rom = new Romfs(Control.OpenSection(0, false, IntegrityCheckLevel.None, true)); var OpenControl = Rom.OpenFile(Rom.Files.FirstOrDefault(f => f.Name == "control.nacp")); var OpenIcon = Rom.OpenFile(Rom.Files.FirstOrDefault(f => f.Name.Contains("icon"))); var ControlNacp = new Nacp(OpenControl.AsStream()); var Lang = ControlNacp.Descriptions.FirstOrDefault(l => l.Title.Length > 1); Info[0] = Lang.Title; Info[1] = Lang.Developer; pictureBox1.Image = Image.FromStream(OpenIcon.AsStream()); } return(Info); }
private string[] GetTitleMeta(string TitleID = null) { var Info = new string[] { "Title missing from database", "" }; if (Control != null) { var Rom = new Romfs(Control.OpenSection(0, false, IntegrityCheckLevel.None)); var OpenControl = Rom.OpenFile(Rom.Files.FirstOrDefault(f => f.Name == "control.nacp")); var OpenIcon = Rom.OpenFile(Rom.Files.FirstOrDefault(f => f.Name.Contains("icon"))); var ControlNacp = new Nacp(new BinaryReader(OpenControl)); var Lang = ControlNacp.Languages.FirstOrDefault(l => l.Title.Length > 1); Info[0] = Lang.Title; Info[1] = Lang.Developer; pictureBox1.Image = Image.FromStream(OpenIcon); } else { try { using (var WC = new WebClient()) { var Cli = WC.DownloadData($"https://gamechat.network/nucleus?title_id={TitleID}"); pictureBox1.Image = Image.FromStream(new MemoryStream(Cli)); Info[0] = Encoding.UTF8.GetString ( WC.ResponseHeaders.Get("X-GCN-Game-Name") .Select(b => (byte)b).ToArray() ); Info[1] = Encoding.UTF8.GetString ( WC.ResponseHeaders.Get("X-GCN-Game-Dev") .Select(b => (byte)b).ToArray() ); } } catch { } } return(Info); }
public override void Export() { string fileName = Path.GetFileName(FileName.RemoveIllegaleFolderNameCharacters()); SaveFileDialog sfd = new SaveFileDialog(); sfd.FileName = fileName; sfd.DefaultExt = Path.GetExtension(fileName); sfd.Filter = "Raw Data (*.*)|*.*"; if (sfd.ShowDialog() == DialogResult.OK) { ParentROMFS.OpenFile(File).WriteAllBytes($"{sfd.FileName}"); } }
public long OpenFile(string Name, out IFile FileInterface) { if (RomFs.FileExists(Name)) { Stream Stream = RomFs.OpenFile(Name); FileInterface = new IFile(Stream, Name); return(0); } FileInterface = null; return(MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist)); }
public long OpenFile(string name, out IFile fileInterface) { if (_romFs.FileExists(name)) { Stream stream = _romFs.OpenFile(name); fileInterface = new IFile(stream, name); return(0); } fileInterface = null; return(MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist)); }
private void rawToolStripMenuItem_Click(object sender, EventArgs e) { if (Control != null) { using (var Rom = Control.OpenSection(0, false, IntegrityCheckLevel.None)) { var Romfs = new Romfs(Rom); var Dialog = folderBrowserDialog1.ShowDialog(); if (Dialog != DialogResult.Cancel) { using (var Nacp = Romfs.OpenFile(Romfs.Files.FirstOrDefault(f => f.Name == "control.nacp"))) Nacp.WriteAllBytes($"{folderBrowserDialog1.SelectedPath}/{Nca.Header.TitleId:x16}_control.nacp"); } } } else { MessageBox.Show("Error: No control is present!"); } }
private static void processControlNca(Nca nca, ref Title title) { log?.WriteLine("Processing Control NCA"); if (nca.Header.ContentType == ContentType.Control) { title.titleID = String.Format("{0:X16}", nca.Header.TitleId); if (title.type == TitleType.Patch) { title.titleID = title.titleID.Substring(0, Math.Min(title.titleID.Length, 13)) + "800"; } try { Romfs romfs = new Romfs(nca.OpenSection(0, false, IntegrityCheckLevel.ErrorOnInvalid, true)); RomfsFile[] romfsFiles = romfs.Files.ToArray(); foreach (RomfsFile romfsFile in romfsFiles) { if (romfsFile.Name.Equals("control.nacp")) { using (var control = romfs.OpenFile(romfsFile).AsStream()) { processControlNacp(control, ref title); } } } } catch (MissingKeyException ex) { title.error = String.Format("Missing {0}: {1}", ex.Type == KeyType.Title ? "Title Key" : "Key", ex.Name.Replace("key_area_key_application", "master_key")); log?.WriteLine(title.error); } catch (FileNotFoundException) { } } }
public static byte[] GetFirmwareData(Switch device) { byte[] data = null; long titleId = 0x0100000000000809; string contentPath = device.System.ContentManager.GetInstalledContentPath(titleId, StorageId.NandSystem, ContentType.Data); if (string.IsNullOrWhiteSpace(contentPath)) { return(null); } string firmwareTitlePath = device.FileSystem.SwitchPathToSystemPath(contentPath); FileStream firmwareStream = File.Open(firmwareTitlePath, FileMode.Open, FileAccess.Read); Nca firmwareContent = new Nca(device.System.KeySet, firmwareStream, false); Stream romFsStream = firmwareContent.OpenSection(0, false, device.System.FsIntegrityCheckLevel); if (romFsStream == null) { return(null); } Romfs firmwareRomFs = new Romfs(romFsStream); using (MemoryStream memoryStream = new MemoryStream()) { using (Stream firmwareFile = firmwareRomFs.OpenFile("/file")) { firmwareFile.CopyTo(memoryStream); } data = memoryStream.ToArray(); } firmwareContent.Dispose(); firmwareStream.Dispose(); return(data); }
public static byte[] GetFirmwareData(Switch Device) { byte[] Data = null; long TitleId = 0x0100000000000809; string ContentPath = Device.System.ContentManager.GetInstalledContentPath(TitleId, StorageId.NandSystem, ContentType.Data); if (string.IsNullOrWhiteSpace(ContentPath)) { return(null); } string FirmwareTitlePath = Device.FileSystem.SwitchPathToSystemPath(ContentPath); FileStream FirmwareStream = File.Open(FirmwareTitlePath, FileMode.Open, FileAccess.Read); Nca FirmwareContent = new Nca(Device.System.KeySet, FirmwareStream, false); Stream RomFsStream = FirmwareContent.OpenSection(0, false, Device.System.FsIntegrityCheckLevel); if (RomFsStream == null) { return(null); } Romfs FirmwareRomFs = new Romfs(RomFsStream); using (MemoryStream MemoryStream = new MemoryStream()) { using (Stream FirmwareFile = FirmwareRomFs.OpenFile("/file")) { FirmwareFile.CopyTo(MemoryStream); } Data = MemoryStream.ToArray(); } FirmwareContent.Dispose(); FirmwareStream.Dispose(); return(Data); }
public void LoadNca(Nca mainNca, Nca controlNca) { if (mainNca.Header.ContentType != ContentType.Program) { Logger.PrintError(LogClass.Loader, "Selected NCA is not a \"Program\" NCA"); return; } IStorage romfsStorage = mainNca.OpenSection(ProgramPartitionType.Data, false, FsIntegrityCheckLevel, false); IStorage exefsStorage = mainNca.OpenSection(ProgramPartitionType.Code, false, FsIntegrityCheckLevel, true); if (exefsStorage == null) { Logger.PrintError(LogClass.Loader, "No ExeFS found in NCA"); return; } if (romfsStorage == null) { Logger.PrintWarning(LogClass.Loader, "No RomFS found in NCA"); } else { Device.FileSystem.SetRomFs(romfsStorage.AsStream(false)); } Pfs exefs = new Pfs(exefsStorage); Npdm metaData = null; if (exefs.FileExists("main.npdm")) { Logger.PrintInfo(LogClass.Loader, "Loading main.npdm..."); metaData = new Npdm(exefs.OpenFile("main.npdm").AsStream()); } else { Logger.PrintWarning(LogClass.Loader, $"NPDM file not found, using default values!"); metaData = GetDefaultNpdm(); } List <IExecutable> staticObjects = new List <IExecutable>(); void LoadNso(string filename) { foreach (PfsFileEntry file in exefs.Files.Where(x => x.Name.StartsWith(filename))) { if (Path.GetExtension(file.Name) != string.Empty) { continue; } Logger.PrintInfo(LogClass.Loader, $"Loading {filename}..."); NxStaticObject staticObject = new NxStaticObject(exefs.OpenFile(file).AsStream()); staticObjects.Add(staticObject); } } Nacp ReadControlData() { Romfs controlRomfs = new Romfs(controlNca.OpenSection(0, false, FsIntegrityCheckLevel, true)); IStorage controlFile = controlRomfs.OpenFile("/control.nacp"); Nacp controlData = new Nacp(controlFile.AsStream()); CurrentTitle = controlData.Descriptions[(int)State.DesiredTitleLanguage].Title; if (string.IsNullOrWhiteSpace(CurrentTitle)) { CurrentTitle = controlData.Descriptions.ToList().Find(x => !string.IsNullOrWhiteSpace(x.Title)).Title; } return(controlData); } if (controlNca != null) { ReadControlData(); } else { CurrentTitle = metaData.Aci0.TitleId.ToString("x16"); } LoadNso("rtld"); LoadNso("main"); LoadNso("subsdk"); LoadNso("sdk"); ContentManager.LoadEntries(); ProgramLoader.LoadStaticObjects(this, metaData, staticObjects.ToArray()); }
public void EnsureInitialized(ContentManager contentManager) { if (_fontData == null) { _device.Memory.FillWithZeros(_physicalAddress, Horizon.FontSize); uint fontOffset = 0; FontInfo CreateFont(string name) { if (contentManager.TryGetFontTitle(name, out long fontTitle)) { string contentPath = contentManager.GetInstalledContentPath(fontTitle, StorageId.NandSystem, ContentType.Data); string fontPath = _device.FileSystem.SwitchPathToSystemPath(contentPath); if (!string.IsNullOrWhiteSpace(fontPath)) { int fileIndex = 0; //Use second file in Chinese Font title for standard if (name == "FontChineseSimplified") { fileIndex = 1; } byte[] data; using (FileStream ncaFileStream = new FileStream(fontPath, FileMode.Open, FileAccess.Read)) { Nca nca = new Nca(_device.System.KeySet, ncaFileStream.AsStorage(), false); NcaSection romfsSection = nca.Sections.FirstOrDefault(x => x?.Type == SectionType.Romfs); Romfs romfs = new Romfs(nca.OpenSection(romfsSection.SectionNum, false, _device.System.FsIntegrityCheckLevel, false)); Stream fontFile = romfs.OpenFile(romfs.Files[fileIndex]).AsStream(); data = DecryptFont(fontFile); } FontInfo info = new FontInfo((int)fontOffset, data.Length); WriteMagicAndSize(_physicalAddress + fontOffset, data.Length); fontOffset += 8; uint start = fontOffset; for (; fontOffset - start < data.Length; fontOffset++) { _device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]); } return(info); } } string fontFilePath = Path.Combine(_fontsPath, name + ".ttf"); if (File.Exists(fontFilePath)) { byte[] data = File.ReadAllBytes(fontFilePath); FontInfo info = new FontInfo((int)fontOffset, data.Length); WriteMagicAndSize(_physicalAddress + fontOffset, data.Length); fontOffset += 8; uint start = fontOffset; for (; fontOffset - start < data.Length; fontOffset++) { _device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]); } return(info); } else { throw new InvalidSystemResourceException($"Font \"{name}.ttf\" not found. Please provide it in \"{_fontsPath}\"."); } } _fontData = new Dictionary <SharedFontType, FontInfo> { { SharedFontType.JapanUsEurope, CreateFont("FontStandard") }, { SharedFontType.SimplifiedChinese, CreateFont("FontChineseSimplified") }, { SharedFontType.SimplifiedChineseEx, CreateFont("FontExtendedChineseSimplified") }, { SharedFontType.TraditionalChinese, CreateFont("FontChineseTraditional") }, { SharedFontType.Korean, CreateFont("FontKorean") }, { SharedFontType.NintendoEx, CreateFont("FontNintendoExtended") } }; if (fontOffset > Horizon.FontSize) { throw new InvalidSystemResourceException( $"The sum of all fonts size exceed the shared memory size. " + $"Please make sure that the fonts don't exceed {Horizon.FontSize} bytes in total. " + $"(actual size: {fontOffset} bytes)."); } } }
public void EnsureInitialized(ContentManager ContentManager) { if (FontData == null) { Device.Memory.FillWithZeros(PhysicalAddress, Horizon.FontSize); uint FontOffset = 0; FontInfo CreateFont(string Name) { if (ContentManager.TryGetFontTitle(Name, out long FontTitle)) { string ContentPath = ContentManager.GetInstalledContentPath(FontTitle, StorageId.NandSystem, ContentType.Data); string FontPath = Device.FileSystem.SwitchPathToSystemPath(ContentPath); if (!string.IsNullOrWhiteSpace(FontPath)) { int FileIndex = 0; //Use second file in Chinese Font title for standard if (Name == "FontChineseSimplified") { FileIndex = 1; } FileStream NcaFileStream = new FileStream(FontPath, FileMode.Open, FileAccess.Read); Nca Nca = new Nca(Device.System.KeySet, NcaFileStream, false); NcaSection RomfsSection = Nca.Sections.FirstOrDefault(x => x?.Type == SectionType.Romfs); Romfs Romfs = new Romfs(Nca.OpenSection(RomfsSection.SectionNum, false, Device.System.FsIntegrityCheckLevel)); Stream FontFile = Romfs.OpenFile(Romfs.Files[FileIndex]); byte[] Data = DecryptFont(FontFile); FontInfo Info = new FontInfo((int)FontOffset, Data.Length); WriteMagicAndSize(PhysicalAddress + FontOffset, Data.Length); FontOffset += 8; uint Start = FontOffset; for (; FontOffset - Start < Data.Length; FontOffset++) { Device.Memory.WriteByte(PhysicalAddress + FontOffset, Data[FontOffset - Start]); } NcaFileStream.Dispose(); Nca.Dispose(); return(Info); } } string FontFilePath = Path.Combine(FontsPath, Name + ".ttf"); if (File.Exists(FontFilePath)) { byte[] Data = File.ReadAllBytes(FontFilePath); FontInfo Info = new FontInfo((int)FontOffset, Data.Length); WriteMagicAndSize(PhysicalAddress + FontOffset, Data.Length); FontOffset += 8; uint Start = FontOffset; for (; FontOffset - Start < Data.Length; FontOffset++) { Device.Memory.WriteByte(PhysicalAddress + FontOffset, Data[FontOffset - Start]); } return(Info); } else { throw new InvalidSystemResourceException($"Font \"{Name}.ttf\" not found. Please provide it in \"{FontsPath}\"."); } } FontData = new Dictionary <SharedFontType, FontInfo>() { { SharedFontType.JapanUsEurope, CreateFont("FontStandard") }, { SharedFontType.SimplifiedChinese, CreateFont("FontChineseSimplified") }, { SharedFontType.SimplifiedChineseEx, CreateFont("FontExtendedChineseSimplified") }, { SharedFontType.TraditionalChinese, CreateFont("FontChineseTraditional") }, { SharedFontType.Korean, CreateFont("FontKorean") }, { SharedFontType.NintendoEx, CreateFont("FontNintendoExtended") } }; if (FontOffset > Horizon.FontSize) { throw new InvalidSystemResourceException( $"The sum of all fonts size exceed the shared memory size. " + $"Please make sure that the fonts don't exceed {Horizon.FontSize} bytes in total. " + $"(actual size: {FontOffset} bytes)."); } } }
public ControlNACP(Romfs romfs, string baseTitleID) { TitleNames = new string[15]; DeveloperNames = new string[15]; Icons = new Bitmap[15]; Languages = new List <Languages>(); BaseTitleID = baseTitleID; using (var control = new BinaryReader(romfs.OpenFile("/control.nacp"))) { var versionBytes = new byte[16]; control.BaseStream.Seek(0x3060, SeekOrigin.Begin); control.Read(versionBytes, 0, 0x10); var version = Encoding.UTF8.GetString(versionBytes); var index = version.IndexOf("\0", StringComparison.Ordinal); if (index == 0) { version = string.Empty; } if (index > 0) { version = version.Substring(0, index); } Version = version; for (var i = 0; i < 15; i++) { var offset = i * 0x300; control.BaseStream.Seek(offset, SeekOrigin.Begin); var titlenameBytes = new byte[0x200]; var developernameBytes = new byte[0x100]; control.Read(titlenameBytes, 0, 0x200); control.Read(developernameBytes, 0, 0x100); var lname = ((Languages)i).ToString(); lname = $"/icon_{lname}.dat"; if (!romfs.FileExists(lname)) { continue; } Bitmap icon; using (var bm = new Bitmap(romfs.OpenFile(lname))) { icon = new Bitmap(bm); } Languages.Add((Languages)i); var titlename = Encoding.UTF8.GetString(titlenameBytes); index = titlename.IndexOf("\0", StringComparison.Ordinal); if (index == 0) { titlename = string.Empty; } if (index > 0) { titlename = titlename.Substring(0, index); } var developername = Encoding.UTF8.GetString(developernameBytes); index = developername.IndexOf("\0", StringComparison.Ordinal); if (index == 0) { developername = string.Empty; } if (index > 0) { developername = developername.Substring(0, index); } TitleNames[i] = titlename; DeveloperNames[i] = developername; Icons[i] = icon; } } }