private static void ProcessRemoveRegionRequest(ref BinaryReader reader, int playerNumber) { if (Network.Players[playerNumber].Group.IsAdmin) { int regionID = reader.ReadInt32(); Region region = Network.GetRegionByID(regionID); if (region != null) { DatabaseController.RemoveRegion(region); Network.Regions.Remove(region); SendRegionListToAllPlayers(); } } }
private static void ProcessChangeRegionColorRequest(ref BinaryReader reader, int playerNumber) { if (Network.Players[playerNumber].Group.IsAdmin) { Region region = Network.GetRegionByID(reader.ReadInt32()); if (region == null) { return; } Color color = reader.ReadRGB(); region.Color = color; DatabaseController.WriteRegionColor(region); SendRegionListToAllPlayers(); } }
private static void ProcessRemoveGroupFromRegionRequest(ref BinaryReader reader, int playerNumber) { if (Network.Players[playerNumber].Group.IsAdmin) { Region region = Network.GetRegionByID(reader.ReadInt32()); if (region == null) { return; } int groupID = reader.ReadInt32(); if (region.RemoveGroup(groupID)) { DatabaseController.WriteRegionPermissions(region); SendRegionListToAllPlayers(); } } }
//public static byte HEROsModNetworkMessageType //{ // get { return (byte)(Main.maxMsg - 1); } //} public static void InitializeWorld() { if (NetworkMode == NetworkMode.Server) { TileLastChangedBy = new int[Main.maxTilesX, Main.maxTilesY]; for (int x = 0; x < TileLastChangedBy.GetLength(0); x++) { for (int y = 0; y < TileLastChangedBy.GetLength(1); y++) { TileLastChangedBy[x, y] = -1; } } TileChangeController.Init(); Groups = DatabaseController.GetGroups(); Regions = DatabaseController.GetRegions(); } }
private static void ProcessSetOfflinePlayerGroupRequest(ref BinaryReader reader, int playerNumber) { if (Network.Players[playerNumber].Group.IsAdmin) { int id = reader.ReadInt32(); int groupID = reader.ReadInt32(); //Network.Players[id].Group = Network.GetGroupByID(groupID); //SendPlayerPermissions(id); DatabaseController.SetPlayerGroup(id, groupID); for (int i = 0; i < Network.Players.Length; i++) { HEROsModPlayer player = Network.Players[i]; if (player.ServerInstance.IsActive && player.Group.IsAdmin) { GeneralMessages.SendRegisteredUsersToPlayer(i); } } //GeneralMessages.SendRegisteredUsersToPlayer(playerNumber); } }
private static void ProcessDeleteGroupRequest(ref BinaryReader reader, int playerNumber) { if (Network.NetworkMode == NetworkMode.Server) { if (!Network.Players[playerNumber].Group.IsAdmin) { return; } Group groupToDelete = Network.GetGroupByID(reader.ReadInt32()); if (groupToDelete != null) { if (groupToDelete.Name.ToLower() != "default") { for (int i = 0; i < Network.Players.Length; i++) { if (Network.Players[i].Group == groupToDelete) { Network.Players[i].Group = Network.DefaultGroup; LoginService.SendPlayerPermissions(i); } } DatabaseController.DeleteGroup(groupToDelete); Network.Groups.Remove(groupToDelete); if (GroupChanged != null) { GroupChanged(null, EventArgs.Empty); } } else { Network.SendTextToPlayer("You can not delete the default group.", playerNumber); } } else { Network.SendTextToPlayer("Group could not be found", playerNumber); } } }
public static void SendRegisteredUsersToPlayer(int playerNumber) { WriteHeader(MessageType.RegisteredUsers); // Console.WriteLine("Buffer Length: " + Writer.BaseStream.Length); UserWithID[] players = DatabaseController.GetRegisteredUsers(); if (players.Length == 0) { return; } Writer.Write(players.Length); for (int i = 0; i < players.Length; i++) { Writer.Write(players[i].Username); Writer.Write(players[i].ID); Writer.Write(players[i].groupID); } Network.SendDataToPlayer(playerNumber); }
private static void ProcessTileModificationCheckRequest(ref BinaryReader reader, int playerNumber) { if (Network.Players[playerNumber].Group.HasPermission("CheckTiles")) { int x = reader.ReadInt32(); int y = reader.ReadInt32(); if (x >= 0 && y >= 0 && x < Main.maxTilesX && y < Main.maxTilesY) { int playerID = Network.TileLastChangedBy[x, y]; if (playerID >= 0) { UserWithID user = null; UserWithID[] users = DatabaseController.GetRegisteredUsers(); for (int i = 0; i < users.Length; i++) { if (users[i].ID == playerID) { user = users[i]; } } if (user != null) { Network.SendTextToPlayer(string.Format(HEROsMod.HeroText("TileLastModifiedBy"), user.Username), playerNumber); } } else { // TODO: These should all be NetworkText.FromKey so they show up in correct language on Client Network.SendTextToPlayer(HEROsMod.HeroText("TileNotModified"), playerNumber); } } } else { Network.SendTextToPlayer(HEROsMod.HeroText("YouDontHavePermissionToDoThat"), playerNumber); } }
private static void ProcessTileModificationCheckRequest(ref BinaryReader reader, int playerNumber) { if (Network.Players[playerNumber].Group.HasPermission("CheckTiles")) { int x = reader.ReadInt32(); int y = reader.ReadInt32(); if (x >= 0 && y >= 0 && x < Main.maxTilesX && y < Main.maxTilesY) { int playerID = Network.TileLastChangedBy[x, y]; if (playerID >= 0) { UserWithID user = null; UserWithID[] users = DatabaseController.GetRegisteredUsers(); for (int i = 0; i < users.Length; i++) { if (users[i].ID == playerID) { user = users[i]; } } if (user != null) { Network.SendTextToPlayer("Tile last modified by " + user.Username, playerNumber); } } else { Network.SendTextToPlayer("This tile has not been modified since last server boot up.", playerNumber); } } } else { Network.SendTextToPlayer("You don't have permission to do that.", playerNumber); } }
private static void ProcessSetGroupPermissionsRequest(ref BinaryReader reader, int playerNumber) { if (Network.NetworkMode == NetworkMode.Server) { if (!Network.Players[playerNumber].Group.IsAdmin) { return; } int id = reader.ReadInt32(); Group group = Network.GetGroupByID(id); int permissionsLength = reader.ReadInt32(); group.ImportPermissions(reader.ReadBytes(permissionsLength)); DatabaseController.SetGroupPermissions(group); for (int i = 0; i < Network.Players.Length; i++) { if (Network.Players[i].Group == group) { SendPlayerPermissions(i); } } GroupChanged?.Invoke(null, EventArgs.Empty); } }
// On Load Mod public static void Init() { // Reset Values to defaults. Group.PermissionList.Clear(); foreach (var item in Group.DefaultPermissions) { Group.PermissionList.Add(item); } ServerUsingHEROsMod = false; GravestonesAllowed = true; WillFreezeNonLoggedIn = true; Groups = new List <Group>(); Players = new HEROsModPlayer[255]; RegisteredUsers = new List <UserWithID>(); for (int i = 0; i < Players.Length; i++) { Players[i] = new HEROsModPlayer(i); } Regions = new List <Region>(); ResetWriter(); LoggedIn = false; AdminGroup = new Group("Admin"); AdminGroup.MakeAdmin(); DatabaseController.Init(); if (NetworkMode == NetworkMode.Server) { //TileLastChangedBy = new int[Main.maxTilesX, Main.maxTilesY]; //for (int x = 0; x < TileLastChangedBy.GetLength(0); x++) //{ // for (int y = 0; y < TileLastChangedBy.GetLength(1); y++) // { // TileLastChangedBy[x, y] = -1; // } //} //TileChangeController.Init(); Groups = DatabaseController.GetGroups(); //Regions = DatabaseController.GetRegions(); //CTFGroup = new Group("CTFGroup"); //CTFGroup.Permissions["StartCTF"] = true; foreach (Group group in Groups) { if (group.Name == "Default") { DefaultGroup = group; break; } } LoginService.GroupChanged += LoginService_GroupChanged; AuthCode = Main.rand.Next(100000, 999999); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("HERO's Mod: Create an account, login, and type /auth " + AuthCode + " to become Admin."); Console.ResetColor(); } }
// TODO -- How will any of these work....? public static bool CheckIncomingDataForHEROsModMessage(ref byte msgType, ref BinaryReader binaryReader, int playerNumber) { long readerPos = binaryReader.BaseStream.Position; switch (msgType) { case 12: if (NetworkMode == NetworkMode.Server) { //if (CTF.CaptureTheFlag.GameInProgress && Netplay.Clients[playerNumber].State == 10) //{ // if (Players[playerNumber].CTFTeam != CTF.TeamColor.None) // { // CTF.CTFMessages.SendPlayerToSpawnPlatform(Players[playerNumber]); // return true; // } //} if (Netplay.Clients[playerNumber].State == 3) { PlayerJoined(playerNumber); } } break; //case 14: // if (NetworkMode != global::HEROsModMod.NetworkMode.Server) // { // if (CTF.CaptureTheFlag.GameInProgress) // { // int index = (int)binaryReader.ReadByte(); // byte active = binaryReader.ReadByte(); // if (Players[index].CTFTeam == CTF.TeamColor.None && active == 1 && Players[index].GameInstance.ghost) // { // return true; // } // } // } // break; case 17: //Terrain Modified if (NetworkMode == NetworkMode.Server) { bool canBuild = false; TileModifyType tileModifyType = (TileModifyType)binaryReader.ReadByte(); int x = (int)binaryReader.ReadInt16(); int y = (int)binaryReader.ReadInt16(); short placeType = binaryReader.ReadInt16(); int style = (int)binaryReader.ReadByte(); bool fail = placeType == 1; HEROsModPlayer player = Players[playerNumber]; Tile tile; if (x >= 0 && y >= 0 && x < Main.maxTilesX && y < Main.maxTilesY) { tile = Main.tile[x, y]; } else { binaryReader.BaseStream.Position = readerPos; return false; } //if (CTF.CaptureTheFlag.GameInProgress && player.CTFTeam != CTF.TeamColor.None && CTF.CaptureTheFlag.AllowTerrainModification) //{ // canBuild = true; // if (CTF.CaptureTheFlag.ListeningForTileChanges) // { // Tile backupTile = CTF.CaptureTheFlag.ModifiedTiles[x, y]; // if (backupTile == null) // { // CTF.CaptureTheFlag.ModifiedTiles[x, y] = new Tile(); // CTF.CaptureTheFlag.ModifiedTiles[x, y].CopyFrom(tile); // Console.WriteLine("tile added"); // } // } //} if (!canBuild) { canBuild = PlayerHasPermissionToBuildAtBlock(player, x, y); } if (tileModifyType == TileModifyType.PlaceTile && placeType == TileID.LandMine) { SendTextToPlayer("Landmines are disabled on this server", playerNumber, Color.Red); } else if (canBuild) { TileLastChangedBy[x, y] = player.ID; binaryReader.BaseStream.Position = readerPos; if (tileModifyType == TileModifyType.KillTile) { LastTileKilledBy = player; WorldGen.KillTile(x, y, fail, false, false); NetMessage.SendData(17, -1, playerNumber, "", (int)tileModifyType, (float)x, (float)y, (float)placeType, style); LastTileKilledBy = null; return true; } else { TileChangeController.RecordChanges(player, x, y); } return false; } else { SendTextToPlayer("You do not have permission to build here", playerNumber, Color.Red); } switch (tileModifyType) { case TileModifyType.KillTile: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.PlaceTile, (float)x, (float)y, (float)tile.type, tile.slope()); break; case TileModifyType.PlaceTile: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.KillTile, (float)x, (float)y, (float)placeType, style); break; case TileModifyType.KillWall: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.PlaceWall, (float)x, (float)y, (float)tile.wall, style); break; case TileModifyType.PlaceWall: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.KillWall, (float)x, (float)y, (float)placeType, style); break; case TileModifyType.KillTileNoItem: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.PlaceTile, (float)x, (float)y, (float)tile.type, tile.slope()); break; case TileModifyType.PlaceWire: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.KillWire, (float)x, (float)y, (float)placeType, style); break; case TileModifyType.PlaceWire2: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.KillWire2, (float)x, (float)y, (float)placeType, style); break; case TileModifyType.PlaceWire3: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.KillWire3, (float)x, (float)y, (float)placeType, style); break; case TileModifyType.KillWire: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.PlaceWire, (float)x, (float)y, (float)placeType, style); break; case TileModifyType.KillWire2: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.PlaceWire2, (float)x, (float)y, (float)placeType, style); break; case TileModifyType.KillWire3: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.PlaceWire3, (float)x, (float)y, (float)placeType, style); break; case TileModifyType.KillActuator: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.PlaceActuator, (float)x, (float)y, (float)placeType, style); break; case TileModifyType.PlaceActuator: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.KillActuator, (float)x, (float)y, (float)placeType, style); break; case TileModifyType.PoundTile: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.PoundTile, (float)x, (float)y, (float)placeType, tile.slope()); break; case TileModifyType.SlopeTile: NetMessage.SendData(17, playerNumber, -1, "", (int)TileModifyType.SlopeTile, (float)x, (float)y, (float)placeType, tile.slope()); break; } return true; } break; case 25: //revieved a chat message binaryReader.ReadByte(); Color color = binaryReader.ReadRGB(); string text = binaryReader.ReadString(); if (NetworkMode == NetworkMode.Client) { if (text == HEROsModCheckMessage) { ServerUsingHEROsMod = true; HEROsMod.ServiceHotbar.Visible = true; GeneralMessages.TellSereverImUsingHEROsMod(); return true; } } else if (NetworkMode == NetworkMode.Server) { if (text.Length > 1 && text.Substring(0, 1) == "/") { string[] parameters = text.Substring(1, text.Length - 1).Split(' '); parameters[0] = parameters[0].ToLower(); switch (parameters[0]) { case "auth": if (parameters.Length != 2 || parameters[1].Length != 6) { SendTextToPlayer("Invalid Input", playerNumber); return true; } string authStr = parameters[1]; if (authStr == AuthCode.ToString()) { if (!Players[playerNumber].UsingHEROsMod) { SendTextToPlayer("You need HEROsMod Mod to use this feature", playerNumber); return true; } if (Players[playerNumber].Username.Length > 0) { Players[playerNumber].Group = AdminGroup; DatabaseController.SetPlayerGroup(Players[playerNumber].ID, Players[playerNumber].Group.ID); LoginService.SendPlayerPermissions(playerNumber); SendTextToPlayer("You are now Admin", playerNumber); return true; } else { SendTextToPlayer("Please login first", playerNumber); return true; } } else { SendTextToPlayer("Invalid Auth Code", playerNumber); return true; } case "login": if (parameters.Length != 3) { SendTextToPlayer("Invalid Input", playerNumber); return true; } LoginService.ProcessLoginRequest(parameters[1], parameters[2], playerNumber); return true; case "logout": if (parameters.Length != 1) { SendTextToPlayer("Invalid Input", playerNumber); return true; } LoginService.ProcessLogoutRequest(playerNumber); return true; case "register": if (parameters.Length != 3) { SendTextToPlayer("Invalid Input", playerNumber); return true; } LoginService.ProcessRegistrationRequest(parameters[1], parameters[2], playerNumber); break; case "help": if (parameters.Length != 1) { SendTextToPlayer("Invalid Input", playerNumber); } SendTextToPlayer("/login <username> <password> - Login with your account", playerNumber); SendTextToPlayer("/register <username> <password> - Create an account", playerNumber); SendTextToPlayer("/logout - Logout of your account", playerNumber); SendTextToPlayer("Use HEROsMod Mod to unlock all server features.", playerNumber); break; default: SendTextToPlayer("Invalid Command, type /help for a list of commands.", playerNumber); break; } } else { string text2 = text.ToLower(); if (text2 == Lang.mp[6] || text2 == Lang.mp[21]) { string text3 = ""; for (int i = 0; i < 255; i++) { if (Main.player[i].active) { if (text3 == "") { text3 = Main.player[i].name; } else { text3 = text3 + ", " + Main.player[i].name; } } } NetMessage.SendData(25, playerNumber, -1, Lang.mp[7] + " " + text3 + ".", 255, 255f, 240f, 20f, 0); } else if (text2.StartsWith("/me ")) { NetMessage.SendData(25, -1, -1, "*" + Main.player[playerNumber].name + " " + text.Substring(4), 255, 200f, 100f, 0f, 0); } else if (text2 == Lang.mp[8]) { NetMessage.SendData(25, -1, -1, string.Concat(new object[] { "*", Main.player[playerNumber].name, " ", Lang.mp[9], " ", Main.rand.Next(1, 101) }), 255, 255f, 240f, 20f, 0); } else if (text2.StartsWith("/p ")) { int num28 = Main.player[playerNumber].team; color = Main.teamColor[num28]; if (num28 != 0) { for (int i = 0; i < 255; i++) { if (Main.player[i].team == num28) { NetMessage.SendData(25, i, -1, text.Substring(3), 255, (float)color.R, (float)color.G, (float)color.B, 0); } } } else { NetMessage.SendData(25, playerNumber, -1, Lang.mp[10], 255, 255f, 240f, 20f, 0); } } else { return false; // why are chat messages randomized? /*color = chatColor[chatColorIndex]; chatColorIndex++; if (chatColorIndex >= chatColor.Length) chatColorIndex = 0; NetMessage.SendData(25, -1, -1, text, 255, (float)color.R, (float)color.G, (float)color.B, 0); if (Main.dedServ) { Console.WriteLine("<" + Main.player[playerNumber].name + "> " + text); }*/ } } return true; } break; //case 27: // if (ItemBanner.ItemsBanned && !Players[playerNumber].Group.IsAdmin) // { // int projIdentity = (int)binaryReader.ReadInt16(); // Vector2 position = binaryReader.ReadVector2(); // Vector2 velocity = binaryReader.ReadVector2(); // float knockback = binaryReader.ReadSingle(); // int damage = (int)binaryReader.ReadInt16(); // int owner = (int)binaryReader.ReadByte(); // int type = (int)binaryReader.ReadInt16(); // Console.WriteLine("Prof: " + type); // int[] bannedProjectiles = ItemBanner.bannedProjectiles; // for (int i = 0; i < bannedProjectiles.Length; i++) // { // if (bannedProjectiles[i] == type) // { // Projectile newProj = new Projectile(); // newProj.SetDefaults(type); // SendTextToPlayer(newProj.name + " is banned on the server", playerNumber, Color.Red); // int projIndex = 0; // for (int j = 0; j < 1000; j++) // { // if (!Main.projectile[j].active) // { // Projectile proj = Main.projectile[j]; // proj.owner = owner; // projIndex = j; // break; // } // } // NetMessage.SendData(27, playerNumber, -1, "", projIndex); // NetMessage.SendData(29, playerNumber, -1, "", projIdentity, (float)owner); // return true; // } // } // } // break; //case 30: // if (NetworkMode == global::HEROsModMod.NetworkMode.Server) // { // if (CTF.CaptureTheFlag.GameInProgress) // { // SendTextToPlayer("You cannot change your hostility while Capture the Flag is in progress.", playerNumber); // CTF.CaptureTheFlag.SetPlayerHostility(Players[playerNumber]); // return true; // } // } // break; //case 45: // if (NetworkMode == global::HEROsModMod.NetworkMode.Server) // { // if (CTF.CaptureTheFlag.GameInProgress) // { // SendTextToPlayer("You cannot change parties while Capture the Flag is in progress.", playerNumber); // CTF.CaptureTheFlag.SetPlayerHostility(Players[playerNumber]); // return true; // } // } // break; case 63: //block painted if (NetworkMode == global::HEROsMod.NetworkMode.Server) { int x = (int)binaryReader.ReadInt16(); int y = (int)binaryReader.ReadInt16(); byte paintColor = binaryReader.ReadByte(); HEROsModPlayer player = Players[playerNumber]; if (PlayerHasPermissionToBuildAtBlock(player, x, y)) { TileLastChangedBy[x, y] = player.ID; binaryReader.BaseStream.Position = readerPos; return false; } else { NetMessage.SendData(63, playerNumber, -1, "", x, (float)y, (float)Main.tile[x, y].color()); SendTextToPlayer("You do not have permission to build here", playerNumber, Color.Red); return true; } } break; case 64: //wall painted if (NetworkMode == global::HEROsMod.NetworkMode.Server) { int x = (int)binaryReader.ReadInt16(); int y = (int)binaryReader.ReadInt16(); byte paintColor = binaryReader.ReadByte(); HEROsModPlayer player = Players[playerNumber]; if (PlayerHasPermissionToBuildAtBlock(player, x, y)) { TileLastChangedBy[x, y] = player.ID; binaryReader.BaseStream.Position = readerPos; return false; } else { NetMessage.SendData(64, playerNumber, -1, "", x, (float)y, (float)Main.tile[x, y].wallColor()); SendTextToPlayer("You do not have permission to build here", playerNumber, Color.Red); return true; } } break; } //if (msgType == HEROsModNetworkMessageType) //{ // //We found a HEROsMod only message // MessageType subMsgType = (MessageType)binaryReader.ReadByte(); // switch(subMsgType) // { // case MessageType.GeneralMessage: // GeneralMessages.ProcessData(ref binaryReader, playerNumber); // break; // case MessageType.LoginMessage: // LoginService.ProcessData(ref binaryReader, playerNumber); // break; // case MessageType.CTFMessage: // CTF.CTFMessages.ProcessData(ref binaryReader, playerNumber); // break; // } //} //we need to set the stream position back to where it was before we got it binaryReader.BaseStream.Position = readerPos; return false; }