mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-22 12:05:51 -04:00
Better error checking for property file reading.
This commit is contained in:
parent
8939de0698
commit
b63a8d8294
@ -20,44 +20,68 @@ using System.IO;
|
|||||||
|
|
||||||
namespace MCGalaxy {
|
namespace MCGalaxy {
|
||||||
|
|
||||||
public delegate void LineProcessor<T>(string key, string value, ref T state);
|
public delegate void LineProcessor<T>(string key, string value, ref T state);
|
||||||
|
|
||||||
/// <summary> Handles text files that have multiple key-value lines in the format 'key=value'.
|
/// <summary> Handles text files that have multiple key-value lines in the format 'key=value'.
|
||||||
/// Also supports # for commented lines. </summary>
|
/// Also supports # for commented lines. </summary>
|
||||||
public static class PropertiesFile {
|
public static class PropertiesFile {
|
||||||
|
|
||||||
public static bool Read(string path, Action<string, string> processor, char separator = '=') {
|
public static bool Read(string path, Action<string, string> processor,
|
||||||
|
char separator = '=', bool trimValue = true) {
|
||||||
if (!File.Exists(path)) return false;
|
if (!File.Exists(path)) return false;
|
||||||
|
|
||||||
using (CP437Reader reader = new CP437Reader(path)) {
|
using (CP437Reader reader = new CP437Reader(path)) {
|
||||||
string line;
|
string line;
|
||||||
while ((line = reader.ReadLine()) != null) {
|
while ((line = reader.ReadLine()) != null) {
|
||||||
if (line == "" || line[0] == '#') continue;
|
int index = ParseLine(line, path, separator);
|
||||||
int index = line.IndexOf(separator);
|
if (index == -1) continue;
|
||||||
|
|
||||||
string key = index < 0 ? line : line.Substring(0, index);
|
string key = line.Substring(0, index), value = line.Substring(index + 1);
|
||||||
string value = index < 0 ? "" : line.Substring(index + 1);
|
if (trimValue) value = value.Trim();
|
||||||
processor(key.Trim(), value.Trim());
|
|
||||||
|
try {
|
||||||
|
processor(key.Trim(), value);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
Server.ErrorLog(ex);
|
||||||
|
Server.s.Log("Line \"" + line + "\" in " + path + " caused an error");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool Read<T>(string path, ref T state, LineProcessor<T> processor, char separator = '=') {
|
public static bool Read<T>(string path, ref T state, LineProcessor<T> processor,
|
||||||
|
char separator = '=', bool trimValue = true) {
|
||||||
if (!File.Exists(path)) return false;
|
if (!File.Exists(path)) return false;
|
||||||
|
|
||||||
using (CP437Reader reader = new CP437Reader(path)) {
|
using (CP437Reader reader = new CP437Reader(path)) {
|
||||||
string line;
|
string line;
|
||||||
while ((line = reader.ReadLine()) != null) {
|
while ((line = reader.ReadLine()) != null) {
|
||||||
if (line == "" || line[0] == '#') continue;
|
int index = ParseLine(line, path, separator);
|
||||||
int index = line.IndexOf(separator);
|
if (index == -1) continue;
|
||||||
|
|
||||||
string key = index < 0 ? line : line.Substring(0, index);
|
string key = line.Substring(0, index), value = line.Substring(index + 1);
|
||||||
string value = index < 0 ? "" : line.Substring(index + 1);
|
if (trimValue) value = value.Trim();
|
||||||
processor(key.Trim(), value.Trim(), ref state);
|
|
||||||
|
try {
|
||||||
|
processor(key.Trim(), value, ref state);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
Server.ErrorLog(ex);
|
||||||
|
Server.s.Log("Line \"" + line + "\" in " + path + " caused an error");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int ParseLine(string line, string path, char separator) {
|
||||||
|
if (line == "" || line[0] == '#') return -1;
|
||||||
|
int index = line.IndexOf(separator);
|
||||||
|
if (index == -1) {
|
||||||
|
Server.s.Log("Line \"" + line + "\" in " + path + " is missing a value");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user