mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-23 04:32:50 -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) {
|
static object ParseValue(int token, JsonContext ctx) {
|
||||||
switch (token) {
|
switch (token) {
|
||||||
case '{': return ParseObject(ctx);
|
case '{': return ParseObject(ctx);
|
||||||
case '[': return ParseArray(ctx);
|
case '[': return ParseArray(ctx);
|
||||||
case '"': return ParseString(ctx);
|
case '"': return ParseString(ctx);
|
||||||
|
|
||||||
case T_NUM: return ParseNumber(ctx);
|
case T_NUM: return ParseNumber(ctx);
|
||||||
case T_TRUE: return "true";
|
case T_TRUE: return "true";
|
||||||
case T_FALSE: return "false";
|
case T_FALSE: return "false";
|
||||||
case T_NULL: return null;
|
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) {
|
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);
|
||||||
|
@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
|
||||||
|
@ -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 = "";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user