Skip to content

Commit

Permalink
Cleanup BotsFile.cs a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Dec 18, 2020
1 parent 1e09de0 commit 695c867
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
12 changes: 5 additions & 7 deletions MCGalaxy/Bots/BotsFile.cs
Expand Up @@ -24,19 +24,16 @@ namespace MCGalaxy.Bots {

/// <summary> Maintains persistent data for in-game bots. </summary>
public static class BotsFile {

public static string BotsPath(string map) { return "extra/bots/" + map + ".json"; }
static ConfigElement[] elems;

public static void Load(Level lvl) { lock (lvl.botsIOLock) { LoadCore(lvl); } }
static void LoadCore(Level lvl) {
string path = BotsPath(lvl.MapName);
string path = Paths.BotsPath(lvl.MapName);
if (!File.Exists(path)) return;
string json = File.ReadAllText(path);
List<BotProperties> props = null;

try {
props = ReadAll(json);
props = ReadAll(path);
} catch (Exception ex) {
Logger.LogError("Reading bots file", ex); return;
}
Expand All @@ -51,9 +48,10 @@ public static class BotsFile {
}
}

internal static List<BotProperties> ReadAll(string json) {
internal static List<BotProperties> ReadAll(string path) {
List<BotProperties> props = new List<BotProperties>();
if (elems == null) elems = ConfigElement.GetAll(typeof(BotProperties));
string json = File.ReadAllText(path);

bool success;
JsonArray array = (JsonArray)Json.Parse(json, out success);
Expand All @@ -75,7 +73,7 @@ public static class BotsFile {
public static void Save(Level lvl) { lock (lvl.botsIOLock) { SaveCore(lvl); } }
static void SaveCore(Level lvl) {
PlayerBot[] bots = lvl.Bots.Items;
string path = BotsPath(lvl.MapName);
string path = Paths.BotsPath(lvl.MapName);
if (!File.Exists(path) && bots.Length == 0) return;

List<BotProperties> props = new List<BotProperties>(bots.Length);
Expand Down
8 changes: 4 additions & 4 deletions MCGalaxy/Levels/LevelActions.cs
Expand Up @@ -41,10 +41,10 @@ public static class LevelActions {

bool lvl = DoAction(LevelInfo.MapPath(map), Path.Combine(path, map + ".lvl"), action_copy);
bool props = DoAction(LevelInfo.PropsPath(map), Path.Combine(path, "map.properties"), action_copy);
bool defs = DoAction(Paths.MapBlockDefs(map), Path.Combine(path, "blockdefs.json"), action_copy);
bool defs = DoAction(Paths.MapBlockDefs(map), Path.Combine(path, "blockdefs.json"), action_copy);
bool blkOld = DoAction(BlockPropsOldPath(map), Path.Combine(path, "blockprops.txt"), action_copy);
bool blkCur = DoAction(BlockPropsLvlPath(map), Path.Combine(path, "blockprops.txt"), action_copy);
bool bots = DoAction(BotsFile.BotsPath(map), Path.Combine(path, "bots.json"), action_copy);
bool bots = DoAction(Paths.BotsPath(map), Path.Combine(path, "bots.json"), action_copy);

return lvl && props && defs && blkOld && blkCur && bots;
}
Expand Down Expand Up @@ -281,8 +281,8 @@ public static class LevelActions {
BlockPropsOldPath(dst), action);
DoAction(BlockPropsLvlPath(src),
BlockPropsLvlPath(dst), action);
DoAction(BotsFile.BotsPath(src),
BotsFile.BotsPath(dst), action);
DoAction(Paths.BotsPath(src),
Paths.BotsPath(dst), action);
}

static bool DoAction(string src, string dst, byte action) {
Expand Down
1 change: 0 additions & 1 deletion MCGalaxy/Server/Server.Fields.cs
Expand Up @@ -77,7 +77,6 @@ public sealed partial class Server {
public static string DefaultColor;
[Obsolete("Use Server.Config.Currency")]
public static string moneys;
public static string IP;
public static string RestartPath;

// Extra storage for custom commands
Expand Down
12 changes: 6 additions & 6 deletions MCGalaxy/Server/Tasks/UpgradeTasks.cs
Expand Up @@ -128,14 +128,14 @@ internal static class UpgradeTasks {
File.WriteAllLines(Paths.TempRanksFile, lines);
}

const string oldBotsFile = "extra/bots.json";
internal static void UpgradeBots(SchedulerTask task) {
if (!File.Exists(Paths.BotsFile)) return;
string json = File.ReadAllText(Paths.BotsFile);
File.WriteAllText(Paths.BotsFile + ".bak", json);
if (!File.Exists(oldBotsFile)) return;
File.Copy(oldBotsFile, oldBotsFile + ".bak", true);
Logger.Log(LogType.SystemActivity, "Making bots file per-level.. " +
"saved backup of global bots file to extra/bots.json.bak");

List<BotProperties> bots = BotsFile.ReadAll(json);
List<BotProperties> bots = BotsFile.ReadAll(oldBotsFile);
Dictionary<string, List<BotProperties>> botsByLevel = new Dictionary<string, List<BotProperties>>();

foreach (BotProperties bot in bots) {
Expand All @@ -150,7 +150,7 @@ internal static class UpgradeTasks {
}

foreach (var kvp in botsByLevel) {
string path = BotsFile.BotsPath(kvp.Key);
string path = Paths.BotsPath(kvp.Key);
using (StreamWriter w = new StreamWriter(path)) {
BotsFile.WriteAll(w, kvp.Value);
}
Expand All @@ -159,7 +159,7 @@ internal static class UpgradeTasks {
if (Server.mainLevel.Bots.Count == 0) {
BotsFile.Load(Server.mainLevel);
}
File.Delete(Paths.BotsFile);
File.Delete(oldBotsFile);
}


Expand Down
6 changes: 4 additions & 2 deletions MCGalaxy/util/IO/Paths.cs
Expand Up @@ -19,6 +19,7 @@

namespace MCGalaxy {

/// <summary> Provides a centralised list of files and paths used. </summary>
public static class Paths {

public const string CustomColorsFile = "text/customcolors.txt";
Expand All @@ -44,11 +45,12 @@ public static class Paths {
public const string ServerPropsFile = "properties/server.properties";
public const string RankPropsFile = "properties/ranks.properties";

public const string BotsFile = "extra/bots.json";

public const string ImportsDir = "extra/import/";
public const string WaypointsDir = "extra/Waypoints/";

/// <summary> Relative path of the file containing a map's bots. </summary>
public static string BotsPath(string map) { return "extra/bots/" + map + ".json"; }

/// <summary> Relative path of the file containing a map's block definitions. </summary>
public static string MapBlockDefs(string map) { return "blockdefs/lvl_" + map + ".json"; }

Expand Down

0 comments on commit 695c867

Please sign in to comment.