diff options
| author | TSR Berry <20988865+TSRBerry@users.noreply.github.com> | 2023-04-08 01:22:00 +0200 |
|---|---|---|
| committer | Mary <thog@protonmail.com> | 2023-04-27 23:51:14 +0200 |
| commit | cee712105850ac3385cd0091a923438167433f9f (patch) | |
| tree | 4a5274b21d8b7f938c0d0ce18736d3f2993b11b1 /src/Ryujinx.HLE/HOS/Services/Sdb | |
| parent | cd124bda587ef09668a971fa1cac1c3f0cfc9f21 (diff) | |
Move solution and projects to src
Diffstat (limited to 'src/Ryujinx.HLE/HOS/Services/Sdb')
10 files changed, 496 insertions, 0 deletions
diff --git a/src/Ryujinx.HLE/HOS/Services/Sdb/Avm/IAvmService.cs b/src/Ryujinx.HLE/HOS/Services/Sdb/Avm/IAvmService.cs new file mode 100644 index 00000000..d65c8bba --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Sdb/Avm/IAvmService.cs @@ -0,0 +1,8 @@ +namespace Ryujinx.HLE.HOS.Services.Am.Tcap +{ + [Service("avm")] // 6.0.0+ + class IAvmService : IpcService + { + public IAvmService(ServiceCtx context) { } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/INotifyService.cs b/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/INotifyService.cs new file mode 100644 index 00000000..5247a238 --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/INotifyService.cs @@ -0,0 +1,8 @@ +namespace Ryujinx.HLE.HOS.Services.Sdb.Pdm +{ + [Service("pdm:ntfy")] + class INotifyService : IpcService + { + public INotifyService(ServiceCtx context) { } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/IQueryService.cs b/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/IQueryService.cs new file mode 100644 index 00000000..1f66ff9d --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/IQueryService.cs @@ -0,0 +1,24 @@ +using Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService; + +namespace Ryujinx.HLE.HOS.Services.Sdb.Pdm +{ + [Service("pdm:qry")] + class IQueryService : IpcService + { + public IQueryService(ServiceCtx context) { } + + [CommandCmif(13)] // 5.0.0+ + // QueryApplicationPlayStatisticsForSystem(buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count) + public ResultCode QueryApplicationPlayStatisticsForSystem(ServiceCtx context) + { + return QueryPlayStatisticsManager.GetPlayStatistics(context); + } + + [CommandCmif(16)] // 6.0.0+ + // QueryApplicationPlayStatisticsByUserAccountIdForSystem(nn::account::Uid, buffer<bytes, 5> title_id_list) -> (buffer<bytes, 6> entries, s32 entries_count) + public ResultCode QueryApplicationPlayStatisticsByUserAccountIdForSystem(ServiceCtx context) + { + return QueryPlayStatisticsManager.GetPlayStatistics(context, true); + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/QueryService/QueryPlayStatisticsManager.cs b/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/QueryService/QueryPlayStatisticsManager.cs new file mode 100644 index 00000000..52a07d46 --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/QueryService/QueryPlayStatisticsManager.cs @@ -0,0 +1,84 @@ +using Ryujinx.Common; +using Ryujinx.Cpu; +using Ryujinx.HLE.HOS.Services.Account.Acc; +using Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService.Types; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; + +namespace Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService +{ + static class QueryPlayStatisticsManager + { + private static Dictionary<UserId, ApplicationPlayStatistics> applicationPlayStatistics = new Dictionary<UserId, ApplicationPlayStatistics>(); + + internal static ResultCode GetPlayStatistics(ServiceCtx context, bool byUserId = false) + { + ulong inputPosition = context.Request.SendBuff[0].Position; + ulong inputSize = context.Request.SendBuff[0].Size; + + ulong outputPosition = context.Request.ReceiveBuff[0].Position; + ulong outputSize = context.Request.ReceiveBuff[0].Size; + + UserId userId = byUserId ? context.RequestData.ReadStruct<UserId>() : new UserId(); + + if (byUserId) + { + if (!context.Device.System.AccountManager.TryGetUser(userId, out _)) + { + return ResultCode.UserNotFound; + } + } + + PlayLogQueryCapability queryCapability = (PlayLogQueryCapability)context.Device.Processes.ActiveApplication.ApplicationControlProperties.PlayLogQueryCapability; + + List<ulong> titleIds = new List<ulong>(); + + for (ulong i = 0; i < inputSize / sizeof(ulong); i++) + { + titleIds.Add(context.Memory.Read<ulong>(inputPosition)); + } + + if (queryCapability == PlayLogQueryCapability.WhiteList) + { + // Check if input title ids are in the whitelist. + foreach (ulong titleId in titleIds) + { + if (!context.Device.Processes.ActiveApplication.ApplicationControlProperties.PlayLogQueryableApplicationId.ItemsRo.Contains(titleId)) + { + return (ResultCode)Am.ResultCode.ObjectInvalid; + } + } + } + + MemoryHelper.FillWithZeros(context.Memory, outputPosition, (int)outputSize); + + // Return ResultCode.ServiceUnavailable if data is locked by another process. + var filteredApplicationPlayStatistics = applicationPlayStatistics.AsEnumerable(); + + if (queryCapability == PlayLogQueryCapability.None) + { + filteredApplicationPlayStatistics = filteredApplicationPlayStatistics.Where(kv => kv.Value.TitleId == context.Process.TitleId); + } + else // PlayLogQueryCapability.All + { + filteredApplicationPlayStatistics = filteredApplicationPlayStatistics.Where(kv => titleIds.Contains(kv.Value.TitleId)); + } + + if (byUserId) + { + filteredApplicationPlayStatistics = filteredApplicationPlayStatistics.Where(kv => kv.Key == userId); + } + + for (int i = 0; i < filteredApplicationPlayStatistics.Count(); i++) + { + MemoryHelper.Write(context.Memory, outputPosition + (ulong)(i * Unsafe.SizeOf<ApplicationPlayStatistics>()), filteredApplicationPlayStatistics.ElementAt(i).Value); + } + + context.ResponseData.Write(filteredApplicationPlayStatistics.Count()); + + return ResultCode.Success; + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/QueryService/Types/ApplicationPlayStatistics.cs b/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/QueryService/Types/ApplicationPlayStatistics.cs new file mode 100644 index 00000000..c28d757e --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/QueryService/Types/ApplicationPlayStatistics.cs @@ -0,0 +1,12 @@ +using System.Runtime.InteropServices; + +namespace Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService.Types +{ + [StructLayout(LayoutKind.Sequential, Size = 0x18)] + struct ApplicationPlayStatistics + { + public ulong TitleId; + public long TotalPlayTime; // In nanoseconds. + public long TotalLaunchCount; + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/QueryService/Types/PlayLogQueryCapability.cs b/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/QueryService/Types/PlayLogQueryCapability.cs new file mode 100644 index 00000000..9e4b85de --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/QueryService/Types/PlayLogQueryCapability.cs @@ -0,0 +1,9 @@ +namespace Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService.Types +{ + enum PlayLogQueryCapability + { + None, + WhiteList, + All + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/ResultCode.cs b/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/ResultCode.cs new file mode 100644 index 00000000..c337051b --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Sdb/Pdm/ResultCode.cs @@ -0,0 +1,15 @@ +namespace Ryujinx.HLE.HOS.Services.Sdb.Pdm +{ + enum ResultCode + { + ModuleId = 178, + ErrorCodeShift = 9, + + Success = 0, + + InvalidUserID = (100 << ErrorCodeShift) | ModuleId, + UserNotFound = (101 << ErrorCodeShift) | ModuleId, + ServiceUnavailable = (150 << ErrorCodeShift) | ModuleId, + FileStorageFailure = (200 << ErrorCodeShift) | ModuleId + } +} diff --git a/src/Ryujinx.HLE/HOS/Services/Sdb/Pl/ISharedFontManager.cs b/src/Ryujinx.HLE/HOS/Services/Sdb/Pl/ISharedFontManager.cs new file mode 100644 index 00000000..9e2f7a4e --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Sdb/Pl/ISharedFontManager.cs @@ -0,0 +1,140 @@ +using Ryujinx.HLE.HOS.Ipc; +using Ryujinx.HLE.HOS.Services.Sdb.Pl.Types; +using Ryujinx.Horizon.Common; +using System; + +namespace Ryujinx.HLE.HOS.Services.Sdb.Pl +{ + [Service("pl:u")] + [Service("pl:s")] // 9.0.0+ + class ISharedFontManager : IpcService + { + private int _fontSharedMemHandle; + + public ISharedFontManager(ServiceCtx context) { } + + [CommandCmif(0)] + // RequestLoad(u32) + public ResultCode RequestLoad(ServiceCtx context) + { + SharedFontType fontType = (SharedFontType)context.RequestData.ReadInt32(); + + // We don't need to do anything here because we do lazy initialization + // on SharedFontManager (the font is loaded when necessary). + return ResultCode.Success; + } + + [CommandCmif(1)] + // GetLoadState(u32) -> u32 + public ResultCode GetLoadState(ServiceCtx context) + { + SharedFontType fontType = (SharedFontType)context.RequestData.ReadInt32(); + + // 1 (true) indicates that the font is already loaded. + // All fonts are already loaded. + context.ResponseData.Write(1); + + return ResultCode.Success; + } + + [CommandCmif(2)] + // GetFontSize(u32) -> u32 + public ResultCode GetFontSize(ServiceCtx context) + { + SharedFontType fontType = (SharedFontType)context.RequestData.ReadInt32(); + + context.ResponseData.Write(context.Device.System.SharedFontManager.GetFontSize(fontType)); + + return ResultCode.Success; + } + + [CommandCmif(3)] + // GetSharedMemoryAddressOffset(u32) -> u32 + public ResultCode GetSharedMemoryAddressOffset(ServiceCtx context) + { + SharedFontType fontType = (SharedFontType)context.RequestData.ReadInt32(); + + context.ResponseData.Write(context.Device.System.SharedFontManager.GetSharedMemoryAddressOffset(fontType)); + + return ResultCode.Success; + } + + [CommandCmif(4)] + // GetSharedMemoryNativeHandle() -> handle<copy> + public ResultCode GetSharedMemoryNativeHandle(ServiceCtx context) + { + context.Device.System.SharedFontManager.EnsureInitialized(context.Device.System.ContentManager); + + if (_fontSharedMemHandle == 0) + { + if (context.Process.HandleTable.GenerateHandle(context.Device.System.FontSharedMem, out _fontSharedMemHandle) != Result.Success) + { + throw new InvalidOperationException("Out of handles!"); + } + } + + context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_fontSharedMemHandle); + + return ResultCode.Success; + } + + [CommandCmif(5)] + // GetSharedFontInOrderOfPriority(bytes<8, 1>) -> (u8, u32, buffer<unknown, 6>, buffer<unknown, 6>, buffer<unknown, 6>) + public ResultCode GetSharedFontInOrderOfPriority(ServiceCtx context) + { + long languageCode = context.RequestData.ReadInt64(); + int loadedCount = 0; + + for (SharedFontType type = 0; type < SharedFontType.Count; type++) + { + uint offset = (uint)type * 4; + + if (!AddFontToOrderOfPriorityList(context, type, offset)) + { + break; + } + + loadedCount++; + } + + context.ResponseData.Write(loadedCount); + context.ResponseData.Write((int)SharedFontType.Count); + + return ResultCode.Success; + } + + [CommandCmif(6)] // 4.0.0+ + // GetSharedFontInOrderOfPriorityForSystem(bytes<8, 1>) -> (u8, u32, buffer<unknown, 6>, buffer<unknown, 6>, buffer<unknown, 6>) + public ResultCode GetSharedFontInOrderOfPriorityForSystem(ServiceCtx context) + { + // TODO: Check the differencies with GetSharedFontInOrderOfPriority. + + return GetSharedFontInOrderOfPriority(context); + } + + private bool AddFontToOrderOfPriorityList(ServiceCtx context, SharedFontType fontType, uint offset) + { + ulong typesPosition = context.Request.ReceiveBuff[0].Position; + ulong typesSize = context.Request.ReceiveBuff[0].Size; + + ulong offsetsPosition = context.Request.ReceiveBuff[1].Position; + ulong offsetsSize = context.Request.ReceiveBuff[1].Size; + + ulong fontSizeBufferPosition = context.Request.ReceiveBuff[2].Position; + ulong fontSizeBufferSize = context.Request.ReceiveBuff[2].Size; + + if (offset + 4 > (uint)typesSize || + offset + 4 > (uint)offsetsSize || + offset + 4 > (uint)fontSizeBufferSize) + { + return false; + } + + context.Memory.Write(typesPosition + offset, (int)fontType); + context.Memory.Write(offsetsPosition + offset, context.Device.System.SharedFontManager.GetSharedMemoryAddressOffset(fontType)); + context.Memory.Write(fontSizeBufferPosition + offset, context.Device.System.SharedFontManager.GetFontSize(fontType)); + + return true; + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Sdb/Pl/SharedFontManager.cs b/src/Ryujinx.HLE/HOS/Services/Sdb/Pl/SharedFontManager.cs new file mode 100644 index 00000000..fef82cbc --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Sdb/Pl/SharedFontManager.cs @@ -0,0 +1,183 @@ +using LibHac.Common; +using LibHac.Fs; +using LibHac.Fs.Fsa; +using LibHac.FsSystem; +using LibHac.Ncm; +using LibHac.Tools.FsSystem; +using LibHac.Tools.FsSystem.NcaUtils; +using Ryujinx.Common.Memory; +using Ryujinx.HLE.Exceptions; +using Ryujinx.HLE.FileSystem; +using Ryujinx.HLE.HOS.Kernel.Memory; +using Ryujinx.HLE.HOS.Services.Sdb.Pl.Types; +using System; +using System.Buffers.Binary; +using System.Collections.Generic; +using System.IO; + +namespace Ryujinx.HLE.HOS.Services.Sdb.Pl +{ + class SharedFontManager + { + private static readonly uint FontKey = 0x06186249; + private static readonly uint BFTTFMagic = 0x18029a7f; + + private readonly Switch _device; + private readonly SharedMemoryStorage _storage; + + private struct FontInfo + { + public int Offset; + public int Size; + + public FontInfo(int offset, int size) + { + Offset = offset; + Size = size; + } + } + + private Dictionary<SharedFontType, FontInfo> _fontData; + + public SharedFontManager(Switch device, SharedMemoryStorage storage) + { + _device = device; + _storage = storage; + } + + public void Initialize() + { + _fontData?.Clear(); + _fontData = null; + + } + + public void EnsureInitialized(ContentManager contentManager) + { + if (_fontData == null) + { + _storage.ZeroFill(); + + uint fontOffset = 0; + + FontInfo CreateFont(string name) + { + if (contentManager.TryGetFontTitle(name, out ulong fontTitle) && contentManager.TryGetFontFilename(name, out string fontFilename)) + { + string contentPath = contentManager.GetInstalledContentPath(fontTitle, StorageId.BuiltInSystem, NcaContentType.Data); + string fontPath = _device.FileSystem.SwitchPathToSystemPath(contentPath); + + if (!string.IsNullOrWhiteSpace(fontPath)) + { + byte[] data; + + using (IStorage ncaFileStream = new LocalStorage(fontPath, FileAccess.Read, FileMode.Open)) + { + Nca nca = new Nca(_device.System.KeySet, ncaFileStream); + IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _device.System.FsIntegrityCheckLevel); + + using var fontFile = new UniqueRef<IFile>(); + + romfs.OpenFile(ref fontFile.Ref, ("/" + fontFilename).ToU8Span(), OpenMode.Read).ThrowIfFailure(); + + data = DecryptFont(fontFile.Get.AsStream()); + } + + FontInfo info = new FontInfo((int)fontOffset, data.Length); + + WriteMagicAndSize(fontOffset, data.Length); + + fontOffset += 8; + + uint start = fontOffset; + + for (; fontOffset - start < data.Length; fontOffset++) + { + _storage.GetRef<byte>(fontOffset) = data[fontOffset - start]; + } + + return info; + } + else + { + if (!contentManager.TryGetSystemTitlesName(fontTitle, out string titleName)) + { + titleName = "Unknown"; + } + + 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)"); + } + } + else + { + throw new ArgumentException($"Unknown font \"{name}\"!"); + } + } + + _fontData = new Dictionary<SharedFontType, FontInfo> + { + { SharedFontType.JapanUsEurope, CreateFont("FontStandard") }, + { SharedFontType.SimplifiedChinese, CreateFont("FontChineseSimplified") }, + { SharedFontType.SimplifiedChineseEx, CreateFont("FontExtendedChineseSimplified") }, + { SharedFontType.TraditionalChinese, CreateFont("FontChineseTraditional") }, + { SharedFontType.Korean, CreateFont("FontKorean") }, + { SharedFontType.NintendoEx, CreateFont("FontNintendoExtended") } + }; + + if (fontOffset > Horizon.FontSize) + { + 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. (actual size: {fontOffset} bytes)."); + } + } + } + + private void WriteMagicAndSize(ulong offset, int size) + { + const int key = 0x49621806; + + int encryptedSize = BinaryPrimitives.ReverseEndianness(size ^ key); + + _storage.GetRef<int>(offset + 0) = (int)BFTTFMagic; + _storage.GetRef<int>(offset + 4) = encryptedSize; + } + + public int GetFontSize(SharedFontType fontType) + { + EnsureInitialized(_device.System.ContentManager); + + return _fontData[fontType].Size; + } + + public int GetSharedMemoryAddressOffset(SharedFontType fontType) + { + EnsureInitialized(_device.System.ContentManager); + + return _fontData[fontType].Offset + 8; + } + + private static byte[] DecryptFont(Stream bfttfStream) + { + static uint KXor(uint data) => data ^ FontKey; + + using (BinaryReader reader = new BinaryReader(bfttfStream)) + using (MemoryStream ttfStream = MemoryStreamManager.Shared.GetStream()) + using (BinaryWriter output = new BinaryWriter(ttfStream)) + { + if (KXor(reader.ReadUInt32()) != BFTTFMagic) + { + throw new InvalidDataException("Error: Input file is not in BFTTF format!"); + } + + bfttfStream.Position += 4; + + for (int i = 0; i < (bfttfStream.Length - 8) / 4; i++) + { + output.Write(KXor(reader.ReadUInt32())); + } + + return ttfStream.ToArray(); + } + } + } +}
\ No newline at end of file diff --git a/src/Ryujinx.HLE/HOS/Services/Sdb/Pl/Types/SharedFontType.cs b/src/Ryujinx.HLE/HOS/Services/Sdb/Pl/Types/SharedFontType.cs new file mode 100644 index 00000000..90ee4f03 --- /dev/null +++ b/src/Ryujinx.HLE/HOS/Services/Sdb/Pl/Types/SharedFontType.cs @@ -0,0 +1,13 @@ +namespace Ryujinx.HLE.HOS.Services.Sdb.Pl.Types +{ + public enum SharedFontType + { + JapanUsEurope = 0, + SimplifiedChinese = 1, + SimplifiedChineseEx = 2, + TraditionalChinese = 3, + Korean = 4, + NintendoEx = 5, + Count + } +}
\ No newline at end of file |
