Skip to content

Commit

Permalink
Add JSON deserialization for EggCreator and export
Browse files Browse the repository at this point in the history
  • Loading branch information
evinjaff committed May 26, 2024
1 parent d15aa08 commit ee6ed57
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 27 deletions.
29 changes: 29 additions & 0 deletions pkhex/pkhex-egglocke-tests/pkhex-egglocke-tests/EggCreatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using pkhexEgglocke;
using pkhexEgglockeTests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pkhexEgglockeTests
{
[TestClass]
public class EggCreatorTests
{
[TestMethod]
public void TestDecoder()
{
SaveWriter sw = new SaveWriter(testConstants.JOHTO_PLUS_SOUL_SILVER_SAVE);

EggCreator ec = EggCreator.decodeJSON(testConstants.BLANK_GEN4_MAREEP_VALID);

sw.addEgg(ec, 1);

sw.export("testPROG.sav");

}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class SaveWriterTest
public void TestSaveImportConstructorDoesNotCrashOnValidFile()
{

SaveWriter sw = new SaveWriter(testConstants.BLANK_SOULSILVER_SAVE);
SaveWriter sw = new SaveWriter(testConstants.JOHTO_PLUS_SOUL_SILVER_SAVE);

}

Expand All @@ -30,7 +30,7 @@ public void TestSaveExportConstructorCrashesOnInvalidFile()
public void TestSaveImportOTMatches()
{
string expectedOT = testConstants.BLANK_SOULSILVER_OT_STRING;
SaveWriter sw = new SaveWriter(testConstants.BLANK_SOULSILVER_SAVE);
SaveWriter sw = new SaveWriter(testConstants.JOHTO_PLUS_SOUL_SILVER_SAVE);
string actualOT = sw.getOTString();

Assert.AreEqual(expectedOT, actualOT);
Expand All @@ -42,7 +42,7 @@ public void TestSaveImportOTMatches()
public void TestSaveImportVersionMatches()
{
string expectedOT = testConstants.BLANK_SOULSILVER_OT_STRING;
SaveWriter sw = new SaveWriter(testConstants.BLANK_SOULSILVER_SAVE);
SaveWriter sw = new SaveWriter(testConstants.JOHTO_PLUS_SOUL_SILVER_SAVE);
string actualOT = sw.getOTString();

Assert.AreEqual(expectedOT, actualOT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<None Update="testSources\emptySoulSilver.sav">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="testSources\Mareep.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ namespace pkhexEgglockeTests
public static class testConstants
{
// Gen 4 - Soul Silver Blank Save File
public static string BLANK_SOULSILVER_SAVE => Path.Combine(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"testSources"), "emptySoulSilver.sav");
public static string JOHTO_PLUS_SOUL_SILVER_SAVE => Path.Combine(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"testSources"), "emptySoulSilver.sav");

Check warning on line 13 in pkhex/pkhex-egglocke-tests/pkhex-egglocke-tests/testConstants.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'path1' in 'string Path.Combine(string path1, string path2)'.
public static string BLANK_SOULSILVER_OT_STRING => "Egg";

// Gen 4 - Blank JSON
public static string BLANK_GEN4_MAREEP_VALID => Path.Combine(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"testSources"), "Mareep.json");

Check warning on line 17 in pkhex/pkhex-egglocke-tests/pkhex-egglocke-tests/testConstants.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'path1' in 'string Path.Combine(string path1, string path2)'.

// Gen 6 - Omega Ruby Save File
public static string BLANK_OMEGARUBY_SAVE => Path.Combine(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"testSources"), "emptyOmegaRuby.sav");

Check warning on line 20 in pkhex/pkhex-egglocke-tests/pkhex-egglocke-tests/testConstants.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'path1' in 'string Path.Combine(string path1, string path2)'.





}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"dexNumber": 179,
"ball": 2,
"language": 1,
"ability":9,
"nature": 1,
"OT": "Dawn",
"OTGender": 1,
"nickname": "WLW",
"IV": [ 31, 31, 31, 31, 31, 31 ],
"EV": [ 0, 0, 0, 0, 0, 0 ],
"moves": [ 425, 262 ],
"movespp": [ 30, 40 ]

}
137 changes: 137 additions & 0 deletions pkhex/pkhex-egglocke/pkhex-egglocke/EggCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* PokemonCreator.cs
* Bridge class that creates PkHEX pokemon files based on JSON objects exported from web app.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PKHeX.Core;
using Newtonsoft.Json;
using System.Text.RegularExpressions;

