public static LuaDecompiler.DecompiledOPCode DoOperator(LuaFile.LuaFunction function, LuaFile.LuaOPCode opCode, string Operator) { LuaDecompiler.DecompiledOPCode str; string returnVal = function.getNewReturnVal(); if (opCode.C > 255) { str = new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.String, String.Format("local {0} = {1} {2} {3}", returnVal, function.Strings[opCode.C - 256].String, Operator, function.Registers[opCode.B])); } else { str = new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.String, String.Format("local {0} = {1} {2} {3}", returnVal, function.Registers[opCode.C], Operator, function.Registers[opCode.B])); } function.Registers[opCode.A] = returnVal; return(str); }
public static LuaDecompiler.DecompiledOPCode CallFunctionWithoutParameters(LuaFile.LuaFunction function, LuaFile.LuaOPCode opCode, int index) { if (opCode.C > 1) { string returnRegisters = opCode.A.ToString(); int oldStartCount = function.returnValCount; string returnStrings = function.getNewReturnVal(); if (opCode.C > 2) { for (int j = opCode.A + 1; j < opCode.A + opCode.C - 1; j++) { returnRegisters += ", " + j.ToString(); string returnVal = function.getNewReturnVal(); returnStrings += ", " + returnVal; //function.Registers[j] = returnVal; } } LuaDecompiler.DecompiledOPCode str = new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.String, String.Format("local {0} = {1}()", returnStrings, function.Registers[opCode.A])); function.returnValCount = oldStartCount; function.Registers[opCode.A] = function.getNewReturnVal(); if (opCode.C > 2) { for (int j = opCode.A + 1; j < opCode.A + opCode.C - 1; j++) { function.Registers[j] = function.getNewReturnVal(); } } return(str); } else { if (function.OPCodes[index + 1].OPCode == 0x2 || function.OPCodes[index + 1].OPCode == 0x4C || function.OPCodes[index + 1].OPCode == 0x16) { if (function.OPCodes[index + 1].B == 0) { function.Registers[opCode.A] = String.Format("{0}()", function.Registers[opCode.A]); return(new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.empty, "")); } } return(new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.String, String.Format("{0}()", function.Registers[opCode.A]))); } }
public static LuaDecompiler.DecompiledOPCode Not(LuaFile.LuaFunction function, LuaFile.LuaOPCode opCode) { string returnVal = function.getNewReturnVal(); LuaDecompiler.DecompiledOPCode str = new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.String, String.Format("local {0} = not {1}", returnVal, function.Registers[opCode.B])); function.Registers[opCode.A] = returnVal; return(str); }
public static LuaDecompiler.DecompiledOPCode DoOperatorBackWards(LuaFile.LuaFunction function, LuaFile.LuaOPCode opCode, string Operator) { string returnVal = function.getNewReturnVal(); LuaDecompiler.DecompiledOPCode str = new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.String, String.Format("local {0} = {1} {2} {3}", returnVal, function.Strings[opCode.B].String, Operator, function.Registers[opCode.C])); function.Registers[opCode.A] = returnVal; return(str); }
private static LuaDecompiler.DecompiledOPCode doCondition(LuaFile.LuaFunction function, int index, string Oper, string OperFalse) { LuaFile.LuaOPCode opCode = function.OPCodes[index]; try { if (orString.Length < 1) { orString = ""; codeBlockStart = -1; } } catch { orString = ""; codeBlockStart = -1; } bool conditionToReg = false; if (function.OPCodes.Count > index + 3) { if (function.OPCodes[index + 2].OPCode == 0xD && function.OPCodes[index + 3].OPCode == 0xD) { conditionToReg = true; } } /*Console.WriteLine("now: " + index); * Console.WriteLine("next: " + (function.OPCodes[index + getSkipLines(function, index + 1) + 1].OPCode));*/ //Console.WriteLine("next: " + isConditioOpCode(function.OPCodes[index + getSkipLines(function, index + 1)].OPCode)); if (isConditioOpCode(function.OPCodes[index + getSkipLines(function, index + 1)].OPCode)) { if (orString.Length < 1) { orString = ""; } string cond = "or"; /*if (index + getSkipLines(function, index + 1) < codeBlockStart) * { * cond = "and"; * opCode.A = (byte)(1 - opCode.A); * } * else * cond = "or";*/ string old = Oper; Oper = OperFalse; OperFalse = old; if (opCode.C > 255) { orString += String.Format("{0} {2} {1} {3} ", function.Registers[opCode.B], function.Strings[opCode.C - 256].getString(), (opCode.A == 0) ? Oper : OperFalse, cond); } else { orString += String.Format("{0} {2} {1} {3} ", function.Registers[opCode.B], function.Registers[opCode.C], (opCode.A == 0) ? Oper : OperFalse, cond); } if (codeBlockStart == -1) { codeBlockStart = index + getSkipLines(function, index + 1); } return(new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.orCondition, orString)); } LuaDecompiler.DecompiledOPCode str; int elseLines = 0; if (function.OPCodes[index + getSkipLines(function, index + 1) + 1].OPCode == 0x1C) { if (!isConditioOpCode(function.OPCodes[index + getSkipLines(function, index + 1) - 2].OPCode)) { elseLines = getSkipLines(function, index + getSkipLines(function, index + 1) + 1); } } if (opCode.C > 255) { if (conditionToReg) { string returnVal = function.getNewReturnVal(); str = new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.conditionToReg, String.Format("local {4} = ({3}{0} {2} {1})", function.Registers[opCode.B], function.Strings[opCode.C - 256].getString(), (opCode.A == 0) ? Oper : OperFalse, orString, returnVal)); function.Registers[function.OPCodes[index + 2].A] = returnVal; } else { str = new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.skipLines, String.Format("if {3}{0} {2} {1} then", function.Registers[opCode.B], function.Strings[opCode.C - 256].getString(), (opCode.A == 0) ? Oper : OperFalse, orString), getSkipLines(function, index + 1), elseLines); } } else { if (conditionToReg) { string returnVal = function.getNewReturnVal(); str = new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.conditionToReg, String.Format("local {4} = ({3}{0} {2} {1})", function.Registers[opCode.B], function.Registers[opCode.C], (opCode.A == 0) ? Oper : OperFalse, orString, returnVal)); function.Registers[function.OPCodes[index + 2].A] = returnVal; } else { str = new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.skipLines, String.Format("if {3}{0} {2} {1} then", function.Registers[opCode.B], function.Registers[opCode.C], (opCode.A == 0) ? Oper : OperFalse, orString), getSkipLines(function, index + 1), elseLines); } } if (orString.Length > 0) { orString = ""; } codeBlockStart = -1; return(str); }
public static LuaDecompiler.DecompiledOPCode CallFunctionWithParameters(LuaFile.LuaFunction function, LuaFile.LuaOPCode opCode, int index, bool tailCall = false) { string funcName = function.Registers[opCode.A]; int parameterCount = opCode.B - 1; int returnValues = opCode.C - 1; if (returnValues < 0) { returnValues = 0; tailCall = true; } string parameterRegisters = ""; string parametersString = ""; if (parameterCount > 0) { parameterRegisters += opCode.A + 1; for (int j = opCode.A + 2; j <= opCode.A + parameterCount; j++) { parameterRegisters += ", " + j; } if (funcName.Contains(":") && opCode.A + 2 <= opCode.A + parameterCount) { parametersString += function.Registers[opCode.A + 2]; for (int j = opCode.A + 3; j <= opCode.A + parameterCount; j++) { parametersString += ", " + function.Registers[j]; } } else { parametersString += function.Registers[opCode.A + 1]; for (int j = opCode.A + 2; j <= opCode.A + parameterCount; j++) { parametersString += ", " + function.Registers[j]; } } } else if (parameterCount != 0) { parameterRegisters += opCode.A + 1; for (int j = opCode.A + 2; j <= function.OPCodes[index - 1].A; j++) { parameterRegisters += ", " + j; } int startpoint = 2; parametersString = function.Registers[opCode.A + 1]; if (funcName.Contains(":")) { parametersString = function.Registers[opCode.A + 2]; startpoint = 3; } for (int j = opCode.A + startpoint; j <= function.OPCodes[index - 1].A; j++) { parametersString += ", " + function.Registers[j]; } } if (returnValues > 0) { function.Registers[opCode.A] = funcName + "(" + parametersString + ")"; string returnRegisters = opCode.A.ToString(); string returnVal = function.getNewReturnVal(); string returnStrings = returnVal; function.Registers[opCode.A] = returnVal; if (returnValues > 1) { for (int j = opCode.A + 1; j < opCode.A + returnValues; j++) { returnRegisters += ", " + j.ToString(); returnVal = function.getNewReturnVal(); returnStrings += ", " + returnVal; function.Registers[j] = returnVal; } } return(new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.String, String.Format("local {0} = {1}({2})", returnStrings, funcName, parametersString))); } else { function.Registers[opCode.A] = funcName + "(" + parametersString + ")"; if (function.OPCodes[index + 1].OPCode == 0x2 || function.OPCodes[index + 1].OPCode == 0x4C || function.OPCodes[index + 1].OPCode == 0x16) { if (function.OPCodes[index + 1].B == 0) { return(new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.empty, "")); } } return(new LuaDecompiler.DecompiledOPCode(LuaDecompiler.opCodeType.String, String.Format("{0}{1}({2})", (tailCall) ? "return " : "", funcName, parametersString))); } }