Inheritance: MonoBehaviour
Ejemplo n.º 1
0
 public ChangeScopeCommand(DebugConsole console)
     : base("cd", "Changes the current scope.",
     null, 1, "cd <Scope(^ move up, * move in)>")
 {
     this._Execute = Run;
     this.console = console;
 }
Ejemplo n.º 2
0
 public ExecCommand(DebugConsole console)
     : base("ex", "Executes C# cod e in the current scope.",
     null, 1, "ex <C#-Expression>\n C#-Expression: var S = scope;")
 {
     this._Console = console;
     this._Execute = Run;
 }
Ejemplo n.º 3
0
 private void OnDestroy()
 {
     if (Instance == this)
     {
         Instance = null;
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates a display scope command.
 /// </summary>
 public DisplayScopeCommand(DebugConsole console)
     : base("dir", "Displays all variables within the current scope.",
     null, 0,
     "dir <display-mode> (public/private)\n -Display-Modes: all,fields,properties,methods")
 {
     this.console = console;
     this._Execute = new ExecutionDelegate(this.Run);
 }
Ejemplo n.º 5
0
    public void Start()
    {
        Instance = this;
        _text = GetComponent<Text>();
        _outputConsole = new string[LinesNumber];

        for (int i = 0; i < LinesNumber; i++)
            _outputConsole[i] = "";

    }
 private void Awake()
 {
     if ((_instance != null) && (_instance != this))
     {
         UnityEngine.Object.DestroyImmediate(this, true);
     }
     else
     {
         _instance = this;
     }
 }
Ejemplo n.º 7
0
    void Awake()
    {
        // Do not destroy this game object
        DontDestroyOnLoad(this.gameObject);

        if(instance == null)
        {
            instance = this;
        }
        else
        {
            DestroyImmediate(this.gameObject.transform.root.gameObject);
        }
    }
Ejemplo n.º 8
0
        private void Show()
        {
            _debugConsole = new DebugConsole
            {
                Width = Window.Current.Bounds.Width,
                Height = Window.Current.Bounds.Height,
            };

            _popup = new Popup
            {
                Child = _debugConsole,
                IsOpen = true
            };

            Window.Current.SizeChanged += OnWindowSizeChanged;
        }
Ejemplo n.º 9
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Database.DefaultConnectionFactory = new System.Data.Entity.Infrastructure.SqlCeConnectionFactory(
                "System.Data.SqlServerCe.4.0",
                @".\",
                @"Data Source=.\db.sdf");

            Konzole = new DebugConsole
                          {
                              AutoSave = ConsoleAutoSave.OnLineAdd,
                              Caption = "Konzole",
                              EchoCommands = true,
                              ScreenLocation = ConsoleLocation.TopRight,
                              ProcessInternalCommands = true,
                              UsePlainView = true
                          };
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DataContext>());
            Data.Database.CreateIfNotExists();
            Konzole.Show();
            Application.Run(MainForm = new MainForm());
        }
 public Message(object messageObject, DebugConsole.MessageType messageType, Color displayColor)
 {
     if (messageObject == null)
     {
         this.text = "<null>";
     }
     else
     {
         this.text = messageObject.ToString();
     }
     this.formatted = string.Empty;
     this.type = messageType;
     this.color = displayColor;
 }
Ejemplo n.º 11
0
 void Awake()
 {
     DebugConsole = new DebugConsole();
 }
Ejemplo n.º 12
0
    void Awake()
    {
        s_Instance = this;
        InitGuis();

        GameObject.DontDestroyOnLoad(gameObject);
    }
Ejemplo n.º 13
0
 //private printDelegateType printDelegate;
 //private printDelegateDialogResultType printDelegateDialogResult;
 public StdOutInterfaceForm()
 {
     console = new DebugConsole();
 }
Ejemplo n.º 14
0
 void Awake()
 {
     if (m_instance == null)
     {
         m_instance = this;
     }
     else
     {
         DebugConsole[] consoles = GameObject.FindObjectsOfType<DebugConsole>();
         for (int i = 0; i < consoles.Length; i++)
         {
             if (consoles[i] != m_instance)
                 Destroy(consoles[i].gameObject);
         }
     }
 }
Ejemplo n.º 15
0
        public override void Update(float deltaTime)
        {
            if (netServer == null)
            {
                return;
            }

            if (OnOwnerDetermined != null && OwnerConnection != null)
            {
                OnOwnerDetermined?.Invoke(OwnerConnection);
                OnOwnerDetermined = null;
            }

            netServer.ReadMessages(incomingLidgrenMessages);

            //process incoming connections first
            foreach (NetIncomingMessage inc in incomingLidgrenMessages.Where(m => m.MessageType == NetIncomingMessageType.ConnectionApproval))
            {
                HandleConnection(inc);
            }

            try
            {
                //after processing connections, go ahead with the rest of the messages
                foreach (NetIncomingMessage inc in incomingLidgrenMessages.Where(m => m.MessageType != NetIncomingMessageType.ConnectionApproval))
                {
                    switch (inc.MessageType)
                    {
                    case NetIncomingMessageType.Data:
                        HandleDataMessage(inc);
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        HandleStatusChanged(inc);
                        break;
                    }
                }
            }

            catch (Exception e)
            {
                string errorMsg = "Server failed to read an incoming message. {" + e + "}\n" + e.StackTrace;
                GameAnalyticsManager.AddErrorEventOnce("LidgrenServerPeer.Update:ClientReadException" + e.TargetSite.ToString(), GameAnalyticsSDK.Net.EGAErrorSeverity.Error, errorMsg);
#if DEBUG
                DebugConsole.ThrowError(errorMsg);
#else
                if (GameSettings.VerboseLogging)
                {
                    DebugConsole.ThrowError(errorMsg);
                }
#endif
            }

            for (int i = 0; i < pendingClients.Count; i++)
            {
                PendingClient pendingClient = pendingClients[i];

                var connection = pendingClient.Connection as LidgrenConnection;
                if (connection.NetConnection.Status == NetConnectionStatus.InitiatedConnect ||
                    connection.NetConnection.Status == NetConnectionStatus.ReceivedInitiation ||
                    connection.NetConnection.Status == NetConnectionStatus.RespondedAwaitingApproval ||
                    connection.NetConnection.Status == NetConnectionStatus.RespondedConnect)
                {
                    continue;
                }
                UpdatePendingClient(pendingClient);
                if (i >= pendingClients.Count || pendingClients[i] != pendingClient)
                {
                    i--;
                }
            }

            incomingLidgrenMessages.Clear();
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Add required resources and activate console
 /// </summary>
 /// <param name="module">Console type</param>
 public static void Activate(DebugConsole module)
 {
     Debug.Activate(module, null);
 }
Ejemplo n.º 17
0
        public static void ServerRead(IReadMessage msg, Client c)
        {
            c.KickAFKTimer = 0.0f;

            UInt16          ID   = msg.ReadUInt16();
            ChatMessageType type = (ChatMessageType)msg.ReadByte();
            string          txt;

            Character        orderTargetCharacter = null;
            Entity           orderTargetEntity    = null;
            OrderChatMessage orderMsg             = null;
            OrderTarget      orderTargetPosition  = null;

            if (type == ChatMessageType.Order)
            {
                int orderIndex = msg.ReadByte();
                orderTargetCharacter = Entity.FindEntityByID(msg.ReadUInt16()) as Character;
                orderTargetEntity    = Entity.FindEntityByID(msg.ReadUInt16()) as Entity;
                int orderOptionIndex = msg.ReadByte();
                if (msg.ReadBoolean())
                {
                    var x    = msg.ReadSingle();
                    var y    = msg.ReadSingle();
                    var hull = Entity.FindEntityByID(msg.ReadUInt16()) as Hull;
                    orderTargetPosition = new OrderTarget(new Vector2(x, y), hull, true);
                }

                if (orderIndex < 0 || orderIndex >= Order.PrefabList.Count)
                {
                    DebugConsole.ThrowError($"Invalid order message from client \"{c.Name}\" - order index out of bounds ({orderIndex}, {orderOptionIndex}).");
                    if (NetIdUtils.IdMoreRecent(ID, c.LastSentChatMsgID))
                    {
                        c.LastSentChatMsgID = ID;
                    }
                    return;
                }

                Order  order       = Order.PrefabList[orderIndex];
                string orderOption = orderOptionIndex < 0 || orderOptionIndex >= order.Options.Length ? "" : order.Options[orderOptionIndex];
                orderMsg = new OrderChatMessage(order, orderOption, orderTargetPosition ?? orderTargetEntity as ISpatialEntity, orderTargetCharacter, c.Character);
                txt      = orderMsg.Text;
            }
            else
            {
                txt = msg.ReadString() ?? "";
            }

            if (!NetIdUtils.IdMoreRecent(ID, c.LastSentChatMsgID))
            {
                return;
            }

            c.LastSentChatMsgID = ID;

            if (txt.Length > MaxLength)
            {
                txt = txt.Substring(0, MaxLength);
            }

            c.LastSentChatMessages.Add(txt);
            if (c.LastSentChatMessages.Count > 10)
            {
                c.LastSentChatMessages.RemoveRange(0, c.LastSentChatMessages.Count - 10);
            }

            float similarity = 0.0f;

            for (int i = 0; i < c.LastSentChatMessages.Count; i++)
            {
                float closeFactor = 1.0f / (c.LastSentChatMessages.Count - i);

                if (string.IsNullOrEmpty(txt))
                {
                    similarity += closeFactor;
                }
                else
                {
                    int levenshteinDist = ToolBox.LevenshteinDistance(txt, c.LastSentChatMessages[i]);
                    similarity += Math.Max((txt.Length - levenshteinDist) / (float)txt.Length * closeFactor, 0.0f);
                }
            }
            //order/report messages can be sent a little faster than normal messages without triggering the spam filter
            if (orderMsg != null)
            {
                similarity *= 0.25f;
            }

            bool isOwner = GameMain.Server.OwnerConnection != null && c.Connection == GameMain.Server.OwnerConnection;

            if (similarity + c.ChatSpamSpeed > 5.0f && !isOwner)
            {
                GameMain.Server.KarmaManager.OnSpamFilterTriggered(c);

                c.ChatSpamCount++;
                if (c.ChatSpamCount > 3)
                {
                    //kick for spamming too much
                    GameMain.Server.KickClient(c, TextManager.Get("SpamFilterKicked"));
                }
                else
                {
                    ChatMessage denyMsg = Create("", TextManager.Get("SpamFilterBlocked"), ChatMessageType.Server, null);
                    c.ChatSpamTimer = 10.0f;
                    GameMain.Server.SendDirectChatMessage(denyMsg, c);
                }
                return;
            }

            c.ChatSpamSpeed += similarity + 0.5f;

            if (c.ChatSpamTimer > 0.0f && !isOwner)
            {
                ChatMessage denyMsg = Create("", TextManager.Get("SpamFilterBlocked"), ChatMessageType.Server, null);
                c.ChatSpamTimer = 10.0f;
                GameMain.Server.SendDirectChatMessage(denyMsg, c);
                return;
            }

            if (type == ChatMessageType.Order)
            {
                if (c.Character == null || c.Character.SpeechImpediment >= 100.0f || c.Character.IsDead)
                {
                    return;
                }
                if (orderMsg.Order.TargetAllCharacters)
                {
                    HumanAIController.ReportProblem(orderMsg.Sender, orderMsg.Order);
                }
                else if (orderTargetCharacter != null)
                {
                    var order = orderTargetPosition == null ?
                                new Order(orderMsg.Order.Prefab, orderTargetEntity, orderMsg.Order.Prefab?.GetTargetItemComponent(orderTargetEntity as Item), orderMsg.Sender) :
                                new Order(orderMsg.Order.Prefab, orderTargetPosition, orderMsg.Sender);
                    orderTargetCharacter.SetOrder(order, orderMsg.OrderOption, orderMsg.Sender);
                }
                GameMain.Server.SendOrderChatMessage(orderMsg);
            }
            else
            {
                GameMain.Server.SendChatMessage(txt, null, c);
            }
        }
Ejemplo n.º 18
0
        public void Read(IReadMessage msg)
        {
            byte      queueId = msg.ReadByte();
            VoipQueue queue   = queues.Find(q => q.QueueID == queueId);

            if (queue == null)
            {
#if DEBUG
                DebugConsole.NewMessage("Couldn't find VoipQueue with id " + queueId.ToString() + "!", GUI.Style.Red);
#endif
                return;
            }

            Client client = gameClient.ConnectedClients.Find(c => c.VoipQueue == queue);
            if (queue.Read(msg, discardData: client.Muted || client.MutedLocally))
            {
                if (client.Muted || client.MutedLocally)
                {
                    return;
                }
                if (client.VoipSound == null)
                {
                    DebugConsole.Log("Recreating voipsound " + queueId);
                    client.VoipSound = new VoipSound(client.Name, GameMain.SoundManager, client.VoipQueue);
                }

                if (client.Character != null && !client.Character.IsDead && !client.Character.Removed && client.Character.SpeechImpediment <= 100.0f)
                {
                    WifiComponent radio       = null;
                    var           messageType = !client.VoipQueue.ForceLocal && ChatMessage.CanUseRadio(client.Character, out radio) ? ChatMessageType.Radio : ChatMessageType.Default;
                    client.Character.ShowSpeechBubble(1.25f, ChatMessage.MessageColor[(int)messageType]);

                    client.VoipSound.UseRadioFilter = messageType == ChatMessageType.Radio;
                    if (client.VoipSound.UseRadioFilter)
                    {
                        client.VoipSound.SetRange(radio.Range * 0.8f, radio.Range);
                    }
                    else
                    {
                        client.VoipSound.SetRange(ChatMessage.SpeakRange * 0.4f, ChatMessage.SpeakRange);
                    }
                    if (!client.VoipSound.UseRadioFilter && Character.Controlled != null)
                    {
                        client.VoipSound.UseMuffleFilter = SoundPlayer.ShouldMuffleSound(Character.Controlled, client.Character.WorldPosition, ChatMessage.SpeakRange, client.Character.CurrentHull);
                    }
                }

                GameMain.NetLobbyScreen?.SetPlayerSpeaking(client);
                GameMain.GameSession?.CrewManager?.SetClientSpeaking(client);

                if ((client.VoipSound.CurrentAmplitude * client.VoipSound.Gain * GameMain.SoundManager.GetCategoryGainMultiplier("voip")) > 0.1f) //TODO: might need to tweak
                {
                    if (client.Character != null && !client.Character.Removed)
                    {
                        Vector3 clientPos       = new Vector3(client.Character.WorldPosition.X, client.Character.WorldPosition.Y, 0.0f);
                        Vector3 listenerPos     = GameMain.SoundManager.ListenerPosition;
                        float   attenuationDist = client.VoipSound.Near * 1.125f;
                        if (Vector3.DistanceSquared(clientPos, listenerPos) < attenuationDist * attenuationDist)
                        {
                            GameMain.SoundManager.VoipAttenuatedGain = 0.5f;
                        }
                    }
                    else
                    {
                        GameMain.SoundManager.VoipAttenuatedGain = 0.5f;
                    }
                }
            }
        }
        protected override void Update()
        {
            base.Update();

            // ESC
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                Debug.Log("press key ESC : Application Quit");
                DebugConsole.Log("press key ESC : Application Quit");

                if (Application.platform != RuntimePlatform.WindowsEditor)
                {
                    // アプリケーション終了
                    Application.Quit();
                }
            }

            // D
            if (Input.GetKeyDown(KeyCode.D))
            {
                Debug.Log("press key D : Visible Debug View");
                DebugConsole.Log("press key D : Visible Debug View");

                // デバッグ表示のトグル
                DebugManager debugManager = AppMain.Instance.debugManager;
                debugManager.IsDebug = !debugManager.IsDebug;
                debugManager.ToggleShowDebugView();
            }

            // C
            if (Input.GetKeyDown(KeyCode.C))
            {
                Debug.Log("press key C : Clear DebugConsole");
                DebugConsole.Log("press key C : Clear DebugConsole");

                // デバッグコンソールのクリア
                DebugConsole.Clear();
            }

            // G
            if (Input.GetKeyDown(KeyCode.G))
            {
                Debug.Log("press key G : System GC Collect");
                DebugConsole.Log("press key G : System GC Collect");

                // 強制CG
                System.GC.Collect();
            }

            // R
            if (Input.GetKeyDown(KeyCode.R))
            {
                Debug.Log("press key R : Reload ApplicationSetting");
                DebugConsole.Log("press key R : Reload ApplicationSetting");

                // 設定ファイルの再読み込み
                ApplicationSetting.Instance.LoadXML();
            }

            // Space
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Debug.Log("press key Space : Change Stage");
                DebugConsole.Log("press key Space : Change Stage");

                // ステージの変更
                SceneStateManager sceneStateManager = AppMain.Instance.sceneStateManager;
                TimeManager       timeManager       = AppMain.Instance.timeManager;
                if (sceneStateManager.CurrentState == SceneStateManager.SceneState.STARTUP)
                {
                    sceneStateManager.ChangeState(SceneStateManager.SceneState.WAIT);
                }
                else if (sceneStateManager.CurrentState == SceneStateManager.SceneState.WAIT)
                {
                    sceneStateManager.ChangeState(SceneStateManager.SceneState.PLAY);
                }
                else if (sceneStateManager.CurrentState == SceneStateManager.SceneState.PLAY)
                {
                    timeManager.timerEvents[0].StartTimer(ApplicationSetting.Instance.GetInt("GameTime"));
                }
                else if (sceneStateManager.CurrentState == SceneStateManager.SceneState.RESULT)
                {
                    sceneStateManager.ChangeAsyncState(SceneStateManager.SceneState.WAIT);
                }
            }
        }
