2021-03-18 21:40:20 +01:00
|
|
|
|
using Ryujinx.Common.Configuration;
|
|
|
|
|
using Ryujinx.Common.Memory;
|
2022-05-31 21:29:35 +02:00
|
|
|
|
using Ryujinx.Cpu;
|
2021-03-18 21:40:20 +01:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Mii;
|
|
|
|
|
using Ryujinx.HLE.HOS.Services.Mii.Types;
|
2021-06-24 01:05:40 +02:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Nfc.Nfp.NfpManager;
|
2021-03-18 21:40:20 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2023-03-22 00:14:46 +01:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
2022-11-16 19:34:18 +01:00
|
|
|
|
Random.Shared.NextBytes(uuid);
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
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>()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 21:29:35 +02:00
|
|
|
|
public static RegisterInfo GetRegisterInfo(ITickSource tickSource, string amiiboId, string nickname)
|
2021-03-18 21:40:20 +01:00
|
|
|
|
{
|
|
|
|
|
VirtualAmiiboFile amiiboFile = LoadAmiiboFile(amiiboId);
|
|
|
|
|
|
2022-05-31 21:29:35 +02:00
|
|
|
|
UtilityImpl utilityImpl = new UtilityImpl(tickSource);
|
2021-03-18 21:40:20 +01:00
|
|
|
|
CharInfo charInfo = new CharInfo();
|
|
|
|
|
|
|
|
|
|
charInfo.SetFromStoreData(StoreData.BuildDefault(utilityImpl, 0));
|
|
|
|
|
|
2021-11-16 00:03:17 +01:00
|
|
|
|
charInfo.Nickname = Nickname.FromString(nickname);
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
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>()
|
|
|
|
|
};
|
2022-11-16 20:30:12 +01:00
|
|
|
|
"Ryujinx"u8.CopyTo(registerInfo.Nickname.AsSpan());
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
2023-03-22 00:14:46 +01:00
|
|
|
|
virtualAmiiboFile = JsonSerializer.Deserialize<VirtualAmiiboFile>(File.ReadAllText(filePath), new JsonSerializerOptions(JsonSerializerDefaults.General));
|
2021-03-18 21:40:20 +01:00
|
|
|
|
}
|
|
|
|
|
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");
|
2023-03-22 00:14:46 +01:00
|
|
|
|
|
|
|
|
|
File.WriteAllText(filePath, JsonSerializer.Serialize(virtualAmiiboFile));
|
2021-03-18 21:40:20 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-16 00:03:17 +01:00
|
|
|
|
}
|