SharedFontManager.cs 6.8 KB

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