Minor perf: cache successful gameversion from rom

Skip fetching/counting all files for the unpacked ROM that is used in determining which ROM it is.
Once we've computed it the first time, we can just reuse the value.
This commit is contained in:
Kurt 2022-10-01 14:24:20 -07:00
parent 035edab21e
commit 103b0bab2f
4 changed files with 22 additions and 13 deletions

View File

@ -1,6 +1,5 @@
using System;
using System;
using System.IO;
using System.Linq;
using pkNX.Structures;
namespace pkNX.Game
@ -36,8 +35,9 @@ private GameLocation(string romfs, string exefs, GameVersion game)
/// Determines the <see cref="GameVersion"/> of the input directory and detects the location of files for editing.
/// </summary>
/// <param name="dir">Directory the game data is in</param>
/// <param name="gameOverride">Detected version</param>
/// <returns>New <see cref="GameLocation"/> object with references to file paths.</returns>
public static GameLocation GetGame(string dir)
public static GameLocation GetGame(string dir, GameVersion gameOverride = GameVersion.Any)
{
if (dir == null || !Directory.Exists(dir))
return null;
@ -49,13 +49,18 @@ public static GameLocation GetGame(string dir)
if (romfs == null && exefs == null)
return null;
var files = Directory.GetFiles(romfs, "*", SearchOption.AllDirectories);
var game = GetGameFromCount(files.Length, romfs, exefs);
var game = gameOverride != GameVersion.Any ? gameOverride : GetGameFromPath(romfs, exefs);
if (game == GameVersion.Invalid)
return null;
return new GameLocation(romfs, exefs, game);
}
private static GameVersion GetGameFromPath(string romfs, string exefs)
{
var files = Directory.GetFiles(romfs, "*", SearchOption.AllDirectories);
return GetGameFromCount(files.Length, romfs, exefs);
}
private const int FILECOUNT_XY = 271;
private const int FILECOUNT_ORASDEMO = 301;
private const int FILECOUNT_ORAS = 299;

View File

@ -35,7 +35,7 @@ public Main()
Settings = SettingsSerializer.GetSettings<ProgramSettings>(ProgramSettingsPath).Result;
CB_Lang.SelectedIndex = Settings.Language;
if (!string.IsNullOrWhiteSpace(Settings.GamePath))
OpenPath(Settings.GamePath);
OpenPath(Settings.GamePath, Settings.GameOverride);
DragDrop += (s, e) =>
{
@ -83,6 +83,7 @@ private async void Main_FormClosing(object sender, FormClosingEventArgs e)
EditUtil.SaveSettings(Editor.Game);
Settings.Language = CB_Lang.SelectedIndex;
Settings.GamePath = TB_Path.Text;
Settings.GameOverride = Editor.Game;
await SettingsSerializer.SaveSettings(Settings, ProgramSettingsPath);
}
@ -115,12 +116,12 @@ private void Menu_SetRNGSeed_Click(object sender, EventArgs e)
WinFormsUtil.Alert("Unable to set seed.");
}
private void OpenPath(string path)
private void OpenPath(string path, GameVersion gameOverride = GameVersion.Any)
{
try
{
if (Directory.Exists(path))
OpenFolder(path);
OpenFolder(path, gameOverride);
else
OpenFile(path);
}
@ -143,9 +144,9 @@ private static void OpenFile(string path)
Process.Start("explorer.exe", resultPath);
}
private void OpenFolder(string path)
private void OpenFolder(string path, GameVersion gameOverride)
{
var editor = EditorBase.GetEditor(path, Language);
var editor = EditorBase.GetEditor(path, Language, gameOverride);
if (editor == null)
{
var msg = "Invalid folder loaded." + Environment.NewLine + "Unable to recognize game data.";
@ -188,7 +189,7 @@ private void LoadROM(EditorBase editor)
var squareSide = Math.Sqrt(totalArea);
var columns = (int)Math.Ceiling(squareSide / wp) + 1;
var rows = (count / columns) + 2;
Width = columns * wp + 6;
Width = (columns * wp) + 6;
Height = FLP_Controls.Location.Y + (rows * hp) + 6;
CenterToScreen();

View File

@ -104,9 +104,9 @@ public IEnumerable<Button> GetControls(int width, int height)
_ => null,
};
public static EditorBase? GetEditor(string loc, int language)
public static EditorBase? GetEditor(string loc, int language, GameVersion gameOverride)
{
var gl = GameLocation.GetGame(loc);
var gl = GameLocation.GetGame(loc, gameOverride);
if (gl == null)
return null;

View File

@ -1,7 +1,10 @@
using pkNX.Structures;
namespace pkNX.WinForms;
public class ProgramSettings
{
public int Language { get; set; } = 2;
public string GamePath { get; set; } = string.Empty;
public GameVersion GameOverride { get; set; } = GameVersion.Any;
}