SharedFontManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.Collections.Generic;
  9. using System.IO;
  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. contentManager.TryGetFontFilename(name, out string fontFilename))
  45. {
  46. string contentPath = contentManager.GetInstalledContentPath(fontTitle, StorageId.NandSystem, NcaContentType.Data);
  47. string fontPath = _device.FileSystem.SwitchPathToSystemPath(contentPath);
  48. if (!string.IsNullOrWhiteSpace(fontPath))
  49. {
  50. byte[] data;
  51. using (IStorage ncaFileStream = new LocalStorage(fontPath, FileAccess.Read, FileMode.Open))
  52. {
  53. Nca nca = new Nca(_device.System.KeySet, ncaFileStream);
  54. IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
  55. romfs.OpenFile(out IFile fontFile, "/" + fontFilename, OpenMode.Read).ThrowIfFailure();
  56. data = DecryptFont(fontFile.AsStream());
  57. }
  58. FontInfo info = new FontInfo((int)fontOffset, data.Length);
  59. WriteMagicAndSize(_physicalAddress + fontOffset, data.Length);
  60. fontOffset += 8;
  61. uint start = fontOffset;
  62. for (; fontOffset - start < data.Length; fontOffset++)
  63. {
  64. _device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]);
  65. }
  66. return info;
  67. }
  68. }
  69. string fontFilePath = Path.Combine(_fontsPath, name + ".ttf");
  70. if (File.Exists(fontFilePath))
  71. {
  72. byte[] data = File.ReadAllBytes(fontFilePath);
  73. FontInfo info = new FontInfo((int)fontOffset, data.Length);
  74. WriteMagicAndSize(_physicalAddress + fontOffset, data.Length);
  75. fontOffset += 8;
  76. uint start = fontOffset;
  77. for (; fontOffset - start < data.Length; fontOffset++)
  78. {
  79. _device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]);
  80. }
  81. return info;
  82. }
  83. else
  84. {
  85. throw new InvalidSystemResourceException($"Font \"{name}.ttf\" not found. Please provide it in \"{_fontsPath}\".");
  86. }
  87. }
  88. _fontData = new Dictionary<SharedFontType, FontInfo>
  89. {
  90. { SharedFontType.JapanUsEurope, CreateFont("FontStandard") },
  91. { SharedFontType.SimplifiedChinese, CreateFont("FontChineseSimplified") },
  92. { SharedFontType.SimplifiedChineseEx, CreateFont("FontExtendedChineseSimplified") },
  93. { SharedFontType.TraditionalChinese, CreateFont("FontChineseTraditional") },
  94. { SharedFontType.Korean, CreateFont("FontKorean") },
  95. { SharedFontType.NintendoEx, CreateFont("FontNintendoExtended") }
  96. };
  97. if (fontOffset > Horizon.FontSize)
  98. {
  99. throw new InvalidSystemResourceException(
  100. $"The sum of all fonts size exceed the shared memory size. " +
  101. $"Please make sure that the fonts don't exceed {Horizon.FontSize} bytes in total. " +
  102. $"(actual size: {fontOffset} bytes).");
  103. }
  104. }
  105. }
  106. private void WriteMagicAndSize(long position, int size)
  107. {
  108. const int decMagic = 0x18029a7f;
  109. const int key = 0x49621806;
  110. int encryptedSize = EndianSwap.Swap32(size ^ key);
  111. _device.Memory.WriteInt32(position + 0, decMagic);
  112. _device.Memory.WriteInt32(position + 4, encryptedSize);
  113. }
  114. public int GetFontSize(SharedFontType fontType)
  115. {
  116. EnsureInitialized(_device.System.ContentManager);
  117. return _fontData[fontType].Size;
  118. }
  119. public int GetSharedMemoryAddressOffset(SharedFontType fontType)
  120. {
  121. EnsureInitialized(_device.System.ContentManager);
  122. return _fontData[fontType].Offset + 8;
  123. }
  124. }
  125. }