SharedFontManager.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using LibHac.Fs;
  2. using LibHac.Fs.NcaUtils;
  3. using Ryujinx.Common;
  4. using Ryujinx.HLE.FileSystem;
  5. using Ryujinx.HLE.FileSystem.Content;
  6. using Ryujinx.HLE.Resource;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using static Ryujinx.HLE.Utilities.FontUtils;
  10. namespace Ryujinx.HLE.HOS.Font
  11. {
  12. class SharedFontManager
  13. {
  14. private Switch _device;
  15. private long _physicalAddress;
  16. private string _fontsPath;
  17. private struct FontInfo
  18. {
  19. public int Offset;
  20. public int Size;
  21. public FontInfo(int offset, int size)
  22. {
  23. Offset = offset;
  24. Size = size;
  25. }
  26. }
  27. private Dictionary<SharedFontType, FontInfo> _fontData;
  28. public SharedFontManager(Switch device, long physicalAddress)
  29. {
  30. _physicalAddress = physicalAddress;
  31. _device = device;
  32. _fontsPath = Path.Combine(device.FileSystem.GetSystemPath(), "fonts");
  33. }
  34. public void EnsureInitialized(ContentManager contentManager)
  35. {
  36. if (_fontData == null)
  37. {
  38. _device.Memory.FillWithZeros(_physicalAddress, Horizon.FontSize);
  39. uint fontOffset = 0;
  40. FontInfo CreateFont(string name)
  41. {
  42. if (contentManager.TryGetFontTitle(name, out long fontTitle) &&
  43. contentManager.TryGetFontFilename(name, out string fontFilename))
  44. {
  45. string contentPath = contentManager.GetInstalledContentPath(fontTitle, StorageId.NandSystem, ContentType.Data);
  46. string fontPath = _device.FileSystem.SwitchPathToSystemPath(contentPath);
  47. if (!string.IsNullOrWhiteSpace(fontPath))
  48. {
  49. byte[] data;
  50. using (IStorage ncaFileStream = new LocalStorage(fontPath, FileAccess.Read, FileMode.Open))
  51. {
  52. Nca nca = new Nca(_device.System.KeySet, ncaFileStream);
  53. IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
  54. Stream fontFile = romfs.OpenFile(fontFilename, OpenMode.Read).AsStream();
  55. data = DecryptFont(fontFile);
  56. }
  57. FontInfo info = new FontInfo((int)fontOffset, data.Length);
  58. WriteMagicAndSize(_physicalAddress + fontOffset, data.Length);
  59. fontOffset += 8;
  60. uint start = fontOffset;
  61. for (; fontOffset - start < data.Length; fontOffset++)
  62. {
  63. _device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]);
  64. }
  65. return info;
  66. }
  67. }
  68. string fontFilePath = Path.Combine(_fontsPath, name + ".ttf");
  69. if (File.Exists(fontFilePath))
  70. {
  71. byte[] data = File.ReadAllBytes(fontFilePath);
  72. FontInfo info = new FontInfo((int)fontOffset, data.Length);
  73. WriteMagicAndSize(_physicalAddress + fontOffset, data.Length);
  74. fontOffset += 8;
  75. uint start = fontOffset;
  76. for (; fontOffset - start < data.Length; fontOffset++)
  77. {
  78. _device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]);
  79. }
  80. return info;
  81. }
  82. else
  83. {
  84. throw new InvalidSystemResourceException($"Font \"{name}.ttf\" not found. Please provide it in \"{_fontsPath}\".");
  85. }
  86. }
  87. _fontData = new Dictionary<SharedFontType, FontInfo>
  88. {
  89. { SharedFontType.JapanUsEurope, CreateFont("FontStandard") },
  90. { SharedFontType.SimplifiedChinese, CreateFont("FontChineseSimplified") },
  91. { SharedFontType.SimplifiedChineseEx, CreateFont("FontExtendedChineseSimplified") },
  92. { SharedFontType.TraditionalChinese, CreateFont("FontChineseTraditional") },
  93. { SharedFontType.Korean, CreateFont("FontKorean") },
  94. { SharedFontType.NintendoEx, CreateFont("FontNintendoExtended") }
  95. };
  96. if (fontOffset > Horizon.FontSize)
  97. {
  98. throw new InvalidSystemResourceException(
  99. $"The sum of all fonts size exceed the shared memory size. " +
  100. $"Please make sure that the fonts don't exceed {Horizon.FontSize} bytes in total. " +
  101. $"(actual size: {fontOffset} bytes).");
  102. }
  103. }
  104. }
  105. private void WriteMagicAndSize(long position, int size)
  106. {
  107. const int decMagic = 0x18029a7f;
  108. const int key = 0x49621806;
  109. int encryptedSize = EndianSwap.Swap32(size ^ key);
  110. _device.Memory.WriteInt32(position + 0, decMagic);
  111. _device.Memory.WriteInt32(position + 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. }
  124. }