Beispiel #1
0
        public bool RegisterSmoothingAction <T>(PortableFunctionPointer <SmoothingActionDelegate> action) where T : struct, IComponentData
        {
            var type = ComponentType.ReadWrite <T>();

            if (type.IsBuffer)
            {
                UnityEngine.Debug.LogError("Smoothing actions are not supported for buffers");
                return(false);
            }
            if (m_SmoothingActions.ContainsKey(type))
            {
                UnityEngine.Debug.LogError($"There is already a action registered for the type {type.ToString()}");
                return(false);
            }

            var actionData = new SmoothingActionState
            {
                action           = action,
                compIndex        = -1,
                compSize         = -1,
                serializerIndex  = -1,
                entityIndex      = -1,
                compBackupOffset = -1,
                userTypeId       = -1,
                userTypeSize     = -1
            };

            m_SmoothingActions.Add(type, actionData);
            if (m_HasSmoothingAction == Entity.Null)
            {
                m_HasSmoothingAction = EntityManager.CreateEntity(ComponentType.ReadOnly <SmoothingAction>());
            }
            return(true);
        }
        public void RegisterRpc(ComponentType type, PortableFunctionPointer <RpcExecutor.ExecuteDelegate> exec)
        {
            if (!m_CanRegister)
            {
                throw new InvalidOperationException("Cannot register new RPCs after the RpcSystem has started running");
            }

            if (!exec.Ptr.IsCreated)
            {
                throw new InvalidOperationException($"Cannot register RPC for type {type.GetManagedType()}: Ptr property is not created (null)" +
                                                    "Check CompileExecute() and verify you are initializing the PortableFunctionPointer with a valid static function delegate, decorated with [BurstCompile] attribute");
            }

            var hash = TypeManager.GetTypeInfo(type.TypeIndex).StableTypeHash;

            if (hash == 0)
            {
                throw new InvalidOperationException(String.Format("Unexpected 0 hash for type {0}", type.GetManagedType()));
            }
            if (m_RpcTypeHashToIndex.TryGetValue(hash, out var index))
            {
                var rpcData = m_RpcData[index];
                if (rpcData.TypeHash != 0)
                {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                    if (rpcData.RpcType == type)
                    {
                        throw new InvalidOperationException(
                                  String.Format("Registering RPC {0} multiple times is not allowed", type.GetManagedType()));
                    }
                    throw new InvalidOperationException(
                              String.Format("Type hash collision between types {0} and {1}", type.GetManagedType(), rpcData.RpcType.GetManagedType()));
#else
                    throw new InvalidOperationException(
                              String.Format("Hash collision or multiple registrations for {0}", type.GetManagedType()));
#endif
                }

                rpcData.TypeHash = hash;
                rpcData.Execute  = exec;
                m_RpcData[index] = rpcData;
            }
            else
            {
                m_RpcTypeHashToIndex.Add(hash, m_RpcData.Length);
                m_RpcData.Add(new RpcData
                {
                    TypeHash = hash,
                    Execute  = exec,
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                    RpcType = type
#endif
                });
            }
        }
Beispiel #3
0
        public bool RegisterSmoothingAction <T, U>(PortableFunctionPointer <SmoothingActionDelegate> action)
            where T : struct, IComponentData
            where U : struct, IComponentData
        {
            if (!RegisterSmoothingAction <T>(action))
            {
                return(false);
            }

            var type       = ComponentType.ReadWrite <T>();
            var userType   = ComponentType.ReadWrite <U>();
            var userTypeId = -1;

            for (int i = 0; i < m_UserSpecifiedComponentData.Length; ++i)
            {
                if (userType == m_UserSpecifiedComponentData[i])
                {
                    userTypeId = i;
                    break;
                }
            }
            if (userTypeId == -1)
            {
                if (m_UserSpecifiedComponentData.Length == 8)
                {
                    UnityEngine.Debug.LogError("There can only be 8 components registered as user data.");

                    m_SmoothingActions.Remove(type);
                    if (m_SmoothingActions.IsEmpty)
                    {
                        EntityManager.DestroyEntity(m_HasSmoothingAction);
                    }

                    return(false);
                }
                m_UserSpecifiedComponentData.Add(userType);
                userTypeId = m_UserSpecifiedComponentData.Length - 1;
            }
            var actionState = m_SmoothingActions[type];

            actionState.userTypeId   = userTypeId;
            actionState.userTypeSize = UnsafeUtility.SizeOf <U>();

            m_SmoothingActions[type] = actionState;
            return(true);
        }