diff --git a/NHSE.Parsing/BCSV/BCSV.cs b/NHSE.Parsing/BCSV/BCSV.cs index 10818ae..4c55731 100644 --- a/NHSE.Parsing/BCSV/BCSV.cs +++ b/NHSE.Parsing/BCSV/BCSV.cs @@ -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) diff --git a/NHSE.Parsing/BCSV/BCSVConverter.cs b/NHSE.Parsing/BCSV/BCSVConverter.cs index 207695d..f0deabd 100644 --- a/NHSE.Parsing/BCSV/BCSVConverter.cs +++ b/NHSE.Parsing/BCSV/BCSVConverter.cs @@ -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); + } } -} \ No newline at end of file +} diff --git a/NHSE.Parsing/GameBCSVDumper.cs b/NHSE.Parsing/GameBCSVDumper.cs index 4b9db21..b5a8c4b 100644 --- a/NHSE.Parsing/GameBCSVDumper.cs +++ b/NHSE.Parsing/GameBCSVDumper.cs @@ -2,13 +2,76 @@ using System.Collections.Generic; using System.ComponentModel; using System.IO; +using System.Linq; using NHSE.Core; namespace NHSE.Parsing { + /// + /// Converts files to other formats digestible by the program. + /// public static class GameBCSVDumper { - public static byte[] GetItemTypeArray(string pathBCSV, string fn = "ItemParam.bcsv") + /// + /// Dumps everything the program uses to the provided , using the provided . + /// + /// Source of files. + /// Destination folder where the dumps will be saved. + public static void UpdateDumps(string pathBCSV, string dest) + { + BCSVConverter.DumpAll(pathBCSV, dest); + var itemNames = GameInfo.Strings.itemlist; + + void DumpS(string fn, IEnumerable 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 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(); + 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 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(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 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 GetRecipeList(string pathBCSV, string fn = "RecipeCra public static List 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 GetMilestoneList(string pathBCSV, string fn = "EventF result.Sort(); return result; } + + public static IEnumerable GetInsectList(string pathBCSV, string fn = "InsectStatusParam.bcsv") + { + return GetItemList(pathBCSV, fn, 0x20CB67BC); + } + + public static IEnumerable GetFishList(string pathBCSV, string fn = "FishStatusParam.bcsv") + { + return GetItemList(pathBCSV, fn, 0x20CB67BC); + } + + public static IEnumerable GetInsectList(string pathBCSV, IReadOnlyList items, string fn = "InsectStatusParam.bcsv") + { + var insects = GetInsectList(pathBCSV, fn); + return insects.Select(z => $"{z:00000}, // {items[z]}"); + } + + public static IEnumerable GetFishList(string pathBCSV, IReadOnlyList items, string fn = "FishStatusParam.bcsv") + { + var insects = GetFishList(pathBCSV, fn); + return insects.Select(z => $"{z:00000}, // {items[z]}"); + } + + private static IEnumerable 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(); + 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; + } } }