Backups added

Pvp throttle added
This commit is contained in:
high 2011-06-17 21:12:37 -04:00
parent 4339650439
commit c901e8482d
7 changed files with 550 additions and 396 deletions

View file

@ -0,0 +1,98 @@
/*
TShock, a server mod for Terraria
Copyright (C) 2011 The TShock Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Terraria;
namespace TShockAPI
{
public class BackupManager
{
public string BackupPath { get; set; }
public int Interval { get; set; }
public int KeepFor { get; set; }
DateTime lastbackup = DateTime.UtcNow;
public BackupManager(string path)
{
BackupPath = path;
}
public bool IsBackupTime
{
get
{
return (Interval > 0) && ((DateTime.UtcNow - lastbackup).TotalMinutes >= Interval);
}
}
public void Backup()
{
lastbackup = DateTime.UtcNow;
ThreadPool.QueueUserWorkItem(DoBackup);
ThreadPool.QueueUserWorkItem(DeleteOld);
}
void DoBackup(object o)
{
try
{
string worldname = Main.worldPathName;
string name = Path.GetFileName(worldname);
Main.worldPathName = Path.Combine(BackupPath, string.Format("{0}.{1:dd.MM.yy-HH.mm.ss}.bak", name, DateTime.UtcNow));
string worldpath = Path.GetDirectoryName(Main.worldPathName);
if (worldpath != null && !Directory.Exists(worldpath))
Directory.CreateDirectory(worldpath);
WorldGen.saveWorld();
Console.WriteLine("World backed up");
Log.Error(string.Format("World backed up ({0})", Main.worldPathName));
Main.worldPathName = worldname;
}
catch (Exception ex)
{
Console.WriteLine("Backup failed");
Log.Error("Backup failed");
Log.Error(ex.ToString());
}
}
void DeleteOld(object o)
{
if (KeepFor <= 0)
return;
foreach (var fi in new DirectoryInfo(BackupPath).GetFiles("*.bak"))
{
if ((DateTime.UtcNow - fi.LastWriteTimeUtc).TotalMinutes > KeepFor)
{
fi.Delete();
}
}
}
}
}

View file

@ -41,7 +41,15 @@ namespace TShockAPI
public bool RangeChecks = true;
public bool SpamChecks = false;
public bool DisableBuild = false;
public float[] AdminChatRGB = {255, 0, 0};
public string AdminChatPrefix = "(Admin) ";
public int PvpThrottle = 0;
public int BackupInterval = 0;
public int BackupKeepFor = 60;
public bool ListServer = false;
}
}

View file

@ -55,6 +55,25 @@ namespace TShockAPI
public static float[] AdminChatRGB = {255, 0, 0};
public static string AdminChatPrefix = "(Admin) ";
/// <summary>
/// Don't allow pvp changing for x seconds.
/// </summary>
public static int PvpThrottle = 0;
/// <summary>
/// Backup every x minutes
/// </summary>
public static int BackupInterval = 0;
/// <summary>
/// Delete backups that are older than x mintues.
/// </summary>
public static int BackupKeepFor = 60;
/// <summary>
/// Server will broadcast itself to the server list.
/// </summary>
public static bool ListServer = false;
public static void ReadJsonConfiguration()
{
TextReader tr = new StreamReader(FileTools.ConfigPath);
@ -87,6 +106,10 @@ namespace TShockAPI
NPC.defaultSpawnRate = DefaultSpawnRate;
AdminChatRGB = cfg.AdminChatRGB;
AdminChatPrefix = cfg.AdminChatPrefix;
PvpThrottle = cfg.PvpThrottle;
BackupInterval = cfg.BackupInterval;
BackupKeepFor = cfg.BackupKeepFor;
ListServer = cfg.ListServer;
}
public static void WriteJsonConfiguration()
@ -115,6 +138,10 @@ namespace TShockAPI
cfg.DisableBuild = DisableBuild;
cfg.AdminChatRGB = AdminChatRGB;
cfg.AdminChatPrefix = AdminChatPrefix;
cfg.PvpThrottle = PvpThrottle;
cfg.BackupInterval = BackupInterval;
cfg.BackupKeepFor = BackupKeepFor;
cfg.ListServer = ListServer;
string json = JsonConvert.SerializeObject(cfg, Formatting.Indented);
TextWriter tr = new StreamWriter(FileTools.ConfigPath);
tr.Write(json);

View file

@ -139,7 +139,7 @@ namespace TShockAPI
Color underShirtColor = new Color(args.Data.ReadInt8(), args.Data.ReadInt8(), args.Data.ReadInt8());
Color pantsColor = new Color(args.Data.ReadInt8(), args.Data.ReadInt8(), args.Data.ReadInt8());
Color shoeColor = new Color(args.Data.ReadInt8(), args.Data.ReadInt8(), args.Data.ReadInt8());
string name = Encoding.ASCII.GetString(args.Data.ReadBytes((int) (args.Data.Length - args.Data.Position - 1)));
string name = Encoding.ASCII.GetString(args.Data.ReadBytes((int)(args.Data.Length - args.Data.Position - 1)));
if (hair >= Main.maxHair)
{
@ -254,7 +254,16 @@ namespace TShockAPI
int id = args.Data.ReadByte();
bool pvp = args.Data.ReadBoolean();
long seconds = (long)(DateTime.UtcNow - args.Player.LastPvpChange).TotalSeconds;
if (ConfigurationManager.PvpThrottle > 0 && seconds < ConfigurationManager.PvpThrottle)
{
args.Player.SendMessage(string.Format("You cannot change pvp status for {0} seconds", ConfigurationManager.PvpThrottle - seconds), 255, 0, 0);
args.Player.SetPvP(id != args.Player.Index || ConfigurationManager.PermaPvp ? true : args.TPlayer.hostile);
}
else
{
args.Player.SetPvP(id != args.Player.Index || ConfigurationManager.PermaPvp ? true : pvp);
}
return true;
}

View file

@ -34,6 +34,7 @@ namespace TShockAPI
public Group Group { get; set; }
public bool ReceivedInfo { get; set; }
public int Index { get; protected set; }
public DateTime LastPvpChange { get; protected set; }
public bool RealPlayer
{
get { return Index >= 0 && Index < Main.maxNetPlayers; }
@ -171,10 +172,12 @@ namespace TShockAPI
{
if (TPlayer.hostile != pvp)
{
LastPvpChange = DateTime.UtcNow;
TPlayer.hostile = pvp;
NetMessage.SendData((int)PacketTypes.TogglePVP, -1, -1, "", Index);
All.SendMessage(string.Format("{0} has {1} PvP!", Name, pvp ? "enabled" : "disabled"), Main.teamColor[Team]);
}
//Broadcast anyways to keep players synced
NetMessage.SendData((int)PacketTypes.TogglePVP, -1, -1, "", Index);
}
}

View file

@ -39,6 +39,7 @@ namespace TShockAPI
public static TSPlayer[] Players = new TSPlayer[Main.maxPlayers];
public static BanManager Bans = new BanManager(Path.Combine(SavePath, "bans.txt"));
public static BackupManager Backups = new BackupManager(Path.Combine(SavePath, "backups"));
public override Version Version
{
@ -101,6 +102,9 @@ namespace TShockAPI
Commands.InitCommands();
Log.Info("Commands initialized");
Backups.KeepFor = ConfigurationManager.BackupKeepFor;
Backups.Interval = ConfigurationManager.BackupInterval;
HandleCommandLine(Environment.GetCommandLineArgs());
}
@ -178,6 +182,10 @@ namespace TShockAPI
private void OnUpdate(GameTime time)
{
UpdateManager.UpdateProcedureCheck();
if (Backups.IsBackupTime)
Backups.Backup();
foreach (TSPlayer player in TShock.Players)
{
if (player != null && player.Active)

View file

@ -12,6 +12,7 @@
<AssemblyName>TShockAPI</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
@ -24,7 +25,6 @@
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
@ -69,6 +69,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="BackupManager.cs" />
<Compile Include="BanManager.cs" />
<Compile Include="Commands.cs" />
<Compile Include="ConfigFile.cs" />
@ -133,7 +134,7 @@
</PropertyGroup>
<ProjectExtensions>
<VisualStudio>
<UserProperties BuildVersion_IncrementBeforeBuild="False" BuildVersion_StartDate="2011/6/17" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_BuildAction="Both" BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" />
<UserProperties BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" BuildVersion_BuildAction="Both" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_StartDate="2011/6/17" BuildVersion_IncrementBeforeBuild="False" />
</VisualStudio>
</ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.