SharedFontManager.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using LibHac;
  2. using LibHac.IO;
  3. using Ryujinx.HLE.FileSystem;
  4. using Ryujinx.HLE.FileSystem.Content;
  5. using Ryujinx.HLE.Resource;
  6. using Ryujinx.HLE.Utilities;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using static Ryujinx.HLE.Utilities.FontUtils;
  11. namespace Ryujinx.HLE.HOS.Font
  12. {
  13. class SharedFontManager
  14. {
  15. private Switch _device;
  16. private long _physicalAddress;
  17. private string _fontsPath;
  18. private struct FontInfo
  19. {
  20. public int Offset;
  21. public int Size;
  22. public FontInfo(int offset, int size)
  23. {
  24. Offset = offset;
  25. Size = size;
  26. }
  27. }
  28. private Dictionary<SharedFontType, FontInfo> _fontData;
  29. public SharedFontManager(Switch device, long physicalAddress)
  30. {
  31. _physicalAddress = physicalAddress;
  32. _device = device;
  33. _fontsPath = Path.Combine(device.FileSystem.GetSystemPath(), "fonts");
  34. }
  35. public void EnsureInitialized(ContentManager contentManager)
  36. {
  37. if (_fontData == null)
  38. {
  39. _device.Memory.FillWithZeros(_physicalAddress, Horizon.FontSize);
  40. uint fontOffset = 0;
  41. FontInfo CreateFont(string name)
  42. {
  43. if (contentManager.TryGetFontTitle(name, out long fontTitle))
  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. int fileIndex = 0;
  50. //Use second file in Chinese Font title for standard
  51. if(name == "FontChineseSimplified")
  52. {
  53. fileIndex = 1;
  54. }
  55. byte[] data;
  56. using (FileStream ncaFileStream = new FileStream(fontPath, FileMode.Open, FileAccess.Read))
  57. {
  58. Nca nca = new Nca(_device.System.KeySet, ncaFileStream.AsStorage(), false);
  59. NcaSection romfsSection = nca.Sections.FirstOrDefault(x => x?.Type == SectionType.Romfs);
  60. Romfs romfs = new Romfs(nca.OpenSection(romfsSection.SectionNum, false, _device.System.FsIntegrityCheckLevel, false));
  61. Stream fontFile = romfs.OpenFile(romfs.Files[fileIndex]).AsStream();
  62. data = DecryptFont(fontFile);
  63. }
  64. FontInfo info = new FontInfo((int)fontOffset, data.Length);
  65. WriteMagicAndSize(_physicalAddress + fontOffset, data.Length);
  66. fontOffset += 8;
  67. uint start = fontOffset;
  68. for (; fontOffset - start < data.Length; fontOffset++)
  69. {
  70. _device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]);
  71. }
  72. return info;
  73. }
  74. }
  75. string fontFilePath = Path.Combine(_fontsPath, name + ".ttf");
  76. if (File.Exists(fontFilePath))
  77. {
  78. byte[] data = File.ReadAllBytes(fontFilePath);
  79. FontInfo info = new FontInfo((int)fontOffset, data.Length);
  80. WriteMagicAndSize(_physicalAddress + fontOffset, data.Length);
  81. fontOffset += 8;
  82. uint start = fontOffset;
  83. for (; fontOffset - start < data.Length; fontOffset++)
  84. {
  85. _device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]);
  86. }
  87. return info;
  88. }
  89. else
  90. {
  91. throw new InvalidSystemResourceException($"Font \"{name}.ttf\" not found. Please provide it in \"{_fontsPath}\".");
  92. }
  93. }
  94. _fontData = new Dictionary<SharedFontType, FontInfo>
  95. {
  96. { SharedFontType.JapanUsEurope, CreateFont("FontStandard") },
  97. { SharedFontType.SimplifiedChinese, CreateFont("FontChineseSimplified") },
  98. { SharedFontType.SimplifiedChineseEx, CreateFont("FontExtendedChineseSimplified") },
  99. { SharedFontType.TraditionalChinese, CreateFont("FontChineseTraditional") },
  100. { SharedFontType.Korean, CreateFont("FontKorean") },
  101. { SharedFontType.NintendoEx, CreateFont("FontNintendoExtended") }
  102. };
  103. if (fontOffset > Horizon.FontSize)
  104. {
  105. throw new InvalidSystemResourceException(
  106. $"The sum of all fonts size exceed the shared memory size. " +
  107. $"Please make sure that the fonts don't exceed {Horizon.FontSize} bytes in total. " +
  108. $"(actual size: {fontOffset} bytes).");
  109. }
  110. }
  111. }
  112. private void WriteMagicAndSize(long position, int size)
  113. {
  114. const int decMagic = 0x18029a7f;
  115. const int key = 0x49621806;
  116. int encryptedSize = EndianSwap.Swap32(size ^ key);
  117. _device.Memory.WriteInt32(position + 0, decMagic);
  118. _device.Memory.WriteInt32(position + 4, encryptedSize);
  119. }
  120. public int GetFontSize(SharedFontType fontType)
  121. {
  122. EnsureInitialized(_device.System.ContentManager);
  123. return _fontData[fontType].Size;
  124. }
  125. public int GetSharedMemoryAddressOffset(SharedFontType fontType)
  126. {
  127. EnsureInitialized(_device.System.ContentManager);
  128. return _fontData[fontType].Offset + 8;
  129. }
  130. }
  131. }