Merge in remainder of my dumping logic

This commit is contained in:
Kurt
2020-04-01 15:50:36 -07:00
parent 46840b7125
commit 5578f4898b
3 changed files with 123 additions and 21 deletions

View File

@@ -68,18 +68,14 @@ public BCSV(byte[] data)
private string ReadFieldUnknownType(in int offset, in int fieldIndex)
{
var length = GetFieldLength(fieldIndex);
// ReSharper disable once ConvertSwitchStatementToSwitchExpression
#pragma warning disable IDE0066 // Convert switch statement to expression
switch (length)
#pragma warning restore IDE0066 // Convert switch statement to expression
return length switch
{
case 1: return Data[offset].ToString();
case 2: return BitConverter.ToInt16(Data, offset).ToString();
case 4: return $"0x{BitConverter.ToUInt32(Data, offset):X8}";
case 8: return $"0x{BitConverter.ToUInt64(Data, offset):X16}";
default: return Encoding.UTF8.GetString(Data, offset, length);
}
1 => Data[offset].ToString(),
2 => BitConverter.ToInt16(Data, offset).ToString(),
4 => $"0x{BitConverter.ToUInt32(Data, offset):X8}",
8 => $"0x{BitConverter.ToUInt64(Data, offset):X16}",
_ => Encoding.UTF8.GetString(Data, offset, length),
};
}
private int GetFieldLength(in int i)

View File

@@ -14,7 +14,7 @@ public static class BCSVConverter
public static void DumpAll(string path, string dest, string delim = "\t")
{
var files = Directory.EnumerateFiles(path);
var files = Directory.EnumerateFiles(path, "*.bcsv");
Directory.CreateDirectory(dest);
foreach (var f in files)
{
@@ -32,5 +32,12 @@ public static void Dump(string path, string dest, string delim = "\t")
File.WriteAllLines(dest, result);
Console.WriteLine($"Dumped to CSV: {path}");
}
public static BCSV GetBCSV(string pathBCSV, string fn)
{
var path = Path.Combine(pathBCSV, fn);
var data = File.ReadAllBytes(path);
return new BCSV(data);
}
}
}
}

View File

@@ -2,13 +2,76 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using NHSE.Core;
namespace NHSE.Parsing
{
/// <summary>
/// Converts <see cref="BCSV"/> files to other formats digestible by the program.
/// </summary>
public static class GameBCSVDumper
{
public static byte[] GetItemTypeArray(string pathBCSV, string fn = "ItemParam.bcsv")
/// <summary>
/// Dumps everything the program uses to the provided <see cref="dest"/>, using the provided <see cref="pathBCSV"/>.
/// </summary>
/// <param name="pathBCSV">Source of <see cref="BCSV"/> files.</param>
/// <param name="dest">Destination folder where the dumps will be saved.</param>
public static void UpdateDumps(string pathBCSV, string dest)
{
BCSVConverter.DumpAll(pathBCSV, dest);
var itemNames = GameInfo.Strings.itemlist;
void DumpS(string fn, IEnumerable<string> lines)
{
File.WriteAllLines(Path.Combine(dest, fn), lines);
Console.WriteLine($"Created {fn}");
}
void DumpB(string fn, byte[] bytes)
{
File.WriteAllBytes(Path.Combine(dest, fn), bytes);
Console.WriteLine($"Created {fn}");
}
DumpS("milestones.txt", GetMilestoneList(pathBCSV));
DumpS("recipeDictionary.txt", GetRecipeList(pathBCSV));
DumpS("fish.txt", GetFishList(pathBCSV, itemNames));
DumpS("bugs.txt", GetInsectList(pathBCSV, itemNames));
DumpS("ItemKind.txt", GetItemKindsEnum(pathBCSV));
DumpB("item_kind.bin", GetItemKindArray(pathBCSV));
}
public static IEnumerable<string> GetItemKinds(string pathBCSV, string fn = "ItemParam.bcsv")
{
var path = Path.Combine(pathBCSV, fn);
var data = File.ReadAllBytes(path);
var bcsv = new BCSV(data);
var dict = bcsv.GetFieldDictionary();
var fType = dict[0xFC275E86];
var types = new HashSet<string>();
for (int i = 0; i < bcsv.EntryCount; i++)
{
var type = bcsv.ReadValue(i, fType).TrimEnd('\0');
if (!types.Contains(type))
types.Add(type);
}
return types;
}
private static IEnumerable<string> GetItemKindsEnum(string pathBCSV)
{
var kinds = GetItemKinds(pathBCSV);
var ordered = kinds.OrderBy(z => z);
return ordered.Select(z => $"{z},");
}
public static byte[] GetItemKindArray(string pathBCSV, string fn = "ItemParam.bcsv")
{
var path = Path.Combine(pathBCSV, fn);
var data = File.ReadAllBytes(path);
@@ -27,7 +90,7 @@ public static byte[] GetItemTypeArray(string pathBCSV, string fn = "ItemParam.bc
var type = bcsv.ReadValue(i, fType).TrimEnd('\0');
if (!Enum.TryParse<ItemKind>(type, out var k))
throw new InvalidEnumArgumentException($"{type} is not a known enum value @ index {i}.");
throw new InvalidEnumArgumentException($"{type} is not a known enum value @ index {i}. Update the enum index first.");
types.Add(ival, k);
if (ival > max)
@@ -43,9 +106,7 @@ public static byte[] GetItemTypeArray(string pathBCSV, string fn = "ItemParam.bc
public static List<string> GetRecipeList(string pathBCSV, string fn = "RecipeCraftParam.bcsv")
{
var path = Path.Combine(pathBCSV, fn);
var data = File.ReadAllBytes(path);
var bcsv = new BCSV(data);
var bcsv = BCSVConverter.GetBCSV(pathBCSV, fn);
var dict = bcsv.GetFieldDictionary();
var frecipe = dict[0x54706054];
@@ -72,9 +133,7 @@ public static List<string> GetRecipeList(string pathBCSV, string fn = "RecipeCra
public static List<string> GetMilestoneList(string pathBCSV, string fn = "EventFlagsLifeSupportAchievementParam.bcsv")
{
var path = Path.Combine(pathBCSV, fn);
var data = File.ReadAllBytes(path);
var bcsv = new BCSV(data);
var bcsv = BCSVConverter.GetBCSV(pathBCSV, fn);
var dict = bcsv.GetFieldDictionary();
var findex = dict[0x54706054];
@@ -97,5 +156,45 @@ public static List<string> GetMilestoneList(string pathBCSV, string fn = "EventF
result.Sort();
return result;
}
public static IEnumerable<ushort> GetInsectList(string pathBCSV, string fn = "InsectStatusParam.bcsv")
{
return GetItemList(pathBCSV, fn, 0x20CB67BC);
}
public static IEnumerable<ushort> GetFishList(string pathBCSV, string fn = "FishStatusParam.bcsv")
{
return GetItemList(pathBCSV, fn, 0x20CB67BC);
}
public static IEnumerable<string> GetInsectList(string pathBCSV, IReadOnlyList<string> items, string fn = "InsectStatusParam.bcsv")
{
var insects = GetInsectList(pathBCSV, fn);
return insects.Select(z => $"{z:00000}, // {items[z]}");
}
public static IEnumerable<string> GetFishList(string pathBCSV, IReadOnlyList<string> items, string fn = "FishStatusParam.bcsv")
{
var insects = GetFishList(pathBCSV, fn);
return insects.Select(z => $"{z:00000}, // {items[z]}");
}
private static IEnumerable<ushort> GetItemList(string pathBCSV, string fn, uint keyItemIDColumn)
{
var bcsv = BCSVConverter.GetBCSV(pathBCSV, fn);
var dict = bcsv.GetFieldDictionary();
var findex = dict[keyItemIDColumn];
var result = new List<ushort>();
for (int i = 0; i < bcsv.EntryCount; i++)
{
var iid = bcsv.ReadValue(i, findex);
var ival = ushort.Parse(iid);
result.Add(ival);
}
result.Sort();
return result;
}
}
}