CommandParser.GetEnum should only allow actual values of the enum, improve CTFConfig load/save, start modifying /ctf to let you set config values

This commit is contained in:
UnknownShadow200 2017-06-30 13:35:04 +10:00
parent e27c8ec482
commit 2f2ac17758
5 changed files with 79 additions and 28 deletions

View File

@ -43,12 +43,13 @@ namespace MCGalaxy.Commands {
ref TEnum result) where TEnum : struct { ref TEnum result) where TEnum : struct {
try { try {
result = (TEnum)Enum.Parse(typeof(TEnum), input, true); result = (TEnum)Enum.Parse(typeof(TEnum), input, true);
return true; if (Enum.IsDefined(typeof(TEnum), result)) return true;
} catch (Exception) { } catch {
string[] names = Enum.GetNames(typeof(TEnum));
Player.Message(p, argName + " must be one of the following: " + names.Join());
return false;
} }
string[] names = Enum.GetNames(typeof(TEnum));
Player.Message(p, argName + " must be one of the following: " + names.Join());
return false;
} }
/// <summary> Attempts to parse the given argument as an timespan in short form. </summary> /// <summary> Attempts to parse the given argument as an timespan in short form. </summary>

View File

@ -16,11 +16,13 @@
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System; using System;
using System.IO;
using MCGalaxy.Games; using MCGalaxy.Games;
namespace MCGalaxy.Commands.Fun { namespace MCGalaxy.Commands.Fun {
public sealed class CmdCTF : Command { public sealed class CmdCTF : Command {
public override string name { get { return "ctf"; } } public override string name { get { return "ctf"; } }
public override string shortcut { get { return "ctfsetup"; } }
public override string type { get { return CommandTypes.Games; } } public override string type { get { return CommandTypes.Games; } }
public override bool museumUsable { get { return false; } } public override bool museumUsable { get { return false; } }
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } } public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
@ -35,17 +37,44 @@ namespace MCGalaxy.Commands.Fun {
if (!Server.ctf.Start(p)) return; if (!Server.ctf.Start(p)) return;
Chat.MessageGlobal("A CTF GAME IS STARTING AT CTF! TYPE /goto CTF to join!"); Chat.MessageGlobal("A CTF GAME IS STARTING AT CTF! TYPE /goto CTF to join!");
} else if (message == "stop") { } else if (message.CaselessEq("stop")) {
if (Server.ctf == null || !Server.ctf.started) { if (Server.ctf == null || !Server.ctf.started) {
Player.Message(p, "No CTF game is active."); return; Player.Message(p, "No CTF game is active."); return;
} }
Server.ctf.Stop(); Server.ctf.Stop();
} else if (message.CaselessEq("bluespawn")) {
CTFConfig cfg = Retrieve(p);
cfg.BlueSpawnX = p.Pos.X; cfg.BlueSpawnY = p.Pos.Y; cfg.BlueSpawnZ = p.Pos.Z;
Update(p, cfg);
Player.Message(p, "Set spawn of blue team to your position.");
} else if (message.CaselessEq("redspawn")) {
CTFConfig cfg = Retrieve(p);
cfg.RedSpawnX = p.Pos.X; cfg.RedSpawnY = p.Pos.Y; cfg.RedSpawnZ = p.Pos.Z;
Update(p, cfg);
Player.Message(p, "Set spawn of red team to your position.");
} }
} }
static CTFConfig Retrieve(Player p) {
CTFConfig cfg = new CTFConfig();
cfg.SetDefaults(p.level);
cfg.Retrieve(p.level.name);
return cfg;
}
static void Update(Player p, CTFConfig cfg) {
if (!Directory.Exists("CTF")) Directory.CreateDirectory("CTF");
cfg.Save(p.level.name);
if (Server.ctf != null && p.level == Server.ctf.map) Server.ctf.UpdateConfig();
}
public override void Help(Player p) { public override void Help(Player p) {
Player.Message(p, "%T/ctf start/stop"); Player.Message(p, "%T/ctf start/stop");
Player.Message(p, "%HStarts/Stops the CTF game."); Player.Message(p, "%HStarts/stops the CTF game.");
Player.Message(p, "%T/ctf redspawn/bluespawn");
Player.Message(p, "%HSets spawn of red/blue team to your position.");
} }
} }
} }

View File

@ -58,6 +58,7 @@ namespace MCGalaxy {
return false; return false;
} }
/// <summary> Writes all config elements to the given stream, grouped by named sections. </summary>
public static void Serialise(ConfigElement[] elements, string suffix, public static void Serialise(ConfigElement[] elements, string suffix,
StreamWriter dst, object instance) { StreamWriter dst, object instance) {
Dictionary<string, List<ConfigElement>> sections Dictionary<string, List<ConfigElement>> sections
@ -81,5 +82,13 @@ namespace MCGalaxy {
dst.WriteLine(); dst.WriteLine();
} }
} }
/// <summary> Writes all config elements to the given stream. </summary>
public static void SerialisePlain(ConfigElement[] elements, StreamWriter dst, object instance) {
foreach (ConfigElement elem in elements) {
object value = elem.Field.GetValue(instance);
dst.WriteLine(elem.Attrib.Name + " = " + elem.Attrib.Serialise(value));
}
}
} }
} }

View File

@ -16,14 +16,15 @@
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System; using System;
using System.IO;
using MCGalaxy.Config; using MCGalaxy.Config;
namespace MCGalaxy.Games { namespace MCGalaxy.Games {
public sealed class CTFConfig { public sealed class CTFConfig {
[ConfigInt("base.red.x", null, 0)] [ConfigInt("base.red.x", null, 0)]
public int RedFlagX; public int RedFlagX;
[ConfigInt("base.red.y", null, 0)] [ConfigInt("base.red.y", null, 0)]
@ -32,7 +33,7 @@ namespace MCGalaxy.Games {
public int RedFlagZ; public int RedFlagZ;
[ConfigByte("base.red.block", null, 0)] [ConfigByte("base.red.block", null, 0)]
public byte RedFlagBlock; public byte RedFlagBlock;
[ConfigInt("base.blue.x", null, 0)] [ConfigInt("base.blue.x", null, 0)]
public int BlueFlagX; public int BlueFlagX;
[ConfigInt("base.blue.y", null, 0)] [ConfigInt("base.blue.y", null, 0)]
@ -48,7 +49,7 @@ namespace MCGalaxy.Games {
public int RedSpawnY; public int RedSpawnY;
[ConfigInt("base.red.spawnz", null, 0)] [ConfigInt("base.red.spawnz", null, 0)]
public int RedSpawnZ; public int RedSpawnZ;
[ConfigInt("base.blue.spawnx", null, 0)] [ConfigInt("base.blue.spawnx", null, 0)]
public int BlueSpawnX; public int BlueSpawnX;
[ConfigInt("base.blue.spawny", null, 0)] [ConfigInt("base.blue.spawny", null, 0)]
@ -57,7 +58,7 @@ namespace MCGalaxy.Games {
public int BlueSpawnZ; public int BlueSpawnZ;
[ConfigInt("map.line.z", null, 0)] [ConfigInt("map.line.z", null, 0)]
public int ZDivider; public int ZDivider;
[ConfigInt("game.maxpoints", null, 0)] [ConfigInt("game.maxpoints", null, 0)]
public int MaxPoints; public int MaxPoints;
[ConfigInt("game.tag.points-gain", null, 0)] [ConfigInt("game.tag.points-gain", null, 0)]
@ -69,13 +70,15 @@ namespace MCGalaxy.Games {
[ConfigInt("game.capture.points-lose", null, 0)] [ConfigInt("game.capture.points-lose", null, 0)]
public int Capture_PointsLost; public int Capture_PointsLost;
/// <summary> Sets the default CTF config values for the given map. </summary>
public void SetDefaults(Level map) { public void SetDefaults(Level map) {
ZDivider = map.Length / 2; ZDivider = map.Length / 2;
RedFlagBlock = Block.red; RedFlagBlock = Block.red;
BlueFlagBlock = Block.blue; BlueFlagBlock = Block.blue;
int midX = map.Width / 2, maxZ = map.Length - 1; int midX = map.Width / 2, maxZ = map.Length - 1;
RedFlagX = midX; RedSpawnX = midX * 32; RedFlagX = midX; RedSpawnX = midX * 32;
RedFlagY = 6; RedSpawnY = 4 * 32 + Entities.CharacterHeight; RedFlagY = 6; RedSpawnY = 4 * 32 + Entities.CharacterHeight;
RedFlagZ = 0; RedSpawnZ = 0 * 32; RedFlagZ = 0; RedSpawnZ = 0 * 32;
@ -89,5 +92,24 @@ namespace MCGalaxy.Games {
Capture_PointsGained = 10; Capture_PointsGained = 10;
Capture_PointsLost = 10; Capture_PointsLost = 10;
} }
static ConfigElement[] elems;
public void Retrieve(string mapName) {
if (elems == null) elems = ConfigElement.GetAll(typeof(CTFConfig));
PropertiesFile.Read("CTF/" + mapName + ".config", LineProcessor);
}
public void Save(string mapName) {
using (StreamWriter w = new StreamWriter("CTF/" + mapName + ".config")) {
ConfigElement.SerialisePlain(elems, w, this);
}
}
void LineProcessor(string key, string value) {
if (!ConfigElement.Parse(elems, key, value, this)) {
Logger.Log(LogType.Warning, "\"{0}\" was not a recognised CTF config key.", key);
}
}
} }
} }

View File

@ -48,12 +48,11 @@ namespace MCGalaxy.Games {
CtfTeam2 red; CtfTeam2 red;
CtfTeam2 blue; CtfTeam2 blue;
Level map; public Level map;
List<string> maps = new List<string>(); List<string> maps = new List<string>();
List<Data> cache = new List<Data>(); List<Data> cache = new List<Data>();
public CTFConfig Config = new CTFConfig(); public CTFConfig Config = new CTFConfig();
ConfigElement[] ctfConfigElems;
/// <summary> Create a new CTF object </summary> /// <summary> Create a new CTF object </summary>
public CTFGame() { public CTFGame() {
@ -95,15 +94,12 @@ namespace MCGalaxy.Games {
File.Copy("CTF/maps/" + mapName + ".lvl", "levels/ctf.lvl"); File.Copy("CTF/maps/" + mapName + ".lvl", "levels/ctf.lvl");
CmdLoad.LoadLevel(null, "ctf"); CmdLoad.LoadLevel(null, "ctf");
map = LevelInfo.FindExact("ctf"); map = LevelInfo.FindExact("ctf");
LoadMapConfig(); UpdateConfig();
} }
void LoadMapConfig() { public void UpdateConfig() {
if (ctfConfigElems == null)
ctfConfigElems = ConfigElement.GetAll(typeof(CTFConfig));
Config.SetDefaults(map); Config.SetDefaults(map);
PropertiesFile.Read("CTF/" + map.name + ".config", LineProcessor); Config.Retrieve(map.name);
CTFConfig cfg = Config; CTFConfig cfg = Config;
red.FlagBlock = ExtBlock.FromRaw(cfg.RedFlagBlock); red.FlagBlock = ExtBlock.FromRaw(cfg.RedFlagBlock);
@ -115,12 +111,6 @@ namespace MCGalaxy.Games {
blue.SpawnPos = new Position(cfg.BlueSpawnX, cfg.BlueSpawnY, cfg.BlueSpawnZ); blue.SpawnPos = new Position(cfg.BlueSpawnX, cfg.BlueSpawnY, cfg.BlueSpawnZ);
} }
void LineProcessor(string key, string value) {
if (!ConfigElement.Parse(ctfConfigElems, key, value, Config)) {
Logger.Log(LogType.Warning, "\"{0}\" was not a recognised CTF config key.", key);
}
}
bool LoadConfig() { bool LoadConfig() {
//Load some configs //Load some configs
if (!Directory.Exists("CTF")) Directory.CreateDirectory("CTF"); if (!Directory.Exists("CTF")) Directory.CreateDirectory("CTF");