From 734aaa2fb85cb749cb96b6f6e22e759d85d99078 Mon Sep 17 00:00:00 2001 From: Shank Date: Wed, 1 Jun 2011 00:10:25 -0600 Subject: [PATCH] Moved some code around Fixed dead code --- TShockAPI/ConfigurationManager.cs | 33 ++ TShockAPI/FileTools.cs | 194 +++++++++++ TShockAPI/TShock.cs | 541 ++++-------------------------- TShockAPI/TShockAPI.csproj | 3 + TShockAPI/Tools.cs | 279 +++++++++++++++ Terraria/ShankShock.cs | 19 +- 6 files changed, 578 insertions(+), 491 deletions(-) create mode 100644 TShockAPI/ConfigurationManager.cs create mode 100644 TShockAPI/FileTools.cs create mode 100644 TShockAPI/Tools.cs diff --git a/TShockAPI/ConfigurationManager.cs b/TShockAPI/ConfigurationManager.cs new file mode 100644 index 00000000..1dcdcbd3 --- /dev/null +++ b/TShockAPI/ConfigurationManager.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace TShockAPI +{ + /// + /// Provides all the stupid little variables a home away from home. + /// + class ConfigurationManager + { + public static int invasionMultiplier = 1; + public static int defaultMaxSpawns = 4; + public static int defaultSpawnRate = 700; + public static int serverPort = 7777; + public static bool enableWhitelist = false; + public static bool infiniteInvasion = false; + public static bool permaPvp = false; + public static int killCount = 0; + public static bool startedInvasion = false; + public static bool kickCheater = true; + public static bool banCheater = true; + public static bool banTnt = false; + public static bool kickTnt = false; + public enum NPCList : int + { + WORLD_EATER = 0, + EYE = 1, + SKELETRON = 2 + } + } +} diff --git a/TShockAPI/FileTools.cs b/TShockAPI/FileTools.cs new file mode 100644 index 00000000..29033e98 --- /dev/null +++ b/TShockAPI/FileTools.cs @@ -0,0 +1,194 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; +using Terraria; + +namespace TShockAPI +{ + class FileTools + { + public static string saveDir = "./tshock/"; + public static void CreateFile(string file) + { + using (FileStream fs = File.Create(file)) { } + } + /// + /// Adds a 'cheater' to cheaters.txt + /// + /// You should know what this does by now. + public static void WriteCheater(int ply) + { + string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); + string cheaters = ""; + TextReader tr = new StreamReader(saveDir + "cheaters.txt"); + cheaters = tr.ReadToEnd(); + tr.Close(); + if (cheaters.Contains(Main.player[ply].name) && cheaters.Contains(ip)) { return; } + TextWriter sw = new StreamWriter(saveDir + "cheaters.txt", true); + sw.WriteLine("[" + Main.player[ply].name + "] " + "[" + ip + "]"); + sw.Close(); + } + /// + /// Writes a 'banned idiot' to the ban list + /// + /// + public static void WriteBan(int ply) + { + string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); + TextWriter tw = new StreamWriter(saveDir + "bans.txt", true); + tw.WriteLine("[" + Main.player[ply].name + "] " + "[" + ip + "]"); + tw.Close(); + } + /// + /// Writes a tnt user to grief.txt + /// + /// int player + public static void WriteGrief(int ply) + { + TextWriter tw = new StreamWriter(saveDir + "grief.txt", true); + tw.WriteLine("[" + Main.player[ply].name + "] [" + Tools.GetRealIP(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint.ToString()) + "]"); + tw.Close(); + } + /// + /// Writes an error message to errors.txt + /// + /// string message + public static void WriteError(string err) + { + if (System.IO.File.Exists(saveDir + "errors.txt")) + { + TextWriter tw = new StreamWriter(saveDir + "errors.txt", true); + tw.WriteLine(err); + tw.Close(); + } + else + { + FileTools.CreateFile(saveDir + "errors.txt"); + TextWriter tw = new StreamWriter(saveDir + "errors.txt", true); + tw.WriteLine(err); + tw.Close(); + } + } + /// + /// Sets up the configuration file for all variables, and creates any missing files. + /// + public static void SetupConfig() + { + if (!System.IO.Directory.Exists(saveDir)) { System.IO.Directory.CreateDirectory(saveDir); } + if (System.IO.File.Exists(saveDir + "tiles.txt")) + { + System.IO.File.Delete(saveDir + "tiles.txt"); + } + if (!System.IO.File.Exists(saveDir + "motd.txt")) + { + FileTools.CreateFile(saveDir + "motd.txt"); + TextWriter tw = new StreamWriter(saveDir + "motd.txt"); + tw.WriteLine("This server is running TShock. Type /help for a list of commands."); + tw.WriteLine("%255,000,000%Current map: %map%"); + tw.WriteLine("Current players: %players%"); + tw.Close(); + } + if (!System.IO.File.Exists(saveDir + "bans.txt")) { FileTools.CreateFile(saveDir + "bans.txt"); } + if (!System.IO.File.Exists(saveDir + "cheaters.txt")) { FileTools.CreateFile(saveDir + "cheaters.txt"); } + if (!System.IO.File.Exists(saveDir + "admins.txt")) { FileTools.CreateFile(saveDir + "admins.txt"); } + if (!System.IO.File.Exists(saveDir + "grief.txt")) { FileTools.CreateFile(saveDir + "grief.txt"); } + if (!System.IO.File.Exists(saveDir + "whitelist.txt")) { FileTools.CreateFile(saveDir + "whitelist.txt"); } + if (!System.IO.File.Exists(saveDir + "config.txt")) + { + FileTools.CreateFile(saveDir + "config.txt"); + TextWriter tw = new StreamWriter(saveDir + "config.txt"); + tw.WriteLine("true,50,4,700,true,true,7777,false,false,false,false,false"); + tw.Close(); + } + TextReader tr = new StreamReader(saveDir + "config.txt"); + string config = tr.ReadToEnd(); + config = config.Replace("\n", ""); + config = config.Replace("\r", ""); + config = config.Replace(" ", ""); + tr.Close(); + string[] configuration = config.Split(','); + try + { + ConfigurationManager.invasionMultiplier = Convert.ToInt32(configuration[1]); + ConfigurationManager.defaultMaxSpawns = Convert.ToInt32(configuration[2]); + ConfigurationManager.defaultSpawnRate = Convert.ToInt32(configuration[3]); + ConfigurationManager.kickCheater = Convert.ToBoolean(configuration[4]); + ConfigurationManager.banCheater = Convert.ToBoolean(configuration[5]); + ConfigurationManager.serverPort = Convert.ToInt32(configuration[6]); + ConfigurationManager.enableWhitelist = Convert.ToBoolean(configuration[7]); + ConfigurationManager.infiniteInvasion = Convert.ToBoolean(configuration[8]); + ConfigurationManager.permaPvp = Convert.ToBoolean(configuration[9]); + ConfigurationManager.kickTnt = Convert.ToBoolean(configuration[10]); + ConfigurationManager.banTnt = Convert.ToBoolean(configuration[11]); + } + catch (Exception e) + { + FileTools.WriteError(e.Message); + } + + Netplay.serverPort = ConfigurationManager.serverPort; + } + /// + /// Checks if a user is banned + /// + /// string ip + /// true/false + public static bool CheckBanned(String p) + { + String ip = p.Split(':')[0]; + TextReader tr = new StreamReader(saveDir + "bans.txt"); + string banlist = tr.ReadToEnd(); + tr.Close(); + banlist = banlist.Trim(); + if (banlist.Contains(ip)) + return true; + return false; + } + /// + /// Tells if a user is on the whitelist + /// + /// string ip of the user + /// true/false + public static bool OnWhitelist(string ip) + { + if (!ConfigurationManager.enableWhitelist) { return true; } + if (!System.IO.File.Exists(saveDir + "whitelist.txt")) { FileTools.CreateFile(saveDir + "whitelist.txt"); TextWriter tw = new StreamWriter(saveDir + "whitelist.txt"); tw.WriteLine("127.0.0.1"); tw.Close(); } + TextReader tr = new StreamReader(saveDir + "whitelist.txt"); + string whitelist = tr.ReadToEnd(); + ip = Tools.GetRealIP(ip); + if (whitelist.Contains(ip)) { return true; } else { return false; } + } + /// + /// Tells if the user is on grief.txt + /// + /// + /// + public static bool CheckGreif(String ip) + { + ip = Tools.GetRealIP(ip); + if (!ConfigurationManager.banTnt) { return false; } + TextReader tr = new StreamReader(saveDir + "grief.txt"); + string list = tr.ReadToEnd(); + tr.Close(); + + return list.Contains(ip); + } + + public static bool CheckCheat(String ip) + { + ip = Tools.GetRealIP(ip); + if (!ConfigurationManager.banCheater) { return false; } + TextReader tr = new StreamReader(saveDir + "cheaters.txt"); + string trr = tr.ReadToEnd(); + tr.Close(); + if (trr.Contains(ip)) + { + return true; + } + return false; + } + public FileTools() { } + } +} diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index ce7422fe..cb4bb61e 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -9,6 +9,7 @@ using TerrariaAPI.Hooks; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System.Net; +using TShockAPI; namespace TShockAPI { @@ -22,29 +23,7 @@ namespace TShockAPI public static bool shownVersion = false; - public static bool killGuide = true; - public static int invasionMultiplier = 1; - public static int defaultMaxSpawns = 4; - public static int defaultSpawnRate = 700; - public static bool kickCheater = true; - public static bool banCheater = true; - public static int serverPort = 7777; - public static bool enableWhitelist = false; - public static bool infiniteInvasion = false; - public static bool permaPvp = false; - public static int killCount = 0; - public static bool startedInvasion = false; - public static string tileWhitelist = ""; - private static bool banTnt = false; - private static bool kickTnt = false; - - public enum NPCList : int - { - WORLD_EATER = 0, - EYE = 1, - SKELETRON = 2 - } public override Version Version { @@ -107,7 +86,7 @@ namespace TShockAPI void NpcHooks_OnStrikeNpc(NpcStrikeEventArgs e) { - if (infiniteInvasion) + if (ConfigurationManager.infiniteInvasion) { IncrementKills(); if (Main.invasionSize < 10) @@ -120,7 +99,7 @@ namespace TShockAPI void OnPreGetData(byte id, messageBuffer msg, int idx, int length, HandledEventArgs e) { if (Main.netMode != 2) { return; } - if (id == 0x1e && permaPvp) + if (id == 0x1e && ConfigurationManager.permaPvp) { e.Handled = true; } @@ -159,17 +138,17 @@ namespace TShockAPI if (Main.netMode != 2) { return; } int plr = who; //legacy support ShowUpdateReminder(who); - ShowMOTD(who); + Tools.ShowMOTD(who); if (Main.player[plr].statLifeMax > 400 || Main.player[plr].statManaMax > 200 || Main.player[plr].statLife > 400 || Main.player[plr].statMana > 200) { - HandleCheater(plr); + Tools.HandleCheater(plr); } - if (permaPvp) + if (ConfigurationManager.permaPvp) { Main.player[who].hostile = true; NetMessage.SendData(30, -1, -1, "", who); } - if (IsAdmin(who) && infiniteInvasion && !startedInvasion) + if (Tools.IsAdmin(who) && ConfigurationManager.infiniteInvasion && !ConfigurationManager.startedInvasion) { StartInvasion(); } @@ -182,15 +161,15 @@ namespace TShockAPI int x = (int) Main.player[ply].position.X; int y = (int) Main.player[ply].position.Y; - if (IsAdmin(ply)) + if (Tools.IsAdmin(ply)) { if (msg.Length > 5 && msg.Substring(0, 5) == "/kick") { string plStr = msg.Remove(0, 5).Trim(); - if (!(FindPlayer(plStr) == -1 || plStr == "")) + if (!(Tools.FindPlayer(plStr) == -1 || plStr == "")) { - Kick(FindPlayer(plStr), "You were kicked."); - Broadcast(plStr + " was kicked by " + FindPlayer(ply)); + Tools.Kick(Tools.FindPlayer(plStr), "You were kicked."); + Tools.Broadcast(plStr + " was kicked by " + Tools.FindPlayer(ply)); } handler.Handled = true; } @@ -198,10 +177,10 @@ namespace TShockAPI if (msg.Length > 4 && msg.Substring(0, 4) == "/ban") { string plStr = msg.Remove(0, 4).Trim(); - if (!(FindPlayer(plStr) == -1 || plStr == "")) + if (!(Tools.FindPlayer(plStr) == -1 || plStr == "")) { - WriteBan(FindPlayer(plStr)); - Kick(FindPlayer(plStr), "You were banned."); + FileTools.WriteBan(Tools.FindPlayer(plStr)); + Tools.Kick(Tools.FindPlayer(plStr), "You were banned."); } handler.Handled = true; } @@ -214,7 +193,7 @@ namespace TShockAPI if (msg == "/reload") { - SetupConfig(); + FileTools.SetupConfig(); handler.Handled = true; } @@ -243,7 +222,7 @@ namespace TShockAPI } if (msg == "/bloodmoon") { - Broadcast(FindPlayer(ply) + " turned on blood moon."); + Tools.Broadcast(Tools.FindPlayer(ply) + " turned on blood moon."); Main.bloodMoon = true; Main.time = 0; Main.dayTime = false; @@ -254,34 +233,34 @@ namespace TShockAPI } if (msg == "/eater") { - NewNPC((int)NPCList.WORLD_EATER, x, y, ply); - Broadcast(FindPlayer(ply) + " has spawned an eater of worlds!"); + Tools.NewNPC((int)ConfigurationManager.NPCList.WORLD_EATER, x, y, ply); + Tools.Broadcast(Tools.FindPlayer(ply) + " has spawned an eater of worlds!"); handler.Handled = true; } if (msg == "/eye") { - NewNPC((int)NPCList.EYE, x, y, ply); - Broadcast(FindPlayer(ply) + " has spawned an eye!"); + Tools.NewNPC((int)ConfigurationManager.NPCList.EYE, x, y, ply); + Tools.Broadcast(Tools.FindPlayer(ply) + " has spawned an eye!"); handler.Handled = true; } if (msg == "/skeletron") { - NewNPC((int)NPCList.SKELETRON, x, y, ply); - Broadcast(FindPlayer(ply) + " has spawned skeletron!"); + Tools.NewNPC((int)ConfigurationManager.NPCList.SKELETRON, x, y, ply); + Tools.Broadcast(Tools.FindPlayer(ply) + " has spawned skeletron!"); handler.Handled = true; } if (msg == "/hardcore") { for (int i = 0; i <= 2; i++) { - NewNPC(i, x, y, ply); + Tools.NewNPC(i, x, y, ply); } - Broadcast(FindPlayer(ply) + " has spawned all 3 bosses!"); + Tools.Broadcast(Tools.FindPlayer(ply) + " has spawned all 3 bosses!"); handler.Handled = true; } if (msg == "/invade") { - Broadcast(Main.player[ply].name + " started an invasion."); + Tools.Broadcast(Main.player[ply].name + " started an invasion."); StartInvasion(); handler.Handled = true; } @@ -289,50 +268,50 @@ namespace TShockAPI { string passwd = msg.Remove(0, 9).Trim(); Netplay.password = passwd; - SendMessage(ply, "Server password changed to: " + passwd); + Tools.SendMessage(ply, "Server password changed to: " + passwd); handler.Handled = true; } if (msg == "/save") { WorldGen.saveWorld(); - SendMessage(ply, "World saved."); + Tools.SendMessage(ply, "World saved."); handler.Handled = true; } if (msg == "/spawn") { Teleport(ply, Main.player[ply].SpawnX * 16, Main.player[ply].SpawnY * 16); - SendMessage(ply, "Teleported to your spawnpoint."); + Tools.SendMessage(ply, "Teleported to your spawnpoint."); handler.Handled = true; } if (msg.Length > 3 && msg.Substring(0, 3) == "/tp") { string player = msg.Remove(0, 3).Trim(); - if (!(FindPlayer(player) == -1) && !(player == "")) + if (!(Tools.FindPlayer(player) == -1) && !(player == "")) { - Teleport(ply, Main.player[FindPlayer(player)].position.X, Main.player[FindPlayer(player)].position.Y); - SendMessage(ply, "Teleported to " + player); + Teleport(ply, Main.player[Tools.FindPlayer(player)].position.X, Main.player[Tools.FindPlayer(player)].position.Y); + Tools.SendMessage(ply, "Teleported to " + player); handler.Handled = true; } } if (msg.Length > 7 && msg.Substring(0, 7) == "/tphere") { string player = msg.Remove(0, 7).Trim(); - if (!(FindPlayer(player) == -1) && !(player == "")) + if (!(Tools.FindPlayer(player) == -1) && !(player == "")) { - Teleport(FindPlayer(player), Main.player[ply].position.X, Main.player[ply].position.Y); - SendMessage(FindPlayer(player), "You were teleported to " + FindPlayer(ply) + "."); - SendMessage(ply, "You brought " + player + " here."); + Teleport(Tools.FindPlayer(player), Main.player[ply].position.X, Main.player[ply].position.Y); + Tools.SendMessage(Tools.FindPlayer(player), "You were teleported to " + Tools.FindPlayer(ply) + "."); + Tools.SendMessage(ply, "You brought " + player + " here."); handler.Handled = true; } } } if (msg == "/help") { - SendMessage(ply, "TShock Commands:"); - SendMessage(ply, "/kick, /ban, /reload, /off, /dropmeteor, /invade"); - SendMessage(ply, "/star, /skeletron, /eye, /eater, /hardcore"); - SendMessage(ply, "Terraria commands:"); - SendMessage(ply, "/playing, /p, /me"); + Tools.SendMessage(ply, "TShock Commands:"); + Tools.SendMessage(ply, "/kick, /ban, /reload, /off, /dropmeteor, /invade"); + Tools.SendMessage(ply, "/star, /skeletron, /eye, /eater, /hardcore"); + Tools.SendMessage(ply, "Terraria commands:"); + Tools.SendMessage(ply, "/playing, /p, /me"); handler.Handled = true; } } @@ -341,14 +320,14 @@ namespace TShockAPI void OnJoin(int ply, AllowEventArgs handler) { if (Main.netMode != 2) { return; } - string ip = GetRealIP((Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint))); - if (CheckBanned(ip) || CheckCheat(ip) || CheckGreif(ip)) + string ip = Tools.GetRealIP((Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint))); + if (FileTools.CheckBanned(ip) || FileTools.CheckCheat(ip) || FileTools.CheckGreif(ip)) { - Kick(ply, "You are banned."); + Tools.Kick(ply, "You are banned."); } - if (!OnWhitelist(ip)) + if (!FileTools.OnWhitelist(ip)) { - Kick(ply, "Not on whitelist."); + Tools.Kick(ply, "Not on whitelist."); } } @@ -359,7 +338,7 @@ namespace TShockAPI void OnPreInit() { - SetupConfig(); + FileTools.SetupConfig(); } void OnPostInit() @@ -376,8 +355,8 @@ namespace TShockAPI { if (Main.player[i] != null) { - WriteGrief((int)i); - Kick((int)i, "Kill tile abuse detected."); + FileTools.WriteGrief((int)i); + Tools.Kick((int)i, "Kill tile abuse detected."); } tileThreshold[i] = 0; } @@ -396,7 +375,7 @@ namespace TShockAPI { if (!shownVersion) { - if (IsAdmin(FindPlayer(ply))) + if (Tools.IsAdmin(Tools.FindPlayer(ply))) { WebClient client = new WebClient(); client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)"); @@ -407,16 +386,16 @@ namespace TShockAPI float[] color = { 255, 255, 000 }; if (Convert.ToDouble(changes[0]) > version) { - SendMessage(ply, "This server is out of date."); + Tools.SendMessage(ply, "This server is out of date."); for (int i = 1; i <= changes.Length; i++) { - SendMessage(ply, changes[i], color); + Tools.SendMessage(ply, changes[i], color); } } } catch (Exception e) { - WriteError(e.Message); + FileTools.WriteError(e.Message); } shownVersion = true; } @@ -444,13 +423,13 @@ namespace TShockAPI public static void StartInvasion() { Main.invasionType = 1; - if (infiniteInvasion) + if (ConfigurationManager.infiniteInvasion) { Main.invasionSize = 20000000; } else { - Main.invasionSize = 100 + (invasionMultiplier * activePlayers()); + Main.invasionSize = 100 + (ConfigurationManager.invasionMultiplier * Tools.activePlayers()); } Main.invasionWarn = 0; @@ -466,430 +445,34 @@ namespace TShockAPI public static void IncrementKills() { - killCount++; + ConfigurationManager.killCount++; Random r = new Random(); int random = r.Next(5); - if (killCount % 100 == 0) + if (ConfigurationManager.killCount % 100 == 0) { switch (random) { case 0: - Broadcast("You call that a lot? " + killCount + " goblins killed!"); + Tools.Broadcast("You call that a lot? " + ConfigurationManager.killCount + " goblins killed!"); break; case 1: - Broadcast("Fatality! " + killCount + " goblins killed!"); + Tools.Broadcast("Fatality! " + ConfigurationManager.killCount + " goblins killed!"); break; case 2: - Broadcast("Number of 'noobs' killed to date: " + killCount); + Tools.Broadcast("Number of 'noobs' killed to date: " + ConfigurationManager.killCount); break; case 3: - Broadcast("Duke Nukem would be proud. " + killCount + " goblins killed."); + Tools.Broadcast("Duke Nukem would be proud. " + ConfigurationManager.killCount + " goblins killed."); break; case 4: - Broadcast("You call that a lot? " + killCount + " goblins killed!"); + Tools.Broadcast("You call that a lot? " + ConfigurationManager.killCount + " goblins killed!"); break; case 5: - Broadcast(killCount + " copies of Call of Duty smashed."); + Tools.Broadcast(ConfigurationManager.killCount + " copies of Call of Duty smashed."); break; } } } - - public static int activePlayers() - { - int num = 0; - for (int i = 0; i < Main.maxPlayers; i++) - { - if (Main.player[i].active) - { - num++; - } - } - return num; - } - - public static bool OnWhitelist(string ip) - { - if (!enableWhitelist) { return true; } - if (!System.IO.File.Exists(saveDir + "whitelist.txt")) { CreateFile(saveDir + "whitelist.txt"); TextWriter tw = new StreamWriter(saveDir + "whitelist.txt"); tw.WriteLine("127.0.0.1"); tw.Close(); } - TextReader tr = new StreamReader(saveDir + "whitelist.txt"); - string whitelist = tr.ReadToEnd(); - ip = GetRealIP(ip); - if (whitelist.Contains(ip)) { return true; } else { return false; } - } - - public static bool CheckGreif(String ip) - { - ip = GetRealIP(ip); - if (!banTnt) { return false; } - TextReader tr = new StreamReader(saveDir + "grief.txt"); - string list = tr.ReadToEnd(); - tr.Close(); - - return list.Contains(ip); - } - - public static bool CheckCheat(String ip) - { - ip = GetRealIP(ip); - if (!banCheater) { return false; } - TextReader tr = new StreamReader(saveDir + "cheaters.txt"); - string trr = tr.ReadToEnd(); - tr.Close(); - if (trr.Contains(ip)) - { - return true; - } - return false; - } - - public static bool CheckBanned(String p) - { - String ip = p.Split(':')[0]; - TextReader tr = new StreamReader(saveDir + "bans.txt"); - string banlist = tr.ReadToEnd(); - tr.Close(); - banlist = banlist.Trim(); - if (banlist.Contains(ip)) - return true; - return false; - } - - private static void KeepTilesUpToDate() - { - TextReader tr = new StreamReader(saveDir + "tiles.txt"); - string file = tr.ReadToEnd(); - tr.Close(); - if (!file.Contains("0x3d")) - { - System.IO.File.Delete(saveDir + "tiles.txt"); - CreateFile(saveDir + "tiles.txt"); - TextWriter tw = new StreamWriter(saveDir + "tiles.txt"); - tw.Write("0x03, 0x05, 0x14, 0x25, 0x18, 0x18, 0x20, 0x1b, 0x34, 0x48, 0x33, 0x3d, 0x47, 0x49, 0x4a, 0x35, 0x3d, 0x3e, 0x45, 0x47, 0x49, 0x4a,"); - tw.Close(); - } - } - - public static void SetupConfig() - { - if (!System.IO.Directory.Exists(saveDir)) { System.IO.Directory.CreateDirectory(saveDir); } - if (!System.IO.File.Exists(saveDir + "tiles.txt")) - { - CreateFile(saveDir + "tiles.txt"); - TextWriter tw = new StreamWriter(saveDir + "tiles.txt"); - tw.Write("0x03, 0x05, 0x14, 0x25, 0x18, 0x18, 0x20, 0x1b, 0x34, 0x48, 0x33, 0x3d, 0x47, 0x49, 0x4a, 0x35, 0x3d, 0x3e, 0x45, 0x47, 0x49, 0x4a,"); - tw.Close(); - } - if (!System.IO.File.Exists(saveDir + "motd.txt")) - { - CreateFile(saveDir + "motd.txt"); - TextWriter tw = new StreamWriter(saveDir + "motd.txt"); - tw.WriteLine("This server is running TShock. Type /help for a list of commands."); - tw.WriteLine("%255,000,000%Current map: %map%"); - tw.WriteLine("Current players: %players%"); - tw.Close(); - } - if (!System.IO.File.Exists(saveDir + "bans.txt")) { CreateFile(saveDir + "bans.txt"); } - if (!System.IO.File.Exists(saveDir + "cheaters.txt")) { CreateFile(saveDir + "cheaters.txt"); } - if (!System.IO.File.Exists(saveDir + "admins.txt")) { CreateFile(saveDir + "admins.txt"); } - if (!System.IO.File.Exists(saveDir + "grief.txt")) { CreateFile(saveDir + "grief.txt"); } - if (!System.IO.File.Exists(saveDir + "whitelist.txt")) { CreateFile(saveDir + "whitelist.txt"); } - if (!System.IO.File.Exists(saveDir + "config.txt")) - { - CreateFile(saveDir + "config.txt"); - TextWriter tw = new StreamWriter(saveDir + "config.txt"); - tw.WriteLine("true,50,4,700,true,true,7777,false,false,false,false,false"); - tw.Close(); - } - KeepTilesUpToDate(); - TextReader tr = new StreamReader(saveDir + "config.txt"); - string config = tr.ReadToEnd(); - config = config.Replace("\n", ""); - config = config.Replace("\r", ""); - config = config.Replace(" ", ""); - tr.Close(); - string[] configuration = config.Split(','); - try - { - killGuide = Convert.ToBoolean(configuration[0]); - invasionMultiplier = Convert.ToInt32(configuration[1]); - defaultMaxSpawns = Convert.ToInt32(configuration[2]); - defaultSpawnRate = Convert.ToInt32(configuration[3]); - kickCheater = Convert.ToBoolean(configuration[4]); - banCheater = Convert.ToBoolean(configuration[5]); - serverPort = Convert.ToInt32(configuration[6]); - enableWhitelist = Convert.ToBoolean(configuration[7]); - infiniteInvasion = Convert.ToBoolean(configuration[8]); - permaPvp = Convert.ToBoolean(configuration[9]); - kickTnt = Convert.ToBoolean(configuration[10]); - banTnt = Convert.ToBoolean(configuration[11]); - if (infiniteInvasion) - { - //Main.startInv(); - } - } - catch (Exception e) - { - WriteError(e.Message); - } - - Netplay.serverPort = serverPort; - } - - public static void Kick(int ply, string reason) - { - NetMessage.SendData(0x2, ply, -1, reason, 0x0, 0f, 0f, 0f); - Netplay.serverSock[ply].kill = true; - NetMessage.syncPlayers(); - } - - public static bool IsAdmin(string ply) - { - string remoteEndPoint = Convert.ToString((Netplay.serverSock[FindPlayer(ply)].tcpClient.Client.RemoteEndPoint)); - string[] remoteEndPointIP = remoteEndPoint.Split(':'); - TextReader tr = new StreamReader(saveDir + "admins.txt"); - string adminlist = tr.ReadToEnd(); - tr.Close(); - if (adminlist.Contains(remoteEndPointIP[0])) - { - return true; - } - return false; - } - - public static bool IsAdmin(int ply) - { - string remoteEndPoint = Convert.ToString((Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); - string[] remoteEndPointIP = remoteEndPoint.Split(':'); - TextReader tr = new StreamReader(saveDir + "admins.txt"); - string adminlist = tr.ReadToEnd(); - tr.Close(); - if (adminlist.Contains(remoteEndPointIP[0])) - { - return true; - } - return false; - } - - public static int FindPlayer(string ply) - { - int pl = -1; - for (int i = 0; i < Main.player.Length; i++) - { - if ((ply.ToLower()) == Main.player[i].name.ToLower()) - { - pl = i; - break; - } - } - return pl; - } - - public static void HandleCheater(int ply) - { - string cheater = FindPlayer(ply); - string ip = GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); - - WriteGrief(ply); - WriteCheater(ply); - if (!kickCheater) { return; } - Netplay.serverSock[ply].kill = true; - Netplay.serverSock[ply].Reset(); - NetMessage.syncPlayers(); - Broadcast(cheater + " was " + (banCheater ? "banned " : "kicked ") + "for cheating."); - - } - - public static string FindPlayer(int ply) - { - for (int i = 0; i < Main.player.Length; i++) - { - if (i == ply) - { - return Main.player[i].name; - } - } - return "null"; - } - - public static void Broadcast(string msg) - { - for (int i = 0; i < Main.player.Length; i++) - { - SendMessage(i, msg); - } - } - - public static string GetRealIP(string mess) - { - return mess.Split(':')[0]; - } - - public static void SendMessage(int ply, string msg, float[] color) - { - NetMessage.SendData(0x19, ply, -1, msg, 8, color[0], color[1], color[2]); - } - - public static void SendMessage(int ply, string message) - { - NetMessage.SendData(0x19, ply, -1, message, 8, 0f, 255f, 0f); - } - - private static void WriteGrief(int ply) - { - TextWriter tw = new StreamWriter(saveDir + "grief.txt", true); - tw.WriteLine("[" + Main.player[ply].name + "] [" + GetRealIP(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint.ToString()) + "]"); - tw.Close(); - } - - private static void WriteError(string err) - { - if (System.IO.File.Exists(saveDir + "errors.txt")) - { - TextWriter tw = new StreamWriter(saveDir + "errors.txt", true); - tw.WriteLine(err); - tw.Close(); - } - else - { - CreateFile(saveDir + "errors.txt"); - TextWriter tw = new StreamWriter(saveDir + "errors.txt", true); - tw.WriteLine(err); - tw.Close(); - } - } - - public static void WriteBan(int ply) - { - string ip = GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); - TextWriter tw = new StreamWriter(saveDir + "bans.txt", true); - tw.WriteLine("[" + Main.player[ply].name + "] " + "[" + ip + "]"); - tw.Close(); - } - - private static void CreateFile(string file) - { - using (FileStream fs = File.Create(file)) { } - } - - public static void ShowMOTD(int ply) - { - string foo = ""; - TextReader tr = new StreamReader(saveDir + "motd.txt"); - while ((foo = tr.ReadLine()) != null) - { - foo = foo.Replace("%map%", Main.worldName); - foo = foo.Replace("%players%", GetPlayers()); - if (foo.Substring(0, 1) == "%" && foo.Substring(12, 1) == "%") //Look for a beginning color code. - { - string possibleColor = foo.Substring(0, 13); - foo = foo.Remove(0, 13); - float[] pC = { 0, 0, 0 }; - possibleColor = possibleColor.Replace("%", ""); - string[] pCc = possibleColor.Split(','); - if (pCc.Length == 3) - { - try - { - pC[0] = Clamp(Convert.ToInt32(pCc[0]), 255, 0); - pC[1] = Clamp(Convert.ToInt32(pCc[1]), 255, 0); - pC[2] = Clamp(Convert.ToInt32(pCc[2]), 255, 0); - SendMessage(ply, foo, pC); - continue; - } - catch (Exception e) - { - WriteError(e.Message); - } - } - } - SendMessage(ply, foo); - } - tr.Close(); - } - - public static T Clamp(T value, T max, T min) - where T : System.IComparable - { - T result = value; - if (value.CompareTo(max) > 0) - result = max; - if (value.CompareTo(min) < 0) - result = min; - return result; - } - - public static void WriteCheater(int ply) - { - string ip = GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); - string cheaters = ""; - TextReader tr = new StreamReader(saveDir + "cheaters.txt"); - cheaters = tr.ReadToEnd(); - tr.Close(); - if (cheaters.Contains(Main.player[ply].name) && cheaters.Contains(ip)) { return; } - TextWriter sw = new StreamWriter(saveDir + "cheaters.txt", true); - sw.WriteLine("[" + Main.player[ply].name + "] " + "[" + ip + "]"); - sw.Close(); - } - - private static string GetPlayers() - { - string str = ""; - for (int i = 0; i < Main.maxPlayers; i++) - { - if (Main.player[i].active) - { - if (str == "") - { - str = str + Main.player[i].name; - } - else - { - str = str + ", " + Main.player[i].name; - } - } - } - return str; - } - - public static void NewNPC(int type, int x, int y, int target) - { - - switch (type) - { - case 0: //World Eater - WorldGen.shadowOrbSmashed = true; - WorldGen.shadowOrbCount = 3; - int w = NPC.NewNPC(x, y, 13, 1); - Main.npc[w].target = target; - break; - case 1: //Eye - Main.time = 4861; - Main.dayTime = false; - WorldGen.spawnEye = true; - break; - case 2: //Skeletron - int enpeecee = NPC.NewNPC(x, y, 0x23, 0); - Main.npc[enpeecee].netUpdate = true; - break; - - } - - } - - public static bool TileOnWhitelist(byte tile) - { - int _tile = (int)tile; - TextReader tr2 = new StreamReader(saveDir + "tiles.txt"); - tileWhitelist = tr2.ReadToEnd(); tr2.Close(); - string hexValue = _tile.ToString("X"); - if (hexValue == "0") - { - return false; - } - Console.WriteLine(hexValue); - return tileWhitelist.Contains(hexValue); - } } } \ No newline at end of file diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index 639cb641..9c04a324 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -49,6 +49,9 @@ + + + diff --git a/TShockAPI/Tools.cs b/TShockAPI/Tools.cs new file mode 100644 index 00000000..156c7981 --- /dev/null +++ b/TShockAPI/Tools.cs @@ -0,0 +1,279 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; +using Terraria; + +namespace TShockAPI +{ + class Tools + { + /// + /// Provides the real IP address from a RemoteEndPoint string that contains a port and an IP + /// + /// A string IPv4 address in IP:PORT form. + /// A string IPv4 address. + public static string GetRealIP(string mess) + { + return mess.Split(':')[0]; + } + /// + /// Used for some places where a list of players might be used. + /// + /// String of players seperated by commas. + public static string GetPlayers() + { + string str = ""; + for (int i = 0; i < Main.maxPlayers; i++) + { + if (Main.player[i].active) + { + if (str == "") + { + str = str + Main.player[i].name; + } + else + { + str = str + ", " + Main.player[i].name; + } + } + } + return str; + } + /// + /// It's a clamp function + /// + /// + /// Value to clamp + /// Maximum bounds of the clamp + /// Minimum bounds of the clamp + /// + public static T Clamp(T value, T max, T min) + where T : System.IComparable + { + T result = value; + if (value.CompareTo(max) > 0) + result = max; + if (value.CompareTo(min) < 0) + result = min; + return result; + } + /// + /// Broadcasts a message to all players + /// + /// string message + public static void Broadcast(string msg) + { + for (int i = 0; i < Main.player.Length; i++) + { + SendMessage(i, msg); + } + } + /// + /// Sends a message out to a single player + /// + /// int socket thingy for the player from the server socket + /// String message + /// Float containing red, blue, and green color values + public static void SendMessage(int ply, string msg, float[] color) + { + NetMessage.SendData(0x19, ply, -1, msg, 8, color[0], color[1], color[2]); + } + /// + /// Sends a green message to a player + /// + /// int socket thingy for the player from the server socket + /// string message + public static void SendMessage(int ply, string message) + { + NetMessage.SendData(0x19, ply, -1, message, 8, 0f, 255f, 0f); + } + /// + /// The number of active players on the server. + /// + /// int playerCount + public static int activePlayers() + { + int num = 0; + for (int i = 0; i < Main.maxPlayers; i++) + { + if (Main.player[i].active) + { + num++; + } + } + return num; + } + /// + /// Finds the name of the player of the int given + /// + /// string player name + /// int player + public static int FindPlayer(string ply) + { + int pl = -1; + for (int i = 0; i < Main.player.Length; i++) + { + if ((ply.ToLower()) == Main.player[i].name.ToLower()) + { + pl = i; + break; + } + } + return pl; + } + /// + /// Gets the given player's name + /// + /// int player + /// string name + public static string FindPlayer(int ply) + { + for (int i = 0; i < Main.player.Length; i++) + { + if (i == ply) + { + return Main.player[i].name; + } + } + return "null"; + } + /// + /// Creates an NPC + /// + /// Type is defined in the enum NPC list + /// X coord of the desired npc + /// Y coord of the desired npc + /// int player that the npc targets + public static void NewNPC(int type, int x, int y, int target) + { + + switch (type) + { + case 0: //World Eater + WorldGen.shadowOrbSmashed = true; + WorldGen.shadowOrbCount = 3; + int w = NPC.NewNPC(x, y, 13, 1); + Main.npc[w].target = target; + break; + case 1: //Eye + Main.time = 4861; + Main.dayTime = false; + WorldGen.spawnEye = true; + break; + case 2: //Skeletron + int enpeecee = NPC.NewNPC(x, y, 0x23, 0); + Main.npc[enpeecee].netUpdate = true; + break; + + } + + } + /// + /// Finds a player, reads admins.txt, and determines if their IP address is on that list. + /// + /// int player + /// true/false + public static bool IsAdmin(int ply) + { + string remoteEndPoint = Convert.ToString((Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); + string[] remoteEndPointIP = remoteEndPoint.Split(':'); + TextReader tr = new StreamReader(FileTools.saveDir + "admins.txt"); + string adminlist = tr.ReadToEnd(); + tr.Close(); + if (adminlist.Contains(remoteEndPointIP[0])) + { + return true; + } + return false; + } + /// + /// Finds a player based on their name, reads admins.txt, and determines if thier IP address is on that list. + /// + /// + /// + public static bool IsAdmin(string ply) + { + string remoteEndPoint = Convert.ToString((Netplay.serverSock[Tools.FindPlayer(ply)].tcpClient.Client.RemoteEndPoint)); + string[] remoteEndPointIP = remoteEndPoint.Split(':'); + TextReader tr = new StreamReader(FileTools.saveDir + "admins.txt"); + string adminlist = tr.ReadToEnd(); + tr.Close(); + if (adminlist.Contains(remoteEndPointIP[0])) + { + return true; + } + return false; + } + /// + /// Kicks a player from the server. + /// + /// int player + /// string reason + public static void Kick(int ply, string reason) + { + NetMessage.SendData(0x2, ply, -1, reason, 0x0, 0f, 0f, 0f); + Netplay.serverSock[ply].kill = true; + NetMessage.syncPlayers(); + } + + /// + /// Determines what to do with someone listed in cheaters.txt + /// + /// int player + public static void HandleCheater(int ply) + { + string cheater = Tools.FindPlayer(ply); + string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); + + FileTools.WriteGrief(ply); + FileTools.WriteCheater(ply); + if (!ConfigurationManager.kickCheater) { return; } + Netplay.serverSock[ply].kill = true; + Netplay.serverSock[ply].Reset(); + NetMessage.syncPlayers(); + Tools.Broadcast(cheater + " was " + (ConfigurationManager.banCheater ? "banned " : "kicked ") + "for cheating."); + } + /// + /// Shows a MOTD to the player + /// + /// int player + public static void ShowMOTD(int ply) + { + string foo = ""; + TextReader tr = new StreamReader(FileTools.saveDir + "motd.txt"); + while ((foo = tr.ReadLine()) != null) + { + foo = foo.Replace("%map%", Main.worldName); + foo = foo.Replace("%players%", Tools.GetPlayers()); + if (foo.Substring(0, 1) == "%" && foo.Substring(12, 1) == "%") //Look for a beginning color code. + { + string possibleColor = foo.Substring(0, 13); + foo = foo.Remove(0, 13); + float[] pC = { 0, 0, 0 }; + possibleColor = possibleColor.Replace("%", ""); + string[] pCc = possibleColor.Split(','); + if (pCc.Length == 3) + { + try + { + pC[0] = Tools.Clamp(Convert.ToInt32(pCc[0]), 255, 0); + pC[1] = Tools.Clamp(Convert.ToInt32(pCc[1]), 255, 0); + pC[2] = Tools.Clamp(Convert.ToInt32(pCc[2]), 255, 0); + Tools.SendMessage(ply, foo, pC); + continue; + } + catch (Exception e) + { + FileTools.WriteError(e.Message); + } + } + } + Tools.SendMessage(ply, foo); + } + tr.Close(); + } + public Tools() { } + } +} diff --git a/Terraria/ShankShock.cs b/Terraria/ShankShock.cs index 463a5e4b..a8d3395c 100644 --- a/Terraria/ShankShock.cs +++ b/Terraria/ShankShock.cs @@ -106,7 +106,7 @@ namespace Terraria if (!System.IO.File.Exists(saveDir +"whitelist.txt")) { CreateFile(saveDir + "whitelist.txt"); TextWriter tw = new StreamWriter(saveDir + "whitelist.txt"); tw.WriteLine("127.0.0.1"); tw.Close(); } TextReader tr = new StreamReader(saveDir + "whitelist.txt"); string whitelist = tr.ReadToEnd(); - ip = GetRealIP(ip); + ip = Tools.GetRealIP(ip); if (whitelist.Contains(ip)) { return true; } else { return false; } } @@ -314,7 +314,7 @@ namespace Terraria private static void _writeGrief(int ply) { TextWriter tw = new StreamWriter(saveDir + "grief.txt", true); - tw.WriteLine("[" + Main.player[ply].name + "] [" + GetRealIP(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint.ToString()) + "]"); + tw.WriteLine("[" + Main.player[ply].name + "] [" + Tools.GetRealIP(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint.ToString()) + "]"); tw.Close(); } @@ -337,7 +337,7 @@ namespace Terraria public static void _writeban(int ply) { - string ip = ShankShock.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); + string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); TextWriter tw = new StreamWriter(saveDir + "bans.txt", true); tw.WriteLine("[" + Main.player[ply].name + "] " + "[" + ip + "]"); tw.Close(); @@ -345,7 +345,7 @@ namespace Terraria public static void _writecheater(int ply) { - string ip = ShankShock.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); + string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); string cheaters = ""; TextReader tr = new StreamReader(saveDir + "cheaters.txt"); cheaters = tr.ReadToEnd(); @@ -366,7 +366,7 @@ namespace Terraria public static void HandleCheater(int ply) { string cheater = ShankShock.FindPlayer(ply); - string ip = ShankShock.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); + string ip = Tools.GetRealIP(Convert.ToString(Netplay.serverSock[ply].tcpClient.Client.RemoteEndPoint)); _writecheater(ply); if (!kickCheater) { return; } @@ -379,7 +379,7 @@ namespace Terraria public static bool CheckGreif(String ip) { - ip = GetRealIP(ip); + ip = Tools.GetRealIP(ip); if (!banTnt) { return false; } TextReader tr = new StreamReader(saveDir + "grief.txt"); string list = tr.ReadToEnd(); @@ -390,7 +390,7 @@ namespace Terraria public static bool CheckCheat(String ip) { - ip = GetRealIP(ip); + ip = Tools.GetRealIP(ip); if (!banCheater) { return false; } TextReader tr = new StreamReader(saveDir + "cheaters.txt"); string trr = tr.ReadToEnd(); @@ -484,10 +484,5 @@ namespace Terraria } return false; } - - public static string GetRealIP(string mess) - { - return mess.Split(':')[0]; - } } }