Core: Rewrite /lockdown to use a single text file list instead of an empty file for each level. Also start modularising Server.cs

This commit is contained in:
UnknownShadow200 2016-09-14 12:55:37 +10:00
parent 2bbba49106
commit ced7daffc6
7 changed files with 173 additions and 122 deletions

View File

@ -38,19 +38,20 @@ namespace MCGalaxy.Commands {
} }
if (args[0].CaselessEq("map")) { if (args[0].CaselessEq("map")) {
args[1] = args[1].ToLower();
if (!Formatter.ValidName(p, args[1], "level")) return; if (!Formatter.ValidName(p, args[1], "level")) return;
string path = "text/lockdown/map/" + args[1]; bool unlocking = Server.lockdown.Contains(args[1]);
if (!File.Exists(path)) { Chat.MessageAll("The map {0} has been {0}locked", args[1], unlocking ? "un" : "");
File.Create(path).Dispose(); string srcName = (p == null) ? "(console)" : p.ColoredName;
Chat.MessageAll("The map {0} has been locked", args[1]);
Chat.MessageOps("Locked by: " + ((p == null) ? "Console" : p.name)); if (unlocking) {
Server.lockdown.Remove(args[1]);
Chat.MessageOps("Unlocked by: " + srcName);
} else { } else {
File.Delete(path); Server.lockdown.AddOrReplace(args[1]);
Chat.MessageAll("The map {0} has been unlocked", args[1]); Chat.MessageOps("Locked by: " + srcName);
Chat.MessageOps("Unlocked by: " + ((p == null) ? "Console" : p.name));
} }
Server.lockdown.Save();
} else { } else {
Player who = PlayerInfo.FindMatches(p, args[1]); Player who = PlayerInfo.FindMatches(p, args[1]);
if (who == null) return; if (who == null) return;
@ -67,7 +68,7 @@ namespace MCGalaxy.Commands {
Chat.MessageAll("{0} %Shas been locked down!", who.ColoredName); Chat.MessageAll("{0} %Shas been locked down!", who.ColoredName);
Chat.MessageOps("Locked by: " + ((p == null) ? "Console" : p.name)); Chat.MessageOps("Locked by: " + ((p == null) ? "Console" : p.name));
} else { } else {
Chat.MessageAll("{0} %Shas been unlocked.", who.ColoredName); Chat.MessageAll("{0} %Shas been unlocked.", who.ColoredName);
Chat.MessageOps("Unlocked by: " + ((p == null) ? "Console" : p.name)); Chat.MessageOps("Unlocked by: " + ((p == null) ? "Console" : p.name));
} }
who.jailed = !who.jailed; who.jailed = !who.jailed;
@ -75,9 +76,9 @@ namespace MCGalaxy.Commands {
} }
public override void Help(Player p) { public override void Help(Player p) {
Player.Message(p, "%T/lockdown [map/player] [name]"); Player.Message(p, "%T/lockdown map/player [name]");
Player.Message(p, "%H'map' - prevents new players from joining that map."); Player.Message(p, "%H\"map\" - prevents new players from joining that map.");
Player.Message(p, "%H'player' - prevents that player from using commands."); Player.Message(p, "%H\"player\" - prevents that player from using commands.");
Player.Message(p, "%HUsing /lockdown again will unlock that map/player."); Player.Message(p, "%HUsing /lockdown again will unlock that map/player.");
} }
} }

View File

