Add support for importing 08 (8byte) cheats

Closes #303
This commit is contained in:
Kurt 2020-06-08 10:55:06 -07:00
parent 8302184c43
commit eccc762998

View File

@ -18,20 +18,51 @@ public static byte[] ReadCode(string paste)
public static byte[] ReadCode(IEnumerable<string> lines)
{
var result = new List<byte>();
foreach (var l in lines)
foreach (var line in lines)
{
var lastSpace = l.LastIndexOf(' ');
if (lastSpace < 0)
var l = line.Trim();
var chunkCount = GetChunkCount(l);
if (chunkCount == 0)
continue;
var lastChunk = l.Substring(lastSpace + 1);
if (!uint.TryParse(lastChunk, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out var hex))
continue;
var bytes = BitConverter.GetBytes(hex);
result.AddRange(bytes);
// Since the codes are stored BB AA, we need to import them in reverse (AA BB)
int indexOfSpace = l.Length - 1;
for (int i = 0; i < chunkCount; i++)
{
indexOfSpace = l.LastIndexOf(' ', indexOfSpace);
if (indexOfSpace < 0)
continue;
var length = l.Length - indexOfSpace - 1;
if (length < 8)
continue;
var lastChunk = l.Substring(indexOfSpace + 1, 8);
AddU32Chunk(lastChunk, result);
indexOfSpace -= 2;
}
}
return result.ToArray();
}
private static int GetChunkCount(string line)
{
if (line.Length < 2)
return 0;
// 08 or 04
var second = line[1];
return second == '8' ? 2 : 1;
}
private static void AddU32Chunk(string lastChunk, List<byte> result)
{
if (!uint.TryParse(lastChunk, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out var hex))
return;
var bytes = BitConverter.GetBytes(hex);
result.AddRange(bytes);
}
public static IEnumerable<string> WriteCode(IEnumerable<Item> items, uint offset)
{
int ctr = 0;