From 5c633d205c4c003ef6ecaa62dba523f404c59cbe Mon Sep 17 00:00:00 2001 From: Kurt Date: Fri, 3 Nov 2017 21:15:36 -0700 Subject: [PATCH] misc changes add class/struct array to csv table with named columns utility slight usum prep / cleanup --- pk3DS.Core/CTR/ETC1.cs | 2 +- pk3DS.Core/Game/GameInfo.cs | 6 ++-- pk3DS.Core/Legality/Tables7.cs | 4 +++ pk3DS.Core/TableUtil.cs | 55 ++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 pk3DS.Core/TableUtil.cs diff --git a/pk3DS.Core/CTR/ETC1.cs b/pk3DS.Core/CTR/ETC1.cs index a89da15..2a940de 100644 --- a/pk3DS.Core/CTR/ETC1.cs +++ b/pk3DS.Core/CTR/ETC1.cs @@ -6,6 +6,6 @@ namespace pk3DS.Core.CTR internal static class ETC1 { [DllImport("ETC1Lib.dll", EntryPoint = "ConvertETC1", CallingConvention = CallingConvention.Cdecl)] - public static extern void ConvertETC1(IntPtr dataOut, ref uint dataOutSize, IntPtr dataIn, UInt16 width, UInt16 height, bool alpha); + public static extern void ConvertETC1(IntPtr dataOut, ref uint dataOutSize, IntPtr dataIn, ushort width, ushort height, bool alpha); } } \ No newline at end of file diff --git a/pk3DS.Core/Game/GameInfo.cs b/pk3DS.Core/Game/GameInfo.cs index 07879be..af6ac1c 100644 --- a/pk3DS.Core/Game/GameInfo.cs +++ b/pk3DS.Core/Game/GameInfo.cs @@ -50,9 +50,9 @@ private void LoadSM() private void LoadUSUM() { - MaxSpeciesID = Legal.MaxSpeciesID_7; - MaxMoveID = Legal.MaxMoveID_7; - MaxItemID = Legal.MaxItemID_7; + MaxSpeciesID = Legal.MaxSpeciesID_7_USUM; + MaxMoveID = Legal.MaxMoveID_7_USUM; + MaxItemID = Legal.MaxItemID_7_USUM; HeldItems = Legal.HeldItems_SM; MaxAbilityID = Legal.MaxAbilityID_7; } diff --git a/pk3DS.Core/Legality/Tables7.cs b/pk3DS.Core/Legality/Tables7.cs index 55e12ed..88995e0 100644 --- a/pk3DS.Core/Legality/Tables7.cs +++ b/pk3DS.Core/Legality/Tables7.cs @@ -11,6 +11,10 @@ public static partial class Legal internal const int MaxBallID_7 = 0x1A; // 26 internal const int MaxGameID_7 = 41; // Crystal (VC?) + internal const int MaxSpeciesID_7_USUM = 802; + internal const int MaxMoveID_7_USUM = 720; + internal const int MaxItemID_7_USUM = 920; + #region Met Locations internal static readonly int[] Met_SMc = { 0, 60002, 30002, }; diff --git a/pk3DS.Core/TableUtil.cs b/pk3DS.Core/TableUtil.cs new file mode 100644 index 0000000..eadcbf3 --- /dev/null +++ b/pk3DS.Core/TableUtil.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace pk3DS.Core +{ + public static class TableUtil + { + private const string sep = "\t"; + + public static string GetTable(IEnumerable arr, IList names, string name = null) where T : new() + { + var list = GetTableRaw(arr).ToArray(); + + // slap in name to column header + list[0] = $"{name ?? typeof(T).Name}{sep}{list[0]}"; + + // slap in row name to row + for (int i = 1; i < list.Length; i++) + list[i] = $"{names[i - 1]}{sep}{list[i]}"; + + return string.Join(Environment.NewLine, list); + } + public static string GetTable(IEnumerable arr) where T : new() + { + return string.Join(Environment.NewLine, GetTableRaw(arr)); + } + + private static IEnumerable GetTableRaw(IEnumerable arr) where T : new() + { + return Table(arr).Select(row => string.Join(sep, row)); + } + private static IEnumerable> Table(IEnumerable arr) where T : new() + { + var type = typeof(T); + yield return GetNames(type); + foreach (var z in arr) + yield return GetValues(z, type); + } + private static IEnumerable GetNames(Type type) + { + foreach (var z in type.GetProperties()) + yield return z.Name; + foreach (var z in type.GetFields()) + yield return z.Name; + } + private static IEnumerable GetValues(object obj, Type type) + { + foreach (var z in type.GetProperties()) + yield return z.GetValue(obj, null)?.ToString() ?? ""; + foreach (var z in type.GetFields()) + yield return z.GetValue(obj)?.ToString() ?? ""; + } + } +}