less allocations

This commit is contained in:
UnknownShadow200 2018-07-19 19:50:37 +10:00
parent 7ff12b4026
commit 5acc894351
4 changed files with 59 additions and 60 deletions

View File

@ -176,7 +176,7 @@ namespace MCGalaxy.Config {
static void WriteValue(StreamWriter w, ConfigAttribute a, string value) { static void WriteValue(StreamWriter w, ConfigAttribute a, string value) {
if (String.IsNullOrEmpty(value)) { if (String.IsNullOrEmpty(value)) {
w.Write("null"); w.Write("null");
} else if (a is ConfigBoolAttribute || a is ConfigIntAttribute || a is ConfigRealAttribute) { } else if (a is ConfigBoolAttribute || a is ConfigIntegerAttribute || a is ConfigRealAttribute) {
w.Write(value); w.Write(value);
} else { } else {
WriteString(w, value); WriteString(w, value);

View File

@ -20,7 +20,31 @@ using BlockID = System.UInt16;
namespace MCGalaxy.Config { namespace MCGalaxy.Config {
public class ConfigIntAttribute : ConfigAttribute { public abstract class ConfigIntegerAttribute : ConfigAttribute {
public ConfigIntegerAttribute(string name, string section)
: base(name, section) { }
// separate function to avoid boxing in derived classes
protected int ParseInteger(string raw, int def, int min, int max) {
int value;
if (!int.TryParse(raw, out value)) {
Logger.Log(LogType.Warning, "Config key \"{0}\" is not a valid integer, using default of {1}", Name, def);
value = def;
}
if (value < min) {
Logger.Log(LogType.Warning, "Config key \"{0}\" is too small an integer, using {1}", Name, min);
value = min;
}
if (value > max) {
Logger.Log(LogType.Warning, "Config key \"{0}\" is too big an integer, using {1}", Name, max);
value = max;
}
return value;
}
}
public sealed class ConfigIntAttribute : ConfigIntegerAttribute {
int defValue, minValue, maxValue; int defValue, minValue, maxValue;
public ConfigIntAttribute() public ConfigIntAttribute()
@ -29,73 +53,50 @@ namespace MCGalaxy.Config {
int min = int.MinValue, int max = int.MaxValue) int min = int.MinValue, int max = int.MaxValue)
: base(name, section) { defValue = def; minValue = min; maxValue = max; } : base(name, section) { defValue = def; minValue = min; maxValue = max; }
public override object Parse(string raw) { public override object Parse(string value) {
int value; return ParseInteger(value, defValue, minValue, maxValue);
if (!int.TryParse(raw, out value)) {
Logger.Log(LogType.Warning, "Config key \"{0}\" is not a valid integer, using default of {1}", Name, defValue);
value = defValue;
}
if (value < minValue) {
Logger.Log(LogType.Warning, "Config key \"{0}\" is too small an integer, using {1}", Name, minValue);
value = minValue;
}
if (value > maxValue) {
Logger.Log(LogType.Warning, "Config key \"{0}\" is too big an integer, using {1}", Name, maxValue);
value = maxValue;
}
return value;
} }
} }
// Hacky workaround for old ExponentialFog attribute // Hacky workaround for old ExponentialFog attribute
sealed class ConfigBoolIntAttribute : ConfigIntAttribute { sealed class ConfigBoolIntAttribute : ConfigIntegerAttribute {
public ConfigBoolIntAttribute(string name, string section, int defValue) public ConfigBoolIntAttribute(string name, string section)
: base(name, section, defValue, -1, 1) { : base(name, section) { }
}
public override object Parse(string raw) { public override object Parse(string raw) {
bool value; bool value;
if (bool.TryParse(raw, out value)) { return value ? 1 : 0; } if (bool.TryParse(raw, out value)) return value ? 1 : 0;
return base.Parse(raw); return ParseInteger(raw, 0, -1, 1);
} }
} }
public class ConfigBlockAttribute : ConfigIntAttribute { public sealed class ConfigBlockAttribute : ConfigIntegerAttribute {
public ConfigBlockAttribute(string name, string section, int def) BlockID defBlock;
: base(name, section, def, 0, Block.ExtendedCount - 1) { public ConfigBlockAttribute(string name, string section, BlockID def)
} : base(name, section) { defBlock = def; }
public override object Parse(string raw) { public override object Parse(string raw) {
int value = (int)base.Parse(raw); BlockID block = (BlockID)ParseInteger(raw, defBlock, 0, Block.ExtendedCount - 1);
if (block == Block.Invalid) return Block.Invalid;
// Can't directly unbox object to block ID - must unbox to int, then cast to block ID return Block.MapOldRaw(block);
if (value == Block.Invalid) return Block.Invalid;
return Block.MapOldRaw((BlockID)value);
} }
} }
public class ConfigByteAttribute : ConfigIntAttribute { public class ConfigByteAttribute : ConfigIntegerAttribute {
public ConfigByteAttribute() : this(null, null) { } public ConfigByteAttribute() : this(null, null) { }
public ConfigByteAttribute(string name, string section) public ConfigByteAttribute(string name, string section) : base(name, section) { }
: base(name, section, 0, 0, 255) { }
public override object Parse(string raw) { public override object Parse(string raw) {
int value = (int)base.Parse(raw); return (byte)ParseInteger(raw, 0, 0, byte.MaxValue);
// Can't directly unbox object to byte - must unbox to byte, then cast to byte
return (byte)value;
} }
} }
public class ConfigUShortAttribute : ConfigIntAttribute { public class ConfigUShortAttribute : ConfigIntegerAttribute {
public ConfigUShortAttribute() : this(null, null) { } public ConfigUShortAttribute() : this(null, null) { }
public ConfigUShortAttribute(string name, string section) public ConfigUShortAttribute(string name, string section) : base(name, section) { }
: base(name, section, 0, 0, 65535) { }
public override object Parse(string raw) { public override object Parse(string raw) {
int value = (int)base.Parse(raw); return (ushort)ParseInteger(raw, 0, 0, ushort.MaxValue);
// Can't directly unbox object to ushort - must unbox to ushort, then cast to ushort
return (ushort)value;
} }
} }

View File

@ -317,9 +317,7 @@ namespace MCGalaxy.SQL {
if (state != ConnectionState.Closed) throw new InvalidOperationException(); if (state != ConnectionState.Closed) throw new InvalidOperationException();
Close(); Close();
string path = Utils.FolderPath + "/MCGalaxy.db"; const string path = "MCGalaxy.db";
path = Path.GetFullPath(path);
try { try {
if (ServerConfig.DatabasePooling) handle = RemoveFromPool(); if (ServerConfig.DatabasePooling) handle = RemoveFromPool();

View File

@ -85,7 +85,7 @@ namespace MCGalaxy {
[ConfigBlock("EdgeBlock", "Env", Block.Bedrock)] [ConfigBlock("EdgeBlock", "Env", Block.Bedrock)]
public BlockID EdgeBlock = Block.Invalid; public BlockID EdgeBlock = Block.Invalid;
/// <summary> Whether exponential fog mode is used client-side. </summary> /// <summary> Whether exponential fog mode is used client-side. </summary>
[ConfigBoolInt("ExpFog", "Env", 0)] [ConfigBoolInt("ExpFog", "Env")]
public int ExpFog = -1; public int ExpFog = -1;
[ConfigString("Texture", "Env", "", true)] [ConfigString("Texture", "Env", "", true)]
public string Terrain = ""; public string Terrain = "";