SharedFontManager.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using LibHac.Common;
  2. using LibHac.Fs;
  3. using LibHac.Fs.Fsa;
  4. using LibHac.FsSystem;
  5. using LibHac.FsSystem.NcaUtils;
  6. using Ryujinx.HLE.Exceptions;
  7. using Ryujinx.HLE.FileSystem;
  8. using Ryujinx.HLE.FileSystem.Content;
  9. using Ryujinx.HLE.HOS.Kernel.Memory;
  10. using Ryujinx.HLE.HOS.Services.Sdb.Pl.Types;
  11. using System;
  12. using System.Buffers.Binary;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. namespace Ryujinx.HLE.HOS.Services.Sdb.Pl
  16. {
  17. class SharedFontManager
  18. {
  19. private static readonly uint FontKey = 0x06186249;
  20. private static readonly uint BFTTFMagic = 0x18029a7f;
  21. private readonly Switch _device;
  22. private readonly SharedMemoryStorage _storage;
  23. private struct FontInfo
  24. {
  25. public int Offset;
  26. public int Size;
  27. public FontInfo(int offset, int size)
  28. {
  29. Offset = offset;
  30. Size = size;
  31. }
  32. }
  33. private Dictionary<SharedFontType, FontInfo> _fontData;
  34. public SharedFontManager(Switch device, SharedMemoryStorage storage)
  35. {
  36. _device = device;
  37. _storage = storage;
  38. }
  39. public void Initialize()
  40. {
  41. _fontData?.Clear();
  42. _fontData = null;
  43. }
  44. public void EnsureInitialized(ContentManager contentManager)
  45. {
  46. if (_fontData == null)
  47. {
  48. _storage.ZeroFill();
  49. uint fontOffset = 0;
  50. FontInfo CreateFont(string name)
  51. {
  52. if (contentManager.TryGetFontTitle(name, out ulong fontTitle) && contentManager.TryGetFontFilename(name, out string fontFilename))
  53. {
  54. string contentPath = contentManager.GetInstalledContentPath(fontTitle, StorageId.NandSystem, NcaContentType.Data);
  55. string fontPath = _device.FileSystem.SwitchPathToSystemPath(contentPath);
  56. if (!string.IsNullOrWhiteSpace(fontPath))
  57. {
  58. byte[] data;
  59. using (IStorage ncaFileStream = new LocalStorage(fontPath, FileAccess.Read, FileMode.Open))
  60. {
  61. Nca nca = new Nca(_device.System.KeySet, ncaFileStream);
  62. IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
  63. using var fontFile = new UniqueRef<IFile>();
  64. romfs.OpenFile(ref fontFile.Ref(), ("/" + fontFilename).ToU8Span(), OpenMode.Read).ThrowIfFailure();
  65. data = DecryptFont(fontFile.Get.AsStream());
  66. }
  67. FontInfo info = new FontInfo((int)fontOffset, data.Length);
  68. WriteMagicAndSize(fontOffset, data.Length);
  69. fontOffset += 8;
  70. uint start = fontOffset;
  71. for (; fontOffset - start < data.Length; fontOffset++)
  72. {
  73. _storage.GetRef<byte>(fontOffset) = data[fontOffset - start];
  74. }
  75. return info;
  76. }
  77. else
  78. {
  79. if (!contentManager.TryGetSystemTitlesName(fontTitle, out string titleName))
  80. {
  81. titleName = "Unknown";
  82. }
  83. throw new InvalidSystemResourceException($"{titleName} ({fontTitle:x8}) system title not found! This font will not work, provide the system archive to fix this error. (See https://github.com/Ryujinx/Ryujinx#requirements for more information)");
  84. }
  85. }
  86. else
  87. {
  88. throw new ArgumentException($"Unknown font \"{name}\"!");
  89. }
  90. }
  91. _fontData = new Dictionary<SharedFontType, FontInfo>
  92. {
  93. { SharedFontType.JapanUsEurope, CreateFont("FontStandard") },
  94. { SharedFontType.SimplifiedChinese, CreateFont("FontChineseSimplified") },
  95. { SharedFontType.SimplifiedChineseEx, CreateFont("FontExtendedChineseSimplified") },
  96. { SharedFontType.TraditionalChinese, CreateFont("FontChineseTraditional") },
  97. { SharedFontType.Korean, CreateFont("FontKorean") },
  98. { SharedFontType.NintendoEx, CreateFont("FontNintendoExtended") }
  99. };
  100. if (fontOffset > Horizon.FontSize)
  101. {
  102. throw new InvalidSystemResourceException("The sum of all fonts size exceed the shared memory size. " +
  103. $"Please make sure that the fonts don't exceed {Horizon.FontSize} bytes in total. (actual size: {fontOffset} bytes).");
  104. }
  105. }
  106. }
  107. private void WriteMagicAndSize(ulong offset, int size)
  108. {
  109. const int key = 0x49621806;
  110. int encryptedSize = BinaryPrimitives.ReverseEndianness(size ^ key);
  111. _storage.GetRef<int>(offset + 0) = (int)BFTTFMagic;
  112. _storage.GetRef<int>(offset + 4) = encryptedSize;
  113. }
  114. public int GetFontSize(SharedFontType fontType)
  115. {
  116. EnsureInitialized(_device.System.ContentManager);
  117. return _fontData[fontType].Size;
  118. }
  119. public int GetSharedMemoryAddressOffset(SharedFontType fontType)
  120. {
  121. EnsureInitialized(_device.System.ContentManager);
  122. return _fontData[fontType].Offset + 8;
  123. }
  124. private static byte[] DecryptFont(Stream bfttfStream)
  125. {
  126. static uint KXor(uint data) => data ^ FontKey;
  127. using (BinaryReader reader = new BinaryReader(bfttfStream))
  128. using (MemoryStream ttfStream = new MemoryStream())
  129. using (BinaryWriter output = new BinaryWriter(ttfStream))
  130. {
  131. if (KXor(reader.ReadUInt32()) != BFTTFMagic)
  132. {
  133. throw new InvalidDataException("Error: Input file is not in BFTTF format!");
  134. }
  135. bfttfStream.Position += 4;
  136. for (int i = 0; i < (bfttfStream.Length - 8) / 4; i++)
  137. {
  138. output.Write(KXor(reader.ReadUInt32()));
  139. }
  140. return ttfStream.ToArray();
  141. }
  142. }
  143. }
  144. }