Esempio n. 1
0
 public override void ProcessNode(GroupingNode node)
 {
     foreach (NodeBase child in node.Children)
     {
         child.VisitThis(this);
     }
 }
Esempio n. 2
0
            public void PerformInsertions(GroupingNode grouping)
            {
                m_inserts.Sort(delegate(InsertionRequest x, InsertionRequest y) { return(x.Index.CompareTo(y.Index)); });

                int count = m_inserts.Count;

                for (int i = 0; i < count; i++)
                {
                    InsertionRequest ir = m_inserts[i];
                    grouping.Insert(ir.Index + i, ir.Item);
                }
            }
        public override void ProcessNode(ConditionalConstructNode node)
        {
            List <NodeBase> conditions         = node.Conditions;
            List <NodeBase> expressionSets     = node.ExpressionSets;
            GroupingNode    elseExpressionNode = node.ElseExpressionNode;

            ProcessConditionalSection(ConditionalStage.If, conditions[0], expressionSets[0] as GroupingNode);

            int lastElifIndex = conditions.Count - 2;

            for (int i = 1; i <= lastElifIndex; i++)
            {
                ProcessConditionalSection(ConditionalStage.ElseIf, conditions[i], expressionSets[i] as GroupingNode);
            }

            if (elseExpressionNode != null)
            {
                ProcessConditionalSection(ConditionalStage.Else, null, elseExpressionNode);
            }
        }
Esempio n. 4
0
        public override void ProcessNode(BslFunctionCallNode node)
        {
            ExpressionReference expressionReference = m_decompilerLookup.FunctionOpCodeLookup[node.OperationCode];

            FunctionReference functionReference;
            OperatorReference operatorReference;
            GlobalReference   globalReference;

            if ((functionReference = expressionReference as FunctionReference) != null)
            {
                if (functionReference.Function.IsOverloadedByBooleanArgument)
                {
                    node.Children.Add(new BoolValueNode(functionReference.Overload == 1));
                }
                node.Replace(new PslFunctionUsageNode(functionReference.Function, node.Children));

                foreach (NodeBase child in node.Children)
                {
                    child.VisitThis(this);
                }
            }
            else if ((operatorReference = expressionReference as OperatorReference) != null)
            {
                switch (operatorReference.Operation.Specification)
                {
                case FunctionSpecification.Begin:
                    node.Replace(new GroupingNode(node.Children));
                    foreach (NodeBase child in node.Children)
                    {
                        child.VisitThis(this);
                    }
                    break;

                case FunctionSpecification.BeginRandom:
                    ValueDiscrepancy vd = SingleValueDiscrepancySearch.Perform(node.Children);

                    if (vd != null)
                    {
                        short           typeIndex             = vd.TypeIndex;
                        string          pluralTypeName        = m_engineDefinition.GetTypeName(typeIndex);
                        List <NodeBase> randomizerExpressions = node.Children[0].Children;
                        VariableNode    iteratorParameter     = new VariableNode(pluralTypeName + "s", typeIndex, true, true, null);
                        vd.Nodes[0].Replace(new VariableReferenceValueNode(iteratorParameter, typeIndex));
                        List <NodeBase> parameters = new List <NodeBase>();
                        parameters.Add(iteratorParameter);
                        ScriptNode randomFunction = new ScriptNode("Randomize" + StringOps.CapitalizeWords(pluralTypeName), ScriptType.Random, (short)IntegralValueType.Void, randomizerExpressions);
                        m_scriptInsertionQueue.QueueInsertion(m_state.CurrentScriptIndex, randomFunction);

                        List <NodeBase> randomFunctionArguments = new List <NodeBase>();
                        randomFunctionArguments.Add(new PslArrayNode(typeIndex, vd.Nodes));

                        node.Replace(new ScriptCallNode(randomFunction, randomFunctionArguments));

                        foreach (NodeBase child in node.Children)
                        {
                            child.VisitThis(this);
                        }
                    }
                    break;

                case FunctionSpecification.If:
                    // Early recursion to identify GroupingNodes and sub Ifs before constructing conditional node
                    foreach (NodeBase child in node.Children)
                    {
                        child.VisitThis(this);
                    }

                    ConditionalConstructNode conditionalConstruct = null;

                    NodeBase     condition   = node.Children[0];
                    GroupingNode expressions = GroupingNode.MakeGrouping(node.Children[1]);

                    GroupingNode elseExpressions = null;
                    if (node.Children.Count > 2)
                    {
                        elseExpressions = GroupingNode.MakeGrouping(node.Children[2]);

                        ConditionalConstructNode embeddedIf;
                        if (elseExpressions.Children.Count == 1 && (embeddedIf = elseExpressions.Children[0] as ConditionalConstructNode) != null)
                        {
                            BoolValueNode potentialAlwaysTrueStatement = embeddedIf.Conditions[0] as BoolValueNode;
                            if (potentialAlwaysTrueStatement != null && potentialAlwaysTrueStatement.Value)
                            {
                                elseExpressions = embeddedIf.ExpressionSets[0] as GroupingNode;
                            }
                            else
                            {
                                conditionalConstruct = embeddedIf;
                                conditionalConstruct.InsertConditional(condition, expressions);
                            }
                        }
                    }

                    if (conditionalConstruct == null)
                    {
                        List <NodeBase> conditions = new List <NodeBase>();
                        conditions.Add(condition);
                        List <NodeBase> expressionSets = new List <NodeBase>();
                        expressionSets.Add(expressions);
                        conditionalConstruct = new ConditionalConstructNode(conditions, expressionSets, elseExpressions);
                    }

                    node.Replace(conditionalConstruct);
                    break;

                case FunctionSpecification.Cond:
                    throw new NeedMoreResearchException("How does cond look in compiled form - how are its children mapped out?");

                //node.Replace(new ConditionalConstructNode());
                //break;
                default:
                    List <NodeBase> children = node.Children;
                    node.Replace(new PslOperatorUsageNode(operatorReference.Operation, children));
                    foreach (NodeBase child in children)
                    {
                        child.VisitThis(this);
                    }
                    break;
                }
            }
            else if ((globalReference = expressionReference as GlobalReference) != null)
            {
                switch (globalReference.Method)
                {
                case GlobalReference.AccessMethod.Get:
                    node.Replace(new PslGameGlobalReferenceNode(globalReference.Global));
                    break;

                case GlobalReference.AccessMethod.Set:
                    List <NodeBase> operands = new List <NodeBase>();
                    operands.Add(new PslGameGlobalReferenceNode(globalReference.Global));
                    operands.Add(node.Children[0]);
                    node.Replace(new PslOperatorUsageNode(m_engineDefinition.SpecificFunctions[(int)FunctionSpecification.Set], operands));

                    operands[1].VisitThis(this);
                    break;
                }
            }
            else if (expressionReference is CasterReference)
            {
                node.Replace(node.Children[0]);
                node.Children[0].VisitThis(this);
            }
        }
 public override void ProcessNode(GroupingNode node)
 {
 }
        private void ProcessConditionalSection(ConditionalStage stage, NodeBase condition, GroupingNode expressionNode)
        {
            switch (stage)
            {
            case ConditionalStage.If:
                m_output.Append("if ");
                break;

            case ConditionalStage.ElseIf:
                m_output.Append("else if ");
                break;

            case ConditionalStage.Else:
                m_output.Append("else");
                condition = null;
                break;
            }
            if (condition != null)
            {
                m_output.Append(OPEN_BRACKET);
                condition.VisitThis(this);
                m_output.Append(CLOSE_BRACKET);
            }

            List <NodeBase> expressions         = expressionNode.Children;
            int             lastExpressionIndex = expressions.Count - 1;

            if (lastExpressionIndex > 0)
            {
                BeginBlock();
                m_tierStateStack.Push(new PslInspectionTierState(PslContext.Root, false));

                for (int i = 0; i <= lastExpressionIndex; i++)
                {
                    expressions[i].VisitThis(this);
                    if (i != lastExpressionIndex)
                    {
                        NewLine();
                    }
                }

                m_tierStateStack.Pop();
                EndBlock();
            }
            else
            {
                NewLine(1);
                m_tierStateStack.Push(new PslInspectionTierState(PslContext.Inline, false));
                expressions[0].VisitThis(this);
                NewLine();
                m_tierStateStack.Pop();
            }
        }
 public override void ProcessNode(GroupingNode node)
 {
     throw new OnlyForDebuggingException("Grouping nodes apparently do get visited.");
     //foreach (NodeBase child in node.Children)
     //	child.VisitThis(this);
 }
 public abstract void ProcessNode(GroupingNode node);
