Make sure that font sum doesn't exceed 17MB

Also rename font data dictionary for coherence.
This commit is contained in:
Thog 2018-08-02 02:00:06 +02:00
commit 31f4110fb9
No known key found for this signature in database
GPG key ID: 0CD291558FAFDBC6
3 changed files with 33 additions and 21 deletions

View file

@ -13,6 +13,7 @@ namespace Ryujinx.HLE.Font
{ {
public class SharedFontManager public class SharedFontManager
{ {
private const uint SharedMemorySize = 0x1100000;
private Logger Log; private Logger Log;
private string FontsPath; private string FontsPath;
@ -21,7 +22,7 @@ namespace Ryujinx.HLE.Font
private (AMemory, long, long)[] ShMemPositions; private (AMemory, long, long)[] ShMemPositions;
private Dictionary<SharedFontType, byte[]> FontEmbeddedPaths; private Dictionary<SharedFontType, byte[]> FontData;
private uint[] LoadedFonts; private uint[] LoadedFonts;
@ -34,7 +35,7 @@ namespace Ryujinx.HLE.Font
ShMemPositions = new(AMemory, long, long)[0]; ShMemPositions = new(AMemory, long, long)[0];
FontEmbeddedPaths = new Dictionary<SharedFontType, byte[]>() FontData = new Dictionary<SharedFontType, byte[]>()
{ {
{ SharedFontType.JapanUsEurope, GetData("FontStandard") }, { SharedFontType.JapanUsEurope, GetData("FontStandard") },
{ SharedFontType.SimplifiedChinese, GetData("FontChineseSimplified") }, { SharedFontType.SimplifiedChinese, GetData("FontChineseSimplified") },
@ -44,7 +45,19 @@ namespace Ryujinx.HLE.Font
{ SharedFontType.NintendoEx, GetData("FontNintendoExtended") } { SharedFontType.NintendoEx, GetData("FontNintendoExtended") }
}; };
LoadedFonts = new uint[FontEmbeddedPaths.Count]; int FontMemoryUsage = 0;
foreach (byte[] data in FontData.Values)
{
FontMemoryUsage += data.Length;
FontMemoryUsage += 0x8;
}
if (FontMemoryUsage > SharedMemorySize)
{
throw new InvalidSystemResourceException($"The sum of all fonts size exceed the shared memory size. Please make sure that the fonts don't exceed {SharedMemorySize} bytes in total. (actual size: {FontMemoryUsage} bytes)");
}
LoadedFonts = new uint[FontData.Count];
} }
public byte[] GetData(string FontName) public byte[] GetData(string FontName)
@ -56,7 +69,7 @@ namespace Ryujinx.HLE.Font
} }
else else
{ {
throw new SystemResourceNotFoundException($"Font \"{FontName}.ttf\" not found. Please provide it in \"{FontsPath}\"."); throw new InvalidSystemResourceException($"Font \"{FontName}.ttf\" not found. Please provide it in \"{FontsPath}\".");
} }
} }
@ -65,7 +78,7 @@ namespace Ryujinx.HLE.Font
uint SharedMemoryAddressOffset = GetSharedMemoryAddressOffset(FontType); uint SharedMemoryAddressOffset = GetSharedMemoryAddressOffset(FontType);
// TODO: find what are the 8 bytes before the font // TODO: find what are the 8 bytes before the font
Memory.WriteUInt64(Position + SharedMemoryAddressOffset - 8, 0); Memory.WriteUInt64(Position + SharedMemoryAddressOffset - 8, 0);
Memory.WriteBytes(Position + SharedMemoryAddressOffset, FontEmbeddedPaths[FontType]); Memory.WriteBytes(Position + SharedMemoryAddressOffset, FontData[FontType]);
} }
public void PropagateNewMapFont(SharedFontType Type) public void PropagateNewMapFont(SharedFontType Type)
@ -143,7 +156,7 @@ namespace Ryujinx.HLE.Font
public uint GetFontSize(SharedFontType FontType) public uint GetFontSize(SharedFontType FontType)
{ {
return (uint)FontEmbeddedPaths[FontType].Length; return (uint)FontData[FontType].Length;
} }
public uint GetSharedMemoryAddressOffset(SharedFontType FontType) public uint GetSharedMemoryAddressOffset(SharedFontType FontType)
@ -159,6 +172,6 @@ namespace Ryujinx.HLE.Font
return Pos; return Pos;
} }
public int Count => FontEmbeddedPaths.Count; public int Count => FontData.Count;
} }
} }

View file

@ -0,0 +1,13 @@
using System;
namespace Ryujinx.HLE.Resource
{
public class InvalidSystemResourceException : Exception
{
public InvalidSystemResourceException(string message)
: base(message)
{
}
}
}

View file

@ -1,14 +0,0 @@
using System;
namespace Ryujinx.HLE.Resource
{
public class SystemResourceNotFoundException: Exception
{
public SystemResourceNotFoundException(string message)
: base(message)
{
}
}
}