Style: use less .NET 4.0 methods

This commit is contained in:
UnknownShadow200 2016-12-23 11:19:53 +11:00
parent 85fed97805
commit b635045f0e
5 changed files with 24 additions and 4 deletions

View File

@ -59,7 +59,7 @@ namespace MCGalaxy.Commands {
HUD.UpdateAllSecondary(Server.zombie);
} else if (args[0].CaselessEq("build") || args[0].CaselessEq("buildtype")) {
BuildType value;
if (!Enum.TryParse(args[1], true, out value)) {
if (!Utils.TryParseEnum(args[1], out value)) {
Player.Message(p, "Value must be 'normal', 'modifyonly', or 'nomodify'"); return;
}

View File

@ -401,7 +401,7 @@ namespace MCGalaxy.Generator {
static bool GenerateMap(MapGenArgs genArgs) {
MapGenTheme theme = MapGenTheme.Forest;
if (genArgs.Args != "" && !Enum.TryParse(genArgs.Args, true, out theme)) {
if (genArgs.Args != "" && !Utils.TryParseEnum(genArgs.Args, out theme)) {
string[] themes = Enum.GetNames(typeof(MapGenTheme));
Player.Message(genArgs.Player, "Seed must be one of the following themes: " + themes.Join());
return false;

View File

@ -59,7 +59,7 @@ namespace MCGalaxy {
Player.Message(pl, "\"{0}\" is not a valid level name.", name); return null;
}
var files = Directory.EnumerateFiles("levels", "*.lvl");
string[] files = Directory.GetFiles("levels", "*.lvl");
string map = Utils.FindMatches<string>(pl, name, out matches, files,
l => true, l => Path.GetFileNameWithoutExtension(l), "levels");
if (map != null)

View File

@ -79,8 +79,18 @@ namespace MCGalaxy {
return new string(a);
}
// Duplicated to avoid the memory allocation overhead from formatter delegate
public static string Join(this IEnumerable<string> items, string separator = ", ") {
return String.Join(separator, items);
StringBuilder builder = new StringBuilder();
bool first = true;
foreach (string value in items) {
if (value == null) continue;
if (!first) builder.Append(separator);
builder.Append(value);
first = false;
}
return builder.ToString();
}
public static string Join<T>(this IEnumerable<T> items,

View File

@ -67,6 +67,16 @@ namespace MCGalaxy {
*srcByte = value; srcByte++;
}
}
public static bool TryParseEnum<TEnum>(string value, out TEnum result) where TEnum : struct {
try {
result = (TEnum)Enum.Parse(typeof(TEnum), value, true);
return true;
} catch (Exception) {
result = default(TEnum);
return false;
}
}
public static int Clamp(int value, int lo, int hi) {
return Math.Max(Math.Min(value, hi), lo);