Esempio n. 1
0
        public T GetComponent <T>() where T : MySessionComponentBase
        {
            MySessionComponentBase comp;

            m_sessionComponents.TryGetValue(typeof(T), out comp);
            return(comp as T);
        }
        private void RegisterDrone(long antennaEntityId, MyEntity droneMainEntity, bool immediate = true)
        {
            var newInfo = DroneInfo.Allocate(antennaEntityId, MySandboxGame.TotalGamePlayTimeInMilliseconds + DRONE_DESPAWN_TIMER);

            m_droneInfos.Add(droneMainEntity.EntityId, newInfo, immediate: immediate);
            droneMainEntity.OnClosing += DroneMainEntityOnClosing;

            PirateAntennaInfo antennaInfo = null;

            if (!m_pirateAntennas.TryGetValue(antennaEntityId, out antennaInfo))
            {
                MyEntity entity;
                if (MyEntities.TryGetEntityById(antennaEntityId, out entity))
                {
                    var antenna = entity as MyRadioAntenna;
                    if (antenna != null)
                    {
                        antenna.UpdatePirateAntenna();
                        m_pirateAntennas.TryGetValue(antennaEntityId, out antennaInfo);
                    }
                }
            }
            if (antennaInfo != null)
            {
                antennaInfo.SpawnedDrones++;
            }
            Debug.Assert(antennaEntityId == 0 || antennaInfo != null, "Antenna info not present when registering a drone!");

            var remote = droneMainEntity as MyRemoteControl;

            if (remote != null)
            {
                remote.OwnershipChanged += DroneRemoteOwnershipChanged;
            }
        }
Esempio n. 3
0
        private void RemoveEntity(IMyEntity x)
        {
            TrackedEntity tracker;

            if (!m_trackedEntities.TryGetValue(x, out tracker))
            {
                return;
            }
            Log(MyLogSeverity.Debug, "Removing tracking for entity {0} ({1})", x, x.GetFriendlyName());
            m_dirtyVolumes.Enqueue(tracker.CurrentView);
            m_trackedEntities.Remove(x);
            x.OnMarkForClose -= RemoveEntity;
        }
        private void UnregisterDrone(MyEntity entity, bool immediate = true)
        {
            long antennaEntityId = 0;

            DroneInfo info = null;

            Debug.Assert(m_droneInfos.ContainsKey(entity.EntityId), "Unregistering drone with inconsistend entity id");
            m_droneInfos.TryGetValue(entity.EntityId, out info);
            if (info != null)
            {
                antennaEntityId = info.AntennaEntityId;
                DroneInfo.Deallocate(info);
            }
            m_droneInfos.Remove(entity.EntityId, immediate: immediate);

            PirateAntennaInfo antennaInfo = null;

            m_pirateAntennas.TryGetValue(antennaEntityId, out antennaInfo);
            if (antennaInfo != null)
            {
                antennaInfo.SpawnedDrones--;
                Debug.Assert(antennaInfo.SpawnedDrones >= 0, "Inconsistence in registered drone counts!");
            }

            entity.OnClosing -= DroneMainEntityOnClosing;
            var remote = entity as MyRemoteControl;

            if (remote != null)
            {
                remote.OwnershipChanged -= DroneRemoteOwnershipChanged;
            }
        }
Esempio n. 5
0
        public bool TryGetExtension <T>(out T extension)
            where T : class, IMyToolbarExtension
        {
            extension = null;

            if (m_extensions == null)
            {
                return(false);
            }
            IMyToolbarExtension retval = null;

            if (m_extensions.TryGetValue(typeof(T), out retval))
            {
                extension = retval as T;
            }
            return(extension != null);
        }
Esempio n. 6
0
        internal void SyncClientEwarBlocks()
        {
            foreach (var ewarPair in CurrentClientEwaredCubes)
            {
                BlockState state;
                MyEntity   ent;
                var        entId = ewarPair.Key;
                if (MyEntities.TryGetEntityById(entId, out ent))
                {
                    var cube = (MyCubeBlock)ent;
                    var func = (IMyFunctionalBlock)cube;
                    func.RefreshCustomInfo();

                    if (!_activeEwarCubes.ContainsKey(entId))
                    {
                        state = new BlockState {
                            FunctBlock = func, FirstState = func.Enabled, Endtick = Tick + ewarPair.Value.EndTick, Session = this
                        };
                        _activeEwarCubes[entId] = state;
                        ActivateClientEwarState(ref state);
                    }
                }
                else if (_activeEwarCubes.TryGetValue(entId, out state))
                {
                    DeactivateClientEwarState(ref state);
                    _activeEwarCubes.Remove(entId);
                }

                ClientEwarStale = false;
            }

            _activeEwarCubes.ApplyChanges();
            foreach (var activeEwar in _activeEwarCubes)
            {
                if (!CurrentClientEwaredCubes.ContainsKey(activeEwar.Key))
                {
                    var state = activeEwar.Value;
                    DeactivateClientEwarState(ref state);
                    _activeEwarCubes.Remove(activeEwar.Key);
                }
            }
            _activeEwarCubes.ApplyRemovals();
        }
        public int GetRespawnCooldownSeconds(MyPlayer.PlayerId controllerId, string respawnShipId)
        {
            var respawnShip = MyDefinitionManager.Static.GetRespawnShipDefinition(respawnShipId);

            System.Diagnostics.Debug.Assert(respawnShip != null);
            if (respawnShip == null)
            {
                return(0);
            }

            var key = new RespawnKey()
            {
                ControllerId = controllerId, RespawnShipId = respawnShipId
            };
            int currentTime = MySandboxGame.TotalGamePlayTimeInMilliseconds;
            int time        = currentTime;

            m_globalRespawnTimesMs.TryGetValue(key, out time);
            return(Math.Max((time - currentTime) / 1000, 0));
        }
