More code tidying.

This commit is contained in:
high 2011-06-14 18:13:46 -04:00
parent fff6e1bcbc
commit 966735ec2e
6 changed files with 97 additions and 136 deletions

View file

@ -603,7 +603,7 @@ namespace TShockAPI
int givenCode = Convert.ToInt32(args.Parameters[0]);
if (givenCode == ConfigurationManager.authToken)
{
TextWriter tw = new StreamWriter(FileTools.SaveDir + "users.txt", true);
TextWriter tw = new StreamWriter(FileTools.UsersPath, true);
tw.Write("\n" +
Tools.GetRealIP(
Convert.ToString(Netplay.serverSock[args.PlayerID].tcpClient.Client.RemoteEndPoint)) +
@ -912,7 +912,7 @@ namespace TShockAPI
List<Command> cmdlist = new List<Command>();
for (int j = 0; j < commands.Count; j++)
{
if (commands[j].CanRun(TShock.players[ply]))
if (commands[j].CanRun(TShock.Players[ply]))
{
cmdlist.Add(commands[j]);
}

View file

@ -64,7 +64,7 @@ namespace TShockAPI
public static void ReadJsonConfiguration()
{
TextReader tr = new StreamReader(FileTools.SaveDir + "config.json");
TextReader tr = new StreamReader(FileTools.ConfigPath);
ConfigFile cfg = JsonConvert.DeserializeObject<ConfigFile>(tr.ReadToEnd());
tr.Close();
@ -98,11 +98,6 @@ namespace TShockAPI
public static void WriteJsonConfiguration()
{
if (System.IO.File.Exists(FileTools.SaveDir + "config.json"))
{
System.IO.File.Delete(FileTools.SaveDir + "config.json");
}
FileTools.CreateFile(FileTools.SaveDir + "config.json");
ConfigFile cfg = new ConfigFile();
cfg.InvasionMultiplier = invasionMultiplier;
cfg.DefaultMaximumSpawns = defaultMaxSpawns;
@ -128,7 +123,7 @@ namespace TShockAPI
cfg.AdminChatRGB = adminChatRGB;
cfg.AdminChatPrefix = adminChatPrefix;
string json = JsonConvert.SerializeObject(cfg, Formatting.Indented);
TextWriter tr = new StreamWriter(FileTools.SaveDir + "config.json");
TextWriter tr = new StreamWriter(FileTools.ConfigPath);
tr.Write(json);
tr.Close();
}

View file

@ -22,12 +22,24 @@ namespace TShockAPI
{
internal class FileTools
{
public static string SaveDir = "./tshock/";
public static readonly string ErrorsPath = Path.Combine(TShock.SavePath, "errors.txt");
public static readonly string RulesPath = Path.Combine(TShock.SavePath, "rules.txt");
public static readonly string MotdPath = Path.Combine(TShock.SavePath, "motd.txt");
public static readonly string BansPath = Path.Combine(TShock.SavePath, "bans.txt");
public static readonly string WhitelistPath = Path.Combine(TShock.SavePath, "whitelist.txt");
public static readonly string GroupsPath = Path.Combine(TShock.SavePath, "groups.txt");
public static readonly string UsersPath = Path.Combine(TShock.SavePath, "users.txt");
public static readonly string ConfigPath = Path.Combine(TShock.SavePath, "config.json");
public static void CreateFile(string file)
{
using (FileStream fs = File.Create(file))
File.Create(file).Close();
}
public static void CreateIfNot(string file, string data = "")
{
if (!File.Exists(file))
{
File.WriteAllText(file, data);
}
}
@ -37,19 +49,9 @@ namespace TShockAPI
/// <param name="err">string message</param>
public static void WriteError(string err)
{
if (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();
}
TextWriter tw = new StreamWriter(ErrorsPath, true);
tw.WriteLine(err);
tw.Close();
}
/// <summary>
@ -57,53 +59,23 @@ namespace TShockAPI
/// </summary>
public static void SetupConfig()
{
if (!Directory.Exists(SaveDir))
if (!Directory.Exists(TShock.SavePath))
{
Directory.CreateDirectory(SaveDir);
Directory.CreateDirectory(TShock.SavePath);
}
if (!File.Exists(SaveDir + "rules.txt"))
{
CreateFile(SaveDir + "rules.txt");
TextWriter tw = new StreamWriter(SaveDir + "rules.txt");
tw.WriteLine("Respect the admins!");
tw.WriteLine("Don't use TNT!");
tw.Close();
}
if (!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 (!File.Exists(SaveDir + "bans.txt"))
{
CreateFile(SaveDir + "bans.txt");
}
if (!File.Exists(SaveDir + "whitelist.txt"))
{
CreateFile(SaveDir + "whitelist.txt");
}
if (!File.Exists(SaveDir + "groups.txt"))
{
CreateFile(SaveDir + "groups.txt");
StreamWriter sw = new StreamWriter(SaveDir + "groups.txt");
sw.Write(Resources.groups);
sw.Close();
}
if (!File.Exists(SaveDir + "users.txt"))
{
CreateFile(SaveDir + "users.txt");
StreamWriter sw = new StreamWriter(SaveDir + "users.txt");
sw.Write(Resources.users);
sw.Close();
}
if (File.Exists(FileTools.SaveDir + "config.json"))
CreateIfNot(RulesPath, "Respect the admins!\nDon't use TNT!");
CreateIfNot(MotdPath, "This server is running TShock. Type /help for a list of commands.\n%255,000,000%Current map: %map%\nCurrent players: %players%");
CreateIfNot(BansPath);
CreateIfNot(WhitelistPath);
CreateIfNot(GroupsPath, Resources.groups);
CreateIfNot(UsersPath, Resources.users);
if (File.Exists(ConfigPath))
{
ConfigurationManager.ReadJsonConfiguration();
} else
}
else
{
ConfigurationManager.WriteJsonConfiguration();
ConfigurationManager.ReadJsonConfiguration();
@ -123,14 +95,8 @@ namespace TShockAPI
{
return true;
}
if (!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");
CreateIfNot(WhitelistPath, "127.0.0.1");
TextReader tr = new StreamReader(WhitelistPath);
string whitelist = tr.ReadToEnd();
ip = Tools.GetRealIP(ip);
return whitelist.Contains(ip);

View file

@ -33,17 +33,17 @@ namespace TShockAPI
[APIVersion(1, 3)]
public class TShock : TerrariaPlugin
{
public static TSPlayer[] players = new TSPlayer[Main.maxPlayers];
public static TSPlayer[] Players = new TSPlayer[Main.maxPlayers];
public static string saveDir = "./tshock/";
public static readonly string SavePath = "./tshock/";
public static Version VersionNum = new Version(2, 1, 0, 3);
public static readonly Version VersionNum = new Version(2, 1, 0, 3);
public static string VersionCodename = "Forgot to increase the version.";
public static readonly string VersionCodename = "Forgot to increase the version.";
private static bool[] BlacklistTiles;
public static BanManager Bans = new BanManager(Path.Combine(saveDir, "bans.txt"));
public static BanManager Bans = new BanManager(Path.Combine(SavePath, "bans.txt"));
delegate bool HandleGetDataD(MemoryStream data, GetDataEventArgs e);
Dictionary<byte, HandleGetDataD> GetDataFuncs;
@ -122,18 +122,18 @@ namespace TShockAPI
GetDataFuncs = new Dictionary<byte, HandleGetDataD>
{
{0x4, HandlePlayerInfo},
{0xA, HandleSendSection},
{0xD, HandlePlayerUpdate},
{0x11, HandleTile},
{0x14, HandleSendTileSquare},
{0x17, HandleNpcUpdate},
{0x1A, HandlePlayerDamage},
{0x1B, HandleProjectileNew},
{0x1E, HandleTogglePvp},
{0x22, HandleTileKill},
{0x2C, HandlePlayerKillMe},
{0x30, HandleLiquidSet},
{(byte)PacketTypes.PlayerInfo, HandlePlayerInfo},
{(byte)PacketTypes.TileSendSection, HandleSendSection},
{(byte)PacketTypes.PlayerUpdate, HandlePlayerUpdate},
{(byte)PacketTypes.Tile, HandleTile},
{(byte)PacketTypes.TileSendSquare, HandleSendTileSquare},
{(byte)PacketTypes.NPCUpdate, HandleNpcUpdate},
{(byte)PacketTypes.PlayerDamage, HandlePlayerDamage},
{(byte)PacketTypes.ProjectileNew, HandleProjectileNew},
{(byte)PacketTypes.TogglePVP, HandleTogglePvp},
{(byte)PacketTypes.TileKill, HandleTileKill},
{(byte)PacketTypes.PlayerKillMe, HandlePlayerKillMe},
{(byte)PacketTypes.LiquidSet, HandleLiquidSet},
};
}
@ -149,7 +149,7 @@ namespace TShockAPI
}
string version = string.Format("TShock Version {0} ({1}) now running.", Version, VersionCodename);
Console.WriteLine(version);
Log.Initialize(FileTools.SaveDir + "log.txt", LogLevel.All, false);
Log.Initialize(Path.Combine(SavePath, "log.txt"), LogLevel.All, false);
Log.Info(version);
Log.Info("Starting...");
@ -275,17 +275,17 @@ namespace TShockAPI
Tools.ForceKick(e.Msg.whoAmI, "Empty Name.");
return true;
}
if (players[e.Msg.whoAmI] == null)
if (Players[e.Msg.whoAmI] == null)
{
Tools.ForceKick(e.Msg.whoAmI, "Player doesn't exist");
return true;
}
if (players[e.Msg.whoAmI].ReceivedInfo)
if (Players[e.Msg.whoAmI].ReceivedInfo)
{
return Tools.HandleGriefer(e.Msg.whoAmI, "Sent client info more than once");
}
players[e.Msg.whoAmI].ReceivedInfo = true;
Players[e.Msg.whoAmI].ReceivedInfo = true;
return false;
}
@ -341,7 +341,7 @@ namespace TShockAPI
}
if (ConfigurationManager.disableBuild)
{
if (!players[e.Msg.whoAmI].Group.HasPermission("editspawn"))
if (!Players[e.Msg.whoAmI].Group.HasPermission("editspawn"))
{
Tools.SendMessage(e.Msg.whoAmI, "World protected from changes.", Color.Red);
RevertPlayerChanges(e.Msg.whoAmI, type, x, y);
@ -350,7 +350,7 @@ namespace TShockAPI
}
if (ConfigurationManager.spawnProtect)
{
if (!players[e.Msg.whoAmI].Group.HasPermission("editspawn"))
if (!Players[e.Msg.whoAmI].Group.HasPermission("editspawn"))
{
var flag = CheckSpawn(x, y);
if (flag)
@ -364,8 +364,8 @@ namespace TShockAPI
if (type == 0 && BlacklistTiles[Main.tile[x, y].type] && Main.player[e.Msg.whoAmI].active)
{
players[e.Msg.whoAmI].TileThreshold++;
players[e.Msg.whoAmI].TilesDestroyed.Add(new Position(x, y), Main.tile[x, y]);
Players[e.Msg.whoAmI].TileThreshold++;
Players[e.Msg.whoAmI].TilesDestroyed.Add(new Position(x, y), Main.tile[x, y]);
}
return false;
@ -495,14 +495,14 @@ namespace TShockAPI
}
}
if (lava && !players[e.Msg.whoAmI].Group.HasPermission("canlava"))
if (lava && !Players[e.Msg.whoAmI].Group.HasPermission("canlava"))
{
Tools.SendMessage(e.Msg.whoAmI, "You do not have permission to use lava", Color.Red);
Tools.SendLogs(string.Format("{0} tried using lava", Main.player[e.Msg.whoAmI].name), Color.Red);
return true;
}
if (!lava && !players[e.Msg.whoAmI].Group.HasPermission("canwater"))
if (!lava && !Players[e.Msg.whoAmI].Group.HasPermission("canwater"))
{
Tools.SendMessage(e.Msg.whoAmI, "You do not have permission to use water", Color.Red);
Tools.SendLogs(string.Format("{0} tried using water", Main.player[e.Msg.whoAmI].name), Color.Red);
@ -533,7 +533,7 @@ namespace TShockAPI
if (ConfigurationManager.spawnProtect)
{
if (!players[e.Msg.whoAmI].Group.HasPermission("editspawn"))
if (!Players[e.Msg.whoAmI].Group.HasPermission("editspawn"))
{
var flag = CheckSpawn(x, y);
if (flag)
@ -570,7 +570,7 @@ namespace TShockAPI
if (Main.netMode != 2)
return;
Log.Info(string.Format("{0} ({1}) from '{2}' group joined.", Tools.FindPlayer(who), Tools.GetPlayerIP(who), players[who].Group.Name));
Log.Info(string.Format("{0} ({1}) from '{2}' group joined.", Tools.FindPlayer(who), Tools.GetPlayerIP(who), Players[who].Group.Name));
Tools.ShowMOTD(who);
if (HackedHealth(who))
@ -582,7 +582,7 @@ namespace TShockAPI
Main.player[who].hostile = true;
NetMessage.SendData(30, -1, -1, "", who);
}
if (players[who].Group.HasPermission("causeevents") && ConfigurationManager.infiniteInvasion)
if (Players[who].Group.HasPermission("causeevents") && ConfigurationManager.infiniteInvasion)
{
StartInvasion();
}
@ -600,7 +600,7 @@ namespace TShockAPI
return;
}
if (players[ply].Group.HasPermission("adminchat") && !text.StartsWith("/"))
if (Players[ply].Group.HasPermission("adminchat") && !text.StartsWith("/"))
{
Tools.Broadcast(ConfigurationManager.adminChatPrefix + "<" + Main.player[ply].name + "> " + text, (byte)ConfigurationManager.adminChatRGB[0], (byte)ConfigurationManager.adminChatRGB[1], (byte)ConfigurationManager.adminChatRGB[2]);
e.Handled = true;
@ -636,7 +636,7 @@ namespace TShockAPI
}
else
{
if (!cmd.CanRun(players[ply]))
if (!cmd.CanRun(Players[ply]))
{
Tools.SendLogs(string.Format("{0} tried to execute {1}", Tools.FindPlayer(ply), cmd.Name()), Color.Red);
Tools.SendMessage(ply, "You do not have access to that command.", Color.Red);
@ -644,7 +644,7 @@ namespace TShockAPI
else
{
Tools.SendLogs(string.Format("{0} executed: /{1}", Tools.FindPlayer(ply), text), Color.Red);
cmd.Run(text, players[ply], args);
cmd.Run(text, Players[ply], args);
}
}
e.Handled = true;
@ -659,11 +659,11 @@ namespace TShockAPI
}
string ip = Tools.GetPlayerIP(ply);
players[ply] = new TSPlayer(ply);
players[ply].Group = Tools.GetGroupForIP(ip);
Players[ply] = new TSPlayer(ply);
Players[ply].Group = Tools.GetGroupForIP(ip);
if (Tools.ActivePlayers() + 1 > ConfigurationManager.maxSlots &&
!players[ply].Group.HasPermission("reservedslot"))
!Players[ply].Group.HasPermission("reservedslot"))
{
Tools.ForceKick(ply, "Server is full");
handler.Handled = true;
@ -688,14 +688,14 @@ namespace TShockAPI
private void OnPostInit()
{
if (!File.Exists(FileTools.SaveDir + "auth.lck"))
if (!File.Exists(Path.Combine(SavePath, "auth.lck")))
{
Random r = new Random((int)DateTime.Now.ToBinary());
var r = new Random((int)DateTime.Now.ToBinary());
ConfigurationManager.authToken = r.Next(100000, 10000000);
Console.WriteLine("TShock Notice: To become SuperAdmin, join the game and type /auth " +
ConfigurationManager.authToken);
Console.WriteLine("This token will only display ONCE. This only works ONCE. If you don't use it and the server goes down, delete auth.lck.");
FileTools.CreateFile(FileTools.SaveDir + "auth.lck");
FileTools.CreateFile(Path.Combine(SavePath, "auth.lck"));
}
}
@ -726,24 +726,24 @@ namespace TShockAPI
{
if (Main.player[i].active == false)
continue;
if (players[i].TileThreshold >= 20)
if (Players[i].TileThreshold >= 20)
{
if (Tools.HandleTntUser(i, "Kill tile abuse detected."))
{
RevertKillTile(i);
players[i].TileThreshold = 0;
players[i].TilesDestroyed.Clear();
Players[i].TileThreshold = 0;
Players[i].TilesDestroyed.Clear();
}
else if (players[i].TileThreshold > 0)
else if (Players[i].TileThreshold > 0)
{
players[i].TileThreshold = 0;
players[i].TilesDestroyed.Clear();
Players[i].TileThreshold = 0;
Players[i].TilesDestroyed.Clear();
}
}
else if (players[i].TileThreshold > 0)
else if (Players[i].TileThreshold > 0)
{
players[i].TileThreshold = 0;
Players[i].TileThreshold = 0;
}
}
}
@ -913,11 +913,11 @@ namespace TShockAPI
public static void RevertKillTile(int ply)
{
Tile[] tiles = new Tile[players[ply].TilesDestroyed.Count];
players[ply].TilesDestroyed.Values.CopyTo(tiles, 0);
Position[] positions = new Position[players[ply].TilesDestroyed.Count];
players[ply].TilesDestroyed.Keys.CopyTo(positions, 0);
for (int i = (players[ply].TilesDestroyed.Count - 1); i >= 0; i--)
Tile[] tiles = new Tile[Players[ply].TilesDestroyed.Count];
Players[ply].TilesDestroyed.Values.CopyTo(tiles, 0);
Position[] positions = new Position[Players[ply].TilesDestroyed.Count];
Players[ply].TilesDestroyed.Keys.CopyTo(positions, 0);
for (int i = (Players[ply].TilesDestroyed.Count - 1); i >= 0; i--)
{
Main.tile[(int)positions[i].X, (int)positions[i].Y] = tiles[i];
NetMessage.SendData(17, -1, -1, "", 1, positions[i].X, positions[i].Y, (float)0);

View file

@ -139,9 +139,9 @@ namespace TShockAPI
Log.Info(log);
for (int i = 0; i < Main.maxPlayers; i++)
{
if (TShock.players[i] == null)
if (TShock.Players[i] == null)
continue;
if (!TShock.players[i].Group.HasPermission("logs"))
if (!TShock.Players[i].Group.HasPermission("logs"))
continue;
SendMessage(i, log, color);
@ -265,7 +265,7 @@ namespace TShockAPI
{
if (!Netplay.serverSock[ply].active || Netplay.serverSock[ply].kill)
return true;
if (!TShock.players[ply].Group.HasPermission("immunetokick"))
if (!TShock.Players[ply].Group.HasPermission("immunetokick"))
{
string playerName = Main.player[ply].name;
NetMessage.SendData(0x2, ply, -1, string.Format("Kicked: {0}", reason), 0x0, 0f, 0f, 0f);
@ -288,7 +288,7 @@ namespace TShockAPI
{
if (!Netplay.serverSock[plr].active || Netplay.serverSock[plr].kill)
return true;
if (!TShock.players[plr].Group.HasPermission("immunetoban"))
if (!TShock.Players[plr].Group.HasPermission("immunetoban"))
{
string ip = GetPlayerIP(plr);
string playerName = Main.player[plr].name;
@ -326,7 +326,7 @@ namespace TShockAPI
private static bool HandleBadPlayer(int ply, string overridePermission, bool ban, bool kick, string reason)
{
if (!TShock.players[ply].Group.HasPermission(overridePermission))
if (!TShock.Players[ply].Group.HasPermission(overridePermission))
{
if (ban)
{
@ -353,7 +353,7 @@ namespace TShockAPI
public static void ShowFileToUser(int ply, string file)
{
string foo = "";
TextReader tr = new StreamReader(FileTools.SaveDir + file);
TextReader tr = new StreamReader(Path.Combine(TShock.SavePath + file));
while ((foo = tr.ReadLine()) != null)
{
foo = foo.Replace("%map%", Main.worldName);
@ -388,7 +388,7 @@ namespace TShockAPI
groups = new List<Group>();
groups.Add(new SuperAdminGroup("superadmin"));
StreamReader sr = new StreamReader(FileTools.SaveDir + "groups.txt");
StreamReader sr = new StreamReader(FileTools.GroupsPath);
string data = sr.ReadToEnd();
data = data.Replace("\r", "");
string[] lines = data.Split('\n');
@ -483,7 +483,7 @@ namespace TShockAPI
{
ip = GetRealIP(ip);
StreamReader sr = new StreamReader(FileTools.SaveDir + "users.txt");
StreamReader sr = new StreamReader(FileTools.UsersPath);
string data = sr.ReadToEnd();
data = data.Replace("\r", "");
string[] lines = data.Split('\n');

View file

@ -77,7 +77,7 @@ namespace TShockAPI
{
if (Main.player[i].active)
{
if (!TShock.players[i].Group.HasPermission("maintenance"))
if (!TShock.Players[i].Group.HasPermission("maintenance"))
return;
Tools.SendMessage(i, "The server is out of date. To update, type /updatenow.");
for (int j = 4; j < changes.Length; j++)