public VisitStatus Visit <TProperty, TContainer>(IPropertyVisitor visitor, TProperty property, ref TContainer container, ref TypeReference value, ref ChangeTracker changeTracker) where TProperty : IProperty <TContainer, TypeReference>
        {
            if (!property.Attributes.HasAttribute <TypeSearcherAttribute>())
            {
                return(VisitStatus.Unhandled);
            }

            var key = new Tuple <INodeModel, string>(VisitorModel, ((IProperty)property).GetName());

            if (!SafeTypeCache.TryGetValue(key, out var previousType))
            {
                int typeIndex = TypeManager.GetTypeIndexFromStableTypeHash(value.TypeHash);
                if (typeIndex != -1 && typeIndex != 0)
                {
                    previousType       = TypeManager.GetType(typeIndex).GenerateTypeHandle(Stencil);
                    SafeTypeCache[key] = previousType;
                }
                else
                {
                    previousType = default;
                }
            }

            if (ExposeProperty(property, ref changeTracker, ref previousType))
            {
                SafeTypeCache[key] = previousType;
                value.TypeHash     = TypeHash.CalculateStableTypeHash(previousType.Resolve(Stencil));
            }

            return(VisitStatus.Handled);
        }
        static bool TranslateNode(GraphBuilder builder, INodeModel nodeModel, out INode node,
                                  out Dictionary <string, uint> portToOffsetMapping, out uint?preAllocatedDataIndex)
        {
            Assert.IsNotNull(nodeModel);
            preAllocatedDataIndex = null;

            switch (nodeModel)
            {
            case SetVariableNodeModel setVariableNodeModel:
            {
                node = setVariableNodeModel.Node;
                portToOffsetMapping = setVariableNodeModel.PortToOffsetMapping;
                if (setVariableNodeModel.DeclarationModel == null)
                {
                    return(false);
                }
                preAllocatedDataIndex = builder.GetVariableDataIndex(setVariableNodeModel.DeclarationModel).DataIndex;
                return(true);
            }

            case IEventNodeModel eventNodeModel:
                node = eventNodeModel.Node;
                ((IEventNode)node).EventId = TypeHash.CalculateStableTypeHash(
                    eventNodeModel.TypeHandle.Resolve(nodeModel.GraphModel.Stencil));
                portToOffsetMapping = eventNodeModel.PortToOffsetMapping;
                return(true);

            case SubgraphReferenceNodeModel subgraphReferenceNodeModel:
                node = subgraphReferenceNodeModel.Node;
                portToOffsetMapping = subgraphReferenceNodeModel.PortToOffsetMapping;
                return(true);

            case IDotsNodeModel dotsNodeModel:
                node = dotsNodeModel.Node;
                portToOffsetMapping = dotsNodeModel.PortToOffsetMapping;
                if (nodeModel is IReferenceComponentTypes referenceComponentTypes)
                {
                    foreach (var typeReference in referenceComponentTypes.ReferencedTypes)
                    {
                        if (typeReference.TypeIndex != -1)
                        {
                            builder.AddReferencedComponent(typeReference);
                        }
                    }
                }
                return(true);

            case IConstantNodeModel constantNodeModel:
                HandleConstants(builder, out node, out portToOffsetMapping, constantNodeModel);
                return(true);

            case IVariableModel variableModel:
                return(HandleVariable(builder, out node, out portToOffsetMapping,
                                      out preAllocatedDataIndex, variableModel));

            default:
                throw new NotImplementedException(
                          $"Don't know how to translate a node of type {nodeModel.GetType()}: {nodeModel}");
            }
        }
示例#3
0
 public void Write <T>(T evt, int index) where T : struct, IVisualScriptingEvent
 {
     m_Writer.BeginForEachIndex(index);
     m_Writer.Write(TypeHash.CalculateStableTypeHash(typeof(T)));
     m_Writer.Write(UnsafeUtility.SizeOf <T>());
     m_Writer.Write(false);
     m_Writer.Write(evt);
     m_Writer.EndForEachIndex();
 }
示例#4
0
 protected override void ShowSearcher(Vector2 mousePosition, INodeModel nodeModel,
                                      Action <TypeReference> notifyChanged)
 {
     SearcherService.ShowTypes(nodeModel.VSGraphModel.Stencil, mousePosition, (handle, i) =>
     {
         var typeReference      = Target;
         typeReference.TypeHash = TypeHash.CalculateStableTypeHash(handle.Resolve(nodeModel.VSGraphModel.Stencil));
         notifyChanged(typeReference);
     }, MakeSearcherAdapterFromAttribute(null, nodeModel));
 }
        static List <TypeObjectData> BuildObjectData()
        {
            var types = TypeCache.GetTypesWithAttribute <GenerateAuthoringComponentAttribute>();

            return(types
                   .Select(t => new TypeObjectData {
                Value = new TypeReference {
                    TypeHash = TypeHash.CalculateStableTypeHash(t)
                }, SearcherTitle = t.Name
            })
                   .ToList());
        }
 static List <TypeObjectData> BuildObjectData()
 {
     return(TypeCache.GetTypesDerivedFrom <IComponentData>()
            .Where(TypeHasUsableComponentFields)
            .Select(t =>
     {
         bool isAuthoring = (t.CustomAttributes.Any(a => a.AttributeType == typeof(GenerateAuthoringComponentAttribute)));
         return new TypeObjectData {
             Value = new TypeReference {
                 TypeHash = TypeHash.CalculateStableTypeHash(t)
             }, SearcherTitle = t.Name, Path = isAuthoring ? null : "Advanced"
         };
     })
            .ToList());
 }
