Henrybk

profileChanger plugin v1

Feb 21st, 2016
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.43 KB | None | 0 0
  1. ############################################################
  2. #
  3. # profileChanger
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18. #
  19. #
  20. # Plugin made by Henrybk from openkore Brasil
  21. #
  22. # What id does: Enables the change of profiles during execution
  23. #
  24. # How to use:
  25. # 1 - Create a folder on your control folder and name it 'profiles'.
  26. # 2 - Inside the profile folder create your profiles, one folder for each.
  27. # 3 - Create also inside this folder a text file named profileConf.txt.
  28. # 4 - Open this file and add a line 'startingProfile profile'
  29. # 5 - Change the 'profile' in the line to whichever profile you want to be loaded when openkore starts.
  30. # 6 - Save and close the file.
  31. # 7 - End.
  32. #
  33. # How it works: When openkore starts it will load all the config files it finds inside the profile folder you provided, the rest will be loaded from the original control folder.
  34. #
  35. # Commands:
  36. # changeProfile 'profilename' => changes the current profile to the one provided, unloads the current files inside the old profile folder and load new ones from the new profile.
  37. # changeStartingProfile 'profilename' => changes the profile name inside profileConf.txt to whichever profile name you provided, next time kore starts it will use this profile to load.
  38. #
  39. # By Henrybk
  40. #
  41. ############################################################
  42. package profileChanger;
  43.  
  44. use strict;
  45. use File::Spec;
  46. use Plugins;
  47. use Globals qw($interface $quit);
  48. use Log qw(debug message warning error);
  49. use Settings;
  50. use Misc;
  51.  
  52. Plugins::register('profileChanger', 'profileChanger', \&Unload);
  53.  
  54. my $hooks = Plugins::addHooks(
  55.       ['start', \&onStart]
  56.    );
  57.  
  58. my $chooks = Commands::register(
  59.     ['changeProfile', "Changes profile", \&commandHandler],
  60.     ['changeStartingProfile', "Changes the starting profile", \&changeStartingProfile]
  61. );
  62.  
  63. my $baseControlFolder;
  64. my $pluginFolderInControl;
  65. my $pluginConfigFile;
  66.  
  67. my $startingProfile;
  68.  
  69. my %profileList;
  70.  
  71. my $currentProfile;
  72.  
  73. # onUnload
  74. sub Unload {
  75.     Plugins::delHooks($hooks);
  76.     Commands::unregister($chooks);
  77. }
  78.  
  79. sub onStart {
  80.     $baseControlFolder = $Settings::controlFolders[0];
  81.     $pluginFolderInControl = File::Spec->catdir($baseControlFolder, 'profiles');
  82.     $pluginConfigFile = File::Spec->catfile($pluginFolderInControl, 'profileConf.txt');
  83.    
  84.     open my $conf, "<:utf8", $pluginConfigFile;
  85.     while (<$conf>) {
  86.         $. == 1 && s/^\x{FEFF}//;
  87.             s/(.*)[\s\t]+#.*$/$1/;
  88.             s/^\s*#.*$//;
  89.             s/^\s*//;
  90.             s/\s*[\r\n]?$//g;
  91.             s/  +/ /g;
  92.             next unless ($_);
  93.             if (/^startingProfile\s+(\w+)$/i) {
  94.                 $startingProfile = $1;
  95.             }
  96.     }
  97.     close($conf);
  98.    
  99.     unless (defined $startingProfile) {
  100.          $quit = 1;
  101.          return;
  102.     }
  103.    
  104.     opendir my $d, $pluginFolderInControl;
  105.     my @fileList = readdir($d);
  106.     closedir $d;
  107.    
  108.     foreach (@fileList) {
  109.         next unless -d File::Spec->catdir($pluginFolderInControl, $_);
  110.         next if ($_ =~ /^\./);
  111.         $profileList{$_} = 1;
  112.     }
  113.    
  114.     unless (exists $profileList{$startingProfile}) {
  115.         $quit = 1;
  116.         return;
  117.     }
  118.    
  119.     loadProfile($startingProfile, 1);
  120. }
  121.  
  122. sub changeStartingProfile {
  123.     my (undef, $profile) = @_;
  124.     if (exists $profileList{$profile}) {
  125.         open my $conf, ">:utf8", $pluginConfigFile;
  126.         print $conf "startingProfile ".$profile."";
  127.         close($conf);
  128.         message "[PC] Starting profile changed from '".$startingProfile."' to '".$profile."' \n", "system";
  129.         $startingProfile = $profile;
  130.     } else {
  131.         error "[PC] The profile you provided is not valid\n";
  132.     }
  133. }
  134.  
  135. sub commandHandler {
  136.     if ($_[1] && exists $profileList{$_[1]}) {
  137.         loadProfile($_[1], 0);
  138.     } else {
  139.         error "[PC] The profile you provided is not valid\n";
  140.     }
  141. }
  142.  
  143. sub loadProfile {
  144.     my ($newProfile, $load) = @_;
  145.     my $newProfileFolder = File::Spec->catdir($pluginFolderInControl, $newProfile);
  146.     message "[PC] Preparing to load new profile '".$newProfile."' at '".$newProfileFolder."' \n", "system";
  147.     if ($load) {
  148.         unshift @Settings::controlFolders, $newProfileFolder;
  149.     } else {
  150.         my $oldProfile = $currentProfile;
  151.         my %reloadFiles;
  152.         message "[PC] Looking for loaded files in old profile '".$oldProfile."' to unload \n";
  153.         foreach my $file (@{$Settings::files->getItems}) {
  154.             next if ($file->{'type'} != 0);
  155.             my $filepath;
  156.             if ($file->{'autoSearch'} == 1) {
  157.                 $filepath = Settings::_findFileFromFolders($file->{'name'}, \@Settings::controlFolders);
  158.             } else {
  159.                 $filepath = $file->{'name'};
  160.             }
  161.             my (undef,$directories,$filename) = File::Spec->splitpath($filepath);
  162.             my @dirs = File::Spec->splitdir($directories);
  163.            
  164.             if ($dirs[-2] eq $oldProfile) {
  165.                 message "[PC] Unloading '".$filename."' from '".$oldProfile."'\n", "system";
  166.                 $reloadFiles{$file->{'index'}} = $filename;
  167.             }
  168.         }
  169.        
  170.         opendir my $d, $newProfileFolder;
  171.         my @newProfileFiles = readdir($d);
  172.         closedir $d;
  173.        
  174.         message "[PC] Looking for files in new profile '".$newProfile."'\n";
  175.         foreach my $filename (@newProfileFiles) {
  176.             next unless -f File::Spec->catdir($newProfileFolder, $filename);
  177.             next if ($filename =~ /^\./);
  178.             foreach my $file (@{$Settings::files->getItems}) {
  179.                 next if ($file->{'type'} != 0);
  180.                 next if (exists $reloadFiles{$file->{'index'}});
  181.                 if ($file->{'autoSearch'} == 1) {
  182.                     if ($file->{'name'} eq $filename) {
  183.                         $reloadFiles{$file->{'index'}} = $filename;
  184.                         message "[PC] Unloading '".$filename."' from '".$baseControlFolder."'\n", "system";
  185.                     }
  186.                 } else {
  187.                     my (undef,undef,$testFileName) = File::Spec->splitpath($file->{'name'});
  188.                     if ($testFileName eq $filename) {
  189.                         $reloadFiles{$file->{'index'}} = $filename;
  190.                         message "[PC] Unloading '".$filename."' from '".$baseControlFolder."'\n", "system";
  191.                     }
  192.                 }
  193.             }
  194.         }
  195.        
  196.         shift @Settings::controlFolders;
  197.         unshift @Settings::controlFolders, $newProfileFolder;
  198.        
  199.         message "[PC] Loading necessary files\n";
  200.         foreach my $relodingFileIndex (keys %reloadFiles) {
  201.             my $reloadingFile = $Settings::files->get($relodingFileIndex);
  202.             my $filename = $reloadFiles{$relodingFileIndex};
  203.             my $newFilePath = Settings::_findFileFromFolders($filename, \@Settings::controlFolders);
  204.             if ($reloadingFile->{'autoSearch'} == 0) {
  205.                 $reloadingFile->{'name'} = $newFilePath;
  206.             }
  207.            
  208.             my (undef,$directories,undef) = File::Spec->splitpath($newFilePath);
  209.             my @dirs = File::Spec->splitdir($directories);
  210.            
  211.             if ($dirs[-2] eq $newProfile) {
  212.                 message "[PC] Loading '".$filename."' from '".$newProfile."'\n", "system";
  213.             } else {
  214.                 message "[PC] Loading '".$filename."' from '".$baseControlFolder."'\n", "system";
  215.             }
  216.            
  217.             if (ref($reloadingFile->{loader}) eq 'ARRAY') {
  218.                 my @array = @{$reloadingFile->{loader}};
  219.                 my $loader = shift @array;
  220.                 $loader->($newFilePath, @array);
  221.             } else {
  222.                 $reloadingFile->{loader}->($newFilePath);
  223.             }
  224.            
  225.         }
  226.         message "[PC] Loading ended, enjoy.\n", "system";
  227.     }
  228.     $currentProfile = $newProfile;
  229. }
  230.  
  231. return 1;
Advertisement
Add Comment
Please, Sign In to add comment