@ -147,7 +147,7 @@ namespace MCGalaxy {
public bool CanJoin(Player p, bool ignorePerms = false) { public bool CanJoin(Player p, bool ignorePerms = false) {
if (p == null) return true; if (p == null) return true;
if (!VisitAccess.CheckDetailed(p, ignorePerms)) return false; if (!VisitAccess.CheckDetailed(p, ignorePerms)) return false;
if (File.Exists("text/lockdown/map/" + name)) { if (Server.lockdown.Contains(name)) {
Player.Message(p, "The level " + name + " is locked."); return false; Player.Message(p, "The level " + name + " is locked."); return false;
} }
return true; return true;

View File

@ -608,6 +608,7 @@
<Compile Include="Server\Server.Fields.cs" /> <Compile Include="Server\Server.Fields.cs" />
<Compile Include="Server\Server.Init.cs" /> <Compile Include="Server\Server.Init.cs" />
<Compile Include="Server\Server.Tasks.cs" /> <Compile Include="Server\Server.Tasks.cs" />
<Compile Include="Server\Tasks\UpgradeTasks.cs" />
<Compile Include="Server\ThreadSafeCache.cs" /> <Compile Include="Server\ThreadSafeCache.cs" />
<Compile Include="Server\Updater.cs" /> <Compile Include="Server\Updater.cs" />
<Compile Include="API\WebServer.cs" /> <Compile Include="API\WebServer.cs" />
@ -731,6 +732,7 @@
<Folder Include="Events" /> <Folder Include="Events" />
<Folder Include="Chat" /> <Folder Include="Chat" />
<Folder Include="CorePlugin" /> <Folder Include="CorePlugin" />
<Folder Include="Server\Tasks" />
<Folder Include="util\Math" /> <Folder Include="util\Math" />
<Folder Include="Player\Group" /> <Folder Include="Player\Group" />
<Folder Include="Player\List" /> <Folder Include="Player\List" />

View File

@ -79,7 +79,7 @@ namespace MCGalaxy {
public static bool ServerSetupFinished = false; public static bool ServerSetupFinished = false;
public static Auto_CTF ctf = null; public static Auto_CTF ctf = null;
public static PlayerList bannedIP, whiteList, ircControllers, muted; public static PlayerList bannedIP, whiteList, ircControllers, muted;
public static PlayerList ignored, frozen, hidden, agreed, vip, noEmotes; public static PlayerList ignored, frozen, hidden, agreed, vip, noEmotes, lockdown;
public static PlayerExtList jailed, models, skins, reach; public static PlayerExtList jailed, models, skins, reach;
public static readonly List<string> Devs = new List<string>(), Mods = new List<string>(); public static readonly List<string> Devs = new List<string>(), Mods = new List<string>();

View File

@ -23,6 +23,7 @@ using System.Threading;
using MCGalaxy.Commands.World; using MCGalaxy.Commands.World;
using MCGalaxy.Games; using MCGalaxy.Games;
using MCGalaxy.Generator; using MCGalaxy.Generator;
using MCGalaxy.Tasks;
namespace MCGalaxy { namespace MCGalaxy {
@ -70,7 +71,7 @@ namespace MCGalaxy {
void LoadPlayerLists() { void LoadPlayerLists() {
agreed = new PlayerList("ranks/agreed.txt"); agreed = new PlayerList("ranks/agreed.txt");
try { try {
UpgradeOldAgreed(); UpgradeTasks.UpgradeOldAgreed();
agreed = PlayerList.Load("agreed.txt"); agreed = PlayerList.Load("agreed.txt");
} catch (Exception ex) { } catch (Exception ex) {
Server.ErrorLog(ex); Server.ErrorLog(ex);
@ -83,6 +84,7 @@ namespace MCGalaxy {
hidden = PlayerList.Load("hidden.txt"); hidden = PlayerList.Load("hidden.txt");
vip = PlayerList.Load("text/vip.txt"); vip = PlayerList.Load("text/vip.txt");
noEmotes = PlayerList.Load("text/emotelist.txt"); noEmotes = PlayerList.Load("text/emotelist.txt");
lockdown = PlayerList.Load("text/lockdown.txt");
jailed = PlayerExtList.Load("ranks/jailed.txt"); jailed = PlayerExtList.Load("ranks/jailed.txt");
models = PlayerExtList.Load("extra/models.txt"); models = PlayerExtList.Load("extra/models.txt");
@ -95,46 +97,6 @@ namespace MCGalaxy {
whiteList = PlayerList.Load("whitelist.txt"); whiteList = PlayerList.Load("whitelist.txt");
} }
static void UpgradeOldBlacklist() {
if (!Directory.Exists("levels/blacklists")) return;
string[] files = Directory.GetFiles("levels/blacklists");
for (int i = 0; i < files.Length; i++) {
string[] blacklist = File.ReadAllLines(files[i]);
List<string> names = new List<string>();
// Lines are in the format: day.month.year name+
foreach (string entry in blacklist) {
string[] parts = entry.Split(' ');
string name = parts[parts.Length - 1];
name = name.Substring(0, name.Length - 1);
names.Add(name);
}
if (names.Count > 0) {
string lvlName = Path.GetFileNameWithoutExtension(files[i]);
string propsPath = LevelInfo.PropertiesPath(lvlName);
using (StreamWriter w = new StreamWriter(propsPath, true)) {
w.WriteLine("VisitBlacklist = " + names.Join());
}
}
File.Delete(files[i]);
}
Directory.Delete("levels/blacklists");
}
static void UpgradeOldAgreed() {
// agreed.txt format used to be names separated by spaces, we need to fix that up.
if (!File.Exists("ranks/agreed.txt")) return;
string data = null;
using (FileStream fs = File.OpenRead("ranks/agreed.txt")) {
if (fs.ReadByte() != ' ') return;
data = new StreamReader(fs).ReadToEnd();
data = data.Replace(" ", Environment.NewLine);
}
File.WriteAllText("ranks/agreed.txt", data);
}
void LoadAutoloadCommands() { void LoadAutoloadCommands() {
if (File.Exists("text/autoload.txt")) { if (File.Exists("text/autoload.txt")) {
try { try {
@ -165,69 +127,6 @@ namespace MCGalaxy {
} }
} }
} }
void MovePreviousLevelFiles() {
if (!Directory.Exists("levels")) return;
try {
string[] files = Directory.GetFiles("levels", "*.prev");
if (files.Length == 0) return;
if (!Directory.Exists("levels/prev"))
Directory.CreateDirectory("levels/prev");
foreach (string file in files) {
string name = Path.GetFileName(file);
string newFile = "levels/prev/" + name;
try {
File.Move(file, newFile);
} catch (Exception ex) {
Server.s.Log("Error while trying to move .lvl.prev file");
Server.ErrorLog(ex);
}
}
} catch (Exception ex) {
Server.ErrorLog(ex);
}
}
void CombineEnvFiles() {
if (!Directory.Exists("levels/level properties")) return;
try {
string[] files = Directory.GetFiles("levels/level properties", "*.env");
if (files.Length == 0) return;
Server.s.Log("Combining " + files.Length + " .env and .properties files..");
foreach (string envFile in files) {
try {
Combine(envFile);
} catch (Exception ex) {
Server.s.Log("Error while trying to combine .env and .properties file");
Server.ErrorLog(ex);
}
}
Server.s.Log("Finished combining .env and .properties files.");
} catch (Exception ex) {
Server.ErrorLog(ex);
}
}
static void Combine(string envFile) {
string name = Path.GetFileNameWithoutExtension(envFile);
string propFile = LevelInfo.FindPropertiesFile(name);
List<string> lines = new List<string>();
if (propFile != null)
lines = CP437Reader.ReadAllLines(propFile);
using (StreamReader r = new StreamReader(envFile)) {
string line = null;
while ((line = r.ReadLine()) != null)
lines.Add(line);
}
propFile = LevelInfo.PropertiesPath(name);
CP437Writer.WriteAllLines(propFile, lines.ToArray());
File.Delete(envFile);
}
void SetupSocket() { void SetupSocket() {
Log("Creating listening socket on port " + port + "... "); Log("Creating listening socket on port " + port + "... ");

View File

@ -20,6 +20,7 @@ using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using MCGalaxy.Games; using MCGalaxy.Games;
using MCGalaxy.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace MCGalaxy { namespace MCGalaxy {
@ -99,13 +100,14 @@ namespace MCGalaxy {
foreach (Level l in loaded) foreach (Level l in loaded)
l.Unload(); l.Unload();
Background.QueueOnce(CombineEnvFiles); Background.QueueOnce(UpgradeTasks.CombineEnvFiles);
Background.QueueOnce(LoadMainLevel); Background.QueueOnce(LoadMainLevel);
Plugin.Load(); Plugin.Load();
Background.QueueOnce(UpgradeOldBlacklist); Background.QueueOnce(UpgradeTasks.UpgradeOldBlacklist);
Background.QueueOnce(LoadPlayerLists); Background.QueueOnce(LoadPlayerLists);
Background.QueueOnce(LoadAutoloadCommands); Background.QueueOnce(LoadAutoloadCommands);
Background.QueueOnce(MovePreviousLevelFiles); Background.QueueOnce(UpgradeTasks.MovePreviousLevelFiles);
Background.QueueOnce(UpgradeTasks.UpgradeOldLockdown);
Background.QueueOnce(SetupSocket); Background.QueueOnce(SetupSocket);
Background.QueueOnce(InitTimers); Background.QueueOnce(InitTimers);

View File

@ -0,0 +1,147 @@
/*
Copyright 2015 MCGalaxy
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading;
using MCGalaxy.Commands.World;
using MCGalaxy.Games;
using MCGalaxy.Generator;
namespace MCGalaxy.Tasks {
internal static class UpgradeTasks {
internal static void UpgradeOldBlacklist() {
if (!Directory.Exists("levels/blacklists")) return;
string[] files = Directory.GetFiles("levels/blacklists");
for (int i = 0; i < files.Length; i++) {
string[] blacklist = File.ReadAllLines(files[i]);
List<string> names = new List<string>();
// Lines are in the format: day.month.year name+
foreach (string entry in blacklist) {
string[] parts = entry.Split(' ');
string name = parts[parts.Length - 1];
name = name.Substring(0, name.Length - 1);
names.Add(name);
}
if (names.Count > 0) {
string lvlName = Path.GetFileNameWithoutExtension(files[i]);
string propsPath = LevelInfo.PropertiesPath(lvlName);
using (StreamWriter w = new StreamWriter(propsPath, true)) {
w.WriteLine("VisitBlacklist = " + names.Join());
}
}
File.Delete(files[i]);
}
Directory.Delete("levels/blacklists");
}
internal static void UpgradeOldAgreed() {
// agreed.txt format used to be names separated by spaces, we need to fix that up.
if (!File.Exists("ranks/agreed.txt")) return;
string data = null;
using (FileStream fs = File.OpenRead("ranks/agreed.txt")) {
if (fs.ReadByte() != ' ') return;
data = new StreamReader(fs).ReadToEnd();
data = data.Replace(" ", Environment.NewLine);
}
File.WriteAllText("ranks/agreed.txt", data);
}
internal static void MovePreviousLevelFiles() {
if (!Directory.Exists("levels")) return;
try {
string[] files = Directory.GetFiles("levels", "*.prev");
if (files.Length == 0) return;
if (!Directory.Exists("levels/prev"))
Directory.CreateDirectory("levels/prev");
foreach (string file in files) {
string name = Path.GetFileName(file);
string newFile = "levels/prev/" + name;
try {
File.Move(file, newFile);
} catch (Exception ex) {
Server.s.Log("Error while trying to move .lvl.prev file");
Server.ErrorLog(ex);
}
}
} catch (Exception ex) {
Server.ErrorLog(ex);
}
}
internal static void CombineEnvFiles() {
if (!Directory.Exists("levels/level properties")) return;
try {
string[] files = Directory.GetFiles("levels/level properties", "*.env");
if (files.Length == 0) return;
Server.s.Log("Combining " + files.Length + " .env and .properties files..");
foreach (string envFile in files) {
try {
Combine(envFile);
} catch (Exception ex) {
Server.s.Log("Error while trying to combine .env and .properties file");
Server.ErrorLog(ex);
}
}
Server.s.Log("Finished combining .env and .properties files.");
} catch (Exception ex) {
Server.ErrorLog(ex);
}
}
static void Combine(string envFile) {
string name = Path.GetFileNameWithoutExtension(envFile);
string propFile = LevelInfo.FindPropertiesFile(name);
List<string> lines = new List<string>();
if (propFile != null)
lines = CP437Reader.ReadAllLines(propFile);
using (StreamReader r = new StreamReader(envFile)) {
string line = null;
while ((line = r.ReadLine()) != null)
lines.Add(line);
}
propFile = LevelInfo.PropertiesPath(name);
CP437Writer.WriteAllLines(propFile, lines.ToArray());
File.Delete(envFile);
}
internal static void UpgradeOldLockdown() {
if (!Directory.Exists("text/lockdown/map")) return;
string[] files = Directory.GetFiles("text/lockdown/map");
for (int i = 0; i < files.Length; i++) {
File.Delete(files[i]);
string level = Path.GetFileName(files[i]);
Server.lockdown.AddOrReplace(level);
}
Server.lockdown.Save();
Directory.Delete("text/lockdown/map");
}
}
}