Refactor for new configuration

This commit is contained in:
Chris 2020-12-03 17:43:10 +10:30
parent a03647ce38
commit 9423febd0a
26 changed files with 351 additions and 433 deletions

View file

@ -153,7 +153,7 @@ namespace Rests
tokenBucketTimer = new Timer((e) =>
{
DegradeBucket();
}, null, TimeSpan.Zero, TimeSpan.FromMinutes(Math.Max(TShock.Config.RESTRequestBucketDecreaseIntervalMinutes, 1)));
}, null, TimeSpan.Zero, TimeSpan.FromMinutes(Math.Max(TShock.Config.Settings.RESTRequestBucketDecreaseIntervalMinutes, 1)));
}
catch (Exception ex)
@ -356,7 +356,7 @@ namespace Rests
protected virtual object ExecuteCommand(RestCommand cmd, RestVerbs verbs, IParameterCollection parms, IRequest request, IHttpContext context)
{
object result = cmd.Execute(verbs, parms, request, context);
if (cmd.DoLog && TShock.Config.LogRest)
if (cmd.DoLog && TShock.Config.Settings.LogRest)
{
TShock.Log.ConsoleInfo("Anonymous requested REST endpoint: " + BuildRequestUri(cmd, verbs, parms, false));
}
@ -430,4 +430,4 @@ namespace Rests
#endregion
}
}
}

View file

