Esempio n. 1
0
        // Create and initialize the player object
        private MDPlayerInfo GetOrCreatePlayerObject(int PeerId)
        {
            if (Players.ContainsKey(PeerId))
            {
                return(Players[PeerId]);
            }

            Type PlayerType = GameInstance.GetPlayerInfoType();

            if (!MDStatics.IsSameOrSubclass(PlayerType, typeof(MDPlayerInfo)))
            {
                MDLog.Error(LOG_CAT, $"Provided player type [{PlayerType.Name}] is not a subclass of MDPlayerInfo");
                return(null);
            }

            MDPlayerInfo Player = Activator.CreateInstance(PlayerType) as MDPlayerInfo;

            Player.SetPeerId(PeerId);
            Player.PauseMode = PauseModeEnum.Process;
            AddChild(Player);
            Players.Add(PeerId, Player);

            OnPlayerInfoCreated(Player);

            return(Player);
        }
        /// <summary>
        /// Returns the attribute object for the specified type,
        /// climbing the hierarchy until Node is reached or the attribute is found
        /// </summary>
        /// <param name="InstanceType">The type to search</param>
        /// <typeparam name="T">The type to find</typeparam>
        /// <returns>The attribute object for the specified type or null if not found</returns>
        public static T FindClassAttributeInNode <T>(Type InstanceType) where T : Attribute
        {
            // Check the buffer
            string key = $"{InstanceType.Name}#{typeof(T).Name}";

            if (ClassAttributeCache.ContainsKey(key))
            {
                return((T)ClassAttributeCache[key]);
            }

            if (MDStatics.IsSameOrSubclass(InstanceType, typeof(Node)) == false)
            {
                return(null);
            }

            T FoundAtr = Attribute.GetCustomAttribute(InstanceType, typeof(T)) as T;

            if (FoundAtr != null && InstanceType != typeof(Node) && InstanceType.BaseType != null)
            {
                FoundAtr = FindClassAttributeInNode <T>(InstanceType.BaseType);
            }

            if (FoundAtr != null)
            {
                // Add to buffer if found
                ClassAttributeCache.Add(key, FoundAtr);
                return(FoundAtr);
            }

            ClassAttributeCache.Add(key, null);

            return(null);
        }
Esempio n. 3
0
        public static void PopulateBindNodes(Node Instance)
        {
            List <MemberInfo> Members = MDStatics.GetTypeMemberInfos(Instance);

            foreach (MemberInfo Member in Members)
            {
                MDBindNode BindAttr = Member.GetCustomAttribute(typeof(MDBindNode)) as MDBindNode;
                if (BindAttr == null)
                {
                    continue;
                }

                Type         MemberType = null;
                FieldInfo    Field      = Member as FieldInfo;
                PropertyInfo Property   = Member as PropertyInfo;
                if (Field != null)
                {
                    MemberType = Field.FieldType;
                }
                else if (Property != null)
                {
                    MemberType = Property.PropertyType;
                }

                if (!MDStatics.IsSameOrSubclass(MemberType, typeof(Node)))
                {
                    MDLog.Error(LOG_CAT,
                                $"Not Node-Type field [{Member.Name}] on Node {Instance.Name} with Type [{Instance.GetType().Name}] was marked with [MDBindNode()]");
                    continue;
                }

                string PathToNode = BindAttr.GetNodePath(Member.Name);
                Node   BoundNode  = FindNode(Instance, PathToNode);
                if (BoundNode == null)
                {
                    continue;
                }

                if (Field != null)
                {
                    Field.SetValue(Instance, BoundNode);
                }
                else if (Property != null)
                {
                    Property.SetValue(Instance, BoundNode);
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Spawn a network node
        /// </summary>
        /// <param name="NodeType">The type of node to spawn</param>
        /// <param name="Parent">The parent that the new instance will be a child of</param>
        /// <param name="NodeName">The name of the new node</param>
        /// <param name="UseRandomName">If set to true a random number will be added at the end of the node name</param>
        /// <param name="NetworkMaster">The peer that should own this, default is server</param>
        /// <param name="SpawnPos">Where the spawn this node</param>
        /// <returns>The new node</returns>
        public Node SpawnNetworkedNode(Type NodeType, Node Parent, string NodeName, bool UseRandomName = true,
                                       int NetworkMaster = -1, Vector3?SpawnPos = null)
        {
            if (this.IsMaster() == false)
            {
                MDLog.Error(LOG_CAT, "Only server can spawn networked nodes");
                return(null);
            }

            if (!MDStatics.IsSameOrSubclass(NodeType, typeof(Node)))
            {
                MDLog.Error(LOG_CAT, $"Provided type [{NodeType.Name}] is not a subclass of Node");
                return(null);
            }

            if (!Parent.IsInsideTree())
            {
                MDLog.Error(LOG_CAT, $"Parent [{Parent.Name}] is not inside the tree");
                return(null);
            }

            NodeName = BuildNodeName(NodeName, UseRandomName);

            int    NodeMaster     = NetworkMaster != -1 ? NetworkMaster : MDStatics.GetPeerId();
            string NodeTypeString = NodeType.AssemblyQualifiedName;
            string ParentPath     = Parent.GetPath();

            Vector3 SpawnPosVal = SpawnPos.GetValueOrDefault();

            if (MDStatics.IsNetworkActive())
            {
                Rpc(nameof(SpawnNodeType), NodeTypeString, ParentPath, NodeName, NodeMaster, SpawnPosVal);
            }

            return(SpawnNodeType(NodeTypeString, ParentPath, NodeName, NodeMaster, SpawnPosVal));
        }