Downloads containing levelpack_v1.4.3_source.php

Downloads
Name Author Game Mode Rating
Level Packer v1.4.3Featured Download djazz Utility 9.5 Download file

File preview

<?php
echo "LevelPack program by DJazzy of J2NSM\nPHP build 100405 v1.4.3\nCredits to Stijn for the J2L reader!\n\n";

function Pause(){
	$input=fgets(GetStdin(), 8192);
	return substr($input,0,-2);
}
function GetStdin(){
	if (!isset($stdin)) {
		$stdin = fopen('php://stdin', 'r');
	}
	return $stdin;
}
function byteConvert($bytes) { // Good byte-converter function i found on php.net
	$s = array('byte', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb');
	$e = floor(log($bytes)/log(1024));
	if(!$bytes==0) {
		return sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));
	}
	return "0 byte";
}

function new_parse_ini($f) {
// if cannot open file, return false
if (!is_file($f))
    return false;

$ini = file($f);

// to hold the categories, and within them the entries
$cats = array();

foreach ($ini as $i) {
	if(strpos($i,";")===0) continue;
    if (@preg_match('/\[(.+)\]/', $i, $matches)) {
        $last = $matches[1];
    } elseif (@preg_match('/(.+)=(.+)/', $i, $matches)) {
        $cats[$last][intval($matches[1])] = substr($matches[2],0,-1);
    }
}

return $cats;
}
//error_reporting(0);

if($argc==1) {
	echo "Usage:\n  Drag and drop J2L levels on the program\n\nPress enter to close";
	Pause();
	exit;
}

$inipath=dirname($argv[0])."\levelpack.ini";
$settings = @parse_ini_file($inipath,1);

if(!file_exists($inipath) && !$settings) {
	echo "Warning: Couldn't parse the levelpack.ini file, maybe it's missing?\nRead and Save paths is now default\n\n";
	$settings['ReadFrom'][0]="";
	$settings['SaveTo']['path']="";
}
if($settings && $settings['ReadFrom']==null) {
	$settings['ReadFrom'][0]="";
}
if($settings && $settings['SaveTo']==null) {
	$settings['SaveTo']['path']="";
}
$outputType="all";
if($settings && $settings['Settings']['outputType']==null) {
	$settings['Settings']['outputType']="all";
}
else {
	$outputType=strtolower($settings['Settings']['outputType']);
	$outputstr="";
	switch($outputType) {
		case "all": $outputstr="All files";break;
		case "levels": $outputstr="Level files only";break;
		case "levelstilesets": $outputstr="Levels and tilesets only";break;
		case "music": $outputstr="Music only";break;
		default: {
			$outputType="all";
			$outputstr="Invalid value, output set to All files";
			break;
		}
	}
}

$file=null;
$noJ2L=true;
$gotLL=false;
while(list($nr,$val)=each($argv)) {
	if($nr>0) {
		if(strpos(strtolower($val),'.j2l') > -1) {$file[] = strtolower($val);$noJ2L=false;}
		elseif(strpos(strtolower($val),'levellist.ini') > -1) {$gotLL=true;$LLfile=strtolower($val);}
	}
}
if($noJ2L==true && $gotLL==false) {
	echo "You must specify a j2l file!\n\nPress enter to close";
	Pause();
	exit;
}
elseif($gotLL) {
	echo "Reading Level List...\n";
	echo $LLfile."\n\n";
	$LL = new_parse_ini($LLfile);
	echo "Please specify gamemode: ";
	$gm=Pause();
	echo "\n";
	$keys=array_keys($LL);
	$key="";
	for($i=0;$i < count($keys);$i++) {
		if(strtolower($keys[$i])==strtolower($gm)) {
			$key=$keys[$i];
			break;
		}
	}
	if($key=="") {
		echo "Specified gamemode was not found!\n\nClick enter to close";
		Pause();
		exit;
	}
	echo "Gamemode: ".$key."\n";
	$file=null;
	$err=false;
	$tmperr=true;
	$notfound=null;
	for($i=0;$i < count($LL[$key]);$i++) {
		$tmperr=true;
		if(file_exists(dirname($LLfile)."\\".$LL[$key][$i+1])) {
			$file[]=dirname($LLfile)."\\".$LL[$key][$i+1];
			$tmperr=false;
			continue;
		}
		
		for($j=1;$j <= count($settings['ReadFrom']); $j++) {
			if(file_exists($settings['ReadFrom'][$j]."\\".$LL[$key][$i+1])) {
				$file[]=$settings['ReadFrom'][$j]."\\".$LL[$key][$i+1];
				$tmperr=false;
				break;
			}
		}
		
		if($tmperr) {
			$notfound[]=$LL[$key][$i+1];
			$err=true;
		}
		
	}
	if($err) {
		echo "\nFollowing levels was not found:\n\n";
		for($i=0;$i < count($notfound);$i++) {
			echo "  ".$notfound[$i]."\n";
		}
		echo "\nPress enter to continue anyway";
		Pause();
	}
	
	echo "\n";
	if($file==null) {
		echo "Error: No files was found!\n\nPress enter to close";
		Pause();
		exit;
	}
	$settings['Settings']['includeNextLevels']=false;
	
}

