SharedFontManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using LibHac.Common;
  2. using LibHac.Fs;
  3. using LibHac.FsSystem;
  4. using LibHac.FsSystem.NcaUtils;
  5. using Ryujinx.Common.Logging;
  6. using Ryujinx.HLE.Exceptions;
  7. using Ryujinx.HLE.FileSystem;
  8. using Ryujinx.HLE.FileSystem.Content;
  9. using System;
  10. using System.Buffers.Binary;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using static Ryujinx.HLE.Utilities.FontUtils;
  14. namespace Ryujinx.HLE.HOS.Font
  15. {
  16. class SharedFontManager
  17. {
  18. private Switch _device;
  19. private ulong _physicalAddress;
  20. private struct FontInfo
  21. {
  22. public int Offset;
  23. public int Size;
  24. public FontInfo(int offset, int size)
  25. {
  26. Offset = offset;
  27. Size = size;
  28. }
  29. }
  30. private Dictionary<SharedFontType, FontInfo> _fontData;
  31. public SharedFontManager(Switch device, ulong physicalAddress)
  32. {
  33. _physicalAddress = physicalAddress;
  34. _device = device;
  35. }
  36. public void Initialize(ContentManager contentManager)
  37. {
  38. _fontData?.Clear();
  39. _fontData = null;
  40. }
  41. public void EnsureInitialized(ContentManager contentManager)
  42. {
  43. if (_fontData == null)
  44. {
  45. _device.Memory.ZeroFill(_physicalAddress, Horizon.FontSize);
  46. uint fontOffset = 0;
  47. FontInfo CreateFont(string name)
  48. {
  49. if (contentManager.TryGetFontTitle(name, out long fontTitle) &&
  50. contentManager.TryGetFontFilename(name, out string fontFilename))
  51. {
  52. string contentPath = contentManager.GetInstalledContentPath(fontTitle, StorageId.NandSystem, NcaContentType.Data);
  53. string fontPath = _device.FileSystem.SwitchPathToSystemPath(contentPath);
  54. if (!string.IsNullOrWhiteSpace(fontPath))
  55. {
  56. byte[] data;
  57. using (IStorage ncaFileStream = new LocalStorage(fontPath, FileAccess.Read, FileMode.Open))
  58. {
  59. Nca nca = new Nca(_device.System.KeySet, ncaFileStream);
  60. IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel);
  61. romfs.OpenFile(out IFile fontFile, ("/" + fontFilename).ToU8Span(), OpenMode.Read).ThrowIfFailure();
  62. data = DecryptFont(fontFile.AsStream());
  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.Write(_physicalAddress + fontOffset, data[fontOffset - start]);
  71. }
  72. return info;
  73. }
  74. else
  75. {
  76. if (!contentManager.TryGetSystemTitlesName(fontTitle, out string titleName))
  77. {
  78. titleName = "Unknown";
  79. }
  80. throw new InvalidSystemResourceException($"{titleName} ({fontTitle:x8}) system title not found! This font will not work, provide the system archive to fix this error. (See https://github.com/Ryujinx/Ryujinx#requirements for more information)");
  81. }
  82. }
  83. else
  84. {
  85. throw new ArgumentException($"Unknown font \"{name}\"!");
  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(ulong address, int size)
  107. {
  108. const int decMagic = 0x18029a7f;
  109. const int key = 0x49621806;
  110. int encryptedSize = BinaryPrimitives.ReverseEndianness(size ^ key);
  111. _device.Memory.Write(address + 0, decMagic);
  112. _device.Memory.Write(address + 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. }