兼容 PHP 5.2

This commit is contained in:
FGHRSH 2019-02-09 15:17:15 +08:00
parent bf2147e0e5
commit 5b263466ea
8 changed files with 83 additions and 20 deletions

View File

@ -6,20 +6,24 @@ require '../tools/modelTextures.php';
$modelList = new modelList(); $modelList = new modelList();
$modelTextures = new modelTextures(); $modelTextures = new modelTextures();
$modelList = $modelList->get_list()['models']; $modelList = $modelList->get_list();
$modelList = $modelList['models'];
foreach ($modelList as $modelName) { foreach ($modelList as $modelName) {
if (file_exists('../model/'.$modelName.'/textures.cache')) { if (!is_array($modelName) && file_exists('../model/'.$modelName.'/textures.cache')) {
$textures = $texturesNew = array(); $textures = $texturesNew = array();
foreach ($modelTextures->get_list($modelName)['textures'] as $v) $textures[] = json_encode($v, JSON_UNESCAPED_SLASHES); $modelTexturesList = $modelTextures->get_list($modelName);
foreach ($modelTextures->get_textures($modelName) as $v) $texturesNew[] = json_encode($v, JSON_UNESCAPED_SLASHES); $modelNameTextures = $modelTextures->get_textures($modelName);
if (is_array($modelTexturesList)) foreach ($modelTexturesList['textures'] as $v) $textures[] = str_replace('\/', '/', json_encode($v));
if (is_array($modelNameTextures)) foreach ($modelNameTextures as $v) $texturesNew[] = str_replace('\/', '/', json_encode($v));
if ($textures == NULL) continue; elseif (empty(array_diff($texturesNew, $textures))) { $texturesDiff = array_diff($texturesNew, $textures);
if (empty($textures)) continue; elseif (empty($texturesDiff)) {
echo '<p>'.$modelName.' / textures.cache / No Update.</p>'; echo '<p>'.$modelName.' / textures.cache / No Update.</p>';
} else { } else {
foreach (array_values(array_unique(array_merge($textures, $texturesNew))) as $v) $texturesMerge[] = json_decode($v, 1); foreach (array_values(array_unique(array_merge($textures, $texturesNew))) as $v) $texturesMerge[] = json_decode($v, 1);
file_put_contents('../model/'.$modelName.'/textures.cache', json_encode($texturesMerge, JSON_UNESCAPED_SLASHES)); file_put_contents('../model/'.$modelName.'/textures.cache', str_replace('\/', '/', json_encode($texturesMerge)));
echo '<p>'.$modelName.' / textures.cache / Updated.</p>'; echo '<p>'.$modelName.' / textures.cache / Updated.</p>';
} }

View File

@ -3,9 +3,11 @@ isset($_GET['id']) ? $id = $_GET['id'] : exit('error');
require '../tools/modelList.php'; require '../tools/modelList.php';
require '../tools/modelTextures.php'; require '../tools/modelTextures.php';
require '../tools/jsonCompatible.php';
$modelList = new modelList(); $modelList = new modelList();
$modelTextures = new modelTextures(); $modelTextures = new modelTextures();
$jsonCompatible = new jsonCompatible();
$id = explode('-', $id); $id = explode('-', $id);
$modelId = (int)$id[0]; $modelId = (int)$id[0];
@ -49,4 +51,4 @@ if (isset($json['expressions'])) {
} }
header("Content-type: application/json"); header("Content-type: application/json");
echo json_encode($json, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); echo $jsonCompatible->json_encode($json);

View File

@ -2,8 +2,10 @@
isset($_GET['id']) ? $modelId = (int)$_GET['id'] : exit('error'); isset($_GET['id']) ? $modelId = (int)$_GET['id'] : exit('error');
require '../tools/modelList.php'; require '../tools/modelList.php';
require '../tools/jsonCompatible.php';
$modelList = new modelList(); $modelList = new modelList();
$jsonCompatible = new jsonCompatible();
$modelList = $modelList->get_list(); $modelList = $modelList->get_list();
@ -14,8 +16,8 @@ while ($modelRandNewId) {
} }
header("Content-type: application/json"); header("Content-type: application/json");
echo json_encode(array('model' => array( echo $jsonCompatible->json_encode(array('model' => array(
'id' => $modelRandId, 'id' => $modelRandId,
'name' => $modelList['models'][$modelRandId-1], 'name' => $modelList['models'][$modelRandId-1],
'message' => $modelList['messages'][$modelRandId-1] 'message' => $modelList['messages'][$modelRandId-1]
)), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); )));

View File

@ -3,16 +3,18 @@ isset($_GET['id']) ? $id = $_GET['id'] : exit('error');
require '../tools/modelList.php'; require '../tools/modelList.php';
require '../tools/modelTextures.php'; require '../tools/modelTextures.php';
require '../tools/jsonCompatible.php';
$modelList = new modelList(); $modelList = new modelList();
$modelTextures = new modelTextures(); $modelTextures = new modelTextures();
$jsonCompatible = new jsonCompatible();
$id = explode('-', $id); $id = explode('-', $id);
$modelId = (int)$id[0]; $modelId = (int)$id[0];
$modelTexturesId = isset($id[1]) ? (int)$id[1] : false; $modelTexturesId = isset($id[1]) ? (int)$id[1] : false;
$modelName = $modelList->id_to_name($modelId); $modelName = $modelList->id_to_name($modelId);
$modelTexturesList = is_array($modelName) ? ['textures' => $modelName] : $modelTextures->get_list($modelName); $modelTexturesList = is_array($modelName) ? array('textures' => $modelName) : $modelTextures->get_list($modelName);
if (count($modelTexturesList['textures']) <= 1) { if (count($modelTexturesList['textures']) <= 1) {
$modelTexturesNewId = 1; $modelTexturesNewId = 1;
@ -26,8 +28,8 @@ if (count($modelTexturesList['textures']) <= 1) {
} }
header("Content-type: application/json"); header("Content-type: application/json");
echo json_encode(array('textures' => array( echo $jsonCompatible->json_encode(array('textures' => array(
'id' => $modelTexturesNewId, 'id' => $modelTexturesNewId,
'name' => $modelTexturesList['textures'][$modelTexturesNewId-1], 'name' => $modelTexturesList['textures'][$modelTexturesNewId-1],
'model' => is_array($modelName) ? $modelName[$modelTexturesNewId-1] : $modelName 'model' => is_array($modelName) ? $modelName[$modelTexturesNewId-1] : $modelName
)), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); )));

View File

@ -2,16 +2,18 @@
isset($_GET['id']) ? $modelId = (int)$_GET['id'] : exit('error'); isset($_GET['id']) ? $modelId = (int)$_GET['id'] : exit('error');
require '../tools/modelList.php'; require '../tools/modelList.php';
require '../tools/jsonCompatible.php';
$modelList = new modelList(); $modelList = new modelList();
$jsonCompatible = new jsonCompatible();
$modelList = $modelList->get_list(); $modelList = $modelList->get_list();
$modelSwitchId = $modelId + 1; $modelSwitchId = $modelId + 1;
if (!isset($modelList['models'][$modelSwitchId-1])) $modelSwitchId = 1; if (!isset($modelList['models'][$modelSwitchId-1])) $modelSwitchId = 1;
header("Content-type: application/json"); header("Content-type: application/json");
echo json_encode(array('model' => array( echo $jsonCompatible->json_encode(array('model' => array(
'id' => $modelSwitchId, 'id' => $modelSwitchId,
'name' => $modelList['models'][$modelSwitchId-1], 'name' => $modelList['models'][$modelSwitchId-1],
'message' => $modelList['messages'][$modelSwitchId-1] 'message' => $modelList['messages'][$modelSwitchId-1]
)), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); )));

View File

@ -3,22 +3,24 @@ isset($_GET['id']) ? $id = $_GET['id'] : exit('error');
require '../tools/modelList.php'; require '../tools/modelList.php';
require '../tools/modelTextures.php'; require '../tools/modelTextures.php';
require '../tools/jsonCompatible.php';
$modelList = new modelList(); $modelList = new modelList();
$modelTextures = new modelTextures(); $modelTextures = new modelTextures();
$jsonCompatible = new jsonCompatible();
$id = explode('-', $id); $id = explode('-', $id);
$modelId = (int)$id[0]; $modelId = (int)$id[0];
$modelTexturesId = isset($id[1]) ? (int)$id[1] : 0; $modelTexturesId = isset($id[1]) ? (int)$id[1] : 0;
$modelName = $modelList->id_to_name($modelId); $modelName = $modelList->id_to_name($modelId);
$modelTexturesList = is_array($modelName) ? ['textures' => $modelName] : $modelTextures->get_list($modelName); $modelTexturesList = is_array($modelName) ? array('textures' => $modelName) : $modelTextures->get_list($modelName);
$modelTexturesNewId = $modelTexturesId == 0 ? 2 : $modelTexturesId + 1; $modelTexturesNewId = $modelTexturesId == 0 ? 2 : $modelTexturesId + 1;
if (!isset($modelTexturesList['textures'][$modelTexturesNewId-1])) $modelTexturesNewId = 1; if (!isset($modelTexturesList['textures'][$modelTexturesNewId-1])) $modelTexturesNewId = 1;
header("Content-type: application/json"); header("Content-type: application/json");
echo json_encode(array('textures' => array( echo $jsonCompatible->json_encode(array('textures' => array(
'id' => $modelTexturesNewId, 'id' => $modelTexturesNewId,
'name' => $modelTexturesList['textures'][$modelTexturesNewId-1], 'name' => $modelTexturesList['textures'][$modelTexturesNewId-1],
'model' => is_array($modelName) ? $modelName[$modelTexturesNewId-1] : $modelName 'model' => is_array($modelName) ? $modelName[$modelTexturesNewId-1] : $modelName
)), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); )));

49
tools/jsonCompatible.php Normal file
View File

@ -0,0 +1,49 @@
<?php class jsonCompatible {
public function json_encode($json) {
if (version_compare(PHP_VERSION,'5.4.0','<')) {
$json = json_encode($json);
$json = str_replace('\/', '/', $json);
$json = preg_replace_callback("/\\\u([0-9a-f]{4})/i", array($this,'json_preg_replace'), $json);
return $this->json_pretty_print($json, ' ');
} else return json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
protected function json_preg_replace($matchs) {
return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
}
protected function json_pretty_print($json, $indent = "\t") {
$result = '';
$indentCount = 0;
$inString = false;
$len = strlen($json);
for ($c = 0; $c < $len; $c++) {
$char = $json[$c];
if ($char === '{' || $char === '[') {
if (!$inString) {
$indentCount++;
if ($char === '[' && $json[$c+1] == "]") $result .= $char . PHP_EOL;
elseif ($char === '{' && $json[$c+1] == "}") $result .= $char . PHP_EOL;
else $result .= $char . PHP_EOL . str_repeat($indent, $indentCount);
} else $result .= $char;
} elseif ($char === '}' || $char === ']') {
if (!$inString) {
$indentCount--;
$result .= PHP_EOL . str_repeat($indent, $indentCount) . $char;
} else $result .= $char;
} elseif ($char === ',') {
if (!$inString) $result .= ',' . PHP_EOL . str_repeat($indent, $indentCount);
else $result .= $char;
} elseif ($char === ':') {
if (!$inString) $result .= ': ';
else $result .= $char;
} elseif ($char === '"') {
if (($c > 0 && $json[$c - 1] !== '\\') || ($c > 1 && $json[$c - 2].$json[$c - 1] === '\\\\')) $inString = !$inString;
$result .= $char;
} else {
$result .= $char;
}
}
return $result;
}
}

View File

@ -12,7 +12,7 @@
$textures = json_decode(file_get_contents('../model/'.$modelName.'/textures.cache'), true); $textures = json_decode(file_get_contents('../model/'.$modelName.'/textures.cache'), true);
} else { } else {
$textures = self::get_textures($modelName); $textures = self::get_textures($modelName);
file_put_contents('../model/'.$modelName.'/textures.cache', json_encode($textures, JSON_UNESCAPED_SLASHES)); if (!empty($textures)) file_put_contents('../model/'.$modelName.'/textures.cache', str_replace('\/', '/', json_encode($textures)));
} return isset($textures) ? array('textures' => $textures) : false; } return isset($textures) ? array('textures' => $textures) : false;
} }
@ -24,13 +24,13 @@
$tmp3 = array(); foreach (glob('../model/'.$modelName.'/'.$textures_dir.'/*') as $n => $m) $tmp3 = array(); foreach (glob('../model/'.$modelName.'/'.$textures_dir.'/*') as $n => $m)
$tmp3['merge'.$n] = str_replace('../model/'.$modelName.'/', '', $m); $tmp3['merge'.$n] = str_replace('../model/'.$modelName.'/', '', $m);
$tmp2 = array_merge_recursive($tmp2, $tmp3); } $tmp2 = array_merge_recursive($tmp2, $tmp3); }
foreach ($tmp2 as $v4) $tmp4[$k][] = json_encode($v4, JSON_UNESCAPED_SLASHES); foreach ($tmp2 as $v4) $tmp4[$k][] = str_replace('\/', '/', json_encode($v4));
$tmp = self::array_exhaustive($tmp, $tmp4[$k]); } $tmp = self::array_exhaustive($tmp, $tmp4[$k]); }
foreach ($tmp as $v) $textures[] = json_decode('['.$v.']', 1); return $textures; foreach ($tmp as $v) $textures[] = json_decode('['.$v.']', 1); return $textures;
} else { } else {
foreach (glob('../model/'.$modelName.'/textures/*') as $v) foreach (glob('../model/'.$modelName.'/textures/*') as $v)
$textures[] = str_replace('../model/'.$modelName.'/', '', $v); $textures[] = str_replace('../model/'.$modelName.'/', '', $v);
return $textures; return empty($textures) ? null : $textures;
} }
} }