Esempio n. 9
0
        /// <summary>
        /// Reads a VRML file at a specified path, parses the information, and returns the contained models.
        /// </summary>
        /// <param name="filePath">A full path to a valid VRML file.</param>
        /// <returns>The extracted models.</returns>
        public List <Mesh> Read(string filePath)
        {
            string fullPath = Environment.ExpandEnvironmentVariables(filePath);

            Logger.Info("Parsing .wrl file at path \"" + fullPath + "\"");

            FileInfo fileInfo = new FileInfo(fullPath);

            if (!fileInfo.Exists)
            {
                Logger.Error("File \"" + fullPath + "\" does not exist!");
                return(null);
            }

            // parse the Vrml file
            VrmlScene scene;

            using (var stream = new StreamReader(filePath))
            {
                VrmlParser parser = new VrmlParser(new VrmlTokenizer(stream));
                scene = parser.Parse();
            }

            List <Mesh>            meshes             = new List <Mesh>();
            MeshBuilder            builder            = new MeshBuilder();
            Dictionary <int, uint> indexToVertexIndex = new Dictionary <int, uint>();
            List <uint>            triangleIndices    = new List <uint>();

            // Do a depth first search on the VRML scene to find all meshes
            Stack <GroupingNode> toVisit = new Stack <GroupingNode>();

            toVisit.Push(scene.Root);

            HashSet <Node> visited = new HashSet <Node>(toVisit);

            string currentDescription = "DefaultName";

            while (toVisit.Count > 0)
            {
                GroupingNode node = toVisit.Pop();

                // keep track of the most recent description in the heirarchy to name child meshes using
                if (node is AnchorNode)
                {
                    currentDescription = (node as AnchorNode).Description;
                }

                foreach (Node child in node.Children)
                {
                    // If the child node may have children nodes we must visit it if we have not already
                    if (child is GroupingNode)
                    {
                        if (!visited.Contains(child))
                        {
                            toVisit.Push(child as GroupingNode);
                            visited.Add(child);
                        }
                    }
                    // If the node is a mesh extract it
                    if (child is ShapeNode)
                    {
                        ShapeNode shape = child as ShapeNode;

                        if (shape.Geometry.Node is IndexedFaceSetNode)
                        {
                            IndexedFaceSetNode indexFaceSet = shape.Geometry.Node as IndexedFaceSetNode;
                            CoordinateNode     coords       = indexFaceSet.Coord.Node as CoordinateNode;

                            // Must have at least one index given to be a mesh
                            if (indexFaceSet.CoordIndex.Length == 0)
                            {
                                continue;
                            }

                            // The indicies in the mesh we are building may not match those in the file if
                            // not all points from the coords are used, so maintain a mapping from original
                            // index in the file to the corresponding index in the mesh currently being built.
                            builder.Clear();
                            indexToVertexIndex.Clear();
                            triangleIndices.Clear();

                            foreach (SFInt32 index in indexFaceSet.CoordIndex)
                            {
                                if (index < 0)
                                {
                                    builder.AddTriangle(new Triangle(triangleIndices[0], triangleIndices[1], triangleIndices[2]));
                                    triangleIndices.Clear();
                                }
                                else
                                {
                                    uint i;
                                    if (!indexToVertexIndex.TryGetValue(index, out i))
                                    {
                                        SFVec3f pos = coords.Point.GetValue(index);

                                        i = (uint)builder.VertexCount;
                                        indexToVertexIndex.Add(index.Value, i);
                                        builder.AddVertex(new Vector3(pos.X, pos.Y, pos.Z));
                                    }
                                    triangleIndices.Add(i);
                                }
                            }

                            // Generate the mesh and calculate the vertex normals
                            Mesh mesh = builder.CreateMesh(currentDescription);
                            meshes.Add(mesh);
                            Logger.Info($"Found Mesh: {mesh}");
                        }
                    }
                }
            }

            Logger.Info("Finished parsing file");
            return(meshes);
        }
 public void InsertConditional(NodeBase condition, GroupingNode expressions)
 {
     m_conditionsNode.Insert(0, condition);
     m_expressionSetsNode.Insert(0, expressions);
 }
 public ConditionalConstructNode(List <NodeBase> conditions, List <NodeBase> expressionSets, GroupingNode elseExpressions)
     : base(elseExpressions == null ? new List <NodeBase>(new NodeBase[] { new GroupingNode(conditions), new GroupingNode(expressionSets) }) : new List <NodeBase>(new NodeBase[] { new GroupingNode(conditions), new GroupingNode(expressionSets), elseExpressions }))
 {
 }