Skip to content

Commit

Permalink
Fix Gen3 checksum calc
Browse files Browse the repository at this point in the history
Also when checking the active save, check the block0 for both save files
since it is always updated.
  • Loading branch information
kwsch committed Jul 29, 2016
1 parent 2365e97 commit 9b3d6e5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
40 changes: 31 additions & 9 deletions Saves/SAV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,33 @@ public SAV3(byte[] data = null, GameVersion versionOverride = GameVersion.Any)
else Version = SaveUtil.getIsG3SAV(Data);
if (Version == GameVersion.Invalid)
return;


int[] BlockOrder1 = new int[14];
for (int i = 0; i < 14; i++)
BlockOrder1[i] = BitConverter.ToInt16(Data, i*0x1000 + 0xFF4);
int zeroBlock1 = Array.IndexOf(BlockOrder1, 0);

if (data.Length > SaveUtil.SIZE_G3RAWHALF)
{
int[] BlockOrder2 = new int[14];
for (int i = 0; i < 14; i++)
BlockOrder2[i] = BitConverter.ToInt16(Data, 0xE000 + i*0x1000 + 0xFF4);
int zeroBlock2 = Array.IndexOf(BlockOrder2, 0);

ActiveSAV = BitConverter.ToUInt32(Data, zeroBlock1*0x1000 + 0xFFC) >
BitConverter.ToUInt32(Data, zeroBlock2*0x1000 + 0xEFFC)
? 0
: 1;
BlockOrder = ActiveSAV == 0 ? BlockOrder1 : BlockOrder2;
}
else
{
ActiveSAV = 0;
BlockOrder = BlockOrder1;
}

BlockOrder = new int[14];
BlockOfs = new int[14];
ActiveSAV = SaveUtil.SIZE_G3RAWHALF == data.Length || BitConverter.ToUInt32(Data, 0xFFC) > BitConverter.ToUInt32(Data, 0xEFFC) ? 0 : 1;
for (int i = 0; i < 14; i++)
BlockOrder[i] = BitConverter.ToInt16(Data, ABO + i*0x1000 + 0xFF4);
for (int i = 0; i < 14; i++)
BlockOfs[i] = Array.IndexOf(BlockOrder, i)*0x1000 + ABO;

Expand Down Expand Up @@ -86,7 +107,7 @@ public SAV3(byte[] data = null, GameVersion versionOverride = GameVersion.Any)
OFS_PouchBalls = BlockOfs[1] + 0x0430;
OFS_PouchTMHM = BlockOfs[1] + 0x0464;
OFS_PouchBerry = BlockOfs[1] + 0x054C;
Personal = PersonalTable.FR; // todo split FR & LG
Personal = PersonalTable.FR;
break;
case GameVersion.E:
LegalKeyItems = Legal.Pouch_Key_E;
Expand Down Expand Up @@ -161,7 +182,7 @@ protected override void setChecksums()
{
byte[] chunk = Data.Skip(ABO + i*0x1000).Take(chunkLength[BlockOrder[i]]).ToArray();
ushort chk = SaveUtil.check32(chunk);
BitConverter.GetBytes(chk).CopyTo(Data, ABO + i + 0xFF4);
BitConverter.GetBytes(chk).CopyTo(Data, ABO + i*0x1000 + 0xFF6);
}
}
public override bool ChecksumsValid
Expand All @@ -172,7 +193,7 @@ public override bool ChecksumsValid
{
byte[] chunk = Data.Skip(ABO + i * 0x1000).Take(chunkLength[BlockOrder[i]]).ToArray();
ushort chk = SaveUtil.check32(chunk);
if (chk != BitConverter.ToUInt16(Data, ABO + i*0xFF4))
if (chk != BitConverter.ToUInt16(Data, ABO + i*0x1000 + 0xFF6))
return false;
}
return true;
Expand All @@ -187,8 +208,9 @@ public override string ChecksumInfo
{
byte[] chunk = Data.Skip(ABO + i * 0x1000).Take(chunkLength[BlockOrder[i]]).ToArray();
ushort chk = SaveUtil.check32(chunk);
if (chk != BitConverter.ToUInt16(Data, ABO + i * 0xFF4))
r += $"Block {BlockOrder[i]} @ {i*0x1000} (len {chunkLength[BlockOrder[i]]}) invalid." + Environment.NewLine;
ushort old = BitConverter.ToUInt16(Data, ABO + i*0x1000 + 0xFF6);
if (chk != old)
r += $"Block {BlockOrder[i].ToString("00")} @ {(i*0x1000).ToString("X5")} invalid." + Environment.NewLine;
}
return r.Length == 0 ? "Checksums valid." : r.TrimEnd();
}
Expand Down
2 changes: 1 addition & 1 deletion Saves/SaveUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ internal static ushort check32(byte[] data)
uint val = 0;
for (int i = 0; i < data.Length; i += 4)
val += BitConverter.ToUInt32(data, i);
return (ushort)(val + val >> 16);
return (ushort)((val & 0xFFFF) + (val >> 16));
}

public static int getDexFormIndexXY(int species, int formct)
Expand Down

0 comments on commit 9b3d6e5

Please sign in to comment.