SharedFontManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. romfs.OpenFile(out IFile fontFile, ("/" + fontFilename).ToU8Span(), OpenMode.Read).ThrowIfFailure();
  64. data = DecryptFont(fontFile.AsStream());
  65. }
  66. FontInfo info = new FontInfo((int)fontOffset, data.Length);
  67. WriteMagicAndSize(fontOffset, data.Length);
  68. fontOffset += 8;
  69. uint start = fontOffset;
  70. for (; fontOffset - start < data.Length; fontOffset++)
  71. {
  72. _storage.GetRef<byte>(fontOffset) = data[fontOffset - start];
  73. }
  74. return info;
  75. }
  76. else
  77. {
  78. if (!contentManager.TryGetSystemTitlesName(fontTitle, out string titleName))
  79. {
  80. titleName = "Unknown";
  81. }
  82. 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)");
  83. }
  84. }
  85. else
  86. {
  87. throw new ArgumentException($"Unknown font \"{name}\"!");
  88. }
  89. }
  90. _fontData = new Dictionary<SharedFontType, FontInfo>
  91. {
  92. { SharedFontType.JapanUsEurope, CreateFont("FontStandard") },
  93. { SharedFontType.SimplifiedChinese, CreateFont("FontChineseSimplified") },
  94. { SharedFontType.SimplifiedChineseEx, CreateFont("FontExtendedChineseSimplified") },
  95. { SharedFontType.TraditionalChinese, CreateFont("FontChineseTraditional") },
  96. { SharedFontType.Korean, CreateFont("FontKorean") },
  97. { SharedFontType.NintendoEx, CreateFont("FontNintendoExtended") }
  98. };
  99. if (fontOffset > Horizon.FontSize)
  100. {
  101. throw new InvalidSystemResourceException("The sum of all fonts size exceed the shared memory size. " +
  102. $"Please make sure that the fonts don't exceed {Horizon.FontSize} bytes in total. (actual size: {fontOffset} bytes).");
  103. }
  104. }
  105. }
  106. private void WriteMagicAndSize(ulong offset, int size)
  107. {
  108. const int key = 0x49621806;
  109. int encryptedSize = BinaryPrimitives.ReverseEndianness(size ^ key);
  110. _storage.GetRef<int>(offset + 0) = (int)BFTTFMagic;
  111. _storage.GetRef<int>(offset + 4) = encryptedSize;
  112. }
  113. public int GetFontSize(SharedFontType fontType)
  114. {
  115. EnsureInitialized(_device.System.ContentManager);
  116. return _fontData[fontType].Size;
  117. }
  118. public int GetSharedMemoryAddressOffset(SharedFontType fontType)
  119. {
  120. EnsureInitialized(_device.System.ContentManager);
  121. return _fontData[fontType].Offset + 8;
  122. }
  123. private static byte[] DecryptFont(Stream bfttfStream)
  124. {
  125. static uint KXor(uint data) => data ^ FontKey;
  126. using (BinaryReader reader = new BinaryReader(bfttfStream))
  127. using (MemoryStream ttfStream = new MemoryStream())
  128. using (BinaryWriter output = new BinaryWriter(ttfStream))
  129. {
  130. if (KXor(reader.ReadUInt32()) != BFTTFMagic)
  131. {
  132. throw new InvalidDataException("Error: Input file is not in BFTTF format!");
  133. }
  134. bfttfStream.Position += 4;
  135. for (int i = 0; i < (bfttfStream.Length - 8) / 4; i++)
  136. {
  137. output.Write(KXor(reader.ReadUInt32()));
  138. }
  139. return ttfStream.ToArray();
  140. }
  141. }
  142. }
  143. }