Minor tweaks

Reduce allocation of button string fetch (for fun!)
Type return covariance for Editors (no longer limited by netframework!)
This commit is contained in:
Kurt
2022-10-02 10:24:44 -07:00
parent 33a2ab24f1
commit efb34f556b
8 changed files with 97 additions and 81 deletions

View File

@@ -103,7 +103,7 @@ private void PackSARC(BinaryWriter bw, ContainerHandler handler, CancellationTok
bw.Flush();
}
private void StartPack() { }
private static void StartPack() { } // do nothing?
private void WriteIntro(BinaryWriter bw, bool finalPass = false)
{
@@ -146,7 +146,7 @@ public override void Dump(string? path, ContainerHandler handler)
if (path == null)
throw new ArgumentNullException(nameof(path));
var folder = FileName ?? "sarc";
var folder = FileName ?? Identifier;
string dir = Path.Combine(path, folder);
Directory.CreateDirectory(dir);

View File

@@ -1,3 +1,4 @@
using System.Collections;
using pkNX.Containers;
using System.Collections.Generic;
using System.Diagnostics;
@@ -15,7 +16,7 @@ public sealed class PersonalTable8LA : IPersonalTable, IPersonalTable<PersonalIn
public int MaxSpeciesID => MaxSpecies;
private readonly IFileContainer File;
public PersonalTableLAfb Root { get; private set; }
public PersonalTableLAfb Root { get; }
public PersonalTable8LA(IFileContainer file)
{
@@ -38,7 +39,7 @@ public PersonalTable8LA(IFileContainer file)
Table = baseForms.Concat(formTable).ToArray();
}
private PersonalInfo8LA GetObj(PersonalInfoLAfb e, List<PersonalInfoLAfb> forms, ushort max, List<PersonalInfo8LA> formTable, int f = 0)
private static PersonalInfo8LA GetObj(PersonalInfoLAfb e, ICollection forms, ushort max, ICollection formTable, int f = 0)
{
return new PersonalInfo8LA(e)
{
@@ -116,45 +117,43 @@ public void FixMissingData()
Debug.Assert(l.DexIndexNational == s.DexIndexNational);
if (l.HP == 0)
{
l.SetPersonalInfo(s);
}
if (l.FormCount == 1)
continue;
var expectFormIndex = MaxSpeciesID + 1 + laFormCount;
if (l.FormStatsIndex != 0)
Debug.Assert(l.FormStatsIndex == (MaxSpeciesID + 1) + laFormCount);
Debug.Assert(l.FormStatsIndex == expectFormIndex);
l.FormStatsIndex = (MaxSpeciesID + 1) + laFormCount;
l.FormStatsIndex = expectFormIndex;
laFormCount += l.FormCount - 1;
for (byte f = 1; f < l.FormCount; f++)
{
var formL = Table[l.FormStatsIndex + (f - 1)];
if (formL.HP != 0)
continue;
if (formL.HP == 0)
// Check if SWSH table has form data for this entry
if (f < s.FormCount)
{
// Check if SWSH table has form data for this entry
if (f < s.FormCount)
if (s.FormCount <= l.FormCount || (FormInfo.HasBattleOnlyForm(i) && !FormInfo.IsBattleOnlyForm(i, f, 8)))
{
if (s.FormCount <= l.FormCount || (FormInfo.HasBattleOnlyForm(i) && !FormInfo.IsBattleOnlyForm(i, f, 8)))
{
var formS = ResourcesUtil.SWSH.GetFormEntry(i, f);
var formS = ResourcesUtil.SWSH.GetFormEntry(i, f);
Debug.Assert(formL.DexIndexNational == formS.DexIndexNational);
formL.SetPersonalInfo(formS);
}
}
else
{
// No form data was found, just write the base form data
formL.SetPersonalInfo(l);
Debug.Assert(formL.DexIndexNational == formS.DexIndexNational);
formL.SetPersonalInfo(formS);
}
}
else
{
// No form data was found, just write the base form data
formL.SetPersonalInfo(l);
}
}
}
Debug.WriteLine("Auto fix for PLA data succeded");
Debug.WriteLine("Auto fix for PLA data succeeded");
}
}

View File

@@ -143,6 +143,6 @@ public void FixMissingData()
}
}
Debug.WriteLine("Auto fix for SWSH data succeded");
Debug.WriteLine("Auto fix for SWSH data succeeded");
}
}

View File

