<?php  

if (file_exists('test_gamefiles.json')) {

	unlink('test_gamefiles.json');
}

$executable_file = 'launcher.exe';
$archive_file = 'test_gamefiles.rar';
$unrar_file = 'UnRAR.exe';

$scanned_files = array();
$total_size = 0;

function scan($dir){

    global $scanned_files;
    global $total_size;

    $main = array_diff(scandir($dir), array('..', '.'));

    if (!count($main))
        return;
    
    foreach($main as $sub) {

        if(is_dir($dir . '/' . $sub)) {

        	scan($dir . '/' . $sub);
        }
        else {

        	$pos = strpos($dir, "/");
        	$normalize_dir = !($pos) ? "" : substr($dir, $pos + 1);

        	$scanned = array("NAME" => $sub, "DIRECTORY" => $normalize_dir, "SIZE" => filesize($dir . '/' . $sub), "HASH" => hash_file("md5", $dir . '/' . $sub), "ADDRESS" => 'https://dl.gcrmp.com/launcher_042023/' . $dir . '/' . $sub, "APPEND" => false);
			$scanned_files["FILES"][] = $scanned;

			$total_size += filesize($dir . '/' . $sub);
        }
    }
}
scan('gamefiles');


$scanned = array("NAME" => $archive_file, "DIRECTORY" => "", "SIZE" => filesize($unrar_file), "HASH" => hash_file("md5", $unrar_file), "ADDRESS" => 'https://dl.gcrmp.com/launcher_042023/' . $unrar_file, "APPEND" => false);

$scanned_files["UNRAR"][] = $scanned;

$scanned = array("NAME" => $executable_file, "DIRECTORY" => "", "SIZE" => filesize($executable_file), "HASH" => hash_file("md5", $executable_file), "ADDRESS" => 'https://dl.gcrmp.com/launcher_042023/' . $executable_file, "APPEND" => false);

$scanned_files["LAUNCHER"] = $scanned;

$scanned = array("NAME" => $archive_file, "DIRECTORY" => "", "SIZE" => filesize($archive_file), "HASH" => hash_file("md5", $archive_file), "ADDRESS" => 'https://dl.gcrmp.com/launcher_042023/' . $archive_file, "APPEND" => false);

$scanned_files["ARCHIVE"][] = $scanned;

$scanned_files["TOTALSIZE"] = $total_size;


$json = fopen('test_gamefiles.json', 'w');

fwrite($json, json_encode($scanned_files));
fclose($json);


echo "generated2 succesfully";

?>
