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

@ -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,14 +109,16 @@ 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
@ -141,7 +143,6 @@ namespace MCGalaxy.Commands {
} }
} }
} }
}
static bool IsDescription(string arg) { static bool IsDescription(string arg) {
foreach (char c in arg) { foreach (char c in arg) {

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, ':');

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

@ -45,18 +45,14 @@ namespace MCGalaxy.Drawing.Brushes {
continue; continue;
} }
BlockID block;
int sepIndex = parts[i].IndexOf('/'); int sepIndex = parts[i].IndexOf('/');
string name = sepIndex >= 0 ? parts[i].Substring(0, sepIndex) : parts[i]; string arg = sepIndex >= 0 ? parts[i].Substring(0, sepIndex) : parts[i];
if (!CommandParser.GetBlockIfAllowed(p, name, out block, true)) return null; if (!CommandParser.GetBlockIfAllowed(p, arg, out blocks[j], 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;

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

@ -36,8 +36,6 @@ namespace MCGalaxy.Scripting {
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;