Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the functionality to load the Mii from Bin Amiibos. #438

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/Ryujinx.HLE/HOS/Horizon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,9 @@ public void SimulateWakeUpMessage()

public void ScanAmiibo(int nfpDeviceId, string amiiboId, bool useRandomUuid)
{
if (VirtualAmiibo.ApplicationBytes.Length > 0)
if (VirtualAmiibo.VirtualAmiiboBinFile != null)
{
VirtualAmiibo.ApplicationBytes = Array.Empty<byte>();
VirtualAmiibo.InputBin = string.Empty;
VirtualAmiibo.VirtualAmiiboBinFile.SaveFile();
}
if (NfpDevices[nfpDeviceId].State == NfpDeviceState.SearchingForTag)
{
Expand All @@ -353,13 +352,7 @@ public void ScanAmiibo(int nfpDeviceId, string amiiboId, bool useRandomUuid)
}
public void ScanAmiiboFromBin(string path)
{
VirtualAmiibo.InputBin = path;
if (VirtualAmiibo.ApplicationBytes.Length > 0)
{
VirtualAmiibo.ApplicationBytes = Array.Empty<byte>();
}
byte[] encryptedData = File.ReadAllBytes(path);
VirtualAmiiboFile newFile = AmiiboBinReader.ReadBinFile(encryptedData);
VirtualAmiiboFile newFile = AmiiboBinReader.ReadBinFile(path);
if (SearchingForAmiibo(out int nfpDeviceId))
{
NfpDevices[nfpDeviceId].State = NfpDeviceState.TagFound;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Ryujinx.HLE.HOS.Services.Mii.Types;
using Ryujinx.HLE.HOS.Services.Nfc.Nfp;
using Ryujinx.HLE.HOS.Services.Settings;
using System;

Expand Down Expand Up @@ -154,6 +155,10 @@ protected override ResultCode Move(CreateId createId, int newIndex)

protected override ResultCode AddOrReplace(StoreData storeData)
{
if (VirtualAmiibo.VirtualAmiiboBinFile != null)
{
storeData = VirtualAmiibo.VirtualAmiiboBinFile.StoreData;
}
if (!_isSystem)
{
return ResultCode.PermissionDenied;
Expand Down
64 changes: 64 additions & 0 deletions src/Ryujinx.HLE/HOS/Services/Mii/Types/CharInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,70 @@ public void SetFromStoreData(StoreData storeData)
MoleY = storeData.CoreData.MoleY;
Reserved = 0;
}
public static StoreData BuildFromCharInfo(UtilityImpl utilImpl, CharInfo charInfo)
{
StoreData result = new()
{
CoreData = new CoreData
{
Nickname = charInfo.Nickname,
FontRegion = charInfo.FontRegion,
FavoriteColor = charInfo.FavoriteColor,
Gender = charInfo.Gender,
Height = charInfo.Height,
Build = charInfo.Build,
Type = charInfo.Type,
RegionMove = charInfo.RegionMove,
FacelineType = charInfo.FacelineType,
FacelineColor = charInfo.FacelineColor,
FacelineWrinkle = charInfo.FacelineWrinkle,
FacelineMake = charInfo.FacelineMake,
HairType = charInfo.HairType,
HairColor = charInfo.HairColor,
HairFlip = charInfo.HairFlip,
EyeType = charInfo.EyeType,
EyeColor = charInfo.EyeColor,
EyeScale = charInfo.EyeScale,
EyeAspect = charInfo.EyeAspect,
EyeRotate = charInfo.EyeRotate,
EyeX = charInfo.EyeX,
EyeY = charInfo.EyeY,
EyebrowType = charInfo.EyebrowType,
EyebrowColor = charInfo.EyebrowColor,
EyebrowScale = charInfo.EyebrowScale,
EyebrowAspect = charInfo.EyebrowAspect,
EyebrowRotate = charInfo.EyebrowRotate,
EyebrowX = charInfo.EyebrowX,
EyebrowY = charInfo.EyebrowY,
NoseType = charInfo.NoseType,
NoseScale = charInfo.NoseScale,
NoseY = charInfo.NoseY,
MouthType = charInfo.MouthType,
MouthColor = charInfo.MouthColor,
MouthScale = charInfo.MouthScale,
MouthAspect = charInfo.MouthAspect,
MouthY = charInfo.MouthY,
BeardColor = charInfo.BeardColor,
BeardType = charInfo.BeardType,
MustacheType = charInfo.MustacheType,
MustacheScale = charInfo.MustacheScale,
MustacheY = charInfo.MustacheY,
GlassType = charInfo.GlassType,
GlassColor = charInfo.GlassColor,
GlassScale = charInfo.GlassScale,
GlassY = charInfo.GlassY,
MoleType = charInfo.MoleType,
MoleScale = charInfo.MoleScale,
MoleX = charInfo.MoleX,
MoleY = charInfo.MoleY
}
};

result.UpdateCreateID(utilImpl);
result.UpdateCrc();

return result;
}

public readonly void SetSource(Source source)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Ryujinx.HLE/HOS/Services/Mii/Types/StoreData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ private void UpdateDeviceCrc()
DeviceCrc = CalculateDeviceCrc();
}

public void UpdateCreateID(UtilityImpl impl)
{
_createId = impl.MakeCreateId();
}

public void UpdateCrc()
{
UpdateDataCrc();
Expand Down
120 changes: 41 additions & 79 deletions src/Ryujinx.HLE/HOS/Services/Nfc/AmiiboDecryption/AmiiboBinReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ private static byte CalculateBCC1(byte[] uid)
return (byte)(uid[3] ^ uid[4] ^ uid[5] ^ uid[6]);
}

public static VirtualAmiiboFile ReadBinFile(byte[] fileBytes)
public static VirtualAmiiboFile ReadBinFile(string filePath)
{
Logger.Info?.Print(LogClass.ServiceNfp, "Reading bin file.");
byte[] fileBytes;
try
{
fileBytes = File.ReadAllBytes(filePath);
}
catch (Exception ex)
{
Logger.Error?.Print(LogClass.ServiceNfp, $"Error reading file: {ex.Message}");
return new VirtualAmiiboFile();
}
string keyRetailBinPath = GetKeyRetailBinPath();
if (string.IsNullOrEmpty(keyRetailBinPath))
{
Expand All @@ -39,6 +50,7 @@ public static VirtualAmiiboFile ReadBinFile(byte[] fileBytes)
byte[] newFileBytes = new byte[totalBytes];
Array.Copy(fileBytes, newFileBytes, fileBytes.Length);
fileBytes = newFileBytes;
Logger.Info?.Print(LogClass.ServiceNfp, "Added 8 bytes to the end of the file.");
}

AmiiboDecryptor amiiboDecryptor = new(keyRetailBinPath);
Expand All @@ -59,6 +71,7 @@ public static VirtualAmiiboFile ReadBinFile(byte[] fileBytes)
byte[] dataFull = amiiboDump.GetData();
Logger.Debug?.Print(LogClass.ServiceNfp, $"Data Full Length: {dataFull.Length}");
byte[] uid = new byte[7];
byte[] mii = new byte[98];
Array.Copy(dataFull, 0, uid, 0, 7);

byte bcc0 = CalculateBCC0(uid);
Expand Down Expand Up @@ -96,6 +109,10 @@ public static VirtualAmiiboFile ReadBinFile(byte[] fileBytes)
setID[0] = pageData[2];
formData = pageData[3];
break;
case >= 40 and <= 63:
int miiOffset = (page - 40) * 4;
Array.Copy(pageData, 0, mii, miiOffset, 4);
break;
case 64:
case 65:
// Extract title ID
Expand All @@ -113,7 +130,6 @@ public static VirtualAmiiboFile ReadBinFile(byte[] fileBytes)
break;
}
}

string usedCharacterStr = BitConverter.ToString(usedCharacter).Replace("-", "");
string variationStr = BitConverter.ToString(variation).Replace("-", "");
string amiiboIDStr = BitConverter.ToString(amiiboID).Replace("-", "");
Expand Down Expand Up @@ -141,14 +157,20 @@ public static VirtualAmiiboFile ReadBinFile(byte[] fileBytes)
LastWriteDate = writeDateTime,
WriteCounter = writeCounterValue,
};
if (writeCounterValue > 0)
VirtualAmiiboBinFile virtualAmiiboBinFile = new VirtualAmiiboBinFile
{
VirtualAmiibo.ApplicationBytes = applicationAreas;
}
VirtualAmiibo.NickName = nickName;
ApplicationBytes = applicationAreas,
MiiBytes = mii,
InputBin = filePath,
NickName = nickName,
WriteCounter = writeCounterValue,
LastWriteDate = writeDateTime,
TagUuid = uid,
};
VirtualAmiibo.VirtualAmiiboBinFile = virtualAmiiboBinFile;
return virtualAmiiboFile;
}
public static bool SaveBinFile(string inputFile, byte[] appData)
public static bool SaveBinFile(string inputFile, VirtualAmiiboBinFile virtualAmiiboBinFile)
{
Logger.Info?.Print(LogClass.ServiceNfp, "Saving bin file.");
byte[] readBytes;
Expand All @@ -168,12 +190,6 @@ public static bool SaveBinFile(string inputFile, byte[] appData)
return false;
}