@ -183,7 +183,7 @@ namespace TShockAPI
public void RegisterRestfulCommands()
{
// Server Commands
if (TShock.Config.EnableTokenEndpointAuthentication)
if (TShock.Config.Settings.EnableTokenEndpointAuthentication)
{
Rest.Register(new SecureRestCommand("/v2/server/status", ServerStatusV2));
Rest.Register(new SecureRestCommand("/v3/server/motd", ServerMotd));
@ -398,15 +398,15 @@ namespace TShockAPI
{
var ret = new RestObject()
{
{"name", TShock.Config.ServerName},
{"name", TShock.Config.Settings.ServerName},
{"serverversion", Main.versionNumber},
{"tshockversion", TShock.VersionNum},
{"port", TShock.Config.ServerPort},
{"port", TShock.Config.Settings.ServerPort},
{"playercount", Main.player.Where(p => null != p && p.active).Count()},
{"maxplayers", TShock.Config.MaxSlots},
{"world", (TShock.Config.UseServerName ? TShock.Config.ServerName : Main.worldName)},
{"maxplayers", TShock.Config.Settings.MaxSlots},
{"world", (TShock.Config.Settings.UseServerName ? TShock.Config.Settings.ServerName : Main.worldName)},
{"uptime", (DateTime.Now - System.Diagnostics.Process.GetCurrentProcess().StartTime).ToString(@"d'.'hh':'mm':'ss")},
{"serverpassword", !string.IsNullOrEmpty(TShock.Config.ServerPassword)}
{"serverpassword", !string.IsNullOrEmpty(TShock.Config.Settings.ServerPassword)}
};
if (GetBool(args.Parameters["players"], false))
@ -424,18 +424,18 @@ namespace TShockAPI
if (GetBool(args.Parameters["rules"], false))
{
var rules = new Dictionary<string, object>();
rules.Add("AutoSave", TShock.Config.AutoSave);
rules.Add("DisableBuild", TShock.Config.DisableBuild);
rules.Add("DisableClownBombs", TShock.Config.DisableClownBombs);
rules.Add("DisableDungeonGuardian", TShock.Config.DisableDungeonGuardian);
rules.Add("DisableInvisPvP", TShock.Config.DisableInvisPvP);
rules.Add("DisableSnowBalls", TShock.Config.DisableSnowBalls);
rules.Add("DisableTombstones", TShock.Config.DisableTombstones);
rules.Add("EnableWhitelist", TShock.Config.EnableWhitelist);
rules.Add("HardcoreOnly", TShock.Config.HardcoreOnly);
rules.Add("PvPMode", TShock.Config.PvPMode);
rules.Add("SpawnProtection", TShock.Config.SpawnProtection);
rules.Add("SpawnProtectionRadius", TShock.Config.SpawnProtectionRadius);
rules.Add("AutoSave", TShock.Config.Settings.AutoSave);
rules.Add("DisableBuild", TShock.Config.Settings.DisableBuild);
rules.Add("DisableClownBombs", TShock.Config.Settings.DisableClownBombs);
rules.Add("DisableDungeonGuardian", TShock.Config.Settings.DisableDungeonGuardian);
rules.Add("DisableInvisPvP", TShock.Config.Settings.DisableInvisPvP);
rules.Add("DisableSnowBalls", TShock.Config.Settings.DisableSnowBalls);
rules.Add("DisableTombstones", TShock.Config.Settings.DisableTombstones);
rules.Add("EnableWhitelist", TShock.Config.Settings.EnableWhitelist);
rules.Add("HardcoreOnly", TShock.Config.Settings.HardcoreOnly);
rules.Add("PvPMode", TShock.Config.Settings.PvPMode);
rules.Add("SpawnProtection", TShock.Config.Settings.SpawnProtection);
rules.Add("SpawnProtectionRadius", TShock.Config.Settings.SpawnProtectionRadius);
rules.Add("ServerSideInventory", Main.ServerSideCharacter);
ret.Add("rules", rules);
@ -496,7 +496,7 @@ namespace TShockAPI
var group = args.Parameters["group"];
if (string.IsNullOrWhiteSpace(group))
group = TShock.Config.DefaultRegistrationGroupName;
group = TShock.Config.Settings.DefaultRegistrationGroupName;
var password = args.Parameters["password"];
if (string.IsNullOrWhiteSpace(password))
@ -775,7 +775,7 @@ namespace TShockAPI
bool autoSave;
if (!bool.TryParse(args.Verbs["state"], out autoSave))
return RestInvalidParam("state");
TShock.Config.AutoSave = autoSave;
TShock.Config.Settings.AutoSave = autoSave;
var resp = RestResponse("AutoSave has been set to " + autoSave);
resp.Add("upgrade", "/v3/world/autosave");
@ -791,11 +791,11 @@ namespace TShockAPI
bool autoSave;
if (!bool.TryParse(args.Parameters["state"], out autoSave))
{
return RestResponse($"Autosave is currently {(TShock.Config.AutoSave ? "enabled" : "disabled")}");
return RestResponse($"Autosave is currently {(TShock.Config.Settings.AutoSave ? "enabled" : "disabled")}");
}
TShock.Config.AutoSave = autoSave;
TShock.Config.Settings.AutoSave = autoSave;
return RestResponse($"AutoSave has been {(TShock.Config.AutoSave ? "enabled" : "disabled")}");
return RestResponse($"AutoSave has been {(TShock.Config.Settings.AutoSave ? "enabled" : "disabled")}");
}
[Description("Save the world.")]
@ -840,7 +840,7 @@ namespace TShockAPI
{
return new RestObject()
{
{"name", (TShock.Config.UseServerName ? TShock.Config.ServerName : Main.worldName)},
{"name", (TShock.Config.Settings.UseServerName ? TShock.Config.Settings.ServerName : Main.worldName)},
{"size", Main.maxTilesX + "*" + Main.maxTilesY},
{"time", Main.time},
{"daytime", Main.dayTime},

View file

@ -59,7 +59,7 @@ namespace Rests
AppTokens.Add(t.Key, t.Value);
}
foreach (KeyValuePair<string, TokenData> t in TShock.Config.ApplicationRestTokens)
foreach (KeyValuePair<string, TokenData> t in TShock.Config.Settings.ApplicationRestTokens)
{
AppTokens.Add(t.Key, t.Value);
}
@ -115,7 +115,7 @@ namespace Rests
int tokens = 0;
if (tokenBucket.TryGetValue(context.RemoteEndPoint.Address.ToString(), out tokens))
{
if (tokens >= TShock.Config.RESTMaximumRequestsPerInterval)
if (tokens >= TShock.Config.Settings.RESTMaximumRequestsPerInterval)
{
TShock.Log.ConsoleError("A REST login from {0} was blocked as it currently has {1} tokens", context.RemoteEndPoint.Address.ToString(), tokens);
tokenBucket[context.RemoteEndPoint.Address.ToString()] += 1; // Tokens over limit, increment by one and reject request
@ -208,7 +208,7 @@ namespace Rests
}
object result = secureCmd.Execute(verbs, parms, tokenData, request, context);
if (cmd.DoLog && TShock.Config.LogRest)
if (cmd.DoLog && TShock.Config.Settings.LogRest)
TShock.Utils.SendLogs(string.Format(
"\"{0}\" requested REST endpoint: {1}", tokenData.Username, this.BuildRequestUri(cmd, verbs, parms, false)),
Color.PaleVioletRed);