Changeset 443 for trunk/includes/dbc.php


Ignore:
Timestamp:
Apr 11, 2010, 11:48:39 AM (14 years ago)
Author:
george
Message:
  • Upraveno: Přepracována a vylepšena třídy DBC. Nově je formát sloupců určen pomocí pole párů indexů a typů. Doplněny optimalizace předvytvoření prázdných polí se známou velikostí.
  • Upraveno: Přepracován systém určování textových sloupců DBC souborů dle verzí klienta. Vytvořena samostatná tabulka GroupItemDBC obsahující sloupce id verze klienta, id položky GroupItem a index v DBC souboru. Odstraněny podobné údaje z ClientVersion. Upraven DBC import a export.
  • Přidáno: Další překladové skupiny z DBC souborů.
  • Upraveno: Export přepracován pro jednoduchost zpět na PHP pomalou variantu. Bude později přepracován a zoptimalizován.
  • Přidáno: Zobrazení času úpravy překladu.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/dbc.php

    r297 r443  
    44
    55define('NOT_DBC_FILE', 'Není DBC soubor.');
    6 define('FIELD_COUNT_NOT_MATCH', 'Počet sloupců neodpovídá délce formátu.');
    76define('RECORD_SIZE_NOT_MATCH', 'Velikost řádku neodpovídá zadanému formátu.');
    8  
     7
     8define('DBC_SIGNATURE', 0x43424457);
     9
     10define('FORMAT_UINT32', 0);
     11define('FORMAT_SINT32', 1);
     12define('FORMAT_SINGLE', 2);
     13define('FORMAT_STRING', 3);
     14define('FORMAT_BYTE', 4);
     15
    916class DBCFile extends FileStream
    1017{
     
    1421  private $StringList = array();
    1522  private $StringListOffset = array();
    16   private $Format;
     23  private $ColumnFormat; // Array (Index => Type)
     24  private $EndOffset; // Calculated RecordSize according columns type
    1725   
    1826  private $RecordSize;
     
    2129  private $FieldCount;
    2230     
    23   public function OpenFile($FileName, $Format)
     31  public function OpenFile($FileName, $ColumnFormat = array())
    2432  {
    2533    parent::OpenFile($FileName);
    2634   
    27     $this->Format = $Format;
    28     if($this->ReadUint() != 0x43424457) die(NOT_DBC_FILE);
     35    $this->ColumnFormat = $ColumnFormat;
     36    if($this->ReadUint() != DBC_SIGNATURE) die(NOT_DBC_FILE);
    2937       
    3038    $this->RecordCount = $this->ReadUint();
     
    3341    $this->StringBlockSize = $this->ReadUint();
    3442     
    35     if(strlen($this->Format) != $this->FieldCount)
    36       die(FIELD_COUNT_NOT_MATCH.' Počet v souboru:'.$this->FieldCount.' Počet v struktuře: '.strlen($Format));
    37      
    38     $this->GenerateOffsetTable($Format);
    39     if($this->Offsets[count($this->Offsets) - 1] != $this->RecordSize)
    40     die(RECORD_SIZE_NOT_MATCH.$this->Offsets[count($this->Offsets) - 1].' <> '.$this->RecordSize);
    41   }
    42 
    43   public function CreateFile($FileName, $Format)
     43    $this->GenerateOffsetTable();
     44    if($this->EndOffset != $this->RecordSize)
     45    die(RECORD_SIZE_NOT_MATCH.$this->EndOffset.' <> '.$this->RecordSize);
     46  }
     47
     48  public function CreateFile($FileName, $ColumnFormat = array())
    4449  {
    4550    parent::CreateFile($FileName);
    4651   
    47     $this->WriteUint(0x43424457);
     52    $this->WriteUint(DBC_SIGNATURE);
    4853       
    4954    $this->StringList = array();
    5055    $this->StringOffset = 1;
    51     $this->Format = $Format;
    52     $this->GenerateOffsetTable($Format);
    53     $this->FieldCount = strlen($Format);
     56    $this->ColumnFormat = $ColumnFormat;
     57    $this->FieldCount = 0;
     58    $this->GenerateOffsetTable();
    5459    $this->RecordCount = 0;
    55     $this->RecordSize = $this->Offsets[count($this->Offsets) - 1];
     60    $this->RecordSize = $this->EndOffset;
    5661    $this->StringBlockSize = 0;
    5762
     
    6267  }
    6368   
    64   private function GenerateOffsetTable($Format)
    65   {
    66     $this->Offsets = array();
    67     $this->Offsets[0] = 0;
    68     for($I = 0; $I < strlen($Format); $I++)
    69     {
    70       $this->Offsets[$I + 1] = $this->Offsets[$I];
    71       switch($Format[$I])
     69  private function GenerateOffsetTable()
     70  {
     71    // Preallocate array
     72    if($this->FieldCount > 0) $this->Offsets = array_fill(0, $this->FieldCount, 0);
     73      else $this->Offsets = array();
     74   
     75    $Offset = 0;
     76    $I = 0;
     77    while($I < $this->FieldCount)
     78    {
     79      $this->Offsets[$I] = $Offset;
     80      if(array_key_exists($I, $this->ColumnFormat)) $Format = $this->ColumnFormat[$I];
     81        else $Format = FORMAT_UINT32;
     82      switch($Format)
    7283      {
    73         case "b":
    74         case "X":
    75           $this->Offsets[$I + 1] += 1; 
    76           break;
    77         case "x":
    78         case "u":
    79         case "i":
    80         case "f":
    81         case "s":
    82           $this->Offsets[$I + 1] += 4; 
     84        case FORMAT_BYTE:
     85          $Offset += 1; 
     86          break;
     87        case FORMAT_UINT32:
     88        case FORMAT_SINT32:
     89        case FORMAT_SINGLE:
     90        case FORMAT_STRING:
     91          $Offset += 4; 
    8392          break;
    8493      }
     94      $I++;
    8595    }   
     96    $this->EndOffset = $Offset;
    8697  }
    8798 
     
    98109  }   
    99110   
    100   public function GetUint($Row, $Column)
     111  public function GetUInt($Row, $Column)
    101112  {
    102113    $this->SeekPosi($Row, $Column); 
     
    142153  public function GetString($Row, $Column)
    143154  {
    144   $Offset = $this->GetUint($Row, $Column);
     155    $Offset = $this->GetUint($Row, $Column);
    145156   
    146157    $Position = $this->HeaderSize + $this->RecordCount * $this->RecordSize + $Offset;
     
    172183  public function Commit()
    173184  {
     185    $this->SetSize($this->HeaderSize + $this->RecordSize * $this->RecordCount); // Preallocate file
    174186    $this->Seek(0);
    175     $this->WriteUint(0x43424457);
     187    $this->WriteUint(DBC_SIGNATURE);
    176188    $this->WriteUint($this->RecordCount);
    177189    $this->WriteUint($this->FieldCount);
     
    188200  public function GetLine($Row)
    189201  {
    190     $Line = array();
     202    // Preallocate array
     203    if($this->FieldCount > 0) $Line = array_fill(0, $this->FieldCount, 0);
     204      else $Line = array();
    191205    for($I = 0; $I < $this->FieldCount; $I++)
    192206    {
    193       switch($this->Format[$I])
     207      if(array_key_exists($I, $this->ColumnFormat)) $Format = $this->ColumnFormat[$I];
     208        else $Format = FORMAT_UINT32;
     209      switch($Format)
    194210      {
    195         case 'b':
     211        case FORMAT_BYTE:
    196212          $Line[$I] = $this->GetByte($Row, $I);
    197213          break;
    198         case 'u':
    199           $Line[$I] = $this->GetUint($Row, $I);
    200           break;
    201         case 'i':
     214        case FORMAT_UINT32:
     215          $Line[$I] = $this->GetUInt($Row, $I);
     216          break;
     217        case FORMAT_SINT32:
    202218          $Line[$I] = $this->GetInt($Row, $I);
    203219          break;
    204         case 'f':
     220        case FORMAT_SINGLE:
    205221          $Line[$i] = $this->GetFloat($Row, $I);
    206222          break;
    207         case 's':
     223        case FORMAT_STRING:
    208224          $Line[$I] = $this->GetString($Row, $I);
    209225          break;
    210         case 'x':
    211         case 'X':
    212226        default:
    213227          break;
     
    221235    for($I = 0; $I < $this->FieldCount; $I++)
    222236    {
    223       switch($this->Format[$I])
     237      if(array_key_exists($I, $this->ColumnFormat)) $Format = $this->ColumnFormat[$I];
     238        else $Format = FORMAT_UINT32;
     239      switch($Format)
    224240      {
    225         case 'b':
     241        case FORMAT_BYTE:
    226242          $this->SetByte($Row, $I, $Line[$I]);
    227243          break;
    228         case 'u':
     244        case FORMAT_UINT32:
    229245          $this->SetUint($Row, $I, $Line[$I]);
    230246          break;
    231         case 'i':
     247        case FORMAT_SINT32:
    232248          $this->SetInt($Row, $I, $Line[$I]);
    233249          break;
    234         case 'f':
     250        case FORMAT_SINGLE:
    235251          $this->SetFloat($Row, $I, $Line[$i]);
    236252          break;
    237         case 's':
     253        case FORMAT_STRING:
    238254          $this->SetString($Row, $I, $Line[$I]);
    239255          break;
    240         case 'x':
    241         case 'X':
    242256        default:
    243257          break;
     
    249263  public function GetLineCols($Row, $Columns)
    250264  {
    251     $Line = array();
     265    // Preallocate array
     266    if(count($Columns) > 0) $Line = array_fill(0, count($Columns), 0);
     267      else $Line = array();
    252268    for($I = 0; $I < count($Columns); $I++)
    253269    {
    254       switch($this->Format[$Columns[$I]])
     270      if(array_key_exists($I, $this->ColumnFormat)) $Format = $this->ColumnFormat[$I];
     271        else $Format = FORMAT_UINT32;
     272      switch($Format)
    255273      {
    256         case 'b':
     274        case FORMAT_BYTE:
    257275          $Line[$I] = $this->GetByte($Row, $Columns[$I]);
    258276          break;
    259         case 'u':
     277        case FORMAT_UINT32:
    260278          $Line[$I] = $this->GetUint($Row, $Columns[$I]);
    261279          break;
    262         case 'i':
     280        case FORMAT_SINT32:
    263281          $Line[$I] = $this->GetInt($Row, $Columns[$I]);
    264282          break;
    265         case 'f':
     283        case FORMAT_SINGLE:
    266284          $Line[$i] = $this->GetFloat($Row, $Columns[$I]);
    267285          break;
    268         case 's':
     286        case FORMAT_STRING:
    269287          $Line[$I] = $this->GetString($Row, $Columns[$I]);
    270288          break;
    271         case 'x':
    272         case 'X':
    273289        default:
    274290          break;
     
    285301  public function SetRecordCount($Value)
    286302  {
    287   $this->RecordCount = $Value;
     303    $this->RecordCount = $Value;
    288304  }
    289305
     
    292308    return($this->FieldCount);
    293309  }
     310
     311  public function SetFieldCount($Value)
     312  {
     313    $this->FieldCount = $Value;
     314    $this->GenerateOffsetTable();
     315    $this->RecordSize = $this->EndOffset;
     316  }
    294317}
    295318 
Note: See TracChangeset for help on using the changeset viewer.