Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /** *************************************************************
- * Copyright (C) 2013 Steffen Roßkamp
- *
- * All rights reserved
- *
- * This script is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * The GNU General Public License can be found at
- * http://www.gnu.org/copyleft/gpl.html.
- *
- * This script is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * This copyright notice MUST APPEAR in all copies of the script!
- * *************************************************************
- */
- define('DEBUG', TRUE);
- debug('Merge-Script run at ' . date('c'));
- /**
- * @param $dbg
- */
- function debug($dbg) {
- if (DEBUG === TRUE) {
- file_put_contents('merge.log', print_r($dbg, TRUE)."\n", FILE_APPEND);
- }
- }
- /**
- * @param $exportFile
- * @return array
- */
- function computeBPHashes($exportFile) {
- $handle = fopen($exportFile, 'r');
- while (($exportDataEntry = fgetcsv($handle, 1024, ',')) != false) {
- // Item Name,QL,Character,Backpack,Location,LowID,HighID,ContainerID,Link
- if (isset($exportDataEntry[7]) && $exportDataEntry[7] > 2) {
- debug("BP '{$exportDataEntry[3]}' item (id={$exportDataEntry[5]}&hid={$exportDataEntry[6]}&ql={$exportDataEntry[1]}): {$exportDataEntry[0]}");
- $tempExportData[$exportDataEntry[7]]['items'][] = md5(json_encode(array(
- 'LID' => $exportDataEntry[5],
- 'HID' => $exportDataEntry[6],
- 'QL' => $exportDataEntry[1],
- )));
- $tempExportData[$exportDataEntry[7]]['id'] = $exportDataEntry[7];
- $tempExportData[$exportDataEntry[7]]['name'] = $exportDataEntry[3];
- } else {
- debug("Non-BP item (id={$exportDataEntry[5]}&hid={$exportDataEntry[6]}&ql={$exportDataEntry[1]}): {$exportDataEntry[0]}");
- }
- }
- fclose($handle);
- $exportData = array();
- foreach ($tempExportData as &$exportDataEntry) {
- sort($exportDataEntry['items']);
- $exportData[md5(json_encode($exportDataEntry['items']))] = array(
- 'name' => $exportDataEntry['name'],
- 'id' => $exportDataEntry['id']
- );
- }
- debug($exportData);
- return $exportData;
- }
- if ($argc != 4) {
- echo <<<'EOT'
- This script tries to match old backpacks to new ones by their content and
- then update the appropriate container and dock area files.
- usage: php merge.php oldExport newExport containerDir
- oldExport : Pre merge AOIS data export
- newExport : Post merge AOIS data export
- charDir : New preference directory of the exports character
- EOT;
- exit();
- }
- if (!is_readable($argv[1]) || !is_readable($argv[2])) {
- echo <<<'EOT'
- One of your export files is not readable.
- Please check that:
- * Both paths to the files are correct.
- * Both files are readable by your current user.
- EOT;
- exit();
- }
- $charDir = $argv[3];
- if (!is_dir($charDir) || !is_writable($charDir . 'Containers/') || !is_writable($charDir . 'DockAreas/')) {
- echo <<<'EOT'
- You chosen character preference directory either doesn't exist or the
- 'Containers' or 'DockAreas' directories are not writable.
- Please check that:
- * The path to the characters diretcory is correct.
- * Both mentioned directories are writable by your current user.
- EOT;
- exit();
- }
- $oldExportData = computeBPHashes($argv[1]);
- $newExportData = computeBPHashes($argv[2]);
- foreach ($newExportData as $hash => $entry) {
- if (is_array($oldExportData[$hash])) {
- $matchData[$oldExportData[$hash]['id']] = $entry['id'];
- }
- }
- debug($matchData);
- $containerFiles = scandir($charDir . 'Containers/');
- foreach ($containerFiles as $containerFile) {
- if (preg_match('/(Container_51017x)([0-9]+)(\.xml)/', $containerFile, $name)) {
- if (isset($matchData[$name[2]])) {
- debug("Container OID \"{$name[2]}\" -> NID \"{$matchData[$name[2]]}\".");
- $file = @file_get_contents($charDir . 'Containers/' . $containerFile);
- if ($file) {
- // Get the DockArea name for the container to update them appropriately
- preg_match('/\<String name="DockableViewDockName" value=\'\"(\w+)\"\' \/\>/', $file, $result);
- if ($result) {
- $matchDockArea[$result[1]][] = array(
- 'oid' => $name[2],
- 'nid' => $matchData[$name[2]]
- );
- }
- }
- if (is_file($charDir . 'Containers/' . $name[1] . $matchData[$name[2]] . $name[3])) {
- if(!is_dir($charDir . 'Backup')) {
- debug('Creating Backup directory.');
- mkdir($charDir . 'Backup');
- mkdir($charDir . 'Backup/Containers');
- }
- debug($charDir . 'Containers/' . $name[1] . $matchData[$name[2]] . $name[3] . ' already exists, backing up.');
- // Backup container file with new ID if already created by the game
- rename($charDir . 'Containers/' . $name[1] . $matchData[$name[2]] . $name[3], $charDir . 'Backup/Containers/' . $name[1] . $matchData[$name[2]] . $name[3]);
- }
- debug("Renaming\n'{$charDir}Containers/{$containerFile}'\nto\n'{$charDir}Containers/{$name[1]}{$matchData[$name[2]]}{$name[3]}'");
- rename($charDir . 'Containers/' . $containerFile, $charDir . 'Containers/' . $name[1] . $matchData[$name[2]] . $name[3]);
- }
- }
- }
- if(isset($matchDockArea)) {
- foreach ($matchDockArea as $filename => $matchIds) {
- $file = @file_get_contents($charDir . 'DockAreas/' . $filename . '.xml');
- if ($file) {
- foreach ($matchIds as $matchId) {
- $file = str_replace("<String value='"Container_51017x{$matchId['oid']}.xml"' />", "<String value='"Container_51017x{$matchId['nid']}.xml"' />", $file);
- }
- file_put_contents($charDir . 'DockAreas/' . $filename . '.xml', $file);
- debug('DockArea updated: ' . $charDir . 'DockAreas/' . $filename . '.xml');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement