Exemple #1
0
        private bool GenerateVariables(IndentedTextWriter output, bool showInfo)
        {
            if (_scripts.Variables is null || _scripts.Variables.Count == 0)
            {
                return(false);
            }

            WriteComment("VARIABLES", output);
            output.WriteLine();

            for (int counter = 0; counter < _scripts.Variables.Count; counter++)
            {
                ScriptGlobal variable = _scripts.Variables[counter];
                output.Write("(variable {0} {1} ", _opcodes.GetTypeInfo((ushort)variable.Type).Name, variable.Name);
                WriteExpression(variable.ExpressionIndex, output);
                if (showInfo)
                {
                    output.WriteLine(")\t\t; Index: {0}, Expression Index: {1}", counter.ToString(), variable.ExpressionIndex.Index.ToString());
                }
                else
                {
                    output.WriteLine(")");
                }
            }

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Writes the globals to the output.
        /// </summary>
        /// <param name="output">The output.</param>
        /// <param name="showInfo">If set to true, writes additional information to the decompiled text.</param>
        /// <returns>Returns true if globals were written to the output.</returns>
        private bool WriteGlobals(IndentedTextWriter output, bool showInfo)
        {
            if (_scripts.Globals is null || _scripts.Globals.Count == 0)
            {
                return(false);
            }

            // write Header.
            WriteComment(output, "GLOBALS");
            output.WriteLine();

            for (int i = 0; i < _scripts.Globals.Count; i++)
            {
                var glo = _scripts.Globals[i];

                // write global declaration.
                output.Write("(global {0} {1} ", _opcodes.GetTypeInfo((ushort)glo.Type).Name, glo.Name);

                // write value.
                FollowRootIndex(output, glo.ExpressionIndex, false, BranchType.Call);

                // write additional Information.
                if (showInfo)
                {
                    output.WriteLine(")\t\t; Index: {0}, Expression Index: {1}", i, glo.ExpressionIndex.Index.ToString());
                }
                else
                {
                    output.WriteLine(")");
                }
            }

            return(true);
        }
        private void GenerateCode(ScriptExpression expression, IndentedTextWriter output, bool firstrun = false)
        {
            int  firstIndentedArg = int.MaxValue;
            bool isFunctionCall   = false;

            if (expression.Type == ScriptExpressionType.Expression || expression.Type == ScriptExpressionType.Expression4)
            {
                ScriptValueType type = _opcodes.GetTypeInfo((ushort)expression.ReturnType);
                if (type.Name == "function_name")
                {
                    isFunctionCall = true;

                    if (!_nextFunctionIsScript)
                    {
                        ScriptFunctionInfo info = _opcodes.GetFunctionInfo(expression.Opcode);
                        if (info != null)
                        {
                            if (info.Name.StartsWith("begin"))
                            {
                                firstIndentedArg = 0;
                                if (expression.LineNumber > 0 && !_onNewLine)
                                {
                                    output.Indent++;
                                    output.WriteLine();
                                }
                            }
                            else if (info.Name == "if")
                            {
                                firstIndentedArg = 1;
                            }
                        }
                    }
                    if (expression.LineNumber > 0)
                    {
                        output.Write("(");
                    }
                }
            }



            bool wroteAnything = wroteAnything = HandleExpression(expression, output);
            int  startIndent   = output.Indent;

            int currentArg = 0;

            if (_h4 && firstrun)
            {
                firstIndentedArg = 0;
                currentArg       = 1;
                _h4 = false;
            }

            ScriptExpression sibling = expression.Next;

            while (sibling != null)
            {
                if (wroteAnything && !_nextExpressionIsVar)
                {
                    if (currentArg == firstIndentedArg)
                    {
                        output.Indent++;
                    }
                    if (currentArg >= firstIndentedArg)
                    {
                        output.WriteLine();
                        _onNewLine = true;
                    }
                    else if (output.Indent != startIndent)
                    {
                        output.WriteLine();
                        _onNewLine = true;
                    }
                    else
                    {
                        output.Write(" ");
                    }
                }

                if (!_nextExpressionIsVar)
                {
                    wroteAnything = HandleExpression(sibling, output);
                }
                else if ((_nextExpressionIsVar && sibling.Opcode != 0xFFFF))
                {
                    if (!_varTypeWritten)
                    {
                        ScriptValueType type = _opcodes.GetTypeInfo((ushort)sibling.ReturnType);
                        output.Write(type.Name + " var_" + localVarCounter.ToString() + " ");
                        _varTypeWritten = true;
                    }

                    wroteAnything = HandleExpression(sibling, output);
                }

                sibling = sibling.Next;
                currentArg++;
            }

            if (isFunctionCall && expression.LineNumber > 0)
            {
                if (output.Indent != startIndent)
                {
                    output.Indent = startIndent;
                    if (wroteAnything)
                    {
                        output.WriteLine();
                    }
                    output.Write(")");
                    _onNewLine = true;
                }
                else
                {
                    output.Write(")");
                }
            }
        }
        private void GenerateCode(ScriptExpression expression, IndentedTextWriter output)
        {
            int  firstIndentedArg = int.MaxValue;
            bool isFunctionCall   = false;

            if (expression.Type == ScriptExpressionType.Expression)
            {
                ScriptValueType type = _opcodes.GetTypeInfo((ushort)expression.ReturnType);
                if (type.Name == "function_name")
                {
                    isFunctionCall = true;

                    if (!_nextFunctionIsScript)
                    {
                        ScriptFunctionInfo info = _opcodes.GetFunctionInfo(expression.Opcode);
                        if (info != null)
                        {
                            if (info.Name.StartsWith("begin"))
                            {
                                firstIndentedArg = 0;
                                if (expression.LineNumber > 0 && !_onNewLine)
                                {
                                    output.Indent++;
                                    output.WriteLine();
                                }
                            }
                            else if (info.Name == "if")
                            {
                                firstIndentedArg = 1;
                            }
                        }
                    }
                    if (expression.LineNumber > 0)
                    {
                        output.Write("(");
                    }
                }
            }

            bool wroteAnything = HandleExpression(expression, output);
            int  startIndent   = output.Indent;

            int currentArg           = 0;
            ScriptExpression sibling = expression.Next;

            while (sibling != null)
            {
                if (wroteAnything)
                {
                    if (currentArg == firstIndentedArg)
                    {
                        output.Indent++;
                    }
                    if (currentArg >= firstIndentedArg || output.Indent != startIndent)
                    {
                        output.WriteLine();
                        _onNewLine = true;
                    }
                    else
                    {
                        output.Write(" ");
                    }
                }

                wroteAnything = HandleExpression(sibling, output);
                sibling       = sibling.Next;
                currentArg++;
            }

            if (isFunctionCall && expression.LineNumber > 0)
            {
                if (output.Indent != startIndent)
                {
                    output.Indent = startIndent;
                    if (wroteAnything)
                    {
                        output.WriteLine();
                    }
                    output.Write(")");
                    _onNewLine = true;
                }
                else
                {
                    output.Write(")");
                }
            }
        }