if(strtolower($settings['Settings']['includeNextLevels'])==true) {
	echo "Including next-levels...\n\n";
}

$filename=null;
$nextL=null;
$tileset=null;
$music=null;
$tilesetExists=null;
$musicExists=null;
$nextLExists=null;
$levelVersion=null;
$tilesetName=null;
$tilesetVersion=null;
$path="";
include "classZip.php";
include "classLevel.php";
for($key=0;$key < count($file); $key++) {
	if(strpos(strtolower(basename($file[$key])),".j2l") === false) continue;
	$l=construct($file[$key]);
	$filename[$key]=$file[$key];
	$path=$filename[$key];
	$tileset[$key]=$l['Tileset'];
	$music[$key]=$l['MusicFile'];
	$nextL[$key]=$l['NextLevel'];
	if(strpos(strtolower($nextL[$key]),".j2l") === false && $nextL[$key]!="") $nextL[$key].=".j2l";
	$tilesetExists[$key]=false;
	$musicExists[$key]=false;
	$nextLExists[$key]=false;
	$levelVersion[$key]=($sVersion==514)?"1.23":"TSF";
	echo "Level:   ".basename($filename[$key])." - ".$l['LevelName']." - ".$levelVersion[$key]."\n";
	if(strpos($music[$key],".") === false && $music[$key]!="") $music[$key].=".j2b";
	
	for($i=1;$i <= count($settings['ReadFrom']); $i++) {
		if($settings['ReadFrom'][$i]!="" && substr($settings['ReadFrom'][$i],-1)!="\\") $settings['ReadFrom'][$i].="\\";
		if($tilesetExists[$key]==null && file_exists($settings['ReadFrom'][$i]."".$tileset[$key])) $tilesetExists[$key]=$settings['ReadFrom'][$i]."".$tileset[$key];
		if($music[$key]!="") {
			if($musicExists[$key]==null && file_exists($settings['ReadFrom'][$i]."".$music[$key])) $musicExists[$key]=$settings['ReadFrom'][$i]."".$music[$key];
		}
		if($nextL[$key]!="") {
			if($nextLExists[$key]==null && file_exists($settings['ReadFrom'][$i]."".$nextL[$key])) $nextLExists[$key]=$settings['ReadFrom'][$i]."".$nextL[$key];
		}
	}
	
	echo "Tileset: ".(($tilesetExists[$key])?"yes":"no ")." (".$tileset[$key].")";
	if($tilesetExists[$key]) {
		$fp = @fopen($tilesetExists[$key],"r");
		if($fp==null) {echo " - Error reading";}
		else {
			$header = fread($fp, 262);
			$tilesetName[$key] = trim(substr($header,188,32));
			list(,$ver) = unpack('v',substr($header,220,2));
			$tilesetVersion[$key]=($ver==512)?"1.23":"TSF";
			echo " - ".$tilesetName[$key]." - ".$tilesetVersion[$key];
			fclose($fp);
			
		}
		unset($fp);
	}
	echo "\n";
	if($music[$key]!="") {
		echo "Music:   ".(($musicExists[$key])?"yes":"no ")." (".$music[$key].")\n";
	}
	if($nextL[$key]!="" && $nextLExists[$key] && strtolower($nextL[$key])!=strtolower(basename($filename[$key])) && strtolower($settings['Settings']['includeNextLevels'])==true) {
		echo "Next:    ".(($nextLExists[$key])?"yes":"no ")." (".$nextL[$key].")\n";
		$nextNotExists=true;
		for($k=0;$k < count($file);$k++) {
			if(strpos(strtolower($file[$k]),strtolower($nextL[$key])) !== FALSE) {
				$nextNotExists=false;
				
				
			}
		}
		if($nextNotExists) {array_push($file,$nextLExists[$key]);}
	}
	else
		$nextLExists[$key]=true;
	
	echo "\n";
	$l=null;
}
$tilesetError=false;
if(in_array(false,$musicExists)) {
	echo "Following files was not found:\n\n";
	if(in_array(false,$tilesetExists)) {
		echo " Tileset files:\n";
		for($key=0;$key < count($tilesetExists); $key++) {
			if($tilesetExists[$key]==false) echo "  ".$tileset[$key]."\n";
		}
		echo "\n";
		$tilesetError=true;
	}
	if(in_array(false,$musicExists)) {
		echo " Music files:\n";
		for($key=0;$key < count($musicExists); $key++) {
			if($musicExists[$key]==false) echo "  ".$music[$key]."\n";
		}
		echo "\n";
	}
if($tilesetError) {
		echo "You must find these before you can pack them.";
		Pause();
		exit;
}
}

	
	echo "Output files: ".$outputstr."\n\n";
	echo "What do you want to call the pack? (max 32) ";
	$name=substr(Pause(),0,32);
	if($name=="") {
		echo "\nYou must enter a name!\n\nPress enter to close";
		Pause();
		exit;
	}
	echo "\nName: ".$name.".zip\nTrying to copy files...\n";
	
	$exists=(file_exists($name.".zip") || file_exists($settings['SaveTo']['path']."/".$name.".zip"));
	$overwrite=true;
	if($exists) {
		echo "Archive already exists! Do you want to overwrite? y/n: ";
		$overwrite=(strtolower(substr(Pause(),0,1))=="y")? true: false;
		echo "\n";
	}
	if($overwrite) {
		$ziper = new zipfile();
	
	$addedSets=array();
	$addedMusic=array();
	
	$read=false;
	
	for($key=0;$key < count($file); $key++) {
		if($outputType=="all" || $outputType=="levels" || $outputType=="levelstilesets") {
			$ziper->addFile(fread(fopen($filename[$key],"r"),filesize($filename[$key])),basename($filename[$key]),filemtime($filename[$key]));
			echo " added ".basename($filename[$key])."\n";
		}
		if(($outputType=="all" || $outputType=="tilesets" || $outputType=="levelstilesets") && !in_array($tileset[$key],$addedSets)) {
			$read=false;
			if($read=@fread(@fopen($tilesetExists[$key],"r"),@filesize($tilesetExists[$key]))) {
				$ziper->addFile($read,$tileset[$key],filemtime($tilesetExists[$key]));
			}
			if($read!=false && $read!="")
				echo " added ".$tileset[$key]."\n";
			else {
				echo " failed to add ".$tileset[$key]."\n";
			}
		}
		if(($outputType=="all" || $outputType=="music") && $music[$key]!="" && !in_array($music[$key],$addedMusic)) {
			$read=false;
			if($read=@fread(@fopen($musicExists[$key],"r"),@filesize($musicExists[$key]))) {
				$ziper->addFile($read,$music[$key],filemtime($musicExists[$key]));
			}
			if($read!=false && $read!="")
				echo " added ".$music[$key]."\n";
			else {
				echo "! failed to add ".$music[$key]."\n";
			}
		}
		$addedSets[$key]=$tileset[$key];
		$addedMusic[$key]=$music[$key];
	}
	echo "\nCreating archive...";
	$check=false;
	$outpath="";
	if($settings['SaveTo']['path']!="" && is_dir($settings['SaveTo']['path'])) {
		$check=@$ziper->output($settings['SaveTo']['path']."/".$name.".zip");
		$outpath=$settings['SaveTo']['path']."/".$name.".zip";
		echo " done\n\n";
	}
	else {
		$outpath=$name.".zip";
		echo "\n\nWarning: SaveTo folder does not exist!\nZIP file will be saved to the folder where the levels are.\n\n";
		$check=@$ziper->output($name.".zip");
	}
	
	if($check) {
		echo "ZIP archive was created successfully!\nFilesize: ".byteConvert(filesize(realpath($outpath)))."\n\nYou can close this window now";
		exec("explorer /select,".realpath($outpath));
	}
	else
	echo "There was an error while copying, please report the bug to Dany!";
	}
	else {
		echo "Operation was canceled!\nYou can close this window now";
	}
	

Pause();
?>