Skip to content

Commit

Permalink
handle SCBlock changes in PKHeX (#88)
Browse files Browse the repository at this point in the history
* handle SCBlock changes in PKHeX
update the reflection to use Memory and the Raw field of SCBlock.

* SCBlock changes
handles the PKHeX signature changes for outbreak reads as well
  • Loading branch information
santacrab2 authored Jan 19, 2025
1 parent 2d5ff24 commit 6ce38d7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
10 changes: 5 additions & 5 deletions TeraFinder.Core/Classes/GameCoordinates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ public class GameCoordinates(SCBlock coordinates)
{
protected readonly SCBlock Coordinates = coordinates;

public float X { get => ReadSingleLittleEndian(Coordinates.Data.AsSpan()); set => SetX(value); }
public float Y { get => ReadSingleLittleEndian(Coordinates.Data.AsSpan()[4..]); set => SetY(value); }
public float Z { get => ReadSingleLittleEndian(Coordinates.Data.AsSpan()[8..]); set => SetZ(value); }
public float X { get => ReadSingleLittleEndian(Coordinates.Data); set => SetX(value); }
public float Y { get => ReadSingleLittleEndian(Coordinates.Data[4..]); set => SetY(value); }
public float Z { get => ReadSingleLittleEndian(Coordinates.Data[8..]); set => SetZ(value); }

private void SetX(float x) => SetCoordinates(0, x);
private void SetY(float y) => SetCoordinates(4, y);
private void SetZ(float z) => SetCoordinates(8, z);

private void SetCoordinates(int index, float value) => WriteSingleLittleEndian(Coordinates.Data.AsSpan()[index..], value);
private void SetCoordinates(int index, float value) => WriteSingleLittleEndian(Coordinates.Data[index..], value);

public ReadOnlySpan<byte> GetCoordinates() => Coordinates.Data.AsSpan();
public ReadOnlySpan<byte> GetCoordinates() => Coordinates.Data;
public void SetCoordinates(ReadOnlySpan<byte> coordinates) => Coordinates.ChangeData(coordinates);
}
24 changes: 12 additions & 12 deletions TeraFinder.Core/Utils/BlockUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ namespace TeraFinder.Core;

public static class BlockUtil
{
private static SCBlock CreateBlock(uint key, SCTypeCode dummy, ReadOnlySpan<byte> data)
private static SCBlock CreateBlock(uint key, SCTypeCode dummy, Memory<byte> data)
{
Type type = typeof(SCBlock);
var instance = type.Assembly.CreateInstance(
type.FullName!, false,
BindingFlags.Instance | BindingFlags.NonPublic,
null, [key, dummy, data.ToArray()], null, null
null, [key, dummy, data], null, null
);
return (SCBlock)instance!;
}

public static SCBlock CreateObjectBlock(uint key, ReadOnlySpan<byte> data) => CreateBlock(key, SCTypeCode.Object, data);
public static SCBlock CreateObjectBlock(uint key, Memory<byte> data) => CreateBlock(key, SCTypeCode.Object, data);

public static SCBlock CreateDummyBlock(uint key, SCTypeCode dummy) => CreateBlock(key, dummy, Array.Empty<byte>());

Expand All @@ -31,32 +31,32 @@ public static void AddBlockToFakeSAV(SAV9SV sav, SCBlock block)
typeInfo.SetValue(sav.Blocks, list);
}

public static void EditBlock(SCBlock block, SCTypeCode type, ReadOnlySpan<byte> data)
public static void EditBlock(SCBlock block, SCTypeCode type, Memory<byte> data)
{
EditBlockType(block, type);
var dataInfo = typeof(SCBlock).GetField("Data", BindingFlags.Instance | BindingFlags.Public)!;
dataInfo.SetValue(block, data.ToArray());
var dataInfo = typeof(SCBlock).GetField("Raw", BindingFlags.Instance | BindingFlags.Public)!;
dataInfo.SetValue(block, data);
}

public static void EditBlock(SCBlock block, SCTypeCode type, uint value)
{
EditBlockType(block, type);
var dataInfo = typeof(SCBlock).GetField("Data", BindingFlags.Instance | BindingFlags.Public)!;
dataInfo.SetValue(block, BitConverter.GetBytes(value));
var dataInfo = typeof(SCBlock).GetField("Raw", BindingFlags.Instance | BindingFlags.Public)!;
dataInfo.SetValue(block, BitConverter.GetBytes(value).AsMemory());
}

public static void EditBlock(SCBlock block, SCTypeCode type, int value)
{
EditBlockType(block, type);
var dataInfo = typeof(SCBlock).GetField("Data", BindingFlags.Instance | BindingFlags.Public)!;
dataInfo.SetValue(block, BitConverter.GetBytes(value));
var dataInfo = typeof(SCBlock).GetField("Raw", BindingFlags.Instance | BindingFlags.Public)!;
dataInfo.SetValue(block, BitConverter.GetBytes(value).AsMemory());
}

public static void EditBlock(SCBlock block, SCTypeCode type, byte value)
{
EditBlockType(block, type);
var dataInfo = typeof(SCBlock).GetField("Data", BindingFlags.Instance | BindingFlags.Public)!;
dataInfo.SetValue(block, new byte[] { value });
var dataInfo = typeof(SCBlock).GetField("Raw", BindingFlags.Instance | BindingFlags.Public)!;
dataInfo.SetValue(block, new byte[] { value }.AsMemory());
}

public static void EditBlockType(SCBlock block, SCTypeCode type)
Expand Down
8 changes: 4 additions & 4 deletions TeraFinder.Core/Utils/EventUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public static (string distRewards, string mightyRewards) GetCurrentEventRewards(
{
var KBCATFixedRewardItemArray = sav.Accessor.FindOrDefault(BlockDefinitions.KBCATFixedRewardItemArray.Key).Data;
var KBCATLotteryRewardItemArray = sav.Accessor.FindOrDefault(BlockDefinitions.KBCATLotteryRewardItemArray.Key).Data;
var tableDrops = pkNX.Structures.FlatBuffers.FlatBufferConverter.DeserializeFrom<DeliveryRaidFixedRewardItemArray>(KBCATFixedRewardItemArray);
var tableBonus = pkNX.Structures.FlatBuffers.FlatBufferConverter.DeserializeFrom<DeliveryRaidLotteryRewardItemArray>(KBCATLotteryRewardItemArray);
var tableDrops = pkNX.Structures.FlatBuffers.FlatBufferConverter.DeserializeFrom<DeliveryRaidFixedRewardItemArray>(KBCATFixedRewardItemArray.ToArray());
var tableBonus = pkNX.Structures.FlatBuffers.FlatBufferConverter.DeserializeFrom<DeliveryRaidLotteryRewardItemArray>(KBCATLotteryRewardItemArray.ToArray());
var opt = new JsonSerializerOptions { WriteIndented = true };
var drops = JsonSerializer.Serialize(tableDrops, opt);
var lottery = JsonSerializer.Serialize(tableBonus, opt);
Expand All @@ -56,7 +56,7 @@ private static (byte[] distData, byte[] mightyData) GetEventEncounterData(SAV9SV
var type3list = new List<byte[]>();

var KBCATRaidEnemyArray = sav.Accessor.FindOrDefault(BlockDefinitions.KBCATRaidEnemyArray.Key).Data;
var tableEncounters = pkNX.Structures.FlatBuffers.FlatBufferConverter.DeserializeFrom<DeliveryRaidEnemyTableArray>(KBCATRaidEnemyArray);
var tableEncounters = pkNX.Structures.FlatBuffers.FlatBufferConverter.DeserializeFrom<DeliveryRaidEnemyTableArray>(KBCATRaidEnemyArray.ToArray());

var byGroupID = tableEncounters.Table
.Where(z => z.Info.Rate != 0)
Expand Down Expand Up @@ -101,7 +101,7 @@ private static (byte[] distData, byte[] mightyData) GetEventEncounterData(SAV9SV
var KBCATRaidPriorityArray = sav.Accessor.FindOrDefault(BlockDefinitions.KBCATRaidPriorityArray.Key);
if (KBCATRaidPriorityArray.Type is not SCTypeCode.None && KBCATRaidPriorityArray.Data.Length > 0)
{
try { return pkNX.Structures.FlatBuffers.FlatBufferConverter.DeserializeFrom<DeliveryRaidPriorityArray>(KBCATRaidPriorityArray.Data).Table.First(); }
try { return pkNX.Structures.FlatBuffers.FlatBufferConverter.DeserializeFrom<DeliveryRaidPriorityArray>(KBCATRaidPriorityArray.Data.ToArray()).Table.First(); }
catch { }
}
return null;
Expand Down
14 changes: 7 additions & 7 deletions TeraFinder.Plugins/Forms/ConnectionForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private async void btnConnect_Click(object sender, EventArgs e)
return;
}
}

private async Task DownloadEventData(CancellationToken token)
{
var KBCATEventRaidIdentifier = SAV.Accessor.FindOrDefault(BlockDefinitions.KBCATEventRaidIdentifier.Key);
Expand Down Expand Up @@ -264,7 +264,7 @@ private async Task DownloadOutbreaksMainData(CancellationToken token)
if (KMassOutbreakAmount.Type is not SCTypeCode.None)
KMassOutbreakAmount.ChangeData((new byte[] { (byte)KMassOutbreakAmountData! }).AsSpan());
else
BlockUtil.EditBlock(KMassOutbreakAmount, BlockDefinitions.KOutbreakMainNumActive.Type, (new byte[] { (byte)KMassOutbreakAmountData! }).AsSpan());
BlockUtil.EditBlock(KMassOutbreakAmount, BlockDefinitions.KOutbreakMainNumActive.Type, (new byte[] { (byte)KMassOutbreakAmountData! }).AsMemory());

UpdateProgress(CurrentProgress++, MaxProgress);

Expand Down Expand Up @@ -359,7 +359,7 @@ private async Task DownloadEventOutbreaksMainData(CancellationToken token)
if (KMassOutbreakAmount.Type is not SCTypeCode.None)
KMassOutbreakAmount.ChangeData((new byte[] { (byte)KMassOutbreakAmountData! }).AsSpan());
else
BlockUtil.EditBlock(KMassOutbreakAmount, BlockDefinitions.KOutbreakBCMainNumActive.Type, (new byte[] { (byte)KMassOutbreakAmountData! }).AsSpan());
BlockUtil.EditBlock(KMassOutbreakAmount, BlockDefinitions.KOutbreakBCMainNumActive.Type, (new byte[] { (byte)KMassOutbreakAmountData! }).AsMemory());

UpdateProgress(CurrentProgress++, MaxProgress);

Expand Down Expand Up @@ -456,7 +456,7 @@ private async Task DownloadOutbreaksDLCData(CancellationToken token)
if (KMassOutbreakAmount.Type is not SCTypeCode.None)
KMassOutbreakAmount.ChangeData((new byte[] { (byte)KMassOutbreakAmountData! }).AsSpan());
else
BlockUtil.EditBlock(KMassOutbreakAmount, BlockDefinitions.KOutbreakDLC1NumActive.Type, (new byte[] { (byte)KMassOutbreakAmountData! }).AsSpan());
BlockUtil.EditBlock(KMassOutbreakAmount, BlockDefinitions.KOutbreakDLC1NumActive.Type, (new byte[] { (byte)KMassOutbreakAmountData! }).AsMemory());

UpdateProgress(CurrentProgress++, MaxProgress);

Expand Down Expand Up @@ -567,7 +567,7 @@ private async Task DownloadEventOutbreaksDLCData(CancellationToken token)
if (KMassOutbreakAmount.Type is not SCTypeCode.None)
KMassOutbreakAmount.ChangeData((new byte[] { (byte)KMassOutbreakAmountData! }).AsSpan());
else
BlockUtil.EditBlock(KMassOutbreakAmount, BlockDefinitions.KOutbreakBCDLC1NumActive.Type, (new byte[] { (byte)KMassOutbreakAmountData! }).AsSpan());
BlockUtil.EditBlock(KMassOutbreakAmount, BlockDefinitions.KOutbreakBCDLC1NumActive.Type, (new byte[] { (byte)KMassOutbreakAmountData! }).AsMemory());

UpdateProgress(CurrentProgress++, MaxProgress);

Expand Down Expand Up @@ -678,7 +678,7 @@ private async Task DownloadOutbreaksDLC2Data(CancellationToken token)
if (KMassOutbreakAmount.Type is not SCTypeCode.None)
KMassOutbreakAmount.ChangeData((new byte[] { (byte)KMassOutbreakAmountData! }).AsSpan());
else
BlockUtil.EditBlock(KMassOutbreakAmount, BlockDefinitions.KOutbreakDLC2NumActive.Type, (new byte[] { (byte)KMassOutbreakAmountData! }).AsSpan());
BlockUtil.EditBlock(KMassOutbreakAmount, BlockDefinitions.KOutbreakDLC2NumActive.Type, (new byte[] { (byte)KMassOutbreakAmountData! }).AsMemory());

UpdateProgress(CurrentProgress++, MaxProgress);

Expand Down Expand Up @@ -789,7 +789,7 @@ private async Task DownloadEventOutbreaksDLC2Data(CancellationToken token)
if (KMassOutbreakAmount.Type is not SCTypeCode.None)
KMassOutbreakAmount.ChangeData((new byte[] { (byte)KMassOutbreakAmountData! }).AsSpan());
else
BlockUtil.EditBlock(KMassOutbreakAmount, BlockDefinitions.KOutbreakBCDLC2NumActive.Type, (new byte[] { (byte)KMassOutbreakAmountData! }).AsSpan());
BlockUtil.EditBlock(KMassOutbreakAmount, BlockDefinitions.KOutbreakBCDLC2NumActive.Type, (new byte[] { (byte)KMassOutbreakAmountData! }).AsMemory());

UpdateProgress(CurrentProgress++, MaxProgress);

Expand Down
2 changes: 1 addition & 1 deletion TeraFinder.Plugins/Forms/EditorForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ private async Task UpdateRemote()
{
var block = CurrMap is TeraRaidMapParent.Paldea ? BlockDefinitions.KTeraRaidPaldea : BlockDefinitions.KTeraRaidDLC;
var savBlock = SAV.Accessor.FindOrDefault(block.Key)!;
await Connection.Executor.WriteBlock(savBlock.Data, block, new CancellationToken()).ConfigureAwait(false);
await Connection.Executor.WriteBlock(savBlock.Data.ToArray(), block, new CancellationToken()).ConfigureAwait(false);
}
}
catch (Exception)
Expand Down
2 changes: 1 addition & 1 deletion TeraFinder.Plugins/TeraPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public uint GetOutbreakEventIdentifier()
if (enableBlock.Type == SCTypeCode.None || enableBlock.Type == SCTypeCode.Bool1)
return 0;
var pokeDataBlock = SAV.Accessor.FindOrDefault(BlockDefinitions.KBCATOutbreakPokeData.Key);
var tablePokeData = FlatBufferConverter.DeserializeFrom<DeliveryOutbreakPokeDataArray>(pokeDataBlock.Data);
var tablePokeData = FlatBufferConverter.DeserializeFrom<DeliveryOutbreakPokeDataArray>(pokeDataBlock.Data.ToArray());
return tablePokeData.Table[0].ID > 0 ? uint.Parse($"{tablePokeData.Table[0].ID}"[..8]) : 0;
}

Expand Down

0 comments on commit 6ce38d7

Please sign in to comment.