示例#1
0
        ////////////////

        internal void CopyChangesFrom(CustomEntity ent)             // TODO: Copy changes only!
        {
            if (!ent.IsInitialized)
            {
                //throw new HamstarException( "!ModHelpers.CustomEntity.CopyChangesFrom(CustomEntity) - Parameter not initialized." );
                throw new HamstarException("Parameter not initialized.");
            }

            this.CopyChangesFrom(ent.Core, ent.Components, ent.OwnerPlayer);

            if (ModHelpersMod.Instance.Config.DebugModeCustomEntityInfo)
            {
                LogHelpers.Alert("Synced from " + ent.ToString() + " for " + this.ToString());
            }
        }
        public static CustomEntity AddToWorld(int who, CustomEntity ent, bool skipSync = false)
        {
            //if( ent == null ) { throw new HamstarException( "!ModHelpers.CustomEntityManager.AddToWorld - Null ent not allowed." ); }
            //if( !ent.IsInitialized ) { throw new HamstarException( "!ModHelpers.CustomEntityManager.AddToWorld - Initialized ents only." ); }
            if (ent == null)
            {
                throw new HamstarException("Null ent not allowed.");
            }
            if (!ent.IsInitialized)
            {
                throw new HamstarException("Initialized ents only.");
            }

            CustomEntityManager mngr    = ModHelpersMod.Instance.CustomEntMngr;
            CustomEntity        realEnt = ent;

            if (mngr.WorldEntitiesByIndexes.ContainsKey(who))
            {
                //throw new HamstarException( "!ModHelpers.CustomEntityManager.AddToWorld - "
                //	+ "Attempting to add "+ent.ToString()+" to slot "+who+" occupied by "+mngr.WorldEntitiesByIndexes[who].ToString() );
                throw new HamstarException("Attempting to add " + ent.ToString() + " to slot " + who + " occupied by "
                                           + mngr.WorldEntitiesByIndexes[who].ToString());
            }

            if (ent is SerializableCustomEntity)
            {
                realEnt = ((SerializableCustomEntity)ent).Convert();
            }

            Type compType;
            Type baseType = typeof(CustomEntityComponent);

            // Map entity to each of its components
            foreach (CustomEntityComponent component in realEnt.InternalComponents)
            {
                compType = component.GetType();
                lock (CustomEntityManager.MyLock) {
                    do
                    {
                        if (!mngr.WorldEntitiesByComponentType.ContainsKey(compType))
                        {
                            mngr.WorldEntitiesByComponentType[compType] = new HashSet <int>();
                        }

                        mngr.WorldEntitiesByComponentType[compType].Add(who);

                        compType = compType.BaseType;
                    } while(compType != baseType);
                }
            }

            realEnt.Core.whoAmI = who;
            mngr.WorldEntitiesByIndexes[who] = realEnt;

            realEnt.InternalOnAddToWorld();

            // Sync also
            if (!skipSync)
            {
                if (Main.netMode == 1)
                {
                    if (ent.SyncFromClient)
                    {
                        Promises.AddValidatedPromise(SaveableEntityComponent.LoadAllValidator, () => {
                            ent.SyncToAll();
                            return(false);
                        });
                    }
                }
                else if (Main.netMode == 2)
                {
                    if (ent.SyncFromServer)
                    {
                        Promises.AddValidatedPromise(SaveableEntityComponent.LoadAllValidator, () => {
                            ent.SyncToAll();
                            return(false);
                        });
                    }
                }
            }

            if (ModHelpersMod.Instance.Config.DebugModeCustomEntityInfo)
            {
                LogHelpers.Alert("Set " + realEnt.ToString());
            }

            return(realEnt);
        }