Skip to content
This repository has been archived by the owner on Jan 28, 2020. It is now read-only.

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
Removes global state argument passing
  • Loading branch information
kwsch committed Mar 13, 2019
1 parent 9bd8e23 commit c943bf9
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 125 deletions.
53 changes: 0 additions & 53 deletions CoreConsole/APIs/AutoLegality.cs

This file was deleted.

20 changes: 0 additions & 20 deletions CoreConsole/APIs/LegalityCheck.cs

This file was deleted.

40 changes: 40 additions & 0 deletions CoreConsole/ConsoleLegalizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Diagnostics;
using PKHeX.Core;
using PKHeX.Core.AutoMod;

namespace CoreConsole
{
internal static class ConsoleLegalizer
{
public static PKM Legalize(PKM pk, GameVersion ver, LegalityAnalysis old = null)
{
if (old != null)
Debug.WriteLine(old.Report());

var sav = SaveUtil.GetBlankSAV(ver, "PKHeX");
var updated = sav.Legalize(pk);
var la = new LegalityAnalysis(updated);
if (!la.Valid)
return pk;

Console.WriteLine("====================================");
Console.WriteLine("= Legalized with Auto Legality Mod =");
Console.WriteLine("====================================");
Console.WriteLine(la.Report(true));
return updated;
}

public static PKM GetLegalPKM(PKM pk, string ver)
{
var la = new LegalityAnalysis(pk);
if (la.Valid)
return pk;

var parsed = Enum.TryParse<GameVersion>(ver, true, out var game);
if (!parsed)
return pk;
return Legalize(pk, game, la);
}
}
}
3 changes: 1 addition & 2 deletions CoreConsole/CoreConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="APIs\AutoLegality.cs" />
<Compile Include="APIs\LegalityCheck.cs" />
<Compile Include="ConsoleLegalizer.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
97 changes: 47 additions & 50 deletions CoreConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -1,76 +1,73 @@
using System;
using System.IO;
using System.Linq;
using CoreConsole.APIs;
using PKHeX.Core;

namespace CoreConsole
{
class ConsoleIndex
internal static class Program
{
public static PKM pk;
static void Main(string[] args)
private static readonly string appPath = Environment.CurrentDirectory;
private const string RequestLegalityCheck = "-l";
private const string RequestLegalization = "-alm";

private static void Main(string[] args)
{
string appPath = Environment.CurrentDirectory;
var pk = CreatePKMfromArgs(args);

Initialize(args);
if (args.Contains("-l"))
if (args.Contains(RequestLegalityCheck))
{
// Legality API calls
var lc = new LegalityCheck(pk);
if (args.Contains("--verbose")) Console.WriteLine(lc.VerboseReport);
else Console.WriteLine(lc.Report);
var lc = new LegalityAnalysis(pk);
var verbose = args.Contains("--verbose");
Console.WriteLine(lc.Report(verbose));
}
if (args.Contains("-alm"))

if (!args.Contains(RequestLegalization))
return;

if (!args.Contains("--version"))
{
if (!args.Contains("--version")) Console.WriteLine("Specify version with the [--version] tag");
else
{
var alm = new AutoLegality(pk, args[Array.IndexOf(args, "--version") + 1]);
if (alm != null)
{
if (!args.Contains("-o"))
{
string output = Util.CleanFileName(alm.GetLegalPKM().FileName);
File.WriteAllBytes(Path.Combine(appPath, "output", output), alm.GetLegalPKM().DecryptedBoxData);
} else
{
string output = GetOutputPath(args);
File.WriteAllBytes(output, alm.GetLegalPKM().DecryptedBoxData);
}
}
else Console.WriteLine("Invalid version");
}
Console.WriteLine("Specify version with the [--version] tag");
return;
}

var ver = GetArgVal(args, "--version");
pk = ConsoleLegalizer.GetLegalPKM(pk, ver);
var outPath = GetOutputPath(args, pk);
File.WriteAllBytes(outPath, pk.DecryptedBoxData);
}

private static string GetOutputPath(string[] args, PKM p)
{
if (args.Contains("-o"))
return GetArgVal(args, "-o");
return Path.Combine(appPath, "output", Util.CleanFileName(p.FileName));
}
private static void Initialize(string[] args)

private static PKM CreatePKMfromArgs(string[] args)
{
// check -i for input and get file path in the next arg
if (args.Contains("-i"))
{
string path = GetFilePath(args);
byte[] data = File.ReadAllBytes(path);
pk = PKMConverter.GetPKMfromBytes(data);
var path = GetArgVal(args, "-i");
var data = File.ReadAllBytes(path);
return PKMConverter.GetPKMfromBytes(data);
}
else

var gameStr = GetArgVal(args, "--version");
var parsed = Enum.TryParse<GameVersion>(gameStr, true, out var game);
if (!parsed)
{
string set = args[Array.IndexOf(args, "--set") + 1];
bool valid = Enum.TryParse<GameVersion>(args[Array.IndexOf(args, "--version") + 1], true, out var game);
var template = PKMConverter.GetBlank(game.GetGeneration(), game);
template.ApplySetDetails(new ShowdownSet(set.Split(new string[] { "\\n" }, StringSplitOptions.None)));
pk = template;
Console.WriteLine("Invalid version specified: " + gameStr);
game = GameVersion.Any;
}
}

private static string GetFilePath(string[] args)
{
return args[Array.IndexOf(args, "-i") + 1];
}

private static string GetOutputPath(string[] args)
{
return args[Array.IndexOf(args, "-o") + 1];
var showdownSet = GetArgVal(args, "--set");
var template = PKMConverter.GetBlank(game.GetGeneration(), game);
template.ApplySetDetails(new ShowdownSet(showdownSet.Split(new [] { "\\n" }, StringSplitOptions.None)));
return template;
}

private static string GetArgVal(string[] args, string modifier) => args[Array.IndexOf(args, modifier) + 1];
}
}

0 comments on commit c943bf9

Please sign in to comment.