1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#!/usr/bin/env php
<?php
/***********************************************************************************
* Warzone 2100 RMSG to INI converter
* Copyright (C) 2012 Michal Dutkiewicz aka Emdek <emdeck@gmail.com>
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
***********************************************************************************/
function convert_rmsg($input, &$errors)
{
$output = '';
$data = explode("\n", $input);
for ($i = 0, $c = count($data); $i < $c; ++$i)
{
$line = trim($data[$i], "\t\r\n ");
if (strlen($line) < 5)
{
continue;
}
if ($line[0] == '.')
{
if (substr($line, -5) != 'NULL,')
{
$output.= ((substr($output, -2) == '\\'."\n")?"\n":'').substr($line, 1, -1)."\n";
}
}
else if ($line[0] == '_')
{
$output.= $line.'\\'."\n";
}
else if (preg_match('#^[A-Z][a-zA-Z0-9_]+$#', $line))
{
$output.= ($output?"\n":'').'['.$line.']'."\n".'text = \\'."\n";
}
else
{
$errors[] = ($i + 1);
}
}
return $output;
}
if (count($argv) == 1)
{
echo 'Usage:
'.(is_executable($argv[0])?'':'php ').$argv[0].' /path/to/file.rmsg [/path/to/log/file.txt]
';
die();
}
if (!file_exists($argv[1]))
{
echo 'Can not find specified path:
'.$argv[1];
die();
}
$log = 'Converting data from path:
'.$argv[1].'
';
if (is_dir($argv[1]))
{
$argv[1] = rtrim($argv[1], '\\/');
echo 'Converting all files with .rmsg extension from specified directory by creating new with .ini suffix.
';
$files = glob($argv[1].'/*.rmsg');
$total = 0;
$skipped = 0;
for ($i = 0, $c = count($files); $i < $c; ++$i)
{
$log.= $files[$i].'
';
$errors = array();
$path = substr($files[$i], 0, -4).'ini';
if (file_exists($path) && (!isset($argv[3]) || $argv[3] != '!'))
{
$log.= 'Skipped due to already existing file with the same path!
'.$path.'
';
++$skipped;
}
else if (!file_put_contents($path, convert_rmsg(file_get_contents($files[$i]), $errors)))
{
$log.= 'Saving output file failed!
'.$path.'
';
++$skipped;
}
$total += count($errors);
$log.= ($errors?count($errors):'No').' skipped lines (total: '.$total.')
'.($errors?implode(', ', $errors)."\n":'').'
';
}
$log.= 'Total: '.($total?$total:'No').' skipped lines'.($skipped?"\n".$skipped.' files skipped!':'');
}
else
{
$errors = array();
$log.= 'Single file
';
echo convert_rmsg(file_get_contents($argv[1]), $errors);
$log.= ($errors?count($errors):'No').' skipped lines
'.($errors?implode(', ', $errors)."\n":'');
}
if (isset($argv[2]))
{
$log.= "\n";
if ($argv[2] == '!')
{
echo $log;
}
else
{
file_put_contents($argv[2], $log);
}
}
?>
|