Beispiel #1
0
        public SiiFile DeserializeFromString(string sii)
        {
            var siiFile = new SiiFile();

            sii = RemoveComments(sii);

            siiFile.GlobalScope = sii.StartsWith(SiiHeader);

            // get units
            string unitDeclarations = @"[\w]+?\s*:\s*\""?[\w.]+?\""?\s*{"; // don't @ me

            foreach (Match m in Regex.Matches(sii, unitDeclarations, RegexOptions.Singleline))
            {
                // find ending bracket of this unit
                var start         = m.Index;
                var startBracket  = start + m.Length; // start after the opening bracket of the unit
                int?end           = null;
                var bracketsStack = 0;
                for (int i = startBracket; i < sii.Length; i++)
                {
                    if (sii[i] == '{')
                    {
                        bracketsStack++;
                    }
                    else if (sii[i] == '}')
                    {
                        bracketsStack--;
                    }

                    if (bracketsStack == -1)
                    {
                        end = i;
                        break;
                    }
                }

                if (end is null)
                {
                    throw new SiiParserException($"Expected '}}' at {sii.Length - 1}, found EOF");
                }
                else
                {
                    siiFile.Units.Add(ParseUnit(sii.Substring(start, end.Value - start + 1)));
                }
            }

            // parse top level includes
            GetTopLevelIncludes(sii, siiFile);

            return(siiFile);
        }
Beispiel #2
0
        public static string Serialize(MatFile mat)
        {
            var siiParser = new SiiParser
            {
                TupleAttribOpen  = "{",
                TupleAttribClose = "}"
            };

            var siiFile = new SiiFile();

            siiFile.GlobalScope = false;
            var unit = new Unit();

            siiFile.Units.Add(unit);
            unit.Class      = "material";
            unit.Name       = $"\"{mat.Effect}\"";
            unit.Attributes = mat.Attributes;

            return(siiParser.Serialize(siiFile));
        }
Beispiel #3
0
        private void GetTopLevelIncludes(string sii, SiiFile siiFile)
        {
            using var sr = new StringReader(sii);

            var bracketsStack = 0;

            // in files that use the global scope SiiNunit { },
            // ignore first bracket level:
            if (sii.StartsWith(SiiHeader))
            {
                bracketsStack = -1;
            }

            string line;

            while ((line = sr.ReadLine()) != null)
            {
                // only parse top level includes, so
                // make sure we're not inside a unit
                foreach (var character in line)
                {
                    if (character == '{')
                    {
                        ++bracketsStack;
                    }
                    else if (character == '}')
                    {
                        --bracketsStack;
                    }
                }

                if (bracketsStack == 0 && line.StartsWith(IncludeKeyword))
                {
                    var include = ParseInclude(line);
                    siiFile.Includes.Add(include);
                }
            }
        }