if (appData.Length != 216) // Ensure application area size is valid
{
Logger.Error?.Print(LogClass.ServiceNfp, "Invalid application data length. Expected 216 bytes.");
return false;
}

if (readBytes.Length == 532)
{
// add 8 bytes to the end of the file
Expand All @@ -184,76 +200,22 @@ public static bool SaveBinFile(string inputFile, byte[] appData)

AmiiboDecryptor amiiboDecryptor = new AmiiboDecryptor(keyRetailBinPath);
AmiiboDump amiiboDump = amiiboDecryptor.DecryptAmiiboDump(readBytes);

byte[] oldData = amiiboDump.GetData();
if (oldData.Length != 540) // Verify the expected length for NTAG215 tags
{
Logger.Error?.Print(LogClass.ServiceNfp, "Invalid tag data length. Expected 540 bytes.");
return false;
}

byte[] newData = new byte[oldData.Length];
Array.Copy(oldData, newData, oldData.Length);

// Replace application area with appData
int appAreaOffset = 76 * 4; // Starting page (76) times 4 bytes per page
Array.Copy(appData, 0, newData, appAreaOffset, appData.Length);

AmiiboDump encryptedDump = amiiboDecryptor.EncryptAmiiboDump(newData);
byte[] encryptedData = encryptedDump.GetData();

if (encryptedData == null || encryptedData.Length != readBytes.Length)
{
Logger.Error?.Print(LogClass.ServiceNfp, "Failed to encrypt data correctly.");
return false;
}
inputFile = inputFile.Replace("_modified", string.Empty);
// Save the encrypted data to file or return it for saving externally
string outputFilePath = Path.Combine(Path.GetDirectoryName(inputFile), Path.GetFileNameWithoutExtension(inputFile) + "_modified.bin");
try
{
File.WriteAllBytes(outputFilePath, encryptedData);
Logger.Info?.Print(LogClass.ServiceNfp, $"Modified Amiibo data saved to {outputFilePath}.");
return true;
}
catch (Exception ex)
{
Logger.Error?.Print(LogClass.ServiceNfp, $"Error saving file: {ex.Message}");
return false;
}
}
public static bool SaveBinFile(string inputFile, string newNickName)
{
Logger.Info?.Print(LogClass.ServiceNfp, "Saving bin file.");
byte[] readBytes;
try
{
readBytes = File.ReadAllBytes(inputFile);
}
catch (Exception ex)
amiiboDump.AmiiboNickname = virtualAmiiboBinFile.NickName;
byte[] appData = virtualAmiiboBinFile.ApplicationBytes;
if (appData.Length != 216)
{
Logger.Error?.Print(LogClass.ServiceNfp, $"Error reading file: {ex.Message}");
return false;
}
string keyRetailBinPath = GetKeyRetailBinPath();
if (string.IsNullOrEmpty(keyRetailBinPath))
{
Logger.Error?.Print(LogClass.ServiceNfp, "Key retail path is empty.");
Logger.Error?.Print(LogClass.ServiceNfp, "Invalid application data length. Expected 216 bytes.");
return false;
}

if (readBytes.Length == 532)
{
// add 8 bytes to the end of the file
byte[] newFileBytes = new byte[540];
Array.Copy(readBytes, newFileBytes, readBytes.Length);
readBytes = newFileBytes;
}

AmiiboDecryptor amiiboDecryptor = new AmiiboDecryptor(keyRetailBinPath);
AmiiboDump amiiboDump = amiiboDecryptor.DecryptAmiiboDump(readBytes);
amiiboDump.AmiiboNickname = newNickName;
byte[] oldData = amiiboDump.GetData();
Array.Copy(appData, 0, oldData, 304, 216);
// apply write counter and write date change
byte[] writeCounter = BitConverter.GetBytes(virtualAmiiboBinFile.WriteCounter);
byte[] writeDate = BitConverter.GetBytes((ushort)virtualAmiiboBinFile.LastWriteDate.Day);
writeDate = BitConverter.GetBytes((ushort)(writeDate[0] | (virtualAmiiboBinFile.LastWriteDate.Month << 5) | (virtualAmiiboBinFile.LastWriteDate.Year - 2000) << 9));
Array.Copy(writeCounter, 0, oldData, 264, 2);
Array.Copy(writeDate, 0, oldData, 26, 2);

if (oldData.Length != 540) // Verify the expected length for NTAG215 tags
{
Logger.Error?.Print(LogClass.ServiceNfp, "Invalid tag data length. Expected 540 bytes.");
Expand Down
Loading