namespace pkhexEgglocke
{
/// <summary>
/// PokemonCreator: Pokemon data class that decodes JSON pokemon descriptors and
/// </summary>
///

// Parameters
// These parameters mostly allow you to set rules around what user submissions you'd like to include (ex. allow user-supplied IVs)


internal class EggCreator
{

protected bool allowUserIVs => true;
protected bool allowUserShiny => true;
protected bool allowUser => true;

// Egg Parameters that are always true
public bool IsEgg = true;
public ushort EggLocationDP = Locations.Daycare4;
public byte MetLevel = 0;

//Other constant fields
public int language = (int)LanguageID.English;


// Mutable fields
public byte ball = (int)Ball.Dive;

public ushort dexNumber = 1;
public int Language = 1;
public int Ability = 2;
public Nature Nature = Nature.Adamant;



//Misc creator stuff
public string OT = "Default";
public byte OTGender = (int)Gender.Female;
public string nickname = "Hello";

public int[] IV = [31, 31, 31, 31, 31, 31];
public int[] EV = [0, 0, 0, 0, 0, 0];

public ushort [] moves = {425, 262};
public ushort[] movespp = { 30, 40 };


public EggCreator(byte ball, ushort dexNumber, int language, int ability, Nature Nature, string OT, byte OTGender, string nickname, int[] IV, int[] EV, ushort[] moves, ushort[] movespp) {

this.ball = ball;
this.dexNumber = dexNumber;
this.language = language;
this.Ability = ability;
this.Nature = Nature;
this.OT = OT;
this.OTGender = OTGender;
this.nickname = nickname;
this.IV = IV;
this.EV = EV;
this.moves = moves;
this.movespp = movespp;


}


public static EggCreator decodeJSON(string filepath) {
string json = File.ReadAllText(filepath);

dynamic newObject = JsonConvert.DeserializeObject(json);

Check warning on line 85 in pkhex/pkhex-egglocke/pkhex-egglocke/EggCreator.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Nature nature = lookupNatureInt((int) newObject.nature.Value);

Check warning on line 87 in pkhex/pkhex-egglocke/pkhex-egglocke/EggCreator.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

int[] IV = newObject.IV.ToObject<int[]>();
int[] EV = newObject.EV.ToObject<int[]>();

int[] moves = newObject.moves.ToObject<int[]>();
int[] movespp = newObject.movespp.ToObject<int[]>();

return new EggCreator(
newObject.ball,
newObject.dexNumber,
newObject.language,
newObject.ability,
nature,
newObject.OT,
newObject.OTGender,
newObject.nickname,
IV,
EV,
convertIntArrayViaCast(moves),
convertIntArrayViaCast(movespp)

);

}

public static Nature lookupNatureInt(int nature)
{
if (nature == 1) {
return Nature.Adamant;
}

return Nature.Bashful;
}

public static ushort[] convertIntArrayViaCast(int[] array)
{

ushort[] result = new ushort[array.Length];

for (int i = 0; i < result.Length; i++)
{
result[i] = (ushort) array[i];
}

return result;
}


}
}
15 changes: 15 additions & 0 deletions pkhex/pkhex-egglocke/pkhex-egglocke/JSONDecoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pkhex_egglocke
{
internal class JSONDecoder
{

// Very annoying code that decodes strings to class-defined integers

}
}
20 changes: 0 additions & 20 deletions pkhex/pkhex-egglocke/pkhex-egglocke/PokemonCreator.cs

This file was deleted.

25 changes: 24 additions & 1 deletion pkhex/pkhex-egglocke/pkhex-egglocke/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
// See https://aka.ms/new-console-template for more information


using pkhexEgglocke;
using System.Reflection;

class Program {

public static void Main(string[] args)
{

Console.WriteLine("Hello World!");
var BLANK_SOULSILVER_SAVE = @"C:\Users\Evin Jaff\Downloads\johtocomplete.sav";

SaveWriter sw = new SaveWriter(BLANK_SOULSILVER_SAVE);

//EggCreator pc = new EggCreator();

//sw.addEgg(pc, 1);

// Output the new one
//sw.export("testPROG.sav");

//Console.WriteLine("Dumped to testPROG.sav!!");
var BLANK_GEN4_MAREEP_VALID = Path.Combine("C:\\Users\\Evin Jaff\\Documents\\egglocke-maker\\pkhex\\pkhex-egglocke-tests\\pkhex-egglocke-tests\\testSources", "Mareep.json");

EggCreator ec = EggCreator.decodeJSON(BLANK_GEN4_MAREEP_VALID);

sw.addEgg(ec, 1);

sw.export("testPROG.sav");

Console.WriteLine("Dumped testPROG");

}

Expand Down
69 changes: 67 additions & 2 deletions pkhex/pkhex-egglocke/pkhex-egglocke/SaveWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,74 @@ public SaveWriter(string savePath) {

}

public void addEgg( EggCreator pokemon, int boxIndex) {

// hacky start - hardcoded to box 1

var mew = new PK4();

// Standard Egg attributes
mew.IsEgg = pokemon.IsEgg;
mew.EggLocationDP = pokemon.EggLocationDP;
mew.MetLevel = pokemon.MetLevel;
mew.Ball = pokemon.ball;


// Pokemon Info
mew.Species = pokemon.dexNumber;
mew.Nickname = pokemon.nickname;
mew.Language = pokemon.language;
mew.OriginalTrainerName = pokemon.OT;
mew.OriginalTrainerGender = pokemon.OTGender;


mew.Ability = pokemon.Ability;
mew.Nature = pokemon.Nature;

// Moveset
mew.Move1 = (int)Move.ShadowSneak;
mew.Move1_PP = 30;
mew.Move2 = (int)Move.Memento;
mew.Move2_PP = 20;


mew.IVs = pokemon.IV;

var box = this.currentSave.BoxData;

Check warning on line 87 in pkhex/pkhex-egglocke/pkhex-egglocke/SaveWriter.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 87 in pkhex/pkhex-egglocke/pkhex-egglocke/SaveWriter.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

// Check legality of the pokemon

LegalityAnalysis legalitychecker = new LegalityAnalysis(mew);

#if DEBUG
if (legalitychecker.Valid)
{
Console.WriteLine("Legal Egg created!");
}
else {
Console.WriteLine("Illegal Egg Created!");
}
#endif

box[boxIndex] = mew;


// update box
this.currentSave.BoxData = box;

// this.currentSave.AddBoxData( , 1, 1 );


}


public void export(string location) {

byte[] modifiedSaveData = this.currentSave.Write();

Check warning on line 117 in pkhex/pkhex-egglocke/pkhex-egglocke/SaveWriter.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 117 in pkhex/pkhex-egglocke/pkhex-egglocke/SaveWriter.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

// dump to location
File.WriteAllBytes(location, modifiedSaveData);

public void export() {
Console.WriteLine("To be Implemented!");
}


Expand Down
Loading

0 comments on commit ee6ed57

Please sign in to comment.