few less allocations

This commit is contained in:
UnknownShadow200 2018-06-27 09:56:12 +10:00
parent dd02b68dcb
commit c7ce45e64c
13 changed files with 73 additions and 69 deletions

View File

@ -61,7 +61,7 @@ namespace MCGalaxy.Blocks {
} }
public void MessageCannotUse(Player p, string action) { public void MessageCannotUse(Player p, string action) {
Player.Message(p, "Only {0} can {1} {2}", Player.Message(p, "Only {0} can {1} {2}",
Describe(), action, Block.GetName(p, ID)); Describe(), action, Block.GetName(p, ID));
} }
@ -92,8 +92,9 @@ namespace MCGalaxy.Blocks {
// Custom permissions set by the user. // Custom permissions set by the user.
if (File.Exists(Paths.BlockPermsFile)) { if (File.Exists(Paths.BlockPermsFile)) {
string[] lines = File.ReadAllLines(Paths.BlockPermsFile); using (StreamReader r = new StreamReader(Paths.BlockPermsFile)) {
ProcessLines(lines); ProcessLines(r);
}
} else { } else {
Save(); Save();
} }
@ -103,9 +104,11 @@ namespace MCGalaxy.Blocks {
} }
} }
static void ProcessLines(string[] lines) { static void ProcessLines(StreamReader r) {
string[] args = new string[4]; string[] args = new string[4];
foreach (string line in lines) { string line;
while ((line = r.ReadLine()) != null) {
if (line.Length == 0 || line[0] == '#') continue; if (line.Length == 0 || line[0] == '#') continue;
// Format - ID : Lowest : Disallow : Allow // Format - ID : Lowest : Disallow : Allow
line.Replace(" ", "").FixedSplit(args, ':'); line.Replace(" ", "").FixedSplit(args, ':');

View File

@ -109,36 +109,37 @@ namespace MCGalaxy.Commands {
lock (ioLock) { lock (ioLock) {
if (!File.Exists(Paths.CmdExtraPermsFile)) Save(); if (!File.Exists(Paths.CmdExtraPermsFile)) Save();
LoadCore(); using (StreamReader r = new StreamReader(Paths.CmdExtraPermsFile)) {
ProcessLines(r);
}
} }
} }
static void LoadCore() { static void ProcessLines(StreamReader r) {
string[] args = new string[5]; string[] args = new string[5];
using (StreamReader r = new StreamReader(Paths.CmdExtraPermsFile)) { string line;
string line;
while ((line = r.ReadLine()) != null) { while ((line = r.ReadLine()) != null) {
if (line.Length == 0 || line[0] == '#' || line.IndexOf(':') == -1) continue; if (line.Length == 0 || line[0] == '#' || line.IndexOf(':') == -1) continue;
// Format - Name:Num : Lowest : Disallow : Allow // Format - Name:Num : Lowest : Disallow : Allow
line.Replace(" ", "").FixedSplit(args, ':'); line.Replace(" ", "").FixedSplit(args, ':');
try {
LevelPermission min;
List<LevelPermission> allowed, disallowed;
try { // Old format - Name:Num : Lowest : Description
LevelPermission min; if (IsDescription(args[3])) {
List<LevelPermission> allowed, disallowed; min = (LevelPermission)int.Parse(args[2]);
allowed = null; disallowed = null;
// Old format - Name:Num : Lowest : Description } else {
if (IsDescription(args[3])) { Deserialise(args, 2, out min, out allowed, out disallowed);
min = (LevelPermission)int.Parse(args[2]);
allowed = null; disallowed = null;
} else {
Deserialise(args, 2, out min, out allowed, out disallowed);
}
Set(args[0], int.Parse(args[1]), "", min, allowed, disallowed);
} catch (Exception ex) {
Logger.Log(LogType.Warning, "Hit an error on the extra command perms " + line);
Logger.LogError(ex);
} }
Set(args[0], int.Parse(args[1]), "", min, allowed, disallowed);
} catch (Exception ex) {
Logger.Log(LogType.Warning, "Hit an error on the extra command perms " + line);
Logger.LogError(ex);
} }
} }
} }

View File

@ -97,8 +97,9 @@ namespace MCGalaxy.Commands {
} }
if (File.Exists(Paths.CmdPermsFile)) { if (File.Exists(Paths.CmdPermsFile)) {
string[] lines = File.ReadAllLines(Paths.CmdPermsFile); using (StreamReader r = new StreamReader(Paths.CmdPermsFile)) {
ProcessLines(lines); ProcessLines(r);
}
} else { } else {
Save(); Save();
} }
@ -108,9 +109,11 @@ namespace MCGalaxy.Commands {
} }
} }
static void ProcessLines(string[] lines) { static void ProcessLines(StreamReader r) {
string[] args = new string[4]; string[] args = new string[4];
foreach (string line in lines) { string line;
while ((line = r.ReadLine()) != null) {
if (line.Length == 0 || line[0] == '#') continue; if (line.Length == 0 || line[0] == '#') continue;
// Format - Name : Lowest : Disallow : Allow // Format - Name : Lowest : Disallow : Allow
line.Replace(" ", "").FixedSplit(args, ':'); line.Replace(" ", "").FixedSplit(args, ':');
@ -120,7 +123,7 @@ namespace MCGalaxy.Commands {
List<LevelPermission> allowed, disallowed; List<LevelPermission> allowed, disallowed;
Deserialise(args, 1, out min, out allowed, out disallowed); Deserialise(args, 1, out min, out allowed, out disallowed);
Set(args[0], min, allowed, disallowed); Set(args[0], min, allowed, disallowed);
} catch { } catch {
Logger.Log(LogType.Warning, "Hit an error on the command " + line); continue; Logger.Log(LogType.Warning, "Hit an error on the command " + line); continue;
} }

View File

@ -148,7 +148,7 @@ namespace MCGalaxy.Commands.Moderation {
List<string> reports = new List<string>(); List<string> reports = new List<string>();
if (File.Exists("extra/reported/" + target + ".txt")) { if (File.Exists("extra/reported/" + target + ".txt")) {
reports = new List<string>(File.ReadAllLines("extra/reported/" + target + ".txt")); reports = Utils.ReadAllLinesList("extra/reported/" + target + ".txt");
} }
ItemPerms checkPerms = CommandExtraPerms.Find(name, 1); ItemPerms checkPerms = CommandExtraPerms.Find(name, 1);

View File

@ -30,8 +30,8 @@ namespace MCGalaxy.Commands.Building {
public override LevelPermission defaultRank { get { return LevelPermission.Admin; } } public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }
public override void Use(Player p, string message) { public override void Use(Player p, string message) {
BrushArgs args = new BrushArgs(p, message.ToLower(), Block.Air); BrushArgs args = new BrushArgs(p, message, Block.Air);
Brush brush = BrushFactory.Find("replace").Construct(args); Brush brush = BrushFactory.Find("Replace").Construct(args);
if (brush == null) return; if (brush == null) return;
Vec3S32 max = new Vec3S32(p.level.Width - 1, p.level.Height - 1, p.level.Length - 1); Vec3S32 max = new Vec3S32(p.level.Width - 1, p.level.Height - 1, p.level.Length - 1);

View File

@ -38,9 +38,9 @@ namespace MCGalaxy {
char separator = '=', bool trimValue = true) { char separator = '=', bool trimValue = true) {
if (!File.Exists(path)) return false; if (!File.Exists(path)) return false;
using (StreamReader reader = new StreamReader(path)) { using (StreamReader r = new StreamReader(path)) {
string line, key, value; string line, key, value;
while ((line = reader.ReadLine()) != null) { while ((line = r.ReadLine()) != null) {
ParseLine(line, separator, out key, out value); ParseLine(line, separator, out key, out value);
if (key == null) continue; if (key == null) continue;

View File

@ -22,12 +22,12 @@ using BlockID = System.UInt16;
namespace MCGalaxy.Drawing.Brushes { namespace MCGalaxy.Drawing.Brushes {
/// <summary> Contains helper methods for brushes that have blocks with /// <summary> Contains helper methods for brushes that have blocks with
/// optional frequency counts (e.g. random and cloudy brushes) </summary> /// optional frequency counts (e.g. random and cloudy brushes) </summary>
public static class FrequencyBrush { public static class FrequencyBrush {
public static BlockID[] GetBlocks(BrushArgs args, out int[] count, public static BlockID[] GetBlocks(BrushArgs args, out int[] count,
Predicate<string> argFilter, Predicate<string> argHandler) { Predicate<string> argFilter, Predicate<string> argHandler) {
string[] parts = args.Message.SplitSpaces(); string[] parts = args.Message.SplitSpaces();
Player p = args.Player; Player p = args.Player;
BlockID[] blocks; BlockID[] blocks;
@ -45,18 +45,14 @@ namespace MCGalaxy.Drawing.Brushes {
continue; continue;
} }
BlockID block; int sepIndex = parts[i].IndexOf('/');
int sepIndex = parts[i].IndexOf('/'); string arg = sepIndex >= 0 ? parts[i].Substring(0, sepIndex) : parts[i];
string name = sepIndex >= 0 ? parts[i].Substring(0, sepIndex) : parts[i]; if (!CommandParser.GetBlockIfAllowed(p, arg, out blocks[j], true)) return null;
if (!CommandParser.GetBlockIfAllowed(p, name, out block, true)) return null;
blocks[j] = block; if (sepIndex >= 0) {
if (sepIndex < 0) { j++; continue; } arg = parts[i].Substring(sepIndex + 1);
if (!CommandParser.GetInt(p, arg, "Frequency", ref count[j], 1, 10000)) return null;
int chance = 0; }
if (!CommandParser.GetInt(p, parts[i].Substring(sepIndex + 1), "Frequency", ref chance, 1, 10000)) return null;
count[j] = chance;
j++; j++;
} }
return blocks; return blocks;
@ -96,7 +92,7 @@ namespace MCGalaxy.Drawing.Brushes {
} }
public sealed class RandomBrushFactory : BrushFactory { public sealed class RandomBrushFactory : BrushFactory {
public override string Name { get { return "Random"; } } public override string Name { get { return "Random"; } }
public override string[] Help { get { return HelpString; } } public override string[] Help { get { return HelpString; } }
static string[] HelpString = new string[] { static string[] HelpString = new string[] {

View File

@ -41,7 +41,7 @@ namespace MCGalaxy.Games {
if (Running) EndRound(); if (Running) EndRound();
if (Running) VoteAndMoveToNextMap(); if (Running) VoteAndMoveToNextMap();
} }
protected override bool SetMap(string map) { protected override bool SetMap(string map) {
bool success = base.SetMap(map); bool success = base.SetMap(map);
if (success) UpdateMapConfig(); if (success) UpdateMapConfig();

View File

@ -46,23 +46,23 @@ namespace MCGalaxy {
public string Color; public string Color;
public string ColoredName { get { return Color + Name; } } public string ColoredName { get { return Color + Name; } }
[ConfigInt("Limit", null, 0)] [ConfigInt("Limit", null, 0, 0)]
public int DrawLimit; public int DrawLimit;
[ConfigInt("MaxUndo", null, 0)] [ConfigInt("MaxUndo", null, 0, 0)]
public int MaxUndo; public int MaxUndo;
[ConfigString("MOTD", null, "", true)] [ConfigString("MOTD", null, "", true)]
public string MOTD = ""; public string MOTD = "";
[ConfigInt("GenVolume", null, mapGenLimit)] [ConfigInt("GenVolume", null, mapGenLimit)]
public int GenVolume = mapGenLimit; public int GenVolume = mapGenLimit;
[ConfigInt("OSMaps", null, 3)] [ConfigInt("OSMaps", null, 3, 0)]
public int OverseerMaps = 3; public int OverseerMaps = 3;
[ConfigBool("AfkKicked", null, true)] [ConfigBool("AfkKicked", null, true)]
public bool AfkKicked = true; public bool AfkKicked = true;
[ConfigInt("AfkKickMinutes", null, 45)] [ConfigInt("AfkKickMinutes", null, 45, 0)]
public int AfkKickMinutes = 45; public int AfkKickMinutes = 45;
[ConfigString("Prefix", null, "", true)] [ConfigString("Prefix", null, "", true)]
public string Prefix = ""; public string Prefix = "";
[ConfigInt("CopySlots", null, 0)] [ConfigInt("CopySlots", null, 0, 0)]
public int CopySlots = 1; public int CopySlots = 1;
[ConfigString("Filename", null, "", true, ".,_-+=")] [ConfigString("Filename", null, "", true, ".,_-+=")]
internal string filename; internal string filename;

View File

@ -683,7 +683,10 @@ namespace MCGalaxy {
byte bindIndex; byte bindIndex;
if (byte.TryParse(cmdName, out bindIndex) && bindIndex < CmdBindings.Length) { if (byte.TryParse(cmdName, out bindIndex) && bindIndex < CmdBindings.Length) {
if (CmdArgsBindings[bindIndex] == null) { SendMessage("No command is bound to: /" + cmdName); return null; } if (CmdArgsBindings[bindIndex] == null) {
SendMessage("No command is bound to: %T/" + cmdName); return null;
}
cmdName = CmdBindings[bindIndex]; cmdName = CmdBindings[bindIndex];
cmdArgs = CmdArgsBindings[bindIndex] + " " + cmdArgs; cmdArgs = CmdArgsBindings[bindIndex] + " " + cmdArgs;
cmdArgs = cmdArgs.TrimEnd(' '); cmdArgs = cmdArgs.TrimEnd(' ');
@ -695,7 +698,7 @@ namespace MCGalaxy {
Command command = Command.Find(cmdName); Command command = Command.Find(cmdName);
if (command == null) { if (command == null) {
if (Block.Parse(this, cmdName) != Block.Invalid) { if (Block.Parse(this, cmdName) != Block.Invalid) {
cmdArgs = cmdName.ToLower(); cmdName = "mode"; cmdArgs = cmdName; cmdName = "mode";
command = Command.Find("Mode"); command = Command.Find("Mode");
} else { } else {
Logger.Log(LogType.CommandUsage, "{0} tried to use unknown command: /{1} {2}", name, cmdName, cmdArgs); Logger.Log(LogType.CommandUsage, "{0} tried to use unknown command: /{1} {2}", name, cmdName, cmdArgs);

View File

@ -34,10 +34,8 @@ namespace MCGalaxy.Scripting {
public const string DllDir = "extra/commands/dll/"; public const string DllDir = "extra/commands/dll/";
public const string ErrorPath = "logs/errors/compiler.log"; public const string ErrorPath = "logs/errors/compiler.log";
static readonly string divider = new string('-', 25); static readonly string divider = new string('-', 25);
protected CodeDomProvider compiler; protected CodeDomProvider compiler;
protected CompilerParameters args = new CompilerParameters();
protected CompilerResults results;
public abstract string Ext { get; } public abstract string Ext { get; }
public abstract string ProviderName { get; } public abstract string ProviderName { get; }
@ -94,7 +92,7 @@ namespace MCGalaxy.Scripting {
args.OutputAssembly = dstPath; args.OutputAssembly = dstPath;
List<string> source = ReadSourceCode(srcPath, args); List<string> source = ReadSourceCode(srcPath, args);
results = CompileSource(source.Join(Environment.NewLine), args); CompilerResults results = CompileSource(source.Join(Environment.NewLine), args);
if (!results.Errors.HasErrors) return true; if (!results.Errors.HasErrors) return true;
sb = new StringBuilder(); sb = new StringBuilder();

View File

@ -109,11 +109,11 @@ namespace MCGalaxy {
} }
} }
static string NextStatement(StreamReader reader, List<string> buffer) { static string NextStatement(StreamReader r, List<string> buffer) {
buffer.Clear(); buffer.Clear();
string line = null; string line = null;
while ((line = reader.ReadLine()) != null) { while ((line = r.ReadLine()) != null) {
if (line.StartsWith("--")) continue; // comment if (line.StartsWith("--")) continue; // comment
line = line.Trim(); line = line.Trim();
if (line.Length == 0) continue; // whitespace if (line.Length == 0) continue; // whitespace

View File

@ -151,8 +151,8 @@ namespace MCGalaxy.Tasks {
if (!File.Exists(Paths.TempRanksFile)) return; if (!File.Exists(Paths.TempRanksFile)) return;
// Check if empty, or not old form // Check if empty, or not old form
using (StreamReader reader = new StreamReader(Paths.TempRanksFile)) { using (StreamReader r = new StreamReader(Paths.TempRanksFile)) {
string line = reader.ReadLine(); string line = r.ReadLine();
if (line == null) return; if (line == null) return;
string[] parts = line.SplitSpaces(); string[] parts = line.SplitSpaces();
if (parts.Length < 9) return; if (parts.Length < 9) return;