Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // $characterMap is parsed array of characters
- private function ReplaceCharactersToGXTequivalents($a, &$b, &$c)
- {
- $b = '';
- $d = 0;
- $e = strlen($a);
- $f = count($c);
- while($d < $e)
- {
- for(
- $g = $f > $e - $d ? $e - $d : $f;
- $g >= 1;
- --$g)
- {
- $j = '';
- for($i = 0; $i < $g; ++$i)
- $j .= $a[$d+$i];
- foreach($c[$g] as $h)
- {
- if($j === $h['from'])
- {
- $b .= $h['to'];
- $d += $g;
- continue 3;
- }
- }
- }
- throw new Exception('found character with no equivalent in GXT character map: '.substr($a, $d));
- $b = NULL;
- }
- }
- /*
- Exports entries to GXT file
- Versions: 'GTASA'
- Returns filesize of GXT file or FALSE on failure of writing
- */
- public function ExportGxtFile($langID, $gxtVersion, $filename)
- {
- if($fh = fopen($filename, "wb"))
- {
- $LIMIT_MYSQL_ROWS = 500;
- $LIMIT_FILE_BUFFER_BYTES = 4096;
- $errorMessages = '';
- if($gxtVersion == 'GTASA')
- {
- $TABL_SECTION_MEMBER_SIZE = 12;
- $TKEY_SECTION_MEMBER_SIZE = 8;
- $characterMap = $this -> GetLanguageCharacterMapPreparedForReplacingChars($langID);
- $TABL_SECTION = $this -> GetGxtTableNames();
- $TABL_SECTION_count = count($TABL_SECTION);
- $TABL_SECTION_blocksize = $TABL_SECTION_count * $TABL_SECTION_MEMBER_SIZE;
- // // Header and TABL blocksize
- fwrite($fh,
- "\x4\x0\x8\x0TABL"
- .pack('Lx'.$TABL_SECTION_blocksize, $TABL_SECTION_blocksize)
- );
- $TABL_SECTION_offset = 12;
- $TKEY_offset = ftell($fh);
- $TABL_offsets = array();
- // /*Debug*/ $TABL_SECTION_count = 1;
- $FileBuffer = '';
- $gxtTables = array();
- foreach($TABL_SECTION as $tableName)
- {
- $MySQLresult = mysql_query('SELECT gxt_key, gxt_text
- FROM `'.$this -> table_prefix.'table_'.mysql_escape_string($tableName).'`
- WHERE lang_id = '.$langID);
- $gxtTables[$tableName] = array();
- while($gxtEntry = mysql_fetch_assoc($MySQLresult))
- {
- $gxtEntry['gxt_key_hash'] = GTASA_CRC32($gxtEntry['gxt_key']);
- $gxtTables[$tableName][] = $gxtEntry;
- }
- }
- foreach($gxtTables as $gxtTableName => &$gxtTableEntries)
- usort($gxtTableEntries, 'SortGxtEntriesByHash');
- $TABL_SECTION_id = 0;
- foreach($gxtTables as $gxtTableName => &$gxtTableEntries)
- {
- $tableEntriesCount = count($gxtTableEntries);
- $TABL_offsets[$TABL_SECTION_id]['tkey'] = ftell($fh);
- if($TABL_SECTION_id)
- fwrite($fh, pack('a8', $gxtTableName));
- fwrite($fh, 'TKEY'.pack('L', $tableEntriesCount * $TKEY_SECTION_MEMBER_SIZE));
- $TABL_offsets[$TABL_SECTION_id]['tkey_array'] = ftell($fh); // TKEY array of entries
- fwrite($fh, pack('x'.($tableEntriesCount * $TKEY_SECTION_MEMBER_SIZE)));
- // Write TDAT section
- fwrite($fh, 'TDAT');
- $TABL_offsets[$TABL_SECTION_id]['tdat_block_size_offset'] = ftell($fh); // TDAT block size
- fwrite($fh, pack('x4')); // TDAT block size
- $TABL_offsets[$TABL_SECTION_id]['tdat_array_offset'] = $TABL_offsets[$TABL_SECTION_id]['tdat_block_size_offset'] + 4;
- $FilePos = $TABL_offsets[$TABL_SECTION_id]['tdat_array_offset'];
- foreach($gxtTableEntries as &$gxtEntry)
- {
- try {
- $this -> ReplaceCharactersToGXTequivalents($gxtEntry['gxt_text'], $gxtEntry['gxt_text'], $characterMap);
- }
- catch (Exception $e) {
- if(!empty($errorMessages))
- $errorMessages .= "\r\n<br/>";
- $errorMessages .= "Table $tableName, GXT key ".$gxtEntry['gxt_key'].': '.$e -> GetMessage();
- }
- $gxtEntry['tdat_offset'] = $FilePos - $TABL_offsets[$TABL_SECTION_id]['tdat_array_offset'];
- $packed_gxt_text = pack('a*x', $gxtEntry['gxt_text']);
- $FileBuffer .= $packed_gxt_text;
- $FilePos += strlen($packed_gxt_text);
- unset($gxtEntry['gxt_text']);
- if(strlen($FileBuffer) > $LIMIT_FILE_BUFFER_BYTES)
- {
- fwrite($fh, $FileBuffer);
- $FileBuffer = NULL;
- }
- }
- fwrite($fh, $FileBuffer);
- $FileBuffer = NULL;
- $TABL_offsets[$TABL_SECTION_id]['tdat_block_size_value'] = $FilePos - $TABL_offsets[$TABL_SECTION_id]['tdat_array_offset'];
- $TABL_SECTION_id++;
- }
- $GXT_filesize = $FilePos;
- // Write TDAT block sizes
- $TABL_SECTION_id = 0;
- foreach($TABL_offsets as $info)
- {
- fseek($fh, $info['tdat_block_size_offset']);
- fwrite($fh, pack('L', $info['tdat_block_size_value']));
- }
- $TABL_SECTION_id = 0;
- foreach($gxtTables as $gxtTableName => &$gxtTableEntries)
- {
- fseek($fh, $TABL_offsets[$TABL_SECTION_id]['tkey_array']);
- foreach ($gxtTableEntries as &$gxtEntry)
- {
- $packed = pack('LL', $gxtEntry['tdat_offset'], $gxtEntry['gxt_key_hash']);
- $FileBuffer .= $packed;
- if(strlen($FileBuffer) > $LIMIT_FILE_BUFFER_BYTES)
- {
- fwrite($fh, $FileBuffer);
- $FileBuffer = NULL;
- }
- }
- fwrite($fh, $FileBuffer);
- $FileBuffer = NULL;
- $TABL_SECTION_id++;
- }
- // Write TKEY offsets in TABL
- fseek($fh, $TABL_SECTION_offset);
- for($i = 0; $i < $TABL_SECTION_count; $i++)
- $FileBuffer .= pack('a8L', $TABL_SECTION[$i], $TABL_offsets[$i]['tkey']);
- fwrite($fh, $FileBuffer);
- }
- fclose($fh);
- if(empty($errorMessages))
- return $GXT_filesize;
- else
- {
- unlink($filename);
- throw new Exception($errorMessages);
- }
- }
- return FALSE;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement