public override void CaseAFieldDecl(AFieldDecl node)
 {
     if (node.GetStatic() != null)
         return;
     writer.WriteLine(TypeToString(node.GetType()) + " " + node.GetName().Text + ";");
     if (Fields.Any(decl => decl.GetName().Text == node.GetName().Text))
     {
         return;
     }
     Fields.Add(node);
     //node.SetInit(null);
     node.Parent().RemoveChild(node);
 }
 public VariableDescription(AFieldDecl fieldDecl)
 {
     Name = fieldDecl.GetName().Text;
     Type = Util.TypeToString(fieldDecl.GetType());
     PlacementPrefix = "Field";
     VariableType = VariableTypes.Field;
     Const = fieldDecl.GetConst() != null;
     IsStatic = fieldDecl.GetStatic() != null;
     Visibility = fieldDecl.GetVisibilityModifier();
     realType = (PType)fieldDecl.GetType().Clone();
     init = fieldDecl.GetInit();
     Line = fieldDecl.GetName().Line;
     Position = TextPoint.FromCompilerCoords(fieldDecl.GetName());
 }
 public override void CaseAFieldDecl(AFieldDecl node)
 {
     if (node.GetType() is ANamedType)
     {
         ANamedType type = (ANamedType)node.GetType();
         if (finalTrans.data.StructTypeLinks.ContainsKey(type))
         {
             AStructDecl strDecl = finalTrans.data.StructTypeLinks[type];
             if (strDecl.GetLocals().Cast<PLocalDecl>().Select(decl => decl is AALocalDecl).Count() == 0)
             {
                 node.Parent().RemoveChild(node);
                 return;
             }
         }
     }
     base.CaseAFieldDecl(node);
 }
        public override void CaseAFieldDecl(AFieldDecl node)
        {
            if (node.GetConst() == null)
                return;

            initialFieldDecl = node;

            if (IsConstant(node.GetInit()))
            {
                List<AFieldLvalue> lvalues = new List<AFieldLvalue>();
                lvalues.AddRange(data.FieldLinks.Where(link => link.Value == node).Select(link => link.Key));
                foreach (AFieldLvalue lvalue in lvalues)
                {
                    PExp parent = (PExp)lvalue.Parent();
                    parent.ReplaceBy(Util.MakeClone(node.GetInit(), data));
                }
                node.Parent().RemoveChild(node);
            }

            initialFieldDecl = null;
        }
Пример #5
0
 ArrayList New152()
 {
     ArrayList nodeList = new ArrayList();
     ArrayList nodeArrayList5 = (ArrayList) Pop();
     ArrayList nodeArrayList4 = (ArrayList) Pop();
     ArrayList nodeArrayList3 = (ArrayList) Pop();
     ArrayList nodeArrayList2 = (ArrayList) Pop();
     ArrayList nodeArrayList1 = (ArrayList) Pop();
     PVisibilityModifier pvisibilitymodifierNode2 = (PVisibilityModifier)nodeArrayList1[0];
     PType ptypeNode5 = (PType)nodeArrayList2[0];
     TIdentifier tidentifierNode6 = (TIdentifier)nodeArrayList3[0];
     PExp pexpNode7 = (PExp)nodeArrayList4[0];
     AFieldDecl pdeclNode1 = new AFieldDecl (
       pvisibilitymodifierNode2,
       null,
       null,
       ptypeNode5,
       tidentifierNode6,
       pexpNode7
     );
     nodeList.Add(pdeclNode1);
     return nodeList;
 }
        public override void OutAFieldDecl(AFieldDecl node)
        {
            if (node.GetInit() != null)
            {
                PType from = data.ExpTypes[node.GetInit()];
                PType to = node.GetType();
                if (!Assignable(from, to))
                {
                    errors.Add(new ErrorCollection.Error(node.GetName(), currentSourceFile,
                                                         LocRM.GetString("ErrorText151") + Util.TypeToString(from) +
                                                         LocRM.GetString("ErrorText152") + Util.TypeToString(to)));
                }
            }

            //If the return type or the type of any formals is a private struct, and the method is a public context, give an error
            {
                //Is public context
                if (node.GetVisibilityModifier() is APublicVisibilityModifier)
                {
                    PType type = node.GetType();
                    FindPrivateTypes finder = new FindPrivateTypes(data);

                    type.Apply(finder);

                    if (finder.PrivateTypes.Count > 0)
                    {
                        List<ErrorCollection.Error> subErrors = new List<ErrorCollection.Error>();
                        List<PDecl> usedDecls = new List<PDecl>();
                        foreach (ANamedType namedType in finder.PrivateTypes)
                        {
                            if (data.StructTypeLinks.ContainsKey(namedType))
                            {
                                AStructDecl decl = data.StructTypeLinks[namedType];
                                if (usedDecls.Contains(decl))
                                    continue;
                                usedDecls.Add(decl);
                                subErrors.Add(new ErrorCollection.Error(decl.GetName(), LocRM.GetString("ErrorText64")));
                            }
                            else if (data.DelegateTypeLinks.ContainsKey(namedType))
                            {
                                AMethodDecl decl = data.DelegateTypeLinks[namedType];
                                if (usedDecls.Contains(decl))
                                    continue;
                                usedDecls.Add(decl);
                                subErrors.Add(new ErrorCollection.Error(decl.GetName(), LocRM.GetString("ErrorText154")));
                            }
                        }

                        errors.Add(new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText155"), false, subErrors.ToArray()));
                    }
                }
            }

            base.OutAFieldDecl(node);
        }
        public override void OutAAProgram(AAProgram node)
        {
            if (strings.Count == 0)
                return;

            //Obfuscate all strings
            List<string> obfuscated = new List<string>();
            foreach (AStringConstExp stringConstExp in strings)
            {
                TStringLiteral token = stringConstExp.GetStringLiteral();
                string s = token.Text.Substring(1, token.Text.Length - 2);
                obfuscated.Add(Obfuscate(s));
            }

            //Set invokes instead of string constants, and move varaiabes down
            List<AFieldDecl> ignoredFields = new List<AFieldDecl>();
            List<AFieldDecl> moveFieldsIn = new List<AFieldDecl>();
            Dictionary<AFieldDecl, AMethodDecl> fieldMethods = new Dictionary<AFieldDecl, AMethodDecl>();
            for (int i = 0; i < strings.Count; i++)
            {
                AStringConstExp stringExp = strings[i];
                Token token = stringExp.GetStringLiteral();
                bool inDeobfuscator = Util.GetAncestor<AMethodDecl>(stringExp) == finalTrans.data.DeobfuscateMethod;

                if (inDeobfuscator)
                {
                    AFieldDecl field = finalTrans.data.UnobfuscatedStrings[stringExp];

                    AStringConstExp newStringConst = new AStringConstExp(stringExp.GetStringLiteral());

                    field.SetInit(newStringConst);

                    AFieldLvalue fieldRef = new AFieldLvalue(new TIdentifier(field.GetName().Text, token.Line, token.Pos));
                    finalTrans.data.FieldLinks[fieldRef] = field;

                    stringExp.ReplaceBy(new ALvalueExp(fieldRef));
                }
                else
                {
                    AFieldDecl field;
                    if (!finalTrans.data.ObfuscatedStrings.ContainsKey(stringExp))
                    {
                        int line = -finalTrans.data.ObfuscatedStrings.Count - 1;
                        field = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const", line, 0),
                                                new ANamedType(new TIdentifier("string", line, 1), null),
                                                new TIdentifier("Galaxy_pp_stringO" +
                                                                finalTrans.data.ObfuscatedStrings.Count), null);
                        //If the strings are the same - point them to same field
                        bool newField = true;
                        foreach (AStringConstExp oldStringConstExp in finalTrans.data.ObfuscatedStrings.Keys)
                        {
                            if (stringExp.GetStringLiteral().Text == oldStringConstExp.GetStringLiteral().Text)
                            {
                                field = finalTrans.data.ObfuscatedStrings[oldStringConstExp];
                                newField = false;
                                break;
                            }
                        }
                        if (newField)
                        {
                            AASourceFile file = (AASourceFile)finalTrans.data.DeobfuscateMethod.Parent();
                            file.GetDecl().Insert(file.GetDecl().IndexOf(finalTrans.data.DeobfuscateMethod) + 1, field);

                            finalTrans.data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, field));
                        }
                        finalTrans.data.ObfuscatedStrings.Add(stringExp, field);

                    }
                    field = finalTrans.data.ObfuscatedStrings[stringExp];
                    string obfuscatedString = obfuscated[i];

                    ASimpleInvokeExp invoke = new ASimpleInvokeExp();
                    invoke.SetName(new TIdentifier(finalTrans.data.DeobfuscateMethod.GetName().Text,
                                                   stringExp.GetStringLiteral().Line,
                                                   stringExp.GetStringLiteral().Pos));

                    AStringConstExp newStringConst =
                        new AStringConstExp(new TStringLiteral("\"" + obfuscatedString + "\""));
                    invoke.GetArgs().Add(newStringConst);
                    finalTrans.data.SimpleMethodLinks[invoke] = finalTrans.data.DeobfuscateMethod;

                    if (Util.GetAncestor<PStm>(stringExp) == null && false)
                    {
                        ignoredFields.Add(field);
                        /*if (Util.GetAncestor<ASimpleInvokeExp>(stringExp) == null)
                            stringExp.ReplaceBy(invoke);*/
                        //Add obfuscate call to this location);
                        continue;
                        /*ASimpleInvokeExp invoke = new ASimpleInvokeExp();
                        invoke.SetName(new TIdentifier(finalTrans.data.DeobfuscateMethod.GetName().Text,
                                                       stringExp.GetStringLiteral().Line,
                                                       stringExp.GetStringLiteral().Pos));

                        AStringConstExp newStringConst =
                            new AStringConstExp(new TStringLiteral("\"" + obfuscatedString + "\""));
                        invoke.GetArgs().Add(newStringConst);
                        stringExp.ReplaceBy(invoke);

                        finalTrans.data.SimpleMethodLinks[invoke] = finalTrans.data.DeobfuscateMethod;
                        continue;*/
                    }

                    if (field.GetInit() == null)
                    {
                        /*field.SetInit(invoke);
                        field.SetConst(null);*/

                        if (
                            stringExp.GetStringLiteral().Text.Remove(0, 1).Substring(0,
                                                                                     stringExp.GetStringLiteral().Text.
                                                                                         Length - 2) == "")
                        {
                            //Make method
                            /*
                                string <field>Method()
                                {
                                    return "";
                                }
                             *
                             */
                            ANullExp nullExp = new ANullExp();

                            field.SetInit(nullExp);
                            field.SetConst(null);

                            AStringConstExp stringConst = new AStringConstExp(new TStringLiteral("\"\""));
                            AMethodDecl method = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                                 new ANamedType(new TIdentifier("string"), null),
                                                                 new TIdentifier("Get" + field.GetName()),
                                                                 new ArrayList(),
                                                                 new AABlock(
                                                                     new ArrayList()
                                                                         {
                                                                             new AValueReturnStm(new TReturn("return"),
                                                                                                 stringConst)
                                                                         },
                                                                     new TRBrace("}")));

                            AASourceFile pFile = (AASourceFile)field.Parent();
                            pFile.GetDecl().Insert(pFile.GetDecl().IndexOf(field) + 1, method);
                            finalTrans.data.ExpTypes[stringConst] = new ANamedType(new TIdentifier("string"), null);
                            finalTrans.data.ExpTypes[nullExp] = new ANamedType(new TIdentifier("null"), null);

                            fieldMethods[field] = method;
                        }
                        else
                        {
                            //Make method
                            /*
                                string <field>Method()
                                {
                                    if (field == null)
                                    {
                                        field = Invoke;
                                    }
                                    if (field == null)
                                    {
                                        return Invoke;
                                    }
                                    return field;
                                }
                             */

                            ANullExp nullExp1 = new ANullExp();

                            field.SetInit(nullExp1);
                            field.SetConst(null);

                            ANullExp nullExp2 = new ANullExp();
                            AFieldLvalue fieldRef1 = new AFieldLvalue(new TIdentifier(field.GetName().Text));
                            AFieldLvalue fieldRef2 = new AFieldLvalue(new TIdentifier(field.GetName().Text));
                            AFieldLvalue fieldRef3 = new AFieldLvalue(new TIdentifier(field.GetName().Text));
                            ALvalueExp fieldRef1Exp = new ALvalueExp(fieldRef1);
                            ALvalueExp fieldRef3Exp = new ALvalueExp(fieldRef3);
                            ABinopExp binop1 = new ABinopExp(fieldRef1Exp, new AEqBinop(new TEq("==")), nullExp2);
                            AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), fieldRef2, invoke);

                            AIfThenStm ifStm1 = new AIfThenStm(new TLParen("("), binop1,
                                                              new ABlockStm(new TLBrace("{"),
                                                                            new AABlock(
                                                                                new ArrayList()
                                                                                    {
                                                                                        new AExpStm(new TSemicolon(";"),
                                                                                                    assignment)
                                                                                    },
                                                                                new TRBrace("}"))));

                            /*ANullExp nullExp3 = new ANullExp();
                            AFieldLvalue fieldRef4 = new AFieldLvalue(new TIdentifier(field.GetName().Text));
                            ALvalueExp fieldRef4Exp = new ALvalueExp(fieldRef4);
                            AStringConstExp invokeArgClone =
                                new AStringConstExp(new TStringLiteral("\"" + obfuscatedString + "\""));
                            ASimpleInvokeExp invokeClone = new ASimpleInvokeExp(new TIdentifier(invoke.GetName().Text),
                                                                                new ArrayList() { invokeArgClone });
                            finalTrans.data.SimpleMethodLinks[invokeClone] = finalTrans.data.DeobfuscateMethod;
                            ABinopExp binop2 = new ABinopExp(fieldRef4Exp, new AEqBinop(new TEq("==")), nullExp3);

                            AIfThenStm ifStm2 = new AIfThenStm(new TLParen("("), binop2,
                                                              new ABlockStm(new TLBrace("{"),
                                                                            new AABlock(
                                                                                new ArrayList()
                                                                                    {
                                                                                        new AValueReturnStm(new TReturn("return"), invokeClone)
                                                                                    },
                                                                                new TRBrace("}"))));*/

                            AMethodDecl method = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                                 new ANamedType(new TIdentifier("string"), null),
                                                                 new TIdentifier("Get" + field.GetName()),
                                                                 new ArrayList(),
                                                                 new AABlock(
                                                                     new ArrayList()
                                                                         {
                                                                             ifStm1,
                                                                             //ifStm2,
                                                                             new AValueReturnStm(new TReturn("return"),
                                                                                                 fieldRef3Exp)
                                                                         },
                                                                     new TRBrace("}")));
                            AASourceFile pFile = (AASourceFile) field.Parent();
                            pFile.GetDecl().Insert(pFile.GetDecl().IndexOf(field) + 1, method);

                            finalTrans.data.FieldLinks[fieldRef1] =
                                finalTrans.data.FieldLinks[fieldRef2] =
                                finalTrans.data.FieldLinks[fieldRef3] =
                                /*finalTrans.data.FieldLinks[fieldRef4] = */field;
                            finalTrans.data.LvalueTypes[fieldRef1] =
                                finalTrans.data.LvalueTypes[fieldRef2] =
                                finalTrans.data.LvalueTypes[fieldRef3] =
                                //finalTrans.data.LvalueTypes[fieldRef4] =
                                finalTrans.data.ExpTypes[fieldRef1Exp] =
                                finalTrans.data.ExpTypes[fieldRef3Exp] =
                                //finalTrans.data.ExpTypes[fieldRef4Exp] =
                                finalTrans.data.ExpTypes[assignment] = field.GetType();

                            finalTrans.data.ExpTypes[nullExp1] =
                                finalTrans.data.ExpTypes[nullExp2] =
                                /*finalTrans.data.ExpTypes[nullExp3] =*/ new ANamedType(new TIdentifier("null"), null);
                            finalTrans.data.ExpTypes[binop1] =
                                /*finalTrans.data.ExpTypes[binop2] = */new ANamedType(new TIdentifier("bool"), null);

                            fieldMethods[field] = method;
                        }

                        /* AFieldLvalue fieldRef = new AFieldLvalue(new TIdentifier(field.GetName().Text, token.Line, token.Pos));
                         finalTrans.data.FieldLinks[fieldRef] = field;*/

                        //stringExp.ReplaceBy(new ALvalueExp(fieldRef));
                    }
                    ASimpleInvokeExp invoke2 =
                            new ASimpleInvokeExp(new TIdentifier(fieldMethods[field].GetName().Text), new ArrayList());
                    finalTrans.data.SimpleMethodLinks[invoke2] = fieldMethods[field];
                    stringExp.ReplaceBy(invoke2);

                    //If we are in a field, move it in
                    if (Util.GetAncestor<AFieldDecl>(invoke2) != null)
                        moveFieldsIn.Add(Util.GetAncestor<AFieldDecl>(invoke2));
                }
            }

            foreach (AFieldDecl field in finalTrans.data.ObfuscationFields)
            {
                if (field.GetInit() == null && field.Parent() != null)
                {
                    field.Parent().RemoveChild(field);
                }
            }

            //A constant field, or a field used by a constant field cannot be moved in
            List<AFieldDecl> constantFields = new List<AFieldDecl>();
            foreach (SharedData.DeclItem<AFieldDecl> field in finalTrans.data.Fields)
            {
                if (field.Decl.GetConst() != null)
                    constantFields.Add(field.Decl);
            }
            for (int i = 0; i < constantFields.Count; i++)
            {
                GetFieldLvalues lvalues = new GetFieldLvalues();
                constantFields[i].Apply(lvalues);
                foreach (AFieldLvalue lvalue in lvalues.Lvalues)
                {
                    AFieldDecl field = finalTrans.data.FieldLinks[lvalue];
                    if (!constantFields.Contains(field))
                        constantFields.Add(field);
                }
            }
            moveFieldsIn.RemoveAll(constantFields.Contains);
            Dictionary<AFieldDecl, List<AFieldDecl>> dependancies = new Dictionary<AFieldDecl, List<AFieldDecl>>();
            //Order the fields so any dependancies are instansiated first
            foreach (AFieldDecl field in moveFieldsIn)
            {
                dependancies.Add(field, new List<AFieldDecl>());
                GetFieldLvalues lvalues = new GetFieldLvalues();
                field.Apply(lvalues);
                foreach (AFieldLvalue lvalue in lvalues.Lvalues)
                {
                    AFieldDecl dependancy = finalTrans.data.FieldLinks[lvalue];
                    if (!dependancies[field].Contains(dependancy))
                        dependancies[field].Add(dependancy);
                }
            }
            List<PStm> newStatements = new List<PStm>();
            while (dependancies.Keys.Count > 0)
            {
                AFieldDecl field = dependancies.FirstOrDefault(f1 => f1.Value.Count == 0).Key ??
                                   dependancies.Keys.First(f => true);

                AFieldLvalue fieldRef = new AFieldLvalue(new TIdentifier(field.GetName().Text));
                AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), fieldRef, field.GetInit());
                field.SetInit(null);

                newStatements.Add(new AExpStm(new TSemicolon(";"), assignment));

                finalTrans.data.FieldLinks[fieldRef] = field;
                finalTrans.data.LvalueTypes[fieldRef] =
                    finalTrans.data.ExpTypes[assignment] = field.GetType();

                foreach (KeyValuePair<AFieldDecl, List<AFieldDecl>> dependancy in dependancies)
                {
                    if (dependancy.Value.Contains(field))
                        dependancy.Value.Remove(field);
                }

                dependancies.Remove(field);
            }
            AABlock initBody = (AABlock) finalTrans.mainEntry.GetBlock();
            for (int i = newStatements.Count - 1; i >= 0; i--)
            {
                initBody.GetStatements().Insert(0, newStatements[i]);
            }
        }
 public FieldItem(AFieldDecl fieldDecl, Item parent, List<Item> children)
     : base(parent, children)
 {
     FieldDecl = fieldDecl;
 }
 public FieldDecl(AFieldDecl decl)
 {
     Decl = decl;
     if (Name.StartsWith("_"))
         Name = "u" + Name;
 }
Пример #10
0
 public override void CaseAFieldDecl(AFieldDecl node)
 {
     InAFieldDecl(node);
     if (node.GetInit() != null)
     {
         node.GetInit().Apply(this);
     }
     if (node.GetName() != null)
     {
         node.GetName().Apply(this);
     }
     if (node.GetType() != null)
     {
         node.GetType().Apply(this);
     }
     if (node.GetConst() != null)
     {
         node.GetConst().Apply(this);
     }
     if (node.GetStatic() != null)
     {
         node.GetStatic().Apply(this);
     }
     if (node.GetVisibilityModifier() != null)
     {
         node.GetVisibilityModifier().Apply(this);
     }
     OutAFieldDecl(node);
 }
Пример #11
0
 public virtual void CaseAFieldDecl(AFieldDecl node)
 {
     DefaultCase(node);
 }
Пример #12
0
 ArrayList New150()
 {
     ArrayList nodeList = new ArrayList();
     ArrayList nodeArrayList5 = (ArrayList) Pop();
     ArrayList nodeArrayList4 = (ArrayList) Pop();
     ArrayList nodeArrayList3 = (ArrayList) Pop();
     ArrayList nodeArrayList2 = (ArrayList) Pop();
     ArrayList nodeArrayList1 = (ArrayList) Pop();
     PVisibilityModifier pvisibilitymodifierNode2 = (PVisibilityModifier)nodeArrayList1[0];
     TConst tconstNode4 = (TConst)nodeArrayList2[0];
     PType ptypeNode5 = (PType)nodeArrayList3[0];
     TIdentifier tidentifierNode6 = (TIdentifier)nodeArrayList4[0];
     AFieldDecl pdeclNode1 = new AFieldDecl (
       pvisibilitymodifierNode2,
       null,
       tconstNode4,
       ptypeNode5,
       tidentifierNode6,
       null
     );
     nodeList.Add(pdeclNode1);
     return nodeList;
 }
Пример #13
0
 public override void OutAFieldDecl(AFieldDecl node)
 {
     bool isAConstStringVar = data.ObfuscationFields.Contains(node);/* data.ObfuscatedStrings.Values.Any(stringField => stringField == node) ||
                              data.UnobfuscatedStrings.Values.Any(stringField => stringField == node);*/
     if (!isAConstStringVar && node.GetConst() != null && node.GetInit() == null)
         errors.Add(new ErrorCollection.Error(node.GetConst(), currentSourceFile, LocRM.GetString("ErrorText183"), false));
     //If it's protected, it must be in a struct
     if (!Util.HasAncestor<AStructDecl>(node))
     {
         if (node.GetVisibilityModifier() is AProtectedVisibilityModifier)
             errors.Add(new ErrorCollection.Error(node.GetName(),
                                                  LocRM.GetString("ErrorText184")));
     }
     base.OutAFieldDecl(node);
 }
Пример #14
0
 ArrayList New149()
 {
     ArrayList nodeList = new ArrayList();
     ArrayList nodeArrayList5 = (ArrayList) Pop();
     ArrayList nodeArrayList4 = (ArrayList) Pop();
     ArrayList nodeArrayList3 = (ArrayList) Pop();
     ArrayList nodeArrayList2 = (ArrayList) Pop();
     ArrayList nodeArrayList1 = (ArrayList) Pop();
     PVisibilityModifier pvisibilitymodifierNode2 = (PVisibilityModifier)nodeArrayList1[0];
     TStatic tstaticNode3 = (TStatic)nodeArrayList2[0];
     PType ptypeNode5 = (PType)nodeArrayList3[0];
     TIdentifier tidentifierNode6 = (TIdentifier)nodeArrayList4[0];
     AFieldDecl pdeclNode1 = new AFieldDecl (
       pvisibilitymodifierNode2,
       tstaticNode3,
       null,
       ptypeNode5,
       tidentifierNode6,
       null
     );
     nodeList.Add(pdeclNode1);
     return nodeList;
 }
        private void CompileThread()
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(Form1.Language);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(Form1.Language);
            try
            {
                if (!compilingFromCommandLine)
                    form.SetStatusText(LocRM.GetString("GC_Text3"));

                if (!compilingFromCommandLine)
                    ClearErrorWindow();
                //Build a tree with all sourcefiles
                AAProgram root = new AAProgram();
                ErrorCollection errors = new ErrorCollection();
                currentErrorCollection = errors;
                if (!compilingFromCommandLine)
                    errors.ErrorAdded += errors_ErrorAdded;
                bool addedDeobfuscator = false;
                SharedData sharedData = new SharedData();
                sharedData.AllowPrintouts = !compilingFromCommandLine;
                //Parse project files
                List<string> fileNames = new List<string>();
                List<string> sources = new List<string>();
                foreach (
                    FileItem sourceFile in Form1.GetSourceFiles(ProjectProperties.CurrentProjectPropperties.SrcFolder))
                {
                    if (sourceFile.Deactivated)
                        continue;
                    StreamReader reader = sourceFile.File.OpenText();

                    string filename = sourceFile.File.FullName;
                    //Remove c:/.../projectDir/src
                    filename = filename.Remove(0, (ProjectDir.FullName + "/src/").Length);
                    //Remove .galaxy++
                    filename = filename.Remove(filename.Length - ".galaxy++".Length);
                    fileNames.Add(filename);
                    sources.Add(reader.ReadToEnd());
                    reader.Close();
                    continue;

                    Parser parser = new Parser(new Lexer(reader));
                    try
                    {
                        Start start = parser.Parse();
                        AASourceFile sourceNode = (AASourceFile) start.GetPSourceFile();
                        reader.Close();
                        reader = sourceFile.File.OpenText();
                        int lineCount = 0;
                        while (reader.ReadLine() != null)
                        {
                            lineCount++;
                        }
                        reader.Close();
                        sharedData.LineCounts[sourceNode] = lineCount;

                        //Extract encryption function
                       /* {
                            AASourceFile file = (AASourceFile) start.GetPSourceFile();
                            if (file.GetDecl().Count > 0 && file.GetDecl()[0] is AMethodDecl)
                            {
                                AMethodDecl method = (AMethodDecl) file.GetDecl()[0];
                                if (method.GetName().Text == "Galaxy_pp_Deobfuscate")
                                {
                                    FileInfo dobfuscateFile = new FileInfo("Deobfuscator.LibraryData");
                                    IFormatter formatter = new BinaryFormatter();
                                    Stream stream = dobfuscateFile.Open(FileMode.Create);
                                    formatter.Serialize(stream, method);
                                    stream.Close();
                                }
                            }
                        }*/

                        if (Options.Compiler.ObfuscateStrings)
                        {
                            HasStringConstExp checker = new HasStringConstExp();
                            start.Apply(checker);

                            if (!addedDeobfuscator /* && checker.HasStringConst*/)
                            {
                                FileInfo dobfuscateFile = new FileInfo("Deobfuscator.LibraryData");
                                IFormatter formatter = new BinaryFormatter();
                                Stream stream = dobfuscateFile.Open(FileMode.Open);
                                AASourceFile file = (AASourceFile) start.GetPSourceFile();

                                AMethodDecl method = (AMethodDecl) formatter.Deserialize(stream);
                                sharedData.DeobfuscateMethod = method;
                                method.GetName().Line = 0;

                                HasStringConstExp checker2 = new HasStringConstExp();
                                method.Apply(checker2);
                                file.GetDecl().Insert(0, method);
                                stream.Close();

                                addedDeobfuscator = true;

                                foreach (AStringConstExp stringConstExp in checker2.List)
                                {
                                    int line = -sharedData.UnobfuscatedStrings.Count - 1;
                                    AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const", line, 0),
                                                                      new ANamedType(new TIdentifier("string", line, 1),
                                                                                     null),
                                                                      new TIdentifier("Galaxy_pp_stringU" +
                                                                                      sharedData.UnobfuscatedStrings.
                                                                                          Count),
                                                                      null);
                                    //If the strings are the same - point them to same field
                                    bool newField = true;
                                    foreach (AStringConstExp oldStringConstExp in sharedData.UnobfuscatedStrings.Keys)
                                    {
                                        if (stringConstExp.GetStringLiteral().Text ==
                                            oldStringConstExp.GetStringLiteral().Text)
                                        {
                                            field = sharedData.UnobfuscatedStrings[oldStringConstExp];
                                            newField = false;
                                            break;
                                        }
                                    }
                                    if (newField)
                                    {
                                        file.GetDecl().Insert(0, field);
                                        sharedData.ObfuscationFields.Add(field);
                                    }
                                    sharedData.UnobfuscatedStrings.Add(stringConstExp, field);

                                }

                            }
                            foreach (AStringConstExp stringConstExp in checker.List)
                            {
                                int line = -sharedData.ObfuscatedStrings.Count - 1;
                                AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const", line, 0),
                                                                  new ANamedType(new TIdentifier("string", line, 1),
                                                                                 null),
                                                                  new TIdentifier("Galaxy_pp_stringO" +
                                                                                  sharedData.ObfuscatedStrings.Count),
                                                                  null);
                                //If the strings are the same - point them to same field
                                bool newField = true;
                                foreach (AStringConstExp oldStringConstExp in sharedData.ObfuscatedStrings.Keys)
                                {
                                    if (stringConstExp.GetStringLiteral().Text ==
                                        oldStringConstExp.GetStringLiteral().Text)
                                    {
                                        field = sharedData.ObfuscatedStrings[oldStringConstExp];
                                        newField = false;
                                        break;
                                    }
                                }
                                if (newField)
                                {
                                    AASourceFile file = (AASourceFile) sharedData.DeobfuscateMethod.Parent();
                                    file.GetDecl().Insert(file.GetDecl().IndexOf(sharedData.DeobfuscateMethod) + 1,
                                                          field);
                                    sharedData.ObfuscationFields.Add(field);
                                }
                                sharedData.ObfuscatedStrings.Add(stringConstExp, field);
                            }
                        }

                        sourceNode.SetName(new TIdentifier(filename));
                        root.GetSourceFiles().Add(start.GetPSourceFile());

                    }
                    catch (ParserException err)
                    {
                        String errMsg = err.Message;
                        //Remove [...]
                        errMsg = errMsg.Substring(errMsg.IndexOf(']') + 1).TrimStart();
                        errors.Add(new ErrorCollection.Error(err.Token, filename, errMsg));
                    }
                    reader.Close();
                }
                //Parse project dialogs
                foreach (
                    DialogItem dialogItem in Form1.GetDialogsFiles(ProjectProperties.CurrentProjectPropperties.SrcFolder))
                {
                    if (dialogItem.Deactivated)
                        continue;
                   // List<string> fileNames = new List<string>();
                   // List<string> sources = new List<string>();

                    DialogData data;
                    if (dialogItem.OpenFileData != null)
                    {
                        data = dialogItem.OpenFileData;
                        data.Save(dialogItem.FullName);
                    }
                    else
                    {
                        data = DialogData.Load(dialogItem.FullName);
                        data.DialogItem = dialogItem;
                    }

                    string filename = dialogItem.FullName;
                    filename = filename.Remove(0, (ProjectDir.FullName + "/src/").Length);
                    filename = filename.Remove(filename.Length - ".Dialog".Length);

                    fileNames.Add(filename);
                    sources.Add(data.Code ?? "");

                    fileNames.Add(filename + ".Designer");
                    sources.Add(data.DesignerCode ?? "");

                    continue;

                    for (int i = 0; i < fileNames.Count; i++)
                    {
                        filename = fileNames[i];
                        StringReader reader = new StringReader(sources[i] ?? "");
                        Parser parser = new Parser(new Lexer(reader));
                        try
                        {
                            Start start = parser.Parse();
                            AASourceFile sourceNode = (AASourceFile) start.GetPSourceFile();
                            reader.Close();
                            reader.Dispose();
                            reader = new StringReader(sources[i] ?? "");
                            int lineCount = 0;
                            while (reader.ReadLine() != null)
                            {
                                lineCount++;
                            }
                            reader.Close();

                            sharedData.LineCounts[sourceNode] = lineCount;

                            if (Options.Compiler.ObfuscateStrings)
                            {
                                HasStringConstExp checker = new HasStringConstExp();
                                start.Apply(checker);

                                if (!addedDeobfuscator /* && checker.HasStringConst*/)
                                {
                                    FileInfo dobfuscateFile = new FileInfo("Deobfuscator.LibraryData");
                                    IFormatter formatter = new BinaryFormatter();
                                    Stream stream = dobfuscateFile.Open(FileMode.Open);
                                    AASourceFile file = (AASourceFile) start.GetPSourceFile();

                                    AMethodDecl method = (AMethodDecl) formatter.Deserialize(stream);
                                    sharedData.DeobfuscateMethod = method;
                                    method.GetName().Line = 0;

                                    HasStringConstExp checker2 = new HasStringConstExp();
                                    method.Apply(checker2);
                                    file.GetDecl().Insert(0, method);
                                    stream.Close();

                                    addedDeobfuscator = true;

                                    foreach (AStringConstExp stringConstExp in checker2.List)
                                    {
                                        int line = -sharedData.UnobfuscatedStrings.Count - 1;
                                        AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null,
                                                                          new TConst("const", line, 0),
                                                                          new ANamedType(
                                                                              new TIdentifier("string", line, 1),
                                                                              null),
                                                                          new TIdentifier("Galaxy_pp_stringU" +
                                                                                          sharedData.UnobfuscatedStrings
                                                                                              .
                                                                                              Count),
                                                                          null);
                                        //If the strings are the same - point them to same field
                                        bool newField = true;
                                        foreach (
                                            AStringConstExp oldStringConstExp in sharedData.UnobfuscatedStrings.Keys)
                                        {
                                            if (stringConstExp.GetStringLiteral().Text ==
                                                oldStringConstExp.GetStringLiteral().Text)
                                            {
                                                field = sharedData.UnobfuscatedStrings[oldStringConstExp];
                                                newField = false;
                                                break;
                                            }
                                        }
                                        if (newField)
                                        {
                                            file.GetDecl().Insert(0, field);
                                            sharedData.ObfuscationFields.Add(field);
                                        }
                                        sharedData.UnobfuscatedStrings.Add(stringConstExp, field);

                                    }

                                }
                                foreach (AStringConstExp stringConstExp in checker.List)
                                {
                                    int line = -sharedData.ObfuscatedStrings.Count - 1;
                                    AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null,
                                                                      new TConst("const", line, 0),
                                                                      new ANamedType(new TIdentifier("string", line, 1),
                                                                                     null),
                                                                      new TIdentifier("Galaxy_pp_stringO" +
                                                                                      sharedData.ObfuscatedStrings.Count),
                                                                      null);
                                    //If the strings are the same - point them to same field
                                    bool newField = true;
                                    foreach (AStringConstExp oldStringConstExp in sharedData.ObfuscatedStrings.Keys)
                                    {
                                        if (stringConstExp.GetStringLiteral().Text ==
                                            oldStringConstExp.GetStringLiteral().Text)
                                        {
                                            field = sharedData.ObfuscatedStrings[oldStringConstExp];
                                            newField = false;
                                            break;
                                        }
                                    }
                                    if (newField)
                                    {
                                        AASourceFile file = (AASourceFile) sharedData.DeobfuscateMethod.Parent();
                                        file.GetDecl().Insert(file.GetDecl().IndexOf(sharedData.DeobfuscateMethod) + 1,
                                                              field);
                                        sharedData.ObfuscationFields.Add(field);
                                    }
                                    sharedData.ObfuscatedStrings.Add(stringConstExp, field);
                                }
                            }

                            sourceNode.SetName(new TIdentifier(filename));
                            root.GetSourceFiles().Add(start.GetPSourceFile());

                        }
                        catch (ParserException err)
                        {
                            String errMsg = err.Message;
                            //Remove [...]
                            errMsg = errMsg.Substring(errMsg.IndexOf(']') + 1).TrimStart();
                            errors.Add(new ErrorCollection.Error(err.Token, filename, errMsg));
                        }
                        reader.Close();
                    }
                }
               // Preprocessor.Parse(sources, errors);
                for (int i = 0; i < fileNames.Count; i++)
                {
                    string filename = fileNames[i];
                    StringReader reader = new StringReader(sources[i] ?? "");
                    Parser parser = new Parser(new Lexer(reader));
                    try
                    {
                        Start start = parser.Parse();
                        AASourceFile sourceNode = (AASourceFile)start.GetPSourceFile();
                        reader.Close();
                        reader.Dispose();
                        reader = new StringReader(sources[i] ?? "");
                        int lineCount = 0;
                        while (reader.ReadLine() != null)
                        {
                            lineCount++;
                        }
                        reader.Close();

                        sharedData.LineCounts[sourceNode] = lineCount;

                        //Extract encryption function
                         /*{
                             AASourceFile file = (AASourceFile) start.GetPSourceFile();
                             if (file.GetDecl().Count > 0 && file.GetDecl()[0] is AMethodDecl)
                             {
                                 AMethodDecl method = (AMethodDecl) file.GetDecl()[0];
                                 if (method.GetName().Text == "Galaxy_pp_Deobfuscate")
                                 {
                                     FileInfo dobfuscateFile = new FileInfo("Deobfuscator.LibraryData");
                                     IFormatter formatter = new BinaryFormatter();
                                     Stream stream = dobfuscateFile.Open(FileMode.Create);
                                     formatter.Serialize(stream, method);
                                     stream.Close();
                                 }
                             }
                         }*/

                        if (Options.Compiler.ObfuscateStrings)
                        {
                            HasStringConstExp checker = new HasStringConstExp();
                            start.Apply(checker);

                            if (!addedDeobfuscator /* && checker.HasStringConst*/)
                            {
                                FileInfo dobfuscateFile = new FileInfo("Deobfuscator.LibraryData");
                                IFormatter formatter = new BinaryFormatter();
                                Stream stream = dobfuscateFile.Open(FileMode.Open);
                                AASourceFile file = (AASourceFile)start.GetPSourceFile();

                                AMethodDecl method = (AMethodDecl)formatter.Deserialize(stream);
                                sharedData.DeobfuscateMethod = method;
                                method.GetName().Line = 0;

                                HasStringConstExp checker2 = new HasStringConstExp();
                                method.Apply(checker2);
                                file.GetDecl().Insert(0, method);
                                stream.Close();

                                addedDeobfuscator = true;

                                foreach (AStringConstExp stringConstExp in checker2.List)
                                {
                                    int line = -sharedData.UnobfuscatedStrings.Count - 1;
                                    AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null,
                                                                      new TConst("const", line, 0),
                                                                      new ANamedType(
                                                                          new TIdentifier("string", line, 1),
                                                                          null),
                                                                      new TIdentifier("Galaxy_pp_stringU" +
                                                                                      sharedData.UnobfuscatedStrings
                                                                                          .
                                                                                          Count),
                                                                      null);
                                    //If the strings are the same - point them to same field
                                    bool newField = true;
                                    foreach (
                                        AStringConstExp oldStringConstExp in sharedData.UnobfuscatedStrings.Keys)
                                    {
                                        if (stringConstExp.GetStringLiteral().Text ==
                                            oldStringConstExp.GetStringLiteral().Text)
                                        {
                                            field = sharedData.UnobfuscatedStrings[oldStringConstExp];
                                            newField = false;
                                            break;
                                        }
                                    }
                                    if (newField)
                                    {
                                        file.GetDecl().Insert(0, field);
                                        sharedData.ObfuscationFields.Add(field);
                                    }
                                    sharedData.UnobfuscatedStrings.Add(stringConstExp, field);

                                }

                            }
                            foreach (AStringConstExp stringConstExp in checker.List)
                            {
                                int line = -sharedData.ObfuscatedStrings.Count - 1;
                                AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null,
                                                                  new TConst("const", line, 0),
                                                                  new ANamedType(new TIdentifier("string", line, 1),
                                                                                 null),
                                                                  new TIdentifier("Galaxy_pp_stringO" +
                                                                                  sharedData.ObfuscatedStrings.Count),
                                                                  null);
                                //If the strings are the same - point them to same field
                                bool newField = true;
                                foreach (AStringConstExp oldStringConstExp in sharedData.ObfuscatedStrings.Keys)
                                {
                                    if (stringConstExp.GetStringLiteral().Text ==
                                        oldStringConstExp.GetStringLiteral().Text)
                                    {
                                        field = sharedData.ObfuscatedStrings[oldStringConstExp];
                                        newField = false;
                                        break;
                                    }
                                }
                                if (newField)
                                {
                                    AASourceFile file = (AASourceFile)sharedData.DeobfuscateMethod.Parent();
                                    file.GetDecl().Insert(file.GetDecl().IndexOf(sharedData.DeobfuscateMethod) + 1,
                                                          field);
                                    sharedData.ObfuscationFields.Add(field);
                                }
                                sharedData.ObfuscatedStrings.Add(stringConstExp, field);
                            }
                        }

                        sourceNode.SetName(new TIdentifier(filename));
                        root.GetSourceFiles().Add(start.GetPSourceFile());

                    }
                    catch (ParserException err)
                    {
                        String errMsg = err.Message;
                        //Remove [...]
                        errMsg = errMsg.Substring(errMsg.IndexOf(']') + 1).TrimStart();
                        errors.Add(new ErrorCollection.Error(err.Token, filename, errMsg));
                    }
                    reader.Close();
                }

                //Load libraries
                foreach (Library lib in ProjectProperties.CurrentProjectPropperties.Libraries)
                {
                    foreach (KeyValuePair<Library.File, string> sourceFile in lib.GetFiles())
                    {
                        StringReader sReader = new StringReader(sourceFile.Key.Text);
                        {
                            Parser parser = new Parser(new Lexer(sReader));

                            try
                            {
                                Start start = parser.Parse();
                                AASourceFile sourceNode = (AASourceFile) start.GetPSourceFile();
                                sReader.Close();
                                sReader.Dispose();
                                sReader = new StringReader(sourceFile.Key.Text);
                                int lineCount = 0;
                                while (sReader.ReadLine() != null)
                                {
                                    lineCount++;
                                }
                                sReader.Close();
                                sReader.Dispose();
                                sharedData.LineCounts[sourceNode] = lineCount;

                                if (Options.Compiler.ObfuscateStrings)
                                {
                                    HasStringConstExp checker = new HasStringConstExp();
                                    start.Apply(checker);

                                    if (!addedDeobfuscator /* && checker.HasStringConst*/)
                                    {
                                        FileInfo dobfuscateFile = new FileInfo("Deobfuscator.LibraryData");
                                        IFormatter formatter = new BinaryFormatter();
                                        Stream stream = dobfuscateFile.Open(FileMode.Open);
                                        AASourceFile file = (AASourceFile) start.GetPSourceFile();

                                        AMethodDecl method = (AMethodDecl) formatter.Deserialize(stream);
                                        sharedData.DeobfuscateMethod = method;
                                        method.GetName().Line = 0;

                                        HasStringConstExp checker2 = new HasStringConstExp();
                                        method.Apply(checker2);
                                        file.GetDecl().Insert(0, method);
                                        stream.Close();

                                        addedDeobfuscator = true;

                                        foreach (AStringConstExp stringConstExp in checker2.List)
                                        {
                                            int line = -sharedData.UnobfuscatedStrings.Count - 1;
                                            AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null,
                                                                              new TConst("const", line, 0),
                                                                              new ANamedType(
                                                                                  new TIdentifier("string", line, 1),
                                                                                  null),
                                                                              new TIdentifier("Galaxy_pp_stringU" +
                                                                                              sharedData.
                                                                                                  UnobfuscatedStrings
                                                                                                  .
                                                                                                  Count),
                                                                              null);
                                            //If the strings are the same - point them to same field
                                            bool newField = true;
                                            foreach (
                                                AStringConstExp oldStringConstExp in sharedData.UnobfuscatedStrings.Keys
                                                )
                                            {
                                                if (stringConstExp.GetStringLiteral().Text ==
                                                    oldStringConstExp.GetStringLiteral().Text)
                                                {
                                                    field = sharedData.UnobfuscatedStrings[oldStringConstExp];
                                                    newField = false;
                                                    break;
                                                }
                                            }
                                            if (newField)
                                            {
                                                file.GetDecl().Insert(0, field);
                                                sharedData.ObfuscationFields.Add(field);
                                            }
                                            sharedData.UnobfuscatedStrings.Add(stringConstExp, field);

                                        }

                                    }
                                    foreach (AStringConstExp stringConstExp in checker.List)
                                    {
                                        int line = -sharedData.ObfuscatedStrings.Count - 1;
                                        AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null,
                                                                          new TConst("const", line, 0),
                                                                          new ANamedType(
                                                                              new TIdentifier("string", line, 1),
                                                                              null),
                                                                          new TIdentifier("Galaxy_pp_stringO" +
                                                                                          sharedData.ObfuscatedStrings.
                                                                                              Count),
                                                                          null);
                                        //If the strings are the same - point them to same field
                                        bool newField = true;
                                        foreach (AStringConstExp oldStringConstExp in sharedData.ObfuscatedStrings.Keys)
                                        {
                                            if (stringConstExp.GetStringLiteral().Text ==
                                                oldStringConstExp.GetStringLiteral().Text)
                                            {
                                                field = sharedData.ObfuscatedStrings[oldStringConstExp];
                                                newField = false;
                                                break;
                                            }
                                        }
                                        if (newField)
                                        {
                                            AASourceFile file = (AASourceFile) sharedData.DeobfuscateMethod.Parent();
                                            file.GetDecl().Insert(
                                                file.GetDecl().IndexOf(sharedData.DeobfuscateMethod) + 1,
                                                field);
                                            sharedData.ObfuscationFields.Add(field);
                                        }
                                        sharedData.ObfuscatedStrings.Add(stringConstExp, field);
                                    }
                                }

                                sourceNode.SetName(new TIdentifier(sourceFile.Value));
                                root.GetSourceFiles().Add(start.GetPSourceFile());

                            }
                            catch (ParserException err)
                            {
                                String errMsg = err.Message;
                                //Remove [...]
                                errMsg = errMsg.Substring(errMsg.IndexOf(']') + 1).TrimStart();
                                errors.Add(new ErrorCollection.Error(err.Token, sourceFile.Value, errMsg));
                            }
                        }
                    }
                }

                string rootFileName = "";
                DirectoryInfo outputDir = ProjectDir.CreateSubdirectory("output");
                try
                {
                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text4"));
                    sharedData.Libraries = libraryData;
                    Weeder.Parse(root, errors, sharedData);

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text5"));
                    if (!errors.HasErrors) EnviromentBuilding.Parse(root, errors, sharedData);

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text6"));
                    if (!errors.HasErrors) EnviromentChecking.Parse(root, errors, sharedData);

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text7"));
                    if (!errors.HasErrors) root.Apply(new LinkNamedTypes(errors, sharedData));

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text8"));
                    if (!errors.HasErrors) root.Apply(new FixGenerics(errors, sharedData));

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text9"));
                    if (!errors.HasErrors) root.Apply(new Enheritance(sharedData, errors));

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text10"));
                    if (!errors.HasErrors) TypeLinking.Parse(root, errors, sharedData);

                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text11"));
                    if (!errors.HasErrors) TypeChecking.Parse(root, errors, sharedData);
                    if (!errors.HasErrors) root.Apply(new MakeEnrichmentLinks(sharedData, errors));
                    if (!errors.HasErrors) root.Apply(new SetArrayIndexes(sharedData, errors));
                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text12"));
                    if (!errors.HasErrors) FinalTransformations.Parse(root, errors, sharedData, out rootFileName);
                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text13"));
                    if (!errors.HasErrors) CodeGeneration.Parse(root, errors, sharedData, outputDir);
                    if (!errors.HasErrors) GenerateBankPreloadFile.Generate(sharedData, outputDir);
                }
                catch (ParserException err)
                {

                }

                Compiling = false;

                if (!errors.HasErrors)
                {
                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text14"));
                    ProjectProperties.CurrentProjectPropperties.RootFileName = rootFileName;
                    ProjectProperties.CurrentProjectPropperties.CompileStatus =
                        ProjectProperties.ECompileStatus.SuccessfullyCompiled;
                    if (CompilationSuccessfull != null)
                        CompilationSuccessfull();
                }
                else
                {
                    if (!compilingFromCommandLine)
                        form.SetStatusText(LocRM.GetString("GC_Text15"));
                    if (CompilationFailed != null)
                        CompilationFailed();
                }

            }
            #if DEBUG
            finally
            {

            }
            #else
            catch (Exception error)
            {
                Compiling = false;
                //Program.ErrorHandeler(this, new ThreadExceptionEventArgs(error));
                new ExceptionForm(error, true).ShowDialog();
                form.SetStatusText("Critical compile error");
                if (CompilationFailed != null)
                    CompilationFailed();
            }
            #endif
        }
        public void LoadLibraries(List<DirectoryInfo> libraries)
        {
            LibraryData lib = new LibraryData();

            StreamWriter writer = new StreamWriter(new FileInfo("outputList.txt").Open(FileMode.Create, FileAccess.Write));
            foreach (DirectoryInfo library in libraries)
            {
            retry:
                FileInfo precompFile = new FileInfo(library.FullName + "\\Precompiled.LibraryData");
                /*if (!precompFile.Exists)*/
                CompileLibrary(library, writer);
                IFormatter formatter = new BinaryFormatter();
                Stream stream = precompFile.OpenRead();
                try
                {
                    lib.Join((LibraryData) formatter.Deserialize(stream));
                    stream.Close();
                }
                catch (Exception err)
                {
                    stream.Close();
                    precompFile.Delete();
                    goto retry;
                }
            }
            libraryData = lib;

            {
                List<AMethodDecl> newMethods = new List<AMethodDecl>();
                List<AFieldDecl> newFields = new List<AFieldDecl>();
                XmlTextReader reader = new XmlTextReader(new FileInfo("Galaxy.xml").Open(FileMode.Open, FileAccess.Read));

                while (reader.Read())
                {
                    if (reader.NodeType != XmlNodeType.Element)
                        continue;

                    if (reader.Name == "KeyWord")
                    {
                        if (reader.GetAttribute("func") == null)
                        {
                            AFieldDecl fieldDecl = new AFieldDecl(new APublicVisibilityModifier(), null, null, null, new TIdentifier(reader.GetAttribute("name")), null);
                            newFields.Add(fieldDecl);
                            continue;
                        }
                        AMethodDecl methodDecl = new AMethodDecl();
                        methodDecl.SetName(new TIdentifier(reader.GetAttribute("name")));
                        while (reader.Read())
                        {
                            if (reader.NodeType == XmlNodeType.EndElement)
                            {
                                break;
                            }
                            if (reader.NodeType != XmlNodeType.Element)
                                continue;
                            if (reader.Name != "Param")
                                continue;
                            string type = reader.GetAttribute("name");
                            type = type.Substring(0, type.IndexOf(" "));
                            string name = reader.GetAttribute("name");
                            name = name.Substring(name.IndexOf(" ") + 1);

                            methodDecl.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                                        new ANamedType(new TIdentifier(type), null),
                                                                        new TIdentifier(name), null));

                        }
                        if (reader.EOF)
                            break;
                        newMethods.Add(methodDecl);
                    }
                }

                reader.Close();

                List<AMethodDecl> oldMethods = new List<AMethodDecl>();
                oldMethods.AddRange(libraryData.Methods);
                List<AFieldDecl> oldFields = new List<AFieldDecl>();
                oldFields.AddRange(libraryData.Fields);

                //Remove dublicates in old
                for (int i = 0; i < oldMethods.Count; i++)
                {
                    for (int j = i + 1; j < oldMethods.Count; j++)
                    {
                        if (oldMethods[i].GetName().Text == oldMethods[j].GetName().Text)
                        {
                            oldMethods.RemoveAt(j);
                            j--;
                        }
                    }
                }

                for (int i = 0; i < oldFields.Count; i++)
                {
                    for (int j = i + 1; j < oldFields.Count; j++)
                    {
                        if (oldFields[i].GetName().Text == oldFields[j].GetName().Text)
                        {
                            oldFields.RemoveAt(j);
                            j--;
                        }
                    }
                }

                //Remove dublicates in new
                for (int i = 0; i < newMethods.Count; i++)
                {
                    for (int j = i + 1; j < newMethods.Count; j++)
                    {
                        if (newMethods[i].GetName().Text == newMethods[j].GetName().Text)
                        {
                            newMethods.RemoveAt(j);
                            j--;
                        }
                    }
                }

                for (int i = 0; i < newFields.Count; i++)
                {
                    for (int j = i + 1; j < newFields.Count; j++)
                    {
                        if (newFields[i].GetName().Text == newFields[j].GetName().Text)
                        {
                            newFields.RemoveAt(j);
                            j--;
                        }
                    }
                }

                //Remove stuff they agree on
                for (int i = 0; i < newFields.Count; i++)
                {
                    for (int j = 0; j < oldFields.Count; j++)
                    {
                        if (newFields[i].GetName().Text == oldFields[j].GetName().Text)
                        {
                            newFields.RemoveAt(i);
                            oldFields.RemoveAt(j);
                            i--;
                            break;
                        }
                    }
                }
                for (int j = 0; j < oldFields.Count; j++)
                {
                    if (oldFields[j].GetStatic() != null)
                    {
                        oldFields.RemoveAt(j);
                        j--;
                    }
                }
                for (int i = 0; i < newMethods.Count; i++)
                {
                    for (int j = 0; j < oldMethods.Count; j++)
                    {
                        if (newMethods[i].GetName().Text == oldMethods[j].GetName().Text)
                        {
                            newMethods.RemoveAt(i);
                            oldMethods.RemoveAt(j);
                            i--;
                            break;
                        }
                    }
                }
                for (int j = 0; j < oldMethods.Count; j++)
                {
                    if (oldMethods[j].GetStatic() != null ||
                        (oldMethods[j].GetNative() == null && oldMethods[j].GetBlock() == null))
                    {
                        oldMethods.RemoveAt(j);
                        j--;
                    }
                }

            }

            {
                /*StreamWriter writer = new StreamWriter(new FileInfo("outputList.txt").Open(FileMode.Create, FileAccess.Write));
                foreach (AMethodDecl method in libraryData.Methods)
                {
                    string str = "native " + TypeToString(method.GetReturnType()) + " " + method.GetName().Text +
                                 "(";
                    bool first = true;
                    foreach (AALocalDecl formal in method.GetFormals())
                    {
                        if (!first)
                            str += ", ";
                        str += TypeToString(formal.GetType()) + " " + formal.GetName().Text;
                        first = false;
                    }
                    str += ");";

                    writer.WriteLine(str);
                }

                foreach (AFieldDecl field in libraryData.Fields)
                {
                    if (field.GetName().Text == "libNtve_gv__GameUIVisible")
                        writer = writer;
                    writer.WriteLine(TypeToString(field.GetType()) + " " + field.GetName().Text + ";");
                }*/
                writer.Flush();
                writer.Close();
            }
        }
 public override void CaseAFieldDecl(AFieldDecl node)
 {
     node.GetName().Text = namespacePrefix + node.GetName().Text;
 }
        public override void CaseAALocalDecl(AALocalDecl node)
        {
            //Convert a static struct field into a global variable. All refferences to it are structFieldLvalues.
            if (node.GetStatic() == null)
                return;

            AStructDecl str = (AStructDecl) node.Parent();
            if (data.StructFields[str].Contains(node))
                data.StructFields[str].Remove(node);
            AFieldDecl replacementField;
            //Don't enhrit static fields.
            if (data.EnheritanceLocalMap.ContainsKey(node))
            {
                str.RemoveChild(node);

                AALocalDecl realVar = data.EnheritanceLocalMap[node];
                if (convertionMap.ContainsKey(realVar))
                {
                    //Already converted to a field
                    replacementField = convertionMap[realVar];
                    foreach (AStructFieldLvalue lvalue in data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key))
                    {
                        AFieldLvalue newLvalue = new AFieldLvalue(new TIdentifier(replacementField.GetName().Text));
                        data.FieldLinks[newLvalue] = replacementField;
                        data.LvalueTypes[newLvalue] = replacementField.GetType();
                        lvalue.ReplaceBy(newLvalue);
                    }
                }
                else
                {
                    List<AStructFieldLvalue> refferences = new List<AStructFieldLvalue>();
                    refferences.AddRange(data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key));
                    foreach (AStructFieldLvalue lvalue in refferences)
                    {
                        data.StructMethodFieldLinks[lvalue] = realVar;
                    }
                }
                return;
            }

            replacementField = new AFieldDecl(new APublicVisibilityModifier(), null, node.GetConst(), node.GetType(), node.GetName(), node.GetInit());

            replacementField.GetName().Text = str.GetName().Text + "_" + replacementField.GetName().Text;

            AASourceFile file = Util.GetAncestor<AASourceFile>(node);
            file.GetDecl().Insert(file.GetDecl().IndexOf(node.Parent()), replacementField);
            data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, replacementField));

            if (ContainsNewExp(replacementField.GetInit()))
                data.FieldsToInitInMapInit.Add(replacementField);

            foreach (AStructFieldLvalue lvalue in data.StructMethodFieldLinks.Where(link => link.Value == node).Select(link => link.Key))
            {
                AFieldLvalue newLvalue = new AFieldLvalue(new TIdentifier(replacementField.GetName().Text));
                data.FieldLinks[newLvalue] = replacementField;
                data.LvalueTypes[newLvalue] = replacementField.GetType();
                lvalue.ReplaceBy(newLvalue);
            }

            convertionMap.Add(node, replacementField);
            node.Parent().RemoveChild(node);
        }
 public void Add(AFieldDecl field, bool read, bool written)
 {
     if (Fields.ContainsKey(field))
         Fields[field].Union(read, written);
     else
         Fields[field] = new Values(read, written);
 }
Пример #20
0
 ArrayList New155()
 {
     ArrayList nodeList = new ArrayList();
     ArrayList nodeArrayList7 = (ArrayList) Pop();
     ArrayList nodeArrayList6 = (ArrayList) Pop();
     ArrayList nodeArrayList5 = (ArrayList) Pop();
     ArrayList nodeArrayList4 = (ArrayList) Pop();
     ArrayList nodeArrayList3 = (ArrayList) Pop();
     ArrayList nodeArrayList2 = (ArrayList) Pop();
     ArrayList nodeArrayList1 = (ArrayList) Pop();
     PVisibilityModifier pvisibilitymodifierNode2 = (PVisibilityModifier)nodeArrayList1[0];
     TStatic tstaticNode3 = (TStatic)nodeArrayList2[0];
     TConst tconstNode4 = (TConst)nodeArrayList3[0];
     PType ptypeNode5 = (PType)nodeArrayList4[0];
     TIdentifier tidentifierNode6 = (TIdentifier)nodeArrayList5[0];
     PExp pexpNode7 = (PExp)nodeArrayList6[0];
     AFieldDecl pdeclNode1 = new AFieldDecl (
       pvisibilitymodifierNode2,
       tstaticNode3,
       tconstNode4,
       ptypeNode5,
       tidentifierNode6,
       pexpNode7
     );
     nodeList.Add(pdeclNode1);
     return nodeList;
 }
Пример #21
0
 public virtual void InAFieldDecl(AFieldDecl node)
 {
     DefaultIn(node);
 }
 public override void InAFieldDecl(AFieldDecl node)
 {
     decls.Add(node);
     /*node.GetName().Text = nextName;
     NextName();*/
 }
Пример #23
0
 public virtual void OutAFieldDecl(AFieldDecl node)
 {
     DefaultOut(node);
 }
Пример #24
0
 public Field(AFieldDecl fieldDecl)
 {
     FieldDecl = fieldDecl;
 }
 public override void OutAFieldDecl(AFieldDecl node)
 {
     decls.AddLast(new FieldDecl(node));
 }
 public override void CaseAFieldDecl(AFieldDecl node)
 {
     if (node.GetStatic() != null) Write("static ");
     if (node.GetConst() != null) Write("const ");
     node.GetType().Apply(this);
     Write(" " + node.GetName().Text);
     if (node.GetInit() != null)
     {
         Write(" = ");
         node.GetInit().Apply(this);
     }
     Write(";\n\n");
 }
        private AMethodDecl CreateStringDeobfuscator()
        {
            AASourceFile file = (AASourceFile) finalTrans.mainEntry.Parent();

            //Create fields for the string constants
            AStringConstExp emptyStringConst = new AStringConstExp(new TStringLiteral("\"\""));
            AFieldDecl emptyStringField = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const"),
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("fOobar"), emptyStringConst);
            file.GetDecl().Add(emptyStringField);
            data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, emptyStringField));
            AFieldLvalue emptyStringRef1 = new AFieldLvalue(new TIdentifier(emptyStringField.GetName().Text));
            AFieldLvalue emptyStringRef2 = new AFieldLvalue(new TIdentifier(emptyStringField.GetName().Text));
            AFieldLvalue emptyStringRef3 = new AFieldLvalue(new TIdentifier(emptyStringField.GetName().Text));
            ALvalueExp emptyStringRef1Exp = new ALvalueExp(emptyStringRef1);
            ALvalueExp emptyStringRef2Exp = new ALvalueExp(emptyStringRef2);
            ALvalueExp emptyStringRef3Exp = new ALvalueExp(emptyStringRef3);

            AStringConstExp colonStringConst = new AStringConstExp(new TStringLiteral("\":\""));
            AFieldDecl colonStringField = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const"),
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("foObar"), colonStringConst);
            file.GetDecl().Add(colonStringField);
            data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, colonStringField));
            AFieldLvalue colonStringRef = new AFieldLvalue(new TIdentifier(colonStringField.GetName().Text));
            ALvalueExp colonStringRefExp = new ALvalueExp(colonStringRef);

            /*
                string output = "";
                string ch;
                int length = StringLength(s);
                int phase1 = (length - 1)%3;
                int phase2 = (length - 1)%2;
             */

            AALocalDecl stringParam = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                      new ANamedType(new TIdentifier("string"), null),
                                                      new TIdentifier("fo0bar"), emptyStringRef1Exp);
            ALocalLvalue stringParamRef1 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef2 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef3 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef4 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef5 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef6 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef7 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef8 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALvalueExp stringParamRef1Exp = new ALvalueExp(stringParamRef1);
            ALvalueExp stringParamRef2Exp = new ALvalueExp(stringParamRef2);
            ALvalueExp stringParamRef4Exp = new ALvalueExp(stringParamRef4);
            ALvalueExp stringParamRef5Exp = new ALvalueExp(stringParamRef5);
            ALvalueExp stringParamRef7Exp = new ALvalueExp(stringParamRef7);
            ALvalueExp stringParamRef8Exp = new ALvalueExp(stringParamRef8);

            AABlock methodBlock = new AABlock(new ArrayList(), new TRBrace("}"));

            AALocalDecl outputDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                     new ANamedType(new TIdentifier("string"), null),
                                                     new TIdentifier("foobar"), emptyStringRef1Exp);
            methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), outputDecl));
            ALocalLvalue outputRef1 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef2 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef3 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef4 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef5 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef6 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef7 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef8 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef9 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef10 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef11 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef12 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef13 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALvalueExp outputRef2Exp = new ALvalueExp(outputRef2);
            ALvalueExp outputRef4Exp = new ALvalueExp(outputRef4);
            ALvalueExp outputRef5Exp = new ALvalueExp(outputRef5);
            ALvalueExp outputRef6Exp = new ALvalueExp(outputRef6);
            ALvalueExp outputRef7Exp = new ALvalueExp(outputRef7);
            ALvalueExp outputRef8Exp = new ALvalueExp(outputRef8);
            ALvalueExp outputRef10Exp = new ALvalueExp(outputRef10);
            ALvalueExp outputRef11Exp = new ALvalueExp(outputRef11);
            ALvalueExp outputRef12Exp = new ALvalueExp(outputRef12);
            ALvalueExp outputRef13Exp = new ALvalueExp(outputRef13);

            AALocalDecl chDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                     new ANamedType(new TIdentifier("string"), null),
                                                     new TIdentifier("f0obar"), null);
            methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), chDecl));
            ALocalLvalue chRef1 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text));
            ALocalLvalue chRef2 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text));
            ALocalLvalue chRef3 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text));
            ALocalLvalue chRef4 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text));
            ALocalLvalue chRef5 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text));
            ALvalueExp chRef3Exp = new ALvalueExp(chRef3);
            ALvalueExp chRef4Exp = new ALvalueExp(chRef4);
            ALvalueExp chRef5Exp = new ALvalueExp(chRef5);

            ASimpleInvokeExp stringLengthInvoke1 = new ASimpleInvokeExp(new TIdentifier("StringLength"),
                                                                        new ArrayList() { stringParamRef1Exp });
            AALocalDecl lengthDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                     new ANamedType(new TIdentifier("int"), null),
                                                     new TIdentifier("f0Obar"), stringLengthInvoke1);
            methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), lengthDecl));
            ALocalLvalue lengthRef1 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef2 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef3 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef4 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef5 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef6 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef7 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef8 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALvalueExp lengthRef1Exp = new ALvalueExp(lengthRef1);
            ALvalueExp lengthRef2Exp = new ALvalueExp(lengthRef2);
            ALvalueExp lengthRef3Exp = new ALvalueExp(lengthRef3);
            ALvalueExp lengthRef4Exp = new ALvalueExp(lengthRef4);
            ALvalueExp lengthRef5Exp = new ALvalueExp(lengthRef5);
            ALvalueExp lengthRef6Exp = new ALvalueExp(lengthRef6);
            ALvalueExp lengthRef7Exp = new ALvalueExp(lengthRef7);

            AIntConstExp intConstp1Init1 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConstp1Init2 = new AIntConstExp(new TIntegerLiteral("3"));
            ABinopExp binopExpP1InitMinus = new ABinopExp(lengthRef1Exp, new AMinusBinop(new TMinus("-")), intConstp1Init1);
            ABinopExp binopExpP1InitMod = new ABinopExp(binopExpP1InitMinus, new AModuloBinop(new TMod("%")), intConstp1Init2);

            AALocalDecl phase1Decl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                     new ANamedType(new TIdentifier("int"), null),
                                                     new TIdentifier("fO0bar"), binopExpP1InitMod);
            methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), phase1Decl));
            ALocalLvalue phase1Ref1 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref2 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref3 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref4 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref5 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref6 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref7 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref8 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref9 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref10 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref11 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALvalueExp phase1Ref1Exp = new ALvalueExp(phase1Ref1);
            ALvalueExp phase1Ref2Exp = new ALvalueExp(phase1Ref2);
            ALvalueExp phase1Ref4Exp = new ALvalueExp(phase1Ref4);
            ALvalueExp phase1Ref5Exp = new ALvalueExp(phase1Ref5);
            ALvalueExp phase1Ref7Exp = new ALvalueExp(phase1Ref7);
            ALvalueExp phase1Ref9Exp = new ALvalueExp(phase1Ref9);
            ALvalueExp phase1Ref10Exp = new ALvalueExp(phase1Ref10);
            ALvalueExp phase1Ref11Exp = new ALvalueExp(phase1Ref11);

            AIntConstExp intConstp2Init1 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConstp2Init2 = new AIntConstExp(new TIntegerLiteral("2"));
            ABinopExp binopExpP2InitMinus = new ABinopExp(lengthRef2Exp, new AMinusBinop(new TMinus("-")), intConstp2Init1);
            ABinopExp binopExpP2InitMod = new ABinopExp(binopExpP2InitMinus, new AModuloBinop(new TMod("%")), intConstp2Init2);

            AALocalDecl phase2Decl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                     new ANamedType(new TIdentifier("int"), null),
                                                     new TIdentifier("carl"), binopExpP2InitMod);
            methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), phase2Decl));
            ALocalLvalue phase2Ref1 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref2 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref3 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref4 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref5 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref6 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref7 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref8 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref9 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALvalueExp phase2Ref1Exp = new ALvalueExp(phase2Ref1);
            ALvalueExp phase2Ref2Exp = new ALvalueExp(phase2Ref2);
            ALvalueExp phase2Ref4Exp = new ALvalueExp(phase2Ref4);
            ALvalueExp phase2Ref5Exp = new ALvalueExp(phase2Ref5);
            ALvalueExp phase2Ref7Exp = new ALvalueExp(phase2Ref7);
            ALvalueExp phase2Ref9Exp = new ALvalueExp(phase2Ref9);

            /*
                while(length > 0)
                {
                    if(phase2 == 0)
                    {
                        ch = StringSub(s, 1, 1);
                        s = StringReplace(s, "", 1, 1);
                    }
                    else
                    {
                        if(phase2 == 1)
                        {
                            ch = StringSub(s, length, length);
                            s = StringReplace(s, "", length, length);
                        }
                    }

                    if(phase1 == 0)
                    {
                        output = ch + output;
                    }
                    else
                    {
                        if(phase1 == 1)
                        {
                            output = StringSub(output, 1, (StringLength(output) + 1)/2) + ch + StringSub(output, (StringLength(output) + 1)/2 + 1, StringLength(output));
                        }
                        else
                        {
                            output = output + ch;
                        }
                    }
                    phase1 = phase1 - 1;
                    if(phase1 < 0)
                    {
                        phase1 = phase1 + 3;
                    }
                    phase2 = phase2 - 1;
                    if(phase2 < 0)
                    {
                        phase2 = phase2 + 2;
                    }
                    length = StringLength(s);
                }
             */

            AABlock whileBlock = new AABlock(new ArrayList(), new TRBrace("}"));
            AIntConstExp intConstWhileCond = new AIntConstExp(new TIntegerLiteral("0"));
            ABinopExp binopWhileCond = new ABinopExp(lengthRef3Exp, new AGtBinop(new TGt(">")), intConstWhileCond);
            methodBlock.GetStatements().Add(new AWhileStm(new TLParen("("), binopWhileCond,
                                                          new ABlockStm(new TLBrace("{"), whileBlock)));

            /*
                    if(phase2 == 0)
                    {
                        ch = StringSub(s, 1, 1);
                        s = StringReplace(s, "", 1, 1);
                    }
                    else
                    {
                        if(phase2 == 1)
                        {
                            ch = StringSub(s, length, length);
                            s = StringReplace(s, "", length, length);
                        }
                    }
             */
            AIntConstExp intConstIf1Cond = new AIntConstExp(new TIntegerLiteral("0"));
            ABinopExp binopIf1Cond = new ABinopExp(phase2Ref1Exp, new AEqBinop(new TEq("==")), intConstIf1Cond);
            AABlock thenBlock = new AABlock();
            AABlock elseBlock = new AABlock();
            whileBlock.GetStatements().Add(new AIfThenElseStm(new TLParen("("), binopIf1Cond,
                                                              new ABlockStm(new TLBrace("{"), thenBlock),
                                                              new ABlockStm(new TLBrace("{"), elseBlock)));

            //ch = StringSub(s, 1, 1);
            AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral("1"));
            ASimpleInvokeExp invokeStringSub1 = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                           new ArrayList() {stringParamRef2Exp, intConst1, intConst2});
            AAssignmentExp assignment1 = new AAssignmentExp(new TAssign("="), chRef1, invokeStringSub1);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment1));

            //s = StringReplace(s, "", 1, 1);
            AIntConstExp intConst3 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConst4 = new AIntConstExp(new TIntegerLiteral("1"));
            ASimpleInvokeExp invokeStringReplace1 = new ASimpleInvokeExp(new TIdentifier("StringReplace"),
                                                           new ArrayList() { stringParamRef4Exp, emptyStringRef2Exp, intConst3, intConst4 });
            AAssignmentExp assignment2 = new AAssignmentExp(new TAssign("="), stringParamRef3, invokeStringReplace1);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment2));

            //if(phase2 == 1)
            AIntConstExp intConst5 = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp binop1 = new ABinopExp(phase2Ref2Exp, new AEqBinop(new TEq("==")), intConst5);
            thenBlock = new AABlock();
            elseBlock.GetStatements().Add(new AIfThenStm(new TLParen("("), binop1,
                                                         new ABlockStm(new TLBrace("{"), thenBlock)));

            //ch = StringSub(s, length, length);
            ASimpleInvokeExp invokeStringSub2 = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                           new ArrayList() { stringParamRef5Exp, lengthRef3Exp, lengthRef4Exp });
            AAssignmentExp assignment3 = new AAssignmentExp(new TAssign("="), chRef2, invokeStringSub2);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment3));

            //s = StringReplace(s, "", length, length);
            ASimpleInvokeExp invokeStringReplace2 = new ASimpleInvokeExp(new TIdentifier("StringReplace"),
                                                           new ArrayList() { stringParamRef7Exp, emptyStringRef3Exp, lengthRef5Exp, lengthRef6Exp });
            AAssignmentExp assignment4 = new AAssignmentExp(new TAssign("="), stringParamRef6, invokeStringReplace2);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment4));

            //if(phase1 == 0)
            AIntConstExp intConst6 = new AIntConstExp(new TIntegerLiteral("0"));
            ABinopExp binop2 = new ABinopExp(phase1Ref1Exp, new AEqBinop(new TEq("==")), intConst6);
            thenBlock = new AABlock();
            elseBlock = new AABlock();
            whileBlock.GetStatements().Add(new AIfThenElseStm(new TLParen("("), binop2,
                                                              new ABlockStm(new TLBrace("{"), thenBlock),
                                                              new ABlockStm(new TLBrace("{"), elseBlock)));

            //output = ch + output;
            ABinopExp binop3 = new ABinopExp(chRef3Exp, new APlusBinop(new TPlus("+")), outputRef2Exp);
            AAssignmentExp assignment5 = new AAssignmentExp(new TAssign("="), outputRef1, binop3);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment5));

            //if(phase1 == 1)
            AABlock cBlock = elseBlock;
            AIntConstExp intConst7 = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp binop4 = new ABinopExp(phase1Ref2Exp, new AEqBinop(new TEq("==")), intConst7);
            thenBlock = new AABlock();
            elseBlock = new AABlock();
            cBlock.GetStatements().Add(new AIfThenElseStm(new TLParen("("), binop4,
                                                              new ABlockStm(new TLBrace("{"), thenBlock),
                                                              new ABlockStm(new TLBrace("{"), elseBlock)));

            //output = StringSub(output, 1, (StringLength(output) + 1)/2) + ch + StringSub(output, (StringLength(output) + 1)/2 + 1, StringLength(output));
            AIntConstExp intConst8 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConst9 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConst10 = new AIntConstExp(new TIntegerLiteral("2"));
            AIntConstExp intConst11 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConst12 = new AIntConstExp(new TIntegerLiteral("2"));
            AIntConstExp intConst13 = new AIntConstExp(new TIntegerLiteral("1"));

            ASimpleInvokeExp invokeStringLength1 = new ASimpleInvokeExp(new TIdentifier("StringLength"),
                                                                        new ArrayList() {outputRef5Exp});
            ABinopExp binop5 = new ABinopExp(invokeStringLength1, new APlusBinop(new TPlus("+")), intConst9);
            ABinopExp binop6 = new ABinopExp(binop5, new ADivideBinop(new TDiv("/")), intConst10);

            ASimpleInvokeExp invokeStringSub3 = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                           new ArrayList() { outputRef4Exp, intConst8, binop6});

            ABinopExp binop7 = new ABinopExp(invokeStringSub3, new APlusBinop(new TPlus("+")), chRef4Exp);

            ASimpleInvokeExp invokeStringLength2 = new ASimpleInvokeExp(new TIdentifier("StringLength"),
                                                                        new ArrayList() { outputRef7Exp });
            ABinopExp binop8 = new ABinopExp(invokeStringLength2, new APlusBinop(new TPlus("+")), intConst11);
            ABinopExp binop9 = new ABinopExp(binop8, new ADivideBinop(new TDiv("/")), intConst12);
            ABinopExp binop10 = new ABinopExp(binop9, new APlusBinop(new TPlus("+")), intConst13);

            ASimpleInvokeExp invokeStringLength3 = new ASimpleInvokeExp(new TIdentifier("StringLength"),
                                                                        new ArrayList() { outputRef8Exp });

            ASimpleInvokeExp invokeStringSub4 = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                           new ArrayList() { outputRef6Exp, binop10, invokeStringLength3 });

            ABinopExp binop11 = new ABinopExp(binop7, new APlusBinop(new TPlus("+")), invokeStringSub4);

            AAssignmentExp assignment6 = new AAssignmentExp(new TAssign("="), outputRef3, binop11);

            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment6));

            //output = output + ch;
            ABinopExp binop12 = new ABinopExp(outputRef10Exp, new APlusBinop(new TPlus("+")), chRef5Exp);
            AAssignmentExp assignment7 = new AAssignmentExp(new TAssign("="), outputRef9, binop12);
            elseBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment7));

            //phase1 = phase1 - 1;
            AIntConstExp intConst14 = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp binop13 = new ABinopExp(phase1Ref4Exp, new AMinusBinop(new TMinus("-")), intConst14);
            AAssignmentExp assignment8 = new AAssignmentExp(new TAssign("="), phase1Ref3, binop13);
            whileBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment8));

            //if(phase1 < 0)
            AIntConstExp intConst15 = new AIntConstExp(new TIntegerLiteral("0"));
            ABinopExp binop14 = new ABinopExp(phase1Ref5Exp, new ALtBinop(new TLt("<")), intConst15);
            thenBlock = new AABlock();
            whileBlock.GetStatements().Add(new AIfThenStm(new TLParen("("), binop14,
                                                         new ABlockStm(new TLBrace("{"), thenBlock)));

            //phase1 = phase1 + 3;
            AIntConstExp intConst16 = new AIntConstExp(new TIntegerLiteral("3"));
            ABinopExp binop15 = new ABinopExp(phase1Ref7Exp, new APlusBinop(new TPlus("+")), intConst16);
            AAssignmentExp assignment9 = new AAssignmentExp(new TAssign("="), phase1Ref6, binop15);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment9));

            //phase2 = phase2 - 1;
            AIntConstExp intConst17 = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp binop16 = new ABinopExp(phase2Ref4Exp, new AMinusBinop(new TMinus("-")), intConst17);
            AAssignmentExp assignment10 = new AAssignmentExp(new TAssign("="), phase2Ref3, binop16);
            whileBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment10));

            //if(phase2 < 0)
            AIntConstExp intConst18 = new AIntConstExp(new TIntegerLiteral("0"));
            ABinopExp binop17 = new ABinopExp(phase2Ref5Exp, new ALtBinop(new TLt("<")), intConst18);
            thenBlock = new AABlock();
            whileBlock.GetStatements().Add(new AIfThenStm(new TLParen("("), binop17,
                                                         new ABlockStm(new TLBrace("{"), thenBlock)));

            //phase2 = phase2 + 2;
            AIntConstExp intConst19 = new AIntConstExp(new TIntegerLiteral("2"));
            ABinopExp binop18 = new ABinopExp(phase2Ref7Exp, new APlusBinop(new TPlus("+")), intConst19);
            AAssignmentExp assignment11 = new AAssignmentExp(new TAssign("="), phase2Ref6, binop18);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment11));

            //length = StringLength(s);
            ASimpleInvokeExp invokeStringLength4 = new ASimpleInvokeExp(new TIdentifier("StringLength"),
                                                                        new ArrayList() { stringParamRef8Exp });
            AAssignmentExp assignment12 = new AAssignmentExp(new TAssign("="), lengthRef8, invokeStringLength4);
            whileBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment12));

            /*
                phase1 = StringFind(output, ":", false);
                phase2 = StringToInt(StringSub(output, 1, phase1 - 1));
                return StringSub(output, phase1 + 1, phase2 + phase1);
             */

            ABooleanConstExp boolConst1 = new ABooleanConstExp(new AFalseBool());
            ASimpleInvokeExp invokeStringFind = new ASimpleInvokeExp(new TIdentifier("StringFind"),
                                                                     new ArrayList()
                                                                         {outputRef11Exp, colonStringRefExp, boolConst1});
            AAssignmentExp assignment13 = new AAssignmentExp(new TAssign("="), phase1Ref8, invokeStringFind);
            methodBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment13));

            //phase2 = StringToInt(StringSub(output, 1, phase1 - 1));
            AIntConstExp intConst20 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConst21 = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp binop19 = new ABinopExp(phase1Ref9Exp, new AMinusBinop(new TMinus("-")), intConst21);
            ASimpleInvokeExp invokeStringSub5 = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                           new ArrayList() { outputRef12Exp, intConst20, binop19});
            ASimpleInvokeExp invokeStringToInt = new ASimpleInvokeExp(new TIdentifier("StringToInt"),
                                                                      new ArrayList() { invokeStringSub5 });
            AAssignmentExp assignment14 = new AAssignmentExp(new TAssign("="), phase2Ref8, invokeStringToInt);
            methodBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment14));

            //return StringSub(output, phase1 + 1, phase2 + phase1);
            AIntConstExp intConst22 = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp binop20 = new ABinopExp(phase1Ref10Exp, new APlusBinop(new TPlus("+")), intConst22);
            ABinopExp binop21 = new ABinopExp(phase2Ref9Exp, new APlusBinop(new TPlus("+")), phase1Ref11Exp);
            ASimpleInvokeExp invokeStringSub6 = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                           new ArrayList() { outputRef12Exp, binop20, binop21 });
            methodBlock.GetStatements().Add(new AValueReturnStm(new TReturn("return"), invokeStringSub6));

            AMethodDecl method = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                 new ANamedType(new TIdentifier("string"), null),
                                                 new TIdentifier("Galaxypp_Deobfuscate"), new ArrayList() {stringParam},
                                                 methodBlock);

            //Fix data refferences.. I got tired here.

            return method;
        }
 public override void OutAFieldDecl(AFieldDecl node)
 {
     data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(currentSourceFile, node));
     data.UserFields.Add(node);
     base.OutAFieldDecl(node);
 }
 public VariableDecl(AFieldDecl field, AALocalDecl local, VariableDecl @base)
 {
     Field = field;
     Local = local;
     Base = @base;
 }
        public override void CaseAFieldDecl(AFieldDecl node)
        {
            //Check that it is const
            if (node.GetConst() != null && !data.ObfuscationFields.Contains(node))
            {
                if (node.GetInit() == null)
                {
                    errors.Add(new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText112")));
                    return;
                }
                ConstChecker checker = new ConstChecker(data);
                node.GetInit().Apply(checker);
                if (!checker.IsConst)
                {
                    errors.Add(new ErrorCollection.Error(node.GetName(), LocRM.GetString("ErrorText113")));
                    return;
                }
            }

            base.CaseAFieldDecl(node);
        }