2018-11-28 17:18:09 -05:00
|
|
|
using LibHac;
|
2018-11-18 14:37:41 -05:00
|
|
|
using Ryujinx.HLE.FileSystem;
|
|
|
|
using Ryujinx.HLE.FileSystem.Content;
|
2018-08-15 14:59:51 -04:00
|
|
|
using Ryujinx.HLE.Resource;
|
2018-08-16 19:47:36 -04:00
|
|
|
using Ryujinx.HLE.Utilities;
|
2018-08-15 14:59:51 -04:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2018-11-18 14:37:41 -05:00
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using static Ryujinx.HLE.Utilities.FontUtils;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-08-16 19:47:36 -04:00
|
|
|
namespace Ryujinx.HLE.HOS.Font
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
|
|
|
class SharedFontManager
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
private Switch Device;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private long PhysicalAddress;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private string FontsPath;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
private struct FontInfo
|
|
|
|
{
|
|
|
|
public int Offset;
|
|
|
|
public int Size;
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public FontInfo(int Offset, int Size)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
this.Offset = Offset;
|
|
|
|
this.Size = Size;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private Dictionary<SharedFontType, FontInfo> FontData;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public SharedFontManager(Switch Device, long PhysicalAddress)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
this.PhysicalAddress = PhysicalAddress;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
this.Device = Device;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
FontsPath = Path.Combine(Device.FileSystem.GetSystemPath(), "fonts");
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public void EnsureInitialized(ContentManager ContentManager)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
if (FontData == null)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Device.Memory.FillWithZeros(PhysicalAddress, Horizon.FontSize);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
uint FontOffset = 0;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
FontInfo CreateFont(string Name)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
if (ContentManager.TryGetFontTitle(Name, out long FontTitle))
|
2018-11-18 14:37:41 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
string ContentPath = ContentManager.GetInstalledContentPath(FontTitle, StorageId.NandSystem, ContentType.Data);
|
|
|
|
string FontPath = Device.FileSystem.SwitchPathToSystemPath(ContentPath);
|
2018-11-18 14:37:41 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (!string.IsNullOrWhiteSpace(FontPath))
|
2018-11-18 14:37:41 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
int FileIndex = 0;
|
2018-11-18 14:37:41 -05:00
|
|
|
|
|
|
|
//Use second file in Chinese Font title for standard
|
2018-12-04 19:52:39 -05:00
|
|
|
if(Name == "FontChineseSimplified")
|
2018-11-18 14:37:41 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
FileIndex = 1;
|
2018-11-18 14:37:41 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
FileStream NcaFileStream = new FileStream(FontPath, FileMode.Open, FileAccess.Read);
|
|
|
|
Nca Nca = new Nca(Device.System.KeySet, NcaFileStream, false);
|
|
|
|
NcaSection RomfsSection = Nca.Sections.FirstOrDefault(x => x?.Type == SectionType.Romfs);
|
|
|
|
Romfs Romfs = new Romfs(Nca.OpenSection(RomfsSection.SectionNum, false, Device.System.FsIntegrityCheckLevel));
|
|
|
|
Stream FontFile = Romfs.OpenFile(Romfs.Files[FileIndex]);
|
2018-11-18 14:37:41 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
byte[] Data = DecryptFont(FontFile);
|
2018-11-18 14:37:41 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
FontInfo Info = new FontInfo((int)FontOffset, Data.Length);
|
2018-11-18 14:37:41 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
WriteMagicAndSize(PhysicalAddress + FontOffset, Data.Length);
|
2018-11-18 14:37:41 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
FontOffset += 8;
|
2018-11-18 14:37:41 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
uint Start = FontOffset;
|
2018-11-18 14:37:41 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
for (; FontOffset - Start < Data.Length; FontOffset++)
|
2018-11-18 14:37:41 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Device.Memory.WriteByte(PhysicalAddress + FontOffset, Data[FontOffset - Start]);
|
2018-11-18 14:37:41 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
NcaFileStream.Dispose();
|
|
|
|
Nca.Dispose();
|
2018-11-18 14:37:41 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return Info;
|
2018-11-18 14:37:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
string FontFilePath = Path.Combine(FontsPath, Name + ".ttf");
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (File.Exists(FontFilePath))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
byte[] Data = File.ReadAllBytes(FontFilePath);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
FontInfo Info = new FontInfo((int)FontOffset, Data.Length);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
WriteMagicAndSize(PhysicalAddress + FontOffset, Data.Length);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
FontOffset += 8;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
uint Start = FontOffset;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
for (; FontOffset - Start < Data.Length; FontOffset++)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Device.Memory.WriteByte(PhysicalAddress + FontOffset, Data[FontOffset - Start]);
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return Info;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
throw new InvalidSystemResourceException($"Font \"{Name}.ttf\" not found. Please provide it in \"{FontsPath}\".");
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
FontData = new Dictionary<SharedFontType, FontInfo>()
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
|
|
|
{ SharedFontType.JapanUsEurope, CreateFont("FontStandard") },
|
|
|
|
{ SharedFontType.SimplifiedChinese, CreateFont("FontChineseSimplified") },
|
|
|
|
{ SharedFontType.SimplifiedChineseEx, CreateFont("FontExtendedChineseSimplified") },
|
|
|
|
{ SharedFontType.TraditionalChinese, CreateFont("FontChineseTraditional") },
|
|
|
|
{ SharedFontType.Korean, CreateFont("FontKorean") },
|
|
|
|
{ SharedFontType.NintendoEx, CreateFont("FontNintendoExtended") }
|
|
|
|
};
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (FontOffset > Horizon.FontSize)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
|
|
|
throw new InvalidSystemResourceException(
|
|
|
|
$"The sum of all fonts size exceed the shared memory size. " +
|
|
|
|
$"Please make sure that the fonts don't exceed {Horizon.FontSize} bytes in total. " +
|
2018-12-04 19:52:39 -05:00
|
|
|
$"(actual size: {FontOffset} bytes).");
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
private void WriteMagicAndSize(long Position, int Size)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
const int DecMagic = 0x18029a7f;
|
|
|
|
const int Key = 0x49621806;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int EncryptedSize = EndianSwap.Swap32(Size ^ Key);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Device.Memory.WriteInt32(Position + 0, DecMagic);
|
|
|
|
Device.Memory.WriteInt32(Position + 4, EncryptedSize);
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public int GetFontSize(SharedFontType FontType)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
EnsureInitialized(Device.System.ContentManager);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return FontData[FontType].Size;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public int GetSharedMemoryAddressOffset(SharedFontType FontType)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
EnsureInitialized(Device.System.ContentManager);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return FontData[FontType].Offset + 8;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|