Example #1
0
        public void Join(LibraryData other)
        {
            int count = Methods.Count;

            foreach (AMethodDecl method in other.Methods)
            {
                bool add = true;
                for (int i = 0; i < count; i++)
                {
                    if (Util.GetMethodSignature(Methods[i]) != Util.GetMethodSignature(method))
                    {
                        add = false;
                        break;
                    }
                }
                if (add)
                {
                    Methods.Add(method);
                }
            }

            count = Fields.Count;
            foreach (AFieldDecl field in other.Fields)
            {
                bool add = true;
                for (int i = 0; i < count; i++)
                {
                    if (Fields[i].GetName().Text != field.GetName().Text)
                    {
                        add = false;
                        break;
                    }
                }
                if (add)
                {
                    Fields.Add(field);
                }
            }

            count = Structs.Count;
            for (int j = 0; j < other.Structs.Count; j++)
            {
                bool        add = true;
                AStructDecl str = other.Structs[j];
                for (int i = 0; i < count; i++)
                {
                    if (Structs[i].GetName().Text != str.GetName().Text)
                    {
                        add = false;
                        break;
                    }
                }
                if (add)
                {
                    Structs.Add(str);
                    StructMethods.Add(str, other.StructMethods[str]);
                    StructFields.Add(str, other.StructFields[str]);
                }
            }
        }
        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 void LoadLibraries()
        {
            if (Options.General.SC2Exe == null || !StarCraftExecutableFinder.checkPathValidity(Options.General.SC2Exe.FullName))
            {
                string SC2Path = StarCraftExecutableFinder.findExecutable();
                if (SC2Path != null)
                {
                    FileInfo info = new FileInfo(SC2Path);
                    Options.General.SC2Exe = info;
                }
                else
                {
                    Options.General.SC2Exe = null;
                }
            }

            libraryData = FunctionExtractor.LoadFunctions();
        }
        public void CompileLibrary(DirectoryInfo libraryDir, StreamWriter writer)
        {
            AAProgram root = new AAProgram();
            ErrorCollection errors = new ErrorCollection();

            foreach (FileInfo file in GetSourceFiles(libraryDir))
            {
                //Replace keywords
                StreamReader reader = file.OpenText();
                StringBuilder text = new StringBuilder("");
                string[] keywords = new[] {"ref", "out", "InvokeSync", "InvokeAsync",
                                                  "switch", "case", "default", "new", "delete", "this","delegate", "value", "base",
                "inline", "namespace", "using",
                                                                           "Trigger", "Initializer",
                                                                           "events", "conditions", "actions", "class",
                                                                           "typedef", "get", "set", "enrich",
                                                                           "public", "private", "protected",
                "LibraryName", "LibraryVersion", "SupportedVersions", "RequiredLibraries", "global"};

                int i;
                StringBuilder currentIdentifier = new StringBuilder("");
                while ((i = reader.Read()) != -1)
                {
                    if (Util.IsIdentifierLetter((char)i))
                    {
                        currentIdentifier.Append((char)i);
                    }
                    else
                    {
                        if (currentIdentifier.Length > 0)
                        {
                            string identifier = currentIdentifier.ToString();
                            currentIdentifier.Clear();
                            if (keywords.Contains(identifier))
                            {
                                identifier = "_" + identifier;
                            }
                            text.Append(identifier);
                        }
                        text.Append((char) i);
                    }
                }

                Parser parser = new Parser(new Lexer(new StringReader(text.ToString())));
                try
                {
                    Start start = parser.Parse();
                    AASourceFile srcFile = (AASourceFile) start.GetPSourceFile();
                    srcFile.SetName(new TIdentifier(file.FullName.Substring(libraryDir.FullName.Length + 1)));
                    root.GetSourceFiles().Add(srcFile);
                }
                catch (ParserException err)
                {
                    errors.Add(new ErrorCollection.Error(err.Token, file.Name, err.Message, false));
                }
                reader.Close();
            }

            try
            {
                Weeder.Parse(root, errors, new SharedData());
                LibraryData lib = new LibraryData(root, writer);
                FileInfo precompFile = new FileInfo(libraryDir.FullName + "\\Precompiled.LibraryData");
                IFormatter formatter = new BinaryFormatter();
                Stream stream = precompFile.Open(FileMode.Create);
                formatter.Serialize(stream, lib);
                stream.Close();
            }
            catch (Exception err)
            {

            }
            if (errors.HasErrors)
                MessageBox.Show("Errors in libray " + libraryDir.Name);
        }
        public void Join(LibraryData other)
        {
            int count = Methods.Count;
            foreach (AMethodDecl method in other.Methods)
            {
                bool add = true;
                for (int i = 0; i < count; i++)
                {
                    if (Util.GetMethodSignature(Methods[i]) != Util.GetMethodSignature(method))
                    {
                        add = false;
                        break;
                    }
                }
                if (add)
                    Methods.Add(method);
            }

            count = Fields.Count;
            foreach (AFieldDecl field in other.Fields)
            {
                bool add = true;
                for (int i = 0; i < count; i++)
                {
                    if (Fields[i].GetName().Text != field.GetName().Text)
                    {
                        add = false;
                        break;
                    }
                }
                if (add)
                    Fields.Add(field);
            }

            count = Structs.Count;
            for (int j = 0; j < other.Structs.Count; j++)
            {
                bool add = true;
                AStructDecl str = other.Structs[j];
                for (int i = 0; i < count; i++)
                {
                    if (Structs[i].GetName().Text != str.GetName().Text)
                    {
                        add = false;
                        break;
                    }
                }
                if (add)
                {
                    Structs.Add(str);
                    StructMethods.Add(str, other.StructMethods[str]);
                    StructFields.Add(str, other.StructFields[str]);
                }
            }
        }
        /// <summary>
        /// Crawls the StarCraft 2 MPQs to grab the *.galaxy files inside. These files are parsed for (native) functions and constants.
        /// </summary>
        /// <returns>The functions and constants of StarCraft 2 in form of a LibraryData</returns>
        LibraryData loadLibraries()
        {
            // parse the files for functions and constants
            CrawlAndParseMpqs();

            // convert the functions and constants to a LibraryData format
            LibraryData lib = new LibraryData();

            // create the methods with from crawled info
            foreach (ParsedFunction function in functions)
            {
                AMethodDecl method = new AMethodDecl();
                if (function.IsNative)
                    method.SetNative(new TNative("native"));
                method.SetName(new TIdentifier(function.Name));
                method.SetReturnType(new ANamedType(new TIdentifier(function.ReturnType), null));

                // add function parameter
                foreach (var parameter in function.Parameters)
                {
                    method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                                new ANamedType(new TIdentifier(parameter.Item1), null),
                                                                new TIdentifier(parameter.Item2), null));
                }

                lib.Methods.Add(method);
            }

            // create the constants from the crawled info
            foreach (ParsedConstant constant in constants)
            {
                AFieldDecl field = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const"),
                    new ANamedType(new TIdentifier(constant.Type), null),
                    new TIdentifier(constant.Name), new AStringConstExp(new TStringLiteral(constant.Value)));
                lib.Fields.Add(field);
            }

            functions.Clear();
            constants.Clear();

            return lib;
        }