Advertisement
Henrybk

JSONPortals v3

Mar 4th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.86 KB | None | 0 0
  1. package JSONPortals;
  2.  
  3. use strict;
  4. use Globals;
  5. use Utils;
  6. use Misc;
  7. use Plugins;
  8. use Log qw(debug message warning error);
  9. use FileParsers;
  10. use File::Spec;
  11. use JSON;
  12. my $plugin_folder = $Plugins::current_plugin_folder;
  13. my $portals_folder = File::Spec->catdir($plugin_folder, 'JSONPortals');
  14. my $portals_filepath = File::Spec->catfile($portals_folder, 'portals.txt');
  15.  
  16. Plugins::register('JSONPortals', 'Changes the reading method for portals.txt to support JSON');
  17.  
  18. my $hooks = Plugins::addHooks(
  19.     ['pre_load_portals.txt', \&changePortalsFileLocation],
  20.     ['load_portals.txt', \&parsePortalsJSON],
  21.     ['updatePortalLUT', \&updatePortalsJSON],
  22.     ['updatePortalLUT2', \&updatePortalsJSON]
  23. );
  24.  
  25. sub on_unload {
  26.     Plugins::delHooks($hooks);
  27. }
  28.  
  29. sub changePortalsFileLocation {
  30.     my ($self, $pre_load) = @_;
  31.    
  32.     message "[JSONPortals] Changed portals.txt file location to '".$portals_filepath."' for this [re]load\n", "system";
  33.    
  34.     $pre_load->{'filename'} = $portals_filepath;
  35.    
  36.     $pre_load->{'return'} = 1;
  37. }
  38.  
  39. sub parsePortalsJSON {
  40.     my ($self, $plugin_args) = @_;
  41.    
  42.     my $portals_hash_ref = @{$plugin_args->{'args'}}[0];
  43.    
  44.     undef %{$portals_hash_ref};
  45.  
  46.     message "[JSONPortals] Loadind portals file\n", "system";
  47.    
  48.     open FILE, "<:utf8", $plugin_args->{'filename'};
  49.     my @lines = <FILE>;
  50.     close(FILE);
  51.     chomp @lines;
  52.     my $jsonPortalsString = join('',@lines);
  53.    
  54.     my @portals = @{from_json($jsonPortalsString, { utf8  => 1 } )};
  55.    
  56.     foreach my $portal (@portals) {
  57.         my $source = $portal->{'source'}{'map'}." ".$portal->{'source'}{'x'}." ".$portal->{'source'}{'y'};
  58.         my $dest = $portal->{'destination'}{'map'}." ".$portal->{'destination'}{'x'}." ".$portal->{'destination'}{'y'};
  59.        
  60.         $portals_hash_ref->{$source}{'source'}{'map'} = $portal->{'source'}{'map'};
  61.         $portals_hash_ref->{$source}{'source'}{'x'} = $portal->{'source'}{'x'};
  62.         $portals_hash_ref->{$source}{'source'}{'y'} = $portal->{'source'}{'y'};
  63.         $portals_hash_ref->{$source}{'dest'}{$dest}{'map'} = $portal->{'destination'}{'map'};
  64.         $portals_hash_ref->{$source}{'dest'}{$dest}{'x'} = $portal->{'destination'}{'x'};
  65.         $portals_hash_ref->{$source}{'dest'}{$dest}{'y'} = $portal->{'destination'}{'y'};
  66.         $portals_hash_ref->{$source}{'dest'}{$dest}{'enabled'} = 1; # is available permanently (can be used when calculating a route)
  67.         #$portals_hash_ref->{$source}{'dest'}{$dest}{'active'} = 1; # TODO: is available right now (otherwise, wait until it becomes available)
  68.         if (exists $portal->{'cost'}) {
  69.             $portals_hash_ref->{$source}{'dest'}{$dest}{'cost'} = $portal->{'cost'};
  70.         }
  71.         if (exists $portal->{'allow_ticket'}) {
  72.             $portals_hash_ref->{$source}{'dest'}{$dest}{'allow_ticket'} = $portal->{'allow_ticket'};
  73.         }
  74.         if (exists $portal->{'steps'}) {
  75.             $portals_hash_ref->{$source}{'dest'}{$dest}{'steps'} = $portal->{'steps'};
  76.         } else {
  77.             $portals_hash_ref->{$source}{'dest'}{$dest}{'steps'} = "";
  78.         }
  79.     }
  80.     $plugin_args->{'return'} = 1;
  81. }
  82.  
  83. sub updatePortalsJSON {
  84.     my ($self, $plugin_args) = @_;
  85.    
  86.     my %portal;
  87.     $portal{'source'}{'map'} = $plugin_args->{'sourceMap'};
  88.     $portal{'source'}{'x'} = $plugin_args->{'sourceX'};
  89.     $portal{'source'}{'y'} = $plugin_args->{'sourceY'};
  90.     $portal{'destination'}{'map'} = $plugin_args->{'destMap'};
  91.     $portal{'destination'}{'x'} = $plugin_args->{'destX'};
  92.     $portal{'destination'}{'y'} = $plugin_args->{'destY'};
  93.     if (exists $plugin_args->{'steps'}) {
  94.         $portal{'steps'} = $plugin_args->{'steps'};
  95.     }
  96.    
  97.     message "[JSONPortals] Updating portals file\n", "system";
  98.  
  99.     open FILE, "<:utf8", $portals_filepath;
  100.     my @lines = <FILE>;
  101.     close(FILE);
  102.     chomp @lines;
  103.     my $jsonPortalsString = join('',@lines);
  104.     my @portals = @{from_json($jsonPortalsString, { utf8  => 1 } )};
  105.  
  106.     push @portals, \%portal;
  107.  
  108.     open REWRITE, ">:utf8", $portals_filepath;
  109.     print REWRITE to_json(\@portals, {utf8 => 1, pretty => 1, canonical => 1});
  110.     close(REWRITE);
  111.     $plugin_args->{'return'} = 1;
  112. }
  113.  
  114. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement