summaryrefslogtreecommitdiff
path: root/src/core/hle
diff options
context:
space:
mode:
authorGravatar David2019-09-22 18:09:55 +1000
committerGravatar GitHub2019-09-22 18:09:55 +1000
commitcf1e4770f910a49c3fbfeb881168e5f3def26484 (patch)
treedcdcfb617b1fa680eaa74cf1a96f872f259bba1b /src/core/hle
parentMerge pull request #2535 from DarkLordZach/cheat-v2 (diff)
parentRevert "Merge pull request #2709 from DarkLordZach/oss-ext-fonts-1" (diff)
downloadyuzu-cf1e4770f910a49c3fbfeb881168e5f3def26484.tar.gz
yuzu-cf1e4770f910a49c3fbfeb881168e5f3def26484.tar.xz
yuzu-cf1e4770f910a49c3fbfeb881168e5f3def26484.zip
Merge pull request #2893 from ogniK5377/revert-dlz
Revert "Merge pull request #2709 from DarkLordZach/oss-ext-fonts-1"
Diffstat (limited to 'src/core/hle')
-rw-r--r--src/core/hle/service/ns/pl_u.cpp164
-rw-r--r--src/core/hle/service/ns/pl_u.h3
2 files changed, 117 insertions, 50 deletions
diff --git a/src/core/hle/service/ns/pl_u.cpp b/src/core/hle/service/ns/pl_u.cpp
index 8f0c6bc07..9d49f36e8 100644
--- a/src/core/hle/service/ns/pl_u.cpp
+++ b/src/core/hle/service/ns/pl_u.cpp
@@ -6,6 +6,13 @@
6#include <cstring> 6#include <cstring>
7#include <vector> 7#include <vector>
8 8
9#include <FontChineseSimplified.h>
10#include <FontChineseTraditional.h>
11#include <FontExtendedChineseSimplified.h>
12#include <FontKorean.h>
13#include <FontNintendoExtended.h>
14#include <FontStandard.h>
15
9#include "common/assert.h" 16#include "common/assert.h"
10#include "common/common_paths.h" 17#include "common/common_paths.h"
11#include "common/common_types.h" 18#include "common/common_types.h"
@@ -17,7 +24,6 @@
17#include "core/file_sys/nca_metadata.h" 24#include "core/file_sys/nca_metadata.h"
18#include "core/file_sys/registered_cache.h" 25#include "core/file_sys/registered_cache.h"
19#include "core/file_sys/romfs.h" 26#include "core/file_sys/romfs.h"
20#include "core/file_sys/system_archive/system_archive.h"
21#include "core/hle/ipc_helpers.h" 27#include "core/hle/ipc_helpers.h"
22#include "core/hle/kernel/shared_memory.h" 28#include "core/hle/kernel/shared_memory.h"
23#include "core/hle/service/filesystem/filesystem.h" 29#include "core/hle/service/filesystem/filesystem.h"
@@ -88,16 +94,15 @@ static void DecryptSharedFont(const std::vector<u32>& input, Kernel::PhysicalMem
88 offset += transformed_font.size() * sizeof(u32); 94 offset += transformed_font.size() * sizeof(u32);
89} 95}
90 96
91void EncryptSharedFont(const std::vector<u8>& input, Kernel::PhysicalMemory& output) { 97static void EncryptSharedFont(const std::vector<u8>& input, Kernel::PhysicalMemory& output,
92 ASSERT_MSG(input.size() * sizeof(u32) < SHARED_FONT_MEM_SIZE, "Shared fonts exceeds 17mb!"); 98 std::size_t& offset) {
93 99 ASSERT_MSG(offset + input.size() + 8 < SHARED_FONT_MEM_SIZE, "Shared fonts exceeds 17mb!");
94 const auto key = Common::swap32(EXPECTED_RESULT ^ EXPECTED_MAGIC); 100 const u32 KEY = EXPECTED_MAGIC ^ EXPECTED_RESULT;
95 std::vector<u32> transformed_font(input.size() + 2); 101 std::memcpy(output.data() + offset, &EXPECTED_RESULT, sizeof(u32)); // Magic header
96 transformed_font[0] = Common::swap32(EXPECTED_MAGIC); 102 const u32 ENC_SIZE = static_cast<u32>(input.size()) ^ KEY;
97 transformed_font[1] = Common::swap32(input.size() * sizeof(u32)) ^ key; 103 std::memcpy(output.data() + offset + sizeof(u32), &ENC_SIZE, sizeof(u32));
98 std::transform(input.begin(), input.end(), transformed_font.begin() + 2, 104 std::memcpy(output.data() + offset + (sizeof(u32) * 2), input.data(), input.size());
99 [key](u32 in) { return in ^ key; }); 105 offset += input.size() + (sizeof(u32) * 2);
100 std::memcpy(output.data(), transformed_font.data(), transformed_font.size() * sizeof(u32));
101} 106}
102 107
103// Helper function to make BuildSharedFontsRawRegions a bit nicer 108// Helper function to make BuildSharedFontsRawRegions a bit nicer
@@ -159,49 +164,114 @@ PL_U::PL_U(FileSystem::FileSystemController& fsc)
159 // Attempt to load shared font data from disk 164 // Attempt to load shared font data from disk
160 const auto* nand = fsc.GetSystemNANDContents(); 165 const auto* nand = fsc.GetSystemNANDContents();
161 std::size_t offset = 0; 166 std::size_t offset = 0;
162 // Rebuild shared fonts from data ncas or synthesize 167 // Rebuild shared fonts from data ncas
163 168 if (nand->HasEntry(static_cast<u64>(FontArchives::Standard),
164 impl->shared_font = std::make_shared<Kernel::PhysicalMemory>(SHARED_FONT_MEM_SIZE); 169 FileSys::ContentRecordType::Data)) {
165 for (auto font : SHARED_FONTS) { 170 impl->shared_font = std::make_shared<Kernel::PhysicalMemory>(SHARED_FONT_MEM_SIZE);
166 FileSys::VirtualFile romfs; 171 for (auto font : SHARED_FONTS) {
167 const auto nca = 172 const auto nca =
168 nand->GetEntry(static_cast<u64>(font.first), FileSys::ContentRecordType::Data); 173 nand->GetEntry(static_cast<u64>(font.first), FileSys::ContentRecordType::Data);
169 if (nca) { 174 if (!nca) {
170 romfs = nca->GetRomFS(); 175 LOG_ERROR(Service_NS, "Failed to find {:016X}! Skipping",
176 static_cast<u64>(font.first));
177 continue;
178 }
179 const auto romfs = nca->GetRomFS();
180 if (!romfs) {
181 LOG_ERROR(Service_NS, "{:016X} has no RomFS! Skipping",
182 static_cast<u64>(font.first));
183 continue;
184 }
185 const auto extracted_romfs = FileSys::ExtractRomFS(romfs);
186 if (!extracted_romfs) {
187 LOG_ERROR(Service_NS, "Failed to extract RomFS for {:016X}! Skipping",
188 static_cast<u64>(font.first));
189 continue;
190 }
191 const auto font_fp = extracted_romfs->GetFile(font.second);
192 if (!font_fp) {
193 LOG_ERROR(Service_NS, "{:016X} has no file \"{}\"! Skipping",
194 static_cast<u64>(font.first), font.second);
195 continue;
196 }
197 std::vector<u32> font_data_u32(font_fp->GetSize() / sizeof(u32));
198 font_fp->ReadBytes<u32>(font_data_u32.data(), font_fp->GetSize());
199 // We need to be BigEndian as u32s for the xor encryption
200 std::transform(font_data_u32.begin(), font_data_u32.end(), font_data_u32.begin(),
201 Common::swap32);
202 FontRegion region{
203 static_cast<u32>(offset + 8),
204 static_cast<u32>((font_data_u32.size() * sizeof(u32)) -
205 8)}; // Font offset and size do not account for the header
206 DecryptSharedFont(font_data_u32, *impl->shared_font, offset);
207 impl->shared_font_regions.push_back(region);
171 } 208 }
172 209
173 if (!romfs) { 210 } else {
174 romfs = FileSys::SystemArchive::SynthesizeSystemArchive(static_cast<u64>(font.first)); 211 impl->shared_font = std::make_shared<Kernel::PhysicalMemory>(
175 } 212 SHARED_FONT_MEM_SIZE); // Shared memory needs to always be allocated and a fixed size
213
214 const std::string user_path = FileUtil::GetUserPath(FileUtil::UserPath::SysDataDir);
215 const std::string filepath{user_path + SHARED_FONT};
176 216
177 if (!romfs) { 217 // Create path if not already created
178 LOG_ERROR(Service_NS, "Failed to find or synthesize {:016X}! Skipping", 218 if (!FileUtil::CreateFullPath(filepath)) {
179 static_cast<u64>(font.first)); 219 LOG_ERROR(Service_NS, "Failed to create sharedfonts path \"{}\"!", filepath);
180 continue; 220 return;
181 } 221 }
182 222
183 const auto extracted_romfs = FileSys::ExtractRomFS(romfs); 223 bool using_ttf = false;
184 if (!extracted_romfs) { 224 for (const char* font_ttf : SHARED_FONTS_TTF) {
185 LOG_ERROR(Service_NS, "Failed to extract RomFS for {:016X}! Skipping", 225 if (FileUtil::Exists(user_path + font_ttf)) {
186 static_cast<u64>(font.first)); 226 using_ttf = true;
187 continue; 227 FileUtil::IOFile file(user_path + font_ttf, "rb");
228 if (file.IsOpen()) {
229 std::vector<u8> ttf_bytes(file.GetSize());
230 file.ReadBytes<u8>(ttf_bytes.data(), ttf_bytes.size());
231 FontRegion region{
232 static_cast<u32>(offset + 8),
233 static_cast<u32>(ttf_bytes.size())}; // Font offset and size do not account
234 // for the header
235 EncryptSharedFont(ttf_bytes, *impl->shared_font, offset);
236 impl->shared_font_regions.push_back(region);
237 } else {
238 LOG_WARNING(Service_NS, "Unable to load font: {}", font_ttf);
239 }
240 } else if (using_ttf) {
241 LOG_WARNING(Service_NS, "Unable to find font: {}", font_ttf);
242 }
188 } 243 }
189 const auto font_fp = extracted_romfs->GetFile(font.second); 244 if (using_ttf)
190 if (!font_fp) { 245 return;
191 LOG_ERROR(Service_NS, "{:016X} has no file \"{}\"! Skipping", 246 FileUtil::IOFile file(filepath, "rb");
192 static_cast<u64>(font.first), font.second); 247
193 continue; 248 if (file.IsOpen()) {
249 // Read shared font data
250 ASSERT(file.GetSize() == SHARED_FONT_MEM_SIZE);
251 file.ReadBytes(impl->shared_font->data(), impl->shared_font->size());
252 impl->BuildSharedFontsRawRegions(*impl->shared_font);
253 } else {
254 LOG_WARNING(Service_NS,
255 "Shared Font file missing. Loading open source replacement from memory");
256
257 // clang-format off
258 const std::vector<std::vector<u8>> open_source_shared_fonts_ttf = {
259 {std::begin(FontChineseSimplified), std::end(FontChineseSimplified)},
260 {std::begin(FontChineseTraditional), std::end(FontChineseTraditional)},
261 {std::begin(FontExtendedChineseSimplified), std::end(FontExtendedChineseSimplified)},
262 {std::begin(FontKorean), std::end(FontKorean)},
263 {std::begin(FontNintendoExtended), std::end(FontNintendoExtended)},
264 {std::begin(FontStandard), std::end(FontStandard)},
265 };
266 // clang-format on
267
268 for (const std::vector<u8>& font_ttf : open_source_shared_fonts_ttf) {
269 const FontRegion region{static_cast<u32>(offset + 8),
270 static_cast<u32>(font_ttf.size())};
271 EncryptSharedFont(font_ttf, *impl->shared_font, offset);
272 impl->shared_font_regions.push_back(region);
273 }
194 } 274 }
195 std::vector<u32> font_data_u32(font_fp->GetSize() / sizeof(u32));
196 font_fp->ReadBytes<u32>(font_data_u32.data(), font_fp->GetSize());
197 // We need to be BigEndian as u32s for the xor encryption
198 std::transform(font_data_u32.begin(), font_data_u32.end(), font_data_u32.begin(),
199 Common::swap32);
200 // Font offset and size do not account for the header
201 const FontRegion region{static_cast<u32>(offset + 8),
202 static_cast<u32>((font_data_u32.size() * sizeof(u32)) - 8)};
203 DecryptSharedFont(font_data_u32, *impl->shared_font, offset);
204 impl->shared_font_regions.push_back(region);
205 } 275 }
206} 276}
207 277
diff --git a/src/core/hle/service/ns/pl_u.h b/src/core/hle/service/ns/pl_u.h
index 7e9fe6220..35ca424d2 100644
--- a/src/core/hle/service/ns/pl_u.h
+++ b/src/core/hle/service/ns/pl_u.h
@@ -5,7 +5,6 @@
5#pragma once 5#pragma once
6 6
7#include <memory> 7#include <memory>
8#include "core/hle/kernel/physical_memory.h"
9#include "core/hle/service/service.h" 8#include "core/hle/service/service.h"
10 9
11namespace Service { 10namespace Service {
@@ -16,8 +15,6 @@ class FileSystemController;
16 15
17namespace NS { 16namespace NS {
18 17
19void EncryptSharedFont(const std::vector<u8>& input, Kernel::PhysicalMemory& output);
20
21class PL_U final : public ServiceFramework<PL_U> { 18class PL_U final : public ServiceFramework<PL_U> {
22public: 19public:
23 PL_U(FileSystem::FileSystemController& fsc); 20 PL_U(FileSystem::FileSystemController& fsc);