示例#7
0
        void ProcessProperty <TValue>(ref TValue value)
        {
            if (m_CurrentPropertyPath.ToString() != m_SearchPropertyPath)
            {
                return;
            }

            if (TryGetOffsetOfField(m_TargetComponentType, m_CurrentPropertyPath, m_PropertyNameStartIndex, out var offset))
            {
                m_Success        = true;
                m_FieldOffset    = (ushort)offset;
                m_FieldSize      = (ushort)UnsafeUtility.SizeOf(value.GetType());
                m_StableTypeHash = TypeHash.CalculateStableTypeHash(m_TargetComponentType);
            }

            m_OperationComplete = true;
        }
示例#8
0
        public void TestSetComponent()
        {
            GraphBuilder.VariableHandle varIndex = default;
            int initialValue = 42;
            int newValue     = 55;

            SetupTestGraphDefinitionMultipleFrames((b, _) =>
            {
                var componentTypeRef = new TypeReference {
                    TypeHash = TypeHash.CalculateStableTypeHash(typeof(TestComponent))
                };
                var onUpdate = b.AddNode(new OnUpdate());
                var enabled  = b.AddNode(new ConstantBool {
                    Value = true
                });
                var setComponent = b.AddNode(new SetComponent {
                    Type = componentTypeRef, ComponentData = new InputDataMultiPort {
                        DataCount = 2
                    }
                });

                b.CreateEdge(enabled.ValuePort, onUpdate.Enabled);
                varIndex = b.BindVariableToDataIndex("a");
                b.BindVariableToInput(varIndex, setComponent.ComponentData.SelectPort(0));
                b.CreateEdge(onUpdate.Output, setComponent.Set);
                b.AddReferencedComponent(componentTypeRef);
            }
                                                   , (manager, entity, index) =>
            {
                // don't add the component yet, next frame we can test that "set component" doesn't do anything if no component is attached
            }, (manager, entity, index) =>
            {
                LogAssert.NoUnexpectedReceived();
                Assert.That(manager.HasComponent <TestComponent>(entity), Is.False);    // Set component shouldn't add a component
                manager.AddComponentData(entity, new TestComponent {
                    Int = initialValue, Float = 12.7f
                });
                GraphInstance instance = GetGraphInstance(entity);
                instance.WriteValueToDataSlot(varIndex.DataIndex, newValue);
            }, (manager, entity, index) =>
            {
                var componentData = manager.GetComponentData <TestComponent>(entity);
                Assert.That(componentData.Int, Is.EqualTo(newValue));
            });
        }
示例#9
0
        public void TestGetComponent()
        {
            SetupTestGraphDefinitionMultipleFrames((b, _) =>
            {
                var componentTypeRef = new TypeReference {
                    TypeHash = TypeHash.CalculateStableTypeHash(typeof(TestComponent))
                };
                var onUpdate = b.AddNode(new OnUpdate());
                var enabled  = b.AddNode(new ConstantBool {
                    Value = true
                });
                var getComponent = b.AddNode(new GetComponent {
                    Type = componentTypeRef, ComponentData = new OutputDataMultiPort {
                        DataCount = 2
                    }
                });
                var log = b.AddNode(new Log()
                {
                    Messages = new InputDataMultiPort {
                        DataCount = 1
                    }
                });

                b.CreateEdge(enabled.ValuePort, onUpdate.Enabled);
                b.CreateEdge(onUpdate.Output, log.Input);
                b.CreateEdge(getComponent.ComponentData.SelectPort(0), log.Messages.SelectPort(0));
                b.AddReferencedComponent(componentTypeRef);
            }, (manager, entity, index) =>
            {
                // don't add the component yet, next frame we can test that "get component" returns the default value
            }, (manager, entity, index) =>
            {
                LogAssert.Expect(LogType.Log, "0");     // no component on entity should give default value
                manager.AddComponentData(entity, new TestComponent {
                    Int = 42, Float = 12.7f
                });
            }, (manager, entity, index) =>
            {
                LogAssert.Expect(LogType.Log, "42");
            });
        }
示例#10
0
        public static void RegisterMonoScripts()
        {
            if (AssetDatabaseCompatibility.IsAssetImportWorkerProcess() || s_Initialized)
            {
                return;
            }
            s_Initialized = true;

            AssetDatabaseCompatibility.UnregisterCustomDependencyPrefixFilter("UnityEngineType/");

            var behaviours = TypeCache.GetTypesDerivedFrom <UnityEngine.MonoBehaviour>();
            var scripts    = TypeCache.GetTypesDerivedFrom <UnityEngine.ScriptableObject>();

            for (int i = 0; i != behaviours.Count; i++)
            {
                var type = behaviours[i];
                if (type.IsGenericType)
                {
                    continue;
                }
                var hash = TypeHash.CalculateStableTypeHash(type);
                AssetDatabaseCompatibility.RegisterCustomDependency(TypeString(type),
                                                                    new UnityEngine.Hash128(hash, hash));
            }

            for (int i = 0; i != scripts.Count; i++)
            {
                var type = scripts[i];
                if (type.IsGenericType)
                {
                    continue;
                }
                var hash = TypeHash.CalculateStableTypeHash(type);
                AssetDatabaseCompatibility.RegisterCustomDependency(TypeString(type),
                                                                    new UnityEngine.Hash128(hash, hash));
            }
        }