SharedFontManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 EnsureInitialized(ContentManager contentManager)
  37. {
  38. if (_fontData == null)
  39. {
  40. _device.Memory.FillWithZeros(_physicalAddress, Horizon.FontSize);
  41. uint fontOffset = 0;
  42. FontInfo CreateFont(string name)
  43. {
  44. if (contentManager.TryGetFontTitle(name, out long fontTitle) &&
  45. contentManager.TryGetFontFilename(name, out string fontFilename))
  46. {
  47. string contentPath = contentManager.GetInstalledContentPath(fontTitle, StorageId.NandSystem, NcaContentType.Data);
  48. string fontPath = _device.FileSystem.SwitchPathToSystemPath(contentPath);
  49. if (!string.IsNullOrWhiteSpace(fontPath))
  50. {
  51. byte[] data;
  52. using (IStorage ncaFileStream = new LocalStorage(fontPath, FileAccess.Read, FileMode.Open))
  53. {
  54. Nca nca = new Nca(_device.System.KeySet, ncaFileStream);
  55. IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
  56. romfs.OpenFile(out IFile fontFile, "/" + fontFilename, OpenMode.Read).ThrowIfFailure();
  57. data = DecryptFont(fontFile.AsStream());
  58. }
  59. FontInfo info = new FontInfo((int)fontOffset, data.Length);
  60. WriteMagicAndSize(_physicalAddress + fontOffset, data.Length);
  61. fontOffset += 8;
  62. uint start = fontOffset;
  63. for (; fontOffset - start < data.Length; fontOffset++)
  64. {
  65. _device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]);
  66. }
  67. return info;
  68. }
  69. }
  70. string fontFilePath = Path.Combine(_fontsPath, name + ".ttf");
  71. if (File.Exists(fontFilePath))
  72. {
  73. byte[] data = File.ReadAllBytes(fontFilePath);
  74. FontInfo info = new FontInfo((int)fontOffset, data.Length);
  75. WriteMagicAndSize(_physicalAddress + fontOffset, data.Length);
  76. fontOffset += 8;
  77. uint start = fontOffset;
  78. for (; fontOffset - start < data.Length; fontOffset++)
  79. {
  80. _device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]);
  81. }
  82. return info;
  83. }
  84. else
  85. {
  86. throw new InvalidSystemResourceException($"Font \"{name}.ttf\" not found. Please provide it in \"{_fontsPath}\".");
  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(long position, int size)
  108. {
  109. const int decMagic = 0x18029a7f;
  110. const int key = 0x49621806;
  111. int encryptedSize = BinaryPrimitives.ReverseEndianness(size ^ key);
  112. _device.Memory.WriteInt32(position + 0, decMagic);
  113. _device.Memory.WriteInt32(position + 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. }