/// <summary> /// Server side initialization. /// </summary> private void InitServer() { //Debug = true; _isInitialized = true; // Set this first to block any other calls from UpdateBeforeSimulation(). Logger.Init(); Logger.Debug("Server Logger started"); VRage.Utils.MyLog.Default.WriteLine("##Mod## Admin Helper Server Initialisation"); // TODO: restructure the ChatCommandLogic to encapsulate ChatCommandService on the server side. // Required to check security for user calls on the Server side, and call the UpdateBeforeSimulation...() methods for each command. if (!_commandsRegistered) { foreach (ChatCommand command in GetAllChatCommands()) { ChatCommandService.Register(command); } _commandsRegistered = true; ChatCommandService.Init(); } AdminNotificator.Init(); ProtectionHandler.Init_Server(); MyAPIGateway.Multiplayer.RegisterMessageHandler(ConnectionHelper.ConnectionId, MessageHandler); Logger.Debug("Registered ProcessMessage"); ConnectionHelper.Server_MessageCache.Clear(); ServerCfg = new ServerConfig(GetAllChatCommands()); }
public override void UpdateBeforeSimulation() { Instance = this; // This needs to wait until the MyAPIGateway.Session.Player is created, as running on a Dedicated server can cause issues. // It would be nicer to just read a property that indicates this is a dedicated server, and simply return. if (!_isInitialized && MyAPIGateway.Session != null && MyAPIGateway.Session.Player != null) { Debug = MyAPIGateway.Session.Player.IsExperimentalCreator(); if (!MyAPIGateway.Session.OnlineMode.Equals(MyOnlineModeEnum.OFFLINE) && MyAPIGateway.Multiplayer.IsServer && !MyAPIGateway.Utilities.IsDedicated) { InitServer(); } Init(); } if (!_isInitialized && MyAPIGateway.Utilities != null && MyAPIGateway.Multiplayer != null && MyAPIGateway.Session != null && MyAPIGateway.Utilities.IsDedicated && MyAPIGateway.Multiplayer.IsServer) { InitServer(); return; } base.UpdateBeforeSimulation(); ChatCommandService.UpdateBeforeSimulation(); ProtectionHandler.UpdateBeforeSimulation(); TimerRegistry.Update(); }
protected void Application_Start(object sender, EventArgs e) { GlobalHandlers.DatabaseHandler = DatabaseHandler.create(config.handlers.instances.Type.MySQL); GlobalHandlers.SessionHandler = SessionHandler.create(); GlobalHandlers.SettingHandler = SettingHandler.create(); GlobalHandlers.ProtectionHandler = ProtectionHandler.create(); GlobalHandlers.Debugger = GlobalDebugger.create(); GlobalHandlers.TimerHandlers = TimerHandlers.create(); GlobalHandlers.ViewerHandler = ViewerHandler.create(); if (GlobalHandlers.DatabaseHandler.connect()) { Debug.WriteLine("[DatabaseHandler]: Database connected successfully"); } else { Debug.WriteLine("[DatabaseHandler]: Database failed to connect"); GlobalHandlers.Debugger.write("Database failed to connect"); } GlobalHandlers.DatabaseHandler.fillGlobalInformation(); GlobalHandlers.ViewerHandler.populateList(GlobalHandlers.DatabaseHandler.getViewers()); GlobalHandlers.TimerHandlers.startAllTimers(); GlobalHandlers.SettingHandler.setup(); GlobalHandlers.TileManager = TileManager.create(); GlobalHandlers.GCMHandler = GCMHandler.create(); GlobalHandlers.PowerschoolHandler = new config.handlers.features.powerschool.PowerschoolLoginHandler(); }
private void LandingGearOnStateChanged(bool state) { if (!state) { return; } var ship = _landingGear.GetTopMostParent(typeof(IMyCubeGrid)); if (ship == null) { return; } IMyPlayer player = null; foreach (var workingCockpit in _landingGear.CubeGrid.FindWorkingCockpits()) { player = MyAPIGateway.Players.GetPlayerControllingEntity(workingCockpit.Entity); if (player != null) { break; } } var attachedEntity = _landingGear.GetAttachedEntity() as IMyCubeGrid; if (attachedEntity == null) { return; } if (!ProtectionHandler.IsProtected(attachedEntity)) { return; } if (player == null) { _landingGear.ApplyAction("Unlock"); // we turn it off to prevent 'spamming' _landingGear.RequestEnable(false); return; } if (ProtectionHandler.CanModify(player, attachedEntity)) { return; } _landingGear.ApplyAction("Unlock"); // we turn it off to prevent 'spamming' _landingGear.RequestEnable(false); }
private void DetachEvents() { TimerRegistry.Close(); Sandbox.Game.MyVisualScriptLogicProvider.PlayerConnected = null; Sandbox.Game.MyVisualScriptLogicProvider.PlayerDropped = null; Sandbox.Game.MyVisualScriptLogicProvider.PlayerDisconnected = null; // servers: listen and dedicated, MP if (ServerCfg != null) { MyAPIGateway.Multiplayer.UnregisterMessageHandler(ConnectionHelper.ConnectionId, MessageHandler); ProtectionHandler.Close(); Logger.Debug("Unregistered MessageHandler"); } if (MyAPIGateway.Utilities != null && MyAPIGateway.Multiplayer != null && MyAPIGateway.Multiplayer.IsServer && MyAPIGateway.Utilities.IsDedicated) { return; } if (MyAPIGateway.Multiplayer != null && MyAPIGateway.Multiplayer.MultiplayerActive || (ServerCfg != null && ServerConfig.ServerIsClient)) { // all clients, including hosts, MP if (PermissionRequestTimer != null) { PermissionRequestTimer.Stop(); PermissionRequestTimer.Close(); } MyAPIGateway.Session.OnSessionReady -= Session_OnSessionReady; Logger.Debug("Detached Session_OnSessionReady"); // only clients, not the host if (ServerCfg == null) { MyAPIGateway.Multiplayer.UnregisterMessageHandler(ConnectionHelper.ConnectionId, MessageHandler); Logger.Debug("Unregistered MessageHandler"); } } if (MyAPIGateway.Utilities != null) { MyAPIGateway.Utilities.MessageEntered -= Utilities_MessageEntered; Logger.Debug("Detached MessageEntered"); } }
public override void ProcessServer() { bool sync = false; switch (Type) { case ProtectionConfigType.Invert: if (ProtectionHandler.Config.ProtectionInverted != Value) { sync = true; ProtectionHandler.Config.ProtectionInverted = Value; MessageClientTextMessage.SendMessage(SenderSteamId, "Server", $"The protection is {(Value ? "inverted" : "normal")} now."); } break; case ProtectionConfigType.Enable: if (ProtectionHandler.Config.ProtectionEnabled != Value) { sync = true; ProtectionHandler.Config.ProtectionEnabled = Value; MessageClientTextMessage.SendMessage(SenderSteamId, "Server", $"The protection is {(Value ? "enabled" : "disabled")} now"); } break; case ProtectionConfigType.LandingGear: if (ProtectionHandler.Config.ProtectionAllowLandingGear != Value) { sync = true; ProtectionHandler.Config.ProtectionAllowLandingGear = Value; MessageClientTextMessage.SendMessage(SenderSteamId, "Server", $"Protection area LandingGear is now {(Value ? "allowed" : "disabled")}."); } break; } if (sync) { ProtectionHandler.Save(); ConnectionHelper.SendMessageToAllPlayers(new MessageSyncProtection { Config = ProtectionHandler.Config }); } else { MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "The setting was already set to the specified value."); } }
public override void UpdateBeforeSimulation() { if (_multiplayerActive && MyAPIGateway.CubeBuilder != null && MyAPIGateway.CubeBuilder.BlockCreationIsActivated && MyAPIGateway.Session.Player != null && MyAPIGateway.Session.Player.Controller.ControlledEntity != null) { if (ChatCommandLogic.Instance != null && !ChatCommandLogic.Instance.AllowBuilding) { MyAPIGateway.CubeBuilder.DeactivateBlockCreation(); MyAPIGateway.Utilities.ShowNotification( "Protection is not loaded yet so any building is not allowed. Please try again later.", 2000, MyFontEnum.Red); } else if (ProtectionHandler.Config.ProtectionEnabled) { var cubeGrid = Support.FindLookAtEntity(MyAPIGateway.Session.Player.Controller.ControlledEntity, true, false, false, false, false, false) as IMyCubeGrid; if (_cachedGrid == null || (_cachedGrid != null && cubeGrid != _cachedGrid)) { if (cubeGrid != null && MyAPIGateway.Session.Player != null) { // TODO consider permission request from server instead of client side check... downside: might take a while if (!ProtectionHandler.CanModify(MyAPIGateway.Session.Player, cubeGrid) && ProtectionHandler.IsProtected(cubeGrid)) { _cachedGrid = cubeGrid; Deactivate(); } } } else { Deactivate(); } } } else if (!_multiplayerActive && (_multiplayerActive != (MyAPIGateway.Session.OnlineMode != MyOnlineModeEnum.OFFLINE))) { // we need to update it because it is not correctly initialized if the cube placer is created when the game loads _multiplayerActive = MyAPIGateway.Session.OnlineMode != MyOnlineModeEnum.OFFLINE; } base.UpdateBeforeSimulation(); }
public void Save(string customSaveName = null) { //write values into cfgFile Config.MotdHeadLine = CommandMessageOfTheDay.HeadLine; Config.MotdShowInChat = CommandMessageOfTheDay.ShowInChat; //cfg Config.WorldLocation = MyAPIGateway.Session.CurrentPath; _serverConfigFile.Save(customSaveName); //motd _motdFile.Save(); SaveLogs(customSaveName); if (customSaveName != null) { _permissionsFile.Save(customSaveName); } ProtectionHandler.Save(customSaveName); Logger.Debug("Config saved."); }
public override void ProcessServer() { switch (Type) { case ProtectionAreaMessageType.Add: if (ProtectionHandler.AddArea(ProtectionArea)) { MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "Successfully created area."); ConnectionHelper.SendMessageToAllPlayers(new MessageSyncProtection() { Config = ProtectionHandler.Config }); ProtectionHandler.Save(); } else { MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "An area with that name already exists."); } break; case ProtectionAreaMessageType.Remove: if (ProtectionHandler.RemoveArea(ProtectionArea)) { MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "Successfully removed area."); ConnectionHelper.SendMessageToAllPlayers(new MessageSyncProtection() { Config = ProtectionHandler.Config }); ProtectionHandler.Save(); } else { MessageClientTextMessage.SendMessage(SenderSteamId, "Server", "An area with that name could not be found."); } break; } }
private void Start() { protectionHandler = GetComponent <ProtectionHandler>(); }
private void _cubeGrid_OnBlockOwnershipChanged(IMyCubeGrid cubeGrid) { // only execute on server instance if (ChatCommandLogic.Instance != null && ChatCommandLogic.Instance.ServerCfg == null) { return; } if (_firstOwnershipChange) { _firstOwnershipChange = false; _cachedOwners = new List <long>(cubeGrid.GetAllSmallOwners()); return; } var allSmallOwners = cubeGrid.GetAllSmallOwners(); if (_cachedOwners == allSmallOwners) { return; } // if the grid wasn't owned or a owner was removed, we dont need to do anything but update the cached owners if (_cachedOwners.Count == 0 || _cachedOwners.Count > allSmallOwners.Count) { _cachedOwners = new List <long>(allSmallOwners); return; } var newOwners = allSmallOwners.Except(_cachedOwners).ToList(); if (newOwners.Count == 0) { return; } if (!ProtectionHandler.IsProtected(cubeGrid)) { _cachedOwners = new List <long>(allSmallOwners); return; } Dictionary <long, int> blocksPerOwner = new Dictionary <long, int>(); foreach (IMyCubeGrid attachedCubeGrid in cubeGrid.GetAttachedGrids(AttachedGrids.Static)) { List <IMySlimBlock> blocks = new List <IMySlimBlock>(); attachedCubeGrid.GetBlocks(blocks, b => b.FatBlock != null); foreach (IMySlimBlock block in blocks) { long ownerId = block.FatBlock.OwnerId; // we dont want the new owners, the small owners or the 'nobody' (0) if (ownerId == 0 || !attachedCubeGrid.BigOwners.Contains(ownerId) || newOwners.Contains(ownerId)) { continue; } if (!blocksPerOwner.ContainsKey(ownerId)) { blocksPerOwner.Add(ownerId, 1); } else { blocksPerOwner[ownerId]++; } } } var sortedBpo = new List <KeyValuePair <long, int> >(blocksPerOwner.OrderBy(pair => pair.Value)); // if we cannot identify an owner we allow the change if (sortedBpo.Count == 0) { _cachedOwners = new List <long>(allSmallOwners); return; } var bigOwner = sortedBpo[0].Key; List <IMySlimBlock> ownershipChangedBlocks = new List <IMySlimBlock>(); cubeGrid.GetBlocks(ownershipChangedBlocks, b => b.FatBlock != null && newOwners.Contains(b.FatBlock.OwnerId)); foreach (IMySlimBlock slimBlock in ownershipChangedBlocks) { var block = (Sandbox.Game.Entities.MyCubeBlock)slimBlock.FatBlock; // TODO check if the block was created/built just moments ago, do not change owner otherwise block.ChangeOwner(bigOwner, MyOwnershipShareModeEnum.None); ConnectionHelper.SendMessageToAllPlayers(new MessageSyncBlockOwner() { OwnerId = bigOwner, EntityId = block.EntityId }); // no need to update the cached owners as we don't want them to change } // TODO maybe allow the faction to build... }
private void LandingGearOnStateChanged(bool state) { if (!state) { return; } //private void LandingGearOnLockModeChanged(IMyLandingGear myLandingGear, SpaceEngineers.Game.ModAPI.Ingame.LandingGearMode landingGearMode) //{ // if (landingGearMode != SpaceEngineers.Game.ModAPI.Ingame.LandingGearMode.Locked) // return; if (ProtectionHandler.Config == null || !ProtectionHandler.Config.ProtectionEnabled || ProtectionHandler.Config.ProtectionAllowLandingGear) { return; } var ship = _landingGear.GetTopMostParent(typeof(IMyCubeGrid)); if (ship == null) { return; } IMyPlayer player = null; foreach (var workingCockpit in _landingGear.CubeGrid.FindWorkingCockpits()) { player = MyAPIGateway.Players.GetPlayerControllingEntity(workingCockpit.Entity); if (player != null) { break; } } var attachedEntity = _landingGear.GetAttachedEntity() as IMyCubeGrid; if (attachedEntity == null) { return; } if (!ProtectionHandler.IsProtected(attachedEntity)) { return; } if (player == null) { _landingGear.Unlock(); // we turn it off to prevent 'spamming' _landingGear.Enabled = false; return; } if (ProtectionHandler.CanModify(player, attachedEntity)) { return; } _landingGear.Unlock(); // we turn it off to prevent 'spamming' _landingGear.Enabled = false; }
public override void ProcessClient() { ProtectionHandler.InitOrUpdateClient(Config); }