Skip to content

Commit

Permalink
Added res fixes, improved debug logging, add error tile, fix collapse.
Browse files Browse the repository at this point in the history
Fixed error conditions in res.php, such as starting from an empty file set.
Also fixed having a not inified copied cache registry file.
Helped during this process, and hopefully for any similar future errors where PHP would fail early during the process, creating incomplete data.

Added new debug logging mode and converted to using it.
Hopefully more convenient and automatic, especially for res.php.
Fixed loading some scripts when renaming paths.
Added library function for text file counting debug code.

Added error tile config.  To help clarify tile dev more.

Fixed using collapse config properly.
Code organizing.
  • Loading branch information
Pysis868 committed Apr 18, 2023
1 parent be1c814 commit 1efcf42
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 181 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ lostPasswordSubject="Tingle - Password Reset"
lostPasswordBodyTemplateFilePath="content/lostPasswordEmailBodyTemplate.txt"

[INTERNAL]
;debugLoggingMode=errorLog
debugLoggingMode=errorLog
;debugLoggingMode=output
;debugLoggingMode=buffer
;debugLoggingMode=return
12 changes: 6 additions & 6 deletions javascript.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ scripts/widgets/AccountButton.js
scripts/widgets/HeaderBar.js
scripts/widgets/Logo.js
scripts/controls/InfoBox.js
scripts/controls/infoBox/Location.js
scripts/controls/infoBox/location/Center.js
scripts/controls/infoBox/location/Bounds.js
scripts/controls/InfoBox/Location.js
scripts/controls/InfoBox/Location/Center.js
scripts/controls/InfoBox/Location/Bounds.js
scripts/services/SearcherFuse.js
scripts/handlers/SearchMarkerHandler.js
scripts/handlers/ChangelogHandler.js
Expand All @@ -42,6 +42,6 @@ scripts/widgets/GameButton.js
scripts/widgets/MapButton.js
scripts/widgets/MapsMenu.js
scripts/widgets/CategoryButtonMap.js
scripts/controls/infoBox/Mouse.js
scripts/controls/infoBox/mouse/Move.js
scripts/controls/infoBox/mouse/ClickHist.js
scripts/controls/InfoBox/Mouse.js
scripts/controls/InfoBox/Mouse/Move.js
scripts/controls/InfoBox/Mouse/ClickHist.js
15 changes: 15 additions & 0 deletions lib/common/data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
// Source: https://stackoverflow.com/a/2162528/1091943
function getLineCount($file) {
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}

fclose($handle);

return $linecount;
}
?>
6 changes: 5 additions & 1 deletion lib/common/log.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@

include_once(__DIR__."/../../config.php");

$debugLog = '';

