Exemplo n.º 1
0
		/// <summary>
		/// does not check seen for isValid
		/// </summary>
		/// <param name="mes">message to receive</param>
		public void Receive(Message mes)
		{
			using (lock_m_messages.AcquireExclusiveUsing())
			{
				if (m_messages.Contains(mes))
					return;
				m_messages.Add(mes);
			}
			m_logger.debugLog("got a new message: " + mes.Content + ", count is now " + m_messages.Count, "receive()", Logger.severity.TRACE);
		}
Exemplo n.º 2
0
        private void HandleMessage(Message received)
        {
            string param = received.SourceGridName + messageSeparator + received.SourceBlockName + messageSeparator + received.Content;

            if (m_progBlock.TryRun(param))
            {
                m_logger.debugLog("Sent message to program", Logger.severity.DEBUG);
                if (MyAPIGateway.Session.Player != null)
                    (m_block as IMyTerminalBlock).AppendCustomInfo("Received message");
            }
            else
            {
                m_logger.debugLog("Failed to send message to program", Logger.severity.WARNING);
                if (MyAPIGateway.Session.Player != null)
                    (m_block as IMyTerminalBlock).AppendCustomInfo("Received message but failed to run program.");
            }
        }
Exemplo n.º 3
0
        private void LoadData()
        {
            if (m_data == null)
            {
                m_logger.debugLog("No data to load");
                return;
            }

            m_logger.alwaysLog("Save version: " + m_data.ModVersion, Logger.severity.INFO);

            // network

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

                foreach (LastSeen.Builder_LastSeen bls in bns.LastSeenList)
                {
                    LastSeen ls = new LastSeen(bls);
                    if (ls.IsValid)
                        store.Receive(ls);
                    else
                        m_logger.debugLog("failed to create a valid last seen from builder for " + bls.EntityId, Logger.severity.WARNING);
                }

                m_logger.debugLog("added " + bns.LastSeenList.Length + " last seen to " + store.PrimaryNode.LoggingName, Logger.severity.DEBUG);

                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
                    {
                        m_logger.debugLog("found linked message", Logger.severity.TRACE);
                    }
                    if (msg.IsValid)
                        store.Receive(msg);
                    else
                        m_logger.debugLog("failed to create a valid message from builder for " + bm.DestCubeBlock, Logger.severity.WARNING);
                }

                m_logger.debugLog("added " + bns.MessageList.Length + " message to " + store.PrimaryNode.LoggingName, Logger.severity.DEBUG);
            }

            // system disruption

            foreach (Disruption.Builder_Disruption bd in m_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:
                        m_logger.alwaysLog("Unknown disruption: " + bd.Type, Logger.severity.WARNING);
                        continue;
                }
                disrupt.Start(bd);
            }

            // autopilot

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

            // programmable block

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

            // text panel

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

            // weapon

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

            // entity values

            if (m_data.EntityValues != null)
                EntityValue.ResumeFromSave(m_data.EntityValues);

            m_data = null;
        }
Exemplo n.º 4
0
		/// <summary>
		/// writes the message from mod to display name
		/// </summary>
		public static void writeToName(IMyTerminalBlock recipient, Message message)
		{
			recipient.SetCustomName(recipient.CustomName + startOfReceive + message.SourceGridName + separator + message.SourceBlockName + separator + message.Content + endOfReceive);
			return;
		}
Exemplo n.º 5
0
        /// <summary>
        /// <para>Internal receive method. Adds the Message to this storage.</para>
        /// <para>lock_m_messages should be exclusively locked before invoking this method.</para>
        /// </summary>
        private void in_Receive(Message msg)
        {
            Action<Message> handler;
            bool gotHandler;
            m_logger.debugLog("looking for a handler for " + msg.DestCubeBlock.EntityId);
            using (lock_messageHandlers.AcquireSharedUsing())
                gotHandler = m_messageHandlers.TryGetValue(msg.DestCubeBlock.EntityId, out handler);

            if (gotHandler)
            {
                m_logger.debugLog("have a handler for msg");
                msg.IsValid = false;
                handler(msg);
                return;
            }

            if (m_messages.Add(msg))
                m_logger.debugLog("got a new message: " + msg.Content + ", count is now " + m_messages.Count, Logger.severity.DEBUG);
            else
                m_logger.debugLog("already have message: " + msg.Content + ", count is now " + m_messages.Count, Logger.severity.DEBUG);
        }
Exemplo n.º 6
0
        /// <summary>
        /// <para>Add a Message to this storage. Message will be pushed to connected storages.</para>
        /// <para>Not optimized for use within a loop.</para>
        /// </summary>
        /// <param name="msg">Message to receive.</param>
        public void Receive(Message msg)
        {
            HashSet<RelayStorage> sendToSet = ResourcePool<HashSet<RelayStorage>>.Pool.Get();
            try
            {
                AddStorage(sendToSet, this);

                foreach (RelayStorage sto in sendToSet)
                {
                    using (sto.lock_messages.AcquireExclusiveUsing())
                        sto.in_Receive(msg);
                    if (sto.m_nextClean_message <= Globals.UpdateCount)
                    {
                        m_logger.debugLog("Running cleanup on message", Logger.severity.INFO);
                        sto.ForEachMessage(s => { });
                    }
                }
            }
            finally
            {
                sendToSet.Clear();
                ResourcePool<HashSet<RelayStorage>>.Pool.Return(sendToSet);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Run the autopilot
        /// </summary>
        private void UpdateThread()
        {
            try
            {
                if (Globals.UpdateCount > m_nextCustomInfo)
                {
                    m_nextCustomInfo = Globals.UpdateCount + 100ul;
                    UpdateCustomInfo();
                }

                switch (m_state)
                {
                    case State.Disabled:
                        if (CheckControl())
                            m_state = State.Enabled;
                        return;
                    case State.Enabled:
                        if (CheckControl())
                            break;
                        m_state = State.Disabled;
                        return;
                    case State.Player:
                        // wait for player to give back control, do not reset
                        if (MyAPIGateway.Players.GetPlayerControllingEntity(m_controlledGrid) == null)
                            m_state = State.Enabled;
                        return;
                    case State.Halted:
                        if (!m_block.AutopilotControl || Globals.ElapsedTime > m_endOfHalt)
                            m_state = State.Disabled;
                        return;
                    case State.Closed:
                        return;
                    default:
                        throw new Exception("Case not implemented: "+m_state);
                }

                if (MyAPIGateway.Players.GetPlayerControllingEntity(m_controlledGrid) != null)
                {
                    m_state = State.Player;
                    return;
                }

                if (m_message != null)
                {
                    m_interpreter.enqueueAllActions(m_message.Content);
                    m_message = null;
                    m_navSet.OnStartOfCommands();
                    m_interpreter.Mover.MoveAndRotateStop(false);
                }

                if (this.Resume != null)
                    ResumeFromSave();

                EnemyFinder ef = m_navSet.Settings_Current.EnemyFinder;
                if (ef != null)
                    ef.Update();

                if (m_navSet.Settings_Current.WaitUntil > Globals.ElapsedTime)
                    return;

                if (m_interpreter.SyntaxError)
                    m_interpreter.Mover.MoveAndRotateStop();
                else if (MoveAndRotate())
                    return;

                if (m_interpreter.hasInstructions())
                {
                    m_logger.debugLog("running instructions");

                    while (m_interpreter.instructionQueue.Count != 0 && m_navSet.Settings_Current.NavigatorMover == null)
                    {
                        m_interpreter.instructionQueue.Dequeue().Invoke();
                        if (m_navSet.Settings_Current.WaitUntil > Globals.ElapsedTime)
                        {
                            m_logger.debugLog("now waiting until " + m_navSet.Settings_Current.WaitUntil);
                            return;
                        }
                    }

                    if (m_navSet.Settings_Current.NavigatorMover == null)
                    {
                        m_logger.debugLog("interpreter did not yield a navigator", Logger.severity.INFO);
                        ReleaseControlledGrid();
                    }
                    return;
                }

                if (!m_interpreter.SyntaxError)
                    if (Rotate())
                        return;

                if (m_nextAllowedInstructions > Globals.ElapsedTime)
                {
                    m_logger.debugLog("Delaying instructions", Logger.severity.INFO);
                    m_navSet.Settings_Task_NavWay.WaitUntil = m_nextAllowedInstructions;
                    return;
                }

                m_logger.debugLog("enqueing instructions", Logger.severity.DEBUG);
                m_nextAllowedInstructions = Globals.ElapsedTime + MinTimeInstructions;
                m_interpreter.enqueueAllActions(m_block.AutopilotTerminal.AutopilotCommands.ToString(), true);

                if (!m_interpreter.hasInstructions())
                    ReleaseControlledGrid();
                m_navSet.OnStartOfCommands();
                m_interpreter.Mover.MoveAndRotateStop(false);
            }
            catch (Exception ex)
            {
                m_logger.alwaysLog("Exception: " + ex, Logger.severity.ERROR);
                m_state = State.Halted;
            }
            finally
            { lock_execution.ReleaseExclusive(); }
        }
Exemplo n.º 8
0
 private void HandleMessage(Message msg)
 {
     m_message = msg;
     m_interpreter.Mover.SetControl(true);
 }