private void convertButton_Click(object sender, EventArgs e)
{
string agsScript = agsEditWrapper.Text;
SPAGS.ScriptCollection scripts = new SPAGS.ScriptCollection(editor.Version);
scripts.SetStandardConstants(editor.CurrentGame.Settings);
IList<AGS.Types.Script> allHeaders = editor.GetAllScriptHeaders();
scripts.AddHeader(allHeaders[0].FileName, allHeaders[0].Text);
agsTable = new SPAGS.Util.NameDictionary(scripts.GlobalNamespace);
for (int i = 1; i < allHeaders.Count; i++)
{
scripts.AddHeader(allHeaders[i].FileName, allHeaders[i].Text);
}
SPAGS.Script deconstructed;
bool anonFunction = false;
try
{
deconstructed = scripts.CompileScript("ConvertingScript.asc", agsScript);
}
catch
{
try
{
deconstructed = scripts.CompileScript("ConvertingScript.asc", "int __AnonymousFunction__(){" + agsScript + "}");
anonFunction = true;
}
catch
{
deconstructed = null;
anonFunction = false;
}
if (deconstructed == null) throw;
}
StringBuilder luaSB = new StringBuilder();
if (anonFunction)
{
SPAGS.Function func = deconstructed.DefinedFunctions[0];
AddBlockContent(luaSB, 0, func.Body);
}
else
{
foreach (SPAGS.Constant constant in deconstructed.DefinedConstants)
{
luaSB.AppendLine();
SPAGS.Constant.Expression constantExpr = constant as SPAGS.Constant.Expression;
if (constantExpr != null)
{
luaSB.Append(constant.Name + " = ");
AddExpression(luaSB, 0, constantExpr.TheExpression);
luaSB.AppendLine();
}
else if (((SPAGS.Constant.TokenSequence)constant).Tokens.Count == 0)
{
luaSB.AppendLine(constant.Name + " = true");
}
}
foreach (SPAGS.ValueType.Enum enumType in deconstructed.DefinedEnums)
{
luaSB.AppendLine();
luaSB.AppendLine("-- " + enumType.Name);
foreach (SPAGS.EnumValue enumValue in enumType.Entries)
{
luaSB.AppendLine(enumValue.Name + " = " + enumValue.Value);
}
}
foreach (SPAGS.ValueType.Struct structType in deconstructed.DefinedStructs)
{
if (structType.IsManaged) continue;
luaSB.AppendLine();
luaSB.AppendLine(structType.Name + " = ags.struct{");
foreach (SPAGS.StructMember member in structType.Members.EachOf<SPAGS.StructMember>())
{
switch (member.MemberType)
{
case SPAGS.StructMemberType.Attribute:
SPAGS.StructMember.Attribute attr = (SPAGS.StructMember.Attribute)member;
luaSB.Append(" -- ");
if (attr.IsReadOnly) luaSB.Append("readonly ");
if (attr.IsStatic) luaSB.Append("static ");
luaSB.Append("attribute " + attr.Name);
if (attr.IsArray) luaSB.Append("[]");
luaSB.AppendLine();
break;
case SPAGS.StructMemberType.Field:
SPAGS.StructMember.Field field = (SPAGS.StructMember.Field)member;
luaSB.Append(" " + field.Name + " = ");
AddExpression(luaSB, 0, field.Type.CreateDefaultValueExpression());
luaSB.AppendLine(";");
break;
case SPAGS.StructMemberType.Method:
SPAGS.StructMember.Method method = (SPAGS.StructMember.Method)member;
if (!method.IsExtender)
{
luaSB.Append(" -- ");
if (method.IsStatic) luaSB.Append("static ");
luaSB.AppendLine(method.Name + "()");
}
break;
}
}
luaSB.AppendLine("}");
}
foreach (SPAGS.ScriptVariable variable in deconstructed.DefinedVariables)
{
luaSB.AppendLine();
if (!variable.Exported)
{
luaSB.Append("local ");
}
luaSB.Append(variable.Name + " = ");
if (variable.InitialValue != null)
{
AddExpression(luaSB, 0, variable.InitialValue);
}
else
{
AddExpression(luaSB, 0, variable.Type.CreateDefaultValueExpression());
}
luaSB.AppendLine();
}
foreach (SPAGS.Function function in deconstructed.DefinedFunctions)
{
luaSB.AppendLine();
luaSB.Append("function ");
luaSB.Append(function.Name.Replace("::",":"));
luaSB.Append("(");
for (int i = 0; i < function.ParameterVariables.Count; i++)
{
if (i > 0) luaSB.Append(", ");
luaSB.Append(function.ParameterVariables[i].Name);
}
luaSB.AppendLine(")");
AddBlockContent(luaSB, 1, function.Body);
luaSB.AppendLine("end");
}
}
luaOutputWrapper.IsReadOnly = false;
luaOutputWrapper.Text = luaSB.ToString();
luaOutputWrapper.IsReadOnly = true;
agsEditWrapper.Selection.End = agsEditWrapper.Selection.Start = 0;
agsEditWrapper.Scrolling.ScrollToCaret();
}