function debug_log($message) {
global $debugLoggingMode;
global $debugLoggingMode, $debugLog;
$formattedMessage = 'DEBUG: '.$message."\n";

if($debugLoggingMode == 'errorLog') {
error_log($formattedMessage);
} elseif ($debugLoggingMode == 'output') {
print($formattedMessage);
} elseif ($debugLoggingMode == 'buffer') {
$debugLog .= $formattedMessage;
} elseif ($debugLoggingMode == 'return') {
return $formattedMessage;
}
Expand Down
194 changes: 134 additions & 60 deletions res.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,68 @@
<?php
include_once(__DIR__.'/config.php');
include_once(__DIR__.'/lib/common/data.php');
include_once(__DIR__.'/lib/common/log.php');

$commentRegex = '/^\s*\/\//';

$output = '';
$debugOutput = '';

## Library Functions

function checkIfUpdateNecessaryBySourceFiles($cacheRegistryData) {
global $debugOutput;
global $commentRegex;

// $debugOutput .= debug_log('checkIfUpdateNecessaryBySourceFiles START');
// $debugOutput .= debug_log('count(cacheRegistryData): '.count($cacheRegistryData));
// debug_log('checkIfUpdateNecessaryBySourceFiles START');
$cacheRegistryDataCount = count($cacheRegistryData);
// debug_log("cacheRegistryDataCount: $cacheRegistryDataCount");
// // debug_log("cacheRegistryData : ".var_export($cacheRegistryData, true)); // Big
if($cacheRegistryDataCount = 0) {
// debug_log('Cache registry file empty, or could not be parsed as INI data; returning...');
return false;
}

foreach($cacheRegistryData as $resFile => $time) {
// $debugOutput .= debug_log("resfile: $resFile");
// debug_log("resfile: $resFile");

$isCommented = preg_match($commentRegex, $resFile);
$fileExists = file_exists($resFile);
if($isCommented || !$fileExists) {
// $debugOutput .= debug_log("isCommented: " .var_export($isCommented, true));
// $debugOutput .= debug_log("fileExists: " .var_export($fileExists, true));
// debug_log("isCommented: " .var_export($isCommented, true));
// debug_log("fileExists: " .var_export($fileExists, true));

// $debugOutput .= debug_log('checkIfUpdateNecessaryBySourceFiles continue');
// debug_log('checkIfUpdateNecessaryBySourceFiles continue');
continue;
}

if($time != filemtime($resFile)) {
// $debugOutput .= debug_log('checkIfUpdateNecessaryBySourceFiles return');
// debug_log('checkIfUpdateNecessaryBySourceFiles return');
return true;
}
}
// $debugOutput .= debug_log('checkIfUpdateNecessaryBySourceFiles END');
// debug_log('checkIfUpdateNecessaryBySourceFiles END');
}

function inifyFileToFile($fromFile, $toFile) {
$fromFileData = file_get_contents($fromFile);
$fromFileDataInified = preg_replace('/\n/', "=\n", $fromFileData);
file_put_contents("$toFile", $fromFileDataInified);
}

// $debugOutput .= debug_log("__FILE__ : ".__FILE__);
// $debugOutput .= debug_log("__DIR__ : ".__DIR__ );


## Initialization

$commentRegex = '/^\s*\/\//';

if(!isset($_GET["type"])) {

}

// debug_log("__FILE__ : ".__FILE__);
// debug_log("__DIR__ : ".__DIR__ );



## Validation and more init

if(!isset($_GET["type"])) {
print("A type must be provided! javascript or css.");
Expand All @@ -48,7 +75,7 @@ function checkIfUpdateNecessaryBySourceFiles($cacheRegistryData) {
$type != "javascript"
&& $type != "css"
) {
$output .= "Invalid type! ($type)";
$output = "Invalid type! ($type)";
return;
}
$mtype = "text/$type";
Expand All @@ -58,91 +85,138 @@ function checkIfUpdateNecessaryBySourceFiles($cacheRegistryData) {
$ext = ".js";
}

// $debugOutput .= debug_log("type : $type " );
// $debugOutput .= debug_log("mtype : $mtype" );
// $debugOutput .= debug_log("ext : $ext" );
// debug_log("type : $type " );
// debug_log("mtype : $mtype" );
// debug_log("ext : $ext" );

header("Content-Type: $mtype");

$localRegistryFile = "$type.txt";
$localRegistryFileExists = file_exists("$localRegistryFile");
$localRegistryFileMTime = filemtime("$localRegistryFile");
$cacheRegistryFile = "$cacheFolder/$localRegistryFile";
$cacheRegistryFileExists = file_exists("$cacheRegistryFile");
$cacheRegistryFileMTime = filemtime("$cacheRegistryFile");
$cacheDestFile = "$cacheFolder/index$ext";
$cacheDestFileExists = file_exists("$cacheDestFile");
$cacheDestFileMTime = filemtime("$cacheDestFile");

// $debugOutput .= debug_log("localRegistryFile : $localRegistryFile" );
// $debugOutput .= debug_log("localRegistryFileExists: $localRegistryFileExists" );
// $debugOutput .= debug_log("localRegistryFileMTime : $localRegistryFileMTime" );
// $debugOutput .= debug_log("cacheRegistryFile : $cacheRegistryFile" );
// $debugOutput .= debug_log("cacheRegistryFileExists: $cacheRegistryFileExists" );
// $debugOutput .= debug_log("cacheRegistryFileMTime : $cacheRegistryFileMTime" );
// $debugOutput .= debug_log("cacheDestFile : $cacheDestFile" );
// $debugOutput .= debug_log("cacheDestFileExists : $cacheDestFileExists" );
// $debugOutput .= debug_log("cacheDestFileMTime : $cacheDestFileMTime" );
$localRegistryFileExists = file_exists("$localRegistryFile");
if($localRegistryFileExists) {
$localRegistryFileMTime = filemtime("$localRegistryFile");
$localRegistryFileLC = getLineCount("$localRegistryFile");
} else {
$localRegistryFileMTime = false;
$localRegistryFileLC = false;
}
$cacheRegistryFile = "$cacheFolder/$localRegistryFile";
$cacheRegistryFileExists = file_exists("$cacheRegistryFile");
if($cacheRegistryFileExists) {
$cacheRegistryFileMTime = filemtime("$cacheRegistryFile");
$cacheRegistryFileLC = getLineCount("$cacheRegistryFile");
} else {
$cacheRegistryFileMTime = false;
$cacheRegistryFileLC = false;
}
$cacheDestFile = "$cacheFolder/index$ext";
$cacheDestFileExists = file_exists("$cacheDestFile");
if($cacheDestFileExists) {
$cacheDestFileMTime = filemtime("$cacheDestFile");
$cacheDestFileLC = getLineCount("$cacheDestFile");
} else {
$cacheDestFileMTime = false;
$cacheDestFileLC = false;
}

// debug_log("localRegistryFile : $localRegistryFile" );
// debug_log("localRegistryFileExists: $localRegistryFileExists" );
// debug_log("localRegistryFileMTime : $localRegistryFileMTime" );
// debug_log("localRegistryFileLC : $localRegistryFileLC" );
// debug_log("cacheRegistryFile : $cacheRegistryFile" );
// debug_log("cacheRegistryFileExists: $cacheRegistryFileExists" );
// debug_log("cacheRegistryFileMTime : $cacheRegistryFileMTime" );
// debug_log("cacheRegistryFileLC : $cacheRegistryFileLC" );
// debug_log("cacheDestFile : $cacheDestFile" );
// debug_log("cacheDestFileExists : $cacheDestFileExists" );
// debug_log("cacheDestFileMTime : $cacheDestFileMTime" );
// debug_log("cacheDestFileLC : $cacheDestFileLC" );

// exit('Debug forced stop: '."\n".$debugLog); // Debug



## Update category checks

$update = false;
// $debugOutput .= debug_log("update : ".var_export($update, true));
// debug_log("update : ".var_export($update, true));
if(!$cacheRegistryFileExists) {
// $debugOutput .= debug_log('Source files are prompting an update.');
// debug_log('Registry file not currectly cached; copying over, inifying, and prompting an update...');
$update = true;
copy("$localRegistryFile", "$cacheRegistryFile")
or die("Error: Cache file problem with: $cacheRegistryFile");

inifyFileToFile(
$localRegistryFile,
$cacheRegistryFile
);
}

// exit('Debug forced stop: '."\n".$debugLog); // Debug

if(
!$update
&& isset($_GET['update'])
&& (strtolower($_GET['update']) === "true")
) {
// $debugOutput .= debug_log('Resource request is explicitly prompting an update.');
// debug_log('Resource request is explicitly prompting an update.');
$update = true;
}
if(!$update && $localRegistryFileMTime > $cacheRegistryFileMTime) {
// $debugOutput .= debug_log('Local registry file having changes being more recent are prompting an update.');

if(
!$update
&& $localRegistryFileMTime > $cacheRegistryFileMTime
) {
// debug_log('Local registry file having changes being more recent are prompting an update.');
$update = true;

$localRegistryFileData = file_get_contents($localRegistryFile);
$localRegistryFileDataInified = preg_replace('/\n/', "=\n", $localRegistryFileData);
file_put_contents("$cacheRegistryFile", $localRegistryFileDataInified);
inifyFileToFile(
$localRegistryFile,
$cacheRegistryFile
);

// $debugOutput .= debug_log("copy(\"$localRegistryFile\", \"$cacheRegistryFile\")");
// debug_log("copy(\"$localRegistryFile\", \"$cacheRegistryFile\")");
// copy("$localRegistryFile", "$cacheRegistryFile")
// or die("Error: Cache file problem with: $cacheRegistryFile");
}

$cacheRegistryData = parse_ini_file("$cacheRegistryFile");
$cacheRegistryDataLinesCount = count($cacheRegistryData);
// $debugOutput .= debug_log("cacheRegistryDataLinesCount: $cacheRegistryDataLinesCount");
// $debugOutput .= debug_log("cacheRegistryData: ".var_export($cacheRegistryData, true)); // Big
$cacheRegistryData = parse_ini_file("$cacheRegistryFile" );
$cacheRegistryDataCount = count ( $cacheRegistryData );
// debug_log("cacheRegistryDataCount : $cacheRegistryDataCount");
if($cacheRegistryDataCount == 0) {
// debug_log('Cache registry file still empty, or could not be parsed as INI data; removing...');
unlink($cacheRegistryFile);
}

if(!$update) {
if(checkIfUpdateNecessaryBySourceFiles($cacheRegistryData)) {
// $debugOutput .= debug_log('Source files are prompting an update.');
// debug_log('Source files are prompting an update.');
$update = true;
}
}
// $debugOutput .= debug_log("update : ".var_export($update, true));
// debug_log("update : ".var_export($update, true));

# Update the minified cache file.
// exit('Debug forced stop: '."\n\n".$debugLog); // Debug



## Update the minified cache file.
if($update) {
// $debugOutput .= debug_log('Updating cached file...');
// $debugOutput .= debug_log('getcwd: '.getcwd());
// debug_log('Updating cached file...');
// debug_log('getcwd: '.getcwd());

include __DIR__."/lib/minify.php";

$output = "/* index$ext */\n";
foreach($cacheRegistryData as $resFile => $time) {
// $debugOutput .= debug_log("resFile: $resFile");
// debug_log("resFile: $resFile");
$isCommented = preg_match($commentRegex, $resFile);
if($isCommented) {
// $debugOutput .= debug_log('Commented file; skipping...');
// debug_log('Commented file; skipping...');
continue;
}
$resFilePath = __DIR__."/$resFile";
// $debugOutput .= debug_log("resFilePath: $resFilePath");
// debug_log("resFilePath: $resFilePath");
if(!file_exists($resFilePath)) {
$output .= "/* $resFile doesn't exist */\n";

Expand All @@ -152,10 +226,10 @@ function checkIfUpdateNecessaryBySourceFiles($cacheRegistryData) {
$resFileData = file_get_contents($resFilePath);
if($minify) {
if($type == "javascript") {
// $debugOutput .= debug_log('Minifying javascript...');
// debug_log('Minifying javascript...');
$resFileData = minify_js($resFileData);
} else {
// $debugOutput .= debug_log('Minifying css...');
// debug_log('Minifying css...');
$resFileData = minify_css($resFileData);
}
}
Expand All @@ -166,15 +240,15 @@ function checkIfUpdateNecessaryBySourceFiles($cacheRegistryData) {
# Save and send the minified file.
file_put_contents("$cacheDestFile", $output);

# Store timestamps for freshly-chached resource files in the registry file.
# Store timestamps for freshly-cached resource files in the registry file.
$regData = "";
foreach($cacheRegistryData as $resFile=>$time) {
$regData .= "$resFile=$time\n";
}
file_put_contents("$cacheRegistryFile", $regData);
}

$output .= $debugOutput;
$output = $debugLog;
$output .= file_get_contents("$cacheDestFile");
$output .= "\n";
header("X-Updated: ".(($update) ? 'true' : 'false'));
Expand Down
15 changes: 8 additions & 7 deletions scripts/util/ZConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ ZConfig = {
// Main config set-up with defaults

// "exact", "focus" (ZU default)
ZConfig.setConfig("categorySelectionMethod", "focus");
ZConfig.setConfig("tilesBaseURL" , "https://zeldamaps.com/tiles/");
ZConfig.setConfig("tileAxisDirectories" , 'false');
ZConfig.setConfig("tileZoomDirectories" , 'false');
ZConfig.setConfig("categorySelectionMethod" , "focus" );
ZConfig.setConfig("tilesBaseURL" , "https://zeldamaps.com/tiles/");
ZConfig.setConfig("tileAxisDirectories" , 'false' );
ZConfig.setConfig("tileZoomDirectories" , 'false' );
ZConfig.setConfig("tileNameFormat" , (
(ZConfig.getConfig("tileAxisDirectories") == 'true')
? '{z}/{x}/{y}'
: (ZConfig.getConfig("tileZoomDirectories") == 'true')
? '{z}/{x}_{y}'
: '{z}_{x}_{y}'
));
ZConfig.setConfig("markerClusters" , 'false');
ZConfig.setConfig("showInfoControls", 'false');
ZConfig.setConfig("collapsed", 'false');
ZConfig.setConfig("markerClusters" , 'false' );
ZConfig.setConfig("showInfoControls" , 'false' );
ZConfig.setConfig("collapsed" , 'false' );
ZConfig.setConfig("errorTileUrl" , '' );
Loading

0 comments on commit 1efcf42

Please sign in to comment.