void GetSettings() { if (IsServer) { // Server side, get from local drive Logger.Log.Info("Server get settings."); if ((Settings = Database.Instance.GetData <Settings>("settings", typeof(Settings))) == null) { Settings = new Settings(); } } else { // request settings from server. TimeSpan ts = DateTime.Now - m_settingRequested; if (MyAPIGateway.Session.Player != null && ts.TotalSeconds > 10) // request settings every 10 secs if there no response { Logger.Log.Info("Request settings from server!"); SyncPacket packet = new SyncPacket(); packet.proto = SyncPacket.Version; packet.request = true; packet.command = (ushort)Command.SettingsSync; packet.steamId = MyAPIGateway.Session.Player.SteamUserId; SendMessageToServer(packet); // send request only to server m_settingRequested = DateTime.Now; } } }
public static void SendSettingsToServer(Settings settings, ulong steamId) { SyncPacket newpacket = new SyncPacket(); newpacket.proto = SyncPacket.Version; newpacket.request = false; newpacket.command = (ushort)Command.SettingsChange; newpacket.steamId = steamId; newpacket.settings = settings; SendMessageToServer(newpacket); // send only to server }
public static void SendMessageToServer(SyncPacket package) { try { string text = MyAPIGateway.Utilities.SerializeToXML <SyncPacket>(package); MyAPIGateway.Multiplayer.SendMessageToServer(SyncPacket.SyncPacketID, System.Text.Encoding.Unicode.GetBytes(text)); } catch (Exception ex) { Logger.Log.Error("Exception in BeaconSecurity.SendMessageToServer(): {0}", ex.Message); } }
public void RequestEnable(bool enable) { Logger.Log.Debug("BeaconSecurity.RequestEnable() enable={0}", enable); // change status on server side block m_block.RequestEnable(enable); // send packet... SyncPacket packet = new SyncPacket(); packet.proto = SyncPacket.Version; packet.command = (ushort)((enable) ? Command.SyncOn : Command.SyncOff); packet.ownerId = m_block.OwnerId; packet.entityId = Entity.EntityId; Core.SendMessage(packet); }
public static void SendMessage(SyncPacket package, ulong steamId = 0) { try { string text = MyAPIGateway.Utilities.SerializeToXML <SyncPacket>(package); Byte[] message = System.Text.Encoding.Unicode.GetBytes(text); if (steamId != 0) { MyAPIGateway.Multiplayer.SendMessageTo(SyncPacket.SyncPacketID, message, steamId); } else { MyAPIGateway.Multiplayer.SendMessageToOthers(SyncPacket.SyncPacketID, message); } } catch (Exception ex) { Logger.Log.Error("Exception in BeaconSecurity.SendMessage(): {0}", ex.Message); } }
void GetSettings() { if (IsServer) { // Server side, get from local drive Logger.Log.Info("Server get settings."); if ((Settings = Database.Instance.GetData<Settings>("settings", typeof(Settings))) == null) Settings = new Settings(); } else { // request settings from server. TimeSpan ts = DateTime.Now - m_settingRequested; if (MyAPIGateway.Session.Player != null && ts.TotalSeconds > 10) // request settings every 10 secs if there no response { Logger.Log.Info("Request settings from server!"); SyncPacket packet = new SyncPacket(); packet.proto = SyncPacket.Version; packet.request = true; packet.command = (ushort)Command.SettingsSync; packet.steamId = MyAPIGateway.Session.Player.SteamUserId; SendMessageToServer(packet); // send request only to server m_settingRequested = DateTime.Now; } } }
// // == SessionComponent Hooks // public override void UpdateBeforeSimulation() { try { if (MyAPIGateway.Session == null || MyAPIGateway.Utilities == null || MyAPIGateway.Multiplayer == null) // exit if api is not ready return; if (!Inited) // init and set handlers Init(); if (Settings == null) // request or load setting GetSettings(); // if server, settings will be set a this call, so it safe if (!IsServer) // if client exit at this point. Cleaning works only on server side. return; if (Frame++ % 10 == 0) { HashSet<IMyEntity> entities = new HashSet<IMyEntity>(); MyAPIGateway.Entities.GetEntities(entities, x => x is IMyCubeGrid); foreach (IMyEntity entity in entities) { IMyCubeGrid grid = entity as IMyCubeGrid; if (grid == null) continue; // if this grid have an active BS, then turn off destructible for grid gridDestructible(grid, !isActiveBeaconSecurity(grid)); if (!eventedGrids.Contains(grid)) { // skip if already added actions grid.OnBlockAdded += BuildHandler.grid_OnBlockAdded; eventedGrids.Add(grid); Logger.Log.Debug("OnBlockAdded event added to {0} grids.", grid.EntityId); } } // Remove block from queues foreach (var block in queueRemoveBlocks) { IMyCubeGrid grid = block.CubeGrid as IMyCubeGrid; if (grid == null) continue; grid.RemoveBlock(block, true); if (block.FatBlock != null) block.FatBlock.Close(); } queueRemoveBlocks.Clear(); } if (Settings.CleaningFrequency == 0) return; #region Cleanup if ((DateTime.Now - m_cleaning).TotalSeconds > Settings.CleaningFrequency) // Cleaning by frequency { Logger.Log.Debug("Time for cleaning, from last {0} secs elapsed.", (DateTime.Now - m_cleaning).TotalSeconds); m_cleaning = DateTime.Now; HashSet<IMyEntity> entities = new HashSet<IMyEntity>(); MyAPIGateway.Entities.GetEntities(entities, x => x is IMyCubeGrid); int removed = 0; List<IMySlimBlock> ToRemove = new List<IMySlimBlock>(); HashSet<long> AlreadyCounted = new HashSet<long>(); Dictionary<long, long[]> PerFactionCount = new Dictionary<long, long[]>(); Dictionary<long, long[]> PerPlayerCount = new Dictionary<long, long[]>(); foreach (IMyCubeGrid grid in entities) { var blocks = new List<IMySlimBlock>(); grid.GetBlocks(blocks, x => x.FatBlock != null); foreach (IMySlimBlock block in blocks) { //Logger.Log.Debug("Check block {0} {1}", block.FatBlock.DisplayNameText, block.FatBlock.ToString()); BeaconSecurity bs = block.FatBlock.GameLogic as BeaconSecurity; if (bs == null || !bs.IsBeaconSecurity || bs.OwnerId == 0) continue; if (!AlreadyCounted.Contains(block.FatBlock.EntityId)) AlreadyCounted.Add(block.FatBlock.EntityId); else continue; // Priority for players limit. if (!PerPlayerCount.ContainsKey(bs.OwnerId)) // if there no counts, add it PerPlayerCount[bs.OwnerId] = new long[] { 0, 0 }; PerPlayerCount[bs.OwnerId][0]++; if (bs.OwnerId != 0 && PerPlayerCount[bs.OwnerId][0] > Settings.LimitPerPlayer) { PerPlayerCount[bs.OwnerId][1]++; ToRemove.Add(block); continue; } IMyFaction faction = MyAPIGateway.Session.Factions.TryGetPlayerFaction(bs.OwnerId); if (faction != null) { if (!PerFactionCount.ContainsKey(faction.FactionId)) PerFactionCount[faction.FactionId] = new long[] { 0, 0 }; PerFactionCount[faction.FactionId][0]++; if (PerFactionCount[faction.FactionId][0] > Settings.LimitPerFaction) { PerFactionCount[faction.FactionId][1]++; ToRemove.Add(block); } } } foreach (IMySlimBlock block in ToRemove) grid.RemoveBlock(block, true); removed += ToRemove.Count; ToRemove.Clear(); } List<IMyPlayer> players = new List<IMyPlayer>(); MyAPIGateway.Players.GetPlayers(players); // Send warn message about cleaning and restrictions! foreach (long ownerId in PerPlayerCount.Keys) { if (ownerId == 0 || PerPlayerCount[ownerId][1] == 0) continue; IMyPlayer player = players.FirstOrDefault(x => x.PlayerID == ownerId); if (player == null) continue; SyncPacket pckOut = new SyncPacket(); pckOut.proto = SyncPacket.Version; pckOut.command = (ushort)Command.MessageToChat; pckOut.message = string.Format("Removed {0} entity as a result of limitations on the amount {1} per player.", PerPlayerCount[ownerId][1], Settings.LimitPerPlayer); Core.SendMessage(pckOut, player.SteamUserId); } foreach (long factionId in PerFactionCount.Keys) { if (PerFactionCount[factionId][1] == 0) continue; IMyFaction faction = MyAPIGateway.Session.Factions.TryGetFactionById(factionId); if (faction == null) continue; foreach (var member in faction.Members) { IMyPlayer player = players.FirstOrDefault(x => x.PlayerID == member.Key); if (player == null) continue; SyncPacket pckOut = new SyncPacket(); pckOut.proto = SyncPacket.Version; pckOut.command = (ushort)Command.MessageToChat; pckOut.message = string.Format("Removed {0} entity as a result of limitations on the amount {1} per faction.", PerFactionCount[factionId][1], Settings.LimitPerFaction); Core.SendMessage(pckOut, player.SteamUserId); } } if (removed > 0) Logger.Log.Info("Cleaning, totaly removed {0} entities.", removed); } #endregion Cleanup } catch (Exception ex) { Logger.Log.Error("EXCEPTION at BeaconSecurity.UpdateBeforeSimulation(): {0} {1}", ex.Message, ex.StackTrace); } }
public static void SendMessageToServer(SyncPacket package) { try { string text = MyAPIGateway.Utilities.SerializeToXML<SyncPacket>(package); MyAPIGateway.Multiplayer.SendMessageToServer(SyncPacket.SyncPacketID, System.Text.Encoding.Unicode.GetBytes(text)); } catch (Exception ex) { Logger.Log.Error("Exception in BeaconSecurity.SendMessageToServer(): {0}", ex.Message); } }
public static void SendMessage(SyncPacket package, ulong steamId = 0) { try { string text = MyAPIGateway.Utilities.SerializeToXML<SyncPacket>(package); Byte[] message = System.Text.Encoding.Unicode.GetBytes(text); if (steamId != 0) { MyAPIGateway.Multiplayer.SendMessageTo(SyncPacket.SyncPacketID, message, steamId); } else { MyAPIGateway.Multiplayer.SendMessageToOthers(SyncPacket.SyncPacketID, message); } } catch (Exception ex) { Logger.Log.Error("Exception in BeaconSecurity.SendMessage(): {0}", ex.Message); } }
public static void ChatMessageEntered(string messageText, ref bool sendToOthers) { Logger.Log.Debug("ChatEvents.ChatMessageEntered: {0}", messageText); if (messageText.StartsWith("/bs", StringComparison.OrdinalIgnoreCase)) { if (messageText.Length == 3) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, String.Format("Beacon Security is {0}. {1} Current version: {2}", (Core.Settings.Enabled) ? "On" : "Off", (Core.Settings.OnlyForStations) ? "Only for stations." : "", SyncPacket.Version)); } string[] commands = (messageText.Remove(0, 3)).Split(null as string[], 2, StringSplitOptions.RemoveEmptyEntries); if (commands.Length > 0) { string internalCommand = commands[0]; string arguments = (commands.Length > 1) ? commands[1] : ""; Logger.Log.Debug("internalCommand: {0} arguments {1}", internalCommand, arguments); if (internalCommand.Equals("help", StringComparison.OrdinalIgnoreCase)) { #region help var index = 0; try { index = Int32.Parse(arguments); } catch { } if (index < 1 || index > 10) { MyAPIGateway.Utilities.ShowMissionScreen("Help", "Beacon ", "Security", BSHELP); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, BSHELP_CHAT); } else { MyAPIGateway.Utilities.ShowMissionScreen("Help: explanation of variables", "Beacon ", "Security", BSHELP_VARIABLES[index - 1]); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, BSHELP_VARIABLES[index - 1]); } #endregion help } else if (Core.Settings != null) { if (internalCommand.Equals("config", StringComparison.OrdinalIgnoreCase)) { #region config MyAPIGateway.Utilities.ShowMissionScreen("Config", "Beacon ", "Security", String.Format(@"1) DelayBeforeTurningOn(0-3600,D:120) = {0} 2) DistanceBeforeTurningOn(0-10000,D:400) = {1} 3) OnlyForStations(on/off,D:off) = {2} 4) OnlyWithZeroSpeed(on/off,D:on) = {3} 5) BuildingNotAllowed(on/off,D:on) = {4} 6) IndestructibleNoBuilds(on/off,D:on) = {5} 7) IndestructibleGrindOwner(on/off,D:on) = {6} 8) LimitGridSizes(0-1000,D:150) = {7} 9) LimitPerFaction(1-100,D:30) = {8} 10) LimitPerPlayer(1-100,D:3) = {9} 11) CleaningFrequency(0-3600,D:5) = {10} ", Core.Settings.DelayBeforeTurningOn, Core.Settings.DistanceBeforeTurningOn, Core.Settings.OnlyForStations, Core.Settings.OnlyWithZeroSpeed, Core.Settings.BuildingNotAllowed, Core.Settings.IndestructibleNoBuilds, Core.Settings.IndestructibleGrindOwner, Core.Settings.LimitGridSizes, Core.Settings.LimitPerFaction, Core.Settings.LimitPerPlayer, Core.Settings.CleaningFrequency )); #endregion config } if (Core.IsServer || Core.IsAdmin(MyAPIGateway.Session.Player)) { #region alladminsettings if (internalCommand.Equals("on", StringComparison.OrdinalIgnoreCase)) { Core.Settings.Enabled = true; Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); SyncPacket newpacket = new SyncPacket(); newpacket.proto = SyncPacket.Version; newpacket.command = (ushort)Command.MessageToChat; newpacket.message = "Beacon Security is ON."; Core.SendMessage(newpacket); // send to others } else if (internalCommand.Equals("off", StringComparison.OrdinalIgnoreCase)) { Core.Settings.Enabled = false; Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); SyncPacket newpacket = new SyncPacket(); newpacket.proto = SyncPacket.Version; newpacket.command = (ushort)Command.MessageToChat; newpacket.message = "Beacon Security is OFF."; Core.SendMessage(newpacket); // send to others } else if (internalCommand.Equals("debug", StringComparison.OrdinalIgnoreCase)) { Core.Settings.Debug = !Core.Settings.Debug; Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Debug logging is {0}", (Core.Settings.Debug) ? "ON" : "OFF")); } else if (internalCommand.Equals("list", StringComparison.OrdinalIgnoreCase)) { List <string> GridNames = new List <string>(); List <string> GridNamesNotFound = new List <string>(); foreach (long entId in Core.Settings.Indestructible) { string gridName = GetGridName(entId); if (gridName != null) { GridNames.Add(string.Format("'{0}'{1}{2}", gridName, Core.Settings.IndestructibleOverrideBuilds.Contains(entId) ? "[BO]" : "", Core.Settings.IndestructibleOverrideGrindOwner.Contains(entId) ? "[GO]" : "")); } else { GridNamesNotFound.Add(string.Format("id[{0}]{1}{2}", entId, Core.Settings.IndestructibleOverrideBuilds.Contains(entId) ? "[BO]" : "", Core.Settings.IndestructibleOverrideGrindOwner.Contains(entId) ? "[GO]" : "")); } } string list = String.Join(", ", GridNames.ToArray()); string nflist = String.Join(", ", GridNamesNotFound.ToArray()); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Indestructible list: {0}", (GridNames.Count > 0) ? list : "[EMPTY]")); if (GridNamesNotFound.Count > 0) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Currently not found: {0}", nflist)); } } else if (internalCommand.Equals("add", StringComparison.OrdinalIgnoreCase)) { if (arguments.Length > 0) { long entId = 0; if (!long.TryParse(arguments, out entId)) { entId = GetGridEntityId(arguments); } if (entId > 0) { if (Core.Settings.Indestructible.Contains(entId)) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Can't add grid name '{0}', already in list...", arguments)); } else { Core.Settings.Indestructible.Add(entId); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' added.", arguments)); } } else { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found...", arguments)); } } else { List <string> added = new List <string>(); foreach (string gridname in m_lastFound) { long entId = GetGridEntityId(gridname); if (entId <= 0) { continue; } if (!Core.Settings.Indestructible.Contains(entId)) { added.Add(gridname); Core.Settings.Indestructible.Add(entId); } } string list = String.Join(", ", added.ToArray()); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Added {0} grid names: {1}", added.Count, list)); } } else if (internalCommand.Equals("rem", StringComparison.OrdinalIgnoreCase) || internalCommand.Equals("remove", StringComparison.OrdinalIgnoreCase) || internalCommand.Equals("del", StringComparison.OrdinalIgnoreCase)) { if (arguments.Length > 0) { long entId = 0; if (!long.TryParse(arguments, out entId)) { entId = GetGridEntityId(arguments); } if (entId > 0) { if (!Core.Settings.Indestructible.Contains(entId)) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found in list...", arguments)); } else { if (Core.Settings.IndestructibleOverrideBuilds.Contains(entId)) { Core.Settings.IndestructibleOverrideBuilds.Remove(entId); } Core.Settings.Indestructible.Remove(entId); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' removed.", arguments)); } } else { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found...", arguments)); } } else { List <string> removed = new List <string>(); foreach (string gridname in m_lastFound) { long entId = GetGridEntityId(gridname); if (entId <= 0) { continue; } if (Core.Settings.Indestructible.Contains(entId)) { removed.Add(gridname); Core.Settings.Indestructible.Remove(entId); } } string list = String.Join(", ", removed.ToArray()); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Removed {0} grid names: {1}", removed.Count, list)); } } else if (internalCommand.Equals("bo", StringComparison.OrdinalIgnoreCase) || internalCommand.Equals("buildon", StringComparison.OrdinalIgnoreCase)) { if (arguments.Length > 0) { long entId = 0; if (!long.TryParse(arguments, out entId)) { entId = GetGridEntityId(arguments); } if (entId > 0) { if (!Core.Settings.Indestructible.Contains(entId)) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found in list...", arguments)); } else { if (Core.Settings.IndestructibleOverrideBuilds.Contains(entId)) { Core.Settings.IndestructibleOverrideBuilds.Remove(entId); } else { Core.Settings.IndestructibleOverrideBuilds.Add(entId); } Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("For grid name '{0}' building is override {1}.", arguments, Core.Settings.IndestructibleOverrideBuilds.Contains(entId))); } } else { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found...", arguments)); } } else { List <string> marked = new List <string>(); foreach (string gridname in m_lastFound) { long entId = GetGridEntityId(gridname); if (entId <= 0) { continue; } if (Core.Settings.Indestructible.Contains(entId)) { if (Core.Settings.IndestructibleOverrideBuilds.Contains(entId)) { Core.Settings.IndestructibleOverrideBuilds.Remove(entId); } else { Core.Settings.IndestructibleOverrideBuilds.Add(entId); } marked.Add(gridname); } } string list = String.Join(", ", marked.ToArray()); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Building is override {0} grid names: {1}", marked.Count, list)); } } else if (internalCommand.Equals("go", StringComparison.OrdinalIgnoreCase) || internalCommand.Equals("grindon", StringComparison.OrdinalIgnoreCase)) { if (arguments.Length > 0) { long entId = 0; if (!long.TryParse(arguments, out entId)) { entId = GetGridEntityId(arguments); } if (entId > 0) { if (!Core.Settings.Indestructible.Contains(entId)) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found in list...", arguments)); } else { if (Core.Settings.IndestructibleOverrideGrindOwner.Contains(entId)) { Core.Settings.IndestructibleOverrideGrindOwner.Remove(entId); } else { Core.Settings.IndestructibleOverrideGrindOwner.Add(entId); } Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("For grid name '{0}' grind own property is override {1}.", arguments, Core.Settings.IndestructibleOverrideGrindOwner.Contains(entId))); } } else { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found...", arguments)); } } else { List <string> marked = new List <string>(); foreach (string gridname in m_lastFound) { long entId = GetGridEntityId(gridname); if (entId <= 0) { continue; } if (Core.Settings.Indestructible.Contains(entId)) { if (Core.Settings.IndestructibleOverrideGrindOwner.Contains(entId)) { Core.Settings.IndestructibleOverrideGrindOwner.Remove(entId); } else { Core.Settings.IndestructibleOverrideGrindOwner.Add(entId); } marked.Add(gridname); } } string list = String.Join(", ", marked.ToArray()); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grind own property is override {0} grid names: {1}", marked.Count, list)); } } else if (internalCommand.Equals("clear", StringComparison.OrdinalIgnoreCase)) { Core.Settings.Indestructible.Clear(); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, "Indestructible list cleared."); } else if (internalCommand.Equals("find", StringComparison.OrdinalIgnoreCase)) { try { uint value = (arguments.Length > 0) ? UInt32.Parse(arguments) : 10; if (value < 1) { value = 1; } if (value > 100000) { value = 100000; } Vector3D pos = MyAPIGateway.Session.Player.GetPosition(); BoundingSphereD sphere = new BoundingSphereD(pos, value); List <IMyEntity> entities = MyAPIGateway.Entities.GetEntitiesInSphere(ref sphere); string[] found = entities.FindAll(x => x is IMyCubeGrid).Select(x => (x as IMyCubeGrid).DisplayName).ToArray(); string foundedList = String.Join(", ", found); m_lastFound.Clear(); m_lastFound.AddRange(found); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("In radius {0}m found: {1}", value, foundedList)); } catch (Exception ex) { Logger.Log.Error("Exception in command find: {0}", ex.Message); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Exception: {0}", ex.Message)); } } else if (internalCommand.Equals("set", StringComparison.OrdinalIgnoreCase)) { #region setcommand string ResultMessage = ""; string[] argument = (arguments).Split(null as string[], 2, StringSplitOptions.RemoveEmptyEntries); if (argument.Length >= 2) { bool changed = false; if (argument[0].Equals("1") || argument[0].Equals("DelayBeforeTurningOn", StringComparison.OrdinalIgnoreCase)) { try { ushort value = UInt16.Parse(argument[1]); if (value < 0 || value > 3600) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("The value is not within the allowed limits. [ 0 - 3600 ]", value)); } else { Core.Settings.DelayBeforeTurningOn = value; changed = true; ResultMessage = string.Format("DelayBeforeTurningOn changed to {0}", value); } } catch (Exception ex) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Incorrect number. {0}", ex.Message)); } } else if (argument[0].Equals("2") || argument[0].Equals("DistanceBeforeTurningOn", StringComparison.OrdinalIgnoreCase)) { try { ushort value = UInt16.Parse(argument[1]); if (value < 0 || value > 10000) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("The value is not within the allowed limits. [ 0 - 10000 ]", value)); } else { Core.Settings.DistanceBeforeTurningOn = value; changed = true; ResultMessage = string.Format("DistanceBeforeTurningOn changed to {0}", value); } } catch (Exception ex) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Incorrect number. {0}", ex.Message)); } } else if (argument[0].Equals("3") || argument[0].Equals("OnlyForStations", StringComparison.OrdinalIgnoreCase)) { if (argument[1].Equals("on", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("true", StringComparison.OrdinalIgnoreCase)) { Core.Settings.OnlyForStations = true; changed = true; } else if (argument[1].Equals("off", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("false", StringComparison.OrdinalIgnoreCase)) { Core.Settings.OnlyForStations = false; changed = true; } ResultMessage = string.Format("OnlyForStations changed to {0}", (Core.Settings.OnlyForStations) ? "On" : "Off"); } else if (argument[0].Equals("4") || argument[0].Equals("OnlyWithZeroSpeed", StringComparison.OrdinalIgnoreCase)) { if (argument[1].Equals("on", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("true", StringComparison.OrdinalIgnoreCase)) { Core.Settings.OnlyWithZeroSpeed = true; changed = true; } else if (argument[1].Equals("off", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("false", StringComparison.OrdinalIgnoreCase)) { Core.Settings.OnlyWithZeroSpeed = false; changed = true; } ResultMessage = string.Format("OnlyWithZeroSpeed changed to {0}", (Core.Settings.OnlyWithZeroSpeed) ? "On" : "Off"); } else if (argument[0].Equals("5") || argument[0].Equals("BuildingNotAllowed", StringComparison.OrdinalIgnoreCase)) { if (argument[1].Equals("on", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("true", StringComparison.OrdinalIgnoreCase)) { Core.Settings.BuildingNotAllowed = true; changed = true; } else if (argument[1].Equals("off", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("false", StringComparison.OrdinalIgnoreCase)) { Core.Settings.BuildingNotAllowed = false; changed = true; } ResultMessage = string.Format("BuildingNotAllowed changed to {0}", (Core.Settings.BuildingNotAllowed) ? "On" : "Off"); } else if (argument[0].Equals("6") || argument[0].Equals("IndestructibleNoBuilds", StringComparison.OrdinalIgnoreCase)) { if (argument[1].Equals("on", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("true", StringComparison.OrdinalIgnoreCase)) { Core.Settings.IndestructibleNoBuilds = true; changed = true; } else if (argument[1].Equals("off", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("false", StringComparison.OrdinalIgnoreCase)) { Core.Settings.IndestructibleNoBuilds = false; changed = true; } ResultMessage = string.Format("IndestructibleNoBuilds changed to {0}", (Core.Settings.IndestructibleNoBuilds) ? "On" : "Off"); } else if (argument[0].Equals("7") || argument[0].Equals("IndestructibleGrindOwner", StringComparison.OrdinalIgnoreCase)) { if (argument[1].Equals("on", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("true", StringComparison.OrdinalIgnoreCase)) { Core.Settings.IndestructibleGrindOwner = true; changed = true; } else if (argument[1].Equals("off", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("false", StringComparison.OrdinalIgnoreCase)) { Core.Settings.IndestructibleGrindOwner = false; changed = true; } ResultMessage = string.Format("IndestructibleGrindOwner changed to {0}", (Core.Settings.IndestructibleGrindOwner) ? "On" : "Off"); } else if (argument[0].Equals("8") || argument[0].Equals("LimitGridSizes", StringComparison.OrdinalIgnoreCase)) { try { ushort value = UInt16.Parse(argument[1]); if (value < 0 || value > 1000) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("The value is not within the allowed limits. [ 0 - 1000 ]", value)); } else { Core.Settings.LimitGridSizes = value; changed = true; ResultMessage = string.Format("LimitGridSizes changed to {0}", value); } } catch (Exception ex) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Incorrect number. {0}", ex.Message)); } } else if (argument[0].Equals("9") || argument[0].Equals("LimitPerFaction", StringComparison.OrdinalIgnoreCase)) { try { ushort value = UInt16.Parse(argument[1]); if (value < 1 || value > 100) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("The value is not within the allowed limits. [ 1 - 100 ]", value)); } else { Core.Settings.LimitPerFaction = value; changed = true; ResultMessage = string.Format("LimitPerFaction changed to {0}", value); } } catch (Exception ex) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Incorrect number. {0}", ex.Message)); } } else if (argument[0].Equals("10") || argument[0].Equals("LimitPerPlayer", StringComparison.OrdinalIgnoreCase)) { try { ushort value = UInt16.Parse(argument[1]); if (value < 1 || value > 100) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("The value is not within the allowed limits. [ 1 - 100 ]", value)); } else { Core.Settings.LimitPerPlayer = value; changed = true; ResultMessage = string.Format("LimitPerPlayer changed to {0}", value); } } catch (Exception ex) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Incorrect number. {0}", ex.Message)); } } else if (argument[0].Equals("11") || argument[0].Equals("CleaningFrequency", StringComparison.OrdinalIgnoreCase)) { try { ushort value = UInt16.Parse(argument[1]); if (value < 0 || value > 3600) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("The value is not within the allowed limits. [ 0 - 3600 ]", value)); } else { Core.Settings.CleaningFrequency = value; changed = true; ResultMessage = string.Format("CleaningFrequency changed to {0}", value); } } catch (Exception ex) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Incorrect number. {0}", ex.Message)); } } if (changed) { Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); SyncPacket newpacket = new SyncPacket(); newpacket.proto = SyncPacket.Version; newpacket.command = (ushort)Command.MessageToChat; newpacket.message = ResultMessage; Core.SendMessage(newpacket); // send to others } if (!Core.IsServer) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, ResultMessage); } } else { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, "Incorrect syntax for command set. Use: /bs set var value"); } #endregion setcommand } #endregion alladminsettings } else { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, "Only an administrator can change the settings..."); } } } sendToOthers = false; } }
// // == SessionComponent Hooks // public override void UpdateBeforeSimulation() { try { if (MyAPIGateway.Session == null || MyAPIGateway.Utilities == null || MyAPIGateway.Multiplayer == null) // exit if api is not ready { return; } if (!Inited) // init and set handlers { Init(); } if (Settings == null) // request or load setting { GetSettings(); // if server, settings will be set a this call, so it safe } if (!IsServer) // if client exit at this point. Cleaning works only on server side. { return; } if (Frame++ % 10 == 0) { HashSet <IMyEntity> entities = new HashSet <IMyEntity>(); MyAPIGateway.Entities.GetEntities(entities, x => x is IMyCubeGrid); foreach (IMyEntity entity in entities) { IMyCubeGrid grid = entity as IMyCubeGrid; if (grid == null) { continue; } // if this grid have an active BS, then turn off destructible for grid gridDestructible(grid, !isActiveBeaconSecurity(grid)); if (!eventedGrids.Contains(grid)) { // skip if already added actions grid.OnBlockAdded += BuildHandler.grid_OnBlockAdded; eventedGrids.Add(grid); Logger.Log.Debug("OnBlockAdded event added to {0} grids.", grid.EntityId); } } // Remove block from queues foreach (var block in queueRemoveBlocks) { IMyCubeGrid grid = block.CubeGrid as IMyCubeGrid; if (grid == null) { continue; } grid.RemoveBlock(block, true); if (block.FatBlock != null) { block.FatBlock.Close(); } } queueRemoveBlocks.Clear(); } if (Settings.CleaningFrequency == 0) { return; } #region Cleanup if ((DateTime.Now - m_cleaning).TotalSeconds > Settings.CleaningFrequency) // Cleaning by frequency { Logger.Log.Debug("Time for cleaning, from last {0} secs elapsed.", (DateTime.Now - m_cleaning).TotalSeconds); m_cleaning = DateTime.Now; HashSet <IMyEntity> entities = new HashSet <IMyEntity>(); MyAPIGateway.Entities.GetEntities(entities, x => x is IMyCubeGrid); int removed = 0; List <IMySlimBlock> ToRemove = new List <IMySlimBlock>(); HashSet <long> AlreadyCounted = new HashSet <long>(); Dictionary <long, long[]> PerFactionCount = new Dictionary <long, long[]>(); Dictionary <long, long[]> PerPlayerCount = new Dictionary <long, long[]>(); foreach (IMyCubeGrid grid in entities) { var blocks = new List <IMySlimBlock>(); grid.GetBlocks(blocks, x => x.FatBlock != null); foreach (IMySlimBlock block in blocks) { //Logger.Log.Debug("Check block {0} {1}", block.FatBlock.DisplayNameText, block.FatBlock.ToString()); BeaconSecurity bs = block.FatBlock.GameLogic as BeaconSecurity; if (bs == null || !bs.IsBeaconSecurity || bs.OwnerId == 0) { continue; } if (!AlreadyCounted.Contains(block.FatBlock.EntityId)) { AlreadyCounted.Add(block.FatBlock.EntityId); } else { continue; } // Priority for players limit. if (!PerPlayerCount.ContainsKey(bs.OwnerId)) // if there no counts, add it { PerPlayerCount[bs.OwnerId] = new long[] { 0, 0 } } ; PerPlayerCount[bs.OwnerId][0]++; if (bs.OwnerId != 0 && PerPlayerCount[bs.OwnerId][0] > Settings.LimitPerPlayer) { PerPlayerCount[bs.OwnerId][1]++; ToRemove.Add(block); continue; } IMyFaction faction = MyAPIGateway.Session.Factions.TryGetPlayerFaction(bs.OwnerId); if (faction != null) { if (!PerFactionCount.ContainsKey(faction.FactionId)) { PerFactionCount[faction.FactionId] = new long[] { 0, 0 } } ; PerFactionCount[faction.FactionId][0]++; if (PerFactionCount[faction.FactionId][0] > Settings.LimitPerFaction) { PerFactionCount[faction.FactionId][1]++; ToRemove.Add(block); } } } foreach (IMySlimBlock block in ToRemove) { grid.RemoveBlock(block, true); } removed += ToRemove.Count; ToRemove.Clear(); } List <IMyPlayer> players = new List <IMyPlayer>(); MyAPIGateway.Players.GetPlayers(players); // Send warn message about cleaning and restrictions! foreach (long ownerId in PerPlayerCount.Keys) { if (ownerId == 0 || PerPlayerCount[ownerId][1] == 0) { continue; } IMyPlayer player = players.FirstOrDefault(x => x.PlayerID == ownerId); if (player == null) { continue; } SyncPacket pckOut = new SyncPacket(); pckOut.proto = SyncPacket.Version; pckOut.command = (ushort)Command.MessageToChat; pckOut.message = string.Format("Removed {0} entity as a result of limitations on the amount {1} per player.", PerPlayerCount[ownerId][1], Settings.LimitPerPlayer); Core.SendMessage(pckOut, player.SteamUserId); } foreach (long factionId in PerFactionCount.Keys) { if (PerFactionCount[factionId][1] == 0) { continue; } IMyFaction faction = MyAPIGateway.Session.Factions.TryGetFactionById(factionId); if (faction == null) { continue; } foreach (var member in faction.Members) { IMyPlayer player = players.FirstOrDefault(x => x.PlayerID == member.Key); if (player == null) { continue; } SyncPacket pckOut = new SyncPacket(); pckOut.proto = SyncPacket.Version; pckOut.command = (ushort)Command.MessageToChat; pckOut.message = string.Format("Removed {0} entity as a result of limitations on the amount {1} per faction.", PerFactionCount[factionId][1], Settings.LimitPerFaction); Core.SendMessage(pckOut, player.SteamUserId); } } if (removed > 0) { Logger.Log.Info("Cleaning, totaly removed {0} entities.", removed); } } #endregion Cleanup } catch (Exception ex) { Logger.Log.Error("EXCEPTION at BeaconSecurity.UpdateBeforeSimulation(): {0} {1}", ex.Message, ex.StackTrace); } }
public static void OnSyncRequest(byte[] bytes) { try { Logger.Log.Debug("BeaconSecurity.OnSyncRequest() - starts"); SyncPacket pckIn = new SyncPacket(); string data = System.Text.Encoding.Unicode.GetString(bytes); //Logger.Log.Debug(@"*******************************\n{0}\n*******************************\n", data); pckIn = MyAPIGateway.Utilities.SerializeFromXML <SyncPacket>(data); Logger.Log.Info("OnSyncRequest COMMAND:{0}, id:{1}, entity:'{2}', steamid: {3}, isserver: {4}", Enum.GetName(typeof(Command), pckIn.command), pckIn.ownerId, pckIn.entityId, pckIn.steamId, Core.IsServer); if (pckIn.proto != SyncPacket.Version) { Logger.Log.Error("Wrong version of sync protocol client [{0}] <> [{1}] server", SyncPacket.Version, pckIn.proto); MyAPIGateway.Utilities.ShowNotification("Sync Protocol version mismatch! Try to restart game or server!", 5000, MyFontEnum.Red); return; } switch ((Command)pckIn.command) { case Command.MessageToChat: { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, pckIn.message); break; } case Command.SettingsSync: { if (pckIn.request) // Settings sync request { if (Core.IsServer && Core.Settings != null) { // server send settings to client Logger.Log.Info("Send sync packet with settings to user steamId {0}", pckIn.steamId); SyncPacket pckOut = new SyncPacket(); pckOut.proto = SyncPacket.Version; pckOut.request = false; pckOut.command = (ushort)Command.SettingsSync; pckOut.steamId = pckIn.steamId; pckOut.settings = Core.Settings; Core.SendMessage(pckOut, pckIn.steamId); } } else { if (!Core.IsServer) { // setting sync only for clients Logger.Log.Info("User config synced..."); // if settings changes or syncs... Core.setSettings(pckIn.settings); if (pckIn.steamId == 0) // if steamid is zero, so we updating for all clients and notify this message { MyAPIGateway.Utilities.ShowNotification("Beacon Security settings has been updated!", 2000, MyFontEnum.Green); } } } break; } case Command.SettingsChange: { if (Core.IsServer) // Only server can acccept this message { Logger.Log.Info("Some one with steamid={0} trying to change server settings", pckIn.steamId); if (Core.IsAdmin(pckIn.steamId) || pckIn.steamId == MyAPIGateway.Session.Player.SteamUserId) { Logger.Log.Info("Server config changed by steamId {0}", pckIn.steamId); Core.setSettings(pckIn.settings); // resend for all clients a new settings SyncPacket newpacket = new SyncPacket(); newpacket.proto = SyncPacket.Version; newpacket.request = false; newpacket.command = (ushort)Command.SettingsSync; newpacket.steamId = 0; // for all newpacket.settings = Core.Settings; Core.SendMessage(newpacket); } } break; } case Command.SyncOff: { IMyEntity entity; MyAPIGateway.Entities.TryGetEntityById(pckIn.entityId, out entity); if (entity == null) { break; } Logger.Log.Debug("SyncOff found entity id {0}", pckIn.entityId); BeaconSecurity bs = entity.GameLogic as BeaconSecurity; if (bs == null || !bs.IsBeaconSecurity) { break; } Logger.Log.Debug(" * entity is BeaconSecurity"); if (entity is IMyFunctionalBlock) { (entity as IMyFunctionalBlock).RequestEnable(false); } IMyPlayer player = MyAPIGateway.Session.Player; if (player != null) // check this for dedicated servers { MyRelationsBetweenPlayerAndBlock relation = (entity as IMyFunctionalBlock).GetUserRelationToOwner(player.PlayerID); if (relation == MyRelationsBetweenPlayerAndBlock.Owner || relation == MyRelationsBetweenPlayerAndBlock.FactionShare) { // if player has rights to this beacon, show message MyAPIGateway.Utilities.ShowNotification(string.Format("{0} beacon security called '{1}' is deactivated now!", (relation == MyRelationsBetweenPlayerAndBlock.FactionShare) ? "Faction" : "Your", (entity.GameLogic as BeaconSecurity).DisplayName), 5000, MyFontEnum.Red); } } break; } case Command.SyncOn: { IMyEntity entity; MyAPIGateway.Entities.TryGetEntityById(pckIn.entityId, out entity); if (entity == null) { break; } Logger.Log.Debug("SyncOn found entity id {0}", pckIn.entityId); BeaconSecurity bs = entity.GameLogic as BeaconSecurity; if (bs == null || !bs.IsBeaconSecurity) { break; } Logger.Log.Debug(" * entity is BeaconSecurity"); if (entity is IMyFunctionalBlock) { (entity as IMyFunctionalBlock).RequestEnable(true); } IMyPlayer player = MyAPIGateway.Session.Player; if (player != null) // check this for dedicated servers { MyRelationsBetweenPlayerAndBlock relation = (entity as IMyFunctionalBlock).GetUserRelationToOwner(player.PlayerID); if (relation == MyRelationsBetweenPlayerAndBlock.Owner || relation == MyRelationsBetweenPlayerAndBlock.FactionShare) { // if player has rights to this beacon, show message MyAPIGateway.Utilities.ShowNotification(string.Format("{0} beacon security called '{1}' is activated now!", (relation == MyRelationsBetweenPlayerAndBlock.FactionShare) ? "Faction" : "Your", (entity.GameLogic as BeaconSecurity).DisplayName), 5000, MyFontEnum.Green); } } break; } default: { break; } } } catch (Exception ex) { Logger.Log.Error("Exception at BeaconSecurity.OnSyncRequest(): {0}", ex.Message); return; } }
public static void ChatMessageEntered(string messageText, ref bool sendToOthers) { Logger.Log.Debug("ChatEvents.ChatMessageEntered: {0}", messageText); if (messageText.StartsWith("/bs", StringComparison.OrdinalIgnoreCase)) { if (messageText.Length == 3) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, String.Format("Beacon Security is {0}. {1} Current version: {2}", (Core.Settings.Enabled) ? "On" : "Off", (Core.Settings.OnlyForStations) ? "Only for stations." : "", SyncPacket.Version)); } string[] commands = (messageText.Remove(0, 3)).Split(null as string[], 2, StringSplitOptions.RemoveEmptyEntries); if (commands.Length > 0) { string internalCommand = commands[0]; string arguments = (commands.Length > 1) ? commands[1] : ""; Logger.Log.Debug("internalCommand: {0} arguments {1}", internalCommand, arguments); if (internalCommand.Equals("help", StringComparison.OrdinalIgnoreCase)) { #region help var index = 0; try { index = Int32.Parse(arguments); } catch { } if (index < 1 || index > 10) { MyAPIGateway.Utilities.ShowMissionScreen("Help", "Beacon ", "Security", BSHELP); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, BSHELP_CHAT); } else { MyAPIGateway.Utilities.ShowMissionScreen("Help: explanation of variables", "Beacon ", "Security", BSHELP_VARIABLES[index - 1]); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, BSHELP_VARIABLES[index - 1]); } #endregion help } else if (Core.Settings != null) { if (internalCommand.Equals("config", StringComparison.OrdinalIgnoreCase)) { #region config MyAPIGateway.Utilities.ShowMissionScreen("Config", "Beacon ", "Security", String.Format(@"1) DelayBeforeTurningOn(0-3600,D:120) = {0} 2) DistanceBeforeTurningOn(0-10000,D:400) = {1} 3) OnlyForStations(on/off,D:off) = {2} 4) OnlyWithZeroSpeed(on/off,D:on) = {3} 5) BuildingNotAllowed(on/off,D:on) = {4} 6) IndestructibleNoBuilds(on/off,D:on) = {5} 7) IndestructibleGrindOwner(on/off,D:on) = {6} 8) LimitGridSizes(0-1000,D:150) = {7} 9) LimitPerFaction(1-100,D:30) = {8} 10) LimitPerPlayer(1-100,D:3) = {9} 11) CleaningFrequency(0-3600,D:5) = {10} ", Core.Settings.DelayBeforeTurningOn, Core.Settings.DistanceBeforeTurningOn, Core.Settings.OnlyForStations, Core.Settings.OnlyWithZeroSpeed, Core.Settings.BuildingNotAllowed, Core.Settings.IndestructibleNoBuilds, Core.Settings.IndestructibleGrindOwner, Core.Settings.LimitGridSizes, Core.Settings.LimitPerFaction, Core.Settings.LimitPerPlayer, Core.Settings.CleaningFrequency )); #endregion config } if (Core.IsServer || Core.IsAdmin(MyAPIGateway.Session.Player)) { #region alladminsettings if (internalCommand.Equals("on", StringComparison.OrdinalIgnoreCase)) { Core.Settings.Enabled = true; Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); SyncPacket newpacket = new SyncPacket(); newpacket.proto = SyncPacket.Version; newpacket.command = (ushort)Command.MessageToChat; newpacket.message = "Beacon Security is ON."; Core.SendMessage(newpacket); // send to others } else if (internalCommand.Equals("off", StringComparison.OrdinalIgnoreCase)) { Core.Settings.Enabled = false; Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); SyncPacket newpacket = new SyncPacket(); newpacket.proto = SyncPacket.Version; newpacket.command = (ushort)Command.MessageToChat; newpacket.message = "Beacon Security is OFF."; Core.SendMessage(newpacket); // send to others } else if (internalCommand.Equals("debug", StringComparison.OrdinalIgnoreCase)) { Core.Settings.Debug = !Core.Settings.Debug; Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Debug logging is {0}", (Core.Settings.Debug) ? "ON" : "OFF")); } else if (internalCommand.Equals("list", StringComparison.OrdinalIgnoreCase)) { List<string> GridNames = new List<string>(); List<string> GridNamesNotFound = new List<string>(); foreach (long entId in Core.Settings.Indestructible) { string gridName = GetGridName(entId); if (gridName != null) GridNames.Add(string.Format("'{0}'{1}{2}", gridName, Core.Settings.IndestructibleOverrideBuilds.Contains(entId) ? "[BO]" : "", Core.Settings.IndestructibleOverrideGrindOwner.Contains(entId) ? "[GO]" : "")); else GridNamesNotFound.Add(string.Format("id[{0}]{1}{2}", entId, Core.Settings.IndestructibleOverrideBuilds.Contains(entId) ? "[BO]" : "", Core.Settings.IndestructibleOverrideGrindOwner.Contains(entId) ? "[GO]" : "")); } string list = String.Join(", ", GridNames.ToArray()); string nflist = String.Join(", ", GridNamesNotFound.ToArray()); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Indestructible list: {0}", (GridNames.Count > 0) ? list : "[EMPTY]")); if (GridNamesNotFound.Count > 0) MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Currently not found: {0}", nflist)); } else if (internalCommand.Equals("add", StringComparison.OrdinalIgnoreCase)) { if (arguments.Length > 0) { long entId = 0; if (!long.TryParse(arguments, out entId)) entId = GetGridEntityId(arguments); if (entId > 0) { if (Core.Settings.Indestructible.Contains(entId)) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Can't add grid name '{0}', already in list...", arguments)); } else { Core.Settings.Indestructible.Add(entId); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' added.", arguments)); } } else { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found...", arguments)); } } else { List<string> added = new List<string>(); foreach (string gridname in m_lastFound) { long entId = GetGridEntityId(gridname); if (entId <= 0) continue; if (!Core.Settings.Indestructible.Contains(entId)) { added.Add(gridname); Core.Settings.Indestructible.Add(entId); } } string list = String.Join(", ", added.ToArray()); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Added {0} grid names: {1}", added.Count, list)); } } else if (internalCommand.Equals("rem", StringComparison.OrdinalIgnoreCase) || internalCommand.Equals("remove", StringComparison.OrdinalIgnoreCase) || internalCommand.Equals("del", StringComparison.OrdinalIgnoreCase)) { if (arguments.Length > 0) { long entId = 0; if (!long.TryParse(arguments, out entId)) entId = GetGridEntityId(arguments); if (entId > 0) { if (!Core.Settings.Indestructible.Contains(entId)) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found in list...", arguments)); } else { if (Core.Settings.IndestructibleOverrideBuilds.Contains(entId)) Core.Settings.IndestructibleOverrideBuilds.Remove(entId); Core.Settings.Indestructible.Remove(entId); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' removed.", arguments)); } } else { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found...", arguments)); } } else { List<string> removed = new List<string>(); foreach (string gridname in m_lastFound) { long entId = GetGridEntityId(gridname); if (entId <= 0) continue; if (Core.Settings.Indestructible.Contains(entId)) { removed.Add(gridname); Core.Settings.Indestructible.Remove(entId); } } string list = String.Join(", ", removed.ToArray()); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Removed {0} grid names: {1}", removed.Count, list)); } } else if (internalCommand.Equals("bo", StringComparison.OrdinalIgnoreCase) || internalCommand.Equals("buildon", StringComparison.OrdinalIgnoreCase)) { if (arguments.Length > 0) { long entId = 0; if (!long.TryParse(arguments, out entId)) entId = GetGridEntityId(arguments); if (entId > 0) { if (!Core.Settings.Indestructible.Contains(entId)) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found in list...", arguments)); } else { if (Core.Settings.IndestructibleOverrideBuilds.Contains(entId)) Core.Settings.IndestructibleOverrideBuilds.Remove(entId); else Core.Settings.IndestructibleOverrideBuilds.Add(entId); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("For grid name '{0}' building is override {1}.", arguments, Core.Settings.IndestructibleOverrideBuilds.Contains(entId))); } } else { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found...", arguments)); } } else { List<string> marked = new List<string>(); foreach (string gridname in m_lastFound) { long entId = GetGridEntityId(gridname); if (entId <= 0) continue; if (Core.Settings.Indestructible.Contains(entId)) { if (Core.Settings.IndestructibleOverrideBuilds.Contains(entId)) Core.Settings.IndestructibleOverrideBuilds.Remove(entId); else Core.Settings.IndestructibleOverrideBuilds.Add(entId); marked.Add(gridname); } } string list = String.Join(", ", marked.ToArray()); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Building is override {0} grid names: {1}", marked.Count, list)); } } else if (internalCommand.Equals("go", StringComparison.OrdinalIgnoreCase) || internalCommand.Equals("grindon", StringComparison.OrdinalIgnoreCase)) { if (arguments.Length > 0) { long entId = 0; if (!long.TryParse(arguments, out entId)) entId = GetGridEntityId(arguments); if (entId > 0) { if (!Core.Settings.Indestructible.Contains(entId)) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found in list...", arguments)); } else { if (Core.Settings.IndestructibleOverrideGrindOwner.Contains(entId)) Core.Settings.IndestructibleOverrideGrindOwner.Remove(entId); else Core.Settings.IndestructibleOverrideGrindOwner.Add(entId); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("For grid name '{0}' grind own property is override {1}.", arguments, Core.Settings.IndestructibleOverrideGrindOwner.Contains(entId))); } } else { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grid name '{0}' not found...", arguments)); } } else { List<string> marked = new List<string>(); foreach (string gridname in m_lastFound) { long entId = GetGridEntityId(gridname); if (entId <= 0) continue; if (Core.Settings.Indestructible.Contains(entId)) { if (Core.Settings.IndestructibleOverrideGrindOwner.Contains(entId)) Core.Settings.IndestructibleOverrideGrindOwner.Remove(entId); else Core.Settings.IndestructibleOverrideGrindOwner.Add(entId); marked.Add(gridname); } } string list = String.Join(", ", marked.ToArray()); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Grind own property is override {0} grid names: {1}", marked.Count, list)); } } else if (internalCommand.Equals("clear", StringComparison.OrdinalIgnoreCase)) { Core.Settings.Indestructible.Clear(); Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, "Indestructible list cleared."); } else if (internalCommand.Equals("find", StringComparison.OrdinalIgnoreCase)) { try { uint value = (arguments.Length > 0) ? UInt32.Parse(arguments) : 10; if (value < 1) value = 1; if (value > 100000) value = 100000; Vector3D pos = MyAPIGateway.Session.Player.GetPosition(); BoundingSphereD sphere = new BoundingSphereD(pos, value); List<IMyEntity> entities = MyAPIGateway.Entities.GetEntitiesInSphere(ref sphere); string[] found = entities.FindAll(x => x is IMyCubeGrid).Select(x => (x as IMyCubeGrid).DisplayName).ToArray(); string foundedList = String.Join(", ", found); m_lastFound.Clear(); m_lastFound.AddRange(found); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("In radius {0}m found: {1}", value, foundedList)); } catch (Exception ex) { Logger.Log.Error("Exception in command find: {0}", ex.Message); MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Exception: {0}", ex.Message)); } } else if (internalCommand.Equals("set", StringComparison.OrdinalIgnoreCase)) { #region setcommand string ResultMessage = ""; string[] argument = (arguments).Split(null as string[], 2, StringSplitOptions.RemoveEmptyEntries); if (argument.Length >= 2) { bool changed = false; if (argument[0].Equals("1") || argument[0].Equals("DelayBeforeTurningOn", StringComparison.OrdinalIgnoreCase)) { try { ushort value = UInt16.Parse(argument[1]); if (value < 0 || value > 3600) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("The value is not within the allowed limits. [ 0 - 3600 ]", value)); } else { Core.Settings.DelayBeforeTurningOn = value; changed = true; ResultMessage = string.Format("DelayBeforeTurningOn changed to {0}", value); } } catch (Exception ex) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Incorrect number. {0}", ex.Message)); } } else if (argument[0].Equals("2") || argument[0].Equals("DistanceBeforeTurningOn", StringComparison.OrdinalIgnoreCase)) { try { ushort value = UInt16.Parse(argument[1]); if (value < 0 || value > 10000) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("The value is not within the allowed limits. [ 0 - 10000 ]", value)); } else { Core.Settings.DistanceBeforeTurningOn = value; changed = true; ResultMessage = string.Format("DistanceBeforeTurningOn changed to {0}", value); } } catch (Exception ex) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Incorrect number. {0}", ex.Message)); } } else if (argument[0].Equals("3") || argument[0].Equals("OnlyForStations", StringComparison.OrdinalIgnoreCase)) { if (argument[1].Equals("on", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("true", StringComparison.OrdinalIgnoreCase)) { Core.Settings.OnlyForStations = true; changed = true; } else if (argument[1].Equals("off", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("false", StringComparison.OrdinalIgnoreCase)) { Core.Settings.OnlyForStations = false; changed = true; } ResultMessage = string.Format("OnlyForStations changed to {0}", (Core.Settings.OnlyForStations) ? "On" : "Off"); } else if (argument[0].Equals("4") || argument[0].Equals("OnlyWithZeroSpeed", StringComparison.OrdinalIgnoreCase)) { if (argument[1].Equals("on", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("true", StringComparison.OrdinalIgnoreCase)) { Core.Settings.OnlyWithZeroSpeed = true; changed = true; } else if (argument[1].Equals("off", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("false", StringComparison.OrdinalIgnoreCase)) { Core.Settings.OnlyWithZeroSpeed = false; changed = true; } ResultMessage = string.Format("OnlyWithZeroSpeed changed to {0}", (Core.Settings.OnlyWithZeroSpeed) ? "On" : "Off"); } else if (argument[0].Equals("5") || argument[0].Equals("BuildingNotAllowed", StringComparison.OrdinalIgnoreCase)) { if (argument[1].Equals("on", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("true", StringComparison.OrdinalIgnoreCase)) { Core.Settings.BuildingNotAllowed = true; changed = true; } else if (argument[1].Equals("off", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("false", StringComparison.OrdinalIgnoreCase)) { Core.Settings.BuildingNotAllowed = false; changed = true; } ResultMessage = string.Format("BuildingNotAllowed changed to {0}", (Core.Settings.BuildingNotAllowed) ? "On" : "Off"); } else if (argument[0].Equals("6") || argument[0].Equals("IndestructibleNoBuilds", StringComparison.OrdinalIgnoreCase)) { if (argument[1].Equals("on", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("true", StringComparison.OrdinalIgnoreCase)) { Core.Settings.IndestructibleNoBuilds = true; changed = true; } else if (argument[1].Equals("off", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("false", StringComparison.OrdinalIgnoreCase)) { Core.Settings.IndestructibleNoBuilds = false; changed = true; } ResultMessage = string.Format("IndestructibleNoBuilds changed to {0}", (Core.Settings.IndestructibleNoBuilds) ? "On" : "Off"); } else if (argument[0].Equals("7") || argument[0].Equals("IndestructibleGrindOwner", StringComparison.OrdinalIgnoreCase)) { if (argument[1].Equals("on", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("true", StringComparison.OrdinalIgnoreCase)) { Core.Settings.IndestructibleGrindOwner = true; changed = true; } else if (argument[1].Equals("off", StringComparison.OrdinalIgnoreCase) || argument[1].Equals("false", StringComparison.OrdinalIgnoreCase)) { Core.Settings.IndestructibleGrindOwner = false; changed = true; } ResultMessage = string.Format("IndestructibleGrindOwner changed to {0}", (Core.Settings.IndestructibleGrindOwner) ? "On" : "Off"); } else if (argument[0].Equals("8") || argument[0].Equals("LimitGridSizes", StringComparison.OrdinalIgnoreCase)) { try { ushort value = UInt16.Parse(argument[1]); if (value < 0 || value > 1000) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("The value is not within the allowed limits. [ 0 - 1000 ]", value)); } else { Core.Settings.LimitGridSizes = value; changed = true; ResultMessage = string.Format("LimitGridSizes changed to {0}", value); } } catch (Exception ex) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Incorrect number. {0}", ex.Message)); } } else if (argument[0].Equals("9") || argument[0].Equals("LimitPerFaction", StringComparison.OrdinalIgnoreCase)) { try { ushort value = UInt16.Parse(argument[1]); if (value < 1 || value > 100) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("The value is not within the allowed limits. [ 1 - 100 ]", value)); } else { Core.Settings.LimitPerFaction = value; changed = true; ResultMessage = string.Format("LimitPerFaction changed to {0}", value); } } catch (Exception ex) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Incorrect number. {0}", ex.Message)); } } else if (argument[0].Equals("10") || argument[0].Equals("LimitPerPlayer", StringComparison.OrdinalIgnoreCase)) { try { ushort value = UInt16.Parse(argument[1]); if (value < 1 || value > 100) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("The value is not within the allowed limits. [ 1 - 100 ]", value)); } else { Core.Settings.LimitPerPlayer = value; changed = true; ResultMessage = string.Format("LimitPerPlayer changed to {0}", value); } } catch (Exception ex) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Incorrect number. {0}", ex.Message)); } } else if (argument[0].Equals("11") || argument[0].Equals("CleaningFrequency", StringComparison.OrdinalIgnoreCase)) { try { ushort value = UInt16.Parse(argument[1]); if (value < 0 || value > 3600) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("The value is not within the allowed limits. [ 0 - 3600 ]", value)); } else { Core.Settings.CleaningFrequency = value; changed = true; ResultMessage = string.Format("CleaningFrequency changed to {0}", value); } } catch (Exception ex) { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, string.Format("Incorrect number. {0}", ex.Message)); } } if (changed) { Core.SendSettingsToServer(Core.Settings, MyAPIGateway.Session.Player.SteamUserId); SyncPacket newpacket = new SyncPacket(); newpacket.proto = SyncPacket.Version; newpacket.command = (ushort)Command.MessageToChat; newpacket.message = ResultMessage; Core.SendMessage(newpacket); // send to others } if (!Core.IsServer) MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, ResultMessage); } else { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, "Incorrect syntax for command set. Use: /bs set var value"); } #endregion setcommand } #endregion alladminsettings } else { MyAPIGateway.Utilities.ShowMessage(Core.MODSAY, "Only an administrator can change the settings..."); } } } sendToOthers = false; } }