PKHeX/PKHeX.Core/Editing/Program/StartupArguments.cs
Kurt 88830e0d00
Update from .NET Framework 4.6 to .NET 7 (#3729)
Updates from net46->net7, dropping support for mono in favor of using the latest runtime (along with the performance/API improvements). Releases will be posted as 64bit only for now.

Refactors a good amount of internal API methods to be more performant and more customizable for future updates & fixes.

Adds functionality for Batch Editor commands to `>`, `<` and <=/>=

TID/SID properties renamed to TID16/SID16 for clarity; other properties exposed for Gen7 / display variants.

Main window has a new layout to account for DPI scaling (8 point grid)

Fixed: Tatsugiri and Paldean Tauros now output Showdown form names as Showdown expects
Changed: Gen9 species now interact based on the confirmed National Dex IDs (closes #3724)
Fixed: Pokedex set all no longer clears species with unavailable non-base forms (closes #3720)
Changed: Hyper Training suggestions now apply for level 50 in SV. (closes #3714)
Fixed: B2/W2 hatched egg met locations exclusive to specific versions are now explicitly checked (closes #3691)
Added: Properties for ribbon/mark count (closes #3659)
Fixed: Traded SV eggs are now checked correctly (closes #3692)
2023-01-21 20:02:33 -08:00

129 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace PKHeX.Core;
/// <summary>
/// Logic object that assembles parameters used for starting up an editing environment.
/// </summary>
public sealed class StartupArguments
{
public PKM? Entity { get; private set; }
public SaveFile? SAV { get; private set; }
// ReSharper disable once UnassignedGetOnlyAutoProperty
public Exception? Error { get; }
// ReSharper disable once CollectionNeverQueried.Global
public readonly List<object> Extra = new();
/// <summary>
/// Step 1: Reads in command line arguments.
/// </summary>
public void ReadArguments(IEnumerable<string> args)
{
foreach (string path in args)
{
var other = FileUtil.GetSupportedFile(path, SAV);
if (other is SaveFile s)
{
s.Metadata.SetExtraInfo(path);
SAV = s;
}
else if (other is PKM pk)
{
Entity = pk;
}
else if (other is not null)
{
Extra.Add(other);
}
}
}
/// <summary>
/// Step 2: Reads settings config.
/// </summary>
public void ReadSettings(IStartupSettings startup)
{
if (SAV is not null)
return;
if (Entity is { } x)
SAV = ReadSettingsDefinedPKM(startup, x) ?? GetBlank(x);
else
SAV = ReadSettingsAnyPKM(startup) ?? GetBlankSaveFile(startup.DefaultSaveVersion, SAV);
}
// step 3
public void ReadTemplateIfNoEntity(string path)
{
if (Entity is not null)
return;
if (SAV is not { } sav)
return;
var pk = sav.LoadTemplate(path);
var isBlank = pk.Data.SequenceEqual(sav.BlankPKM.Data);
if (isBlank)
EntityTemplates.TemplateFields(pk, sav);
Entity = pk;
}
private static SaveFile? ReadSettingsDefinedPKM(IStartupSettings startup, PKM pk) => startup.AutoLoadSaveOnStartup switch
{
AutoLoadSetting.RecentBackup => SaveFinder.DetectSaveFiles().FirstOrDefault(z => z.IsCompatiblePKM(pk)),
AutoLoadSetting.LastLoaded => GetMostRecentlyLoaded(startup.RecentlyLoaded).FirstOrDefault(z => z.IsCompatiblePKM(pk)),
_ => null,
};
private static SaveFile? ReadSettingsAnyPKM(IStartupSettings startup) => startup.AutoLoadSaveOnStartup switch
{
AutoLoadSetting.RecentBackup => SaveFinder.DetectSaveFiles().FirstOrDefault(),
AutoLoadSetting.LastLoaded => GetMostRecentlyLoaded(startup.RecentlyLoaded).FirstOrDefault(),
_ => null,
};
#region Utility
private static SaveFile GetBlank(PKM pk)
{
var ctx = pk.Context;
var ver = ctx.GetSingleGameVersion();
if (pk is { Format: 1, Japanese: true })
ver = GameVersion.BU;
return SaveUtil.GetBlankSAV(ver, pk.OT_Name, (LanguageID)pk.Language);
}
private static SaveFile GetBlankSaveFile(GameVersion version, SaveFile? current)
{
var lang = SaveUtil.GetSafeLanguage(current);
var tr = SaveUtil.GetSafeTrainerName(current, lang);
var sav = SaveUtil.GetBlankSAV(version, tr, lang);
if (sav.Version == GameVersion.Invalid) // will fail to load
{
var max = GameInfo.VersionDataSource.MaxBy(z => z.Value) ?? throw new Exception();
var ver = (GameVersion)max.Value;
sav = SaveUtil.GetBlankSAV(ver, tr, lang);
}
return sav;
}
private static IEnumerable<SaveFile> GetMostRecentlyLoaded(IEnumerable<string> paths)
{
foreach (var path in paths)
{
if (!File.Exists(path))
continue;
var sav = SaveUtil.GetVariantSAV(path);
if (sav is null)
continue;
yield return sav;
}
}
#endregion
}