2021-10-22 05:13:21 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Logic object that assembles parameters used for starting up an editing environment.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class StartupArguments
|
2021-10-22 05:13:21 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
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();
|
|
|
|
|
|
2021-10-22 05:13:21 +00:00
|
|
|
|
/// <summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
|
/// Step 1: Reads in command line arguments.
|
2021-10-22 05:13:21 +00:00
|
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public void ReadArguments(IEnumerable<string> args)
|
2021-10-22 05:13:21 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
foreach (string path in args)
|
2021-10-22 05:13:21 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var other = FileUtil.GetSupportedFile(path, SAV);
|
|
|
|
|
if (other is SaveFile s)
|
|
|
|
|
{
|
|
|
|
|
s.Metadata.SetExtraInfo(path);
|
|
|
|
|
SAV = s;
|
|
|
|
|
}
|
|
|
|
|
else if (other is PKM pk)
|
2021-10-22 05:13:21 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
Entity = pk;
|
|
|
|
|
}
|
|
|
|
|
else if (other is not null)
|
|
|
|
|
{
|
|
|
|
|
Extra.Add(other);
|
2021-10-22 05:13:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
}
|
2021-10-22 05:13:21 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Step 2: Reads settings config.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void ReadSettings(IStartupSettings startup)
|
|
|
|
|
{
|
|
|
|
|
if (SAV is not null)
|
|
|
|
|
return;
|
2021-10-22 05:13:21 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (Entity is { } x)
|
|
|
|
|
SAV = ReadSettingsDefinedPKM(startup, x) ?? GetBlank(x);
|
|
|
|
|
else
|
|
|
|
|
SAV = ReadSettingsAnyPKM(startup) ?? GetBlankSaveFile(startup.DefaultSaveVersion, SAV);
|
|
|
|
|
}
|
2021-10-22 05:13:21 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
// step 3
|
|
|
|
|
public void ReadTemplateIfNoEntity(string path)
|
|
|
|
|
{
|
|
|
|
|
if (Entity is not null)
|
|
|
|
|
return;
|
|
|
|
|
if (SAV is not { } sav)
|
|
|
|
|
return;
|
2021-10-22 05:13:21 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var pk = sav.LoadTemplate(path);
|
|
|
|
|
var isBlank = pk.Data.SequenceEqual(sav.BlankPKM.Data);
|
|
|
|
|
if (isBlank)
|
|
|
|
|
EntityTemplates.TemplateFields(pk, sav);
|
2021-10-22 05:13:21 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
Entity = pk;
|
|
|
|
|
}
|
2021-10-22 05:13:21 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
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,
|
|
|
|
|
};
|
2021-10-22 05:13:21 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private static SaveFile? ReadSettingsAnyPKM(IStartupSettings startup) => startup.AutoLoadSaveOnStartup switch
|
|
|
|
|
{
|
|
|
|
|
AutoLoadSetting.RecentBackup => SaveFinder.DetectSaveFiles().FirstOrDefault(),
|
|
|
|
|
AutoLoadSetting.LastLoaded => GetMostRecentlyLoaded(startup.RecentlyLoaded).FirstOrDefault(),
|
|
|
|
|
_ => null,
|
|
|
|
|
};
|
2021-10-22 05:13:21 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
#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;
|
2021-10-22 05:13:21 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
return SaveUtil.GetBlankSAV(ver, pk.OT_Name, (LanguageID)pk.Language);
|
|
|
|
|
}
|
2021-10-22 05:13:21 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
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
|
|
|
|
|
sav = SaveUtil.GetBlankSAV((GameVersion)GameInfo.VersionDataSource.Max(z => z.Value), tr, lang);
|
|
|
|
|
return sav;
|
|
|
|
|
}
|
2021-10-22 05:13:21 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private static IEnumerable<SaveFile> GetMostRecentlyLoaded(IEnumerable<string> paths)
|
|
|
|
|
{
|
|
|
|
|
foreach (var path in paths)
|
2021-10-22 05:13:21 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
continue;
|
2021-10-22 05:13:21 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var sav = SaveUtil.GetVariantSAV(path);
|
|
|
|
|
if (sav is null)
|
|
|
|
|
continue;
|
2021-10-22 05:13:21 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
yield return sav;
|
2021-10-22 05:13:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
#endregion
|
2021-10-22 05:13:21 +00:00
|
|
|
|
}
|