VERY experimental Nintendo Switch 2 emulator
C++
Merge pull request #2709 from DarkLordZach/oss-ext-fonts-1
David66d80be727ca
diff --cc src/core/hle/service/ns/pl_u.cppindex 9d49f36e8a,7586e3e6c9..8f0c6bc07b--- a/src/core/hle/service/ns/pl_u.cpp+++ b/src/core/hle/service/ns/pl_u.cpp@@@ -162,116 -156,51 +157,51 @@@ PL_U::PL_U(FileSystem::FileSystemContro};RegisterHandlers(functions);// Attempt to load shared font data from disk- const auto* nand = FileSystem::GetSystemNANDContents();+ const auto* nand = fsc.GetSystemNANDContents();std::size_t offset = 0;- // Rebuild shared fonts from data ncas- if (nand->HasEntry(static_cast<u64>(FontArchives::Standard),- FileSys::ContentRecordType::Data)) {- impl->shared_font = std::make_shared<Kernel::PhysicalMemory>(SHARED_FONT_MEM_SIZE);- for (auto font : SHARED_FONTS) {- const auto nca =- nand->GetEntry(static_cast<u64>(font.first), FileSys::ContentRecordType::Data);- if (!nca) {- LOG_ERROR(Service_NS, "Failed to find {:016X}! Skipping",- static_cast<u64>(font.first));- continue;- }- const auto romfs = nca->GetRomFS();- if (!romfs) {- LOG_ERROR(Service_NS, "{:016X} has no RomFS! Skipping",- static_cast<u64>(font.first));- continue;- }- const auto extracted_romfs = FileSys::ExtractRomFS(romfs);- if (!extracted_romfs) {- LOG_ERROR(Service_NS, "Failed to extract RomFS for {:016X}! Skipping",- static_cast<u64>(font.first));- continue;- }- const auto font_fp = extracted_romfs->GetFile(font.second);- if (!font_fp) {- LOG_ERROR(Service_NS, "{:016X} has no file \"{}\"! Skipping",- static_cast<u64>(font.first), font.second);- continue;- }- std::vector<u32> font_data_u32(font_fp->GetSize() / sizeof(u32));- font_fp->ReadBytes<u32>(font_data_u32.data(), font_fp->GetSize());- // We need to be BigEndian as u32s for the xor encryption- std::transform(font_data_u32.begin(), font_data_u32.end(), font_data_u32.begin(),- Common::swap32);- FontRegion region{- static_cast<u32>(offset + 8),- static_cast<u32>((font_data_u32.size() * sizeof(u32)) -- 8)}; // Font offset and size do not account for the header- DecryptSharedFont(font_data_u32, *impl->shared_font, offset);- impl->shared_font_regions.push_back(region);+ // Rebuild shared fonts from data ncas or synthesize++ impl->shared_font = std::make_shared<Kernel::PhysicalMemory>(SHARED_FONT_MEM_SIZE);+ for (auto font : SHARED_FONTS) {+ FileSys::VirtualFile romfs;+ const auto nca =+ nand->GetEntry(static_cast<u64>(font.first), FileSys::ContentRecordType::Data);+ if (nca) {+ romfs = nca->GetRomFS();}- } else {- impl->shared_font = std::make_shared<Kernel::PhysicalMemory>(- SHARED_FONT_MEM_SIZE); // Shared memory needs to always be allocated and a fixed size-- const std::string user_path = FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir);- const std::string filepath{user_path + SHARED_FONT};-- // Create path if not already created- if (!FileUtil::CreateFullPath(filepath)) {- LOG_ERROR(Service_NS, "Failed to create sharedfonts path \"{}\"!", filepath);- return;+ if (!romfs) {+ romfs = FileSys::SystemArchive::SynthesizeSystemArchive(static_cast<u64>(font.first));}- bool using_ttf = false;- for (const char* font_ttf : SHARED_FONTS_TTF) {- if (FileUtil::Exists(user_path + font_ttf)) {- using_ttf = true;- FileUtil::IOFile file(user_path + font_ttf, "rb");- if (file.IsOpen()) {- std::vector<u8> ttf_bytes(file.GetSize());- file.ReadBytes<u8>(ttf_bytes.data(), ttf_bytes.size());- FontRegion region{- static_cast<u32>(offset + 8),- static_cast<u32>(ttf_bytes.size())}; // Font offset and size do not account- // for the header- EncryptSharedFont(ttf_bytes, *impl->shared_font, offset);- impl->shared_font_regions.push_back(region);- } else {- LOG_WARNING(Service_NS, "Unable to load font: {}", font_ttf);- }- } else if (using_ttf) {- LOG_WARNING(Service_NS, "Unable to find font: {}", font_ttf);- }+ if (!romfs) {+ LOG_ERROR(Service_NS, "Failed to find or synthesize {:016X}! Skipping",+ static_cast<u64>(font.first));+ continue;}- if (using_ttf)- return;- FileUtil::IOFile file(filepath, "rb");-- if (file.IsOpen()) {- // Read shared font data- ASSERT(file.GetSize() == SHARED_FONT_MEM_SIZE);- file.ReadBytes(impl->shared_font->data(), impl->shared_font->size());- impl->BuildSharedFontsRawRegions(*impl->shared_font);- } else {- LOG_WARNING(Service_NS,- "Shared Font file missing. Loading open source replacement from memory");- // clang-format off- const std::vector<std::vector<u8>> open_source_shared_fonts_ttf = {- {std::begin(FontChineseSimplified), std::end(FontChineseSimplified)},- {std::begin(FontChineseTraditional), std::end(FontChineseTraditional)},- {std::begin(FontExtendedChineseSimplified), std::end(FontExtendedChineseSimplified)},- {std::begin(FontKorean), std::end(FontKorean)},- {std::begin(FontNintendoExtended), std::end(FontNintendoExtended)},- {std::begin(FontStandard), std::end(FontStandard)},- };- // clang-format on-- for (const std::vector<u8>& font_ttf : open_source_shared_fonts_ttf) {- const FontRegion region{static_cast<u32>(offset + 8),- static_cast<u32>(font_ttf.size())};- EncryptSharedFont(font_ttf, *impl->shared_font, offset);- impl->shared_font_regions.push_back(region);- }+ const auto extracted_romfs = FileSys::ExtractRomFS(romfs);+ if (!extracted_romfs) {+ LOG_ERROR(Service_NS, "Failed to extract RomFS for {:016X}! Skipping",+ static_cast<u64>(font.first));+ continue;+ }+ const auto font_fp = extracted_romfs->GetFile(font.second);+ if (!font_fp) {+ LOG_ERROR(Service_NS, "{:016X} has no file \"{}\"! Skipping",+ static_cast<u64>(font.first), font.second);+ continue;}+ std::vector<u32> font_data_u32(font_fp->GetSize() / sizeof(u32));+ font_fp->ReadBytes<u32>(font_data_u32.data(), font_fp->GetSize());+ // We need to be BigEndian as u32s for the xor encryption+ std::transform(font_data_u32.begin(), font_data_u32.end(), font_data_u32.begin(),+ Common::swap32);+ // Font offset and size do not account for the header+ const FontRegion region{static_cast<u32>(offset + 8),+ static_cast<u32>((font_data_u32.size() * sizeof(u32)) - 8)};+ DecryptSharedFont(font_data_u32, *impl->shared_font, offset);+ impl->shared_font_regions.push_back(region);}}diff --cc src/core/hle/service/ns/pl_u.hindex 35ca424d29,7375c1c6f0..7e9fe6220b--- a/src/core/hle/service/ns/pl_u.h+++ b/src/core/hle/service/ns/pl_u.h@@@ -5,19 -5,16 +5,22 @@@#pragma once#include <memory>+ #include "core/hle/kernel/physical_memory.h"#include "core/hle/service/service.h"-namespace Service::NS {+namespace Service {++namespace FileSystem {+class FileSystemController;+} // namespace FileSystem++namespace NS {+ void EncryptSharedFont(const std::vector<u8>& input, Kernel::PhysicalMemory& output);+class PL_U final : public ServiceFramework<PL_U> {public:- PL_U();+ PL_U(FileSystem::FileSystemController& fsc);~PL_U() override;private: