diff --git a/add/index.php b/add/index.php index 87a05fa85..82b4a0357 100644 --- a/add/index.php +++ b/add/index.php @@ -6,20 +6,24 @@ $modelList = new modelList(); $modelTextures = new modelTextures(); -$modelList = $modelList->get_list()['models']; +$modelList = $modelList->get_list(); +$modelList = $modelList['models']; foreach ($modelList as $modelName) { - if (file_exists('../model/'.$modelName.'/textures.cache')) { + if (!is_array($modelName) && file_exists('../model/'.$modelName.'/textures.cache')) { $textures = $texturesNew = array(); - foreach ($modelTextures->get_list($modelName)['textures'] as $v) $textures[] = json_encode($v, JSON_UNESCAPED_SLASHES); - foreach ($modelTextures->get_textures($modelName) as $v) $texturesNew[] = json_encode($v, JSON_UNESCAPED_SLASHES); + $modelTexturesList = $modelTextures->get_list($modelName); + $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 '

'.$modelName.' / textures.cache / No Update.

'; } else { 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 '

'.$modelName.' / textures.cache / Updated.

'; } diff --git a/get/index.php b/get/index.php index a09b4a211..741c05b69 100644 --- a/get/index.php +++ b/get/index.php @@ -3,9 +3,11 @@ require '../tools/modelList.php'; require '../tools/modelTextures.php'; +require '../tools/jsonCompatible.php'; $modelList = new modelList(); $modelTextures = new modelTextures(); +$jsonCompatible = new jsonCompatible(); $id = explode('-', $id); $modelId = (int)$id[0]; @@ -49,4 +51,4 @@ } header("Content-type: application/json"); -echo json_encode($json, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); +echo $jsonCompatible->json_encode($json); diff --git a/rand/index.php b/rand/index.php index e89f73e9f..5d7f2e7b7 100644 --- a/rand/index.php +++ b/rand/index.php @@ -2,8 +2,10 @@ isset($_GET['id']) ? $modelId = (int)$_GET['id'] : exit('error'); require '../tools/modelList.php'; +require '../tools/jsonCompatible.php'; $modelList = new modelList(); +$jsonCompatible = new jsonCompatible(); $modelList = $modelList->get_list(); @@ -14,8 +16,8 @@ } header("Content-type: application/json"); -echo json_encode(array('model' => array( +echo $jsonCompatible->json_encode(array('model' => array( 'id' => $modelRandId, 'name' => $modelList['models'][$modelRandId-1], 'message' => $modelList['messages'][$modelRandId-1] -)), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); +))); diff --git a/rand_textures/index.php b/rand_textures/index.php index d2978f39d..3123f01aa 100644 --- a/rand_textures/index.php +++ b/rand_textures/index.php @@ -3,16 +3,18 @@ require '../tools/modelList.php'; require '../tools/modelTextures.php'; +require '../tools/jsonCompatible.php'; $modelList = new modelList(); $modelTextures = new modelTextures(); +$jsonCompatible = new jsonCompatible(); $id = explode('-', $id); $modelId = (int)$id[0]; $modelTexturesId = isset($id[1]) ? (int)$id[1] : false; $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) { $modelTexturesNewId = 1; @@ -26,8 +28,8 @@ } header("Content-type: application/json"); -echo json_encode(array('textures' => array( +echo $jsonCompatible->json_encode(array('textures' => array( 'id' => $modelTexturesNewId, 'name' => $modelTexturesList['textures'][$modelTexturesNewId-1], 'model' => is_array($modelName) ? $modelName[$modelTexturesNewId-1] : $modelName -)), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); +))); diff --git a/switch/index.php b/switch/index.php index 8105693aa..5c3334de5 100644 --- a/switch/index.php +++ b/switch/index.php @@ -2,16 +2,18 @@ isset($_GET['id']) ? $modelId = (int)$_GET['id'] : exit('error'); require '../tools/modelList.php'; +require '../tools/jsonCompatible.php'; $modelList = new modelList(); +$jsonCompatible = new jsonCompatible(); $modelList = $modelList->get_list(); $modelSwitchId = $modelId + 1; if (!isset($modelList['models'][$modelSwitchId-1])) $modelSwitchId = 1; header("Content-type: application/json"); -echo json_encode(array('model' => array( +echo $jsonCompatible->json_encode(array('model' => array( 'id' => $modelSwitchId, 'name' => $modelList['models'][$modelSwitchId-1], 'message' => $modelList['messages'][$modelSwitchId-1] -)), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); +))); diff --git a/switch_textures/index.php b/switch_textures/index.php index 0c29000a3..371a0bd10 100644 --- a/switch_textures/index.php +++ b/switch_textures/index.php @@ -3,22 +3,24 @@ require '../tools/modelList.php'; require '../tools/modelTextures.php'; +require '../tools/jsonCompatible.php'; $modelList = new modelList(); $modelTextures = new modelTextures(); +$jsonCompatible = new jsonCompatible(); $id = explode('-', $id); $modelId = (int)$id[0]; $modelTexturesId = isset($id[1]) ? (int)$id[1] : 0; $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; if (!isset($modelTexturesList['textures'][$modelTexturesNewId-1])) $modelTexturesNewId = 1; header("Content-type: application/json"); -echo json_encode(array('textures' => array( +echo $jsonCompatible->json_encode(array('textures' => array( 'id' => $modelTexturesNewId, 'name' => $modelTexturesList['textures'][$modelTexturesNewId-1], 'model' => is_array($modelName) ? $modelName[$modelTexturesNewId-1] : $modelName -)), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); +))); diff --git a/tools/jsonCompatible.php b/tools/jsonCompatible.php new file mode 100644 index 000000000..792bc38e1 --- /dev/null +++ b/tools/jsonCompatible.php @@ -0,0 +1,49 @@ +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; + } +} diff --git a/tools/modelTextures.php b/tools/modelTextures.php index 0e8900701..b9066b5cc 100644 --- a/tools/modelTextures.php +++ b/tools/modelTextures.php @@ -12,7 +12,7 @@ function get_list($modelName) { $textures = json_decode(file_get_contents('../model/'.$modelName.'/textures.cache'), true); } else { $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; } @@ -24,13 +24,13 @@ function get_textures($modelName) { $tmp3 = array(); foreach (glob('../model/'.$modelName.'/'.$textures_dir.'/*') as $n => $m) $tmp3['merge'.$n] = str_replace('../model/'.$modelName.'/', '', $m); $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]); } foreach ($tmp as $v) $textures[] = json_decode('['.$v.']', 1); return $textures; } else { foreach (glob('../model/'.$modelName.'/textures/*') as $v) $textures[] = str_replace('../model/'.$modelName.'/', '', $v); - return $textures; + return empty($textures) ? null : $textures; } }