Last active
August 29, 2015 14:01
-
-
Save RobinMcCorkell/cd54ba1ec6041e5a10c6 to your computer and use it in GitHub Desktop.
Minecraft Forge ID Copier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# Copyright (C) 2014 Robin McCorkell <[email protected]> | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
use strict; | |
use warnings; | |
use Getopt::Long; | |
use File::Find; | |
use File::Copy; | |
use Pod::Usage; | |
my $origDir; | |
my $modDir; | |
my $opt_help = 0; | |
GetOptions( | |
'orig=s' => \$origDir, | |
'mod=s' => \$modDir, | |
'help|?' => \$opt_help) | |
or pod2usage(2); | |
pod2usage(1) if $opt_help; | |
pod2usage(1) unless $origDir and $modDir; | |
die "Invalid original directory '".$origDir."'\n" unless -d $origDir; | |
die "Invalid mod directory '".$modDir."'\n" unless -d $modDir; | |
# Sanitize inputs | |
$origDir = substr($origDir, 0, -1) if substr($origDir, -1) eq "/"; | |
$modDir = substr($origDir, 0, -1) if substr($modDir, -1) eq "/"; | |
sub chompy { | |
my ($ref) = @_; | |
$$ref =~ s/^\s+//; | |
$$ref =~ s/\s+$//; | |
} | |
sub loopDelims { | |
my ($fileName, $matchFunc, $iterFunc, $startFunc, $endFunc) = @_; | |
open(my $file, "<", $fileName); | |
my @contexts; | |
my $firstLine = $_ = <$file>; | |
$iterFunc->(\@contexts) if $iterFunc; | |
chompy(\$firstLine); | |
my $configFile = 0; | |
if ($firstLine eq "# Configuration file") { | |
$configFile = 1; | |
} | |
while (<$file>) { | |
if ($configFile and not /^[[:space:]]*#/ and not /^[[:space:]]*$/) { | |
if (my ($context) = /^[[:space:]]*([^{]+?)[[:space:]]*{/) { | |
push(@contexts, $context); | |
$startFunc->(\@contexts) if $startFunc; | |
} elsif (@contexts) { | |
if (/}/) { | |
pop(@contexts); | |
$endFunc->(\@contexts) if $endFunc; | |
} else { | |
my $type = 0; | |
my $key = ""; | |
my $valueRef; | |
if (/=/) { | |
($key, $$valueRef) = split(/=/); | |
chompy($valueRef); | |
} elsif (/</) { | |
$type = 1; | |
($key) = /(.*)</; | |
my @values; | |
while(my $listItem = <$file>) { | |
$_ .= $listItem; | |
if ($listItem =~ />/) { | |
last; | |
} | |
chompy(\$listItem); | |
push(@values, $listItem); | |
} | |
} | |
chompy(\$key); | |
$matchFunc->(\@contexts, $type, $key, $valueRef); | |
} | |
} | |
} | |
$iterFunc->(\@contexts) if $iterFunc; | |
} | |
close($file); | |
} | |
sub wanted { | |
return if -d; | |
my $filename = substr($_, length($modDir) + 1); | |
my $origPath = $origDir."/".$filename; | |
my $modPath = $modDir."/".$filename; | |
if (-f $origPath) { | |
# Get hash of existing IDs | |
my %origIDs = (); | |
loopDelims($origPath, sub { | |
my ($contextsRef, $type, $key, $valueRef) = @_; | |
my $index = join("/", @$contextsRef, $key); | |
$origIDs{$index} = $valueRef; | |
}); | |
# Compare and write new IDs | |
open(my $newFile, ">", $modPath.".new"); | |
loopDelims($modPath, sub { | |
my ($contextsRef, $type, $key, $valueRef) = @_; | |
my $index = join("/", @$contextsRef, $key); | |
if ($origIDs{$index}) { | |
my $refType = ref($origIDs{$index}); | |
if ($type == 0) { | |
my $newValue = ""; | |
if ($refType eq "SCALAR") { | |
$newValue = ${$origIDs{$index}}; | |
} else { | |
print("? ".$filename.": ".$index."\n"); | |
} | |
s/$key=$$valueRef/$key=${$origIDs{$index}}/; | |
} elsif ($type == 1) { | |
my ($indent1, $indent2) = /^(\s*).*\n(\s*)/; | |
my @newValues = (); | |
if ($refType eq "ARRAY") { | |
@newValues = @{$origIDs{$index}}; | |
} elsif ($refType eq "SCALAR") { | |
if (${$origIDs{$index}}) { | |
@newValues = ( ${$origIDs{$index}} ); | |
} | |
} else { | |
print("? ".$filename.": ".$index."\n"); | |
} | |
$_ = $indent1.join("\n".$indent2, $key." <", @newValues)."\n".$indent1." >\n"; | |
} | |
} else { | |
print("+ ".$filename.": ".$index."\n"); | |
} | |
}, sub { | |
print $newFile $_; | |
}); | |
close($newFile); | |
move($modPath.".new", $modPath); | |
} else { | |
print("+++ ".$filename."\n"); | |
} | |
} | |
find({ wanted => \&wanted, no_chdir => 1 }, $modDir); | |
__END__ | |
=head1 NAME | |
Minecraft Forge ID Copier | |
=head1 SYNOPSIS | |
id-copy.pl --orig=<dir> --mod=<dir> | |
=head1 OPTIONS | |
=over 8 | |
=item B<--help> | |
Print a brief help message and exit | |
=item B<--orig=<dir>> | |
Path to the source config directory | |
=item B<--mod=<dir>> | |
Path to the destination config directory | |
=back | |
=head1 DESCRIPTION | |
B<This program> copies IDs from one Minecraft config directory to another, | |
useful when mods are updated to new versions. Unhandled IDs are printed to | |
standard output, as are files that did not exist previously. | |
=head1 EXAMPLE | |
id-copy.pl --orig $HOME/.technic/modpacks/bigdig/config --mod $HOME/.technic/modpacks/bigdig-update/config | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment