Esempio n. 1
0
        /// <summary>
        /// Parses a given portrait.gfx file.
        /// </summary>
        /// <param name="filename">Path of the file to parse.</param>
        private void Parse(string filename, PortraitData data)
        {
            if (!File.Exists(filename))
            {
                logger.Error(string.Format("File not found: {0}", filename));
                return;
            }
            // Exclude vanilla files with known errors
            if (filename.EndsWith("DefaultDialog.gfx") || filename.EndsWith("EU3_mapitems.gfx") || filename.EndsWith("chatfonts.gfx") ||
                filename.EndsWith("fonts.gfx") || filename.EndsWith("mapitems.gfx"))
            {
                logger.Info(string.Format("Skipping parsing of file: {0}", filename));
                return;
            }

            StreamReader stream      = new StreamReader(filename, Encoding.GetEncoding(1252));
            string       fileContent = stream.ReadToEnd();

            stream.Dispose();

            //Check the file isn't empty
            string[] lines   = fileContent.Split(fileContent.Contains('\r') ? '\r' : '\n');
            bool     isEmpty = true;

            for (int i = 0; i < lines.Length; i++)
            {
                if (!lines[i].Trim().StartsWith("#") && !String.IsNullOrEmpty(lines[i].Trim()))
                {
                    isEmpty = false;
                    break;
                }
            }

            if (isEmpty)
            {
                logger.Warn("File is empty: " + filename);
                return;
            }

            //Parse the file
            PortraitReaderLexer  lexer  = new PortraitReaderLexer(fileContent);
            PortraitReaderParser parser = new PortraitReaderParser(lexer);

            ParseResult result = parser.Parse();

            if (!result.IsSuccess)
            {
                logger.Error(String.Format("Lexical error in file {0}, line {1}", (new FileInfo(filename).Name), string.Concat(result.Errors)));
                return;
            }

            ParseTree(result.Root, filename, data);
        }
Esempio n. 2
0
        /// <summary>
        /// Parses a given portrait.gfx file.
        /// </summary>
        /// <param name="filename">Path of the file to parse.</param>
        private void Parse(string filename, PortraitData data)
        {
            var fi = new FileInfo(filename);

            if (!fi.Exists)
            {
                logger.LogError($"File not found: {filename}");
                return;
            }

            // Exclude vanilla files with known errors
            if (BadFiles.Contains(fi.Name))
            {
                logger.LogInformation($"Skipping parsing of file: {filename}");
                return;
            }

            //Check the file isn't empty
            var hasContent = File.ReadLines(filename, WesternEncoding)
                             .Select(l => l.Trim())
                             .Any(l => !l.StartsWith("#"));

            if (!hasContent)
            {
                logger.LogWarning($"File is empty: {filename}");
                return;
            }

            ParseResult result;

            using (var fs = File.OpenRead(filename))
                using (var fileReader = new StreamReader(fs, WesternEncoding))
                {
                    //Parse the file
                    PortraitReaderLexer  lexer  = new PortraitReaderLexer(fileReader);
                    PortraitReaderParser parser = new PortraitReaderParser(lexer);

                    result = parser.Parse();
                }

            if (!result.IsSuccess)
            {
                logger.LogError($"Lexical error in file {fi.Name}, line {result.Errors}");
                return;
            }

            ParseTree(result.Root, filename, data);
        }