Skip to content

Instantly share code, notes, and snippets.

@mrudat
Last active May 1, 2019 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrudat/ba7bfe8e9df398eaa148208dceddbfc9 to your computer and use it in GitHub Desktop.
Save mrudat/ba7bfe8e9df398eaa148208dceddbfc9 to your computer and use it in GitHub Desktop.
Does a simple search-and-replace to change some Factorio 0.16 mods to support Factorio 0.17
#!/usr/bin/perl
use warnings;
use strict;
use English qw(:no_match_vars);
use JSON;
use File::Spec;
use File::Path qw(make_path);
use File::Find;
use File::Copy;
use Data::Dumper;
use autodie qw(:default copy);
our ($JSON) = JSON->new;
sub slurp {
my ($filename) = @_;
local $INPUT_RECORD_SEPARATOR;
open my $fh, "<", $filename;
return <$fh>
}
sub splat {
my ($filename, $data) = @_;
open my $fh, ">", $filename;
$fh->print($data);
close $fh;
}
our ($info) = $JSON->decode(slurp("info.json"));
our ($target);
our ($oldmodname, $newmodname);
sub upgrade_to_version {
my ($version, $patchlevel) = @_;
$info->{factorio_version} = $version;
foreach my $dependency (@{$info->{dependencies}}) {
if ($dependency =~ m/^base/) {
$dependency = "base >= $version.$patchlevel"
}
}
$oldmodname = $info->{'name'};
$newmodname = $oldmodname . "-" . $version;
$newmodname =~ s/[^A-Za-z0-9-_]/_/g;
$info->{'name'} = $newmodname;
$info->{'homepage'} = "https://mods.factorio.com/mod/$oldmodname";
my $title = $info->{'title'};
my $author = $info->{'author'};
$info->{'title'} = $title . " by " . $author . " updated for $version";
my $moddir = $newmodname . "_" . $info->{'version'};
$target = File::Spec->catpath(File::Spec->curdir(), File::Spec->updir(), $moddir);
make_path($target);
splat(File::Spec->catfile($target, "README.md"), <<EOF);
[$title](https://mods.factorio.com/mod/$oldmodname) updated for $version until [$author](https://mods.factorio.com/user/$author) gets a [round tuit](https://en.wiktionary.org/wiki/round_tuit).
Updated using [Perl script to do trivial edits for supporting 0.17](https://forums.factorio.com/viewtopic.php?f=34&t=69307).
EOF
}
sub upgrade_to_point_17 {
upgrade_to_version('0.17', '35');
find(
{
wanted => sub {
if (-d $_) {
make_path(File::Spec->catdir($target, $_));
return
}
if ($_ eq "./info.json" or $_ =~ m{./README.md}i) {
return
}
if ($_ =~ m/.lua$/) {
my $code = slurp($_);
$code =~ s/science-pack-1/automation-science-pack/g;
$code =~ s/science-pack-2/logistic-science-pack/g;
$code =~ s/science-pack-3/chemical-science-pack/g;
$code =~ s/high-tech-science-pack/utility-science-pack/g;
$code =~ s/(["'])goes-to-quickbar\1\s*,?//g;
$code =~ s/(["'])goes-to-main-inventory\1\s*,?//g;
$code =~ s/terciary/tertiary/g;
# not sure if this works in the general case, but '../' causes an error.
$code =~ s{(["'])\.\./}{$1__${newmodname}__/}g;
$code =~ s/__${oldmodname}__/__${newmodname}__/g;
# from https://forums.factorio.com/viewtopic.php?f=34&t=70188
$code =~ s/get_inventory\(\s*defines.inventory.(?:player|god)_main\s*)/get_main_inventory()/g;
$code =~ s/defines.inventory.player/defines.inventory.character/g;
$code =~ s/power-armor-2/power-armor-mk2/g;
$code =~ s/personal-roboport-equipment-2/personal-roboport-mk2-equipment/g;
splat(File::Spec->catfile($target, $_), $code);
} else {
copy($_, File::Spec->catfile($target, $_));
}
},
no_chdir => 1,
},
File::Spec->curdir()
);
splat(File::Spec->catfile($target,'info.json'), $JSON->pretty->encode($info));
}
if ($info->{factorio_version} == '0.16') {
upgrade_to_point_17();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment