mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-25 14:17:29 -04:00
Cleanup /renamelvl.
This commit is contained in:
parent
6c5b69ea09
commit
cc191c762c
@ -18,10 +18,9 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using MCGalaxy.SQL;
|
using MCGalaxy.SQL;
|
||||||
namespace MCGalaxy.Commands
|
|
||||||
{
|
namespace MCGalaxy.Commands {
|
||||||
public sealed class CmdRenameLvl : Command
|
public sealed class CmdRenameLvl : Command {
|
||||||
{
|
|
||||||
public override string name { get { return "renamelvl"; } }
|
public override string name { get { return "renamelvl"; } }
|
||||||
public override string shortcut { get { return ""; } }
|
public override string shortcut { get { return ""; } }
|
||||||
public override string type { get { return CommandTypes.Moderation; } }
|
public override string type { get { return CommandTypes.Moderation; } }
|
||||||
@ -29,71 +28,47 @@ namespace MCGalaxy.Commands
|
|||||||
public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }
|
public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }
|
||||||
public CmdRenameLvl() { }
|
public CmdRenameLvl() { }
|
||||||
|
|
||||||
public override void Use(Player p, string message)
|
public override void Use(Player p, string message) {
|
||||||
{
|
|
||||||
if (message == "" || message.IndexOf(' ') == -1) { Help(p); return; }
|
if (message == "" || message.IndexOf(' ') == -1) { Help(p); return; }
|
||||||
Level foundLevel = LevelInfo.Find(message.Split(' ')[0]);
|
string[] args = message.Split(' ');
|
||||||
if (foundLevel == null)
|
Level lvl = LevelInfo.Find(args[0]);
|
||||||
{
|
if (lvl == null) { Player.SendMessage(p, "Level not found"); return; }
|
||||||
Player.SendMessage(p, "Level not found");
|
string newName = args[1];
|
||||||
return;
|
if (!Player.ValidName(newName)) {
|
||||||
|
Player.SendMessage("\"" + newName + "\" is not a valid level name."); return;
|
||||||
}
|
}
|
||||||
|
|
||||||
string newName = message.Split(' ')[1];
|
|
||||||
|
|
||||||
if (LevelInfo.ExistsOffline(newName)) { Player.SendMessage(p, "Level already exists."); return; }
|
|
||||||
if (foundLevel == Server.mainLevel) { Player.SendMessage(p, "Cannot rename the main level."); return; }
|
|
||||||
|
|
||||||
foundLevel.Unload();
|
|
||||||
|
|
||||||
|
|
||||||
File.Move(LevelInfo.LevelPath(foundLevel.name), LevelInfo.LevelPath(newName));
|
|
||||||
try
|
|
||||||
{
|
|
||||||
File.Move(LevelInfo.LevelPath(foundLevel.name) + ".backup", LevelInfo.LevelPath(newName) + ".backup");
|
|
||||||
} catch { }
|
|
||||||
|
|
||||||
try
|
if (LevelInfo.ExistsOffline(newName)) { Player.SendMessage(p, "Level already exists."); return; }
|
||||||
{
|
if (lvl == Server.mainLevel) { Player.SendMessage(p, "Cannot rename the main level."); return; }
|
||||||
File.Move("levels/level properties/" + foundLevel.name + ".properties", "levels/level properties/" + newName + ".properties");
|
lvl.Unload();
|
||||||
|
|
||||||
|
File.Move(LevelInfo.LevelPath(lvl.name), LevelInfo.LevelPath(newName));
|
||||||
|
try {
|
||||||
|
File.Move(LevelInfo.LevelPath(lvl.name) + ".backup", LevelInfo.LevelPath(newName) + ".backup");
|
||||||
|
} catch {
|
||||||
}
|
}
|
||||||
catch { }
|
|
||||||
try
|
|
||||||
{
|
|
||||||
File.Move("levels/level properties/" + foundLevel.name, "levels/level properties/" + newName + ".properties");
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (File.Exists("blockdefs/lvl_" + foundLevel.name + ".json"))
|
File.Move("levels/level properties/" + lvl.name + ".properties", "levels/level properties/" + newName + ".properties");
|
||||||
File.Move("blockdefs/lvl_" + foundLevel.name + ".json", "blockdefs/lvl_" + newName + ".json");
|
} catch {
|
||||||
} catch {}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
File.Move("levels/level properties/" + lvl.name, "levels/level properties/" + newName + ".properties");
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (File.Exists("blockdefs/lvl_" + lvl.name + ".json"))
|
||||||
|
File.Move("blockdefs/lvl_" + lvl.name + ".json", "blockdefs/lvl_" + newName + ".json");
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
|
||||||
//Move and rename backups
|
//Move and rename backups
|
||||||
try
|
try {
|
||||||
{
|
MoveBackups(lvl.name, newName);
|
||||||
string foundLevelDir, newNameDir;
|
} catch {
|
||||||
for (int i = 1; ; i++)
|
|
||||||
{
|
|
||||||
foundLevelDir = LevelInfo.BackupPath(foundLevel.name, i.ToString());
|
|
||||||
newNameDir = LevelInfo.BackupPath(newName, i.ToString());
|
|
||||||
|
|
||||||
if (File.Exists(foundLevelDir + foundLevel.name + ".lvl"))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(newNameDir);
|
|
||||||
File.Move(foundLevelDir + foundLevel.name + ".lvl", newNameDir + newName + ".lvl");
|
|
||||||
if (DirectoryEmpty(foundLevelDir))
|
|
||||||
Directory.Delete(foundLevelDir);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (DirectoryEmpty(Server.backupLocation + "/" + foundLevel.name + "/"))
|
|
||||||
Directory.Delete(Server.backupLocation + "/" + foundLevel.name + "/");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch { }
|
|
||||||
|
|
||||||
//safe against SQL injections because foundLevel is being checked and,
|
//safe against SQL injections because foundLevel is being checked and,
|
||||||
//newName is being split and partly checked on illegal characters reserved for Windows.
|
//newName is being split and partly checked on illegal characters reserved for Windows.
|
||||||
@ -101,37 +76,48 @@ namespace MCGalaxy.Commands
|
|||||||
Database.executeQuery(String.Format("RENAME TABLE `Block{0}` TO `Block{1}`, " +
|
Database.executeQuery(String.Format("RENAME TABLE `Block{0}` TO `Block{1}`, " +
|
||||||
"`Portals{0}` TO `Portals{1}`, " +
|
"`Portals{0}` TO `Portals{1}`, " +
|
||||||
"`Messages{0}` TO `Messages{1}`, " +
|
"`Messages{0}` TO `Messages{1}`, " +
|
||||||
"`Zone{0}` TO `Zone{1}`", foundLevel.name.ToLower(), newName.ToLower()));
|
"`Zone{0}` TO `Zone{1}`", lvl.name.ToLower(), newName.ToLower()));
|
||||||
else {
|
else {
|
||||||
using (BulkTransaction helper = SQLiteBulkTransaction.Create()) { // ensures that it's either all work, or none work.
|
using (BulkTransaction helper = SQLiteBulkTransaction.Create()) { // ensures that it's either all work, or none work.
|
||||||
helper.Execute(String.Format("ALTER TABLE `Block{0}` RENAME TO `Block{1}`", foundLevel.name.ToLower(), newName.ToLower()));
|
helper.Execute(String.Format("ALTER TABLE `Block{0}` RENAME TO `Block{1}`", lvl.name.ToLower(), newName.ToLower()));
|
||||||
helper.Execute(String.Format("ALTER TABLE `Portals{0}` RENAME TO `Portals{1}`", foundLevel.name.ToLower(), newName.ToLower()));
|
helper.Execute(String.Format("ALTER TABLE `Portals{0}` RENAME TO `Portals{1}`", lvl.name.ToLower(), newName.ToLower()));
|
||||||
helper.Execute(String.Format("ALTER TABLE `Messages{0}` RENAME TO `Messages{1}`", foundLevel.name.ToLower(), newName.ToLower()));
|
helper.Execute(String.Format("ALTER TABLE `Messages{0}` RENAME TO `Messages{1}`", lvl.name.ToLower(), newName.ToLower()));
|
||||||
helper.Execute(String.Format("ALTER TABLE `Zone{0}` RENAME TO `Zone{1}`", foundLevel.name.ToLower(), newName.ToLower()));
|
helper.Execute(String.Format("ALTER TABLE `Zone{0}` RENAME TO `Zone{1}`", lvl.name.ToLower(), newName.ToLower()));
|
||||||
helper.Commit();
|
helper.Commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try { Command.all.Find("load").Use(p, newName); }
|
try { Command.all.Find("load").Use(p, newName); }
|
||||||
catch { }
|
catch { }
|
||||||
Player.GlobalMessage("Renamed " + foundLevel.name + " to " + newName);
|
Player.GlobalMessage("Renamed " + lvl.name + " to " + newName);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool DirectoryEmpty(string dir) {
|
||||||
|
if (!Directory.Exists(dir)) return true;
|
||||||
|
if (Directory.GetDirectories(dir).Length > 0) return false;
|
||||||
|
if (Directory.GetFiles(dir).Length > 0) return false;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Help(Player p)
|
static void MoveBackups(string oldName, string newName) {
|
||||||
{
|
for (int i = 1; ; i++) {
|
||||||
|
string oldDir = LevelInfo.BackupPath(oldName, i.ToString());
|
||||||
|
string newDir = LevelInfo.BackupPath(newName, i.ToString());
|
||||||
|
|
||||||
|
if (File.Exists(oldDir + oldName + ".lvl")) {
|
||||||
|
Directory.CreateDirectory(newDir);
|
||||||
|
File.Move(oldDir + oldName + ".lvl", newDir + newName + ".lvl");
|
||||||
|
if (DirectoryEmpty(oldDir)) Directory.Delete(oldDir);
|
||||||
|
} else {
|
||||||
|
if (DirectoryEmpty(Server.backupLocation + "/" + oldName + "/"))
|
||||||
|
Directory.Delete(Server.backupLocation + "/" + oldName + "/");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Help(Player p) {
|
||||||
Player.SendMessage(p, "/renamelvl <level> <new name> - Renames <level> to <new name>");
|
Player.SendMessage(p, "/renamelvl <level> <new name> - Renames <level> to <new name>");
|
||||||
Player.SendMessage(p, "Portals going to <level> will be lost");
|
Player.SendMessage(p, "Portals going to <level> will be lost");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool DirectoryEmpty(string dir)
|
|
||||||
{
|
|
||||||
if (!Directory.Exists(dir))
|
|
||||||
return true;
|
|
||||||
if (Directory.GetDirectories(dir).Length > 0)
|
|
||||||
return false;
|
|
||||||
if (Directory.GetFiles(dir).Length > 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user