| 1 | #!/usr/bin/perl | ||
| 2 | package mapextract; | ||
| 3 | # Axmud map extraction tool, by A S Lewis | ||
| 4 | # Version 1.0, 17 Dec 2018 | ||
| 5 | # Version 1.1, 12 Feb 2019 (update to Gtk3) | ||
| 6 | # Works on data files produced by Axmud v1.1.0 or later | ||
| 7 | # This file is in the public domain | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | #use diagnostics; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | use Glib qw(TRUE FALSE); | ||
| 14 | |||
| 15 | # Modules used by this script | ||
| 16 | use Gtk3 '-init'; | ||
| 17 | use Storable qw(lock_nstore lock_retrieve); | ||
| 18 | |||
| 19 | # Global variable | ||
| 20 | use vars qw( $FINISH_FLAG ); | ||
| 21 | $FINISH_FLAG = FALSE; | ||
| 22 | |||
| 23 | # Standard window creation | ||
| 24 | my $mainWin = Gtk3::Window->new('toplevel'); | ||
| 25 | $mainWin->signal_connect('delete_event' => sub { Gtk3->main_quit; }); | ||
| 26 | $mainWin->set_default_size(600, 400); | ||
| 27 | $mainWin->set_border_width(5); | ||
| 28 | $mainWin->set_position('center_always'); | ||
| 29 | $mainWin->set_title('Axmud map extractor'); | ||
| 30 | |||
| 31 | # Show a textview for confirmations, and a button to do stuff | ||
| 32 | my $vBox = Gtk3::VBox->new(FALSE, 0); | ||
| 33 | $mainWin->add($vBox); | ||
| 34 | |||
| 35 | my $scrollWin = Gtk3::ScrolledWindow->new(undef, undef); | ||
| 36 | $vBox->pack_start($scrollWin, TRUE, TRUE, 5); | ||
| 37 | $scrollWin->set_policy('automatic', 'automatic'); | ||
| 38 | $scrollWin->set_border_width(0); | ||
| 39 | |||
| 40 | my $textView = Gtk3::TextView->new; | ||
| 41 | $scrollWin->add_with_viewport($textView); | ||
| 42 | $textView->set_can_focus(FALSE); | ||
| 43 | $textView->set_wrap_mode('word-char'); | ||
| 44 | $textView->set_justification('left'); | ||
| 45 | my $buffer = $textView->get_buffer(); | ||
| 46 | |||
| 47 | my $button = Gtk3::Button->new_with_label('Select file'); | ||
| 48 | $vBox->pack_start($button, FALSE, FALSE, 5); | ||
| 49 | $button->signal_connect('clicked' => sub { | ||
| 50 | |||
| 51 | if (! $FINISH_FLAG) { | ||
| 52 | |||
| 53 | &buttonCallback($button, $buffer); | ||
| 54 | |||
| 55 | } else { | ||
| 56 | |||
| 57 | $mainWin->destroy(); | ||
| 58 | exit; | ||
| 59 | } | ||
| 60 | }); | ||
| 61 | |||
| 62 | # Show the window | ||
| 63 | $buffer->set_text( | ||
| 64 | "Has your map broken Axmud? Do you want to send your map to the authors, but can't? Then use" | ||
| 65 | . " this script!\n\n" | ||
| 66 | . "The script creates a copy of the map. It removes all labels, then it creates a new file" | ||
| 67 | . " which can be saved in a convenient location (for example, your desktop.)\n\n" | ||
| 68 | . "First find the location of Axmud's data directory. HINT: You can use the following client" | ||
| 69 | . " command:\n\n" | ||
| 70 | . " ;listdatadirectory\n ;ldd\n\n" | ||
| 71 | . "When you've found the data directory, find the world model (map) file. For example, if you" | ||
| 72 | . " want to extract a Discworld map, look for:\n\n" | ||
| 73 | . " .../data/worlds/discworld/worldmodel.axm\n\n" | ||
| 74 | . "When you've found the right file, click the button below, and select that file.", | ||
| 75 | ); | ||
| 76 | |||
| 77 | $mainWin->show_all(); | ||
| 78 | |||
| 79 | # Main event loop | ||
| 80 | Gtk3->main(); | ||
| 81 | |||
| 82 | # Callback functions | ||
| 83 | |||
| 84 | sub buttonCallback { | ||
| 85 | |||
| 86 | my ($button, $buffer) = @_; | ||
| 87 | |||
| 88 | $buffer->set_text("Selecting the worldmodel.axm file..."); | ||
| 89 | |||
| 90 | $button->set_sensitive(FALSE); | ||
| 91 | |||
| 92 | # Get a path to the worldmodel.axm file | ||
| 93 | my $path; | ||
| 94 | |||
| 95 | my $dialogueWin = Gtk3::FileChooserDialog->new( | ||
| 96 | 'Select the world model file (worldmodel.axm)', | ||
| 97 | $mainWin, | ||
| 98 | 'open', | ||
| 99 | 'gtk-cancel' => 'cancel', | ||
| 100 | 'gtk-ok' => 'ok' | ||
| 101 | ); | ||
| 102 | |||
| 103 | $dialogueWin->set_position('center-always'); | ||
| 104 | $dialogueWin->signal_connect('delete-event' => sub { | ||
| 105 | |||
| 106 | $dialogueWin->destroy(); | ||
| 107 | |||
| 108 | $buffer->insert_with_tags_by_name( | ||
| 109 | $buffer->get_end_iter(), | ||
| 110 | "\nOperation cancelled", | ||
| 111 | ); | ||
| 112 | |||
| 113 | return &resetButton($button); | ||
| 114 | }); | ||
| 115 | |||
| 116 | if ($dialogueWin->run ne 'ok') { | ||
| 117 | |||
| 118 | $buffer->insert_with_tags_by_name( | ||
| 119 | $buffer->get_end_iter(), | ||
| 120 | "\nOperation failed", | ||
| 121 | ); | ||
| 122 | |||
| 123 | return &resetButton($button); | ||
| 124 | |||
| 125 | } else { | ||
| 126 | |||
| 127 | $path = $dialogueWin->get_filename(); | ||
| 128 | $dialogueWin->destroy(); | ||
| 129 | |||
| 130 | $buffer->insert_with_tags_by_name( | ||
| 131 | $buffer->get_end_iter(), | ||
| 132 | "\nSelected file $path...", | ||
| 133 | ); | ||
| 134 | } | ||
| 135 | |||
| 136 | # Load the file | ||
| 137 | my $hashRef = Storable::lock_retrieve($path); | ||
| 138 | if (! $hashRef) { | ||
| 139 | |||
| 140 | $buffer->insert_with_tags_by_name( | ||
| 141 | $buffer->get_end_iter(), | ||
| 142 | "\nCould not load $path", | ||
| 143 | ); | ||
| 144 | |||
| 145 | return &resetButton($button); | ||
| 146 | } | ||
| 147 | |||
| 148 | # Extract the world model | ||
| 149 | my $wmObj = $$hashRef{world_model_obj}; | ||
| 150 | if (! $wmObj) { | ||
| 151 | |||
| 152 | $buffer->insert_with_tags_by_name( | ||
| 153 | $buffer->get_end_iter(), | ||
| 154 | "\nOperation failed - file does not contain a world model", | ||
| 155 | ); | ||
| 156 | |||
| 157 | return &resetButton($button); | ||
| 158 | } | ||
| 159 | |||
| 160 | $buffer->insert_with_tags_by_name( | ||
| 161 | $buffer->get_end_iter(), | ||
| 162 | "\nRemoving labels...", | ||
| 163 | ); | ||
| 164 | |||
| 165 | # Replace text of all map label objects | ||
| 166 | my $hashRef2 = $wmObj->{regionmapHash}; | ||
| 167 | foreach my $regionmapObj (values %$hashRef2) { | ||
| 168 | |||
| 169 | my $thisHashRef = $regionmapObj->{gridLabelHash}; | ||
| 170 | |||
| 171 | foreach my $mapLabelObj (values %$thisHashRef) { | ||
| 172 | |||
| 173 | my $name = $mapLabelObj->{name}; | ||
| 174 | |||
| 175 | $mapLabelObj->{name} = "X" x length($name); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | # Replace character visits, if any | ||
| 180 | my $hashRef3 = $wmObj->{roomModelHash}; | ||
| 181 | foreach my $roomObj (values %$hashRef3) { | ||
| 182 | |||
| 183 | $roomObj->{visitHash} = {}; | ||
| 184 | } | ||
| 185 | |||
| 186 | $buffer->insert_with_tags_by_name( | ||
| 187 | $buffer->get_end_iter(), | ||
| 188 | "\nSelecting save location...", | ||
| 189 | ); | ||
| 190 | |||
| 191 | # Get a directory where the extracted file should be saved | ||
| 192 | my $path2; | ||
| 193 | |||
| 194 | my $dialogueWin2 = Gtk3::FileChooserDialog->new( | ||
| 195 | 'Set the name and location of the new file', | ||
| 196 | $mainWin, | ||
| 197 | 'save', | ||
| 198 | 'gtk-cancel' => 'cancel', | ||
| 199 | 'gtk-ok' => 'ok' | ||
| 200 | ); | ||
| 201 | |||
| 202 | $dialogueWin2->set_position('center-always'); | ||
| 203 | $dialogueWin2->signal_connect('delete-event' => sub { | ||
| 204 | |||
| 205 | $dialogueWin2->destroy(); | ||
| 206 | |||
| 207 | $buffer->insert_with_tags_by_name( | ||
| 208 | $buffer->get_end_iter(), | ||
| 209 | "\nOperation cancelled", | ||
| 210 | ); | ||
| 211 | |||
| 212 | return &resetButton($button); | ||
| 213 | }); | ||
| 214 | |||
| 215 | if ($dialogueWin2->run ne 'ok') { | ||
| 216 | |||
| 217 | $buffer->insert_with_tags_by_name( | ||
| 218 | $buffer->get_end_iter(), | ||
| 219 | "\nOperation failed", | ||
| 220 | ); | ||
| 221 | |||
| 222 | return &resetButton($button); | ||
| 223 | |||
| 224 | } else { | ||
| 225 | |||
| 226 | $path2 = $dialogueWin2->get_filename(); | ||
| 227 | $dialogueWin2->destroy(); | ||
| 228 | } | ||
| 229 | |||
| 230 | $buffer->insert_with_tags_by_name( | ||
| 231 | $buffer->get_end_iter(), | ||
| 232 | "\nSaving file to $path2...", | ||
| 233 | ); | ||
| 234 | |||
| 235 | # Save the file | ||
| 236 | $$hashRef{world_model_obj} = $wmObj; | ||
| 237 | Storable::lock_nstore($hashRef, $path2); | ||
| 238 | if ($@) { | ||
| 239 | |||
| 240 | $buffer->insert_with_tags_by_name( | ||
| 241 | $buffer->get_end_iter(), | ||
| 242 | "\nCould not load $path", | ||
| 243 | ); | ||
| 244 | |||
| 245 | return &resetButton($button); | ||
| 246 | } | ||
| 247 | |||
| 248 | $buffer->insert_with_tags_by_name( | ||
| 249 | $buffer->get_end_iter(), | ||
| 250 | "\nFile saved...\nOperation complete!", | ||
| 251 | ); | ||
| 252 | |||
| 253 | return &resetButton($button); | ||
| 254 | } | ||
| 255 | |||
| 256 | sub resetButton { | ||
| 257 | |||
| 258 | my ($button) = @_; | ||
| 259 | |||
| 260 | $button->set_label('Close window'); | ||
| 261 | $button->set_sensitive(TRUE); | ||
| 262 | |||
| 263 | $FINISH_FLAG = TRUE; | ||
| 264 | |||
| 265 | return 1; | ||
| 266 | } |