Cleanup /restore.

This commit is contained in:
UnknownShadow200 2016-05-15 14:59:31 +10:00
parent c692c89c8a
commit 280640f928

View File

@ -1,136 +1,115 @@
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy)
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/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
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
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.
*/
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.IO;
namespace MCGalaxy.Commands
{
public sealed class CmdRestore : Command
{
using System.Text;
namespace MCGalaxy.Commands {
public sealed class CmdRestore : Command {
public override string name { get { return "restore"; } }
public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.World; } }
public override string type { get { return CommandTypes.World; } }
public override bool museumUsable { get { return false; } }
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
public CmdRestore() { }
public override void Use(Player p, string message)
{
if (message != "")
{
Level lvl;
string[] text = new string[2];
text[0] = "";
text[1] = "";
try
{
text[0] = message.Split(' ')[0].ToLower();
text[1] = message.Split(' ')[1].ToLower();
}
catch
{
text[1] = p.level.name;
}
if (message.Split(' ').Length >= 2)
{
lvl = LevelInfo.FindOrShowMatches(p, text[1]);
if (lvl == null) return;
}
else
{
if (p != null && p.level != null) lvl = p.level;
else
{
Server.s.Log("u dun derped, specify the level, silly head");
return;
}
}
public override void Use(Player p, string message) {
if (message == "") { OutputBackups(p); return; }
if (File.Exists(LevelInfo.BackupPath(lvl.name, text[0])))
{
try
{
File.Copy(LevelInfo.BackupPath(lvl.name, text[0]), LevelInfo.LevelPath(lvl.name), true);
Level temp = Level.Load(lvl.name);
temp.StartPhysics();
if (temp != null) {
lvl.spawnx = temp.spawnx;
lvl.spawny = temp.spawny;
lvl.spawnz = temp.spawnz;
lvl.Width = temp.Width; lvl.width = temp.Width;
lvl.Height = temp.Height; lvl.depth = temp.Height;
lvl.Length = temp.Length; lvl.length = temp.Length; lvl.height = temp.Length;
lvl.blocks = temp.blocks;
lvl.CustomBlocks = temp.CustomBlocks;
lvl.setPhysics(0);
lvl.ClearPhysics();
Command.all.Find("reveal").Use(null, "all " + text[1]);
} else {
Server.s.Log("Restore nulled");
File.Copy(LevelInfo.LevelPath(lvl.name) + ".backup", LevelInfo.LevelPath(lvl.name), true);
}
}
catch { Server.s.Log("Restore fail"); }
Level lvl;
string[] args = message.Split(' ');
if (args.Length >= 2) {
lvl = LevelInfo.FindOrShowMatches(p, args[1]);
if (lvl == null) return;
} else {
if (p != null && p.level != null) {
lvl = p.level;
} else {
Player.Message(p, "You must provide a level name when using /restore from console.");
return;
}
else { Player.Message(p, "Backup " + text[0] + " does not exist."); }
}
else
{
if (Directory.Exists(Server.backupLocation + "/" + p.level.name))
{
string[] directories = Directory.GetDirectories(Server.backupLocation + "/" + p.level.name);
int backupNumber = directories.Length;
Player.Message(p, p.level.name + " has " + backupNumber + " backups .");
bool foundOne = false; string foundRestores = "";
foreach (string s in directories)
{
string directoryName = s.Substring(s.LastIndexOf(Path.DirectorySeparatorChar) + 1);
try
{
int.Parse(directoryName);
}
catch
{
foundOne = true;
foundRestores += ", " + directoryName;
}
}
if (foundOne)
{
Player.Message(p, "Custom-named restores:");
Player.Message(p, "> " + foundRestores.Remove(0, 2));
}
}
else
{
Player.Message(p, p.level.name + " has no backups yet.");
if (File.Exists(LevelInfo.BackupPath(lvl.name, args[0]))) {
try {
DoRestore(lvl, args[0]);
} catch {
Server.s.Log("Restore fail");
}
} else {
Player.Message(p, "Backup " + args[0] + " does not exist.");
}
}
public override void Help(Player p)
{
Player.Message(p, "/restore <number> - restores a previous backup of the current map");
Player.Message(p, "/restore <number> <name> - restores a previous backup of the selected map");
static void DoRestore(Level lvl, string backup) {
File.Copy(LevelInfo.BackupPath(lvl.name, backup), LevelInfo.LevelPath(lvl.name), true);
Level temp = Level.Load(lvl.name);
temp.StartPhysics();
if (temp != null) {
lvl.spawnx = temp.spawnx;
lvl.spawny = temp.spawny;
lvl.spawnz = temp.spawnz;
lvl.Width = temp.Width; lvl.width = temp.Width;
lvl.Height = temp.Height; lvl.depth = temp.Height;
lvl.Length = temp.Length; lvl.length = temp.Length; lvl.height = temp.Length;
lvl.blocks = temp.blocks;
lvl.CustomBlocks = temp.CustomBlocks;
lvl.setPhysics(0);
lvl.ClearPhysics();
Command.all.Find("reveal").Use(null, "all " + lvl.name);
} else {
Server.s.Log("Restore nulled");
File.Copy(LevelInfo.LevelPath(lvl.name) + ".backup", LevelInfo.LevelPath(lvl.name), true);
}
}
static void OutputBackups(Player p) {
if (!Directory.Exists(Server.backupLocation + "/" + p.level.name)) {
Player.Message(p, p.level.name + " has no backups yet."); return;
}
string[] dirs = Directory.GetDirectories(Server.backupLocation + "/" + p.level.name);
Player.Message(p, p.level.name + " has &b" + dirs.Length + " %Sbackups.");
int count = 0;
StringBuilder custom = new StringBuilder();
foreach (string s in dirs) {
string name = s.Substring(s.LastIndexOf(Path.DirectorySeparatorChar) + 1);
int num;
if (int.TryParse(name, out num)) continue;
count++;
custom.Append(", " + name);
}
if (count == 0) return;
Player.Message(p, "&b" + count + " %Sof these are custom-named restores:");
Player.Message(p, custom.ToString(2, custom.Length - 2));
}
public override void Help(Player p) {
Player.Message(p, "%T/restore %H- lists all backups for the current map");
Player.Message(p, "%T/restore [number] [name]");
Player.Message(p, "%HRestores a previous backup for the given map.");
Player.Message(p, "%H If [name] is not given, your current map is used.");
}
}
}