コード例 #1
0
 /**
  * Attempts to construct a map based on given DBC, predicting what each field could possibly hold by way of sampling
  */
 public static function fromDBC(DBC $dbc, $attach = true)
 {
     $fields = $dbc->getFieldCount();
     $samples = $dbc->getRecordCount() > self::SAMPLES ? self::SAMPLES : $dbc->getRecordCount();
     $block = $dbc->getStringBlock();
     preg_match_all('#\\0#', $block, $matches, PREG_OFFSET_CAPTURE);
     $strings = array();
     foreach ($matches[0] as $offset) {
         $offset = (int) $offset[1] + 1;
         if ($offset < strlen($block) - 1) {
             $strings[$offset] = true;
         }
     }
     $matrix = array_fill(1, $fields, 0);
     for ($i = 0; $i < $samples; $i++) {
         $record = $dbc->getRecord($i);
         $values = $record->asArray();
         foreach ($values as $offset => $value) {
             if ($value < 0) {
                 $matrix[$offset] += 1 << 0;
             }
             if (self::isProbableFloat($value)) {
                 $matrix[$offset] += 1 << 8;
             }
             if (isset($strings[$value]) || $value === 0) {
                 $matrix[$offset] += 1 << 16;
                 if ($value !== 0) {
                     $matrix[$offset] |= 1 << 24;
                 }
             }
         }
     }
     $map = new self();
     for ($i = 1; $i <= $fields; $i++) {
         $probs = $matrix[$i];
         $int = ($probs & 0xff) / $samples;
         $flt = (($probs & 0xff00) >> 8) / $samples;
         $str = (($probs & 0xff0000) >> 16) / $samples;
         $strbit = ($probs & 0xff000000) >> 24;
         $field = 'field' . $i;
         if ($flt > 0.6) {
             $type = DBC::FLOAT;
         } else {
             if ($strbit > 0 && $str > 0.99) {
                 $type = DBC::STRING;
                 if ($i + DBC::LOCALIZATION <= $fields) {
                     $type = DBC::STRING_LOC;
                     for ($j = $i + 1; $j <= $i + DBC::LOCALIZATION; $j++) {
                         $probs = $matrix[$j];
                         $str = (($probs & 0xff0000) >> 16) / $samples;
                         $strbit = ($probs & 0xff000000) >> 24;
                         if ($str !== 1 || $strbit !== 0) {
                             $type = DBC::STRING;
                         }
                     }
                     if ($type === DBC::STRING_LOC) {
                         $i += DBC::LOCALIZATION;
                     }
                 }
             } else {
                 if ($int > 0.01) {
                     $type = DBC::INT;
                 } else {
                     $type = DBC::UINT;
                 }
             }
         }
         $map->add($field, $type);
     }
     if ($attach && $dbc->getMap() === null) {
         $dbc->attach($map);
     }
     return $map;
 }