SharedFontManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 System;
  11. using System.Buffers.Binary;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using static Ryujinx.HLE.Utilities.FontUtils;
  15. namespace Ryujinx.HLE.HOS.Font
  16. {
  17. class SharedFontManager
  18. {
  19. private readonly Switch _device;
  20. private readonly SharedMemoryStorage _storage;
  21. private struct FontInfo
  22. {
  23. public int Offset;
  24. public int Size;
  25. public FontInfo(int offset, int size)
  26. {
  27. Offset = offset;
  28. Size = size;
  29. }
  30. }
  31. private Dictionary<SharedFontType, FontInfo> _fontData;
  32. public SharedFontManager(Switch device, SharedMemoryStorage storage)
  33. {
  34. _device = device;
  35. _storage = storage;
  36. }
  37. public void Initialize(ContentManager contentManager)
  38. {
  39. _fontData?.Clear();
  40. _fontData = null;
  41. }
  42. public void EnsureInitialized(ContentManager contentManager)
  43. {
  44. if (_fontData == null)
  45. {
  46. _storage.ZeroFill();
  47. uint fontOffset = 0;
  48. FontInfo CreateFont(string name)
  49. {
  50. if (contentManager.TryGetFontTitle(name, out ulong fontTitle) &&
  51. contentManager.TryGetFontFilename(name, out string fontFilename))
  52. {
  53. string contentPath = contentManager.GetInstalledContentPath(fontTitle, StorageId.NandSystem, NcaContentType.Data);
  54. string fontPath = _device.FileSystem.SwitchPathToSystemPath(contentPath);
  55. if (!string.IsNullOrWhiteSpace(fontPath))
  56. {
  57. byte[] data;
  58. using (IStorage ncaFileStream = new LocalStorage(fontPath, FileAccess.Read, FileMode.Open))
  59. {
  60. Nca nca = new Nca(_device.System.KeySet, ncaFileStream);
  61. IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
  62. romfs.OpenFile(out IFile fontFile, ("/" + fontFilename).ToU8Span(), OpenMode.Read).ThrowIfFailure();
  63. data = DecryptFont(fontFile.AsStream());
  64. }
  65. FontInfo info = new FontInfo((int)fontOffset, data.Length);
  66. WriteMagicAndSize(fontOffset, data.Length);
  67. fontOffset += 8;
  68. uint start = fontOffset;
  69. for (; fontOffset - start < data.Length; fontOffset++)
  70. {
  71. _storage.GetRef<byte>(fontOffset) = data[fontOffset - start];
  72. }
  73. return info;
  74. }
  75. else
  76. {
  77. if (!contentManager.TryGetSystemTitlesName(fontTitle, out string titleName))
  78. {
  79. titleName = "Unknown";
  80. }
  81. 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)");
  82. }
  83. }
  84. else
  85. {
  86. throw new ArgumentException($"Unknown font \"{name}\"!");
  87. }
  88. }
  89. _fontData = new Dictionary<SharedFontType, FontInfo>
  90. {
  91. { SharedFontType.JapanUsEurope, CreateFont("FontStandard") },
  92. { SharedFontType.SimplifiedChinese, CreateFont("FontChineseSimplified") },
  93. { SharedFontType.SimplifiedChineseEx, CreateFont("FontExtendedChineseSimplified") },
  94. { SharedFontType.TraditionalChinese, CreateFont("FontChineseTraditional") },
  95. { SharedFontType.Korean, CreateFont("FontKorean") },
  96. { SharedFontType.NintendoEx, CreateFont("FontNintendoExtended") }
  97. };
  98. if (fontOffset > Horizon.FontSize)
  99. {
  100. throw new InvalidSystemResourceException(
  101. $"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. " +
  103. $"(actual size: {fontOffset} bytes).");
  104. }
  105. }
  106. }
  107. private void WriteMagicAndSize(ulong offset, int size)
  108. {
  109. const int decMagic = 0x18029a7f;
  110. const int key = 0x49621806;
  111. int encryptedSize = BinaryPrimitives.ReverseEndianness(size ^ key);
  112. _storage.GetRef<int>(offset + 0) = decMagic;
  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. }
  126. }