protected override void Visit(YamlMappingNode mapping)
 {
     var nestedVisitor = new ContextAwareMappingVisitor(context);
     mapping.Accept(nestedVisitor);
     
     foreach (var item in nestedVisitor.Items)
     {
         this.items.Add(new KeyValuePair<string, string>(item.Key, item.Value));
     }
 }
Exemple #2
0
        protected override void Visit(YamlMappingNode mapping)
        {
            var nestedVisitor = new ContextAwareMappingVisitor(context);

            mapping.Accept(nestedVisitor);

            foreach (var item in nestedVisitor.Items)
            {
                this.items.Add(new KeyValuePair <string, string>(item.Key, item.Value));
            }
        }
    private void NewHandleFile(string file)
    {
        bool filenameWrote = false;

        var fileContent = File.ReadAllText(file);

        //unity seem to use non valid yaml by added a "stripped" keyword on object that are linked to a prefab. Since the pareser itch on those, we remove them
        fileContent = fileContent.Replace("stripped", "");
        var input = new StringReader(fileContent);

        var yaml = new YamlStream();

        yaml.Load(input);

        YamlVisitorEvent visitor = new YamlVisitorEvent();

        visitor.referenceFinder = this;

        //map gameobject id to a hashset of monobehaviour to check for type against the searched type
        Dictionary <string, HashSet <string> > gameobjectToIdToCheck = new Dictionary <string, HashSet <string> >();

        //we store the anchor <-> node mapping, as there don't seem to be anyway to do that quickly through the YAml graph
        Dictionary <string, YamlMappingNode> parsedNodes = new Dictionary <string, YamlMappingNode>();

        foreach (var doc in yaml.Documents)
        {
            var root = (YamlMappingNode)doc.RootNode;

            parsedNodes[root.Anchor] = root;

            foreach (var node in root.Children)
            {
                var scalarNode = (YamlScalarNode)node.Key;
                if (scalarNode.Value == "MonoBehaviour")
                {//if it's a monobehaviour, it may have a list of event as child
                    YamlMappingNode sequenceNode = node.Value as YamlMappingNode;

                    visitor.havePersistentCall = false;
                    visitor.idToCheck.Clear();
                    sequenceNode.Accept(visitor);

                    if (visitor.havePersistentCall)
                    {//we found persistent call
                        string gameobjectID = ((YamlScalarNode)node.Value["m_GameObject"]["fileID"]).Value;

                        if (!gameobjectToIdToCheck.ContainsKey(gameobjectID))
                        {
                            gameobjectToIdToCheck[gameobjectID] = new HashSet <string>();
                        }

                        gameobjectToIdToCheck[gameobjectID].UnionWith(visitor.idToCheck);
                    }
                }
            }
        }

        //now we go over all our gameobject to check, and if one of the monobehaviour they ahve been associated with are of the researched type, they are added to the result
        foreach (var pair in gameobjectToIdToCheck)
        {
            bool haveOneValidCall = false;
            if (!parsedNodes.ContainsKey(pair.Key))
            {
                Debug.LogError("Tried to check an object id that don't exist : " + pair.Key);
                continue;
            }

            foreach (var id in pair.Value)
            {
                var targetNode = parsedNodes[id];
                var guid       = ((YamlScalarNode)targetNode["MonoBehaviour"]["m_Script"]["guid"]).Value;

                MonoScript script = AssetDatabase.LoadAssetAtPath <MonoScript>(AssetDatabase.GUIDToAssetPath(guid));

                if (script.GetClass() == typeSearched)
                {
                    haveOneValidCall = true;
                }
            }

            if (haveOneValidCall)
            {
                if (!filenameWrote)
                {
                    filenameWrote     = true;
                    tempSearchResult += Path.GetFileName(file) + "\n";
                }

                if (((YamlScalarNode)parsedNodes[pair.Key]["GameObject"]["m_PrefabParentObject"]["fileID"]).Value != "0")
                {//this is a prefab instance, need to find the prefab value linked to it!!
                    tempSearchResult += "\t" + "A Prefab \n";
                }
                else
                {
                    string fullPath = "";

                    //make an assumption here that the 1st component of every gameobject will always be its transform.
                    string currentGOId = pair.Key;
                    while (currentGOId != "0")
                    {
                        fullPath = parsedNodes[currentGOId]["GameObject"]["m_Name"] + (fullPath == "" ? "" : "/" + fullPath);

                        string transformID = parsedNodes[currentGOId]["GameObject"]["m_Component"][0]["component"]["fileID"].ToString();

                        Debug.Log("TransformID " + transformID);

                        string parentTransformID = parsedNodes[transformID].Children.Values.ElementAt(0)["m_Father"]["fileID"].ToString();
                        Debug.Log("Parent transform ID " + parentTransformID);
                        if (parentTransformID != "0")
                        {
                            currentGOId = parsedNodes[parentTransformID].Children.Values.ElementAt(0)["m_GameObject"]["fileID"].ToString();
                        }
                        else
                        {
                            currentGOId = "0";
                        }

                        Debug.Log("currentGOID " + currentGOId);
                    }

                    tempSearchResult += "\t" + fullPath + "\n";
                }
            }
        }
    }