Skip to content

Commit

Permalink
- handle opening folders of pks, wondercards files, box bins, and enc…
Browse files Browse the repository at this point in the history
…ounter files.

- update the save files current box based on selected box in viewer
- initially load saves current box
  • Loading branch information
santacrab2 committed Oct 11, 2023
1 parent 68fcb15 commit f70d589
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 9 deletions.
3 changes: 2 additions & 1 deletion boxtab/BoxTab.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public BoxTab()
boxrefresh.IsRefreshing = false;
});
boxrefresh.Command = refreshCommand;
boxnum.SelectedIndex = 0;
boxnum.SelectedIndex = sav.CurrentBox;
}
public static IList<boxsprite> boxsprites = new List<boxsprite>();
public static int CurrentBox = 0;
Expand Down Expand Up @@ -142,6 +142,7 @@ public void copyboxdata()
private void changebox(object sender, EventArgs e)
{
CurrentBox = boxnum.SelectedIndex;
sav.CurrentBox = CurrentBox;
fillbox();

}
Expand Down
100 changes: 92 additions & 8 deletions pktabs/PKTab.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,110 @@ public async void pk9picker_Clicked(object sender, EventArgs e)
var pkfile = await FilePicker.PickAsync();
if (pkfile is null)
return;
var bytes = File.ReadAllBytes(pkfile.FullPath);
pk = EntityFormat.GetFromBytes(bytes);
if (pk.GetType() != sav.PKMType)

var obj = FileUtil.GetSupportedFile(pkfile.FullPath);
if(obj is null) return;
switch (obj)
{
case PKM pkm: OpenPKMFile(pkm);return;
case IPokeGroup b: OpenGroup(b); return;
case MysteryGift g: OpenMG(g); return;
case IEnumerable<byte[]> pkms: OpenPCBoxBin(pkms); return;
case IEncounterConvertible enc: OpenPKMFile(enc.ConvertToPKM(sav));return;
}


}
public async void OpenPCBoxBin(IEnumerable<byte[]> pkms)
{
var data = pkms.SelectMany(z => z).ToArray();
if (sav.GetPCBinary().Length == data.Length)
{
pk = EntityConverter.ConvertToType(pk, sav.PKMType, out var result);
if (sav.IsAnySlotLockedInBox(0, sav.BoxCount - 1))
{
await DisplayAlert("Fail", "Battle Box slots prevent loading of PC data.", "cancel");
return;
}
if(!sav.SetPCBinary(data))
{
await DisplayAlert("Fail", "Failed to import", "cancel");
return;
}
await DisplayAlert("Success", "PC Binary imported.", "okay");

}
else if(sav.GetBoxBinary(sav.CurrentBox).Length == data.Length)
{
if (sav.IsAnySlotLockedInBox(0, sav.BoxCount - 1))
{
await DisplayAlert("Fail", "Battle Box slots prevent loading of Box data.", "cancel");
return;
}
if (!sav.SetBoxBinary(data,sav.CurrentBox))
{
await DisplayAlert("Fail", "Failed to import", "cancel");
return;
}
await DisplayAlert("Success", "Box Binary imported.", "okay");
}
else
{
await DisplayAlert("Fail", "Failed to import", "cancel");
}
}
public async void OpenMG(MysteryGift g)
{
if(!g.IsEntity) return;
var pkm = g.ConvertToPKM(sav);
OpenPKMFile(pkm);
}
public async void OpenGroup(IPokeGroup b)
{
var type = sav.PKMType;
int slotSkipped = 0;
int index = 0;
foreach (var x in b.Contents)
{
var i = index++;
if (sav.IsSlotOverwriteProtected(sav.CurrentBox, i))
{
slotSkipped++;
continue;
}

var convert = EntityConverter.ConvertToType(x, type, out _);
if (convert?.GetType() != type)
{
slotSkipped++;
continue;
}
sav.SetBoxSlotAtIndex(x, sav.CurrentBox, i);
}
}
public async void OpenPKMFile(PKM pkm)
{
if (pkm.GetType() != sav.PKMType)
{
var newpkm = EntityConverter.ConvertToType(pkm, sav.PKMType, out var result);
if (result.IsSuccess() || PSettings.AllowIncompatibleConversion)
{
applymainpkinfo(pk);
sav.AdaptPKM(newpkm);

applymainpkinfo(newpkm);
checklegality();
pk = newpkm;
return;
}
else
{
await DisplayAlert("Incompatible", "This PK file is incompatible with the current save file", "cancel");
await DisplayAlert("Incompatible", "This file is incompatible with the current save file", "cancel");
return;
}
}

applymainpkinfo(pk);
sav.AdaptPKM(pkm);
applymainpkinfo(pkm);
checklegality();
pk = pkm;
}
public void checklegality()
{
Expand Down

0 comments on commit f70d589

Please sign in to comment.