Skip to content

Commit

Permalink
Refactoring (cleanup) (#2)
Browse files Browse the repository at this point in the history
Handle a fair bit of style warnings
  • Loading branch information
kwsch authored and architdate committed Jul 4, 2018
1 parent 8f09f0e commit b56980f
Show file tree
Hide file tree
Showing 30 changed files with 739 additions and 942 deletions.
33 changes: 21 additions & 12 deletions AutoLegalityMod/APILegality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,13 @@ public static void ValidateGender(PKM pkm)

if (genderValid)
return;

if (pkm.Gender == 0)
pkm.Gender = 1;
else if (pkm.Gender == 1)
pkm.Gender = 0;
else
{
if (pkm.Gender == 0) pkm.Gender = 1;
else if (pkm.Gender == 1) pkm.Gender = 0;
else pkm.GetSaneGender();
}
pkm.GetSaneGender();
}

/// <summary>
Expand Down Expand Up @@ -178,13 +179,14 @@ public static bool FixFormes(ShowdownSet SSet, out ShowdownSet changedSet)
/// </summary>
/// <param name="pk">PKM to modify</param>
/// <param name="SSet">SSet to modify</param>
/// <param name="Form">Form to modify</param>
public static void SetSpeciesLevel(PKM pk, ShowdownSet SSet, int Form)
{
pk.Species = SSet.Species;
if (SSet.Gender != null) pk.Gender = (SSet.Gender == "M") ? 0 : 1;
pk.SetAltForm(Form);
pk.IsNicknamed = (SSet.Nickname != null);
pk.Nickname = SSet.Nickname != null ? SSet.Nickname : PKX.GetSpeciesNameGeneration(pk.Species, pk.Language, SAV.Generation);
pk.Nickname = SSet.Nickname ?? PKX.GetSpeciesNameGeneration(pk.Species, pk.Language, SAV.Generation);
pk.CurrentLevel = SSet.Level;
if (pk.CurrentLevel == 50) pk.CurrentLevel = 100; // VGC Override
}
Expand Down Expand Up @@ -296,6 +298,9 @@ public static void SetShinyBoolean(PKM pk, bool isShiny)
/// </summary>
/// <param name="pk"></param>
/// <param name="SSet"></param>
/// <param name="Method"></param>
/// <param name="HPType"></param>
/// <param name="originalPKMN"></param>
public static void SetIVsPID(PKM pk, ShowdownSet SSet, PIDType Method, int HPType, PKM originalPKMN)
{
// Useful Values for computation
Expand All @@ -307,8 +312,8 @@ public static void SetIVsPID(PKM pk, ShowdownSet SSet, PIDType Method, int HPTyp

// Find the encounter
LegalInfo li = EncounterFinder.FindVerifiedEncounter(originalPKMN);
var property = li.EncounterMatch.GetType().GetProperty("PIDType");
// TODO: Something about the gen 5 events. Maybe check for nature and shiny val and not touch the PID in that case?
var property = li.EncounterMatch.GetType().GetProperty("PIDType");
// TODO: Something about the gen 5 events. Maybe check for nature and shiny val and not touch the PID in that case?
// Also need to figure out hidden power handling in that case.. for PIDType 0 that may isn't even be possible.

if (pk.GenNumber > 4 || pk.VC)
Expand Down Expand Up @@ -354,6 +359,7 @@ public static PKM DebugReturn(PKM pk, out bool satisfied, int OptionalGame = -1)
/// <param name="pk">PKM to modify</param>
/// <param name="Method">Given Method</param>
/// <param name="HPType">HPType INT for preserving Hidden powers</param>
/// <param name="originalPKMN"></param>
public static void FindPIDIV(PKM pk, PIDType Method, int HPType, PKM originalPKMN)
{
if (Method == PIDType.None)
Expand All @@ -363,7 +369,7 @@ public static void FindPIDIV(PKM pk, PIDType Method, int HPType, PKM originalPKM
if (Method == PIDType.None) pk.PID = PKX.GetRandomPID(pk.Species, pk.Gender, pk.Version, pk.Nature, pk.Format, (uint)(pk.AbilityNumber * 0x10001));
}
PKM iterPKM = pk;
while (true && Method != PIDType.None)
while (Method != PIDType.None)
{
uint seed = Util.Rand32();
PIDGenerator.SetValuesFromSeed(pk, Method, seed);
Expand All @@ -383,9 +389,9 @@ public static void FindPIDIV(PKM pk, PIDType Method, int HPType, PKM originalPKM
public static PIDType FindLikelyPIDType(PKM pk, PKM pkmn)
{
BruteForce b = new BruteForce();
if (b.usesEventBasedMethod(pk.Species, pk.Moves, "BACD_R"))
if (b.UsesEventBasedMethod(pk.Species, pk.Moves, "BACD_R"))
return PIDType.BACD_R;
if (b.usesEventBasedMethod(pk.Species, pk.Moves, "M2")) return PIDType.Method_2;
if (b.UsesEventBasedMethod(pk.Species, pk.Moves, "M2")) return PIDType.Method_2;
if (pk.Species == 490 && pk.Gen4)
{
pk.Egg_Location = 2002;
Expand Down Expand Up @@ -451,7 +457,10 @@ public static void SetEncryptionConstant(PKM pk)
uint EC = wIndex < 0 ? Util.Rand32() : PKX.GetWurmpleEC(wIndex / 2);
if (!(pk.Species == 658 && pk.AltForm == 1)) pk.EncryptionConstant = EC;
}
else pk.EncryptionConstant = pk.PID; // Generations 3 to 5
else
{
pk.EncryptionConstant = pk.PID; // Generations 3 to 5
}
}

/// <summary>
Expand Down
71 changes: 35 additions & 36 deletions AutoLegalityMod/ArchitMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ public partial class AutoLegalityMod
/// <param name="bytes">byte array</param>
public static void PrintByteArray(byte[] bytes)
{
var sb = new System.Text.StringBuilder("new byte[] { ");
foreach (var b in bytes)
{
sb.Append(b + ", ");
}
sb.Append("}");
Console.WriteLine(sb.ToString());
var str = $"new byte[] {{ {string.Join(", ", bytes)} }}";
Console.WriteLine(str);
}

/// <summary>
Expand All @@ -31,11 +26,12 @@ public static void PrintByteArray(byte[] bytes)
/// <param name="Country">String denoting the exact country</param>
/// <param name="SubRegion">String denoting the exact sub region</param>
/// <param name="ConsoleRegion">String denoting the exact console region</param>
/// <param name="pk"></param>
public static void SetRegions(string Country, string SubRegion, string ConsoleRegion, PKM pk)
{
pk.Country = Util.GetCBList("countries", "en").FirstOrDefault(z => z.Text == Country).Value;
pk.Region = Util.GetCBList($"sr_{pk.Country:000}", "en").FirstOrDefault(z => z.Text == SubRegion).Value;
pk.ConsoleRegion = Util.GetUnsortedCBList("regions3ds").FirstOrDefault(z => z.Text == ConsoleRegion).Value;
pk.Country = Util.GetCBList("countries", "en").Find(z => z.Text == Country).Value;
pk.Region = Util.GetCBList($"sr_{pk.Country:000}", "en").Find(z => z.Text == SubRegion).Value;
pk.ConsoleRegion = Util.GetUnsortedCBList("regions3ds").Find(z => z.Text == ConsoleRegion).Value;
}

/// <summary>
Expand All @@ -60,7 +56,9 @@ public static PKM SetPKMRegions(int Country, int SubRegion, int ConsoleRegion, P
/// <param name="OT">string value of OT name</param>
/// <param name="TID">INT value of TID</param>
/// <param name="SID">INT value of SID</param>
/// <param name="gender"></param>
/// <param name="pk"></param>
/// <param name="APILegalized"></param>
/// <returns></returns>
public static PKM SetTrainerData(string OT, int TID, int SID, int gender, PKM pk, bool APILegalized = false)
{
Expand Down Expand Up @@ -88,7 +86,7 @@ public static PKM SetTrainerData(string OT, int TID, int SID, int gender, PKM pk
/// </summary>
/// <param name="jsonstring">string form of trainerdata.json</param>
/// <returns></returns>
public static string checkMode(string jsonstring = "")
public static string CheckMode(string jsonstring = "")
{
if(jsonstring != "")
{
Expand All @@ -99,12 +97,15 @@ public static string checkMode(string jsonstring = "")
}
else
{
if (!System.IO.File.Exists(Directory.GetCurrentDirectory() + "\\trainerdata.json"))
if (!File.Exists(Directory.GetCurrentDirectory() + "\\trainerdata.json"))
{
return "save"; // Default trainerdata.txt handling
}
jsonstring = System.IO.File.ReadAllText(Directory.GetCurrentDirectory() + "\\trainerdata.json", System.Text.Encoding.UTF8);
if (jsonstring != "") return checkMode(jsonstring);
jsonstring = File.ReadAllText(Directory.GetCurrentDirectory() + "\\trainerdata.json", System.Text.Encoding.UTF8);
if (jsonstring != "")
{
return CheckMode(jsonstring);
}
else
{
MessageBox.Show("Empty trainerdata.json file", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
Expand All @@ -120,10 +121,10 @@ public static string checkMode(string jsonstring = "")
/// <param name="Game">int value of the game</param>
/// <param name="jsonvalue">internal json: trainerdata[Game]</param>
/// <returns></returns>
public static bool checkIfGameExists(string jsonstring, int Game, out string jsonvalue)
public static bool CheckIfGameExists(string jsonstring, int Game, out string jsonvalue)
{
jsonvalue = "";
if (checkMode(jsonstring) == "auto")
if (CheckMode(jsonstring) == "auto")
{
jsonvalue = "auto";
return false;
Expand All @@ -147,7 +148,7 @@ public static bool checkIfGameExists(string jsonstring, int Game, out string jso
/// <param name="key"></param>
/// <param name="finaljson"></param>
/// <returns></returns>
public static string getValueFromKey(string key, string finaljson)
public static string GetValueFromKey(string key, string finaljson)
{
return finaljson.Split(new string[] { key }, StringSplitOptions.None)[1].Split('"')[2].Trim();
}
Expand All @@ -160,7 +161,7 @@ public static string getValueFromKey(string key, string finaljson)
/// <returns></returns>
public static int[] ConvertTIDSID7toTIDSID(int tid7, int sid7)
{
var repack = (long)sid7 * 1_000_000 + tid7;
var repack = ((long)sid7 * 1_000_000) + tid7;
int sid = (ushort)(repack >> 16);
int tid = (ushort)repack;
return new int[] { tid, sid };
Expand All @@ -172,22 +173,22 @@ public static int[] ConvertTIDSID7toTIDSID(int tid7, int sid7)
/// <param name="C_SAV">Current Save Editor</param>
/// <param name="Game">optional Game value in case of mode being game</param>
/// <returns></returns>
public static string[] parseTrainerJSON(SaveFile C_SAV, int Game = -1)
public static string[] ParseTrainerJSON(SaveFile C_SAV, int Game = -1)
{
if (!File.Exists(Directory.GetCurrentDirectory() + "\\trainerdata.json"))
{
return parseTrainerData(C_SAV); // Default trainerdata.txt handling
return ParseTrainerData(); // Default trainerdata.txt handling
}
string jsonstring = System.IO.File.ReadAllText(Directory.GetCurrentDirectory() + "\\trainerdata.json", System.Text.Encoding.UTF8);
if (Game == -1) Game = C_SAV.Game;
if(!checkIfGameExists(jsonstring, Game, out string finaljson)) return parseTrainerData(C_SAV, finaljson == "auto");
string TID = getValueFromKey("TID", finaljson);
string SID = getValueFromKey("SID", finaljson);
string OT = getValueFromKey("OT", finaljson);
string Gender = getValueFromKey("Gender", finaljson);
string Country = getValueFromKey("Country", finaljson);
string SubRegion = getValueFromKey("SubRegion", finaljson);
string ConsoleRegion = getValueFromKey("3DSRegion", finaljson);
string jsonstring = File.ReadAllText(Directory.GetCurrentDirectory() + "\\trainerdata.json", System.Text.Encoding.UTF8);
if (Game == -1) Game = C_SAV.Game;
if(!CheckIfGameExists(jsonstring, Game, out string finaljson)) return ParseTrainerData(finaljson == "auto");
string TID = GetValueFromKey("TID", finaljson);
string SID = GetValueFromKey("SID", finaljson);
string OT = GetValueFromKey("OT", finaljson);
string Gender = GetValueFromKey("Gender", finaljson);
string Country = GetValueFromKey("Country", finaljson);
string SubRegion = GetValueFromKey("SubRegion", finaljson);
string ConsoleRegion = GetValueFromKey("3DSRegion", finaljson);
if (TID.Length == 6 && SID.Length == 4)
{
if(new List<int> { 33, 32, 31, 30 }.IndexOf(Game) == -1) MessageBox.Show("Force Converting G7TID/G7SID to TID/SID", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
Expand All @@ -201,9 +202,9 @@ public static string[] parseTrainerJSON(SaveFile C_SAV, int Game = -1)
/// <summary>
/// Parser for auto and preset trainerdata.txt files
/// </summary>
/// <param name="C_SAV">SAVEditor of the current save file</param>
/// <param name="auto"></param>
/// <returns></returns>
public static string[] parseTrainerData(SaveFile C_SAV, bool auto = false)
public static string[] ParseTrainerData(bool auto = false)
{
// Defaults
string TID = "23456";
Expand All @@ -219,12 +220,12 @@ public static string[] parseTrainerData(SaveFile C_SAV, bool auto = false)
}
string[] trainerdataLines = File.ReadAllText(Directory.GetCurrentDirectory() + "\\trainerdata.txt", System.Text.Encoding.UTF8)
.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
List<string> lstlines = trainerdataLines.OfType<string>().ToList();
List<string> lstlines = trainerdataLines.Where(f => f != null).ToList();
int count = lstlines.Count;
for (int i =0; i < count; i++)
{
string item = lstlines[0];
if (item.TrimEnd() == "" || item.TrimEnd() == "auto") continue;
if (item.TrimEnd().Length == 0 || item.TrimEnd() == "auto") continue;
string key = item.Split(':')[0].TrimEnd();
string value = item.Split(':')[1].TrimEnd();
lstlines.RemoveAt(0);
Expand Down Expand Up @@ -263,7 +264,5 @@ public static string[] parseTrainerData(SaveFile C_SAV, bool auto = false)
}
return new string[] { TID, SID, OT, Gender, Country, SubRegion, ConsoleRegion };
}

}

}
4 changes: 1 addition & 3 deletions AutoLegalityMod/AutoLegalityMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace AutoLegalityMod
{
public partial class AutoLegalityMod : IPlugin
{

/// <summary>
/// Main Plugin Variables
/// </summary>
Expand Down Expand Up @@ -53,7 +52,7 @@ private void AddPluginControl(ToolStripDropDownItem modmenu)
{
var ctrl = new ToolStripMenuItem(Name);
modmenu.DropDownItems.Add(ctrl);
ctrl.Click += new EventHandler(ClickShowdownImportPKMModded);
ctrl.Click += ClickShowdownImportPKMModded;
ctrl.Name = "Menu_AutoLegalityMod";
ctrl.Image = AutoLegalityResources.autolegalitymod;
ctrl.ShortcutKeys = (Keys.Control | Keys.I);
Expand Down Expand Up @@ -83,6 +82,5 @@ public void ClickShowdownImportPKMModded(object sender, EventArgs e)
AutomaticLegality.SaveFileEditor = SaveFileEditor;
AutomaticLegality.ImportModded();
}

}
}
6 changes: 0 additions & 6 deletions AutoLegalityMod/AutoLegalityMod.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="APILegality.cs" />
Expand Down
Loading

0 comments on commit b56980f

Please sign in to comment.