SharedFontManager.cs 6.7 KB

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