Esempio n. 8
0
        public static MySessionComponentBase FindModSessionComponent(string modName, string modScriptsFolder, string typeName)
        {
            string assemblyName = $"{modName}_{modScriptsFolder}, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null";

            CachingDictionary <Type, MySessionComponentBase> sessionComponents = (CachingDictionary <Type, MySessionComponentBase>)
                                                                                 typeof(MySession).GetField("m_sessionComponents", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(MySession.Static);

            if (sessionComponents == null)
            {
                Logger.AlwaysLog("Failed to get m_sessionComponents", Logger.severity.ERROR);
                return(null);
            }

            // Type.GetType(string typeName) with the fully qualified name doesn't seem to work, maybe it's something to do with CodeDOM
            // there can be more than one assembly with the right name
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies().Where(x => x.FullName == assemblyName))
            {
                Type componentType = assembly.GetType(typeName);
                if (componentType == null)
                {
                    Logger.DebugLog($"Failed to get type from assembly. Assembly: {assemblyName}, Type: {typeName}", Logger.severity.TRACE);
                    continue;
                }

                MySessionComponentBase component;
                if (!sessionComponents.TryGetValue(componentType, out component))
                {
                    Logger.DebugLog($"Failed to get MySessionComponentBase. Assembly: {assemblyName}, Type: {typeName}", Logger.severity.TRACE);
                    continue;
                }

                return(component);
            }

            return(null);
        }
Esempio n. 9
0
        private void LoadSaveData(Builder_ArmsData data)
        {
            if (data == null)
            {
                Logger.DebugLog("No data to load");
                return;
            }

#pragma warning disable 612, 618
            if (Comparer <Version> .Default.Compare(data.ArmsVersion, default(Version)) == 0)
            {
                Logger.DebugLog("Old version: " + data.ModVersion);
                data.ArmsVersion = new Version(data.ModVersion);
            }
#pragma warning restore 612, 618

            Logger.AlwaysLog("Save version: " + data.ArmsVersion, Rynchodon.Logger.severity.INFO);

            // relay

            Dictionary <Message.Builder_Message, Message> messages = MyAPIGateway.Multiplayer.IsServer ? new Dictionary <Message.Builder_Message, Message>() : null;
            SerializableGameTime.Adjust = new TimeSpan(data.SaveTime);
            foreach (RelayStorage.Builder_NetworkStorage bns in data.AntennaStorage)
            {
                RelayNode node;
                if (!Registrar.TryGetValue(bns.PrimaryNode, out node))
                {
                    Logger.AlwaysLog("Failed to get node for: " + bns.PrimaryNode, Rynchodon.Logger.severity.WARNING);
                    continue;
                }
                RelayStorage store = node.Storage;
                if (store == null)                 // probably always true
                {
                    node.ForceCreateStorage();
                    store = node.Storage;
                    if (store == null)
                    {
                        Logger.AlwaysLog("failed to create storage for " + node.DebugName, Rynchodon.Logger.severity.ERROR);
                        continue;
                    }
                }

                foreach (LastSeen.Builder_LastSeen bls in bns.LastSeenList)
                {
                    LastSeen ls = new LastSeen(bls);
                    if (ls.IsValid)
                    {
                        store.Receive(ls);
                    }
                    else
                    {
                        Logger.AlwaysLog("failed to create a valid last seen from builder for " + bls.EntityId, Rynchodon.Logger.severity.WARNING);
                        if (m_failedLastSeen == null)
                        {
                            m_failedLastSeen = new CachingDictionary <long, CachingList <LastSeen.Builder_LastSeen> >();
                            UpdateManager.Register(100, RetryLastSeen);
                        }
                        CachingList <LastSeen.Builder_LastSeen> list;
                        if (!m_failedLastSeen.TryGetValue(bns.PrimaryNode, out list))
                        {
                            list = new CachingList <LastSeen.Builder_LastSeen>();
                            m_failedLastSeen.Add(bns.PrimaryNode, list, true);
                        }
                        list.Add(bls);
                        list.ApplyAdditions();
                    }
                }

                Logger.DebugLog("added " + bns.LastSeenList.Length + " last seen to " + store.PrimaryNode.DebugName, Rynchodon.Logger.severity.DEBUG);

                // messages in the save file belong on the server
                if (messages == null)
                {
                    continue;
                }

                foreach (Message.Builder_Message bm in bns.MessageList)
                {
                    Message msg;
                    if (!messages.TryGetValue(bm, out msg))
                    {
                        msg = new Message(bm);
                        messages.Add(bm, msg);
                    }
                    else
                    {
                        Logger.DebugLog("found linked message", Rynchodon.Logger.severity.TRACE);
                    }
                    if (msg.IsValid)
                    {
                        store.Receive(msg);
                    }
                    else
                    {
                        Logger.AlwaysLog("failed to create a valid message from builder for " + bm.DestCubeBlock + "/" + bm.SourceCubeBlock, Rynchodon.Logger.severity.WARNING);
                    }
                }

                Logger.DebugLog("added " + bns.MessageList.Length + " message to " + store.PrimaryNode.DebugName, Rynchodon.Logger.severity.DEBUG);
            }

            // past this point, only synchronized data
            if (!MyAPIGateway.Multiplayer.IsServer)
            {
                data = null;
                return;
            }

            // system disruption

            foreach (Disruption.Builder_Disruption bd in data.SystemDisruption)
            {
                Disruption disrupt;
                switch (bd.Type)
                {
                case "AirVentDepressurize":
                    disrupt = new AirVentDepressurize();
                    break;

                case "CryoChamberMurder":
                    disrupt = new CryoChamberMurder();
                    break;

                case "DisableTurret":
                    disrupt = new DisableTurret();
                    break;

                case "DoorLock":
                    disrupt = new DoorLock();
                    break;

                case "EMP":
                    disrupt = new EMP();
                    break;

                case "GravityReverse":
                    disrupt = new GravityReverse();
                    break;

                case "JumpDriveDrain":
                    disrupt = new JumpDriveDrain();
                    break;

                case "MedicalRoom":
                    disrupt = new MedicalRoom();
                    break;

                case "TraitorTurret":
                    disrupt = new TraitorTurret();
                    break;

                default:
                    Logger.AlwaysLog("Unknown disruption: " + bd.Type, Rynchodon.Logger.severity.WARNING);
                    continue;
                }
                disrupt.Start(bd);
            }

            // autopilot

            if (data.Autopilot != null)
            {
                foreach (ShipAutopilot.Builder_Autopilot ba in data.Autopilot)
                {
                    ShipAutopilot autopilot;
                    if (Registrar.TryGetValue(ba.AutopilotBlock, out autopilot))
                    {
                        autopilot.ResumeFromSave(ba);
                    }
                    else
                    {
                        Logger.AlwaysLog("failed to find autopilot block " + ba.AutopilotBlock, Rynchodon.Logger.severity.WARNING);
                    }
                }
            }

            // programmable block

            if (data.ProgrammableBlock != null)
            {
                foreach (ProgrammableBlock.Builder_ProgrammableBlock bpa in data.ProgrammableBlock)
                {
                    ProgrammableBlock pb;
                    if (Registrar.TryGetValue(bpa.BlockId, out pb))
                    {
                        pb.ResumeFromSave(bpa);
                    }
                    else
                    {
                        Logger.AlwaysLog("failed to find programmable block " + bpa.BlockId, Rynchodon.Logger.severity.WARNING);
                    }
                }
            }

            // text panel

            if (data.TextPanel != null)
            {
                foreach (TextPanel.Builder_TextPanel btp in data.TextPanel)
                {
                    TextPanel panel;
                    if (Registrar.TryGetValue(btp.BlockId, out panel))
                    {
                        panel.ResumeFromSave(btp);
                    }
                    else
                    {
                        Logger.AlwaysLog("failed to find text panel " + btp.BlockId, Rynchodon.Logger.severity.WARNING);
                    }
                }
            }

            // weapon

            if (data.Weapon != null)
            {
                foreach (WeaponTargeting.Builder_WeaponTargeting bwt in data.Weapon)
                {
                    WeaponTargeting targeting;
                    if (WeaponTargeting.TryGetWeaponTargeting(bwt.WeaponId, out targeting))
                    {
                        targeting.ResumeFromSave(bwt);
                    }
                    else
                    {
                        Logger.AlwaysLog("failed to find weapon " + bwt.WeaponId, Rynchodon.Logger.severity.WARNING);
                    }
                }
            }

            // entity values

            if (data.EntityValues != null)
            {
                UpgradeEntityValue.Load(data.EntityValues);
            }

            // sync

            if (data.Sync != null)
            {
                ASync.SetBuilder(data.Sync);
            }

            data = null;
        }