Replace all instances of Town Bytes

Attempt to resolve Issue #7
This commit is contained in:
Kurt 2015-09-19 20:36:45 -07:00
parent 66a0a0d522
commit 9a7ea098ee
2 changed files with 34 additions and 3 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace NLSE
@ -70,5 +71,29 @@ internal static int getIndex(ComboBox cb)
return val;
}
// Find Code off of Reference
internal static int IndexOfBytes(byte[] array, byte[] pattern, int startIndex, int count)
{
int i = startIndex;
int endIndex = count > 0 ? startIndex + count : array.Length;
int fidx = 0;
while (i++ != endIndex - 1)
{
if (array[i] != pattern[fidx]) i -= fidx;
fidx = (array[i] == pattern[fidx]) ? ++fidx : 0;
if (fidx == pattern.Length)
return i - fidx + 1;
}
return -1;
}
internal static void ReplaceAllBytes(byte[] array, byte[] oldPattern, byte[] newPattern)
{
if (oldPattern.SequenceEqual(newPattern))
return;
int offset; // Loop until no instances of oldPattern are found
while ((offset = IndexOfBytes(array, oldPattern, 0, 0)) != -1)
Array.Copy(newPattern, 0, array, offset, newPattern.Length);
}
}
}

View File

@ -192,12 +192,18 @@ public GardenData(byte[] data)
}
public byte[] Write()
{
// Replace all instances of Town Data with Updated Data
byte[] oldTownIDData = Data.Skip(0x5C7B8).Take(0x14).ToArray();
byte[] newTownIDData = new[]
{
(byte) ((Data[0x5C7B8] & 0xFC) | TownHallColor),
(byte) ((Data[0x5C7B9] & 0xFC) | TrainStationColor)
}.Concat(Encoding.Unicode.GetBytes(TownName.PadRight(9, '\0'))).ToArray();
Util.ReplaceAllBytes(Data, oldTownIDData, newTownIDData);
Data[0x4DA81] = (byte)GrassType;
Data[0x5C7B8] = (byte)((Data[0x5C7B8] & 0xFC) | TownHallColor);
Data[0x5C7B9] = (byte)((Data[0x5C7B9] & 0xFC) | TrainStationColor);
Data[0x5C836] = (byte)NativeFruit;
Array.Copy(Encoding.Unicode.GetBytes(TownName.PadRight(9, '\0')), 0, Data, 0x5C7BA, 0x12);
Array.Copy(BitConverter.GetBytes(SecondsPlayed), 0, Data, 0x5C7B0, 4);
Array.Copy(BitConverter.GetBytes(PlayDays), 0, Data, 0x5C83A, 2);
return Data;