예제 #1
0
파일: IController.php 프로젝트: noikiy/mcc
 public function textureAction($packID, $textureMetaID, $textureID)
 {
     if (!Validator::isValidID($packID)) {
         throw new Exception("Invalid Pack ID: '{$packID}'");
     }
     if (!Validator::isValidID($textureMetaID)) {
         throw new Exception("Invalid Meta ID: '{$textureMetaID}'");
     }
     if (!Validator::isValidID($textureID)) {
         throw new Exception("Invalid Texture ID: '{$textureID}'");
     }
     $style = $this->request->get('style');
     if (isset($style)) {
         if (!Validator::isValidTextureStyle($style)) {
             throw new Exception("Invalid style: '{$style}'");
         }
     } else {
         $style = 1;
     }
     $scale = $this->request->get('scale');
     if (isset($scale)) {
         if (!Validator::isValidTextureScale($scale)) {
             throw new Exception("Invalid scale: '{$scale}'");
         }
     } else {
         $scale = 1;
     }
     $workspace = new ResourcePackWorkspace($packID);
     $workspace->outputTextureImage($textureID, $textureMetaID, $style, $scale);
 }
예제 #2
0
 /**
  * @param ZipArchive $zip
  * @throws Exception
  */
 public function importZip($zip)
 {
     if ($zip->numFiles == 0) {
         throw new Exception("Zip to import has no files");
     }
     /** @var \Phalcon\Db\Adapter\Pdo $db */
     $db = $this->getDI()['db'];
     $indexesByImagePath = array();
     $metaIndexesByImagePath = array();
     for ($i = 0; $i < $zip->numFiles; $i++) {
         $path = $zip->getNameIndex($i);
         if (strcasecmp(substr($path, -4), '.png') === 0) {
             $indexesByImagePath[$path] = $i;
         } else {
             if (strcasecmp(substr($path, -7), '.mcmeta') === 0) {
                 $imagePath = substr($path, 0, -7);
                 $metaIndexesByImagePath[$imagePath] = $i;
             }
         }
     }
     $resourcePaths = array_keys($indexesByImagePath);
     if (count($resourcePaths) == 0) {
         throw new Exception("No files to import found in zip");
     }
     $workspace = new ResourcePackWorkspace($this->packID);
     $resourceIDsByMetaID = array();
     $resourceMetasByID = array();
     $db->begin();
     $workspace->beginWrites();
     try {
         $this->enumerateResourceMetaForPaths($resourcePaths, function ($resourceMeta) use($indexesByImagePath, $metaIndexesByImagePath, $zip, $db, $workspace, &$resourceMetasByID, &$resourceIDsByMetaID) {
             $resourceMetasByID[$resourceMeta->id] = $resourceMeta;
             $imageIndex = $indexesByImagePath[$resourceMeta->path];
             $imageData = $zip->getFromIndex($imageIndex);
             $metaData = NULL;
             if (isset($metaIndexesByImagePath[$resourceMeta->path])) {
                 $metaDataIndex = $metaIndexesByImagePath[$resourceMeta->path];
                 $metaData = $this->reformatJSONString($zip->getFromIndex($metaDataIndex));
             }
             if ($imageData == NULL || strlen($imageData) == 0) {
                 echo "Failed reading image data for {$resourceMeta->path}", "<br>";
                 return TRUE;
                 // continue
             }
             $size = getimagesizefromstring($imageData);
             if ($size == NULL) {
                 $size = [0, 0];
             }
             $stmt = $db->prepare("INSERT INTO Resource(packID, metaID, metadata, width, height) VALUES (?, ?, ?, ?, ?)");
             $success = $stmt->execute([$this->packID, $resourceMeta->id, $metaData, $size[0], $size[1]]);
             if (!$success) {
                 $sqlError = $stmt->errorCode() . ': ' . implode(', ', $stmt->errorInfo());
                 echo "Failed inserting Resource - {$sqlError}", "<br>";
                 return TRUE;
                 // continue
             }
             $resourceID = $db->lastInsertId();
             $workspace->addResourceData($resourceID, $resourceMeta->id, $imageData);
             $resourceIDsByMetaID[$resourceMeta->id] = $resourceID;
             return TRUE;
         });
         $workspace->endWrites();
         $db->commit();
     } catch (Exception $e) {
         $db->rollback();
         $workspace->rollbackWrites();
         throw $e;
     }
     if (count($resourceMetasByID) > 0) {
         $resourceMetaIDString = implode(',', array_keys($resourceMetasByID));
         $query = "SELECT textureMetaID, resourceMetaID FROM TextureMeta_ResourceMeta WHERE resourceMetaID IN ({$resourceMetaIDString})";
         $result = $db->query($query);
         $result->setFetchMode(PDO::FETCH_OBJ);
         $resourceIDsByTMID = array();
         while ($obj = $result->fetch()) {
             $resourceID = $resourceIDsByMetaID[$obj->resourceMetaID];
             $textureMetaID = $obj->textureMetaID;
             $resourceIDsByTMID[$textureMetaID][] = $resourceID;
         }
         $result = NULL;
         $failCount = 0;
         foreach ($resourceIDsByTMID as $tmID => $resourceIDs) {
             $db->begin();
             try {
                 $stillSucceeding = $db->execute('INSERT INTO Texture(metaID, packID) VALUES (?, ?)', [$tmID, $this->packID]);
                 if ($stillSucceeding) {
                     $textureID = $db->lastInsertId();
                     foreach ($resourceIDs as $resourceID) {
                         $stillSucceeding = $db->execute('INSERT INTO Texture_Resource(textureID, resourceID) VALUES (?, ?)', [$textureID, $resourceID]);
                         if (!$stillSucceeding) {
                             break;
                         }
                     }
                 }
                 if (!$stillSucceeding) {
                     $failCount++;
                     $db->rollback();
                 }
                 $db->commit();
             } catch (Exception $e) {
                 $db->rollback();
                 throw $e;
             }
         }
         if ($failCount > 0) {
             echo "There were {$failCount} texture creation failures", "<br>";
         } else {
             echo "Complete Success!!!", "<br>";
         }
     }
 }
예제 #3
0
 /**
  * @throws Exception
  */
 private function createFile()
 {
     /** @var \Phalcon\Db\Adapter\Pdo $db */
     $db = $this->getDI()['db'];
     $query = "SELECT t.id AS textureID, t.metaID AS textureMetaID, r.id AS resourceID, r.width AS resourceWidth, r.height AS resourceHeight, r.metaID AS resourceMetaID, rm.path AS resourceMetaPath" . " FROM Texture t" . " INNER JOIN Texture_Resource j ON t.id = j.textureID" . " INNER JOIN Resource r ON j.resourceID = r.id" . " INNER JOIN ResourceMeta rm ON r.metaID = rm.id" . " WHERE t.id IN ({$this->textureIDString})";
     $result = $db->query($query);
     if (!$result->execute()) {
         throw new Exception("Failed to execute Texture search query");
     }
     $result->setFetchMode(PDO::FETCH_OBJ);
     $this->prepareFilePath();
     $specs = array();
     $resourceMetaPathsByRMID = array();
     $resourcePathsByRID = array();
     while ($obj = $result->fetch()) {
         $resourcePath = ResourcePackWorkspace::resourcePath($obj->resourceID, $obj->resourceMetaID, $this->packID);
         if (file_exists($resourcePath)) {
             $resourceMetaPathsByRMID[$obj->resourceMetaID] = $obj->resourceMetaPath;
             $resourcePathsByRID[$obj->resourceID] = $resourcePath;
             if (isset($specs[$obj->resourceMetaID][$obj->resourceID])) {
                 $resource = $specs[$obj->resourceMetaID][$obj->resourceID];
             } else {
                 $resource = new stdClass();
                 $resource->width = $obj->resourceWidth;
                 $resource->height = $obj->resourceHeight;
                 $specs[$obj->resourceMetaID][$obj->resourceID] = $resource;
             }
             $texture = new stdClass();
             $texture->id = $obj->textureID;
             $texture->metaID = $obj->textureMetaID;
             $resource->textures[] = $texture;
         }
     }
     $result = NULL;
     $zip = new ZipArchive();
     $opened = $zip->open($this->packFilePath, ZipArchive::CREATE);
     if ($opened !== TRUE) {
         throw new Exception("Failed to open new zip file");
     }
     $multiSpecs = array();
     foreach ($specs as $rmID => $resources) {
         $resourceCount = count($resources);
         if ($resourceCount == 1) {
             // just add the whole resource since we don't need to merge anything
             reset($resources);
             $rID = key($resources);
             $resourcePath = $resourcePathsByRID[$rID];
             $resourceMetaPath = $resourceMetaPathsByRMID[$rmID];
             $zip->addFile($resourcePath, $resourceMetaPath);
         } else {
             $multiSpecs[$rmID] = $resources;
         }
     }
     $specs = NULL;
     if (count($multiSpecs) > 0) {
         $largestSizeByRMID = array();
         $resourceIDsByTMID = array();
         foreach ($multiSpecs as $rmID => $resources) {
             foreach ($resources as $rID => $resource) {
                 if (!isset($largestSizeByRMID[$rmID]) || $resource->width > $largestSizeByRMID[$rmID][0]) {
                     $largestSizeByRMID[$rmID][0] = $resource->width;
                     $largestSizeByRMID[$rmID][1] = $resource->height;
                 }
                 foreach ($resource->textures as $texture) {
                     $resourceIDsByTMID[$texture->metaID] = $rID;
                 }
             }
         }
         $multiRenderer = new MultiResourceRenderer();
         $multiRMIDs = array_keys($multiSpecs);
         $placeholders = implode(',', array_fill(0, count($multiRMIDs), '?'));
         $rectStmt = $db->query("SELECT j.textureMetaID, j.resourceMetaID, j.sourceFormat FROM TextureMeta_ResourceMeta j WHERE resourceMetaID IN ({$placeholders})", $multiRMIDs);
         $rectStmt->setFetchMode(PDO::FETCH_OBJ);
         while ($obj = $rectStmt->fetch()) {
             $sourceFormat = ResourceRenderer::createSourceFormatFromString($obj->sourceFormat);
             $size = $largestSizeByRMID[$obj->resourceMetaID];
             $resourceID = $resourceIDsByTMID[$obj->textureMetaID];
             $path = $resourcePathsByRID[$resourceID];
             $multiRenderer->addResource($obj->resourceMetaID, $size[0], $size[1], $path, $sourceFormat);
         }
         unset($rectStmt);
         unset($largestSizeByRMID);
         unset($resourceIDsByTMID);
         unset($multiRMIDs);
         while ($image = $multiRenderer->renderOne($imageRMID)) {
             ob_start();
             imagepng($image, NULL, 1, PNG_NO_FILTER);
             $imageData = ob_get_clean();
             imagedestroy($image);
             $metaPath = $resourceMetaPathsByRMID[$imageRMID];
             $zip->addFromString($metaPath, $imageData);
         }
     }
     $isEmpty = $zip->numFiles == 0;
     $zip->close();
     $zip = NULL;
     return !$isEmpty;
 }
예제 #4
0
 public static function getRootDirectory()
 {
     if (self::$rootDirectory == NULL) {
         $webRoot = $_SERVER['DOCUMENT_ROOT'];
         if (substr($webRoot, -1) !== DIRECTORY_SEPARATOR) {
             $webRoot .= DIRECTORY_SEPARATOR;
         }
         self::$rootDirectory = "{$webRoot}../data/pack-workspaces";
     }
     return self::$rootDirectory;
 }