logo
Main

Forums

Downloads

Unreal-Netiquette

Donate for Oldunreal:
Donate

borderline

Links to our wiki:
Wiki

Walkthrough

Links

Tutorials

Unreal Reference

Usermaps

borderline

Contact us:
Submit News
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic PHP - Rename extracted files to original name (Read 2393 times)
Sweden
New Member
*
Offline


Oldunreal member

Posts: 3
Location: Sweden
Joined: Jun 13th, 2012
Gender: Male
PHP - Rename extracted files to original name
Mar 28th, 2013 at 7:50pm
Print Post  
OK, I wrote a PHP script a long time ago that will rename all extracted files
from a *.u file from uED or UCC to its original filename, so you might be able
to update a script that you dont have the original source for anymore, without
to much boring manual work.

Not sure if anyone will have any use for it, but I hope so.
Let me know if you find any problems with it or if something is missing from it.

Save the file as uRenamer.php and run:
php.exe uRenamer.php [directory, such as UnrealI in your Unreal directory]

Code
Select All
<?php
/*
Script to rename all extracted files from a .u file to its original filename to make it
possible to compile again.

TODO:
-Write some code to remove all "unused" files that are present in the class directory
 (Can break ability to compile other stuff that might rely on that file to exist,
  example UnrealShare rely on some files in UnrealI folder, and reverse)
-External packages
-(PACKAGE instead of GROUP?)

rev.1
*/

if(!isset($argv[1])) {
	echo "Usage:\n";
	echo "php fixExec2.php [directory]\n";
	exit();
} else {
	$directory = $argv[1] . "\\";
	$classdir = "Classes\\";
}

$type = array(
	"AUDIO" => "Sounds",
	"TEXTURE" => "Textures",
	"MESH" => "Models",
	"FONT" => "Textures"
);
$renames = array(); //put all the renames in an array...

if($handle = opendir($directory . $classdir)) {
    while(false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
			$line = file($directory . $classdir . $entry);

			foreach($line as $key => $value) {
				if(preg_match('/^(#exec\s+(AUDIO|MESH|TEXTURE|FONT))\s+IMPORT/is', $value, $matches)) { //is an import line
					$currentLineElements = preg_split("/(\\s)/", $value, NULL, PREG_SPLIT_NO_EMPTY); //split on any space (Flare.uc)

					foreach($currentLineElements as $key2 => $value2) {
						$tempvar = explode("=", $value2);
						$tempvar[0] = trim($tempvar[0]);

						if(count($tempvar) > 1) { //if there was an = in the element...
							$tempvar[1] = trim($tempvar[1], '"'); //trim away anyway quotes "
							$currentLineElements[strtoupper($tempvar[0])] = $tempvar[1];
							unset($currentLineElements[$key2]);
						}
					} //each element

					$filetype = $type[strtoupper($currentLineElements[1])];
					$isfont = (strtoupper($currentLineElements[1]) == "FONT");
					$is3d = (isset($currentLineElements["ANIVFILE"], $currentLineElements["DATAFILE"]));

					if(isset($currentLineElements['MESH'])) {
						$name = "\\" . $currentLineElements['MESH'];
					} else if(isset($currentLineElements['NAME'])) {
						$name = "\\" . $currentLineElements['NAME'];
					} else { //why no mesh nor name??? :( (RainGen.uc)
						if($is3d) {
							$name = "\\" . strstr(basename($currentLineElements['MESH']), ".", true);
						} else {
							$name = "\\" . strstr(basename($currentLineElements['FILE']), ".", true);
						}
					}

					$group = (isset($currentLineElements['GROUP']) && $currentLineElements['GROUP'] != "None") ? "\\" . $currentLineElements['GROUP'] : "";

					if($is3d) { //3d
						$extension = substr(strrchr($currentLineElements["ANIVFILE"], '.'), 1);

						$anivfile = $currentLineElements["ANIVFILE"];
						$datafile = $currentLineElements["DATAFILE"];

						$oldfilename = $filetype . $group . $name . "_a." . $extension; //3d
						$renames[$oldfilename] = $anivfile;

						$oldfilename = $filetype . $group . $name . "_d." . $extension; //3d
						$renames[$oldfilename] = $datafile;
					} else {
						$extension = substr(strrchr($currentLineElements["FILE"], '.'), 1);

						$filename = $currentLineElements["FILE"];

						if($isfont) { //extra code for font support
							if($handle2 = opendir($directory . $filetype . $name)) {
								while(false !== ($entry2 = readdir($handle2))) {
									if($entry2 != "." && $entry2 != "..") {
										if(substr(strrchr($entry2, "."), 1) == $extension) {
											$group = $name;
											$name = "\\" . strstr($entry2, ".", true);
										}
									}
								}
								closedir($handle2);
							}
						}

						$oldfilename = $filetype . $group . $name . "." . $extension; //texture,sound,font
						$renames[$oldfilename] = $filename;
					}

					//$line[$key] = implode(" ", $currentLineElements);
				} else if(preg_match('/^(#exec\s+OBJ\s+LOAD)/is', $value, $matches)) { //is an import line
					//TODO: load obj
				}
			} //each line
        }
    }
    closedir($handle);
}

foreach($renames as $key => $value) { //loop the array and do the rename
	$key = $directory . $key; //add directory to key
	$value = $directory . $value; //add directory to value

	if(file_exists($key)) {
		if(!file_exists($value)) {
			$currentDirectory = dirname($value);

			if(!file_exists($currentDirectory)) {
				mkdir($currentDirectory, 0777, true);
			}

			copy($key, $value); //copy because some files might be identical but in more than one place
		} else {
			echo "Warning, file " . $value . " already exists.\n"; //file already existed
		}
	} else {
		echo "Warning, file " . $key . " dosnt exist.\n";
	}
}
 

  
Back to top
 
IP Logged
 
BobIsUnreal
God Member
*****
Offline


gonna be dead soon, gg

Posts: 764
Joined: Apr 12th, 2010
Gender: Male
Re: PHP - Rename extracted files to original name
Reply #1 - Mar 28th, 2013 at 9:42pm
Print Post  
i wrote a script 2 years ago to fix all the import statements  specifically the texnum offset issues  and the filename dosnt match import anamiation path issues when i exported all the xdecoration stuff ,
if this is what i think it is it probably would have been handy to have then...
  

[REM_img]http://i.imgur.com/V3OSsDu.gif.disable[/img]http://i.imgur.com/JOu38Gs.gif
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint
Bookmarks: del.icio.us Digg Facebook Google Google+ Linked in reddit StumbleUpon Twitter Yahoo