Advertisement
OldFarmerJoe

Minecraft Fuzzie Mod Version Compare v4

Dec 15th, 2013
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 12.89 KB | None | 0 0
  1. #!C:Perl\bin\perl.exe
  2.  
  3. # Version: 4 - Dec 1st, 2013
  4. # Version: 3 - Oct 30th, 2013
  5. # Version: 2 - Oct 6th, 2013
  6. #
  7. # Reads Fuzzie's html post to find the current version of mods
  8. # Generates a tab separated file that can be loaded into a spreadsheet program
  9. # Will also read a old version of its output file to compare to
  10. # Will also try to determine the version from the mod files in mods directory.
  11. # Marks New column New, if it is different then the old version number
  12. # Marks Match column Yes, if the file version matches the mod version
  13. # Requires Perl to be installed. Top line of this script must be path to your Perl executable.
  14. #
  15. # Steps to use:
  16. # 1. Save Fuzzie's 1st post as an html file, set $htmlFile to the html file name
  17. # 2. Edit $oldFile line, set it to old file, if you have one
  18. # 3. Set $modDir to your mod dir, maybe expanded %APPDATA%/.minecraft/mods
  19. # 4. Run the program
  20. # 5. Add $trans variables for files that don't match, already done  
  21. # 6. Load the results in a spreadsheet program
  22. # 7. Update your mods marked with as New in the spreadsheet or log
  23.  
  24. use strict;
  25. use warnings;
  26. use File::Path;
  27. use FileHandle;
  28. use Data::Dumper;
  29. my %trans;
  30.  
  31. #########################################
  32. #### User edit this section
  33. my $oldFile  = "modlist_2013.11.06_21.57.39.txt";
  34. my $htmlFile = "Fuzzzie's 1.6.4 Mod List.htm";
  35. my $modDir   = "C:/Users/Bruce/AppData/Roaming/.minecraft/mods";  # more typical modDir
  36. #my $modDir   = "C:/Users/Bruce/Minecraft/Minecraft_1.6.4_ver_3/data/.minecraft/mods";
  37. #my $modDir   = "C:/Users/Bruce/Downloads";
  38.  
  39. # Some mod names don't match well to their file names
  40. #$trans{"Html Name"} = "File Name";
  41. $trans{"Energy Manipulation"} = "EM";
  42. $trans{"Better World Generation 4"} = "BWG4";
  43. $trans{"Nuclear Control"} = "IC2NuclearControl";
  44. $trans{"Extra Utilities"} = "extrautils";
  45. $trans{"Rei's Minimap"} = "ReiMinimap";
  46. $trans{"Barrels Mod"} = "Barrels";
  47. $trans{"Extra Bees/Trees"} = "binnie-mods";
  48. $trans{"Carpenter Blocks"} = "Carpenter's Blocks";
  49. $trans{"Mo Creatures"} = "DrZharks MoCreatures Mod";
  50. $trans{"Forestry"} = "forestry-A";
  51. $trans{"NBTEdit"} = "Forge_NBTEdit";
  52. $trans{"Iron Chests 2"} = "ironchest-universal";
  53. $trans{"NEI Mystcraft Plugin"} = "MystNEI-Plugin";
  54. $trans{"Wireless Redstone CBE"} = "WR-CBE";
  55. $trans{"Zan's Minimap"} = "ZansMinimap";
  56. $trans{"TreeCapitator"} = "TreeCapitator.Forge";
  57. $trans{"MrCrayfish's Furniture"} = "FurnitureMod";
  58. $trans{"BiblioWoods BoP"} = "BiblioWoods[BiomesOPlenty]";
  59. $trans{"Buildcraft"} = "buildcraft-A";
  60. $trans{"Additional Buildcraft Objects"} = "buildcraft-Z-additional-buildcraft-objects";
  61. $trans{"NEI Plugin Pack"} = "NEIPlugins";
  62. $trans{"Immibis's Peripheral"} = "immibis-peripherals";
  63. $trans{"Tinkers Construct"} = "TConstruct";
  64. $trans{"MFFS Classic"} = "MFFS";
  65. $trans{"IndustrialCraft 2 Experimental"} = "industrialcraft-2_";
  66. $trans{"RotartyCraft"} = "RotaryCraft";
  67. $trans{"Applied Energistics"} = "appeng";
  68. $trans{"Applied Energistics Dev"} = "appeng";
  69. $trans{"Universal Electricity Basic Components"} = "Basic_Components";
  70. $trans{"Darkguardsman Mods"} = "Dark-AssemblyLine";
  71. $trans{"Gravitation Suite"} = "mod_zGraviSuite";
  72. $trans{"GregTech Addon"} = "gregtechmod";
  73. $trans{"Advanced Solar Panels"} = "mod_AdvancedSolarPanels";
  74. $trans{"Project: Red"} = "ProjectRedBase";
  75. $trans{"Metallurgy 3"} = "Metallurgy";
  76. $trans{"Steve's Carts 2"} = "Steve's Carts";
  77. $trans{"Thaumcraft 4"} = "Thaumcraft";
  78. $trans{"Minechem 4"} = "Minechem";
  79.  
  80. #########################################
  81. my $debug = 0;
  82. my $data = {};
  83. my $date = GetDateTime();
  84. my $outFile  = "modlist_$date.txt";
  85. my $logFile = "minecraft_fuz_mod_ver.log";
  86. my $LOG = OpenOutRef($logFile);
  87. print "Creating $logFile\n";
  88.  
  89. ReadOldFile($oldFile,$data);
  90. ReadHtmlFile($htmlFile,$data);
  91. ReadModDir($modDir,$data);
  92. Compare($data);
  93. WriteData($outFile,$data);
  94.  
  95. #print $LOG "-----------------------------------\n";
  96. #print $LOG Dumper($data);
  97. print $LOG "-----------------------------------\n";
  98. print $LOG "old count = $data->{oldCnt}\n";
  99. print $LOG "new count = $data->{newCnt}\n";
  100. print $LOG "diff count = $data->{difCnt}\n";
  101. print $LOG "file count = $data->{fileCnt}\n";
  102. print $LOG "-----------------------------------\n";
  103.  
  104. close($LOG);
  105. exit 0;
  106.  
  107. #############################################################################
  108. sub ReadModDir {
  109.   my ($modDir, $data) = @_;
  110.   if ( -d $modDir ) {
  111.     print $LOG "Reading dir : $modDir\n";
  112.     my @files = ReadDir($modDir);
  113.     foreach my $file ( @files ) {
  114.       if ( $file =~ /(.+)\.(zip|jar)$/ ) {
  115.         print $LOG "File : $file\n" if ($debug==3);
  116.         $data->{fileCnt}++;
  117.         my $fileNameVar1 = $1;
  118.         my $fileNameVar2 = $1;
  119.         $fileNameVar1 =~ s/^\[[\w\.]+\]//; # remove starting minecraft version
  120.         $fileNameVar1 =~ s/^[\d\.]+//; # remove starting minecraft version
  121.         $fileNameVar1 =~ s/[-_\.\[\]\s\']+//g;
  122.     #print $LOG "  file changed : $fileNameVar1\n";
  123.         my $gotMatch = 0;
  124.         foreach my $mod ( @{$data->{mod}} ) {
  125.           next if ( $mod->{done} );
  126.           my $modName = $mod->{name};
  127.       #print $LOG "  modname = $modName\n";
  128.           if ( defined($trans{$modName}) ) {
  129.             $modName = $trans{$modName};
  130.         #print $LOG "  changed modName = $modName\n";
  131.           }
  132.           $modName =~ s/[-_\.\[\]\s\']+//g;
  133.  
  134.           if ( $fileNameVar1 =~ /^$modName/i ) {
  135.             $gotMatch = 1;
  136.             $mod->{done} = 1;
  137.             print $LOG "  found match $modName\n" if ($debug==3);
  138.             my @letterMod = split(//,$modName);
  139.             my @letterFile = split(//,$fileNameVar2);
  140.             my $j = 0;
  141.             my $fileModName;
  142.             for(my $i=0; $i <= $#letterMod; $i++) {
  143.               while ( $letterFile[$j] !~ /$letterMod[$i]/i ) {
  144.                 $fileModName .= $letterFile[$j];
  145.         #print $LOG "  $i $j N $letterMod[$i] fileModName = $fileModName\n";
  146.                 $j++
  147.               }
  148.               if ( $letterFile[$j] =~ /$letterMod[$i]/i ) {
  149.                 $fileModName .= $letterFile[$j];
  150.         #print $LOG "  $i $j Y $letterMod[$i] fileModName = $fileModName\n";
  151.                 $j++
  152.               }
  153.             }
  154.         $fileModName =~ s/\[/\\[/g;
  155.             if ( $fileNameVar2 =~ /$fileModName(.+)/ ) {
  156.               my $v = $1;
  157.           $v =~ s/^[-_\s]+//;
  158.           $v =~ s/[\[\]]+//g;
  159.               my $name = $mod->{name};
  160.               $data->{fileVer}{$name} = $v;
  161.           print $LOG "  fileVer = $v\n" if ($debug==3);
  162.             }
  163.           }
  164.       last if ($gotMatch);
  165.         }
  166.         if ( !$gotMatch ) {
  167.           if ( $fileNameVar2 =~ /(.+)(\d+\..+)/ ) {
  168.             my($m,$v) = ($1,$2);
  169.         $v =~ s/^[-_\s]+//;
  170.         $v =~ s/[\[\]]+//g;
  171.         $m =~ s/_v$//;
  172.         $m =~ s/\bv//;
  173.         $m =~ s/(\[|\.)$//;
  174.             my $mod = {};
  175.         $mod->{name} = $m;
  176.             $data->{fileVer}{$m} = $v;
  177.             $mod->{version} = "unknown";
  178.             $mod->{new} = "";
  179.             $mod->{credit} = "";
  180.             $mod->{download} = "";
  181.             $mod->{thread} = "";
  182.             $mod->{done} = 1;
  183.             push(@{$data->{mod}}, $mod);
  184.           }
  185.         }
  186.       }
  187.     }
  188.     #print $LOG "-----------------------------------\n";
  189.   }
  190.   else {
  191.     print $LOG "Error can't find dir : $modDir\n";
  192.     exit 1;
  193.   }
  194. }
  195.  
  196. #############################################################################
  197. # Read the old file
  198. sub ReadOldFile {
  199.   my ($oldFile, $data) = @_;
  200.   my $old;
  201.   $data->{oldCnt} = 0;;
  202.   if ( -e $oldFile ) {
  203.     my $OLD = OpenInRef($oldFile);
  204.     print $LOG "Reading $oldFile\n";
  205.     my @line = <$OLD>;
  206.     close($OLD);
  207.     foreach my $line ( @line ) {
  208.       next if ( $line =~ /^#/ );
  209.       my ($order,$name,$oldVer,$new,$oldFileVer,$match,$credit,$download,$thread) = split(/\t/,$line);
  210.       $data->{old}{$name} = $oldVer;
  211.       $data->{oldCnt}++;
  212.     }
  213.     print $LOG "-----------------------------------\n";
  214.   }
  215.   else {
  216.     print $LOG "Warning unable to find oldFile = $oldFile\n";
  217.   }
  218. }
  219.  
  220. #############################################################################
  221. # Read the html file
  222. sub ReadHtmlFile {
  223.   my($htmlFile,$data) = @_;
  224.   if ( !-e $htmlFile ) {
  225.     print $LOG "Error can't find html file : $htmlFile\n";
  226.     exit 1;
  227.   }
  228.   $data->{newCnt} = 0;
  229.   my($name,$version,$credit,$download,$thread);
  230.   my $i = 0;
  231.   my $INFILE = OpenInRef($htmlFile);
  232.   print $LOG "Reading file : $htmlFile\n";
  233.   my @line = <$INFILE>;
  234.   close($INFILE);
  235.    
  236.   foreach my $line ( @line ) {
  237.     chomp($line);
  238.     #print $LOG "line $i : $line\n";
  239.      # <font color="#0000CD"><div style="text-align: left;">CodeChickenCore</div>
  240.     if ( $i==0 && $line =~ /color=\"\#(0000CD|FF0000)\"><div style=\"text-align: left;\">(.+)<\/div>/ ) {
  241.       $name = $2;
  242.       print $LOG "\nmod=$name\n" if ($debug==2);
  243.       ($version,$credit,$download,$thread) = ("","","","");
  244.       $i=1;
  245.     }
  246.     elsif ( $i==1 && $line =~ /<td class=\"cms_table_grid_td\"><div style=\"text-align: center;\">(.+)<\/div>/ ) {
  247.       $version = $1;
  248.       print $LOG "version=$version\n" if ($debug==2);
  249.       $i=2;
  250.     }
  251.     elsif ( $i==2 && $line =~ /<font color=\"#FF8C00\">(.+)<\/font>/ ) {
  252.       $credit = $1;
  253.       $credit =~ s/<\/div>//;
  254.       $credit =~ s/<div.+>//;
  255.       print $LOG "credit=$credit\n" if ($debug==2);
  256.       $i=3;
  257.     }
  258.     elsif ( $i==3 && $line =~ /a href=\"(\S+)\" target=\"_blank\">Download/ ) {
  259.       $download = $1;
  260.       print $LOG "download=$download\n" if ($debug==2);
  261.       $i=4;
  262.     }
  263.     elsif ( $i==4 && $line =~ /a href=\"(\S+)\" target=\"_blank\">(Thread|Website)/ ) {
  264.       $thread = $1;
  265.       print $LOG "thread=$thread\n" if ($debug==2);
  266.       $i=5;
  267.     }
  268.     elsif ( $i > 1 && $line =~ /<\/tr>/ ) {
  269.       my $mod = {};
  270.       $mod->{name} = $name;
  271.       $mod->{version} = $version;
  272.       $mod->{new} = "";
  273.       $mod->{credit} = $credit;
  274.       $mod->{download} = $download;
  275.       $mod->{thread} = $thread;
  276.       $mod->{done} = 0;
  277.       $data->{fileVer}{$name} = "";
  278.       $data->{newCnt}++;
  279.       push(@{$data->{mod}}, $mod);
  280.       $i=0;
  281.     }
  282.     elsif ( $line =~ /<\/td>/ && $i==1) {
  283.       $version = "unknown";
  284.       $i=2;
  285.     }
  286.     elsif ( $line =~ /<\/table>/ ) {
  287.       $data->{div}{$name} = 1;
  288.     }
  289.   }
  290.   print $LOG "-----------------------------------\n" if ($debug==2);
  291. }
  292.  
  293. #############################################################################
  294. sub Compare {
  295.   my ($data) = @_;
  296.   $data->{difCnt} = 0;
  297.   if ( $data->{oldCnt} ) {
  298.     print $LOG "Comparing old to new\n";
  299.     foreach my $mod ( @{$data->{mod}} ) {
  300.       my $name = $mod->{name};
  301.       my $version = $mod->{version};
  302.       if ( defined($data->{old}{name}) ) {
  303.         if ( $data->{old}{$name} ne $version ) {
  304.           print $LOG "  $name - new version\n";
  305.           $mod->{new} = "New";
  306.           $data->{difCnt}++;
  307.         }
  308.       }
  309.       else {
  310.         $mod->{new} = "New";
  311.         $data->{difCnt}++;
  312.       }
  313.     }
  314.     print $LOG "-----------------------------------\n";
  315.   }
  316. }
  317.  
  318. #############################################################################
  319. # Write the output file
  320. sub WriteData {
  321.   my ($outFile,$data) = @_;
  322.   my $j = 0;
  323.   my $OUTFILE = OpenOutRef($outFile);
  324.   print $LOG "Creating $outFile\n";
  325.   print $OUTFILE "#Order\tMod\tNew\tVersion\tFile Version\tMatch\tCredit\tDownload\tThread\n";
  326.   foreach my $mod ( @{$data->{mod}} ) {
  327.     $j++;
  328.     my $name = $mod->{name};
  329.     my $fv1 = $mod->{version};
  330.     my $fv = $data->{fileVer}{$name};
  331.     my $fv2 = $fv;
  332.     $fv1 =~ s/[\s\.\-]+//g;
  333.     $fv2 =~ s/[\s\.\-]+//g;
  334.     my $match = "";
  335.     if ( $fv2 =~ /$fv1/ ) {
  336.       $match = "Yes";
  337.     }
  338.     print $OUTFILE "$j\t$name\t$mod->{new}\t$mod->{version}\t$fv\t$match\t$mod->{credit}\t$mod->{download}\t$mod->{thread}\n";
  339.     if ( defined($data->{div}{$name}) ) {
  340.       $j++;
  341.       print $OUTFILE "$j\n";
  342.     }
  343.   }
  344.   close($OUTFILE);
  345. }
  346.  
  347. #############################################################################
  348. sub OpenOutRef {
  349.   my ($outFile) = (@_);
  350.   my $OUTFILE = FileHandle->new;
  351.   if ( $OUTFILE->open("> $outFile") == 0 ) {
  352.     print "Error unable to open out file: $outFile\n";
  353.     exit(1);
  354.   }
  355.   return $OUTFILE;
  356. }
  357.  
  358. sub OpenInRef {
  359.   my ($inFile) = (@_);
  360.   my $INFILE = FileHandle->new;
  361.   if ( $INFILE->open("< $inFile") == 0 ) {
  362.     print $LOG "Error unable to open in file: $inFile\n";
  363.     exit(1);
  364.   }
  365.   return $INFILE;
  366. }
  367.  
  368. #############################################################################
  369. sub ReadDir {
  370.   my($dir)= @_;
  371.   if ( opendir(DIR, $dir) ) {
  372.     my @file = readdir(DIR);
  373.     closedir(DIR);
  374.     return @file;
  375.   }
  376.   else {
  377.     print $LOG "Error: Unable to open directory $dir\n $!";
  378.     exit(1);
  379.   }
  380. }
  381.  
  382. #############################################################################
  383. sub GetDateTime {
  384.   my ($sec,$min,$hour,$day,$month,$yr19,@rest) = localtime(time);
  385.   my $year = $yr19+1900;
  386.   $month++;
  387.   my $date = sprintf("%4d.%02d.%02d_%02d.%02d.%02d",$year,$month,$day,$hour,$min,$sec);
  388.   return $date;
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement