SharedFontManager.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using LibHac.Fs;
  2. using LibHac.FsSystem;
  3. using LibHac.FsSystem.NcaUtils;
  4. using Ryujinx.Common;
  5. using Ryujinx.HLE.Exceptions;
  6. using Ryujinx.HLE.FileSystem;
  7. using Ryujinx.HLE.FileSystem.Content;
  8. using System.Buffers.Binary;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using static Ryujinx.HLE.Utilities.FontUtils;
  12. namespace Ryujinx.HLE.HOS.Font
  13. {
  14. class SharedFontManager
  15. {
  16. private Switch _device;
  17. private long _physicalAddress;
  18. private string _fontsPath;
  19. private struct FontInfo
  20. {
  21. public int Offset;
  22. public int Size;
  23. public FontInfo(int offset, int size)
  24. {
  25. Offset = offset;
  26. Size = size;
  27. }
  28. }
  29. private Dictionary<SharedFontType, FontInfo> _fontData;
  30. public SharedFontManager(Switch device, long physicalAddress)
  31. {
  32. _physicalAddress = physicalAddress;
  33. _device = device;
  34. _fontsPath = Path.Combine(device.FileSystem.GetSystemPath(), "fonts");
  35. }
  36. public void Initialize(ContentManager contentManager, bool ignoreMissingFonts)
  37. {
  38. _fontData?.Clear();
  39. _fontData = null;
  40. EnsureInitialized(contentManager, ignoreMissingFonts);
  41. }
  42. public void EnsureInitialized(ContentManager contentManager, bool ignoreMissingFonts)
  43. {
  44. if (_fontData == null)
  45. {
  46. _device.Memory.FillWithZeros(_physicalAddress, Horizon.FontSize);
  47. uint fontOffset = 0;
  48. FontInfo CreateFont(string name)
  49. {
  50. if (contentManager.TryGetFontTitle(name, out long 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, OpenMode.Read).ThrowIfFailure();
  63. data = DecryptFont(fontFile.AsStream());
  64. }
  65. FontInfo info = new FontInfo((int)fontOffset, data.Length);
  66. WriteMagicAndSize(_physicalAddress + fontOffset, data.Length);
  67. fontOffset += 8;
  68. uint start = fontOffset;
  69. for (; fontOffset - start < data.Length; fontOffset++)
  70. {
  71. _device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]);
  72. }
  73. return info;
  74. }
  75. }
  76. string fontFilePath = Path.Combine(_fontsPath, name + ".ttf");
  77. if (File.Exists(fontFilePath))
  78. {
  79. byte[] data = File.ReadAllBytes(fontFilePath);
  80. FontInfo info = new FontInfo((int)fontOffset, data.Length);
  81. WriteMagicAndSize(_physicalAddress + fontOffset, data.Length);
  82. fontOffset += 8;
  83. uint start = fontOffset;
  84. for (; fontOffset - start < data.Length; fontOffset++)
  85. {
  86. _device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]);
  87. }
  88. return info;
  89. }
  90. else if (!ignoreMissingFonts)
  91. {
  92. throw new InvalidSystemResourceException($"Font \"{name}.ttf\" not found. Please provide it in \"{_fontsPath}\".");
  93. }
  94. return new FontInfo();
  95. }
  96. _fontData = new Dictionary<SharedFontType, FontInfo>
  97. {
  98. { SharedFontType.JapanUsEurope, CreateFont("FontStandard") },
  99. { SharedFontType.SimplifiedChinese, CreateFont("FontChineseSimplified") },
  100. { SharedFontType.SimplifiedChineseEx, CreateFont("FontExtendedChineseSimplified") },
  101. { SharedFontType.TraditionalChinese, CreateFont("FontChineseTraditional") },
  102. { SharedFontType.Korean, CreateFont("FontKorean") },
  103. { SharedFontType.NintendoEx, CreateFont("FontNintendoExtended") }
  104. };
  105. if (fontOffset > Horizon.FontSize && !ignoreMissingFonts)
  106. {
  107. throw new InvalidSystemResourceException(
  108. $"The sum of all fonts size exceed the shared memory size. " +
  109. $"Please make sure that the fonts don't exceed {Horizon.FontSize} bytes in total. " +
  110. $"(actual size: {fontOffset} bytes).");
  111. }
  112. }
  113. }
  114. private void WriteMagicAndSize(long position, int size)
  115. {
  116. const int decMagic = 0x18029a7f;
  117. const int key = 0x49621806;
  118. int encryptedSize = BinaryPrimitives.ReverseEndianness(size ^ key);
  119. _device.Memory.WriteInt32(position + 0, decMagic);
  120. _device.Memory.WriteInt32(position + 4, encryptedSize);
  121. }
  122. public int GetFontSize(SharedFontType fontType)
  123. {
  124. EnsureInitialized(_device.System.ContentManager, false);
  125. return _fontData[fontType].Size;
  126. }
  127. public int GetSharedMemoryAddressOffset(SharedFontType fontType)
  128. {
  129. EnsureInitialized(_device.System.ContentManager, false);
  130. return _fontData[fontType].Offset + 8;
  131. }
  132. }
  133. }