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) {
if (String.IsNullOrEmpty(value)) {
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);
} else {
WriteString(w, value);

View File

@ -20,7 +20,31 @@ using BlockID = System.UInt16;
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;
public ConfigIntAttribute()
@ -29,73 +53,50 @@ namespace MCGalaxy.Config {
int min = int.MinValue, int max = int.MaxValue)
: base(name, section) { defValue = def; minValue = min; maxValue = max; }
public override object Parse(string raw) {
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, 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;
public override object Parse(string value) {
return ParseInteger(value, defValue, minValue, maxValue);
}
}
// Hacky workaround for old ExponentialFog attribute
sealed class ConfigBoolIntAttribute : ConfigIntAttribute {
public ConfigBoolIntAttribute(string name, string section, int defValue)
: base(name, section, defValue, -1, 1) {
}
sealed class ConfigBoolIntAttribute : ConfigIntegerAttribute {
public ConfigBoolIntAttribute(string name, string section)
: base(name, section) { }
public override object Parse(string raw) {
bool value;
if (bool.TryParse(raw, out value)) { return value ? 1 : 0; }
return base.Parse(raw);
if (bool.TryParse(raw, out value)) return value ? 1 : 0;
return ParseInteger(raw, 0, -1, 1);
}
}
public class ConfigBlockAttribute : ConfigIntAttribute {
public ConfigBlockAttribute(string name, string section, int def)
: base(name, section, def, 0, Block.ExtendedCount - 1) {
}
public sealed class ConfigBlockAttribute : ConfigIntegerAttribute {
BlockID defBlock;
public ConfigBlockAttribute(string name, string section, BlockID def)
: base(name, section) { defBlock = def; }
public override object Parse(string raw) {
int value = (int)base.Parse(raw);
// Can't directly unbox object to block ID - must unbox to int, then cast to block ID
if (value == Block.Invalid) return Block.Invalid;
return Block.MapOldRaw((BlockID)value);
BlockID block = (BlockID)ParseInteger(raw, defBlock, 0, Block.ExtendedCount - 1);
if (block == Block.Invalid) return Block.Invalid;
return Block.MapOldRaw(block);
}
}
public class ConfigByteAttribute : ConfigIntAttribute {
public class ConfigByteAttribute : ConfigIntegerAttribute {
public ConfigByteAttribute() : this(null, null) { }
public ConfigByteAttribute(string name, string section)
: base(name, section, 0, 0, 255) { }
public ConfigByteAttribute(string name, string section) : base(name, section) { }
public override object Parse(string raw) {
int value = (int)base.Parse(raw);
// Can't directly unbox object to byte - must unbox to byte, then cast to byte
return (byte)value;
return (byte)ParseInteger(raw, 0, 0, byte.MaxValue);
}
}
public class ConfigUShortAttribute : ConfigIntAttribute {
public class ConfigUShortAttribute : ConfigIntegerAttribute {
public ConfigUShortAttribute() : this(null, null) { }
public ConfigUShortAttribute(string name, string section)
: base(name, section, 0, 0, 65535) { }
public ConfigUShortAttribute(string name, string section) : base(name, section) { }
public override object Parse(string raw) {
int value = (int)base.Parse(raw);
// Can't directly unbox object to ushort - must unbox to ushort, then cast to ushort
return (ushort)value;
return (ushort)ParseInteger(raw, 0, 0, ushort.MaxValue);
}
}

View File

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

View File

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