mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-22 03:55:18 -04:00
less allocations
This commit is contained in:
parent
7ff12b4026
commit
5acc894351
@ -66,16 +66,16 @@ namespace MCGalaxy.Config {
|
||||
|
||||
static object ParseValue(int token, JsonContext ctx) {
|
||||
switch (token) {
|
||||
case '{': return ParseObject(ctx);
|
||||
case '[': return ParseArray(ctx);
|
||||
case '"': return ParseString(ctx);
|
||||
case '{': return ParseObject(ctx);
|
||||
case '[': return ParseArray(ctx);
|
||||
case '"': return ParseString(ctx);
|
||||
|
||||
case T_NUM: return ParseNumber(ctx);
|
||||
case T_TRUE: return "true";
|
||||
case T_FALSE: return "false";
|
||||
case T_NULL: return null;
|
||||
case T_NUM: return ParseNumber(ctx);
|
||||
case T_TRUE: return "true";
|
||||
case T_FALSE: return "false";
|
||||
case T_NULL: return null;
|
||||
|
||||
default: return null;
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
@ -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 ConfigByteAttribute() : this(null, null) { }
|
||||
public ConfigByteAttribute(string name, string section)
|
||||
: base(name, section, 0, 0, 255) { }
|
||||
public class ConfigByteAttribute : ConfigIntegerAttribute {
|
||||
public ConfigByteAttribute() : this(null, null) { }
|
||||
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;
|
||||
public override object Parse(string raw) {
|
||||
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;
|
||||
public override object Parse(string raw) {
|
||||
return (ushort)ParseInteger(raw, 0, 0, ushort.MaxValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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 = "";
|
||||
|
Loading…
x
Reference in New Issue
Block a user