예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public ShaderValidation AddGeneralDirective(IGeneralDirective value)
        {
            if (value == null)
            {
                throw new ShaderFileStructureException("Invalid general directive");
            }

            string errorMessage = null;

            if (this.GeneralDirectives.Any(g => g.GetType() == value.GetType() /*TODO: sun*/))
            {
                errorMessage = "Duplicate general directive";
            }
            else if (value is IDeformVertexes && GeneralDirectives.Any(g => g is IDeformVertexes))
            {
                errorMessage = "Duplicate deformVertexes-directive";
            }

            errorMessage = errorMessage + "";

            Logger.Debug($"Added general directive {value.GetType()} to shader.");
            this.GeneralDirectives.Add(value);
            return(null); // TODO
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        private Shader ParseShader()
        {
            PassWhiteSpace();

            // TODO: check if this check is ever needed
            if (Position == ParserPosition.EndOfFile)
            {
                throw new ShaderFileStructureException($"Unexpected end of file.", Index, Path);
            }

            // Get shader, move forward one line and set reader depth
            string shaderName = ParseShaderName();

            if (shaderName == null)
            {
                Logger.Error($"Invalid shader name '{CurrentLine}'", Index, Path);
            }

            Shader shader = new Shader(shaderName);

            Index++;
            Position = ParserPosition.InShader;

            // Skip whitespace and comments
            PassWhiteSpace();

            // Expect an opening brace { after shader name definition
            if (CurrentLine != Token.OpeningBrace)
            {
                throw new ShaderFileStructureException($"Expecting {Token.OpeningBrace}", Index, Path);
            }

            // Move past opening brace, we are now inside the shader
            Position = ParserPosition.InShader;

            while (Position != ParserPosition.Outside)
            {
                // Move forward until hitting some text
                Index++;
                PassWhiteSpace();

                // Parse editor level
                if (Position == ParserPosition.InShader)
                {
                    // Reached end of current shader block
                    if (CurrentLine == Token.ClosingBrace)
                    {
                        Position = ParserPosition.Outside;
                        continue;
                    }

                    // Hit a stage
                    if (CurrentLine == Token.OpeningBrace)
                    {
                        Position = ParserPosition.InStage;
                        continue;
                    }

                    // Parse surfaceparms
                    if (LineStartsWith(Token.surfaceparm))
                    {
                        // Add shader, and log a warning if it's already added
                        Surfaceparms?parsed = ParseSurfaceparm();

                        if (parsed.HasValue)
                        {
                            if (!shader.Surfparms.Add(parsed.Value))
                            {
                                Logger.Warn($"Duplicate surfaceparm {CurrentLine}.", Index, Path);
                            }
                        }
                        else
                        {
                            Logger.Warn($"Invalid surfaceparm {CurrentLine}.", Index, Path);
                        }

                        continue;
                    }
                    else
                    {
                        IGeneralDirective parsed = ParseGeneralDirective();

                        if (parsed != null)
                        {
                            string err = "";//shader.AddGeneralDirective(parsed);

                            if (!string.IsNullOrEmpty(err))
                            {
                                Logger.Warn($"{err} '{CurrentLine}'.", Index, Path);
                            }
                        }

                        continue;
                    }
                }

                // Parse stage
                else if (Position == ParserPosition.InStage)
                {
                    // Reached end of stage
                    if (CurrentLine == Token.ClosingBrace)
                    {
                        Position = ParserPosition.Outside;
                        break;
                    }
                }
            }

            // This point should be reached when current line is the closing brace }
            Index++;
            return(shader);
        }