Esempio n. 1
0
        internal void ObjectProcessComponent(int index, LineTypes.Component line, IParentObject obj, Stack<GameObject> objectStack, Stack<ParserContext> contextStack, ParserContext context)
        {
            #region Object context
            if (context == this.objectContext) {
                switch (index) {
                    case 0:
                    case 2:
                        return;
                    case 1:
                        MeshFilter filter = objectStack.Peek().AddComponent<MeshFilter>();
                        MeshRenderer renderer = objectStack.Peek().AddComponent<MeshRenderer>();
                        filter.mesh = Utility.MeshHelper.GetQuad();
                        renderer.material = new Material(Shader.Find("Standard"));
                        return;
                }
            }
            #endregion

            if (context == this.objectContext.Components[2].context) {
                // COLLIDER

                // todo: this
            }

            // other components that have sub-components

            throw new Exception("Invalid member in parsed lines list");
        }
Esempio n. 2
0
        internal virtual void SetContext()
        {
            this.context = new ParserContext(
                false,
                new LineTypes.Property[0],
                new LineTypes.Component[0],
                ParserContext.ContextType.Root
                );

            SetObjectContext();
        }
Esempio n. 3
0
 internal virtual void ProcessProperty(int index, LineTypes.Property line, IParentObject obj, Stack<GameObject> objectStack, ParserContext context)
 {
     // overload with switch-case and handle functionalities
     throw new Exception("Invalid member in parsed lines list");
 }
Esempio n. 4
0
        internal void Parse(CountingReader reader, ParserContext context)
        {
            while (!reader.EndOfStream) {

                string line = reader.ReadLine();
                line = line.Trim();
                LineType type = GetLineType(line);

                if (type == LineType.Empty || type == LineType.Comment) continue;
                if (type == LineType.Unknown) throw new ParserExceptions.UnknownLineTypeException();

                if (type == LineType.ComponentClose) {
                    if (context.Type != ParserContext.ContextType.Component)
                        throw new Exception("Syntax error - Cannot end component outside of component block");

                    Lines.Add(new LineTypes.ComponentClose());
                    return;
                }
                if (type == LineType.ObjectClose) {
                    if (context.Type != ParserContext.ContextType.Object)
                        throw new Exception("Syntax error - Cannot end object outside of object block");

                    Lines.Add(new LineTypes.ObjectClose());
                    return;
                }

                if (type == LineType.Property) {
                    if (context.Properties.Length == 0) throw new Exception("Properties are not allowed in this context. Valid members are: " + context.ListValidMembers());

                    string propName = line.Split('=')[0].Trim().ToLower();

                    bool found = false;
                    for (int i = 0; i < context.Properties.Length; i++) {
                        if (propName == context.Properties[i].name.ToLower()) {
                            found = true;
                            Lines.Add(GetProperty(line, context.Properties[i]));
                            break;
                        }
                    }

                    if (!found) throw new Exception("Invalid property member \"" + propName + "\". Valid members are: " + context.ListValidMembers());
                    continue;
                }
                if (type == LineType.Component) {
                    if (context.Components.Length == 0) throw new Exception("Components are not allowed in this context. Valid members are: "+context.ListValidMembers());

                    string compName = line.Substring(0, line.IndexOf('(')).Trim().ToLower();

                    bool found = false;
                    for (int i = 0; i < context.Components.Length; i++) {
                        if (compName == context.Components[i].name.ToLower()) {
                            found = true;
                            Lines.Add(new LineTypes.Component(context.Components[i].name, context.Components[i].context));
                            Parse(reader, context.Components[i].context);
                            break;
                        }
                    }

                    if (!found) throw new Exception("Invalid component member \"" + compName + "\". Valid members are: " + context.ListValidMembers());
                    continue;
                }
                if (type == LineType.Object) {
                    if (!context.ObjectsAllowed) throw new Exception("Objects are not allowed in this context. Valid members are: "+context.ListValidMembers());

                    string objectName = line.Substring(0, line.IndexOf('[')).Trim();
                    Lines.Add(new LineTypes.Object(objectName));
                    Parse(reader, this.objectContext);

                    continue;
                }

            }
        }
Esempio n. 5
0
        internal void ObjectProcessProperty(int index, LineTypes.Property line, IParentObject obj, Stack<GameObject> objectStack, ParserContext context)
        {
            #region Object context
            if (context == this.objectContext) {
                // properties for object itself (none atm)
                // return;
            }
            #endregion

            #region Transform
            if (context == this.objectContext.Components[0].context) {
                switch (index) {
                    case 0: // position
                        objectStack.Peek().transform.localPosition = (Vector3)line.argumentsData[0];
                        return;
                    case 1: // rotation
                        objectStack.Peek().transform.localRotation = Quaternion.Euler((Vector3)line.argumentsData[0]);
                        return;
                    case 2: // scale
                        objectStack.Peek().transform.localScale = (Vector3)line.argumentsData[0];
                        return;
                }
            }
            #endregion

            #region Renderer
            if (context == this.objectContext.Components[1].context) {

                MeshRenderer renderer = objectStack.Peek().GetComponent<MeshRenderer>();

                switch (index) {
                    case 0: // texture
                        renderer.sharedMaterial.SetTexture(line.argumentsData[0].ToString(), (Texture2D)line.argumentsData[1]);
                        return;
                    case 1: // texture scale
                        renderer.sharedMaterial.SetTextureScale(line.argumentsData[0].ToString(), (Vector2)line.argumentsData[1]);
                        return;
                    case 2: // texture offset
                        renderer.sharedMaterial.SetTextureOffset(line.argumentsData[0].ToString(), (Vector2)line.argumentsData[1]);
                        return;
                    case 3: // color
                        renderer.sharedMaterial.SetColor(line.argumentsData[0].ToString(), (Color)line.argumentsData[1]);
                        return;
                    case 4: // shader
                        renderer.sharedMaterial = new Material(Shader.Find(line.argumentsData[0].ToString()));
                        return;
                    case 5: // rendering mode
                        string mode = line.argumentsData[0].ToString().Trim().ToLower();
                        switch (mode) {
                            case "opaque":
                                renderer.sharedMaterial.SetFloat("_Mode", 0);
                                break;
                            case "cutout":
                                renderer.sharedMaterial.SetFloat("_Mode", 1);
                                break;
                            case "fade":
                                renderer.sharedMaterial.SetFloat("_Mode", 2);
                                break;
                            case "transparent":
                                renderer.sharedMaterial.SetFloat("_Mode", 3);
                                break;
                            default:
                                throw new ParserExceptions.ParserEnumException(mode, "Opaque", "Cutout", "Fade", "Transparent");
                        }
                        return;
                    default:
                        throw new Exception("Invalid member in parsed lines list");
                }
            }
            #endregion

            // todo: other contexts

            throw new Exception("Invalid member in parsed lines list");
        }
Esempio n. 6
0
        private void SetObjectContext()
        {
            // SUB-CONTEXTS

            #region Transform
            ParserContext transformContext = new ParserContext(
                false,
                new LineTypes.Property[] {
                    new LineTypes.Property("Position", "Position = X, Y, Z", LineTypes.Property.ArgumentTypes.Vector3),
                    new LineTypes.Property("Rotation", "Rotation = X, Y, Z", LineTypes.Property.ArgumentTypes.Vector3),
                    new LineTypes.Property("Scale", "Scale = X, Y, Z", LineTypes.Property.ArgumentTypes.Vector3),
                    // todo: add layer (first add enum handling)
                },
                new LineTypes.Component[] { },
                ParserContext.ContextType.Component
                );
            #endregion
            #region Renderer
            ParserContext rendererContext = new ParserContext(
                false,
                new LineTypes.Property[] {
                    new LineTypes.Property("Texture", "Texture = NAME = PATH", LineTypes.Property.ArgumentTypes.String, LineTypes.Property.ArgumentTypes.TexturePath),
                    new LineTypes.Property("TextureScale", "TextureScale = NAME = XSCALE, YSCALE", LineTypes.Property.ArgumentTypes.String, LineTypes.Property.ArgumentTypes.Vector2),
                    new LineTypes.Property("TextureOffset", "TextureOffset = NAME = XOFFSET, YOFFSET", LineTypes.Property.ArgumentTypes.String, LineTypes.Property.ArgumentTypes.Vector2),
                    new LineTypes.Property("Color", "Color = NAME = VALUE", LineTypes.Property.ArgumentTypes.String, LineTypes.Property.ArgumentTypes.Color),
                    new LineTypes.Property("Shader", "Shader = NAME", LineTypes.Property.ArgumentTypes.String),
                    new LineTypes.Property("Mode", "Mode = VALUE", LineTypes.Property.ArgumentTypes.String),
                },
                new LineTypes.Component[] { },
                ParserContext.ContextType.Component
                );

            #endregion
            #region Collider

            ParserContext physicsMaterialContext = new ParserContext(
                false,
                new LineTypes.Property[] {
                    new LineTypes.Property("Friction", "Friction = VALUE", LineTypes.Property.ArgumentTypes.Float),
                    new LineTypes.Property("Bounciness", "Bounciness = VALUE", LineTypes.Property.ArgumentTypes.Float),
                },
                new LineTypes.Component[] { },
                ParserContext.ContextType.Component
                );

            ParserContext boxColliderContext = new ParserContext(
                false,
                new LineTypes.Property[] {
                    new LineTypes.Property("Center", "Center = X, Y, Z", LineTypes.Property.ArgumentTypes.Vector3),
                    new LineTypes.Property("Size", "Size = X, Y, Z", LineTypes.Property.ArgumentTypes.Vector3),
                },
                new LineTypes.Component[] {
                    new LineTypes.Component("Material", physicsMaterialContext)
                },
                ParserContext.ContextType.Component
                );

            ParserContext circleColliderContext = new ParserContext(
                false,
                new LineTypes.Property[] {
                    new LineTypes.Property("Center", "Center = X, Y, Z", LineTypes.Property.ArgumentTypes.Vector3),
                    new LineTypes.Property("Radius", "Radius = VALUE", LineTypes.Property.ArgumentTypes.Float),
                },
                new LineTypes.Component[] {
                    new LineTypes.Component("Material", physicsMaterialContext)
                },
                ParserContext.ContextType.Component
                );

            ParserContext colliderContext = new ParserContext(
                false,
                new LineTypes.Property[] { },
                new LineTypes.Component[] {
                    new LineTypes.Component("Box", boxColliderContext),
                    new LineTypes.Component("Circle", circleColliderContext)
                    // todo: add polygon collider
                },
                ParserContext.ContextType.Component
                );

            #endregion
            // todo: add lights, interaction, scripts, resources.....

            // MAIN CONTEXT

            this.objectContext = new ParserContext(
                true,
                new LineTypes.Property[0],
                new LineTypes.Component[] {
                    new LineTypes.Component("Transform", transformContext),
                    new LineTypes.Component("Renderer", rendererContext),
                    new LineTypes.Component("Collider", colliderContext),
                },
                ParserContext.ContextType.Object
                );
        }
Esempio n. 7
0
 public Component(string name, ParserContext context)
 {
     this.name = name;
     this.context = context;
 }
Esempio n. 8
0
 internal virtual void ProcessComponent(int index, LineTypes.Component line, IParentObject obj, Stack <GameObject> objectStack, Stack <ParserContext> contextStack, ParserContext context)
 {
     // overload with switch-case and handle functionalities
     throw new Exception("Invalid member in parsed lines list");
 }
Esempio n. 9
0
        private void ProcessLine(IParentObject rootObject, Stack <ParserContext> contextStack, Stack <GameObject> objectStack, IParserLine tempLine)
        {
            LineType      type           = tempLine.GetType();
            ParserContext currentContext = contextStack.Peek();

            int index = -1;

            switch (type)
            {
            case LineType.Property:
                LineTypes.Property lineProperty = (LineTypes.Property)tempLine;

                for (int i = 0; i < currentContext.Properties.Length; i++)
                {
                    if (currentContext.Properties[i].name == lineProperty.name)
                    {
                        index = i;
                        break;
                    }
                }
                if (index == -1)
                {
                    throw new Exception("Invalid member in parsed lines list");
                }
                ProcessProperty(index, lineProperty, rootObject, objectStack, currentContext);
                break;

            case LineType.Component:
                LineTypes.Component lineComponent = (LineTypes.Component)tempLine;

                for (int i = 0; i < currentContext.Components.Length; i++)
                {
                    if (currentContext.Components[i].name == lineComponent.name)
                    {
                        index = i;
                        break;
                    }
                }
                if (index == -1)
                {
                    throw new Exception("Invalid member in parsed lines list");
                }
                ProcessComponent(index, lineComponent, rootObject, objectStack, contextStack, currentContext);
                contextStack.Push(currentContext.Components[index].context);
                break;

            case LineType.Object:
                LineTypes.Object lineObject = (LineTypes.Object)tempLine;

                GameObject tempGO = new GameObject();
                tempGO.name = lineObject.name;
                tempGO.SetActive(false);

                if (objectStack.Count > 0)
                {
                    tempGO.transform.parent = objectStack.Peek().transform;
                }
                rootObject.AssignChild(tempGO);

                objectStack.Push(tempGO);
                contextStack.Push(this.objectContext);
                break;

            case LineType.ObjectClose:
            case LineType.ComponentClose:
                contextStack.Pop();
                break;

            default:
                throw new System.Exception("Invalid line type in parsed lines list");
            }
        }
Esempio n. 10
0
        internal void Parse(CountingReader reader, ParserContext context)
        {
            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                line = line.Trim();
                LineType type = GetLineType(line);

                if (type == LineType.Empty || type == LineType.Comment)
                {
                    continue;
                }
                if (type == LineType.Unknown)
                {
                    throw new ParserExceptions.UnknownLineTypeException();
                }

                if (type == LineType.ComponentClose)
                {
                    if (context.Type != ParserContext.ContextType.Component)
                    {
                        throw new Exception("Syntax error - Cannot end component outside of component block");
                    }

                    Lines.Add(new LineTypes.ComponentClose());
                    return;
                }
                if (type == LineType.ObjectClose)
                {
                    if (context.Type != ParserContext.ContextType.Object)
                    {
                        throw new Exception("Syntax error - Cannot end object outside of object block");
                    }

                    Lines.Add(new LineTypes.ObjectClose());
                    return;
                }

                if (type == LineType.Property)
                {
                    if (context.Properties.Length == 0)
                    {
                        throw new Exception("Properties are not allowed in this context. Valid members are: " + context.ListValidMembers());
                    }

                    string propName = line.Split('=')[0].Trim().ToLower();

                    bool found = false;
                    for (int i = 0; i < context.Properties.Length; i++)
                    {
                        if (propName == context.Properties[i].name.ToLower())
                        {
                            found = true;
                            Lines.Add(GetProperty(line, context.Properties[i]));
                            break;
                        }
                    }

                    if (!found)
                    {
                        throw new Exception("Invalid property member \"" + propName + "\". Valid members are: " + context.ListValidMembers());
                    }
                    continue;
                }
                if (type == LineType.Component)
                {
                    if (context.Components.Length == 0)
                    {
                        throw new Exception("Components are not allowed in this context. Valid members are: " + context.ListValidMembers());
                    }

                    string compName = line.Substring(0, line.IndexOf('(')).Trim().ToLower();

                    bool found = false;
                    for (int i = 0; i < context.Components.Length; i++)
                    {
                        if (compName == context.Components[i].name.ToLower())
                        {
                            found = true;
                            Lines.Add(new LineTypes.Component(context.Components[i].name, context.Components[i].context));
                            Parse(reader, context.Components[i].context);
                            break;
                        }
                    }

                    if (!found)
                    {
                        throw new Exception("Invalid component member \"" + compName + "\". Valid members are: " + context.ListValidMembers());
                    }
                    continue;
                }
                if (type == LineType.Object)
                {
                    if (!context.ObjectsAllowed)
                    {
                        throw new Exception("Objects are not allowed in this context. Valid members are: " + context.ListValidMembers());
                    }

                    string objectName = line.Substring(0, line.IndexOf('[')).Trim();
                    Lines.Add(new LineTypes.Object(objectName));
                    Parse(reader, this.objectContext);

                    continue;
                }
            }
        }