Eliminate some useless File.Exists calls

This commit is contained in:
UnknownShadow200 2017-09-09 19:53:58 +10:00
parent 83e6172137
commit 89a8823981
8 changed files with 26 additions and 35 deletions

View File

@ -217,7 +217,7 @@ namespace MCGalaxy.Commands.Info {
Width = dims.X; Height = dims.Y; Length = dims.Z;
BlockDBEntries = BlockDBFile.CountEntries(name);
path = LevelInfo.FindPropertiesFile(name);
path = LevelInfo.PropertiesPath(name);
LevelConfig cfg = new LevelConfig();
cfg.EdgeLevel = Height / 2; cfg.CloudsHeight = Height + 2;
LevelConfig.Load(path, cfg);

View File

@ -30,18 +30,15 @@ namespace MCGalaxy.Commands.Info {
public override void Use(Player p, string message) {
string[] files = LevelInfo.AllMapFiles();
for (int i = 0; i < files.Length; i++) {
files[i] = Path.GetFileNameWithoutExtension(files[i]);
}
Player.Message(p, "Maps (&c[no] %Sif not visitable): ");
MultiPageOutput.Output(p, files, (map) => FormatMap(p, map),
MultiPageOutput.Output(p, files, (file) => FormatMap(p, file),
"Worlds", "maps", message, false);
}
static string FormatMap(Player p, string map) {
static string FormatMap(Player p, string file) {
LevelPermission visitP, buildP;
bool loadOnGoto;
string map = Path.GetFileNameWithoutExtension(file);
RetrieveProps(map, out visitP, out buildP, out loadOnGoto);
LevelPermission maxPerm = visitP;
@ -57,10 +54,9 @@ namespace MCGalaxy.Commands.Info {
build = LevelPermission.Guest;
loadOnGoto = true;
string file = LevelInfo.FindPropertiesFile(level);
if (file == null) return;
string propsPath = LevelInfo.PropertiesPath(level);
SearchArgs args = new SearchArgs();
PropertiesFile.Read(file, ref args, ProcessLine);
if (!PropertiesFile.Read(propsPath, ref args, ProcessLine)) return;
visit = Group.ParsePermOrName(args.Visit, visit);
build = Group.ParsePermOrName(args.Build, build);

View File

@ -71,9 +71,9 @@ namespace MCGalaxy.Commands.World {
Level lvl = LevelInfo.FindExact(map);
if (lvl != null) return lvl.Config;
string propsPath = LevelInfo.FindPropertiesFile(map);
string propsPath = LevelInfo.PropertiesPath(map);
LevelConfig cfg = new LevelConfig();
if (propsPath != null) LevelConfig.Load(propsPath, cfg);
LevelConfig.Load(propsPath, cfg);
return cfg;
}

View File

@ -221,8 +221,8 @@ namespace MCGalaxy {
if (lvl.IsMuseum) return; // museums do not save properties
lock (lvl.savePropsLock) {
string path = LevelInfo.PropertiesPath(lvl.MapName);
LevelConfig.Save(path, lvl.Config, lvl.name);
string propsPath = LevelInfo.PropertiesPath(lvl.MapName);
LevelConfig.Save(propsPath, lvl.Config, lvl.name);
}
}
@ -363,9 +363,10 @@ namespace MCGalaxy {
public static void LoadMetadata(Level lvl) {
try {
string propsPath = LevelInfo.FindPropertiesFile(lvl.MapName);
if (propsPath != null) {
LevelConfig.Load(propsPath, lvl.Config);
string propsPath = LevelInfo.PropertiesPath(lvl.MapName);
bool propsExisted = LevelConfig.Load(propsPath, lvl.Config);
if (propsExisted) {
lvl.setPhysics(lvl.Config.Physics);
} else {
Logger.Log(LogType.ConsoleMessage, ".properties file for level {0} was not found.", lvl.MapName);

View File

@ -201,8 +201,8 @@ namespace MCGalaxy {
}
public static void Load(string path, LevelConfig config) {
PropertiesFile.Read(path, ref config, LineProcessor);
public static bool Load(string path, LevelConfig config) {
return PropertiesFile.Read(path, ref config, LineProcessor);
}
static void LineProcessor(string key, string value, ref LevelConfig config) {

View File

@ -88,21 +88,15 @@ namespace MCGalaxy {
public static string PropertiesPath(string name) {
return "levels/level properties/" + name + ".properties";
}
public static string FindPropertiesFile(string name) {
string file = "levels/level properties/" + name + ".properties";
if (!File.Exists(file)) file = "levels/level properties/" + name;
return File.Exists(file) ? file : null;
}
public static string FindOfflineProperty(string name, string propKey) {
string file = FindPropertiesFile(name);
if (file == null) return null;
string path = PropertiesPath(name);
if (!File.Exists(path)) return null;
string[] lines = null;
try {
lines = File.ReadAllLines(file);
lines = File.ReadAllLines(path);
} catch {
return null;
}

View File

@ -80,7 +80,7 @@ namespace MCGalaxy {
}
static bool LoadOfflineLevel(Player p, string name) {
string propsPath = LevelInfo.FindPropertiesFile(name);
string propsPath = LevelInfo.PropertiesPath(name);
LevelConfig cfg = new LevelConfig();
LevelConfig.Load(propsPath, cfg);

View File

@ -116,10 +116,11 @@ namespace MCGalaxy.Tasks {
static void Combine(string envFile) {
string name = Path.GetFileNameWithoutExtension(envFile);
string propFile = LevelInfo.FindPropertiesFile(name);
string propsPath = LevelInfo.PropertiesPath(name);
List<string> lines = new List<string>();
if (propFile != null) {
lines = Utils.ReadAllLinesList(propFile);
if (File.Exists(propsPath)) {
lines = Utils.ReadAllLinesList(propsPath);
}
using (StreamReader r = new StreamReader(envFile)) {
@ -128,8 +129,7 @@ namespace MCGalaxy.Tasks {
lines.Add(line);
}
propFile = LevelInfo.PropertiesPath(name);
File.WriteAllLines(propFile, lines.ToArray());
File.WriteAllLines(propsPath, lines.ToArray());
File.Delete(envFile);
}