@@ -12,8 +12,9 @@ namespace pkNX.WinForms.Controls;
internal class EditorGG : EditorBase
{
private GameData Data => ((GameManagerGG)ROM).Data;
protected internal EditorGG(GameManagerGG rom) : base(rom) { }
protected override GameManagerGG ROM { get; }
private GameData Data => ROM.Data;
protected internal EditorGG(GameManagerGG rom) => ROM = rom;
public void EditCommon()
{
@@ -232,7 +233,7 @@ private void PopWildEdit(GameFile type)
var data = file[0];
var obj = FlatBufferConverter.DeserializeFrom<EncounterArchive7b>(data);
using var form = new GGWE((GameManagerGG)ROM, obj);
using var form = new GGWE(ROM, obj);
if (form.ShowDialog() != DialogResult.OK)
return;

View File

@@ -15,8 +15,13 @@ namespace pkNX.WinForms.Controls;
internal class EditorPLA : EditorBase
{
private GameData8a Data => ((GameManagerPLA)ROM).Data;
protected internal EditorPLA(GameManagerPLA rom) : base(rom) => CheckOodleDllPresence();
protected override GameManagerPLA ROM { get; }
private GameData8a Data => ROM.Data;
protected internal EditorPLA(GameManagerPLA rom)
{
ROM = rom;
CheckOodleDllPresence();
}
private static void CheckOodleDllPresence()
{
@@ -329,13 +334,13 @@ public void EditMiscSpeciesInfo()
public void EditMap_Viewer()
{
var resident = (GFPack)ROM.GetFile(GameFile.Resident);
using var form = new MapViewer8a((GameManagerPLA)ROM, resident);
using var form = new MapViewer8a(ROM, resident);
form.ShowDialog();
}
public void EditAreas()
{
using var form = new AreaEditor8a((GameManagerPLA)ROM);
using var form = new AreaEditor8a(ROM);
form.ShowDialog();
}
@@ -396,7 +401,6 @@ public void EditPokemon()
editor.Save();
}
public void PopFlatConfig(GameFile file, string title)
{
var obj = ROM.GetFile(file); // flatbuffer
@@ -456,7 +460,7 @@ public void EditSymbolBehave()
public void EditMasterDump()
{
using var md = new DumperPLA((GameManagerPLA)ROM);
using var md = new DumperPLA(ROM);
md.ShowDialog();
}
}

View File

@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using pkNX.Game;
using pkNX.Structures;
@@ -10,53 +9,14 @@ namespace pkNX.WinForms.Controls;
public abstract class EditorBase
{
protected readonly GameManager ROM;
protected abstract GameManager ROM { get; }
public GameVersion Game => ROM.Game;
public int Language { get => ROM.Language; set => ROM.Language = value; }
protected EditorBase(GameManager rom) => ROM = rom;
public string? Location { get; internal set; }
public string? Location { get; private set; }
public void Initialize() => ROM.Initialize();
private static string GetEditorName(string name)
{
var newName = name.Replace('_', ' ').ToCharArray();
var builder = new StringBuilder();
// Force first char to upper
newName[0] = char.ToUpper(newName[0]);
for (int i = 0; i < newName.Length; ++i)
{
char c = newName[i];
builder.Append(c);
// Check the next char
if (i + 1 >= newName.Length)
continue;
char nextC = newName[i + 1];
// If current is space, replace next with upper char
if (c == ' ')
{
newName[i + 1] = char.ToUpper(nextC);
}
// If current is lower and next is upper, add a space in between
else if (char.IsLower(c) && char.IsUpper(nextC))
{
builder.Append(' ');
}
// If previous is upper, current is upper and next is lower, add a space in between
else if (i + 2 < newName.Length && char.IsUpper(c) && char.IsUpper(nextC) && char.IsLower(newName[i + 2]))
{
builder.Append(' ');
}
}
return builder.ToString();
}
public IEnumerable<Button> GetControls(int width, int height)
{
var type = GetType();
@@ -67,13 +27,13 @@ public IEnumerable<Button> GetControls(int width, int height)
if (!m.Name.StartsWith(prefix))
continue;
var name = m.Name[prefix.Length..];
var name = m.Name.AsSpan(prefix.Length);
var b = new Button
{
Width = width,
Height = height,
Name = $"B_{name}",
Text = GetEditorName(name),
Text = WinFormsUtil.GetSpacedCapitalized(name),
};
b.Click += (s, e) =>
{

View File

@@ -13,8 +13,9 @@ namespace pkNX.WinForms.Controls;
internal class EditorSWSH : EditorBase
{
private GameData Data => ((GameManagerSWSH)ROM).Data;
protected internal EditorSWSH(GameManagerSWSH rom) : base(rom) { }
protected override GameManagerSWSH ROM { get; }
private GameData Data => ROM.Data;
protected internal EditorSWSH(GameManagerSWSH rom) => ROM = rom;
public void EditCommon()
{
@@ -231,7 +232,7 @@ private void PopWildEdit(string file)
h = FlatBufferConverter.DeserializeFrom<EncounterArchive8>(hdo);
}
using var form = new SSWE((GameManagerSWSH)ROM, s, h);
using var form = new SSWE(ROM, s, h);
form.ShowDialog();
if (!form.Modified)
return;
@@ -737,7 +738,7 @@ void Randomize()
public void EditMasterDump()
{
using var md = new DumperSWSH((GameManagerSWSH)ROM);
using var md = new DumperSWSH(ROM);
md.ShowDialog();
}

View File

@@ -36,4 +36,55 @@ internal static DialogResult Prompt(MessageBoxButtons btn, params string[] lines
/// </summary>
/// <param name="cb">ComboBox to retrieve value for.</param>
internal static int GetIndex(ComboBox cb) => (int)(cb.SelectedValue ?? 0);
/// <summary>
/// Manual implementation of setting Title Case, replacing underscores and upper-casing spaced words.
/// </summary>
/// <param name="value">String to convert</param>
/// <returns>Title Case string</returns>
internal static string GetSpacedCapitalized(ReadOnlySpan<char> value)
{
Span<char> tmp = stackalloc char[value.Length * 2];
int ctr = 0;
bool lastSpace = true; // force first capitalized
for (int i = 0; i < value.Length; i++)
{
char c = value[i];
if (c == '_')
{
// If current is space, replace next with upper char.
tmp[ctr++] = ' ';
lastSpace = true;
}
else if (lastSpace)
{
// If previous was space, replace current with upper char.
tmp[ctr++] = char.ToUpper(c);
lastSpace = false;
}
else
{
// If current is upper and next is lower, add a space before.
// If current is lower and next is upper, add a space after.
if (i + 1 < value.Length) // has next
{
var n = value[i + 1];
if (n != '_')
{
var u0 = char.IsUpper(c);
var u1 = char.IsUpper(n);
if (u0 != u1)
{
tmp[ctr++] = u0 ? ' ' : c;
tmp[ctr++] = u1 ? ' ' : c;
c = n; // fall through write next
i++;
}
}
}
tmp[ctr++] = c;
}
}
return new string(tmp[..ctr]);
}
}