| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use 5.010; | ||
| 6 | |||
| 7 | use Cwd qw(getcwd abs_path); | ||
| 8 | use Archive::Zip qw(:ERROR_CODES); | ||
| 9 | use File::Path qw(rmtree); | ||
| 10 | use File::Copy::Recursive qw(fcopy dircopy); | ||
| 11 | |||
| 12 | my $jar_bin; | ||
| 13 | my $mod; | ||
| 14 | my $verbose = 0; | ||
| 15 | my $backup = 0; | ||
| 16 | my $restore = 0; | ||
| 17 | my $home = $ENV{HOME} | ||
| 18 | or die "Could not get home from \$ENV\n"; | ||
| 19 | my $app_dir = "${home}/.minecraft-app"; | ||
| 20 | my $last = $ARGV[ scalar(@ARGV) -1 ]; | ||
| 21 | |||
| 22 | sub _show_usage { | ||
| 23 | print STDERR "Minecraft App\n\n"; | ||
| 24 | print STDERR "Options:\n"; | ||
| 25 | print STDERR " backup - Backups Minecraft directory\n"; | ||
| 26 | print STDERR " restore - Restores Minecraft directory from backup\n"; | ||
| 27 | print STDERR " install <zip> - Installs a mod (Requres zip format)\n\n"; | ||
| 28 | print STDERR "Flags:\n"; | ||
| 29 | print STDERR " -h - This menu\n"; | ||
| 30 | print STDERR " -v|--verbose - More warnings\n"; | ||
| 31 | print STDERR " -j|--jar - Override location of jar binary\n"; | ||
| 32 | exit(0); | ||
| 33 | } | ||
| 34 | |||
| 35 | while(my $arg = shift @ARGV) { | ||
| 36 | for ($arg) { | ||
| 37 | if (/^backup$/) { $backup = 1; } | ||
| 38 | elsif (/^restore$/) { $restore = 1; } | ||
| 39 | elsif (/^install$/) { $mod = 1; } | ||
| 40 | elsif (/^-v$|^--verbose$/) { $verbose = 1; } | ||
| 41 | elsif (/^-j$|^--jar$/) { $jar_bin = shift @ARGV; } | ||
| 42 | elsif (/^-h$|^--help$/) { _show_usage(); } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | $mod = $last | ||
| 47 | if $mod; | ||
| 48 | |||
| 49 | _show_usage() | ||
| 50 | if not $mod and not $backup and not $restore; | ||
| 51 | |||
| 52 | sub _create_config { | ||
| 53 | open my $cfg, '>', "${app_dir}/config.yml" or | ||
| 54 | return (0, "Could not write to ${app_dir}/config.yml"); | ||
| 55 | print $cfg "Minecraft:\n"; | ||
| 56 | print $cfg " bin: ${app_dir}/minecraft.jar\n"; | ||
| 57 | close $cfg; | ||
| 58 | |||
| 59 | return 1; | ||
| 60 | } | ||
| 61 | |||
| 62 | INITIALIZATORING: { | ||
| 63 | say "[info] Initialising Minecraft Manager..." | ||
| 64 | if $verbose; | ||
| 65 | |||
| 66 | if (not -e $app_dir) { | ||
| 67 | print "[info] Could not locate app dir, creating... "; | ||
| 68 | if (my $mkdir = mkdir($app_dir)) { | ||
| 69 | say "Done!"; | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | print "Failed, because: $!\n"; | ||
| 73 | exit(1); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | # is jar installed? | ||
| 78 | print "[info] Searching for the jar binary... " | ||
| 79 | if $verbose; | ||
| 80 | |||
| 81 | if ($jar_bin) { | ||
| 82 | if (not -e $jar_bin) { | ||
| 83 | say "Failed" if $verbose; | ||
| 84 | die "[error] Could not find 'jar' binary in $jar_bin\n"; | ||
| 85 | } | ||
| 86 | else { | ||
| 87 | say "Found in ${jar_bin}" if $verbose; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | else { | ||
| 91 | if (my $path_env = $ENV{PATH}) { | ||
| 92 | my @paths = split ':', $path_env; | ||
| 93 | for my $path (@paths) { | ||
| 94 | $jar_bin = "${path}/jar" | ||
| 95 | if -e "${path}/jar"; | ||
| 96 | } | ||
| 97 | |||
| 98 | if ($jar_bin) { | ||
| 99 | say "Found in ${jar_bin}" if $verbose; | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | say "Failed" if $verbose; | ||
| 103 | die "[error] Could not find the 'jar' binary in your paths or config\n"; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | if ($backup and $restore) { | ||
| 110 | die "[stupidity] Why are you trying to backup and restore at the same time?\n"; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _run_backup { | ||
| 114 | if (my $num_files_and_dirs = dircopy("${home}/.minecraft", "${app_dir}/minecraft")) { | ||
| 115 | say "[info] Successfully backed up ${num_files_and_dirs} files and directories"; | ||
| 116 | exit(0); | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | die "[error] Backup failed\n"; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | sub _run_restore { | ||
| 124 | if (my $num_files_and_dirs = dircopy("${app_dir}/minecraft", "${home}/.minecraft")) { | ||
| 125 | say "[info] Successfully restored ${num_files_and_dirs} files and directories"; | ||
| 126 | exit(0); | ||
| 127 | } | ||
| 128 | else { | ||
| 129 | die "[error] Restore failed\n"; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | sub _run_install { | ||
| 134 | if ($mod) { | ||
| 135 | if (-e $mod) { | ||
| 136 | my ($ext) = $mod =~ /(\.[^.]+)$/; | ||
| 137 | if ($ext ne '.zip') { | ||
| 138 | die "[error] Mod file type must be .zip\n"; | ||
| 139 | } | ||
| 140 | my $err; | ||
| 141 | my $zip = Archive::Zip->new(); | ||
| 142 | say "[info] Installing ${mod}... "; | ||
| 143 | print "[info] Verifying Zip... "; | ||
| 144 | unless ($zip->read($mod) == AZ_OK) { | ||
| 145 | die "Failed\n"; | ||
| 146 | } | ||
| 147 | |||
| 148 | rmtree "${app_dir}/jar" | ||
| 149 | if -d "${app_dir}/jar"; | ||
| 150 | |||
| 151 | my $mod_path = ($mod =~ /\//) ? | ||
| 152 | abs_path($mod) : getcwd(); | ||
| 153 | mkdir "${app_dir}/jar"; | ||
| 154 | chdir "${app_dir}/jar"; | ||
| 155 | $err = `$jar_bin -xf ${home}/.minecraft/bin/minecraft.jar`; | ||
| 156 | |||
| 157 | if ($err) { | ||
| 158 | print STDERR "[error} Failed extracting minecraft jar file\n"; | ||
| 159 | chdir("${app_dir}"); | ||
| 160 | rmtree "${app_dir}/jar"; | ||
| 161 | exit(1); | ||
| 162 | } | ||
| 163 | |||
| 164 | if (-f "${app_dir}/jar/META-INF") { rmtree "{$app_dir}/jar/META-INF"; } | ||
| 165 | say "OK"; | ||
| 166 | |||
| 167 | say "[info] Copying archive and installing..."; | ||
| 168 | fcopy("${mod_path}/${mod}", "${app_dir}/jar/${mod}"); | ||
| 169 | $zip = Archive::Zip->new("${app_dir}/jar/${mod}"); | ||
| 170 | # FIXME: Loop over once to see if the format is OK? | ||
| 171 | foreach my $member ($zip->members) { | ||
| 172 | $member->extractToFileNamed( | ||
| 173 | "${app_dir}/jar/" . $member->fileName); | ||
| 174 | } | ||
| 175 | |||
| 176 | unlink "${app_dir}/jar/${mod}"; | ||
| 177 | $err = `$jar_bin -uf ${home}/.minecraft/bin/minecraft.jar ./`; | ||
| 178 | |||
| 179 | if ($err) { | ||
| 180 | die "[error] There was a problem making minecraft.jar -- Please restore\n"; | ||
| 181 | } | ||
| 182 | |||
| 183 | chdir("${app_dir}"); | ||
| 184 | rmtree("${app_dir}/jar"); | ||
| 185 | say "[info] Finished installing mod"; | ||
| 186 | exit(0); | ||
| 187 | } | ||
| 188 | else { | ||
| 189 | die "[error] Can't find mod ${mod}\n"; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | die "[error] No mod was chosen to be installed\n"; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | _run_backup() if $backup; | ||
| 198 | _run_restore() if $restore; | ||
| 199 | _run_install() if $mod; | ||
| 200 |