nfp: Amiibo scanning support (#2006)
* Initial Impl. * You just want me cause I'm next * Fix some logics * Fix close button
This commit is contained in:
parent
2b92c10105
commit
a56423802c
22 changed files with 1830 additions and 144 deletions
|
@ -22,6 +22,7 @@ using Ryujinx.HLE.HOS.Services.Apm;
|
|||
using Ryujinx.HLE.HOS.Services.Arp;
|
||||
using Ryujinx.HLE.HOS.Services.Audio.AudioRenderer;
|
||||
using Ryujinx.HLE.HOS.Services.Mii;
|
||||
using Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager;
|
||||
using Ryujinx.HLE.HOS.Services.Nv;
|
||||
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl;
|
||||
using Ryujinx.HLE.HOS.Services.Pcv.Bpc;
|
||||
|
@ -33,6 +34,7 @@ using Ryujinx.HLE.HOS.SystemState;
|
|||
using Ryujinx.HLE.Loaders.Executables;
|
||||
using Ryujinx.HLE.Utilities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
@ -65,6 +67,8 @@ namespace Ryujinx.HLE.HOS
|
|||
|
||||
internal AppletStateMgr AppletState { get; private set; }
|
||||
|
||||
internal List<NfpDevice> NfpDevices { get; private set; }
|
||||
|
||||
internal ServerBase BsdServer { get; private set; }
|
||||
internal ServerBase AudRenServer { get; private set; }
|
||||
internal ServerBase AudOutServer { get; private set; }
|
||||
|
@ -113,6 +117,8 @@ namespace Ryujinx.HLE.HOS
|
|||
|
||||
PerformanceState = new PerformanceState();
|
||||
|
||||
NfpDevices = new List<NfpDevice>();
|
||||
|
||||
// Note: This is not really correct, but with HLE of services, the only memory
|
||||
// region used that is used is Application, so we can use the other ones for anything.
|
||||
KMemoryRegionManager region = KernelContext.MemoryRegions[(int)MemoryRegion.NvServices];
|
||||
|
@ -320,6 +326,33 @@ namespace Ryujinx.HLE.HOS
|
|||
AppletState.MessageEvent.ReadableEvent.Signal();
|
||||
}
|
||||
|
||||
public void ScanAmiibo(int nfpDeviceId, string amiiboId, bool useRandomUuid)
|
||||
{
|
||||
if (NfpDevices[nfpDeviceId].State == NfpDeviceState.SearchingForTag)
|
||||
{
|
||||
NfpDevices[nfpDeviceId].State = NfpDeviceState.TagFound;
|
||||
NfpDevices[nfpDeviceId].AmiiboId = amiiboId;
|
||||
NfpDevices[nfpDeviceId].UseRandomUuid = useRandomUuid;
|
||||
}
|
||||
}
|
||||
|
||||
public bool SearchingForAmiibo(out int nfpDeviceId)
|
||||
{
|
||||
nfpDeviceId = default;
|
||||
|
||||
for (int i = 0; i < NfpDevices.Count; i++)
|
||||
{
|
||||
if (NfpDevices[i].State == NfpDeviceState.SearchingForTag)
|
||||
{
|
||||
nfpDeviceId = i;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SignalDisplayResolutionChange()
|
||||
{
|
||||
DisplayResolutionChangeEvent.ReadableEvent.Signal();
|
||||
|
|
|
@ -389,7 +389,7 @@ namespace Ryujinx.HLE.HOS.Services.Mii.Types
|
|||
|
||||
coreData.SetDefault();
|
||||
|
||||
if (gender == Types.Gender.All)
|
||||
if (gender == Gender.All)
|
||||
{
|
||||
gender = (Gender)utilImpl.GetRandom((int)gender);
|
||||
}
|
||||
|
@ -432,7 +432,7 @@ namespace Ryujinx.HLE.HOS.Services.Mii.Types
|
|||
|
||||
int axisY = 0;
|
||||
|
||||
if (gender == Types.Gender.Female && age == Age.Young)
|
||||
if (gender == Gender.Female && age == Age.Young)
|
||||
{
|
||||
axisY = utilImpl.GetRandom(3);
|
||||
}
|
||||
|
@ -466,8 +466,8 @@ namespace Ryujinx.HLE.HOS.Services.Mii.Types
|
|||
// Eye
|
||||
coreData.EyeType = (EyeType)eyeTypeInfo.Values[utilImpl.GetRandom(eyeTypeInfo.ValuesCount)];
|
||||
|
||||
int eyeRotateKey1 = gender != Types.Gender.Male ? 4 : 2;
|
||||
int eyeRotateKey2 = gender != Types.Gender.Male ? 3 : 4;
|
||||
int eyeRotateKey1 = gender != Gender.Male ? 4 : 2;
|
||||
int eyeRotateKey2 = gender != Gender.Male ? 3 : 4;
|
||||
|
||||
byte eyeRotateOffset = (byte)(32 - EyeRotateTable[eyeRotateKey1] + eyeRotateKey2);
|
||||
byte eyeRotate = (byte)(32 - EyeRotateTable[(int)coreData.EyeType]);
|
||||
|
@ -496,14 +496,14 @@ namespace Ryujinx.HLE.HOS.Services.Mii.Types
|
|||
coreData.EyebrowY = (byte)(axisY + eyebrowY);
|
||||
|
||||
// Nose
|
||||
int noseScale = gender == Types.Gender.Female ? 3 : 4;
|
||||
int noseScale = gender == Gender.Female ? 3 : 4;
|
||||
|
||||
coreData.NoseType = (NoseType)noseTypeInfo.Values[utilImpl.GetRandom(noseTypeInfo.ValuesCount)];
|
||||
coreData.NoseScale = (byte)noseScale;
|
||||
coreData.NoseY = (byte)(axisY + 9);
|
||||
|
||||
// Mouth
|
||||
int mouthColor = gender == Types.Gender.Female ? utilImpl.GetRandom(0, 4) : 0;
|
||||
int mouthColor = gender == Gender.Female ? utilImpl.GetRandom(0, 4) : 0;
|
||||
|
||||
coreData.MouthType = (MouthType)mouthTypeInfo.Values[utilImpl.GetRandom(mouthTypeInfo.ValuesCount)];
|
||||
coreData.MouthColor = (CommonColor)Helper.Ver3MouthColorTable[mouthColor];
|
||||
|
@ -515,7 +515,7 @@ namespace Ryujinx.HLE.HOS.Services.Mii.Types
|
|||
coreData.BeardColor = coreData.HairColor;
|
||||
coreData.MustacheScale = 4;
|
||||
|
||||
if (gender == Types.Gender.Male && age != Age.Young && utilImpl.GetRandom(10) < 2)
|
||||
if (gender == Gender.Male && age != Age.Young && utilImpl.GetRandom(10) < 2)
|
||||
{
|
||||
BeardAndMustacheFlag mustacheAndBeardFlag = (BeardAndMustacheFlag)utilImpl.GetRandom(3);
|
||||
|
||||
|
|
|
@ -7,7 +7,12 @@
|
|||
|
||||
Success = 0,
|
||||
|
||||
DeviceNotFound = (64 << ErrorCodeShift) | ModuleId,
|
||||
DevicesBufferIsNull = (65 << ErrorCodeShift) | ModuleId
|
||||
DeviceNotFound = (64 << ErrorCodeShift) | ModuleId,
|
||||
WrongArgument = (65 << ErrorCodeShift) | ModuleId,
|
||||
WrongDeviceState = (73 << ErrorCodeShift) | ModuleId,
|
||||
NfcDisabled = (80 << ErrorCodeShift) | ModuleId,
|
||||
TagNotFound = (97 << ErrorCodeShift) | ModuleId,
|
||||
ApplicationAreaIsNull = (128 << ErrorCodeShift) | ModuleId,
|
||||
ApplicationAreaAlreadyCreated = (168 << ErrorCodeShift) | ModuleId
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,8 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager
|
||||
{
|
||||
static class AmiiboConstants
|
||||
{
|
||||
public const int UuidMaxLength = 10;
|
||||
public const int ApplicationAreaSize = 0xD8;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
using Ryujinx.Common.Memory;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential, Size = 0x40)]
|
||||
struct CommonInfo
|
||||
{
|
||||
public ushort LastWriteYear;
|
||||
public byte LastWriteMonth;
|
||||
public byte LastWriteDay;
|
||||
public ushort WriteCounter;
|
||||
public ushort Version;
|
||||
public uint ApplicationAreaSize;
|
||||
public Array52<byte> Reserved;
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
using Ryujinx.HLE.HOS.Kernel.Threading;
|
||||
using Ryujinx.HLE.HOS.Services.Hid;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager
|
||||
{
|
||||
class Device
|
||||
{
|
||||
public KEvent ActivateEvent;
|
||||
public int ActivateEventHandle;
|
||||
|
||||
public KEvent DeactivateEvent;
|
||||
public int DeactivateEventHandle;
|
||||
|
||||
public DeviceState State = DeviceState.Unavailable;
|
||||
|
||||
public PlayerIndex Handle;
|
||||
public NpadIdType NpadIdType;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager
|
||||
{
|
||||
enum DeviceType : uint
|
||||
{
|
||||
Amiibo
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
using Ryujinx.Common.Memory;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential, Size = 0x40)]
|
||||
struct ModelInfo
|
||||
{
|
||||
public ushort CharacterId;
|
||||
public byte CharacterVariant;
|
||||
public byte Series;
|
||||
public ushort ModelNumber;
|
||||
public byte Type;
|
||||
public Array57<byte> Reserved;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager
|
||||
{
|
||||
enum MountTarget : uint
|
||||
{
|
||||
Rom = 1,
|
||||
Ram = 2,
|
||||
All = 3
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
using Ryujinx.HLE.HOS.Kernel.Threading;
|
||||
using Ryujinx.HLE.HOS.Services.Hid;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager
|
||||
{
|
||||
class NfpDevice
|
||||
{
|
||||
public KEvent ActivateEvent;
|
||||
public KEvent DeactivateEvent;
|
||||
|
||||
public void SignalActivate() => ActivateEvent.ReadableEvent.Signal();
|
||||
public void SignalDeactivate() => DeactivateEvent.ReadableEvent.Signal();
|
||||
|
||||
public NfpDeviceState State = NfpDeviceState.Unavailable;
|
||||
|
||||
public PlayerIndex Handle;
|
||||
public NpadIdType NpadIdType;
|
||||
|
||||
public string AmiiboId;
|
||||
|
||||
public bool UseRandomUuid;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager
|
||||
{
|
||||
enum DeviceState
|
||||
enum NfpDeviceState
|
||||
{
|
||||
Initialized = 0,
|
||||
SearchingForTag = 1,
|
|
@ -0,0 +1,19 @@
|
|||
using Ryujinx.Common.Memory;
|
||||
using Ryujinx.HLE.HOS.Services.Mii.Types;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential, Size = 0x100)]
|
||||
struct RegisterInfo
|
||||
{
|
||||
public CharInfo MiiCharInfo;
|
||||
public ushort FirstWriteYear;
|
||||
public byte FirstWriteMonth;
|
||||
public byte FirstWriteDay;
|
||||
public Array11<byte> Nickname;
|
||||
public byte FontRegion;
|
||||
public Array64<byte> Reserved1;
|
||||
public Array58<byte> Reserved2;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
using Ryujinx.Common.Memory;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential, Size = 0x58)]
|
||||
struct TagInfo
|
||||
{
|
||||
public Array10<byte> Uuid;
|
||||
public byte UuidLength;
|
||||
public Array21<byte> Reserved1;
|
||||
public uint Protocol;
|
||||
public uint TagType;
|
||||
public Array6<byte> Reserved2;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager
|
||||
{
|
||||
struct VirtualAmiiboFile
|
||||
{
|
||||
public uint FileVersion { get; set; }
|
||||
public byte[] TagUuid { get; set; }
|
||||
public string AmiiboId { get; set; }
|
||||
public DateTime FirstWriteDate { get; set; }
|
||||
public DateTime LastWriteDate { get; set; }
|
||||
public ushort WriteCounter { get; set; }
|
||||
public List<VirtualAmiiboApplicationArea> ApplicationAreas { get; set; }
|
||||
}
|
||||
|
||||
struct VirtualAmiiboApplicationArea
|
||||
{
|
||||
public uint ApplicationAreaId { get; set; }
|
||||
public byte[] ApplicationArea { get; set; }
|
||||
}
|
||||
}
|
205
Ryujinx.HLE/HOS/Services/Nfc/Nfp/VirtualAmiibo.cs
Normal file
205
Ryujinx.HLE/HOS/Services/Nfc/Nfp/VirtualAmiibo.cs
Normal file
|
@ -0,0 +1,205 @@
|
|||
using Ryujinx.Common.Configuration;
|
||||
using Ryujinx.Common.Memory;
|
||||
using Ryujinx.HLE.HOS.Services.Mii;
|
||||
using Ryujinx.HLE.HOS.Services.Mii.Types;
|
||||
using Ryujinx.HLE.HOS.Services.Nfc.Nfp.UserManager;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp
|
||||
{
|
||||
static class VirtualAmiibo
|
||||
{
|
||||
private static uint _openedApplicationAreaId;
|
||||
|
||||
public static byte[] GenerateUuid(string amiiboId, bool useRandomUuid)
|
||||
{
|
||||
if (useRandomUuid)
|
||||
{
|
||||
return GenerateRandomUuid();
|
||||
}
|
||||
|
||||
VirtualAmiiboFile virtualAmiiboFile = LoadAmiiboFile(amiiboId);
|
||||
|
||||
if (virtualAmiiboFile.TagUuid.Length == 0)
|
||||
{
|
||||
virtualAmiiboFile.TagUuid = GenerateRandomUuid();
|
||||
|
||||
SaveAmiiboFile(virtualAmiiboFile);
|
||||
}
|
||||
|
||||
return virtualAmiiboFile.TagUuid;
|
||||
}
|
||||
|
||||
private static byte[] GenerateRandomUuid()
|
||||
{
|
||||
byte[] uuid = new byte[9];
|
||||
|
||||
new Random().NextBytes(uuid);
|
||||
|
||||
uuid[3] = (byte)(0x88 ^ uuid[0] ^ uuid[1] ^ uuid[2]);
|
||||
uuid[8] = (byte)(uuid[3] ^ uuid[4] ^ uuid[5] ^ uuid[6]);
|
||||
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public static CommonInfo GetCommonInfo(string amiiboId)
|
||||
{
|
||||
VirtualAmiiboFile amiiboFile = LoadAmiiboFile(amiiboId);
|
||||
|
||||
return new CommonInfo()
|
||||
{
|
||||
LastWriteYear = (ushort)amiiboFile.LastWriteDate.Year,
|
||||
LastWriteMonth = (byte)amiiboFile.LastWriteDate.Month,
|
||||
LastWriteDay = (byte)amiiboFile.LastWriteDate.Day,
|
||||
WriteCounter = amiiboFile.WriteCounter,
|
||||
Version = 1,
|
||||
ApplicationAreaSize = AmiiboConstants.ApplicationAreaSize,
|
||||
Reserved = new Array52<byte>()
|
||||
};
|
||||
}
|
||||
|
||||
public static RegisterInfo GetRegisterInfo(string amiiboId)
|
||||
{
|
||||
VirtualAmiiboFile amiiboFile = LoadAmiiboFile(amiiboId);
|
||||
|
||||
UtilityImpl utilityImpl = new UtilityImpl();
|
||||
CharInfo charInfo = new CharInfo();
|
||||
|
||||
charInfo.SetFromStoreData(StoreData.BuildDefault(utilityImpl, 0));
|
||||
|
||||
// TODO: Maybe change the "no name" by the player name when user profile will be implemented.
|
||||
// charInfo.Nickname = Nickname.FromString("Nickname");
|
||||
|
||||
RegisterInfo registerInfo = new RegisterInfo()
|
||||
{
|
||||
MiiCharInfo = charInfo,
|
||||
FirstWriteYear = (ushort)amiiboFile.FirstWriteDate.Year,
|
||||
FirstWriteMonth = (byte)amiiboFile.FirstWriteDate.Month,
|
||||
FirstWriteDay = (byte)amiiboFile.FirstWriteDate.Day,
|
||||
FontRegion = 0,
|
||||
Reserved1 = new Array64<byte>(),
|
||||
Reserved2 = new Array58<byte>()
|
||||
};
|
||||
|
||||
Encoding.ASCII.GetBytes("Ryujinx").CopyTo(registerInfo.Nickname.ToSpan());
|
||||
|
||||
return registerInfo;
|
||||
}
|
||||
|
||||
public static bool OpenApplicationArea(string amiiboId, uint applicationAreaId)
|
||||
{
|
||||
VirtualAmiiboFile virtualAmiiboFile = LoadAmiiboFile(amiiboId);
|
||||
|
||||
if (virtualAmiiboFile.ApplicationAreas.Any(item => item.ApplicationAreaId == applicationAreaId))
|
||||
{
|
||||
_openedApplicationAreaId = applicationAreaId;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static byte[] GetApplicationArea(string amiiboId)
|
||||
{
|
||||
VirtualAmiiboFile virtualAmiiboFile = LoadAmiiboFile(amiiboId);
|
||||
|
||||
foreach (VirtualAmiiboApplicationArea applicationArea in virtualAmiiboFile.ApplicationAreas)
|
||||
{
|
||||
if (applicationArea.ApplicationAreaId == _openedApplicationAreaId)
|
||||
{
|
||||
return applicationArea.ApplicationArea;
|
||||
}
|
||||
}
|
||||
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
|
||||
public static bool CreateApplicationArea(string amiiboId, uint applicationAreaId, byte[] applicationAreaData)
|
||||
{
|
||||
VirtualAmiiboFile virtualAmiiboFile = LoadAmiiboFile(amiiboId);
|
||||
|
||||
if (virtualAmiiboFile.ApplicationAreas.Any(item => item.ApplicationAreaId == applicationAreaId))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
virtualAmiiboFile.ApplicationAreas.Add(new VirtualAmiiboApplicationArea()
|
||||
{
|
||||
ApplicationAreaId = applicationAreaId,
|
||||
ApplicationArea = applicationAreaData
|
||||
});
|
||||
|
||||
SaveAmiiboFile(virtualAmiiboFile);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void SetApplicationArea(string amiiboId, byte[] applicationAreaData)
|
||||
{
|
||||
VirtualAmiiboFile virtualAmiiboFile = LoadAmiiboFile(amiiboId);
|
||||
|
||||
if (virtualAmiiboFile.ApplicationAreas.Any(item => item.ApplicationAreaId == _openedApplicationAreaId))
|
||||
{
|
||||
for (int i = 0; i < virtualAmiiboFile.ApplicationAreas.Count; i++)
|
||||
{
|
||||
if (virtualAmiiboFile.ApplicationAreas[i].ApplicationAreaId == _openedApplicationAreaId)
|
||||
{
|
||||
virtualAmiiboFile.ApplicationAreas[i] = new VirtualAmiiboApplicationArea()
|
||||
{
|
||||
ApplicationAreaId = _openedApplicationAreaId,
|
||||
ApplicationArea = applicationAreaData
|
||||
};
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SaveAmiiboFile(virtualAmiiboFile);
|
||||
}
|
||||
}
|
||||
|
||||
private static VirtualAmiiboFile LoadAmiiboFile(string amiiboId)
|
||||
{
|
||||
Directory.CreateDirectory(Path.Join(AppDataManager.BaseDirPath, "system", "amiibo"));
|
||||
|
||||
string filePath = Path.Join(AppDataManager.BaseDirPath, "system", "amiibo", $"{amiiboId}.json");
|
||||
|
||||
VirtualAmiiboFile virtualAmiiboFile;
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
virtualAmiiboFile = JsonSerializer.Deserialize<VirtualAmiiboFile>(File.ReadAllText(filePath));
|
||||
}
|
||||
else
|
||||
{
|
||||
virtualAmiiboFile = new VirtualAmiiboFile()
|
||||
{
|
||||
FileVersion = 0,
|
||||
TagUuid = Array.Empty<byte>(),
|
||||
AmiiboId = amiiboId,
|
||||
FirstWriteDate = DateTime.Now,
|
||||
LastWriteDate = DateTime.Now,
|
||||
WriteCounter = 0,
|
||||
ApplicationAreas = new List<VirtualAmiiboApplicationArea>()
|
||||
};
|
||||
|
||||
SaveAmiiboFile(virtualAmiiboFile);
|
||||
}
|
||||
|
||||
return virtualAmiiboFile;
|
||||
}
|
||||
|
||||
private static void SaveAmiiboFile(VirtualAmiiboFile virtualAmiiboFile)
|
||||
{
|
||||
string filePath = Path.Join(AppDataManager.BaseDirPath, "system", "amiibo", $"{virtualAmiiboFile.AmiiboId}.json");
|
||||
|
||||
File.WriteAllText(filePath, JsonSerializer.Serialize(virtualAmiiboFile));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue