236 lines
7.9 KiB
Perl
236 lines
7.9 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;
|
|
|
|
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;
|
|
|
|
Glib::Object::Introspection->setup(
|
|
basename => 'Gtk',
|
|
version => '4.0',
|
|
package => 'Gtk4',
|
|
);
|
|
|
|
Glib::Object::Introspection->setup(
|
|
basename => 'Gdk',
|
|
version => '4.0',
|
|
package => 'Gdk',
|
|
);
|
|
|
|
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 $display = Gdk::Display::get_default();
|
|
my $icon_theme = Gtk4::IconTheme::get_for_display($display);
|
|
$icon_theme->set_search_path(path(__FILE__)->parent->parent->child('resources/icons')->absolute);
|
|
Gtk4::Window::set_default_icon_name('gemetool');
|
|
|
|
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_icon_name('gemetool');
|
|
$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_app = Gtk4::Box->new( 'vertical', 0 );
|
|
my $superdata = get_first_super_data($save);
|
|
|
|
my $search = Gtk4::SearchBar->new;
|
|
my $entry = Gtk4::Entry->new;
|
|
$search->set_child($entry);
|
|
$search->set_search_mode(1);
|
|
$box_app->append($search);
|
|
$box_app->set_property( 'vexpand', 1 );
|
|
my $scroll = Gtk4::ScrolledWindow->new;
|
|
$scroll->set_property( 'vexpand', 1 );
|
|
|
|
my $populate_func = sub {
|
|
my $box_flags = Gtk4::Box->new( 'vertical', 0 );
|
|
for my $rematch_id (
|
|
sort { $a <=> $b }
|
|
grep { $_ =~ /^\d+$/ } keys %rematches
|
|
)
|
|
{
|
|
my $buffer = $entry->get_buffer;
|
|
my $search_text = uc( $buffer->get_text );
|
|
my $text = $rematches{$rematch_id};
|
|
next unless index( $text, $search_text ) >= 0;
|
|
my $toggle = Gtk4::ToggleButton->new_with_label($text);
|
|
$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 $buffer = $entry->get_buffer;
|
|
my $search_text = uc( $buffer->get_text );
|
|
my $text = $flags{$flag_id};
|
|
next unless index( $text, $search_text ) >= 0;
|
|
my $toggle = Gtk4::ToggleButton->new_with_label($text);
|
|
$toggle->set_active( check_flag_id( $save, $superdata, $flag_id ) );
|
|
$toggle->signal_connect(
|
|
toggled => sub {
|
|
my $active = $toggle->get_active;
|
|
Rsaves::set_flag_id( $save, $superdata, $flag_id, $active );
|
|
Rsaves::set_first_super_data( $save, $superdata );
|
|
}
|
|
);
|
|
|
|
$box_flags->append($toggle);
|
|
}
|
|
$scroll->set_child($box_flags);
|
|
};
|
|
$populate_func->();
|
|
$entry->signal_connect( activate => sub {
|
|
$populate_func->();
|
|
});
|
|
$box_app->append($scroll);
|
|
$win->set_child($box_app);
|
|
$save_as->set_enabled(1);
|
|
|
|
}
|
|
|
|
sub activate_about {
|
|
my $about = Gtk4::AboutDialog->new;
|
|
$about->set_program_name('GEmeTool');
|
|
$about->set_copyright("© Sergio Iglesias (2024)");
|
|
$about->set_license_type('gpl-3-0');
|
|
$about->add_credit_section( 'Ideas:', ['SpectreSpecs'] );
|
|
$about->set_authors( ['Sergio Iglesias'] );
|
|
my $texture = Gdk::Texture->new_from_filename(path(__FILE__)->parent->parent->child('resources/icons/scalable/actions/gemetool.svg'));
|
|
$about->set_logo($texture);
|
|
$about->set_website('https://git.owlcode.tech/sergiotarxz/GEmeTool');
|
|
$about->set_website_label('https://git.owlcode.tech/sergiotarxz/GEmeTool');
|
|
|
|
# my $header_bar = Gtk4::HeaderBar->new;
|
|
# $header_bar->set_property(
|
|
# 'title-widget' => Gtk4::Label->new('About GEmeTool') );
|
|
# $about->set_titlebar($header_bar);
|
|
$about->present;
|
|
}
|