Advertisement
Sinistral

merge.php

Jan 18th, 2013
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.45 KB | None | 0 0
  1. <?php
  2. /** *************************************************************
  3.  *  Copyright (C) 2013 Steffen Roßkamp
  4.  *
  5.  *  All rights reserved
  6.  *
  7.  *  This script is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 3 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  The GNU General Public License can be found at
  13.  *  http://www.gnu.org/copyleft/gpl.html.
  14.  *
  15.  *  This script is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  This copyright notice MUST APPEAR in all copies of the script!
  21.  *  *************************************************************
  22.  */
  23.  
  24. define('DEBUG', TRUE);
  25. debug('Merge-Script run at ' . date('c'));
  26.  
  27. /**
  28.  * @param $dbg
  29.  */
  30. function debug($dbg) {
  31.     if (DEBUG === TRUE) {
  32.         file_put_contents('merge.log', print_r($dbg, TRUE)."\n", FILE_APPEND);
  33.     }
  34. }
  35.  
  36. /**
  37.  * @param $exportFile
  38.  * @return array
  39.  */
  40. function computeBPHashes($exportFile) {
  41.     $handle = fopen($exportFile, 'r');
  42.     while (($exportDataEntry = fgetcsv($handle, 1024, ',')) != false) {
  43.         // Item Name,QL,Character,Backpack,Location,LowID,HighID,ContainerID,Link
  44.         if (isset($exportDataEntry[7]) && $exportDataEntry[7] > 2) {
  45.             debug("BP '{$exportDataEntry[3]}' item (id={$exportDataEntry[5]}&hid={$exportDataEntry[6]}&ql={$exportDataEntry[1]}): {$exportDataEntry[0]}");
  46.             $tempExportData[$exportDataEntry[7]]['items'][] = md5(json_encode(array(
  47.                 'LID' => $exportDataEntry[5],
  48.                 'HID' => $exportDataEntry[6],
  49.                 'QL' => $exportDataEntry[1],
  50.             )));
  51.             $tempExportData[$exportDataEntry[7]]['id'] = $exportDataEntry[7];
  52.             $tempExportData[$exportDataEntry[7]]['name'] = $exportDataEntry[3];
  53.         } else {
  54.             debug("Non-BP item (id={$exportDataEntry[5]}&hid={$exportDataEntry[6]}&ql={$exportDataEntry[1]}): {$exportDataEntry[0]}");
  55.         }
  56.     }
  57.     fclose($handle);
  58.     $exportData = array();
  59.     foreach ($tempExportData as &$exportDataEntry) {
  60.         sort($exportDataEntry['items']);
  61.         $exportData[md5(json_encode($exportDataEntry['items']))] = array(
  62.             'name' => $exportDataEntry['name'],
  63.             'id' => $exportDataEntry['id']
  64.         );
  65.     }
  66.     debug($exportData);
  67.     return $exportData;
  68. }
  69.  
  70. if ($argc != 4) {
  71.     echo <<<'EOT'
  72.  
  73. This script tries to match old backpacks to new ones by their content and
  74. then update the appropriate container and dock area files.
  75.  
  76. usage: php merge.php oldExport newExport containerDir
  77.  
  78.   oldExport    : Pre merge AOIS data export
  79.   newExport    : Post merge AOIS data export
  80.   charDir      : New preference directory of the exports character
  81.  
  82.  
  83. EOT;
  84.     exit();
  85. }
  86.  
  87. if (!is_readable($argv[1]) || !is_readable($argv[2])) {
  88.     echo <<<'EOT'
  89.  
  90. One of your export files is not readable.
  91. Please check that:
  92.   * Both paths to the files are correct.
  93.   * Both files are readable by your current user.
  94.  
  95.  
  96. EOT;
  97.     exit();
  98. }
  99.  
  100. $charDir = $argv[3];
  101. if (!is_dir($charDir) || !is_writable($charDir . 'Containers/') || !is_writable($charDir . 'DockAreas/')) {
  102.     echo <<<'EOT'
  103.  
  104. You chosen character preference directory either doesn't exist or the
  105. 'Containers' or 'DockAreas' directories are not writable.
  106.  
  107. Please check that:
  108.   * The path to the characters diretcory is correct.
  109.   * Both mentioned directories are writable by your current user.
  110.  
  111.  
  112. EOT;
  113.     exit();
  114. }
  115.  
  116. $oldExportData = computeBPHashes($argv[1]);
  117. $newExportData = computeBPHashes($argv[2]);
  118. foreach ($newExportData as $hash => $entry) {
  119.     if (is_array($oldExportData[$hash])) {
  120.         $matchData[$oldExportData[$hash]['id']] = $entry['id'];
  121.     }
  122. }
  123. debug($matchData);
  124. $containerFiles = scandir($charDir . 'Containers/');
  125. foreach ($containerFiles as $containerFile) {
  126.     if (preg_match('/(Container_51017x)([0-9]+)(\.xml)/', $containerFile, $name)) {
  127.         if (isset($matchData[$name[2]])) {
  128.             debug("Container OID \"{$name[2]}\" -> NID \"{$matchData[$name[2]]}\".");
  129.             $file = @file_get_contents($charDir . 'Containers/' . $containerFile);
  130.             if ($file) {
  131.                 // Get the DockArea name for the container to update them appropriately
  132.                 preg_match('/\<String name="DockableViewDockName" value=\'\&quot;(\w+)\&quot;\' \/\>/', $file, $result);
  133.                 if ($result) {
  134.                     $matchDockArea[$result[1]][] = array(
  135.                         'oid' => $name[2],
  136.                         'nid' => $matchData[$name[2]]
  137.                     );
  138.                 }
  139.             }
  140.             if (is_file($charDir . 'Containers/' . $name[1] . $matchData[$name[2]] . $name[3])) {
  141.                 if(!is_dir($charDir . 'Backup')) {
  142.                     debug('Creating Backup directory.');
  143.                     mkdir($charDir . 'Backup');
  144.                     mkdir($charDir . 'Backup/Containers');
  145.                 }
  146.                 debug($charDir . 'Containers/' . $name[1] . $matchData[$name[2]] . $name[3] . ' already exists, backing up.');
  147.                 // Backup container file with new ID if already created by the game
  148.                 rename($charDir . 'Containers/' . $name[1] . $matchData[$name[2]] . $name[3], $charDir . 'Backup/Containers/' . $name[1] . $matchData[$name[2]] . $name[3]);
  149.             }
  150.             debug("Renaming\n'{$charDir}Containers/{$containerFile}'\nto\n'{$charDir}Containers/{$name[1]}{$matchData[$name[2]]}{$name[3]}'");
  151.             rename($charDir . 'Containers/' . $containerFile, $charDir . 'Containers/' . $name[1] . $matchData[$name[2]] . $name[3]);
  152.         }
  153.     }
  154. }
  155. if(isset($matchDockArea)) {
  156.     foreach ($matchDockArea as $filename => $matchIds) {
  157.         $file = @file_get_contents($charDir . 'DockAreas/' . $filename . '.xml');
  158.         if ($file) {
  159.             foreach ($matchIds as $matchId) {
  160.                 $file = str_replace("<String value='&quot;Container_51017x{$matchId['oid']}.xml&quot;' />", "<String value='&quot;Container_51017x{$matchId['nid']}.xml&quot;' />", $file);
  161.             }
  162.             file_put_contents($charDir . 'DockAreas/' . $filename . '.xml', $file);
  163.             debug('DockArea updated: ' . $charDir . 'DockAreas/' . $filename . '.xml');
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement