Refactored server stop and world save operations fixing race conditions so as to ensure operations always happen in a predicable order. This fixes output not appearing in the console / log for example. This adds TShock.Utils.StopServer method used by IGA, rcon and the RestAPI.

Fixed console title set not working

Optimised command line parsing

Made Utils a singleton to enforce the fact that only one copy should ever exist

Added name to /v2/user/read output as users can be found by id
This commit is contained in:
stevenh 2012-02-20 22:31:16 +00:00
parent 84789ff4d5
commit d34199b17d
7 changed files with 228 additions and 121 deletions

View file

@ -29,11 +29,10 @@ namespace TShockAPI
{
public class Utils
{
public static bool saving = false;
public Utils()
{
}
// Utils is a Singleton
private static readonly Utils instance = new Utils();
private Utils() {}
public static Utils Instance { get { return instance; } }
public Random Random = new Random();
//private static List<Group> groups = new List<Group>();
@ -135,11 +134,7 @@ namespace TShockAPI
/// </summary>
public void SaveWorld()
{
saving = true;
WorldGen.realsaveWorld();
Broadcast("World saved.", Color.Yellow);
Log.Info(string.Format("World saved at ({0})", Main.worldPathName));
saving = false;
SaveManager.Instance.SaveWorld();
}
/// <summary>
@ -186,15 +181,7 @@ namespace TShockAPI
/// <returns>int playerCount</returns>
public int ActivePlayers()
{
int num = 0;
foreach (TSPlayer player in TShock.Players)
{
if (player != null && player.Active)
{
num++;
}
}
return num;
return Main.player.Where(p => null != p && p.active).Count();
}
/// <summary>
@ -510,6 +497,27 @@ namespace TShockAPI
}
}
/// <summary>
/// Stops the server after kicking all players with a reason message, and optionally saving the world
/// </summary>
/// <param name="save">bool perform a world save before stop (default: true)</param>
/// <param name="reason">string reason (default: "Server shutting down!")</param>
public void StopServer(bool save = true, string reason = "Server shutting down!")
{
ForceKickAll(reason);
if (save)
SaveManager.Instance.SaveWorld();
// Save takes a while so kick again
ForceKickAll(reason);
// Broadcast so console can see we are shutting down as well
TShock.Utils.Broadcast(reason, Color.Red);
// Disconnect after kick as that signifies server is exiting and could cause a race
Netplay.disconnect = true;
}
/// <summary>
/// Kicks a player from the server without checking for immunetokick permission.
/// </summary>