private void ReadReplacementFile(TextReaderWrapper tr, string textFileName)
            {
                textFileName = Path.GetFullPath(textFileName);
                if (IncludedFiles.Contains(textFileName.ToUpperInvariant()))
                {
                    return;
                }
                IncludedFiles.Set(textFileName.ToUpperInvariant());

                string line;

                while (true)
                {
                    line = tr.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    //remove initial whitespace
                    line = line.TrimStart();

                    //check for "#include"
                    if (line.StartsWith("#include "))
                    {
                        string filenameToInclude = line.Substring("#include ".Length);
                        //check for quotes?
                        if (filenameToInclude.StartsWith("\"") && filenameToInclude.EndsWith("\""))
                        {
                            filenameToInclude = filenameToInclude.Substring(1, filenameToInclude.Length - 2);
                        }
                        string basePath = tr.DirectoryName;
                        filenameToInclude = Path.Combine(basePath, filenameToInclude);
                        if (!File.Exists(filenameToInclude))
                        {
                            throw new FileNotFoundException("Cannot find file: " + filenameToInclude, filenameToInclude);
                        }

                        if (File.Exists(filenameToInclude) && !IncludedFiles.Contains(filenameToInclude.ToUpperInvariant()))
                        {
                            IncludedFiles.Add(filenameToInclude.ToUpperInvariant());
                            var encoding = EncodingDetector.DetectEncoding(filenameToInclude);
                            tr.IncludeTextReader(new StreamReader(filenameToInclude, encoding));
                        }
                        continue;
                    }


                    //remove commented text
                    int indexOfComment = line.IndexOf('#');
                    if (indexOfComment >= 0)
                    {
                        line = line.Substring(0, indexOfComment);
                    }

                    //reading one of these lines:
                    //CODE
                    //function x functionName  (or func, f)
                    //string x text (or str, s)
                    //message x text (or msg, m)
                    //id x text (or i)
                    //x text (same as id x text)

                    string lineTrim = line.Trim();

                    if (lineTrim.Equals("CODE", StringComparison.OrdinalIgnoreCase) || lineTrim.Equals("CODE2", StringComparison.OrdinalIgnoreCase))
                    {
                        bool          isCode2  = lineTrim.Equals("CODE2", StringComparison.OrdinalIgnoreCase);
                        StringBuilder codeText = new StringBuilder();
                        while (true)
                        {
                            line     = tr.ReadLine();
                            lineTrim = line.Trim();
                            if (lineTrim.StartsWith("#include"))
                            {
                                string filenameToInclude = lineTrim.Substring("#include ".Length);
                                //check for quotes?
                                if (filenameToInclude.StartsWith("\"") && filenameToInclude.EndsWith("\""))
                                {
                                    filenameToInclude = filenameToInclude.Substring(1, filenameToInclude.Length - 2);
                                }
                                string basePath = tr.DirectoryName;
                                if (!Path.IsPathRooted(filenameToInclude))
                                {
                                    filenameToInclude = Path.Combine(basePath, filenameToInclude);
                                }
                                filenameToInclude = Path.GetFullPath(filenameToInclude);

                                if (!File.Exists(filenameToInclude))
                                {
                                    throw new FileNotFoundException("Cannot find file: " + filenameToInclude, filenameToInclude);
                                }

                                if (File.Exists(filenameToInclude) && !IncludedFiles.Contains(filenameToInclude.ToUpperInvariant()))
                                {
                                    IncludedFiles.Add(filenameToInclude.ToUpperInvariant());

                                    if (isCode2)
                                    {
                                        //replace with #include <fullpath>, let the compiler handle the actual include
                                        codeText.AppendLine("#include " + AssemblerProjectWriter.EscapeAndQuoteString(filenameToInclude));
                                    }
                                    else
                                    {
                                        var encoding = EncodingDetector.DetectEncoding(filenameToInclude);
                                        tr.IncludeTextReader(new StreamReader(filenameToInclude, encoding));
                                    }
                                }
                                continue;
                            }

                            if (lineTrim.ToUpperInvariant() == "ENDCODE")
                            {
                                if (isCode2)
                                {
                                    CodePatches2.AppendLine(codeText.ToString());
                                }
                                else
                                {
                                    CodePatches.Set(currentFunctionName, codeText.ToString());
                                }
                                break;
                            }
                            else
                            {
                                codeText.AppendLine(line);
                            }
                        }
                        continue;
                    }

                    //find first space
                    int spaceIndex = line.IndexOf(' ');
                    if (spaceIndex == -1)
                    {
                        continue;
                    }
                    string tagName = line.Substring(0, spaceIndex);
                    line = line.Substring(spaceIndex + 1);
                    int number;
                    //if it starts with a number, it's a legacy text replacement
                    if (IntUtil.TryParse(tagName, out number) == true)
                    {
                        tagName = "id";
                    }
                    else
                    {
                        bool   isFunction   = false;
                        string tagNameLower = tagName.ToLowerInvariant();
                        if (tagNameLower == "f" || tagNameLower == "func" || tagNameLower == "function")
                        {
                            isFunction = true;
                        }

                        line       = line.TrimStart();
                        spaceIndex = line.IndexOf(' ');
                        if (spaceIndex == -1)
                        {
                            if (isFunction)
                            {
                                number = -1;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            string numberString = line.Substring(0, spaceIndex);
                            line = line.Substring(spaceIndex + 1);

                            if (IntUtil.TryParse(numberString, out number) == false)
                            {
                                if (isFunction)
                                {
                                    line   = numberString + " " + line;
                                    number = -1;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                    }

                    line = StringExportImport.UnescapeText(line);

                    switch (tagName.ToLowerInvariant())
                    {
                    case "f":
                    case "func":
                    case "function":
                        string nextFunctionName = line.Trim();
                        var    function         = ainFile.GetFunction(number);
                        if (function == null)
                        {
                            function = ainFile.GetFunction(nextFunctionName);
                            if (function == null)
                            {
                                continue;
                            }
                        }
                        currentFunctionName = function.Name;
                        stringDictionary    = stringEntries.GetOrAddNew(currentFunctionName);
                        messageDictionary   = messageEntries.GetOrAddNew(currentFunctionName);
                        break;

                    case "string":
                    case "str":
                    case "s":
                        stringDictionary.Set(number, line);
                        break;

                    case "msg":
                    case "message":
                    case "m":
                        messageDictionary.Set(number, line);
                        break;

                    case "i":
                    case "id":
                        numberedStrings.Set(number, line);
                        break;
                    }
                }
            }
Esempio n. 2
0
        public void LoadFile(string fileName)
        {
            var text        = File.ReadAllText(fileName);
            var document    = XDocument.Parse(text);
            var rootElement = document.Element(XNamespace.None + "root");

            if (rootElement == null)
            {
                return;
            }

            foreach (var element in rootElement.Elements())
            {
                if (element.IsNamed("variable"))
                {
                    var metaData = TryReadMetadata(element);

                    string name = element.GetAttribute("name");
                    if (name != null && metaData != null)
                    {
                        IVariable variable           = null;
                        string    parentFunctionName = element.GetAttribute("function");
                        string    parentStructName   = element.GetAttribute("struct");

                        var parentFunction = ainFile.GetFunction(parentFunctionName);
                        var parentStruct   = ainFile.GetStruct(parentStructName);
                        var function       = ainFile.GetFunction(name);
                        var structInfo     = ainFile.GetStruct(name);
                        var global         = ainFile.GetGlobal(name);

                        if (parentFunction != null)
                        {
                            int index;
                            if (!int.TryParse(name, out index))
                            {
                                variable = ainFile.GetFunctionParameter(parentFunction, name);
                            }
                            else
                            {
                                variable = ainFile.GetFunctionParameter(parentFunction, index);
                            }
                        }
                        else if (parentStruct != null)
                        {
                            variable = ainFile.GetStructMember(parentStruct, name);
                        }
                        else if (function != null)
                        {
                            variable = function;
                        }
                        else if (structInfo != null)
                        {
                            variable = structInfo;
                        }
                        else if (global != null)
                        {
                            variable = global;
                        }

                        if (variable != null)
                        {
                            Metadata[variable] = metaData;
                        }
                    }
                }
                else if (element.IsNamed("enumeration"))
                {
                    string name = element.GetAttribute("name");
                    if (name != null)
                    {
                        var enumeration = this.EnumerationTypes.GetOrAddNew(name);
                        enumeration.Name = name;
                        foreach (var enumerationElement in element.Elements())
                        {
                            if (enumerationElement.IsNamed("item"))
                            {
                                string itemName        = enumerationElement.GetAttribute("name");
                                string itemValueString = enumerationElement.GetAttribute("value");
                                if (itemName != null && itemValueString != null)
                                {
                                    int itemValue;
                                    if (int.TryParse(itemValueString, out itemValue))
                                    {
                                        enumeration.Add(itemValue, itemName);
                                    }
                                }
                            }
                        }
                    }
                }
                else if (element.IsNamed("globalgroup"))
                {
                    string indexString = element.GetAttribute("index");
                    int    index;
                    if (IntUtil.TryParse(indexString, out index))
                    {
                        var metaData = TryReadMetadata(element);
                        if (metaData != null)
                        {
                            this.GlobalGroupMetadata.Set(index, metaData);
                        }
                    }
                }
            }
        }