Esempio n. 1
0
        private static void HandleVesselDock(ClientStructure client, VesselBaseMsgData message)
        {
            var msgData = (VesselDockMsgData)message;

            LunaLog.Debug($"Docking message received! Dominant vessel: {msgData.DominantVesselId}");

            if (VesselContext.RemovedVessels.Contains(msgData.WeakVesselId))
            {
                return;
            }

            if (VesselStoreSystem.VesselExists(msgData.DominantVesselId))
            {
                LunaLog.Debug($"Saving DOCKED vessel {msgData.DominantVesselId} from {client.PlayerName}. Bytes: {msgData.NumBytes}");
            }
            VesselDataUpdater.RawConfigNodeInsertOrUpdate(msgData.DominantVesselId, Encoding.UTF8.GetString(msgData.FinalVesselData, 0, msgData.NumBytes));

            //Now remove the weak vessel but DO NOT add to the removed vessels as they might undock!!!
            LunaLog.Debug($"Removing weak docked vessel {msgData.WeakVesselId}");
            VesselStoreSystem.RemoveVessel(msgData.WeakVesselId);

            MessageQueuer.RelayMessage <VesselSrvMsg>(client, msgData);

            //Tell all clients to remove the weak vessel
            var removeMsgData = ServerContext.ServerMessageFactory.CreateNewMessageData <VesselRemoveMsgData>();

            removeMsgData.VesselId = msgData.WeakVesselId;

            MessageQueuer.SendToAllClients <VesselSrvMsg>(removeMsgData);
        }
Esempio n. 2
0
        private static void HandleVesselsSync(ClientStructure client, VesselBaseMsgData message)
        {
            var msgData = (VesselSyncMsgData)message;

            var allVessels = VesselStoreSystem.CurrentVesselsInXmlFormat.Keys.ToList();

            for (var i = 0; i < msgData.VesselsCount; i++)
            {
                allVessels.Remove(msgData.VesselIds[i]);
            }

            var vesselsToSend = allVessels;

            foreach (var vesselId in vesselsToSend)
            {
                var vesselData = VesselStoreSystem.GetVesselInConfigNodeFormat(vesselId);
                if (vesselData.Length > 0)
                {
                    var protoMsg = ServerContext.ServerMessageFactory.CreateNewMessageData <VesselProtoMsgData>();
                    protoMsg.Vessel.Data     = Encoding.UTF8.GetBytes(vesselData);
                    protoMsg.Vessel.NumBytes = vesselData.Length;
                    protoMsg.Vessel.VesselId = vesselId;

                    MessageQueuer.SendToClient <VesselSrvMsg>(client, protoMsg);
                }
            }

            if (allVessels.Count > 0)
            {
                LunaLog.Debug($"Sending {client.PlayerName} {vesselsToSend.Count} vessels");
            }
        }
        private static void RunDekessler()
        {
            var removalCount = 0;

            var vesselList = VesselStoreSystem.CurrentVesselsInXmlFormat.ToArray();

            foreach (var vesselKeyVal in vesselList)
            {
                if (IsVesselDebris(vesselKeyVal.Key, vesselKeyVal.Value))
                {
                    LunaLog.Normal($"Removing debris vessel: {vesselKeyVal.Key}");

                    VesselStoreSystem.RemoveVessel(vesselKeyVal.Key);

                    //Send a vessel remove message
                    var msgData = ServerContext.ServerMessageFactory.CreateNewMessageData <VesselRemoveMsgData>();
                    msgData.VesselId = vesselKeyVal.Key;

                    MessageQueuer.SendToAllClients <VesselSrvMsg>(msgData);

                    removalCount++;
                }
            }

            if (removalCount > 0)
            {
                LunaLog.Normal($"Removed {removalCount} debris");
            }
        }
Esempio n. 4
0
        private static void RunNuke()
        {
            uint removalCount = 0;

            var vesselList = VesselStoreSystem.CurrentVessels.ToArray();

            foreach (var vesselKeyVal in vesselList)
            {
                if (vesselKeyVal.Value.Fields.GetSingle("landed").Value.ToLower() == "true" &&
                    vesselKeyVal.Value.Fields.GetSingle("landedAt").Value.ToLower().Contains("ksc") ||
                    vesselKeyVal.Value.Fields.GetSingle("landedAt").Value.ToLower().Contains("runway") ||
                    vesselKeyVal.Value.Fields.GetSingle("landedAt").Value.ToLower().Contains("launchpad"))
                {
                    LunaLog.Normal($"Removing vessel: {vesselKeyVal.Key} from KSC");

                    VesselStoreSystem.RemoveVessel(vesselKeyVal.Key);

                    //Send a vessel remove message
                    var msgData = ServerContext.ServerMessageFactory.CreateNewMessageData <VesselRemoveMsgData>();
                    msgData.VesselId = vesselKeyVal.Key;

                    MessageQueuer.SendToAllClients <VesselSrvMsg>(msgData);

                    removalCount++;
                }
            }

            if (removalCount > 0)
            {
                LunaLog.Normal($"Nuked {removalCount} vessels around the KSC");
            }
        }
Esempio n. 5
0
        private static void HandleVesselsSync(ClientStructure client, VesselBaseMsgData message)
        {
            var msgData = (VesselSyncMsgData)message;

            var allVessels = VesselStoreSystem.CurrentVessels.Keys.ToList();

            //Here we only remove the vessels that the client ALREADY HAVE so we only send the vessels he DOESN'T have
            for (var i = 0; i < msgData.VesselsCount; i++)
            {
                allVessels.Remove(msgData.VesselIds[i]);
            }

            var vesselsToSend = allVessels;

            foreach (var vesselId in vesselsToSend)
            {
                var vesselData = VesselStoreSystem.GetVesselInConfigNodeFormat(vesselId);
                if (vesselData.Length > 0)
                {
                    var protoMsg = ServerContext.ServerMessageFactory.CreateNewMessageData <VesselProtoMsgData>();
                    protoMsg.Data     = Encoding.UTF8.GetBytes(vesselData);
                    protoMsg.NumBytes = vesselData.Length;
                    protoMsg.VesselId = vesselId;

                    MessageQueuer.SendToClient <VesselSrvMsg>(client, protoMsg);

                    LunaLog.Debug($"Sending {client.PlayerName} vessel {vesselId}");
                }
            }
        }
Esempio n. 6
0
        private static void HandleVesselRemove(ClientStructure client, VesselBaseMsgData message)
        {
            var data = (VesselRemoveMsgData)message;

            if (data.Force)
            {
                LunaLog.Debug($"Received a FORCED remove against vessel {data.VesselId} from {client.PlayerName}");
            }

            if (!data.Force && LockSystem.LockQuery.ControlLockExists(data.VesselId) && !LockSystem.LockQuery.ControlLockBelongsToPlayer(data.VesselId, client.PlayerName))
            {
                return;
            }

            if (VesselStoreSystem.VesselExists(data.VesselId))
            {
                LunaLog.Debug($"Removing vessel {data.VesselId} from {client.PlayerName}");
                VesselStoreSystem.RemoveVessel(data.VesselId);
            }

            if (data.AddToKillList)
            {
                VesselContext.RemovedVessels.Add(data.VesselId);
            }

            //Relay the message.
            MessageQueuer.SendToAllClients <VesselSrvMsg>(data);
        }
        public override bool Execute(string commandArgs)
        {
            var allLocks = new List <LockDefinition>();

            foreach (var lockDefinition in LockSystem.LockQuery.GetAllLocks())
            {
                switch (lockDefinition.Type)
                {
                case LockType.Contract:
                case LockType.Asteroid:
                case LockType.Kerbal:
                case LockType.Spectator:
                    allLocks.Add(lockDefinition);
                    break;

                case LockType.Control:
                case LockType.Update:
                case LockType.UnloadedUpdate:
                    if (VesselStoreSystem.VesselExists(lockDefinition.VesselId))
                    {
                        allLocks.Add(lockDefinition);
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            if (!allLocks.Any())
            {
                LunaLog.Normal("No locks");
            }
            else
            {
                foreach (var lockDef in allLocks)
                {
                    LunaLog.Normal(lockDef.ToString());
                }
            }

            return(true);
        }
        //Removes all matching vessels
        private static void RunRemove(string vesselType, string vesselSituation, string vesselSplashed, string vesselName)
        {
            var removalCount = 0;
            var vesselList   = VesselStoreSystem.CurrentVessels.ToArray();

            foreach (var vesselKeyVal in vesselList.Where(v => IsVesselFound(v.Value, vesselType, vesselSituation, vesselSplashed, vesselName)))
            {
                LunaLog.Normal($"Removing vessel: {vesselKeyVal.Key}");

                VesselStoreSystem.RemoveVessel(vesselKeyVal.Key);

                var msgData = ServerContext.ServerMessageFactory.CreateNewMessageData <VesselRemoveMsgData>();
                msgData.VesselId = vesselKeyVal.Key;
                MessageQueuer.SendToAllClients <VesselSrvMsg>(msgData);

                removalCount++;
            }

            LunaLog.Normal(removalCount > 0 ? $"Removed {removalCount} vessel(s) ..." : "Removed nothing ...");
        }
Esempio n. 9
0
        private static void HandleVesselProto(ClientStructure client, VesselBaseMsgData message)
        {
            var msgData = (VesselProtoMsgData)message;

            if (VesselContext.RemovedVessels.Contains(msgData.Vessel.VesselId))
            {
                return;
            }

            if (msgData.Vessel.NumBytes == 0)
            {
                LunaLog.Warning($"Received a vessel with 0 bytes ({msgData.Vessel.VesselId}) from {client.PlayerName}.");
                return;
            }

            if (!VesselStoreSystem.VesselExists(msgData.Vessel.VesselId))
            {
                LunaLog.Debug($"Saving vessel {msgData.Vessel.VesselId} from {client.PlayerName}. Bytes: {msgData.Vessel.NumBytes}");
            }

            VesselDataUpdater.RawConfigNodeInsertOrUpdate(msgData.Vessel.VesselId, Encoding.UTF8.GetString(msgData.Vessel.Data, 0, msgData.Vessel.NumBytes));
            MessageQueuer.RelayMessage <VesselSrvMsg>(client, msgData);
        }
Esempio n. 10
0
        private static void HandleVesselCouple(ClientStructure client, VesselBaseMsgData message)
        {
            var msgData = (VesselCoupleMsgData)message;

            LunaLog.Debug($"Coupling message received! Dominant vessel: {msgData.VesselId}");
            MessageQueuer.RelayMessage <VesselSrvMsg>(client, msgData);

            if (VesselContext.RemovedVessels.Contains(msgData.CoupledVesselId))
            {
                return;
            }

            //Now remove the weak vessel but DO NOT add to the removed vessels as they might undock!!!
            LunaLog.Debug($"Removing weak coupled vessel {msgData.CoupledVesselId}");
            VesselStoreSystem.RemoveVessel(msgData.CoupledVesselId);

            //Tell all clients to remove the weak vessel
            var removeMsgData = ServerContext.ServerMessageFactory.CreateNewMessageData <VesselRemoveMsgData>();

            removeMsgData.VesselId = msgData.CoupledVesselId;

            MessageQueuer.SendToAllClients <VesselSrvMsg>(removeMsgData);
        }
Esempio n. 11
0
        //Removes all matching vessels
        private static void RunRemove(string vesselType, string vesselSituation, string vesselSplashed, string vesselName, string orbName)
        {
            //Declare variables
            var removalCount = 0;
            var vesselList   = VesselStoreSystem.CurrentVesselsInXmlFormat.ToArray();

            //Cycle vesselList
            foreach (var vesselKeyVal in vesselList)
            {
                //Check if vessel can be found
                if (IsVesselFound(vesselKeyVal.Value, vesselType, vesselSituation, vesselSplashed, vesselName, orbName))
                {
                    //Send a vessel remove server console message
                    LunaLog.Normal($"Removing vessel: {vesselKeyVal.Key}");

                    //Remove vessel from universe
                    VesselStoreSystem.RemoveVessel(vesselKeyVal.Key);

                    //Send a vessel remove message to all clients
                    var msgData = ServerContext.ServerMessageFactory.CreateNewMessageData <VesselRemoveMsgData>();
                    msgData.VesselId = vesselKeyVal.Key;
                    MessageQueuer.SendToAllClients <VesselSrvMsg>(msgData);

                    //Increase counter
                    removalCount++;
                }
            }
            //Send message remove info
            if (removalCount > 0)
            {
                LunaLog.Normal($"Removed {removalCount} vessel(s) ...");
            }
            else
            {
                LunaLog.Normal($"Removed nothing ...");
            }
        }
Esempio n. 12
0
        public static void Main()
        {
            try
            {
                Console.Title = $"LMP {LmpVersioning.CurrentVersion}";

                Console.OutputEncoding  = Encoding.Unicode;
                ServerContext.StartTime = LunaNetworkTime.UtcNow.Ticks;

                LunaLog.Info("Remember! Quit the server by using 'Control + C' so a backup is properly made before closing!");

                if (Common.PlatformIsWindows())
                {
                    ExitSignal.Exit += (sender, args) => Exit();
                }
                else
                {
                    //Register the ctrl+c event and exit signal if we are on linux
                    Console.CancelKeyPress += (sender, args) => Exit();
                }

                //We disable quick edit as otherwise when you select some text for copy/paste then you can't write to the console and server freezes
                //This just happens on windows....
                if (Common.PlatformIsWindows())
                {
                    ConsoleUtil.DisableConsoleQuickEdit();
                }

                //We cannot run more than 6 instances ofd servers + clients as otherwise the sync time will fail (30 seconds / 5 seconds = 6) but we use 3 for safety
                if (GetRunningInstances() > 3)
                {
                    throw new HandledException("Cannot run more than 3 servers at a time!");
                }

                //Start the server clock
                ServerContext.ServerClock.Start();

                ServerContext.ServerStarting = true;

                //Set day for log change
                ServerContext.Day = LunaNetworkTime.Now.Day;

                LunaLog.Normal($"Luna Server version: {LmpVersioning.CurrentVersion} ({Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)})");

                Universe.CheckUniverse();
                LoadSettingsAndGroups();
                VesselStoreSystem.LoadExistingVessels();
                var scenariosCreated = ScenarioSystem.GenerateDefaultScenarios();
                ScenarioStoreSystem.LoadExistingScenarios(scenariosCreated);
                LmpPluginHandler.LoadPlugins();
                WarpSystem.Reset();

                LunaLog.Normal($"Starting '{GeneralSettings.SettingsStore.ServerName}' on Port {ConnectionSettings.SettingsStore.Port}... ");

                LidgrenServer.SetupLidgrenServer();
                LmpPortMapper.OpenLmpPort().Wait();
                LmpPortMapper.OpenWebPort().Wait();
                ServerContext.ServerRunning = true;
                WebServer.StartWebServer();

                //Do not add the command handler thread to the TaskContainer as it's a blocking task
                LongRunTaskFactory.StartNew(CommandHandler.ThreadMain, CancellationTokenSrc.Token);

                TaskContainer.Add(LongRunTaskFactory.StartNew(WebServer.RefreshWebServerInformation, CancellationTokenSrc.Token));

                TaskContainer.Add(LongRunTaskFactory.StartNew(LmpPortMapper.RefreshUpnpPort, CancellationTokenSrc.Token));
                TaskContainer.Add(LongRunTaskFactory.StartNew(LogThread.RunLogThread, CancellationTokenSrc.Token));
                TaskContainer.Add(LongRunTaskFactory.StartNew(ClientMainThread.ThreadMain, CancellationTokenSrc.Token));

                TaskContainer.Add(LongRunTaskFactory.StartNew(() => BackupSystem.PerformBackups(CancellationTokenSrc.Token), CancellationTokenSrc.Token));
                TaskContainer.Add(LongRunTaskFactory.StartNew(LidgrenServer.StartReceivingMessages, CancellationTokenSrc.Token));
                TaskContainer.Add(LongRunTaskFactory.StartNew(LidgrenMasterServer.RegisterWithMasterServer, CancellationTokenSrc.Token));

                TaskContainer.Add(LongRunTaskFactory.StartNew(VersionChecker.RefreshLatestVersion, CancellationTokenSrc.Token));
                TaskContainer.Add(LongRunTaskFactory.StartNew(VersionChecker.DisplayNewVersionMsg, CancellationTokenSrc.Token));

                while (ServerContext.ServerStarting)
                {
                    Thread.Sleep(500);
                }

                LunaLog.Normal("All systems up and running. Поехали!");
                LmpPluginHandler.FireOnServerStart();

                QuitEvent.WaitOne();

                LmpPluginHandler.FireOnServerStop();

                LunaLog.Normal("So long and thanks for all the fish!");
            }
            catch (Exception e)
            {
                LunaLog.Fatal(e is HandledException ? e.Message : $"Error in main server thread, Exception: {e}");
                Console.ReadLine(); //Avoid closing automatically
            }
        }
Esempio n. 13
0
        public static void Main()
        {
            try
            {
                Console.Title = $"LMPServer {LmpVersioning.CurrentVersion}";
#if DEBUG
                Console.Title += " DEBUG";
#endif
                Console.OutputEncoding  = Encoding.Unicode;
                ServerContext.StartTime = LunaTime.UtcNow.Ticks;

                if (!Common.PlatformIsWindows())
                {
                    LunaLog.Warning("Remember! Quit the server by using Control+C so the vessels are saved to the hard drive!");
                }


                if (Common.PlatformIsWindows())
                {
                    ExitSignal.Exit += (sender, args) => Exit();
                }
                else
                {
                    //Register the ctrl+c event and exit signal if we are on linux
                    Console.CancelKeyPress += (sender, args) => Exit();
                }

                //We disable quick edit as otherwise when you select some text for copy/paste then you can't write to the console and server freezes
                //This just happens on windows....
                if (Common.PlatformIsWindows())
                {
                    ConsoleUtil.DisableConsoleQuickEdit();
                }

                //We cannot run more than 6 instances ofd servers + clients as otherwise the sync time will fail (30 seconds / 5 seconds = 6) but we use 3 for safety
                if (GetRunningInstances() > 3)
                {
                    throw new HandledException("Cannot run more than 3 servers at a time!");
                }

                //Start the server clock
                ServerContext.ServerClock.Start();

                //Set the last player activity time to server start
                ServerContext.LastPlayerActivity = ServerContext.ServerClock.ElapsedMilliseconds;

                ServerContext.ServerStarting = true;

                //Set day for log change
                ServerContext.Day = LunaTime.Now.Day;

                LunaLog.Normal($"Starting Luna Server version: {LmpVersioning.CurrentVersion}");

                Universe.CheckUniverse();
                LoadSettingsAndGroups();
                VesselStoreSystem.LoadExistingVessels();
                ScenarioSystem.GenerateDefaultScenarios();
                ScenarioStoreSystem.LoadExistingScenarios();
                LmpPluginHandler.LoadPlugins();
                WarpSystem.Reset();

                LunaLog.Normal($"Starting {GeneralSettings.SettingsStore.WarpMode} server on Port {GeneralSettings.SettingsStore.Port}... ");
                LunaLog.Normal($"Server name: '{GeneralSettings.SettingsStore.ServerName}'...");
                LunaLog.Normal($"Server location: '{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}'...");

                ServerContext.ServerRunning = true;
                LidgrenServer.SetupLidgrenServer();

                //Do not add the command handler thread to the TaskContainer as it's a blocking task
                LongRunTaskFactory.StartNew(() => new CommandHandler().ThreadMain(), CancellationTokenSrc.Token);

                TaskContainer.Add(LongRunTaskFactory.StartNew(LogThread.RunLogThread, CancellationTokenSrc.Token));
                TaskContainer.Add(LongRunTaskFactory.StartNew(() => new ClientMainThread().ThreadMain(), CancellationTokenSrc.Token));

                TaskContainer.Add(LongRunTaskFactory.StartNew(() => BackupSystem.PerformBackups(CancellationTokenSrc.Token), CancellationTokenSrc.Token));
                TaskContainer.Add(LongRunTaskFactory.StartNew(LidgrenServer.StartReceiveingMessages, CancellationTokenSrc.Token));
                TaskContainer.Add(LongRunTaskFactory.StartNew(LidgrenMasterServer.RefreshMasterServersList, CancellationTokenSrc.Token));
                TaskContainer.Add(LongRunTaskFactory.StartNew(LidgrenMasterServer.RegisterWithMasterServer, CancellationTokenSrc.Token));

                TaskContainer.Add(LongRunTaskFactory.StartNew(VesselRelaySystem.RelayOldVesselMessages, CancellationTokenSrc.Token));
                TaskContainer.Add(LongRunTaskFactory.StartNew(VersionChecker.RefreshLatestVersion, CancellationTokenSrc.Token));
                TaskContainer.Add(LongRunTaskFactory.StartNew(VersionChecker.DisplayNewVersionMsg, CancellationTokenSrc.Token));

                while (ServerContext.ServerStarting)
                {
                    Thread.Sleep(500);
                }

                LunaLog.Normal("All systems up and running. Поехали!");
                LmpPluginHandler.FireOnServerStart();

                QuitEvent.WaitOne();

                LmpPluginHandler.FireOnServerStop();

                LunaLog.Normal("So long and thanks for all the fish!");
            }
            catch (Exception e)
            {
                if (e is HandledException)
                {
                    LunaLog.Fatal(e.Message);
                }
                else
                {
                    LunaLog.Fatal($"Error in main server thread, Exception: {e}");
                }
                Console.ReadLine(); //Avoid closing automatically
            }
        }