Ejemplo n.º 20
0
    //--------------------------------------
    // INITIALIZATION
    //--------------------------------------
    void Awake()
    {
        _instance = this;

        _messages = new List<Message>();
        _windowRect = new Rect( 30.0F, 30.0F, 600.0F, 450.0F );
        _cmdTable = new Hashtable();
        _watchVarTable = new Hashtable();
        _displayString = new StringBuilder();

        _messages.Add( new Message( " CCSoft console version " + VERSION.ToString( "F2" ), MessageTypes.System ) );

        this.RegisterCommandCallback( "close", CMDClose );
        this.RegisterCommandCallback( "clear", CMDClear );
    }
Ejemplo n.º 21
0
        private void PushCharactersAway()
        {
            //push characters out of the doorway when the door is closing/opening
            Vector2 simPos = ConvertUnits.ToSimUnits(new Vector2(item.Rect.X, item.Rect.Y));

            Vector2 currSize = isHorizontal ?
                               new Vector2(item.Rect.Width * (1.0f - openState), doorSprite.size.Y) :
                               new Vector2(doorSprite.size.X, item.Rect.Height * (1.0f - openState));

            Vector2 simSize = ConvertUnits.ToSimUnits(currSize);

            if (!MathUtils.IsValid(item.SimPosition))
            {
                DebugConsole.ThrowError("Failed to push a character out of a doorway - position of the door is not valid (" + item.SimPosition + ")");
                GameAnalyticsManager.AddErrorEventOnce("PushCharactersAway:DoorPosInvalid", GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
                                                       "Failed to push a character out of a doorway - position of the door is not valid (" + item.SimPosition + ").");
                return;
            }

            foreach (Character c in Character.CharacterList)
            {
                if (!c.Enabled)
                {
                    continue;
                }
                if (!MathUtils.IsValid(c.SimPosition))
                {
                    DebugConsole.ThrowError("Failed to push a character out of a doorway - position of the character \"" + c.Name + "\" is not valid (" + c.SimPosition + ")");
                    GameAnalyticsManager.AddErrorEventOnce("PushCharactersAway:CharacterPosInvalid", GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
                                                           "Failed to push a character out of a doorway - position of the character \"" + c.Name + "\" is not valid (" + c.SimPosition + ")." +
                                                           " Removed: " + c.Removed +
                                                           " Remoteplayer: " + c.IsRemotePlayer);
                    continue;
                }
                int dir = isHorizontal ? Math.Sign(c.SimPosition.Y - item.SimPosition.Y) : Math.Sign(c.SimPosition.X - item.SimPosition.X);
                //Nilmod crash prevention
                if (c?.AnimController?.Limbs != null)
                {
                    List <PhysicsBody> bodies = c.AnimController.Limbs.Select(l => l.body).ToList();
                    bodies.Add(c.AnimController.Collider);

                    foreach (PhysicsBody body in bodies)
                    {
                        if (!MathUtils.IsValid(body.SimPosition))
                        {
                            DebugConsole.ThrowError("Failed to push a limb out of a doorway - position of the body (character \"" + c.Name + "\") is not valid (" + body.SimPosition + ")");
                            GameAnalyticsManager.AddErrorEventOnce("PushCharactersAway:LimbPosInvalid", GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
                                                                   "Failed to push a character out of a doorway - position of the character \"" + c.Name + "\" is not valid (" + body.SimPosition + ")." +
                                                                   " Removed: " + c.Removed +
                                                                   " Remoteplayer: " + c.IsRemotePlayer);
                            continue;
                        }
                        float diff = 0.0f;

                        if (isHorizontal)
                        {
                            if (body.SimPosition.X < simPos.X || body.SimPosition.X > simPos.X + simSize.X)
                            {
                                continue;
                            }

                            diff = body.SimPosition.Y - item.SimPosition.Y;
                        }
                        else
                        {
                            if (body.SimPosition.Y > simPos.Y || body.SimPosition.Y < simPos.Y - simSize.Y)
                            {
                                continue;
                            }

                            diff = body.SimPosition.X - item.SimPosition.X;
                        }

                        if (Math.Sign(diff) != dir)
                        {
#if CLIENT
                            SoundPlayer.PlayDamageSound("LimbBlunt", 1.0f, body);
#endif

                            if (isHorizontal)
                            {
                                body.SetTransform(new Vector2(body.SimPosition.X, item.SimPosition.Y + dir * simSize.Y * 2.0f), body.Rotation);
                                body.ApplyLinearImpulse(new Vector2(isOpen ? 0.0f : 1.0f, dir * 2.0f));
                            }
                            else
                            {
                                body.SetTransform(new Vector2(item.SimPosition.X + dir * simSize.X * 1.2f, body.SimPosition.Y), body.Rotation);
                                body.ApplyLinearImpulse(new Vector2(dir * 0.5f, isOpen ? 0.0f : -1.0f));
                            }
                        }

                        if (isHorizontal)
                        {
                            if (Math.Abs(body.SimPosition.Y - item.SimPosition.Y) > simSize.Y * 0.5f)
                            {
                                continue;
                            }

                            body.ApplyLinearImpulse(new Vector2(isOpen ? 0.0f : 1.0f, dir * 0.5f));
                        }
                        else
                        {
                            if (Math.Abs(body.SimPosition.X - item.SimPosition.X) > simSize.X * 0.5f)
                            {
                                continue;
                            }

                            body.ApplyLinearImpulse(new Vector2(dir * 0.5f, isOpen ? 0.0f : -1.0f));
                        }

                        //c.SetStun(GameMain.NilMod.DoorStun);
                        c.SetStun(0.2f);
                    }
                }
            }
        }
Ejemplo n.º 22
0
        public override void Send(IWriteMessage msg, DeliveryMethod deliveryMethod)
        {
            if (!isActive)
            {
                return;
            }

            byte[] buf = new byte[msg.LengthBytes + 4];
            buf[0] = (byte)deliveryMethod;

            byte[] bufAux = new byte[msg.LengthBytes];
            bool   isCompressed; int length;

            msg.PrepareForSending(ref bufAux, out isCompressed, out length);

            buf[1] = (byte)(isCompressed ? PacketHeader.IsCompressed : PacketHeader.None);

            buf[2] = (byte)(length & 0xff);
            buf[3] = (byte)((length >> 8) & 0xff);

            Array.Copy(bufAux, 0, buf, 4, length);

            Facepunch.Steamworks.Networking.SendType sendType;
            switch (deliveryMethod)
            {
            case DeliveryMethod.Reliable:
            case DeliveryMethod.ReliableOrdered:
                //the documentation seems to suggest that the Reliable send type
                //enforces packet order (TODO: verify)
                sendType = Facepunch.Steamworks.Networking.SendType.Reliable;
                break;

            default:
                sendType = Facepunch.Steamworks.Networking.SendType.Unreliable;
                break;
            }

            if (length + 8 >= MsgConstants.MTU)
            {
                DebugConsole.Log("WARNING: message length comes close to exceeding MTU, forcing reliable send (" + length.ToString() + " bytes)");
                sendType = Facepunch.Steamworks.Networking.SendType.Reliable;
            }

            heartbeatTimer = 5.0;

#if DEBUG
            CoroutineManager.InvokeAfter(() =>
            {
                if (GameMain.Client == null)
                {
                    return;
                }
                if (Rand.Range(0.0f, 1.0f) < GameMain.Client.SimulatedLoss && sendType != Facepunch.Steamworks.Networking.SendType.Reliable)
                {
                    return;
                }
                int count = Rand.Range(0.0f, 1.0f) < GameMain.Client.SimulatedDuplicatesChance ? 2 : 1;
                for (int i = 0; i < count; i++)
                {
                    Send(buf, length + 4, sendType);
                }
            },
                                         GameMain.Client.SimulatedMinimumLatency + Rand.Range(0.0f, GameMain.Client.SimulatedRandomLatency));
#else
            Send(buf, length + 4, sendType);
#endif
        }
        public void Update(List <Client> clients)
        {
            foreach (BufferedEvent bufferedEvent in bufferedEvents)
            {
                if (bufferedEvent.Character == null || bufferedEvent.Character.IsDead)
                {
                    bufferedEvent.IsProcessed = true;
                    continue;
                }

                //delay reading the events until the inputs for the corresponding frame have been processed

                //UNLESS the character is unconscious, in which case we'll read the messages immediately (because further inputs will be ignored)
                //atm the "give in" command is the only thing unconscious characters can do, other types of events are ignored
                if (!bufferedEvent.Character.IsUnconscious &&
                    NetIdUtils.IdMoreRecent(bufferedEvent.CharacterStateID, bufferedEvent.Character.LastProcessedID))
                {
                    continue;
                }

                try
                {
                    ReadEvent(bufferedEvent.Data, bufferedEvent.TargetEntity, bufferedEvent.Sender);
                }

                catch (Exception e)
                {
                    string entityName = bufferedEvent.TargetEntity == null ? "null" : bufferedEvent.TargetEntity.ToString();
                    if (GameSettings.VerboseLogging)
                    {
                        DebugConsole.ThrowError("Failed to read server event for entity \"" + entityName + "\"!", e);
                    }
                    GameAnalyticsManager.AddErrorEventOnce("ServerEntityEventManager.Read:ReadFailed" + entityName,
                                                           GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
                                                           "Failed to read server event for entity \"" + entityName + "\"!\n" + e.StackTrace);
                }

                bufferedEvent.IsProcessed = true;
            }

            var inGameClients = clients.FindAll(c => c.InGame && !c.NeedsMidRoundSync);

            if (inGameClients.Count > 0)
            {
                lastSentToAll = inGameClients[0].LastRecvEntityEventID;
                inGameClients.ForEach(c => { if (NetIdUtils.IdMoreRecent((ushort)(lastSentToAll - 1), c.LastRecvEntityEventID))
                                             {
                                                 lastSentToAll = (ushort)(c.LastRecvEntityEventID - 1);
                                             }
                                      });

                ServerEntityEvent firstEventToResend = events.Find(e => e.ID == (ushort)(lastSentToAll + 1));
                if (firstEventToResend != null && (Timing.TotalTime - firstEventToResend.CreateTime) > (10.0f * GameMain.NilMod.DesyncTimerMultiplier))
                {
                    //it's been 10 seconds since this event was created
                    //kick everyone that hasn't received it yet, this is way too old
                    List <Client> toKick = inGameClients.FindAll(c => NetIdUtils.IdMoreRecent((UInt16)(lastSentToAll + 1), c.LastRecvEntityEventID));
                    toKick.ForEach(c =>
                    {
                        DebugConsole.NewMessage(c.Name + " was kicked due to excessive desync (expected old event " + c.LastRecvEntityEventID.ToString() + ")", Microsoft.Xna.Framework.Color.Red);
                        server.DisconnectClient(c, "", "You have been disconnected because of excessive desync (Expected old event, inform the server host increasing 'DesyncTimerMultiplier' could help.");
                    }
                                   );
                }

                if (events.Count > 0)
                {
                    //the client is waiting for an event that we don't have anymore
                    //(the ID they're expecting is smaller than the ID of the first event in our list)
                    List <Client> toKick = inGameClients.FindAll(c => NetIdUtils.IdMoreRecent(events[0].ID, (UInt16)(c.LastRecvEntityEventID + 1)));
                    toKick.ForEach(c =>
                    {
                        DebugConsole.NewMessage(c.Name + " was kicked due to excessive desync (expected " + c.LastRecvEntityEventID.ToString() + ", last available is " + events[0].ID.ToString() + ")", Microsoft.Xna.Framework.Color.Red);
                        server.DisconnectClient(c, "", "You have been disconnected because of excessive desync (Event no longer exists)");
                    }
                                   );
                }
            }

            var timedOutClients = clients.FindAll(c => c.InGame && c.NeedsMidRoundSync && Timing.TotalTime > c.MidRoundSyncTimeOut);

            timedOutClients.ForEach(c => GameMain.Server.DisconnectClient(c, "", "You have been disconnected because syncing your client with the server took too long."));

            bufferedEvents.RemoveAll(b => b.IsProcessed);
        }
Ejemplo n.º 24
0
        private void ReadClientSteamAuthRequest(NetIncomingMessage inc, NetConnection senderConnection, out ulong clientSteamID)
        {
            clientSteamID = 0;
            if (!Steam.SteamManager.USE_STEAM)
            {
                DebugConsole.Log("Received a Steam auth request from " + senderConnection.RemoteEndPoint + ". Steam authentication not required, handling auth normally.");
                //not using steam, handle auth normally
                HandleClientAuthRequest(senderConnection, 0);
                return;
            }

            if (senderConnection == OwnerConnection)
            {
                //the client is the owner of the server, no need for authentication
                //(it would fail with a "duplicate request" error anyway)
                HandleClientAuthRequest(senderConnection, 0);
                return;
            }

            clientSteamID = inc.ReadUInt64();
            int authTicketLength = inc.ReadInt32();

            inc.ReadBytes(authTicketLength, out byte[] authTicketData);

            DebugConsole.Log("Received a Steam auth request");
            DebugConsole.Log("  Steam ID: " + clientSteamID);
            DebugConsole.Log("  Auth ticket length: " + authTicketLength);
            DebugConsole.Log("  Auth ticket data: " +
                             ((authTicketData == null) ? "null" : ToolBox.LimitString(string.Concat(authTicketData.Select(b => b.ToString("X2"))), 16)));

            if (senderConnection != OwnerConnection &&
                serverSettings.BanList.IsBanned(senderConnection.RemoteEndPoint.Address, clientSteamID))
            {
                return;
            }
            ulong steamID = clientSteamID;

            if (unauthenticatedClients.Any(uc => uc.Connection == inc.SenderConnection))
            {
                var steamAuthedClient = unauthenticatedClients.Find(uc =>
                                                                    uc.Connection == inc.SenderConnection &&
                                                                    uc.SteamID == steamID &&
                                                                    uc.SteamAuthStatus == Facepunch.Steamworks.ServerAuth.Status.OK);
                if (steamAuthedClient != null)
                {
                    DebugConsole.Log("Client already authenticated, sending AUTH_RESPONSE again...");
                    HandleClientAuthRequest(inc.SenderConnection, steamID);
                }
                DebugConsole.Log("Steam authentication already pending...");
                return;
            }

            if (authTicketData == null)
            {
                DebugConsole.Log("Invalid request");
                return;
            }

            unauthenticatedClients.RemoveAll(uc => uc.Connection == senderConnection);
            int nonce        = CryptoRandom.Instance.Next();
            var unauthClient = new UnauthenticatedClient(senderConnection, nonce, clientSteamID)
            {
                AuthTimer = 20
            };

            unauthenticatedClients.Add(unauthClient);

            if (!Steam.SteamManager.StartAuthSession(authTicketData, clientSteamID))
            {
                unauthenticatedClients.Remove(unauthClient);
                if (GameMain.Config.RequireSteamAuthentication)
                {
                    unauthClient.Connection.Disconnect(DisconnectReason.SteamAuthenticationFailed.ToString());
                    Log("Disconnected unauthenticated client (Steam ID: " + steamID + "). Steam authentication failed.", ServerLog.MessageType.ServerMessage);
                }
                else
                {
                    DebugConsole.Log("Steam authentication failed, skipping to basic auth...");
                    HandleClientAuthRequest(senderConnection);
                    return;
                }
            }

            return;
        }
Ejemplo n.º 25
0
        private void LoadSettings()
        {
            XDocument doc = null;

            if (File.Exists(SettingsFile))
            {
                doc = XMLExtensions.TryLoadXml(SettingsFile);
            }

            if (doc == null)
            {
                doc = new XDocument(new XElement("serversettings"));
            }

            SerializableProperties = SerializableProperty.DeserializeProperties(this, doc.Root);

            AutoRestart = doc.Root.GetAttributeBool("autorestart", false);

            Voting.AllowSubVoting  = SubSelectionMode == SelectionMode.Vote;
            Voting.AllowModeVoting = ModeSelectionMode == SelectionMode.Vote;

            selectedLevelDifficulty = doc.Root.GetAttributeFloat("LevelDifficulty", 20.0f);
            GameMain.NetLobbyScreen.SetLevelDifficulty(selectedLevelDifficulty);

            GameMain.NetLobbyScreen.SetTraitorsEnabled(traitorsEnabled);

            string[] defaultAllowedClientNameChars =
                new string[] {
                "32-33",
                "38-46",
                "48-57",
                "65-90",
                "91",
                "93",
                "95-122",
                "192-255",
                "384-591",
                "1024-1279",
                "19968-40959", "13312-19903", "131072-15043983", "15043985-173791", "173824-178207", "178208-183983", "63744-64255", "194560-195103" //CJK
            };

            string[] allowedClientNameCharsStr = doc.Root.GetAttributeStringArray("AllowedClientNameChars", defaultAllowedClientNameChars);
            if (doc.Root.GetAttributeString("AllowedClientNameChars", "") == "65-90,97-122,48-59")
            {
                allowedClientNameCharsStr = defaultAllowedClientNameChars;
            }

            foreach (string allowedClientNameCharRange in allowedClientNameCharsStr)
            {
                string[] splitRange = allowedClientNameCharRange.Split('-');
                if (splitRange.Length == 0 || splitRange.Length > 2)
                {
                    DebugConsole.ThrowError("Error in server settings - " + allowedClientNameCharRange + " is not a valid range for characters allowed in client names.");
                    continue;
                }

                int min = -1;
                if (!int.TryParse(splitRange[0], out min))
                {
                    DebugConsole.ThrowError("Error in server settings - " + allowedClientNameCharRange + " is not a valid range for characters allowed in client names.");
                    continue;
                }
                int max = min;
                if (splitRange.Length == 2)
                {
                    if (!int.TryParse(splitRange[1], out max))
                    {
                        DebugConsole.ThrowError("Error in server settings - " + allowedClientNameCharRange + " is not a valid range for characters allowed in client names.");
                        continue;
                    }
                }

                if (min > -1 && max > -1)
                {
                    AllowedClientNameChars.Add(new Pair <int, int>(min, max));
                }
            }

            AllowedRandomMissionTypes = new List <MissionType>();
            string[] allowedMissionTypeNames = doc.Root.GetAttributeStringArray(
                "AllowedRandomMissionTypes", Enum.GetValues(typeof(MissionType)).Cast <MissionType>().Select(m => m.ToString()).ToArray());
            foreach (string missionTypeName in allowedMissionTypeNames)
            {
                if (Enum.TryParse(missionTypeName, out MissionType missionType))
                {
                    if (missionType == Barotrauma.MissionType.None)
                    {
                        continue;
                    }
                    AllowedRandomMissionTypes.Add(missionType);
                }
            }

            ServerName = doc.Root.GetAttributeString("name", "");
            if (ServerName.Length > NetConfig.ServerNameMaxLength)
            {
                ServerName = ServerName.Substring(0, NetConfig.ServerNameMaxLength);
            }
            ServerMessageText = doc.Root.GetAttributeString("ServerMessage", "");

            GameMain.NetLobbyScreen.SelectedModeIdentifier = GameModeIdentifier;
            //handle Random as the mission type, which is no longer a valid setting
            //MissionType.All offers equivalent functionality
            if (MissionType == "Random")
            {
                MissionType = "All";
            }
            GameMain.NetLobbyScreen.MissionTypeName = MissionType;

            GameMain.NetLobbyScreen.SetBotSpawnMode(BotSpawnMode);
            GameMain.NetLobbyScreen.SetBotCount(BotCount);

            List <string> monsterNames = CharacterPrefab.Prefabs.Select(p => p.Identifier).ToList();

            MonsterEnabled = new Dictionary <string, bool>();
            foreach (string s in monsterNames)
            {
                if (!MonsterEnabled.ContainsKey(s))
                {
                    MonsterEnabled.Add(s, true);
                }
            }
        }
Ejemplo n.º 26
0
 public DebugDescriptor(DebugConsole type, string css, string script)
 {
     this.type = type;
     this.css = css;
     this.script = script;
 }
Ejemplo n.º 27
0
        public void LoadClientPermissions()
        {
            ClientPermissions.Clear();

            if (!File.Exists(ClientPermissionsFile))
            {
                if (File.Exists("Data/clientpermissions.txt"))
                {
                    LoadClientPermissionsOld("Data/clientpermissions.txt");
                }
                return;
            }

            XDocument doc = XMLExtensions.TryLoadXml(ClientPermissionsFile);

            if (doc == null)
            {
                return;
            }
            foreach (XElement clientElement in doc.Root.Elements())
            {
                string clientName     = clientElement.GetAttributeString("name", "");
                string clientEndPoint = clientElement.GetAttributeString("endpoint", null) ?? clientElement.GetAttributeString("ip", "");
                string steamIdStr     = clientElement.GetAttributeString("steamid", "");

                if (string.IsNullOrWhiteSpace(clientName))
                {
                    DebugConsole.ThrowError("Error in " + ClientPermissionsFile + " - all clients must have a name and an IP address.");
                    continue;
                }
                if (string.IsNullOrWhiteSpace(clientEndPoint) && string.IsNullOrWhiteSpace(steamIdStr))
                {
                    DebugConsole.ThrowError("Error in " + ClientPermissionsFile + " - all clients must have an IP address or a Steam ID.");
                    continue;
                }

                ClientPermissions           permissions       = Networking.ClientPermissions.None;
                List <DebugConsole.Command> permittedCommands = new List <DebugConsole.Command>();

                if (clientElement.Attribute("preset") == null)
                {
                    string permissionsStr = clientElement.GetAttributeString("permissions", "");
                    if (permissionsStr.Equals("all", StringComparison.OrdinalIgnoreCase))
                    {
                        foreach (ClientPermissions permission in Enum.GetValues(typeof(ClientPermissions)))
                        {
                            permissions |= permission;
                        }
                    }
                    else if (!Enum.TryParse(permissionsStr, out permissions))
                    {
                        DebugConsole.ThrowError("Error in " + ClientPermissionsFile + " - \"" + permissionsStr + "\" is not a valid client permission.");
                        continue;
                    }

                    if (permissions.HasFlag(Networking.ClientPermissions.ConsoleCommands))
                    {
                        foreach (XElement commandElement in clientElement.Elements())
                        {
                            if (!commandElement.Name.ToString().Equals("command", StringComparison.OrdinalIgnoreCase))
                            {
                                continue;
                            }

                            string commandName           = commandElement.GetAttributeString("name", "");
                            DebugConsole.Command command = DebugConsole.FindCommand(commandName);
                            if (command == null)
                            {
                                DebugConsole.ThrowError("Error in " + ClientPermissionsFile + " - \"" + commandName + "\" is not a valid console command.");
                                continue;
                            }

                            permittedCommands.Add(command);
                        }
                    }
                }
                else
                {
                    string           presetName = clientElement.GetAttributeString("preset", "");
                    PermissionPreset preset     = PermissionPreset.List.Find(p => p.Name == presetName);
                    if (preset == null)
                    {
                        DebugConsole.ThrowError("Failed to restore saved permissions to the client \"" + clientName + "\". Permission preset \"" + presetName + "\" not found.");
                        return;
                    }
                    else
                    {
                        permissions       = preset.Permissions;
                        permittedCommands = preset.PermittedCommands.ToList();
                    }
                }

                if (!string.IsNullOrEmpty(steamIdStr))
                {
                    if (ulong.TryParse(steamIdStr, out ulong steamID))
                    {
                        ClientPermissions.Add(new SavedClientPermission(clientName, steamID, permissions, permittedCommands));
                    }
                    else
                    {
                        DebugConsole.ThrowError("Error in " + ClientPermissionsFile + " - \"" + steamIdStr + "\" is not a valid Steam ID.");
                        continue;
                    }
                }
                else
                {
                    ClientPermissions.Add(new SavedClientPermission(clientName, clientEndPoint, permissions, permittedCommands));
                }
            }
        }
Ejemplo n.º 28
0
	void Awake()
	{
		s_Instance = this;
		InitGuis();
 
	}
Ejemplo n.º 29
0
        public void SaveClientPermissions()
        {
            //delete old client permission file
            if (File.Exists("Data/clientpermissions.txt"))
            {
                File.Delete("Data/clientpermissions.txt");
            }

            GameServer.Log("Saving client permissions", ServerLog.MessageType.ServerMessage);

            XDocument doc = new XDocument(new XElement("ClientPermissions"));

            foreach (SavedClientPermission clientPermission in ClientPermissions)
            {
                var matchingPreset = PermissionPreset.List.Find(p => p.MatchesPermissions(clientPermission.Permissions, clientPermission.PermittedCommands));
                if (matchingPreset != null && matchingPreset.Name == "None")
                {
                    continue;
                }

                XElement clientElement = new XElement("Client",
                                                      new XAttribute("name", clientPermission.Name));

                if (clientPermission.SteamID > 0)
                {
                    clientElement.Add(new XAttribute("steamid", clientPermission.SteamID));
                }
                else
                {
                    clientElement.Add(new XAttribute("endpoint", clientPermission.EndPoint));
                }

                if (matchingPreset == null)
                {
                    clientElement.Add(new XAttribute("permissions", clientPermission.Permissions.ToString()));
                    if (clientPermission.Permissions.HasFlag(Networking.ClientPermissions.ConsoleCommands))
                    {
                        foreach (DebugConsole.Command command in clientPermission.PermittedCommands)
                        {
                            clientElement.Add(new XElement("command", new XAttribute("name", command.names[0])));
                        }
                    }
                }
                else
                {
                    clientElement.Add(new XAttribute("preset", matchingPreset.Name));
                }
                doc.Root.Add(clientElement);
            }

            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.NewLineOnAttributes = true;

                using (var writer = XmlWriter.Create(ClientPermissionsFile, settings))
                {
                    doc.Save(writer);
                }
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Saving client permissions to " + ClientPermissionsFile + " failed", e);
            }
        }
Ejemplo n.º 30
0
 void Awake()
 {
     dc = this;
     textBox = GetComponent<Text>();
 }
Ejemplo n.º 31
0
 public void Initialize()
 {
     _console         = new DebugConsole(new Vector2i((int)CluwneLib.Window.Viewport.Size.X, 400));
     _console.Visible = false;
 }
Ejemplo n.º 32
0
	void Start()
	{
		currentConsole=gameObject.GetComponent<Text>();
		inst=this;
	}
Ejemplo n.º 33
0
        private void UpdateCapture()
        {
            Array.Copy(uncompressedBuffer, 0, prevUncompressedBuffer, 0, VoipConfig.BUFFER_SIZE);
            Array.Clear(uncompressedBuffer, 0, VoipConfig.BUFFER_SIZE);
            nativeBuffer = Marshal.AllocHGlobal(VoipConfig.BUFFER_SIZE * 2);
            try
            {
                while (capturing)
                {
                    int alcError;

                    if (CanDetectDisconnect)
                    {
                        Alc.GetInteger(captureDevice, Alc.EnumConnected, out int isConnected);
                        alcError = Alc.GetError(captureDevice);
                        if (alcError != Alc.NoError)
                        {
                            throw new Exception("Failed to determine if capture device is connected: " + alcError.ToString());
                        }

                        if (isConnected == 0)
                        {
                            DebugConsole.ThrowError("Capture device has been disconnected. You can select another available device in the settings.");
                            Disconnected = true;
                            break;
                        }
                    }

                    FillBuffer();

                    alcError = Alc.GetError(captureDevice);
                    if (alcError != Alc.NoError)
                    {
                        throw new Exception("Failed to capture samples: " + alcError.ToString());
                    }

                    double maxAmplitude = 0.0f;
                    for (int i = 0; i < VoipConfig.BUFFER_SIZE; i++)
                    {
                        uncompressedBuffer[i] = (short)MathHelper.Clamp((uncompressedBuffer[i] * Gain), -short.MaxValue, short.MaxValue);
                        double sampleVal = uncompressedBuffer[i] / (double)short.MaxValue;
                        maxAmplitude = Math.Max(maxAmplitude, Math.Abs(sampleVal));
                    }
                    double dB = Math.Min(20 * Math.Log10(maxAmplitude), 0.0);

                    LastdB = dB;
                    LastAmplitude = maxAmplitude;

                    bool allowEnqueue = overrideSound != null;
                    if (GameMain.WindowActive)
                    {
                        ForceLocal = captureTimer > 0 ? ForceLocal : GameMain.Config.UseLocalVoiceByDefault;
                        bool pttDown = false;
                        if ((PlayerInput.KeyDown(InputType.Voice) || PlayerInput.KeyDown(InputType.LocalVoice)) &&
                                GUI.KeyboardDispatcher.Subscriber == null)
                        {
                            pttDown = true;
                            if (PlayerInput.KeyDown(InputType.LocalVoice))
                            {
                                ForceLocal = true;
                            }
                            else
                            {
                                ForceLocal = false;
                            }
                        }
                        if (GameMain.Config.VoiceSetting == GameSettings.VoiceMode.Activity)
                        {
                            if (dB > GameMain.Config.NoiseGateThreshold)
                            {
                                allowEnqueue = true;
                            }
                        }
                        else if (GameMain.Config.VoiceSetting == GameSettings.VoiceMode.PushToTalk)
                        {
                            if (pttDown)
                            {
                                allowEnqueue = true;
                            }
                        }
                    }

                    if (allowEnqueue || captureTimer > 0)
                    {
                        LastEnqueueAudio = DateTime.Now;
                        if (GameMain.Client?.Character != null)
                        {
                            var messageType = !ForceLocal && ChatMessage.CanUseRadio(GameMain.Client.Character, out _) ? ChatMessageType.Radio : ChatMessageType.Default;
                            GameMain.Client.Character.ShowSpeechBubble(1.25f, ChatMessage.MessageColor[(int)messageType]);
                        }
                        //encode audio and enqueue it
                        lock (buffers)
                        {
                            if (!prevCaptured) //enqueue the previous buffer if not sent to avoid cutoff
                            {
                                int compressedCountPrev = VoipConfig.Encoder.Encode(prevUncompressedBuffer, 0, VoipConfig.BUFFER_SIZE, BufferToQueue, 0, VoipConfig.MAX_COMPRESSED_SIZE);
                                EnqueueBuffer(compressedCountPrev);
                            }
                            int compressedCount = VoipConfig.Encoder.Encode(uncompressedBuffer, 0, VoipConfig.BUFFER_SIZE, BufferToQueue, 0, VoipConfig.MAX_COMPRESSED_SIZE);
                            EnqueueBuffer(compressedCount);
                        }
                        captureTimer -= (VoipConfig.BUFFER_SIZE * 1000) / VoipConfig.FREQUENCY;
                        if (allowEnqueue)
                        {
                            captureTimer = GameMain.Config.VoiceChatCutoffPrevention;
                        }
                        prevCaptured = true;
                    }
                    else
                    {
                        captureTimer = 0;
                        prevCaptured = false;
                        //enqueue silence
                        lock (buffers)
                        {
                            EnqueueBuffer(0);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError($"VoipCapture threw an exception. Disabling capture...", e);
                capturing = false;
            }
            finally
            {
                Marshal.FreeHGlobal(nativeBuffer);
            }
        }
Ejemplo n.º 34
0
 void Awake()
 {
     if (__instance == null)
     {
         __instance = this;
     }
     logpath = AppPlatform.RuntimeAssetsPath + "/log.txt";
     //清理Log
     if (File.Exists(logpath))
     {
         File.Delete(logpath);
     }
     DontDestroyOnLoad(DebugConsole.Instance);
 }
Ejemplo n.º 35
0
        private VoipCapture(string deviceName) : base(GameMain.Client?.ID ?? 0, true, false)
        {
            Disconnected = false;

            VoipConfig.SetupEncoding();

            //set up capture device
            captureDevice = Alc.CaptureOpenDevice(deviceName, VoipConfig.FREQUENCY, Al.FormatMono16, VoipConfig.BUFFER_SIZE * 5);

            if (captureDevice == IntPtr.Zero)
            {
                DebugConsole.NewMessage("Alc.CaptureOpenDevice attempt 1 failed: error code " + Alc.GetError(IntPtr.Zero).ToString(), Color.Orange);
                //attempt using a smaller buffer size
                captureDevice = Alc.CaptureOpenDevice(deviceName, VoipConfig.FREQUENCY, Al.FormatMono16, VoipConfig.BUFFER_SIZE * 2);
            }

            if (captureDevice == IntPtr.Zero)
            {
                DebugConsole.NewMessage("Alc.CaptureOpenDevice attempt 2 failed: error code " + Alc.GetError(IntPtr.Zero).ToString(), Color.Orange);
                //attempt using the default device
                captureDevice = Alc.CaptureOpenDevice("", VoipConfig.FREQUENCY, Al.FormatMono16, VoipConfig.BUFFER_SIZE * 2);
            }

            if (captureDevice == IntPtr.Zero)
            {
                string errorCode = Alc.GetError(IntPtr.Zero).ToString();
                if (!GUIMessageBox.MessageBoxes.Any(mb => mb.UserData as string == "capturedevicenotfound"))
                {
                    GUI.SettingsMenuOpen = false;
                    new GUIMessageBox(TextManager.Get("Error"),
                        (TextManager.Get("VoipCaptureDeviceNotFound", returnNull: true) ?? "Could not start voice capture, suitable capture device not found.") + " (" + errorCode + ")")
                    {
                        UserData = "capturedevicenotfound"
                    };
                }
                GameAnalyticsManager.AddErrorEventOnce("Alc.CaptureDeviceOpenFailed", GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
                    "Alc.CaptureDeviceOpen(" + deviceName + ") failed. Error code: " + errorCode);
                GameMain.Config.VoiceSetting = GameSettings.VoiceMode.Disabled;
                Instance?.Dispose();
                Instance = null;
                return;
            }

            int alError = Al.GetError();
            int alcError = Alc.GetError(captureDevice);
            if (alcError != Alc.NoError)
            {
                throw new Exception("Failed to open capture device: " + alcError.ToString() + " (ALC)");
            }
            if (alError != Al.NoError)
            {
                throw new Exception("Failed to open capture device: " + alError.ToString() + " (AL)");
            }

            CanDetectDisconnect = Alc.IsExtensionPresent(captureDevice, "ALC_EXT_disconnect");
            alcError = Alc.GetError(captureDevice);
            if (alcError != Alc.NoError)
            {
                throw new Exception("Error determining if disconnect can be detected: " + alcError.ToString());
            }

            Alc.CaptureStart(captureDevice);
            alcError = Alc.GetError(captureDevice);
            if (alcError != Alc.NoError)
            {
                throw new Exception("Failed to start capturing: " + alcError.ToString());
            }

            capturing = true;
            captureThread = new Thread(UpdateCapture)
            {
                IsBackground = true,
                Name = "VoipCapture"
            };
            captureThread.Start();
        }
        public Message(object messageObject, DebugConsole.MessageType messageType) : this(messageObject, messageType, defaultColor)
        {
            switch (messageType)
            {
                case DebugConsole.MessageType.WARNING:
                    this.color = warningColor;
                    break;

                case DebugConsole.MessageType.ERROR:
                    this.color = errorColor;
                    break;

                case DebugConsole.MessageType.SYSTEM:
                    this.color = systemColor;
                    break;

                case DebugConsole.MessageType.INPUT:
                    this.color = inputColor;
                    break;

                case DebugConsole.MessageType.OUTPUT:
                    this.color = outputColor;
                    break;
            }
        }
Ejemplo n.º 37
0
        /// <summary>
        /// Write the events to the outgoing message. The recipient parameter is only needed for ServerEntityEventManager
        /// </summary>
        protected void Write(NetOutgoingMessage msg, List <NetEntityEvent> eventsToSync, out List <NetEntityEvent> sentEvents, Client recipient = null)
        {
            //write into a temporary buffer so we can write the number of events before the actual data
            NetBuffer tempBuffer = new NetBuffer();

            sentEvents = new List <NetEntityEvent>();

            int eventCount = 0;

            foreach (NetEntityEvent e in eventsToSync)
            {
                //write into a temporary buffer so we can write the length before the actual data
                NetBuffer tempEventBuffer = new NetBuffer();
                try
                {
                    WriteEvent(tempEventBuffer, e, recipient);
                }

                catch (Exception exception)
                {
                    DebugConsole.ThrowError("Failed to write an event for the entity \"" + e.Entity + "\"", exception);
                    GameAnalyticsManager.AddErrorEventOnce("NetEntityEventManager.Write:WriteFailed" + e.Entity.ToString(),
                                                           GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
                                                           "Failed to write an event for the entity \"" + e.Entity + "\"\n" + exception.StackTrace);

                    //write an empty event to avoid messing up IDs
                    //(otherwise the clients might read the next event in the message and think its ID
                    //is consecutive to the previous one, even though we skipped over this broken event)
                    tempBuffer.Write(Entity.NullEntityID);
                    tempBuffer.WritePadBits();
                    eventCount++;
                    continue;
                }

                //the length of the data is written as a byte, so the data needs to be less than 255 bytes long
                if (tempEventBuffer.LengthBytes > 255)
                {
                    DebugConsole.ThrowError("Too much data in network event for entity \"" + e.Entity.ToString() + "\" (" + tempEventBuffer.LengthBytes + " bytes");
                    GameAnalyticsManager.AddErrorEventOnce("NetEntityEventManager.Write:TooLong" + e.Entity.ToString(),
                                                           GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
                                                           "Too much data in network event for entity \"" + e.Entity.ToString() + "\" (" + tempEventBuffer.LengthBytes + " bytes");

                    //write an empty event to prevent breaking the event syncing
                    tempBuffer.Write(Entity.NullEntityID);
                    tempBuffer.WritePadBits();
                    eventCount++;
                    continue;
                }

                if (msg.LengthBytes + tempBuffer.LengthBytes + tempEventBuffer.LengthBytes > MaxEventBufferLength)
                {
                    //no more room in this packet
                    break;
                }

                tempBuffer.Write((UInt16)e.Entity.ID);
                tempBuffer.Write((byte)tempEventBuffer.LengthBytes);
                tempBuffer.Write(tempEventBuffer);
                tempBuffer.WritePadBits();
                sentEvents.Add(e);

                eventCount++;
            }

            if (eventCount > 0)
            {
                msg.Write(eventsToSync[0].ID);
                msg.Write((byte)eventCount);
                msg.Write(tempBuffer);
            }
        }
Ejemplo n.º 38
0
        public ItemComponent(Item item, XElement element)
        {
            this.item              = item;
            originalElement        = element;
            name                   = element.Name.ToString();
            SerializableProperties = SerializableProperty.GetProperties(this);
            requiredItems          = new Dictionary <RelatedItem.RelationType, List <RelatedItem> >();
            requiredSkills         = new List <Skill>();

#if CLIENT
            sounds = new Dictionary <ActionType, List <ItemSound> >();
#endif

            SelectKey = InputType.Select;

            try
            {
                string selectKeyStr = element.GetAttributeString("selectkey", "Select");
                selectKeyStr = ToolBox.ConvertInputType(selectKeyStr);
                SelectKey    = (InputType)Enum.Parse(typeof(InputType), selectKeyStr, true);
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Invalid select key in " + element + "!", e);
            }

            PickKey = InputType.Select;

            try
            {
                string pickKeyStr = element.GetAttributeString("pickkey", "Select");
                pickKeyStr = ToolBox.ConvertInputType(pickKeyStr);
                PickKey    = (InputType)Enum.Parse(typeof(InputType), pickKeyStr, true);
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Invalid pick key in " + element + "!", e);
            }

            SerializableProperties = SerializableProperty.DeserializeProperties(this, element);
            ParseMsg();

            foreach (XElement subElement in element.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "requireditem":
                case "requireditems":
                    RelatedItem ri = RelatedItem.Load(subElement, item.Name);
                    if (ri != null)
                    {
                        if (!requiredItems.ContainsKey(ri.Type))
                        {
                            requiredItems.Add(ri.Type, new List <RelatedItem>());
                        }
                        requiredItems[ri.Type].Add(ri);
                    }
                    else
                    {
                        DebugConsole.ThrowError("Error in item config \"" + item.ConfigFile + "\" - component " + GetType().ToString() + " requires an item with no identifiers.");
                    }
                    break;

                case "requiredskill":
                case "requiredskills":
                    if (subElement.Attribute("name") != null)
                    {
                        DebugConsole.ThrowError("Error in item config \"" + item.ConfigFile + "\" - skill requirement in component " + GetType().ToString() + " should use a skill identifier instead of the name of the skill.");
                        continue;
                    }

                    string skillIdentifier = subElement.GetAttributeString("identifier", "");
                    requiredSkills.Add(new Skill(skillIdentifier, subElement.GetAttributeInt("level", 0)));
                    break;

                case "statuseffect":
                    var statusEffect = StatusEffect.Load(subElement, item.Name);

                    if (statusEffectLists == null)
                    {
                        statusEffectLists = new Dictionary <ActionType, List <StatusEffect> >();
                    }

                    List <StatusEffect> effectList;
                    if (!statusEffectLists.TryGetValue(statusEffect.type, out effectList))
                    {
                        effectList = new List <StatusEffect>();
                        statusEffectLists.Add(statusEffect.type, effectList);
                    }

                    effectList.Add(statusEffect);

                    break;

                case "aitarget":
                    AITarget = new AITarget(item, subElement)
                    {
                        Enabled = isActive
                    };
                    break;

                default:
                    if (LoadElemProjSpecific(subElement))
                    {
                        break;
                    }
                    ItemComponent ic = Load(subElement, item, item.ConfigFile, false);
                    if (ic == null)
                    {
                        break;
                    }

                    ic.Parent = this;
                    item.AddComponent(ic);
                    break;
                }
            }
        }
Ejemplo n.º 39
0
        public Connection(XElement element, ConnectionPanel connectionPanel)
        {
#if CLIENT
            if (connector == null)
            {
                connector                 = GUI.Style.GetComponentStyle("ConnectionPanelConnector").Sprites[GUIComponent.ComponentState.None][0].Sprite;
                wireVertical              = GUI.Style.GetComponentStyle("ConnectionPanelWire").Sprites[GUIComponent.ComponentState.None][0].Sprite;
                connectionSprite          = GUI.Style.GetComponentStyle("ConnectionPanelConnection").Sprites[GUIComponent.ComponentState.None][0].Sprite;
                connectionSpriteHighlight = GUI.Style.GetComponentStyle("ConnectionPanelConnection").Sprites[GUIComponent.ComponentState.Hover][0].Sprite;
                screwSprites              = GUI.Style.GetComponentStyle("ConnectionPanelScrew").Sprites[GUIComponent.ComponentState.None].Select(s => s.Sprite).ToList();
            }
#endif
            ConnectionPanel = connectionPanel;
            item            = connectionPanel.Item;

            wires = new Wire[MaxLinked];

            IsOutput = element.Name.ToString() == "output";
            Name     = element.GetAttributeString("name", IsOutput ? "output" : "input");

            string displayNameTag = "", fallbackTag = "";
            //if displayname is not present, attempt to find it from the prefab
            if (element.Attribute("displayname") == null)
            {
                foreach (XElement subElement in item.Prefab.ConfigElement.Elements())
                {
                    if (subElement.Name.ToString().ToLowerInvariant() != "connectionpanel")
                    {
                        continue;
                    }

                    foreach (XElement connectionElement in subElement.Elements())
                    {
                        if (connectionElement.Name.ToString() != element.Name.ToString())
                        {
                            continue;
                        }

                        string prefabConnectionName = element.GetAttributeString("name", IsOutput ? "output" : "input");
                        if (prefabConnectionName == Name)
                        {
                            displayNameTag = connectionElement.GetAttributeString("displayname", "");
                            fallbackTag    = connectionElement.GetAttributeString("fallbackdisplayname", "");
                        }
                    }
                }
            }
            else
            {
                displayNameTag = element.GetAttributeString("displayname", "");
                fallbackTag    = element.GetAttributeString("fallbackdisplayname", null);
            }

            if (!string.IsNullOrEmpty(displayNameTag))
            {
                //extract the tag parts in case the tags contains variables
                string tagWithoutVariables         = displayNameTag?.Split('~')?.FirstOrDefault();
                string fallbackTagWithoutVariables = fallbackTag?.Split('~')?.FirstOrDefault();
                //use displayNameTag if found, otherwise fallBack
                if (TextManager.ContainsTag(tagWithoutVariables))
                {
                    DisplayName = TextManager.GetServerMessage(displayNameTag);
                }
                else if (TextManager.ContainsTag(fallbackTagWithoutVariables))
                {
                    DisplayName = TextManager.GetServerMessage(fallbackTag);
                }
            }

            if (string.IsNullOrEmpty(DisplayName))
            {
#if DEBUG
                DebugConsole.ThrowError("Missing display name in connection " + item.Name + ": " + Name);
#endif
                DisplayName = Name;
            }

            IsPower = Name == "power_in" || Name == "power" || Name == "power_out";

            Effects = new List <StatusEffect>();

            wireId = new ushort[MaxLinked];

            foreach (XElement subElement in element.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "link":
                    int index = -1;
                    for (int i = 0; i < MaxLinked; i++)
                    {
                        if (wireId[i] < 1)
                        {
                            index = i;
                        }
                    }
                    if (index == -1)
                    {
                        break;
                    }

                    int id = subElement.GetAttributeInt("w", 0);
                    if (id < 0)
                    {
                        id = 0;
                    }
                    wireId[index] = (ushort)id;

                    break;

                case "statuseffect":
                    Effects.Add(StatusEffect.Load(subElement, item.Name + ", connection " + Name));
                    break;
                }
            }
        }
Ejemplo n.º 40
0
        /// <summary>
        /// Write the events to the outgoing message. The recipient parameter is only needed for ServerEntityEventManager
        /// </summary>
        protected void Write(NetOutgoingMessage msg, List <NetEntityEvent> eventsToSync, Client recipient = null)
        {
            //write into a temporary buffer so we can write the number of events before the actual data
            NetBuffer tempBuffer = new NetBuffer();

            int eventCount = 0;

            foreach (NetEntityEvent e in eventsToSync)
            {
                //write into a temporary buffer so we can write the length before the actual data
                NetBuffer tempEventBuffer = new NetBuffer();
                try
                {
                    WriteEvent(tempEventBuffer, e, recipient);
                }

                catch (Exception exception)
                {
                    DebugConsole.ThrowError("Failed to write an event for the entity \"" + e.Entity + "\"", exception);
                    GameAnalyticsManager.AddErrorEventOnce("NetEntityEventManager.Write:WriteFailed" + e.Entity.ToString(),
                                                           GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
                                                           "Failed to write an event for the entity \"" + e.Entity + "\"\n" + exception.StackTrace);

                    //write an empty event to avoid messing up IDs
                    //(otherwise the clients might read the next event in the message and think its ID
                    //is consecutive to the previous one, even though we skipped over this broken event)
                    tempBuffer.Write((UInt16)0);
                    tempBuffer.WritePadBits();
                    eventCount++;
                    continue;
                }

                if (msg.LengthBytes + tempBuffer.LengthBytes + tempEventBuffer.LengthBytes > NetPeerConfiguration.kDefaultMTU - 20)
                {
                    //no more room in this packet
                    break;
                }

                if (tempEventBuffer.LengthBytes > 255)
                {
                    DebugConsole.ThrowError("Too much data in network event for entity \"" + e.Entity.ToString() + "\" (" + tempEventBuffer.LengthBytes + " bytes");
                    GameAnalyticsManager.AddErrorEventOnce("NetEntityEventManager.Write:TooLong" + e.Entity.ToString(),
                                                           GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
                                                           "Too much data in network event for entity \"" + e.Entity.ToString() + "\" (" + tempEventBuffer.LengthBytes + " bytes");

                    //write an empty event to prevent breaking the event syncing
                    tempBuffer.Write((UInt16)0);
                    tempBuffer.WritePadBits();
                    eventCount++;
                    continue;
                }
                //the ID has been taken by another entity (the original entity has been removed) -> write an empty event

                /*else if (Entity.FindEntityByID(e.Entity.ID) != e.Entity || e.Entity.IdFreed)
                 * {
                 *  //technically the clients don't have any use for these, but removing events and shifting the IDs of all
                 *  //consecutive ones is so error-prone that I think this is a safer option
                 *  tempBuffer.Write((UInt16)0);
                 *  tempBuffer.WritePadBits();
                 * }*/
                else
                {
                    tempBuffer.Write((UInt16)e.Entity.ID);
                    tempBuffer.Write((byte)tempEventBuffer.LengthBytes);
                    tempBuffer.Write(tempEventBuffer);
                    tempBuffer.WritePadBits();
                }

                eventCount++;
            }

            if (eventCount > 0)
            {
                msg.Write(eventsToSync[0].ID);
                msg.Write((byte)eventCount);
                msg.Write(tempBuffer);
            }
        }
Ejemplo n.º 41
0
        private void Form1_Load(object sender, EventArgs e)
        {
            GeneralDebugLog = "LOG START";
            NetworkDebugLog = "LOG START";
            ApplicationDebugLog = "LOG START";
            ThreadDebugLog = "LOG START";
            CommandDebugLog = "LOG START";
            ServiceDebugLog = "LOG START";

            Debug.WriteLine("GUI started on Thread : " + AppDomain.GetCurrentThreadId());
            Thread thread = Thread.CurrentThread;
            thread.Name = "GUI Thread";

            //SetBlockerVisiable(true);

            debugConsole = new DebugConsole(this);
            debugConsole.Show();

            commandQue = new CommandQue(this);

            //Worker workerObject = new Worker();
            // Thread workerThread = new Thread(workerObject.DoWork);
            //workerThread.Start();

            //TODO: Move object declarations out of method so they can be accessed outside this method

            commandParser = new CommandParser(this);
            commandThread = new Thread(commandParser.ParserMain);
            commandThread.Name = "CommandThread";
            commandThread.Start();

            applicationManager = new ApplicationManager(this);
            applicationThread = new Thread(applicationManager.ApplicationMain);
            applicationThread.Name = "AppManagerThread";
            //applicationThread.Start();

            networkManager= new NetworkManager(this);
            networkThread = new Thread(networkManager.NetworkMain);
            networkThread.Name = "netManager";
            //networkThread.Start();

            serviceManager = new ServiceManager(this);
            serviceThread = new Thread(serviceManager.ServiceMain);
            serviceThread.Name = "ServiceThread";
            serviceThread.Start();

            keyBlockForm = new KeyBlockForm();
        }
Ejemplo n.º 42
0
    void Awake()
    {
        _instance = this;

        _messages = new List<Message>();
        _windowRect = new Rect((Screen.width/2)-300, (Screen.height/2)-(450f/2), 600.0F, 450.0F );
        _cmdTable = new Hashtable();
        _watchVarTable = new Hashtable();
        _displayString = new StringBuilder();

        /*_messages.Add( new Message( " DebugConsole version " + VERSION.ToString( "F2" ), MessageTypes.System ) );
        _messages.Add( new Message( " Copyright 2008-2010 Jeremy Hollingsworth ", MessageTypes.System ) );
        _messages.Add( new Message( " Ennanzus-Interactive.com ", MessageTypes.System ) );
        _messages.Add( new Message( " " ) );*/

        this.RegisterCommandCallback( "close", CMDClose );
        this.RegisterCommandCallback( "clear", CMDClear );
    }
Ejemplo n.º 43
0
        // TODO: Consider using generics, interfaces, or inheritance instead of reflection -> would be easier to debug when something changes/goes wrong.
        // For example, currently we can edit the constructors but they will fail in runtime because the parameters are not changed here.
        // It's also painful to find where the constructors are used, because the references exist only at runtime.
        public static ItemComponent Load(XElement element, Item item, string file, bool errorMessages = true)
        {
            Type   t;
            string type = element.Name.ToString().ToLowerInvariant();

            try
            {
                // Get the type of a specified class.
                t = Type.GetType("Barotrauma.Items.Components." + type + "", false, true);
                if (t == null)
                {
                    if (errorMessages)
                    {
                        DebugConsole.ThrowError("Could not find the component \"" + type + "\" (" + file + ")");
                    }
                    return(null);
                }
            }
            catch (Exception e)
            {
                if (errorMessages)
                {
                    DebugConsole.ThrowError("Could not find the component \"" + type + "\" (" + file + ")", e);
                }
                return(null);
            }

            ConstructorInfo constructor;

            try
            {
                if (t != typeof(ItemComponent) && !t.IsSubclassOf(typeof(ItemComponent)))
                {
                    return(null);
                }
                constructor = t.GetConstructor(new Type[] { typeof(Item), typeof(XElement) });
                if (constructor == null)
                {
                    DebugConsole.ThrowError("Could not find the constructor of the component \"" + type + "\" (" + file + ")");
                    return(null);
                }
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Could not find the constructor of the component \"" + type + "\" (" + file + ")", e);
                return(null);
            }
            ItemComponent ic = null;

            try
            {
                object[] lobject   = new object[] { item, element };
                object   component = constructor.Invoke(lobject);
                ic      = (ItemComponent)component;
                ic.name = element.Name.ToString();
            }
            catch (TargetInvocationException e)
            {
                DebugConsole.ThrowError("Error while loading entity of the type " + t + ".", e.InnerException);
                GameAnalyticsManager.AddErrorEventOnce("ItemComponent.Load:TargetInvocationException" + item.Name + element.Name,
                                                       GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
                                                       "Error while loading entity of the type " + t + " (" + e.InnerException + ")\n" + Environment.StackTrace);
            }

            return(ic);
        }
Ejemplo n.º 44
0
        public DebugDescriptor(DebugConsole type)
        {
            this.type = type;
            string name="";

            switch(type)
            {
                case DebugConsole.Ext:
                    name = "debug";
                    break;
                case DebugConsole.Firebug:
                    name = "firebug-lite";
                    break;
            }

            ResourceManager rm = ResourceManager.GetInstance(HttpContext.Current);
            this.css = rm.GetWebResourceUrl(ResourceManager.ASSEMBLYSLUG + string.Concat(".ux.extensions.debug.",type.ToString().ToLowerInvariant(),".css.",name,"-embedded.css"));
            this.script = rm.GetWebResourceUrl(ResourceManager.ASSEMBLYSLUG + string.Concat(".ux.extensions.debug.", type.ToString().ToLowerInvariant(), ".", name, ".js"));
        }
Ejemplo n.º 45
0
        private void ClientInitRequest(NetIncomingMessage inc)
        {
            DebugConsole.Log("Received client init request");
            if (ConnectedClients.Find(c => c.Connection == inc.SenderConnection) != null)
            {
                //this client was already authenticated
                //another init request means they didn't get any update packets yet
                DebugConsole.Log("Client already connected, ignoring...");
                return;
            }

            UnauthenticatedClient unauthClient = unauthenticatedClients.Find(uc => uc.Connection == inc.SenderConnection);

            if (unauthClient == null)
            {
                //client did not ask for nonce first, can't authorize
                inc.SenderConnection.Disconnect(DisconnectReason.AuthenticationRequired.ToString());
                if (unauthClient.SteamID > 0)
                {
                    Steam.SteamManager.StopAuthSession(unauthClient.SteamID);
                }
                return;
            }

            if (serverSettings.HasPassword && inc.SenderConnection != OwnerConnection)
            {
                //decrypt message and compare password
                string clPw = inc.ReadString();
                if (!serverSettings.IsPasswordCorrect(clPw, unauthClient.Nonce))
                {
                    unauthClient.FailedAttempts++;
                    if (unauthClient.FailedAttempts > 3)
                    {
                        //disconnect and ban after too many failed attempts
                        serverSettings.BanList.BanPlayer("Unnamed", unauthClient.Connection.RemoteEndPoint.Address, "DisconnectMessage.TooManyFailedLogins", duration: null);
                        DisconnectUnauthClient(inc, unauthClient, DisconnectReason.TooManyFailedLogins, "");

                        Log(inc.SenderConnection.RemoteEndPoint.Address.ToString() + " has been banned from the server (too many wrong passwords)", ServerLog.MessageType.Error);
                        DebugConsole.NewMessage(inc.SenderConnection.RemoteEndPoint.Address.ToString() + " has been banned from the server (too many wrong passwords)", Color.Red);
                        return;
                    }
                    else
                    {
                        //not disconnecting the player here, because they'll still use the same connection and nonce if they try logging in again
                        NetOutgoingMessage reject = server.CreateMessage();
                        reject.Write((byte)ServerPacketHeader.AUTH_FAILURE);
                        reject.Write("Wrong password! You have " + Convert.ToString(4 - unauthClient.FailedAttempts) + " more attempts before you're banned from the server.");
                        Log(inc.SenderConnection.RemoteEndPoint.Address.ToString() + " failed to join the server (incorrect password)", ServerLog.MessageType.Error);
                        DebugConsole.NewMessage(inc.SenderConnection.RemoteEndPoint.Address.ToString() + " failed to join the server (incorrect password)", Color.Red);
                        CompressOutgoingMessage(reject);
                        server.SendMessage(reject, unauthClient.Connection, NetDeliveryMethod.Unreliable);
                        unauthClient.AuthTimer = 10.0f;
                        return;
                    }
                }
            }
            string clVersion = inc.ReadString();

            UInt16        contentPackageCount  = inc.ReadUInt16();
            List <string> contentPackageNames  = new List <string>();
            List <string> contentPackageHashes = new List <string>();

            for (int i = 0; i < contentPackageCount; i++)
            {
                string packageName = inc.ReadString();
                string packageHash = inc.ReadString();
                contentPackageNames.Add(packageName);
                contentPackageHashes.Add(packageHash);
                if (contentPackageCount == 0)
                {
                    DebugConsole.Log("Client is using content package " +
                                     (packageName ?? "null") + " (" + (packageHash ?? "null" + ")"));
                }
            }

            if (contentPackageCount == 0)
            {
                DebugConsole.Log("Client did not list any content packages.");
            }

            string clName = Client.SanitizeName(inc.ReadString());

            if (string.IsNullOrWhiteSpace(clName))
            {
                DisconnectUnauthClient(inc, unauthClient, DisconnectReason.NoName, "");

                Log(inc.SenderConnection.RemoteEndPoint.Address.ToString() + " couldn't join the server (no name given)", ServerLog.MessageType.Error);
                DebugConsole.NewMessage(inc.SenderConnection.RemoteEndPoint.Address.ToString() + " couldn't join the server (no name given)", Color.Red);
                return;
            }

            bool?isCompatibleVersion = IsCompatible(clVersion, GameMain.Version.ToString());

            if (isCompatibleVersion.HasValue && !isCompatibleVersion.Value)
            {
                DisconnectUnauthClient(inc, unauthClient, DisconnectReason.InvalidVersion,
                                       $"DisconnectMessage.InvalidVersion~[version]={GameMain.Version.ToString()}~[clientversion]={clVersion}");

                Log(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (incompatible game version)", ServerLog.MessageType.Error);
                DebugConsole.NewMessage(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (incompatible game version)", Color.Red);
                return;
            }

            //check if the client is missing any of the content packages the server requires
            List <ContentPackage> missingPackages = new List <ContentPackage>();

            foreach (ContentPackage contentPackage in GameMain.SelectedPackages)
            {
                if (!contentPackage.HasMultiplayerIncompatibleContent)
                {
                    continue;
                }
                bool packageFound = false;
                for (int i = 0; i < contentPackageCount; i++)
                {
                    if (contentPackageNames[i] == contentPackage.Name && contentPackageHashes[i] == contentPackage.MD5hash.Hash)
                    {
                        packageFound = true;
                        break;
                    }
                }
                if (!packageFound)
                {
                    missingPackages.Add(contentPackage);
                }
            }

            if (missingPackages.Count == 1)
            {
                DisconnectUnauthClient(inc, unauthClient, DisconnectReason.MissingContentPackage, $"DisconnectMessage.MissingContentPackage~[missingcontentpackage]={GetPackageStr(missingPackages[0])}");
                Log(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (missing content package " + GetPackageStr(missingPackages[0]) + ")", ServerLog.MessageType.Error);
                return;
            }
            else if (missingPackages.Count > 1)
            {
                List <string> packageStrs = new List <string>();
                missingPackages.ForEach(cp => packageStrs.Add(GetPackageStr(cp)));
                DisconnectUnauthClient(inc, unauthClient, DisconnectReason.MissingContentPackage, $"DisconnectMessage.MissingContentPackages~[missingcontentpackages]={string.Join(", ", packageStrs)}");
                Log(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (missing content packages " + string.Join(", ", packageStrs) + ")", ServerLog.MessageType.Error);
                return;
            }

            string GetPackageStr(ContentPackage contentPackage)
            {
                return("\"" + contentPackage.Name + "\" (hash " + contentPackage.MD5hash.ShortHash + ")");
            }

            //check if the client is using any contentpackages that are not compatible with the server
            List <Pair <string, string> > incompatiblePackages = new List <Pair <string, string> >();

            for (int i = 0; i < contentPackageNames.Count; i++)
            {
                if (!GameMain.Config.SelectedContentPackages.Any(cp => cp.Name == contentPackageNames[i] && cp.MD5hash.Hash == contentPackageHashes[i]))
                {
                    incompatiblePackages.Add(new Pair <string, string>(contentPackageNames[i], contentPackageHashes[i]));
                }
            }

            if (incompatiblePackages.Count == 1)
            {
                DisconnectUnauthClient(inc, unauthClient, DisconnectReason.IncompatibleContentPackage,
                                       $"DisconnectMessage.IncompatibleContentPackage~[incompatiblecontentpackage]={GetPackageStr2(incompatiblePackages[0])}");
                Log(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (incompatible content package " + GetPackageStr2(incompatiblePackages[0]) + ")", ServerLog.MessageType.Error);
                return;
            }
            else if (incompatiblePackages.Count > 1)
            {
                List <string> packageStrs = new List <string>();
                incompatiblePackages.ForEach(cp => packageStrs.Add(GetPackageStr2(cp)));
                DisconnectUnauthClient(inc, unauthClient, DisconnectReason.IncompatibleContentPackage,
                                       $"DisconnectMessage.IncompatibleContentPackages~[incompatiblecontentpackages]={string.Join(", ", packageStrs)}");
                Log(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (incompatible content packages " + string.Join(", ", packageStrs) + ")", ServerLog.MessageType.Error);
                return;
            }

            string GetPackageStr2(Pair <string, string> nameAndHash)
            {
                return("\"" + nameAndHash.First + "\" (hash " + Md5Hash.GetShortHash(nameAndHash.Second) + ")");
            }

            if (inc.SenderConnection != OwnerConnection && !serverSettings.Whitelist.IsWhiteListed(clName, inc.SenderConnection.RemoteEndPoint.Address))
            {
                DisconnectUnauthClient(inc, unauthClient, DisconnectReason.NotOnWhitelist, "");
                Log(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (not in whitelist)", ServerLog.MessageType.Error);
                DebugConsole.NewMessage(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (not in whitelist)", Color.Red);
                return;
            }
            if (!Client.IsValidName(clName, this))
            {
                DisconnectUnauthClient(inc, unauthClient, DisconnectReason.InvalidName, "");
                Log(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (invalid name)", ServerLog.MessageType.Error);
                DebugConsole.NewMessage(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (invalid name)", Color.Red);
                return;
            }
            if (inc.SenderConnection != OwnerConnection && Homoglyphs.Compare(clName.ToLower(), Name.ToLower()))
            {
                DisconnectUnauthClient(inc, unauthClient, DisconnectReason.NameTaken, "");
                Log(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (name taken by the server)", ServerLog.MessageType.Error);
                DebugConsole.NewMessage(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (name taken by the server)", Color.Red);
                return;
            }
            Client nameTaken = ConnectedClients.Find(c => Homoglyphs.Compare(c.Name.ToLower(), clName.ToLower()));

            if (nameTaken != null)
            {
                if (nameTaken.Connection.RemoteEndPoint.Address.ToString() == inc.SenderEndPoint.Address.ToString())
                {
                    //both name and IP address match, replace this player's connection
                    nameTaken.Connection.Disconnect(DisconnectReason.SessionTaken.ToString());
                    nameTaken.Connection = unauthClient.Connection;
                    nameTaken.InitClientSync(); //reinitialize sync ids because this is a new connection
                    unauthenticatedClients.Remove(unauthClient);
                    unauthClient = null;
                    return;
                }
                else
                {
                    //can't authorize this client
                    DisconnectUnauthClient(inc, unauthClient, DisconnectReason.NameTaken, "");
                    Log(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (name already taken)", ServerLog.MessageType.Error);
                    DebugConsole.NewMessage(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (name already taken)", Color.Red);
                    return;
                }
            }

            //new client
            Client newClient = new Client(clName, GetNewClientID());

            newClient.InitClientSync();
            newClient.Connection = unauthClient.Connection;
            newClient.SteamID    = unauthClient.SteamID;
            unauthenticatedClients.Remove(unauthClient);
            unauthClient = null;
            ConnectedClients.Add(newClient);

            var previousPlayer = previousPlayers.Find(p => p.MatchesClient(newClient));

            if (previousPlayer != null)
            {
                newClient.Karma = previousPlayer.Karma;
                foreach (Client c in previousPlayer.KickVoters)
                {
                    if (!connectedClients.Contains(c))
                    {
                        continue;
                    }
                    newClient.AddKickVote(c);
                }
            }

            LastClientListUpdateID++;

            if (newClient.Connection == OwnerConnection)
            {
                newClient.GivePermission(ClientPermissions.All);
                newClient.PermittedConsoleCommands.AddRange(DebugConsole.Commands);

                GameMain.Server.UpdateClientPermissions(newClient);
                GameMain.Server.SendConsoleMessage("Granted all permissions to " + newClient.Name + ".", newClient);
            }

            GameMain.Server.SendChatMessage($"ServerMessage.JoinedServer~[client]={clName}", ChatMessageType.Server, null);
            serverSettings.ServerDetailsChanged = true;

            if (previousPlayer != null && previousPlayer.Name != newClient.Name)
            {
                GameMain.Server.SendChatMessage($"ServerMessage.PreviousClientName~[client]={clName}~[previousname]={previousPlayer.Name}", ChatMessageType.Server, null);
                previousPlayer.Name = newClient.Name;
            }

            var savedPermissions = serverSettings.ClientPermissions.Find(cp =>
                                                                         cp.SteamID > 0 ?
                                                                         cp.SteamID == newClient.SteamID :
                                                                         newClient.IPMatches(cp.IP));

            if (savedPermissions != null)
            {
                newClient.SetPermissions(savedPermissions.Permissions, savedPermissions.PermittedCommands);
            }
            else
            {
                var defaultPerms = PermissionPreset.List.Find(p => p.Name == "None");
                if (defaultPerms != null)
                {
                    newClient.SetPermissions(defaultPerms.Permissions, defaultPerms.PermittedCommands);
                }
                else
                {
                    newClient.SetPermissions(ClientPermissions.None, new List <DebugConsole.Command>());
                }
            }
        }
Ejemplo n.º 46
0
 /// <summary>
 /// Add required resources and activate console
 /// </summary>
 /// <param name="module">Console type</param>
 /// <param name="callback">callback which fires after console activating</param>
 public static void Activate(DebugConsole module, JFunction callback)
 {
     
     Debug debug = new Debug();
     string scriptUrl = debug.ResourceManager.GetWebResourceUrl(ResourceManager.ASSEMBLYSLUG + ".ux.extensions.debug.Debug.js");
     string activate = "function(){{Ext.net.Debug.activate({0}{1});}}".FormatWith(new DebugDescriptor(module).Serialize(), callback == null ? "" : ","+callback.ToScript());
     debug.AddScript("Ext.net.ResourceMgr.load({{ url: {0}, callback: {1} }});", JSON.Serialize(scriptUrl), activate);
 }
Ejemplo n.º 47
0
        private void LinkHullsToGaps()
        {
            if (gap == null || hulls == null || hulls[0] == null || hulls[1] == null)
            {
#if DEBUG
                DebugConsole.ThrowError("Failed to link dockingport hulls to gap");
#endif
                return;
            }

            gap.linkedTo.Clear();

            if (IsHorizontal)
            {
                if (hulls[0].WorldRect.X < hulls[1].WorldRect.X)
                {
                    gap.linkedTo.Add(hulls[0]);
                    gap.linkedTo.Add(hulls[1]);
                }
                else
                {
                    gap.linkedTo.Add(hulls[1]);
                    gap.linkedTo.Add(hulls[0]);
                }
            }
            else
            {
                if (hulls[0].WorldRect.Y > hulls[1].WorldRect.Y)
                {
                    gap.linkedTo.Add(hulls[0]);
                    gap.linkedTo.Add(hulls[1]);
                }
                else
                {
                    gap.linkedTo.Add(hulls[1]);
                    gap.linkedTo.Add(hulls[0]);
                }
            }

            for (int i = 0; i < 2; i++)
            {
                Gap doorGap = i == 0 ? Door?.LinkedGap : DockingTarget?.Door?.LinkedGap;
                if (doorGap == null)
                {
                    continue;
                }
                doorGap.DisableHullRechecks = true;
                if (doorGap.linkedTo.Count >= 2)
                {
                    continue;
                }

                if (IsHorizontal)
                {
                    if (item.WorldPosition.X < DockingTarget.item.WorldPosition.X)
                    {
                        if (!doorGap.linkedTo.Contains(hulls[0]))
                        {
                            doorGap.linkedTo.Add(hulls[0]);
                        }
                    }
                    else
                    {
                        if (!doorGap.linkedTo.Contains(hulls[1]))
                        {
                            doorGap.linkedTo.Add(hulls[1]);
                        }
                    }
                    //make sure the left hull is linked to the gap first (gap logic assumes that the first hull is the one to the left)
                    if (doorGap.linkedTo.Count > 1 && doorGap.linkedTo[0].Rect.X > doorGap.linkedTo[1].Rect.X)
                    {
                        var temp = doorGap.linkedTo[0];
                        doorGap.linkedTo[0] = doorGap.linkedTo[1];
                        doorGap.linkedTo[1] = temp;
                    }
                }
                else
                {
                    if (item.WorldPosition.Y < DockingTarget.item.WorldPosition.Y)
                    {
                        if (!doorGap.linkedTo.Contains(hulls[0]))
                        {
                            doorGap.linkedTo.Add(hulls[0]);
                        }
                    }
                    else
                    {
                        if (!doorGap.linkedTo.Contains(hulls[1]))
                        {
                            doorGap.linkedTo.Add(hulls[1]);
                        }
                    }
                    //make sure the upper hull is linked to the gap first (gap logic assumes that the first hull is above the second one)
                    if (doorGap.linkedTo.Count > 1 && doorGap.linkedTo[0].Rect.Y < doorGap.linkedTo[1].Rect.Y)
                    {
                        var temp = doorGap.linkedTo[0];
                        doorGap.linkedTo[0] = doorGap.linkedTo[1];
                        doorGap.linkedTo[1] = temp;
                    }
                }
            }
        }
Ejemplo n.º 48
0
        private List <Vector2> FindRaycastHits()
        {
            if (!CastShadows)
            {
                return(null);
            }
            if (Range < 1.0f || Color.A < 0.01f)
            {
                return(null);
            }

            Vector2 drawPos = position;

            if (ParentSub != null)
            {
                drawPos += ParentSub.DrawPosition;
            }

            var hulls = new List <ConvexHull>();

            foreach (ConvexHullList chList in hullsInRange)
            {
                foreach (ConvexHull hull in chList.List)
                {
                    if (!chList.IsHidden.Contains(hull))
                    {
                        hulls.Add(hull);
                    }
                }
                foreach (ConvexHull hull in chList.List)
                {
                    chList.IsHidden.Add(hull);
                }
            }

            float bounds = Range * 2;
            //find convexhull segments that are close enough and facing towards the light source
            List <Segment>      visibleSegments = new List <Segment>();
            List <SegmentPoint> points          = new List <SegmentPoint>();

            foreach (ConvexHull hull in hulls)
            {
                hull.RefreshWorldPositions();
                hull.GetVisibleSegments(drawPos, visibleSegments, ignoreEdges: false);
            }

            //Generate new points at the intersections between segments
            //This is necessary for the light volume to generate properly on some subs
            for (int i = 0; i < visibleSegments.Count; i++)
            {
                Vector2 p1a = visibleSegments[i].Start.WorldPos;
                Vector2 p1b = visibleSegments[i].End.WorldPos;

                for (int j = i + 1; j < visibleSegments.Count; j++)
                {
                    //ignore intersections between parallel axis-aligned segments
                    if (visibleSegments[i].IsAxisAligned && visibleSegments[j].IsAxisAligned &&
                        visibleSegments[i].IsHorizontal == visibleSegments[j].IsHorizontal)
                    {
                        continue;
                    }

                    Vector2 p2a = visibleSegments[j].Start.WorldPos;
                    Vector2 p2b = visibleSegments[j].End.WorldPos;

                    if (Vector2.DistanceSquared(p1a, p2a) < 25.0f ||
                        Vector2.DistanceSquared(p1a, p2b) < 25.0f ||
                        Vector2.DistanceSquared(p1b, p2a) < 25.0f ||
                        Vector2.DistanceSquared(p1b, p2b) < 25.0f)
                    {
                        continue;
                    }

                    bool    intersects;
                    Vector2 intersection = Vector2.Zero;
                    if (visibleSegments[i].IsAxisAligned)
                    {
                        intersects = MathUtils.GetAxisAlignedLineIntersection(p2a, p2b, p1a, p1b, visibleSegments[i].IsHorizontal, out intersection);
                    }
                    else if (visibleSegments[j].IsAxisAligned)
                    {
                        intersects = MathUtils.GetAxisAlignedLineIntersection(p1a, p1b, p2a, p2b, visibleSegments[j].IsHorizontal, out intersection);
                    }
                    else
                    {
                        intersects = MathUtils.GetLineIntersection(p1a, p1b, p2a, p2b, out intersection);
                    }

                    if (intersects)
                    {
                        SegmentPoint start = visibleSegments[i].Start;
                        SegmentPoint end   = visibleSegments[i].End;
                        SegmentPoint mid   = new SegmentPoint(intersection, null);
                        if (visibleSegments[i].ConvexHull?.ParentEntity?.Submarine != null)
                        {
                            mid.Pos -= visibleSegments[i].ConvexHull.ParentEntity.Submarine.DrawPosition;
                        }

                        if (Vector2.DistanceSquared(start.WorldPos, mid.WorldPos) < 25.0f ||
                            Vector2.DistanceSquared(end.WorldPos, mid.WorldPos) < 25.0f)
                        {
                            continue;
                        }

                        Segment seg1 = new Segment(start, mid, visibleSegments[i].ConvexHull)
                        {
                            IsHorizontal = visibleSegments[i].IsHorizontal,
                        };

                        Segment seg2 = new Segment(mid, end, visibleSegments[i].ConvexHull)
                        {
                            IsHorizontal = visibleSegments[i].IsHorizontal
                        };
                        visibleSegments[i] = seg1;
                        visibleSegments.Insert(i + 1, seg2);
                        i--;
                        break;
                    }
                }
            }

            foreach (Segment s in visibleSegments)
            {
                points.Add(s.Start);
                points.Add(s.End);
                if (Math.Abs(s.Start.WorldPos.X - drawPos.X) > bounds)
                {
                    bounds = Math.Abs(s.Start.WorldPos.X - drawPos.X);
                }
                if (Math.Abs(s.Start.WorldPos.Y - drawPos.Y) > bounds)
                {
                    bounds = Math.Abs(s.Start.WorldPos.Y - drawPos.Y);
                }
                if (Math.Abs(s.End.WorldPos.X - drawPos.X) > bounds)
                {
                    bounds = Math.Abs(s.End.WorldPos.X - drawPos.X);
                }
                if (Math.Abs(s.End.WorldPos.Y - drawPos.Y) > bounds)
                {
                    bounds = Math.Abs(s.End.WorldPos.Y - drawPos.Y);
                }
            }

            //add a square-shaped boundary to make sure we've got something to construct the triangles from
            //even if there aren't enough hull segments around the light source

            //(might be more effective to calculate if we actually need these extra points)
            var boundaryCorners = new List <SegmentPoint> {
                new SegmentPoint(new Vector2(drawPos.X + bounds, drawPos.Y + bounds), null),
                new SegmentPoint(new Vector2(drawPos.X + bounds, drawPos.Y - bounds), null),
                new SegmentPoint(new Vector2(drawPos.X - bounds, drawPos.Y - bounds), null),
                new SegmentPoint(new Vector2(drawPos.X - bounds, drawPos.Y + bounds), null)
            };

            points.AddRange(boundaryCorners);

            for (int i = 0; i < 4; i++)
            {
                visibleSegments.Add(new Segment(boundaryCorners[i], boundaryCorners[(i + 1) % 4], null));
            }

            var compareCCW = new CompareSegmentPointCW(drawPos);

            try
            {
                points.Sort(compareCCW);
            }
            catch (Exception e)
            {
                StringBuilder sb = new StringBuilder("Constructing light volumes failed! Light pos: " + drawPos + ", Hull verts:\n");
                foreach (SegmentPoint sp in points)
                {
                    sb.AppendLine(sp.Pos.ToString());
                }
                DebugConsole.ThrowError(sb.ToString(), e);
            }

            List <Vector2> output = new List <Vector2>();

            //List<Pair<int, Vector2>> preOutput = new List<Pair<int, Vector2>>();

            //remove points that are very close to each other
            for (int i = 0; i < points.Count - 1; i++)
            {
                if (Math.Abs(points[i].WorldPos.X - points[i + 1].WorldPos.X) < 6 &&
                    Math.Abs(points[i].WorldPos.Y - points[i + 1].WorldPos.Y) < 6)
                {
                    points.RemoveAt(i + 1);
                    i--;
                }
            }

            foreach (SegmentPoint p in points)
            {
                Vector2 dir       = Vector2.Normalize(p.WorldPos - drawPos);
                Vector2 dirNormal = new Vector2(-dir.Y, dir.X) * 3;

                //do two slightly offset raycasts to hit the segment itself and whatever's behind it
                Pair <int, Vector2> intersection1 = RayCast(drawPos, drawPos + dir * bounds * 2 - dirNormal, visibleSegments);
                Pair <int, Vector2> intersection2 = RayCast(drawPos, drawPos + dir * bounds * 2 + dirNormal, visibleSegments);

                if (intersection1.First < 0)
                {
                    return(new List <Vector2>());
                }
                if (intersection2.First < 0)
                {
                    return(new List <Vector2>());
                }
                Segment seg1 = visibleSegments[intersection1.First];
                Segment seg2 = visibleSegments[intersection2.First];

                bool isPoint1 = MathUtils.LineToPointDistance(seg1.Start.WorldPos, seg1.End.WorldPos, p.WorldPos) < 5.0f;
                bool isPoint2 = MathUtils.LineToPointDistance(seg2.Start.WorldPos, seg2.End.WorldPos, p.WorldPos) < 5.0f;

                if (isPoint1 && isPoint2)
                {
                    //hit at the current segmentpoint -> place the segmentpoint into the list
                    output.Add(p.WorldPos);

                    foreach (ConvexHullList hullList in hullsInRange)
                    {
                        hullList.IsHidden.Remove(p.ConvexHull);
                        hullList.IsHidden.Remove(seg1.ConvexHull);
                        hullList.IsHidden.Remove(seg2.ConvexHull);
                    }
                }
                else if (intersection1.First != intersection2.First)
                {
                    //the raycasts landed on different segments
                    //we definitely want to generate new geometry here
                    output.Add(isPoint1 ? p.WorldPos : intersection1.Second);
                    output.Add(isPoint2 ? p.WorldPos : intersection2.Second);

                    foreach (ConvexHullList hullList in hullsInRange)
                    {
                        hullList.IsHidden.Remove(p.ConvexHull);
                        hullList.IsHidden.Remove(seg1.ConvexHull);
                        hullList.IsHidden.Remove(seg2.ConvexHull);
                    }
                }
                //if neither of the conditions above are met, we just assume
                //that the raycasts both resulted on the same segment
                //and creating geometry here would be wasteful
            }

            //remove points that are very close to each other
            for (int i = 0; i < output.Count - 1; i++)
            {
                if (Math.Abs(output[i].X - output[i + 1].X) < 6 &&
                    Math.Abs(output[i].Y - output[i + 1].Y) < 6)
                {
                    output.RemoveAt(i + 1);
                    i--;
                }
            }

            return(output);
        }
Ejemplo n.º 49
0
        partial void InitProjSpecific(XElement element)
        {
            foreach (XElement subElement in element.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "topsprite":
                    inventoryTopSprite = new Sprite(subElement);
                    break;

                case "backsprite":
                    inventoryBackSprite = new Sprite(subElement);
                    break;

                case "bottomsprite":
                    inventoryBottomSprite = new Sprite(subElement);
                    break;

                case "containedstateindicator":
                    ContainedStateIndicator = new Sprite(subElement);
                    break;

                case "containedstateindicatorempty":
                    ContainedStateIndicatorEmpty = new Sprite(subElement);
                    break;
                }
            }

            if (string.IsNullOrEmpty(ContainedStateIndicatorStyle))
            {
                //if neither a style or a custom sprite is defined, use default style
                if (ContainedStateIndicator == null)
                {
                    IndicatorStyle = GUI.Style.GetComponentStyle("ContainedStateIndicator.Default");
                }
            }
            else
            {
                IndicatorStyle = GUI.Style.GetComponentStyle("ContainedStateIndicator." + ContainedStateIndicatorStyle);
                if (ContainedStateIndicator != null || ContainedStateIndicatorEmpty != null)
                {
                    DebugConsole.AddWarning($"Item \"{item.Name}\" defines both a contained state indicator style and a custom indicator sprite. Will use the custom sprite...");
                }
            }
            if (GuiFrame == null)
            {
                //if a GUIFrame is not defined in the xml,
                //we create a full-screen frame and let the inventory position itself on it
                GuiFrame = new GUIFrame(new RectTransform(Vector2.One, GUI.Canvas), style: null)
                {
                    CanBeFocused = false
                };
                guiCustomComponent = new GUICustomComponent(new RectTransform(Vector2.One, GuiFrame.RectTransform),
                                                            onDraw: (SpriteBatch spriteBatch, GUICustomComponent component) => { Inventory.Draw(spriteBatch); },
                                                            onUpdate: null)
                {
                    CanBeFocused = false
                };
                GuiFrame.RectTransform.ParentChanged += OnGUIParentChanged;
            }
            else
            {
                //if a GUIFrame has been defined, draw the inventory inside it
                CreateGUI();
            }

            containedSpriteDepths = element.GetAttributeFloatArray("containedspritedepths", new float[0]);
        }
Ejemplo n.º 50
0
        public void Dock(DockingPort target)
        {
            if (item.Submarine.DockedTo.Contains(target.item.Submarine))
            {
                return;
            }

            forceLockTimer = 0.0f;

            if (DockingTarget != null)
            {
                Undock();
            }

            if (target.item.Submarine == item.Submarine)
            {
                DebugConsole.ThrowError("Error - tried to dock a submarine to itself");
                DockingTarget = null;
                return;
            }

            target.InitializeLinks();

            if (!item.linkedTo.Contains(target.item))
            {
                item.linkedTo.Add(target.item);
            }
            if (!target.item.linkedTo.Contains(item))
            {
                target.item.linkedTo.Add(item);
            }

            if (!target.item.Submarine.DockedTo.Contains(item.Submarine))
            {
                target.item.Submarine.ConnectedDockingPorts.Add(item.Submarine, target);
            }
            if (!item.Submarine.DockedTo.Contains(target.item.Submarine))
            {
                item.Submarine.ConnectedDockingPorts.Add(target.item.Submarine, this);
            }

            DockingTarget = target;
            DockingTarget.DockingTarget = this;

            docked = true;
            DockingTarget.Docked = true;

            if (Character.Controlled != null &&
                (Character.Controlled.Submarine == DockingTarget.item.Submarine || Character.Controlled.Submarine == item.Submarine))
            {
                GameMain.GameScreen.Cam.Shake = Vector2.Distance(DockingTarget.item.Submarine.Velocity, item.Submarine.Velocity);
            }

            DockingDir = GetDir(DockingTarget);
            DockingTarget.DockingDir = -DockingDir;

            CreateJoint(false);

#if SERVER
            if (GameMain.Server != null && (!item.Submarine?.Loading ?? true))
            {
                originalDockingTargetID = DockingTarget.item.ID;
                item.CreateServerEvent(this);
            }
#endif

            OnDocked?.Invoke();
            OnDocked = null;
        }
Ejemplo n.º 51
0
        public void Lock(bool isNetworkMessage, bool forcePosition = false)
        {
#if CLIENT
            if (GameMain.Client != null && !isNetworkMessage)
            {
                return;
            }
#endif

            if (DockingTarget == null)
            {
                DebugConsole.ThrowError("Error - attempted to lock a docking port that's not connected to anything");
                return;
            }

            if (!(joint is WeldJoint))
            {
                DockingDir = GetDir(DockingTarget);
                DockingTarget.DockingDir = -DockingDir;

                ApplyStatusEffects(ActionType.OnUse, 1.0f);

                Vector2 jointDiff = joint.WorldAnchorB - joint.WorldAnchorA;
                if (item.Submarine.PhysicsBody.Mass < DockingTarget.item.Submarine.PhysicsBody.Mass ||
                    DockingTarget.item.Submarine.Info.IsOutpost)
                {
                    item.Submarine.SubBody.SetPosition(item.Submarine.SubBody.Position + ConvertUnits.ToDisplayUnits(jointDiff));
                }
                else if (DockingTarget.item.Submarine.PhysicsBody.Mass < item.Submarine.PhysicsBody.Mass ||
                         item.Submarine.Info.IsOutpost)
                {
                    DockingTarget.item.Submarine.SubBody.SetPosition(DockingTarget.item.Submarine.SubBody.Position - ConvertUnits.ToDisplayUnits(jointDiff));
                }

                ConnectWireBetweenPorts();
                CreateJoint(true);

#if SERVER
                if (GameMain.Server != null && (!item.Submarine?.Loading ?? true))
                {
                    originalDockingTargetID = DockingTarget.item.ID;
                    item.CreateServerEvent(this);
                }
#else
                if (GameMain.Client != null && GameMain.Client.MidRoundSyncing &&
                    (item.Submarine == Submarine.MainSub || DockingTarget.item.Submarine == Submarine.MainSub))
                {
                    Screen.Selected.Cam.Position = Submarine.MainSub.WorldPosition;
                }
#endif
            }


            List <MapEntity> removedEntities = item.linkedTo.Where(e => e.Removed).ToList();
            foreach (MapEntity removed in removedEntities)
            {
                item.linkedTo.Remove(removed);
            }

            if (!item.linkedTo.Any(e => e is Hull) && !DockingTarget.item.linkedTo.Any(e => e is Hull))
            {
                CreateHulls();
            }

            if (Door != null && DockingTarget.Door != null)
            {
                WayPoint myWayPoint     = WayPoint.WayPointList.Find(wp => Door.LinkedGap == wp.ConnectedGap);
                WayPoint targetWayPoint = WayPoint.WayPointList.Find(wp => DockingTarget.Door.LinkedGap == wp.ConnectedGap);

                if (myWayPoint != null && targetWayPoint != null)
                {
                    myWayPoint.FindHull();
                    myWayPoint.linkedTo.Add(targetWayPoint);
                    targetWayPoint.FindHull();
                    targetWayPoint.linkedTo.Add(myWayPoint);
                }
            }
        }
Ejemplo n.º 52
0
 public void Awake()
 {
     Instance = this;
 }
Ejemplo n.º 53
0
        private void CreateHulls()
        {
            var hullRects = new Rectangle[] { item.WorldRect, DockingTarget.item.WorldRect };
            var subs      = new Submarine[] { item.Submarine, DockingTarget.item.Submarine };

            bodies = new Body[4];

            if (DockingTarget.Door != null)
            {
                CreateDoorBody();
            }

            if (Door != null)
            {
                DockingTarget.CreateDoorBody();
            }

            if (IsHorizontal)
            {
                if (hullRects[0].Center.X > hullRects[1].Center.X)
                {
                    hullRects = new Rectangle[] { DockingTarget.item.WorldRect, item.WorldRect };
                    subs      = new Submarine[] { DockingTarget.item.Submarine, item.Submarine };
                }

                hullRects[0] = new Rectangle(hullRects[0].Center.X, hullRects[0].Y, ((int)DockedDistance / 2), hullRects[0].Height);
                hullRects[1] = new Rectangle(hullRects[1].Center.X - ((int)DockedDistance / 2), hullRects[1].Y, ((int)DockedDistance / 2), hullRects[1].Height);

                //expand hulls if needed, so there's no empty space between the sub's hulls and docking port hulls
                int leftSubRightSide = int.MinValue, rightSubLeftSide = int.MaxValue;
                foreach (Hull hull in Hull.hullList)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        if (hull.Submarine != subs[i])
                        {
                            continue;
                        }
                        if (hull.WorldRect.Y < hullRects[i].Y - hullRects[i].Height)
                        {
                            continue;
                        }
                        if (hull.WorldRect.Y - hull.WorldRect.Height > hullRects[i].Y)
                        {
                            continue;
                        }

                        if (i == 0) //left hull
                        {
                            leftSubRightSide = Math.Max(hull.WorldRect.Right, leftSubRightSide);
                        }
                        else //upper hull
                        {
                            rightSubLeftSide = Math.Min(hull.WorldRect.X, rightSubLeftSide);
                        }
                    }
                }

                if (leftSubRightSide == int.MinValue || rightSubLeftSide == int.MaxValue)
                {
                    DebugConsole.NewMessage("Creating hulls between docking ports failed. Could not find a hull next to the docking port.");
                    return;
                }

                //expand left hull to the rightmost hull of the sub at the left side
                //(unless the difference is more than 100 units - if the distance is very large
                //there's something wrong with the positioning of the docking ports or submarine hulls)
                int leftHullDiff = (hullRects[0].X - leftSubRightSide) + 5;
                if (leftHullDiff > 0)
                {
                    if (leftHullDiff > 100)
                    {
                        DebugConsole.NewMessage("Creating hulls between docking ports failed. The leftmost docking port seems to be very far from any hulls in the left-side submarine.");
                        return;
                    }
                    else
                    {
                        hullRects[0].X     -= leftHullDiff;
                        hullRects[0].Width += leftHullDiff;
                    }
                }

                int rightHullDiff = (rightSubLeftSide - hullRects[1].Right) + 5;
                if (rightHullDiff > 0)
                {
                    if (rightHullDiff > 100)
                    {
                        DebugConsole.NewMessage("Creating hulls between docking ports failed. The rightmost docking port seems to be very far from any hulls in the right-side submarine.");
                        return;
                    }
                    else
                    {
                        hullRects[1].Width += rightHullDiff;
                    }
                }

                for (int i = 0; i < 2; i++)
                {
                    hullRects[i].Location -= MathUtils.ToPoint((subs[i].WorldPosition - subs[i].HiddenSubPosition));
                    hulls[i] = new Hull(MapEntityPrefab.Find(null, "hull"), hullRects[i], subs[i]);
                    hulls[i].AddToGrid(subs[i]);
                    hulls[i].FreeID();

                    for (int j = 0; j < 2; j++)
                    {
                        bodies[i + j * 2] = GameMain.World.CreateEdge(
                            ConvertUnits.ToSimUnits(new Vector2(hullRects[i].X, hullRects[i].Y - hullRects[i].Height * j)),
                            ConvertUnits.ToSimUnits(new Vector2(hullRects[i].Right, hullRects[i].Y - hullRects[i].Height * j)));
                    }
                }

                if (rightHullDiff <= 100 && hulls[0].Submarine != null)
                {
                    outsideBlocker = hulls[0].Submarine.PhysicsBody.FarseerBody.CreateRectangle(
                        ConvertUnits.ToSimUnits(hullRects[0].Width + hullRects[1].Width),
                        ConvertUnits.ToSimUnits(hullRects[0].Height),
                        density: 0.0f,
                        offset: ConvertUnits.ToSimUnits(new Vector2(hullRects[0].Right, hullRects[0].Y - hullRects[0].Height / 2) - hulls[0].Submarine.HiddenSubPosition));
                    outsideBlocker.UserData = this;
                }

                gap = new Gap(new Rectangle(hullRects[0].Right - 2, hullRects[0].Y, 4, hullRects[0].Height), true, subs[0]);
            }
            else
            {
                if (hullRects[0].Center.Y > hullRects[1].Center.Y)
                {
                    hullRects = new Rectangle[] { DockingTarget.item.WorldRect, item.WorldRect };
                    subs      = new Submarine[] { DockingTarget.item.Submarine, item.Submarine };
                }

                hullRects[0] = new Rectangle(hullRects[0].X, hullRects[0].Y + (int)(-hullRects[0].Height + DockedDistance) / 2, hullRects[0].Width, ((int)DockedDistance / 2));
                hullRects[1] = new Rectangle(hullRects[1].X, hullRects[1].Y - hullRects[1].Height / 2, hullRects[1].Width, ((int)DockedDistance / 2));

                //expand hulls if needed, so there's no empty space between the sub's hulls and docking port hulls
                int upperSubBottom = int.MaxValue, lowerSubTop = int.MinValue;
                foreach (Hull hull in Hull.hullList)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        if (hull.Submarine != subs[i])
                        {
                            continue;
                        }
                        if (hull.WorldRect.Right < hullRects[i].X)
                        {
                            continue;
                        }
                        if (hull.WorldRect.X > hullRects[i].Right)
                        {
                            continue;
                        }

                        if (i == 0) //lower hull
                        {
                            lowerSubTop = Math.Max(hull.WorldRect.Y, lowerSubTop);
                        }
                        else //upper hull
                        {
                            upperSubBottom = Math.Min(hull.WorldRect.Y - hull.WorldRect.Height, upperSubBottom);
                        }
                    }
                }

                if (upperSubBottom == int.MaxValue || lowerSubTop == int.MinValue)
                {
                    DebugConsole.NewMessage("Creating hulls between docking ports failed. Could not find a hull next to the docking port.");
                    return;
                }

                //expand lower hull to the topmost hull of the lower sub
                //(unless the difference is more than 100 units - if the distance is very large
                //there's something wrong with the positioning of the docking ports or submarine hulls)
                int lowerHullDiff = ((hullRects[0].Y - hullRects[0].Height) - lowerSubTop) + 5;
                if (lowerHullDiff > 0)
                {
                    if (lowerHullDiff > 100)
                    {
                        DebugConsole.NewMessage("Creating hulls between docking ports failed. The lower docking port seems to be very far from any hulls in the lower submarine.");
                        return;
                    }
                    else
                    {
                        hullRects[0].Height += lowerHullDiff;
                    }
                }

                int upperHullDiff = (upperSubBottom - hullRects[1].Y) + 5;
                if (upperHullDiff > 0)
                {
                    if (upperHullDiff > 100)
                    {
                        DebugConsole.NewMessage("Creating hulls between docking ports failed. The upper docking port seems to be very far from any hulls in the upper submarine.");
                        return;
                    }
                    else
                    {
                        hullRects[1].Y      += upperHullDiff;
                        hullRects[1].Height += upperHullDiff;
                    }
                }

                //difference between the edges of the hulls (to avoid a gap between the hulls)
                //0 is lower
                int midHullDiff = ((hullRects[1].Y - hullRects[1].Height) - hullRects[0].Y) + 2;
                if (midHullDiff > 100)
                {
                    DebugConsole.NewMessage("Creating hulls between docking ports failed. The upper hull seems to be very far from the lower hull.");
                    return;
                }
                else if (midHullDiff > 0)
                {
                    hullRects[0].Height += midHullDiff / 2 + 1;
                    hullRects[1].Y      -= midHullDiff / 2 + 1;
                    hullRects[1].Height += midHullDiff / 2 + 1;
                }


                for (int i = 0; i < 2; i++)
                {
                    hullRects[i].Location -= MathUtils.ToPoint((subs[i].WorldPosition - subs[i].HiddenSubPosition));
                    hulls[i] = new Hull(MapEntityPrefab.Find(null, "hull"), hullRects[i], subs[i]);
                    hulls[i].AddToGrid(subs[i]);
                    hulls[i].FreeID();

                    for (int j = 0; j < 2; j++)
                    {
                        bodies[i + j * 2] = GameMain.World.CreateEdge(
                            ConvertUnits.ToSimUnits(new Vector2(hullRects[i].X + hullRects[i].Width * j, hullRects[i].Y)),
                            ConvertUnits.ToSimUnits(new Vector2(hullRects[i].X + hullRects[i].Width * j, hullRects[i].Y - hullRects[i].Height)));
                    }
                }

                if (midHullDiff <= 100 && hulls[0].Submarine != null)
                {
                    outsideBlocker = hulls[0].Submarine.PhysicsBody.FarseerBody.CreateRectangle(
                        ConvertUnits.ToSimUnits(hullRects[0].Width),
                        ConvertUnits.ToSimUnits(hullRects[0].Height + hullRects[1].Height),
                        density: 0.0f,
                        offset: ConvertUnits.ToSimUnits(new Vector2(hullRects[0].Center.X, hullRects[0].Y) - hulls[0].Submarine.HiddenSubPosition));
                    outsideBlocker.UserData = this;
                }

                gap = new Gap(new Rectangle(hullRects[0].X, hullRects[0].Y + 2, hullRects[0].Width, 4), false, subs[0]);
            }

            LinkHullsToGaps();

            hulls[0].ShouldBeSaved = false;
            hulls[1].ShouldBeSaved = false;
            item.linkedTo.Add(hulls[0]);
            item.linkedTo.Add(hulls[1]);

            gap.FreeID();
            gap.DisableHullRechecks = true;
            gap.ShouldBeSaved       = false;
            item.linkedTo.Add(gap);

            foreach (Body body in bodies)
            {
                if (body == null)
                {
                    continue;
                }
                body.BodyType = BodyType.Static;
                body.Friction = 0.5f;

                body.CollisionCategories = Physics.CollisionWall;
            }
        }
Ejemplo n.º 54
0
 void Awake()
 {
     s_Instance = this;
     InitGuis();
 }
Ejemplo n.º 55
0
 public static void takeScreenshot()
 {
     DebugConsole.Log("taking shot...");
     takeHiResShot = true;
 }