Adding export to .pk3 file option.

This commit is contained in:
sergiotarxz 2024-03-12 15:47:46 +01:00
parent e26e807e8b
commit 7ebbc18eaa
3 changed files with 45 additions and 0 deletions

View File

@ -32,6 +32,12 @@ sub load_from_file {
return $class->new(_pokemon => Rsaves::read_pk3_file($file));
}
sub export_file {
my $self = shift;
my $file = shift;
Rsaves::write_pk3_file($self->_pokemon, $file);
}
sub empty {
my $class = shift;
return $class->new(_pokemon => Rsaves::read_pk3_raw("\0" x 0x80));

View File

@ -103,6 +103,7 @@ sub draw {
my $save_button = Gtk4::Button->new_with_label('Save changes');
my $delete_button = Gtk4::Button->new_with_label('Delete pokemon');
my $import_button = Gtk4::Button->new_with_label('Import .pk3');
my $export_button = Gtk4::Button->new_with_label('Export .pk3');
$self->_selected_species( $pokemon->species );
my $box_right_image = Gtk4::Box->new( 'vertical', 1 );
$box_right_image->set_margin_top(30);
@ -133,6 +134,9 @@ sub draw {
$import_button->signal_connect(clicked => sub {
$self->activate_import_pk3;
});
$export_button->signal_connect(clicked => sub {
$self->activate_export_pk3;
});
$self->draw_dropdown_pokemon_list($box_right_image);
my $box_label_iv = Gtk4::Box->new('horizontal', 10);
$box_label_iv->append(Gtk4::Label->new('Select IV'));
@ -140,11 +144,38 @@ sub draw {
$self->create_select_ivs($box_right_image);
$self->create_modify_personality($grid);
$grid->attach( $save_button, 4, 7, 1, 1 );
$grid->attach( $export_button, 3, 7, 1, 1 );
$grid->attach( $delete_button, 2, 7, 1, 1 );
$grid->attach( $import_button, 1, 7, 1, 1 );
$window->set_child($grid);
}
sub activate_export_pk3 {
my $self = shift;
my $win = $self->_win;
my $dialog = Gtk4::FileDialog->new;
my $options = GEmeTool::Options->new;
my $last_dir = $options->get_last_dir_open;
if ( defined $last_dir && -d $last_dir ) {
my $curdir = Glib::IO::File::new_for_path($last_dir);
$dialog->set_initial_folder($curdir);
$dialog->set_initial_name( Rsaves::translate_3rd_encoding($self->pokemon->nickname) . '-' . $self->pokemon->pokemon_name . '.pk3');
}
$dialog->save(
$win, undef,
sub {
my ( $self_dialog, $res ) = @_;
if ( $res->had_error ) {
return;
}
my $file = $dialog->save_finish($res);
return if !defined $file;
$file = path( $file->get_path );
$self->pokemon->export_file($file);
}
);
}
sub delete {
my $self = shift;
$self->pokemon->copy(GEmeTool::Save::Pokemon->empty);

View File

@ -295,6 +295,14 @@ sub get_pk3_raw {
return $return;
}
sub write_pk3_file {
my $pokemon = shift;
my $file = shift;
my $content = get_pk3_raw($pokemon);
open my $fh, '>', $file or return;
print $fh $content;
}
sub _write_pokemon_fh {
my $fh = shift;
my $pokemon = shift;