Exemplo n.º 1
0
        public IUnityAssetDataElement Build(SeldomInterruptChecker seldomInterruptChecker, IPsiSourceFile currentSourceFile, AssetDocument assetDocument)
        {
            if (AssetUtils.IsMonoBehaviourDocument(assetDocument.Buffer))
            {
                var anchor = AssetUtils.GetAnchorFromBuffer(assetDocument.Buffer);
                if (!anchor.HasValue)
                {
                    return(null);
                }

                var dictionary = new Dictionary <string, IAssetValue>();
                var entries    = assetDocument.Document.FindRootBlockMapEntries()?.Entries;
                if (entries == null)
                {
                    return(null);
                }

                foreach (var entry in entries)
                {
                    var key = entry.Key.GetPlainScalarText();
                    if (key == null || ourIgnoredMonoBehaviourEntries.Contains(key))
                    {
                        continue;
                    }

                    foreach (var deserializer in myDeserializers)
                    {
                        try
                        {
                            if (deserializer.TryGetInspectorValue(currentSourceFile, entry.Content, out var result))
                            {
                                dictionary[key] = result;
                                break;
                            }
                        }
                        catch (Exception e)
                        {
                            myLogger.Error(e, "An error occurred while deserializing value {0}", deserializer.GetType().Name);
                        }
                    }
                }

                if (dictionary.TryGetValue(UnityYamlConstants.ScriptProperty, out var scriptValue) && scriptValue is AssetReferenceValue referenceValue)
                {
                    var location = new LocalReference(currentSourceFile.PsiStorage.PersistentIndex, anchor.Value);
                    var script   = referenceValue.Reference;
                    var list     = new List <InspectorVariableUsage>();
                    foreach (var(key, value) in dictionary)
                    {
                        list.Add(new InspectorVariableUsage(location, script, key, value));
                    }

                    var result = new AssetInspectorValuesDataElement(list);
                    return(result);
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        private LocalReference CreateReferenceToAnimatorState()
        {
            var fileStoragePersistentIndex = myFile.PsiStorage.PersistentIndex.NotNull();
            var boxedAnchor = AssetUtils.GetAnchorFromBuffer(myDocument.Buffer);

            if (!boxedAnchor.HasValue)
            {
                throw new AnimatorExtractorException();
            }
            return(new LocalReference(fileStoragePersistentIndex, boxedAnchor.Value));
        }
        public IUnityAssetDataElement Build(SeldomInterruptChecker checker, IPsiSourceFile currentSourceFile, AssetDocument assetDocument)
        {
            // TODO: deps for other assets
            if (AssetUtils.IsMonoBehaviourDocument(assetDocument.Buffer))
            {
                var  anchorRaw = AssetUtils.GetAnchorFromBuffer(assetDocument.Buffer);
                bool stripped  = AssetUtils.IsStripped(assetDocument.Buffer);
                if (stripped) // we will handle it in prefabs
                {
                    return(null);
                }

                if (!anchorRaw.HasValue)
                {
                    return(null);
                }

                var anchor = anchorRaw.Value;

                var entries = assetDocument.Document.FindRootBlockMapEntries()?.Entries;
                if (entries == null)
                {
                    return(null);
                }
                foreach (var entry in entries)
                {
                    if (!entry.Key.MatchesPlainScalarText("m_Script"))
                    {
                        continue;
                    }

                    var deps = entry.Content.Value.AsFileID()?.ToReference(currentSourceFile);
                    if (deps == null)
                    {
                        continue;
                    }

                    return(new AssetUsagesDataElement(
                               new AssetUsage(
                                   new LocalReference(currentSourceFile.PsiStorage.PersistentIndex, anchor), new [] { deps })));
                }
            }

            return(null);
        }
Exemplo n.º 4
0
        public object Build(SeldomInterruptChecker checker, IPsiSourceFile currentAssetSourceFile, AssetDocument assetDocument)
        {
            // TODO: deps for other assets
            if (AssetUtils.IsMonoBehaviourDocument(assetDocument.Buffer))
            {
                var  anchorRaw = AssetUtils.GetAnchorFromBuffer(assetDocument.Buffer);
                bool stripped  = AssetUtils.IsStripped(assetDocument.Buffer);
                if (stripped) // we will handle it in prefabs
                {
                    return(null);
                }

                if (!anchorRaw.HasValue)
                {
                    return(null);
                }

                var anchor = anchorRaw.Value;

                var entries = assetDocument.Document.FindRootBlockMapEntries()?.Entries;
                if (entries == null)
                {
                    return(null);
                }

                var result = new LocalList <AssetScriptUsage>();
                foreach (var entry in entries)
                {
                    if (!entry.Key.MatchesPlainScalarText("m_Script"))
                    {
                        continue;
                    }

                    var deps = entry.Content.Value.ToHierarchyReference(currentAssetSourceFile);
                    if (deps is ExternalReference externalReference)
                    {
                        result.Add(new AssetScriptUsage(new LocalReference(currentAssetSourceFile.Ptr().Id, anchor), externalReference));
                    }
                }

                return(result);
            }

            return(null);
        }
        private static AnimatorScript?ExtractStateMachineBehaviour([NotNull] AssetDocument document,
                                                                   [NotNull] IPsiSourceFile file)
        {
            var anchorRaw = AssetUtils.GetAnchorFromBuffer(document.Buffer);

            if (!anchorRaw.HasValue)
            {
                return(null);
            }
            var guid = (document.Document.FindRootBlockMapEntries()?.Entries)?
                       .Cast <IBlockMappingEntry>()
                       .Where(entry => entry != null && entry.Key.MatchesPlainScalarText("m_Script") && entry.Content != null)
                       .Select(entry => entry.Content.Value.ToHierarchyReference(file) as ExternalReference?)
                       .FirstOrDefault()?
                       .ExternalAssetGuid;

            return(guid != null ? new AnimatorScript(guid.Value, anchorRaw.Value) : (AnimatorScript?)null);
        }
Exemplo n.º 6
0
        public object Build(SeldomInterruptChecker checker, IPsiSourceFile currentAssetSourceFile, AssetDocument assetDocument)
        {
            var anchorRaw = AssetUtils.GetAnchorFromBuffer(assetDocument.Buffer);

            if (!anchorRaw.HasValue)
            {
                return(null);
            }

            var anchor     = anchorRaw.Value;
            var isStripped = AssetUtils.IsStripped(assetDocument.Buffer);
            var location   = new LocalReference(currentAssetSourceFile.PsiStorage.PersistentIndex.NotNull("owningPsiPersistentIndex != null"), anchor);

            if (isStripped)
            {
                var prefabInstance            = AssetUtils.GetPrefabInstance(currentAssetSourceFile, assetDocument.Buffer) as LocalReference?;
                var correspondingSourceObject = AssetUtils.GetCorrespondingSourceObject(currentAssetSourceFile, assetDocument.Buffer) as ExternalReference?;

                if (prefabInstance != null && correspondingSourceObject != null)
                {
                    return(new StrippedHierarchyElement(location, prefabInstance.Value, correspondingSourceObject.Value));
                }

                return(null);
            }

            var gameObject = AssetUtils.GetGameObjectReference(currentAssetSourceFile, assetDocument.Buffer) as LocalReference?;

            if (gameObject != null && AssetUtils.IsMonoBehaviourDocument(assetDocument.Buffer))
            {
                var entries = assetDocument.Document.FindRootBlockMapEntries()?.Entries;
                if (entries == null)
                {
                    return(null);
                }

                IHierarchyReference documentReference = null;

                foreach (var entry in entries)
                {
                    if (entry.Key.MatchesPlainScalarText(UnityYamlConstants.ScriptProperty))
                    {
                        documentReference = entry.Content.Value.ToHierarchyReference(currentAssetSourceFile);
                        break;
                    }
                }

                if (documentReference is ExternalReference scriptReference)
                {
                    return(new ScriptComponentHierarchy(location, gameObject.Value, scriptReference));
                }
            }
            else if (gameObject != null && AssetUtils.IsTransform(assetDocument.Buffer))
            {
                var father = AssetUtils.GetTransformFather(currentAssetSourceFile, assetDocument.Buffer) as LocalReference?;
                if (father == null)
                {
                    return(null);
                }

                var rootIndex = AssetUtils.GetRootIndex(assetDocument.Buffer);
                return(new TransformHierarchy(location, gameObject.Value, father.Value, rootIndex));
            }
            else if (AssetUtils.IsGameObject(assetDocument.Buffer))
            {
                var name = AssetUtils.GetGameObjectName(assetDocument.Buffer);
                if (name != null)
                {
                    return(new GameObjectHierarchy(location, name));
                }
            }
            else if (AssetUtils.IsPrefabModification(assetDocument.Buffer))
            {
                var modification    = AssetUtils.GetPrefabModification(assetDocument.Document);
                var parentTransform = modification?.GetValue("m_TransformParent")?.ToHierarchyReference(currentAssetSourceFile) as LocalReference? ?? LocalReference.Null;
                var modifications   = modification?.GetValue("m_Modifications") as IBlockSequenceNode;
                var result          = new List <PrefabModification>();
                if (modifications != null)
                {
                    foreach (var entry in modifications.Entries)
                    {
                        var map = entry.Value as IBlockMappingNode;

                        var target = map?.GetValue("target").ToHierarchyReference(currentAssetSourceFile);
                        if (target == null)
                        {
                            continue;
                        }

                        var name = map.GetValue("propertyPath").GetPlainScalarText();
                        if (name == null)
                        {
                            continue;
                        }

                        var valueNode = map.FindMapEntryBySimpleKey("value")?.Content;
                        if (valueNode == null)
                        {
                            continue;
                        }

                        IAssetValue value = null;
                        foreach (var assetInspectorValueDeserializer in myAssetInspectorValueDeserializers)
                        {
                            if (assetInspectorValueDeserializer.TryGetInspectorValue(currentAssetSourceFile, valueNode, out value))
                            {
                                break;
                            }
                        }

                        var objectReference = map.FindMapEntryBySimpleKey("objectReference")?.Content.Value.ToHierarchyReference(currentAssetSourceFile);

                        var valueRange = valueNode.GetTreeTextRange();

                        result.Add(new PrefabModification(target, name, value,
                                                          new TextRange(assetDocument.StartOffset + valueRange.StartOffset.Offset,
                                                                        assetDocument.StartOffset + valueRange.EndOffset.Offset), objectReference));
                    }
                }

                var sourcePrefabGuid = AssetUtils.GetSourcePrefab(currentAssetSourceFile, assetDocument.Buffer) as ExternalReference?;
                if (sourcePrefabGuid == null)
                {
                    return(null);
                }

                return(new PrefabInstanceHierarchy(location, parentTransform, result, sourcePrefabGuid.Value.ExternalAssetGuid));
            }
            else if (gameObject != null)// regular component
            {
                var name = AssetUtils.GetRawComponentName(assetDocument.Buffer);
                if (name == null)
                {
                    return(null);
                }

                return(new ComponentHierarchy(location, gameObject.Value, name));
            }
            return(null);
        }
        public IUnityAssetDataElement Build(SeldomInterruptChecker checker, IPsiSourceFile currentSourceFile, AssetDocument assetDocument)
        {
            var anchorRaw = AssetUtils.GetAnchorFromBuffer(assetDocument.Buffer);

            if (!anchorRaw.HasValue)
            {
                return(null);
            }

            var anchor = anchorRaw.Value;

            var isStripped                = AssetUtils.IsStripped(assetDocument.Buffer);
            var gameObject                = AssetUtils.GetGameObject(assetDocument.Buffer)?.ToReference(currentSourceFile) as LocalReference;
            var prefabInstance            = AssetUtils.GetPrefabInstance(assetDocument.Buffer)?.ToReference(currentSourceFile) as LocalReference;
            var correspondingSourceObject = AssetUtils.GetCorrespondingSourceObject(assetDocument.Buffer)?.ToReference(currentSourceFile) as ExternalReference;
            var location = new LocalReference(currentSourceFile.PsiStorage.PersistentIndex, anchor);

            if (AssetUtils.IsMonoBehaviourDocument(assetDocument.Buffer))
            {
                var entries = assetDocument.Document.FindRootBlockMapEntries()?.Entries;
                if (entries == null)
                {
                    return(null);
                }

                AssetDocumentReference documentReference = null;

                foreach (var entry in entries)
                {
                    if (entry.Key.MatchesPlainScalarText(UnityYamlConstants.ScriptProperty))
                    {
                        documentReference = entry.Content.Value.AsFileID();
                        break;
                    }
                }

                var scriptAnchor = documentReference?.AnchorLong;
                var scriptGuid   = documentReference?.ExternalAssetGuid;
                if (isStripped || scriptAnchor != null && scriptGuid != null)
                {
                    return(new AssetDocumentHierarchyElement(
                               new ScriptComponentHierarchy(location,
                                                            isStripped ? null : new ExternalReference(scriptGuid, scriptAnchor.Value),
                                                            gameObject,
                                                            prefabInstance,
                                                            correspondingSourceObject,
                                                            isStripped
                                                            )));
                }
            }
            else if (AssetUtils.IsTransform(assetDocument.Buffer))
            {
                var father    = AssetUtils.GetTransformFather(assetDocument.Buffer)?.ToReference(currentSourceFile) as LocalReference;
                var rootIndex = AssetUtils.GetRootIndex(assetDocument.Buffer);
                return(new AssetDocumentHierarchyElement(
                           new TransformHierarchy(location, gameObject, father, rootIndex, prefabInstance, correspondingSourceObject, isStripped)));
            }
            else if (AssetUtils.IsGameObject(assetDocument.Buffer))
            {
                var name = AssetUtils.GetGameObjectName(assetDocument.Buffer);
                if (isStripped || name != null)
                {
                    return(new AssetDocumentHierarchyElement(new GameObjectHierarchy(location, name, prefabInstance, correspondingSourceObject, isStripped)));
                }
            }
            else if (AssetUtils.IsPrefabModification(assetDocument.Buffer))
            {
                var modification    = AssetUtils.GetPrefabModification(assetDocument.Document);
                var parentTransform = modification?.GetValue("m_TransformParent")?.AsFileID()?.ToReference(currentSourceFile) as LocalReference;
                var modifications   = modification?.GetValue("m_Modifications") as IBlockSequenceNode;
                var result          = new List <PrefabModification>();
                if (modifications != null)
                {
                    foreach (var entry in modifications.Entries)
                    {
                        var map = entry.Value as IBlockMappingNode;
                        if (map == null)
                        {
                            continue;
                        }

                        var target = map.GetValue("target").AsFileID()?.ToReference(currentSourceFile);
                        if (target == null)
                        {
                            continue;
                        }

                        var name = map.GetValue("propertyPath").GetPlainScalarText();
                        if (name == null)
                        {
                            continue;
                        }

                        var valueNode = map.FindMapEntryBySimpleKey("value")?.Content;
                        if (valueNode == null)
                        {
                            continue;
                        }

                        IAssetValue value = null;
                        foreach (var assetInspectorValueDeserializer in myAssetInspectorValueDeserializers)
                        {
                            if (assetInspectorValueDeserializer.TryGetInspectorValue(currentSourceFile, valueNode, out value))
                            {
                                break;
                            }
                        }
                        if (value == null)
                        {
                            continue;
                        }

                        result.Add(new PrefabModification(target, name, value));
                    }
                }

                var sourcePrefabGuid = AssetUtils.GetSourcePrefab(assetDocument.Buffer)?.ToReference(currentSourceFile) as ExternalReference;
                if (sourcePrefabGuid == null)
                {
                    return(null);
                }
                return(new AssetDocumentHierarchyElement(new PrefabInstanceHierarchy(location, sourcePrefabGuid.ExternalAssetGuid, parentTransform, result)));
            }
            else // regular component
            {
                var name = AssetUtils.GetRawComponentName(assetDocument.Buffer);
                if (name == null)
                {
                    return(null);
                }

                return(new AssetDocumentHierarchyElement(
                           new ComponentHierarchy(name, location, gameObject, prefabInstance, correspondingSourceObject, isStripped)));
            }
            return(null);
        }
        public IUnityAssetDataElement Build(SeldomInterruptChecker checker, IPsiSourceFile currentSourceFile, AssetDocument assetDocument)
        {
            var buffer = assetDocument.Buffer;

            if (ourMethodNameSearcher.Find(buffer) < 0)
            {
                return(null);
            }

            var anchorRaw = AssetUtils.GetAnchorFromBuffer(assetDocument.Buffer);

            if (!anchorRaw.HasValue)
            {
                return(null);
            }

            var anchor = anchorRaw.Value;

            var entries = assetDocument.Document.FindRootBlockMapEntries()?.Entries;

            if (entries == null)
            {
                return(null);
            }

            var result = new List <AssetMethodData>();

            foreach (var entry in entries)
            {
                if (ourMethodNameSearcher.Find(entry.Content.GetTextAsBuffer()) >= 0)
                {
                    var rootMap            = entry.Content.Value as IBlockMappingNode;
                    var persistentCallsMap = rootMap.GetValue("m_PersistentCalls") as IBlockMappingNode;
                    var mCalls             = persistentCallsMap.GetValue("m_Calls") as IBlockSequenceNode;
                    if (mCalls == null)
                    {
                        return(null);
                    }

                    var eventTypeName = rootMap.GetValue("m_TypeName").GetPlainScalarText();

                    foreach (var call in mCalls.Entries)
                    {
                        var methodDescription = call.Value as IBlockMappingNode;
                        if (methodDescription == null)
                        {
                            continue;
                        }

                        var fileID = methodDescription.FindMapEntryBySimpleKey("m_Target")?.Content.Value.AsFileID();
                        if (fileID == null || fileID.IsNullReference)
                        {
                            continue;
                        }

                        var methodNameNode = methodDescription.GetValue("m_MethodName");
                        var methodName     = methodNameNode?.GetPlainScalarText();
                        if (methodName == null)
                        {
                            continue;
                        }

                        var methodNameRange = methodNameNode.GetTreeTextRange();

                        var arguments = methodDescription.GetValue("m_Arguments") as IBlockMappingNode;
                        var modeText  = methodDescription.GetValue("m_Mode")?.GetPlainScalarText();
                        var argMode   = EventHandlerArgumentMode.EventDefined;
                        if (int.TryParse(modeText, out var mode))
                        {
                            if (1 <= mode && mode <= 6)
                            {
                                argMode = (EventHandlerArgumentMode)mode;
                            }
                        }

                        var argumentTypeName = arguments.GetValue("m_ObjectArgumentAssemblyTypeName")?.GetPlainScalarText();

                        var type = argumentTypeName?.Split(',').FirstOrDefault();
                        if (argMode == EventHandlerArgumentMode.EventDefined)
                        {
                            type = eventTypeName?.Split(',').FirstOrDefault();
                        }
                        else if (argMode == EventHandlerArgumentMode.Void)
                        {
                            type = null;
                        }

                        var range = new TextRange(assetDocument.StartOffset + methodNameRange.StartOffset.Offset,
                                                  assetDocument.StartOffset + methodNameRange.EndOffset.Offset);

                        var target = fileID.ToReference(currentSourceFile);
                        if (target != null)
                        {
                            result.Add(new AssetMethodData(
                                           new LocalReference(currentSourceFile.PsiStorage.PersistentIndex, anchor), methodName,
                                           range,
                                           argMode, type, target));
                        }
                    }
                }
            }

            if (result.Count > 0)
            {
                return(new AssetMethodsDataElement(result));
            }
            return(null);
        }