Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package JSONPortals;
- use strict;
- use Globals;
- use Utils;
- use Misc;
- use Plugins;
- use Log qw(debug message warning error);
- use FileParsers;
- use File::Spec;
- use JSON;
- my $plugin_folder = $Plugins::current_plugin_folder;
- my $portals_folder = File::Spec->catdir($plugin_folder, 'JSONPortals');
- my $portals_filepath = File::Spec->catfile($portals_folder, 'portals.txt');
- Plugins::register('JSONPortals', 'Changes the reading method for portals.txt to support JSON');
- my $hooks = Plugins::addHooks(
- ['pre_load_portals.txt', \&changePortalsFileLocation],
- ['load_portals.txt', \&parsePortalsJSON],
- ['updatePortalLUT', \&updatePortalsJSON],
- ['updatePortalLUT2', \&updatePortalsJSON]
- );
- sub on_unload {
- Plugins::delHooks($hooks);
- }
- sub changePortalsFileLocation {
- my ($self, $pre_load) = @_;
- message "[JSONPortals] Changed portals.txt file location to '".$portals_filepath."' for this [re]load\n", "system";
- $pre_load->{'filename'} = $portals_filepath;
- $pre_load->{'return'} = 1;
- }
- sub parsePortalsJSON {
- my ($self, $plugin_args) = @_;
- my $portals_hash_ref = @{$plugin_args->{'args'}}[0];
- undef %{$portals_hash_ref};
- message "[JSONPortals] Loadind portals file\n", "system";
- open FILE, "<:utf8", $plugin_args->{'filename'};
- my @lines = <FILE>;
- close(FILE);
- chomp @lines;
- my $jsonPortalsString = join('',@lines);
- my @portals = @{from_json($jsonPortalsString, { utf8 => 1 } )};
- foreach my $portal (@portals) {
- my $source = $portal->{'source'}{'map'}." ".$portal->{'source'}{'x'}." ".$portal->{'source'}{'y'};
- my $dest = $portal->{'destination'}{'map'}." ".$portal->{'destination'}{'x'}." ".$portal->{'destination'}{'y'};
- $portals_hash_ref->{$source}{'source'}{'map'} = $portal->{'source'}{'map'};
- $portals_hash_ref->{$source}{'source'}{'x'} = $portal->{'source'}{'x'};
- $portals_hash_ref->{$source}{'source'}{'y'} = $portal->{'source'}{'y'};
- $portals_hash_ref->{$source}{'dest'}{$dest}{'map'} = $portal->{'destination'}{'map'};
- $portals_hash_ref->{$source}{'dest'}{$dest}{'x'} = $portal->{'destination'}{'x'};
- $portals_hash_ref->{$source}{'dest'}{$dest}{'y'} = $portal->{'destination'}{'y'};
- $portals_hash_ref->{$source}{'dest'}{$dest}{'enabled'} = 1; # is available permanently (can be used when calculating a route)
- #$portals_hash_ref->{$source}{'dest'}{$dest}{'active'} = 1; # TODO: is available right now (otherwise, wait until it becomes available)
- if (exists $portal->{'cost'}) {
- $portals_hash_ref->{$source}{'dest'}{$dest}{'cost'} = $portal->{'cost'};
- }
- if (exists $portal->{'allow_ticket'}) {
- $portals_hash_ref->{$source}{'dest'}{$dest}{'allow_ticket'} = $portal->{'allow_ticket'};
- }
- if (exists $portal->{'steps'}) {
- $portals_hash_ref->{$source}{'dest'}{$dest}{'steps'} = $portal->{'steps'};
- } else {
- $portals_hash_ref->{$source}{'dest'}{$dest}{'steps'} = "";
- }
- }
- $plugin_args->{'return'} = 1;
- }
- sub updatePortalsJSON {
- my ($self, $plugin_args) = @_;
- my %portal;
- $portal{'source'}{'map'} = $plugin_args->{'sourceMap'};
- $portal{'source'}{'x'} = $plugin_args->{'sourceX'};
- $portal{'source'}{'y'} = $plugin_args->{'sourceY'};
- $portal{'destination'}{'map'} = $plugin_args->{'destMap'};
- $portal{'destination'}{'x'} = $plugin_args->{'destX'};
- $portal{'destination'}{'y'} = $plugin_args->{'destY'};
- if (exists $plugin_args->{'steps'}) {
- $portal{'steps'} = $plugin_args->{'steps'};
- }
- message "[JSONPortals] Updating portals file\n", "system";
- open FILE, "<:utf8", $portals_filepath;
- my @lines = <FILE>;
- close(FILE);
- chomp @lines;
- my $jsonPortalsString = join('',@lines);
- my @portals = @{from_json($jsonPortalsString, { utf8 => 1 } )};
- push @portals, \%portal;
- open REWRITE, ">:utf8", $portals_filepath;
- print REWRITE to_json(\@portals, {utf8 => 1, pretty => 1, canonical => 1});
- close(REWRITE);
- $plugin_args->{'return'} = 1;
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement