mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-05-02 10:14:48 +12:00
Implement shared font relocation
This commit is contained in:
parent
238d84ba3b
commit
8c80099339
8 changed files with 204 additions and 9 deletions
src/core
|
@ -136,7 +136,7 @@ void Kernel::mapMemoryBlock() {
|
|||
break;
|
||||
|
||||
case KernelHandles::FontSharedMemHandle:
|
||||
mem.copySharedFont(ptr);
|
||||
mem.copySharedFont(ptr, addr);
|
||||
break;
|
||||
|
||||
case KernelHandles::CSNDSharedMemHandle:
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include "config_mem.hpp"
|
||||
#include "resource_limits.hpp"
|
||||
#include "services/fonts.hpp"
|
||||
#include "services/ptm.hpp"
|
||||
|
||||
CMRC_DECLARE(ConsoleFonts);
|
||||
|
@ -51,7 +52,7 @@ void Memory::reset() {
|
|||
if (e.handle == KernelHandles::FontSharedMemHandle) {
|
||||
// Read font size from the cmrc filesystem the font is stored in
|
||||
auto fonts = cmrc::ConsoleFonts::get_filesystem();
|
||||
e.size = fonts.open("CitraSharedFontUSRelocated.bin").size();
|
||||
e.size = fonts.open("SharedFontReplacement.bin").size();
|
||||
}
|
||||
|
||||
e.mapped = false;
|
||||
|
@ -520,10 +521,13 @@ Regions Memory::getConsoleRegion() {
|
|||
return region;
|
||||
}
|
||||
|
||||
void Memory::copySharedFont(u8* pointer) {
|
||||
void Memory::copySharedFont(u8* pointer, u32 vaddr) {
|
||||
auto fonts = cmrc::ConsoleFonts::get_filesystem();
|
||||
auto font = fonts.open("CitraSharedFontUSRelocated.bin");
|
||||
auto font = fonts.open("SharedFontReplacement.bin");
|
||||
std::memcpy(pointer, font.begin(), font.size());
|
||||
|
||||
// Relocate shared font to the address it's being loaded to
|
||||
HLE::Fonts::relocateSharedFont(pointer, vaddr);
|
||||
}
|
||||
|
||||
std::optional<u64> Memory::getProgramID() {
|
||||
|
|
107
src/core/services/fonts.cpp
Normal file
107
src/core/services/fonts.cpp
Normal file
|
@ -0,0 +1,107 @@
|
|||
// Copyright 2016 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
// Adapted from https://github.com/PabloMK7/citra/blob/master/src/core/hle/service/apt/bcfnt/bcfnt.cpp
|
||||
|
||||
#include "services/fonts.hpp"
|
||||
|
||||
namespace HLE::Fonts {
|
||||
void relocateSharedFont(u8* sharedFont, u32 newAddress) {
|
||||
constexpr u32 sharedFontStartOffset = 0x80;
|
||||
const u8* cfntData = &sharedFont[sharedFontStartOffset];
|
||||
|
||||
CFNT cfnt;
|
||||
std::memcpy(&cfnt, cfntData, sizeof(cfnt));
|
||||
|
||||
u32 assumedCmapOffset = 0;
|
||||
u32 assumedCwdhOffset = 0;
|
||||
u32 assumedTglpOffset = 0;
|
||||
u32 firstCmapOffset = 0;
|
||||
u32 firstCwdhOffset = 0;
|
||||
u32 firstTglpOffset = 0;
|
||||
|
||||
// First discover the location of sections so that the rebase offset can be auto-detected
|
||||
u32 currentOffset = sharedFontStartOffset + cfnt.headerSize;
|
||||
for (uint block = 0; block < cfnt.numBlocks; ++block) {
|
||||
const u8* data = &sharedFont[currentOffset];
|
||||
|
||||
SectionHeader sectionHeader;
|
||||
std::memcpy(§ionHeader, data, sizeof(sectionHeader));
|
||||
|
||||
if (firstCmapOffset == 0 && std::memcmp(sectionHeader.magic, "CMAP", 4) == 0) {
|
||||
firstCmapOffset = currentOffset;
|
||||
} else if (firstCwdhOffset == 0 && std::memcmp(sectionHeader.magic, "CWDH", 4) == 0) {
|
||||
firstCwdhOffset = currentOffset;
|
||||
} else if (firstTglpOffset == 0 && std::memcmp(sectionHeader.magic, "TGLP", 4) == 0) {
|
||||
firstTglpOffset = currentOffset;
|
||||
} else if (std::memcmp(sectionHeader.magic, "FINF", 4) == 0) {
|
||||
Fonts::FINF finf;
|
||||
std::memcpy(&finf, data, sizeof(finf));
|
||||
|
||||
assumedCmapOffset = finf.cmapOffset - sizeof(SectionHeader);
|
||||
assumedCwdhOffset = finf.cwdhOffset - sizeof(SectionHeader);
|
||||
assumedTglpOffset = finf.tglpOffset - sizeof(SectionHeader);
|
||||
}
|
||||
|
||||
currentOffset += sectionHeader.sectionSize;
|
||||
}
|
||||
|
||||
u32 previousBase = assumedCmapOffset - firstCmapOffset;
|
||||
if ((previousBase != assumedCwdhOffset - firstCwdhOffset) || (previousBase != assumedTglpOffset - firstTglpOffset)) {
|
||||
Helpers::warn("You shouldn't be seeing this. Shared Font file offsets might be borked?");
|
||||
}
|
||||
|
||||
u32 offset = newAddress - previousBase;
|
||||
|
||||
// Reset pointer back to start of sections and do the actual rebase
|
||||
currentOffset = sharedFontStartOffset + cfnt.headerSize;
|
||||
for (uint block = 0; block < cfnt.numBlocks; ++block) {
|
||||
u8* data = &sharedFont[currentOffset];
|
||||
|
||||
SectionHeader sectionHeader;
|
||||
std::memcpy(§ionHeader, data, sizeof(sectionHeader));
|
||||
|
||||
if (std::memcmp(sectionHeader.magic, "FINF", 4) == 0) {
|
||||
Fonts::FINF finf;
|
||||
std::memcpy(&finf, data, sizeof(finf));
|
||||
|
||||
// Relocate the offsets in the FINF section
|
||||
finf.cmapOffset += offset;
|
||||
finf.cwdhOffset += offset;
|
||||
finf.tglpOffset += offset;
|
||||
|
||||
std::memcpy(data, &finf, sizeof(finf));
|
||||
} else if (std::memcmp(sectionHeader.magic, "CMAP", 4) == 0) {
|
||||
Fonts::CMAP cmap;
|
||||
std::memcpy(&cmap, data, sizeof(cmap));
|
||||
|
||||
// Relocate the offsets in the CMAP section
|
||||
if (cmap.nextCmapOffset != 0) {
|
||||
cmap.nextCmapOffset += offset;
|
||||
}
|
||||
|
||||
std::memcpy(data, &cmap, sizeof(cmap));
|
||||
} else if (std::memcmp(sectionHeader.magic, "CWDH", 4) == 0) {
|
||||
Fonts::CWDH cwdh;
|
||||
std::memcpy(&cwdh, data, sizeof(cwdh));
|
||||
|
||||
// Relocate the offsets in the CWDH section
|
||||
if (cwdh.nextCwdhOffset != 0) {
|
||||
cwdh.nextCwdhOffset += offset;
|
||||
}
|
||||
|
||||
std::memcpy(data, &cwdh, sizeof(cwdh));
|
||||
} else if (std::memcmp(sectionHeader.magic, "TGLP", 4) == 0) {
|
||||
Fonts::TGLP tglp;
|
||||
std::memcpy(&tglp, data, sizeof(tglp));
|
||||
|
||||
// Relocate the offsets in the TGLP section
|
||||
tglp.sheetDataOffset += offset;
|
||||
std::memcpy(data, &tglp, sizeof(tglp));
|
||||
}
|
||||
|
||||
currentOffset += sectionHeader.sectionSize;
|
||||
}
|
||||
}
|
||||
} // namespace HLE::Fonts
|
Loading…
Add table
Add a link
Reference in a new issue