GEmeTool/scripts/start.pl

182 lines
5.8 KiB
Perl

#!/usr/bin/env perl
use v5.16.3;
use strict;
use warnings;
use Glib;
use Glib::IO;
use Glib::Object::Introspection;
use Data::Dumper;
use Path::Tiny;
use utf8;
Glib::Object::Introspection->setup(
basename => 'Gtk',
version => '4.0',
package => 'Gtk4',
);
use Rsaves
qw/read_save check_correct_size get_saves find_current_save_index check_correct_size find_pokemon_substruct change_gender read_pc_storage save_pc_changes enable_eon_ticket save_changes pokemon_set_shiny read_pkm_file_box parse_version_name check_flag_id get_first_super_data check_rematch/;
use Rsaves::Constants::Global
qw/$SAPPHIRE_VERSION $RUBY_VERSION $EMERALD_VERSION $FIRERED_VERSION $LEAFGREEN_VERSION $COLOSSEUM_VERSION/;
use Rsaves::Constants::Emerald::Flags;
use Rsaves::Constants::Emerald::Rematches;
my %rematches = @Rsaves::Constants::Emerald::Rematches::REMATCHES;
my $app = Gtk4::Application->new( 'tech.owlcode.GEmeTool', 'default-flags' );
$app->signal_connect( activate => \&activate );
$app->run;
sub activate {
my $win;
my $menu = Glib::IO::Menu->new;
my $about = Glib::IO::SimpleAction->new( 'about', undef );
my $open = Glib::IO::SimpleAction->new( 'open', undef );
my $save_as = Glib::IO::SimpleAction->new( 'save_as', undef );
$save_as->set_enabled(0);
$app->add_action($about);
$app->add_action($open);
$app->add_action($save_as);
my @saves;
my $extra;
my $save_menu_item = Glib::IO::MenuItem->new( 'Save as', 'app.save_as' );
$open->signal_connect(
activate => sub {
activate_open( $win, \@saves, \$extra, $save_as );
}
);
$save_as->signal_connect(
activate => sub {
activate_save( $win, \@saves, $extra );
}
);
$about->signal_connect( activate => \&activate_about, );
my $about_menu_item = Glib::IO::MenuItem->new( 'About', 'app.about' );
my $open_menu_item = Glib::IO::MenuItem->new( 'Open', 'app.open' );
my $submenu_file = Glib::IO::Menu->new;
my $submenu_help = Glib::IO::Menu->new;
$submenu_help->append_item($about_menu_item);
$submenu_file->append_item($open_menu_item);
$submenu_file->append_item($save_menu_item);
$menu->append_submenu( 'File', $submenu_file );
$menu->append_submenu( 'Help', $submenu_help );
$app->set_menubar($menu);
my $box = Gtk4::Box->new( 'vertical', 0 );
$box->append(
Gtk4::Label->new('You have to load a Pokemon Emerald Save.') );
$win = Gtk4::ApplicationWindow->new($app);
$win->set_title('GEmeTool');
$win->set_show_menubar(1);
$win->set_default_size( 600, 600 );
$win->set_child($box);
$win->present;
}
sub activate_open {
my $win = shift;
my $saves = shift;
my $extra = shift;
my $save_as = shift;
my $dialog = Gtk4::FileDialog->new;
my $curdir = Glib::IO::File::new_for_path('.');
$dialog->set_initial_folder($curdir);
$dialog->open(
$win, undef,
sub {
my ( $self, $res ) = @_;
my $file = $dialog->open_finish($res);
return if !defined $file;
$file = path( $file->get_path );
start_editing_file( $win, $file, $saves, $extra, $save_as );
}
);
}
sub activate_save {
my $win = shift;
my $saves = shift;
my $extra = shift;
my $dialog = Gtk4::FileDialog->new;
my $curdir = Glib::IO::File::new_for_path('.');
$dialog->set_initial_folder($curdir);
$dialog->open(
$win, undef,
sub {
my ( $self, $res ) = @_;
my $file = $dialog->open_finish($res);
return if !defined $file;
$file = path( $file->get_path );
save( $file, $saves, $extra );
}
);
}
sub save {
my ( $file, $saves, $extra ) = @_;
save_changes( @$saves, $extra, $file );
}
sub start_editing_file {
my $win = shift;
my $file = shift;
my $saves = shift;
my $extra = shift;
my $save_as = shift;
my (@saves_raw);
( @saves_raw[ 0, 1 ], ${$extra} ) = read_save($file);
@$saves = get_saves( @saves_raw, $EMERALD_VERSION );
my $current_save_index = find_current_save_index(@$saves);
my $save = $saves->[$current_save_index];
my %flags = @Rsaves::Constants::Emerald::Flags::FLAGS;
my $box_flags = Gtk4::Box->new( 'vertical', 0 );
my $superdata = get_first_super_data($save);
for my $rematch_id (
sort { $a <=> $b }
grep { $_ =~ /^\d+$/ } keys %rematches
)
{
my $toggle =
Gtk4::ToggleButton->new_with_label( $rematches{$rematch_id} );
$toggle->set_active( check_rematch( $save, $superdata, $rematch_id ) );
$toggle->signal_connect(
toggled => sub {
my $active = $toggle->get_active;
Rsaves::set_rematch( $save, $superdata, $rematch_id, $active );
Rsaves::set_first_super_data( $save, $superdata );
}
);
$box_flags->append($toggle);
}
# for my $flag_id ( sort { $a <=> $b } grep { $_ =~ /^\d+$/ } keys %flags ) {
# my $toggle = Gtk4::ToggleButton->new_with_label( $flags{$flag_id} );
# $toggle->set_active( check_flag_id( $save, $superdata, $flag_id ) );
#
# $box_flags->append($toggle);
# }
my $scroll = Gtk4::ScrolledWindow->new;
$scroll->set_child($box_flags);
$win->set_child($scroll);
$save_as->set_enabled(1);
}
sub activate_about {
my $about = Gtk4::AboutDialog->new;
$about->set_program_name('GEmeTool');
$about->set_copyright( "©Sergio Iglesias 2024\n" . "GPLv3 or later" );
my $header_bar = Gtk4::HeaderBar->new;
$header_bar->set_property(
'title-widget' => Gtk4::Label->new('About GEmeTool') );
$about->set_titlebar($header_bar);
$about->present;
}