LUA to PHP converter
Ever since i started playing this game, I've been interested in getting data out of it. I've never really was able to find a LUA to PHP data converter, all though i got pretty close on some website which I don't remember. My found there is the basis of the result i came up with here.
This should only be used on LUA files containing data. Not for scripts.
Be adviced: I use eval() to set the returned array. So if there is php code in the lua file you can risk it will be executed, all though my test show it will most likely just fail instead.
LuaConverter.class.php
class LuaConverter{
private $variables;
private $debug;
function __construct($dbg = false){
$this->variables = null;
$this->debug = $dbg;
}
public function convert($lua_text){
if((explode("\n", $lua_text)) > 0){
$result = array();
$lua_text = preg_replace("/\ -- (.*?)\n/","\n",$lua_text);
$lua_text = str_replace(",\n)", "\n)",
str_replace("{", "array(",
str_replace("}", ");",
str_replace("},", "),",
str_replace("[\"", "\"",
str_replace("] = ", " => ",
str_replace("\t", "",
str_replace("{\n}", "false\n", $lua_text )
)
)
)
)
)
)
);
foreach(explode("\n", $lua_text) as $lua_line){
if($this->debug){print($lua_line."\n");}
$lua_line = chop($lua_line);
if(eregi("^[a-z]", $lua_line)){
if(is_numeric($lua_line[strlen($lua_line)-1]) || $lua_line[strlen($lua_line)-1] == "\""){
$lua_line.=";";
}
if($this->debug){print("variable start: $lua_line\n");}
$result[] = $lua_line;
$this->variables[] = substr($lua_line, 0, strpos($lua_line, " "));
}else{
$result[] = $lua_line;
}
}
}
eval('$return = $'.implode("", $result));
return $return;
}
public function getVariables(){
return $this->variables;
}
public function variableCount(){
return(count($this->variables));
}
}
?>
An example of use:
include('LuaConverter.class.php'); // the class file
//If you are reading and uploaded epgp log file.
$myFile = $_FILES['upload_file']['tmp_name']; //The uploaded file
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
// The actual conversion
$lua = new LuaConverter(); // Creating a new object
$epgp_data = $lua->convert($theData); // converting the data.
The $epgp_data is an array matching the LUA array. Just iterate overit and get the data you need.
If you like this and think i should keep updating it, please make a small (or big) donation.