Move zone database code to separate file.

This commit is contained in:
UnknownShadow200 2016-05-12 08:49:59 +10:00
parent af653d3af2
commit f62c16c7c8
6 changed files with 179 additions and 143 deletions

View File

@ -86,11 +86,7 @@ namespace MCGalaxy.Commands
for (int i = 0; i < p.level.ZoneList.Count; i++)
{
Level.Zone Zn = p.level.ZoneList[i];
ParameterisedQuery query = ParameterisedQuery.Create();
query.AddParam("@Owner", Zn.Owner);
Database.executeQuery(query, "DELETE FROM `Zone" + p.level.name + "` WHERE Owner=@Owner AND SmallX='" + Zn.smallX + "' AND SMALLY='" + Zn.smallY +
"' AND SMALLZ='" + Zn.smallZ + "' AND BIGX='" + Zn.bigX + "' AND BIGY='" + Zn.bigY + "' AND BIGZ='" + Zn.bigZ + "'");
Zones.Delete(p.level.name, Zn);
Player.Message(p, "Zone deleted for &b" + Zn.Owner);
p.level.ZoneList.Remove(p.level.ZoneList[i]);
if (i == p.level.ZoneList.Count) { Player.Message(p, "Finished removing all zones"); return; }
@ -157,11 +153,7 @@ namespace MCGalaxy.Commands
Zn.Owner = cpos.Owner;
p.level.ZoneList.Add(Zn);
ParameterisedQuery query = ParameterisedQuery.Create();
query.AddParam("@Owner", Zn.Owner);
Database.executeQuery(query, "INSERT INTO `Zone" + p.level.name + "` (SmallX, SmallY, SmallZ, BigX, BigY, BigZ, Owner) VALUES ("
+ Zn.smallX + ", " + Zn.smallY + ", " + Zn.smallZ + ", " + Zn.bigX + ", " + Zn.bigY + ", " + Zn.bigZ + ", @Owner)");
Zones.Create(p.level.name, Zn);
Player.Message(p, "Added zone for &b" + cpos.Owner);
}

View File

@ -1,123 +1,123 @@
/*
Copyright 2011 MCForge
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.IO;
using MCGalaxy.SQL;
namespace MCGalaxy.Commands {
public sealed class CmdRenameLvl : Command {
public override string name { get { return "renamelvl"; } }
public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.Moderation; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }
public CmdRenameLvl() { }
public override void Use(Player p, string message) {
if (message == "" || message.IndexOf(' ') == -1) { Help(p); return; }
string[] args = message.Split(' ');
Level lvl = LevelInfo.FindOrShowMatches(p, args[0]);
if (lvl == null) return;
string newName = args[1];
if (!Player.ValidName(newName)) {
Player.Message(p, "\"" + newName + "\" is not a valid level name."); return;
}
if (LevelInfo.ExistsOffline(newName)) { Player.Message(p, "Level already exists."); return; }
if (lvl == Server.mainLevel) { Player.Message(p, "Cannot rename the main level."); return; }
lvl.Unload();
File.Move(LevelInfo.LevelPath(lvl.name), LevelInfo.LevelPath(newName));
try {
File.Move(LevelInfo.LevelPath(lvl.name) + ".backup", LevelInfo.LevelPath(newName) + ".backup");
} catch {
}
try {
File.Move("levels/level properties/" + lvl.name + ".properties", "levels/level properties/" + newName + ".properties");
} 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
try {
MoveBackups(lvl.name, newName);
} catch {
}
//safe against SQL injections because foundLevel is being checked and,
//newName is being split and partly checked on illegal characters reserved for Windows.
if (Server.useMySQL)
Database.executeQuery(String.Format("RENAME TABLE `Block{0}` TO `Block{1}`, " +
"`Portals{0}` TO `Portals{1}`, " +
"`Messages{0}` TO `Messages{1}`, " +
"`Zone{0}` TO `Zone{1}`", lvl.name.ToLower(), newName.ToLower()));
else {
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}`", lvl.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}`", lvl.name.ToLower(), newName.ToLower()));
helper.Execute(String.Format("ALTER TABLE `Zone{0}` RENAME TO `Zone{1}`", lvl.name.ToLower(), newName.ToLower()));
helper.Commit();
}
}
try { Command.all.Find("load").Use(p, newName); }
catch { }
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;
}
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.Message(p, "/renamelvl <level> <new name> - Renames <level> to <new name>");
Player.Message(p, "Portals going to <level> will be lost");
}
}
}
/*
Copyright 2011 MCForge
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.IO;
using MCGalaxy.SQL;
namespace MCGalaxy.Commands {
public sealed class CmdRenameLvl : Command {
public override string name { get { return "renamelvl"; } }
public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.Moderation; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }
public CmdRenameLvl() { }
public override void Use(Player p, string message) {
if (message == "" || message.IndexOf(' ') == -1) { Help(p); return; }
string[] args = message.Split(' ');
Level lvl = LevelInfo.FindOrShowMatches(p, args[0]);
if (lvl == null) return;
string newName = args[1];
if (!Player.ValidName(newName)) {
Player.Message(p, "\"" + newName + "\" is not a valid level name."); return;
}
if (LevelInfo.ExistsOffline(newName)) { Player.Message(p, "Level already exists."); return; }
if (lvl == Server.mainLevel) { Player.Message(p, "Cannot rename the main level."); return; }
lvl.Unload();
File.Move(LevelInfo.LevelPath(lvl.name), LevelInfo.LevelPath(newName));
try {
File.Move(LevelInfo.LevelPath(lvl.name) + ".backup", LevelInfo.LevelPath(newName) + ".backup");
} catch {
}
try {
File.Move("levels/level properties/" + lvl.name + ".properties", "levels/level properties/" + newName + ".properties");
} 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
try {
MoveBackups(lvl.name, newName);
} catch {
}
//safe against SQL injections because foundLevel is being checked and,
//newName is being split and partly checked on illegal characters reserved for Windows.
if (Server.useMySQL)
Database.executeQuery(String.Format("RENAME TABLE `Block{0}` TO `Block{1}`, " +
"`Portals{0}` TO `Portals{1}`, " +
"`Messages{0}` TO `Messages{1}`, " +
"`Zone{0}` TO `Zone{1}`", lvl.name.ToLower(), newName.ToLower()));
else {
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}`", lvl.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}`", lvl.name.ToLower(), newName.ToLower()));
helper.Execute(String.Format("ALTER TABLE `Zone{0}` RENAME TO `Zone{1}`", lvl.name.ToLower(), newName.ToLower()));
helper.Commit();
}
}
try { Command.all.Find("load").Use(p, newName); }
catch { }
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;
}
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.Message(p, "/renamelvl <level> <new name> - Renames <level> to <new name>");
Player.Message(p, "Portals going to <level> will be lost");
}
}
}

View File

@ -104,13 +104,17 @@ namespace MCGalaxy.Eco {
Player.Message(p, "%aSuccessfully created your map: '%f" + name + "%a'");
try {
//safe against SQL injections, but will be replaced soon by a new feature
Database.executeQuery("INSERT INTO `Zone" + level.name + "` (SmallX, SmallY, SmallZ, BigX, BigY, BigZ, Owner) parts[1]S " +
"(0,0,0," + (level.Width - 1) + "," + (level.Height - 1) + "," + (level.Length - 1) + ",'" + p.name + "')");
Level.Zone zn = default(Level.Zone);
zn.bigX = (ushort)(level.Width - 1);
zn.bigY = (ushort)(level.Height - 1);
zn.bigZ = (ushort)(level.Length - 1);
zn.Owner = p.name;
level.ZoneList.Add(zn);
Zones.Create(level.name, zn);
Player.Message(p, "%aZoning Succesful");
} catch { Player.Message(p, "%cZoning Failed"); }
} catch {
Player.Message(p, "%cSomething went wrong, Money untouchred"); return;
Player.Message(p, "%cSomething went wrong, Money untouched"); return;
}
Economy.MakePurchase(p, preset.price, "%3Map: %f" + preset.name);
}

View File

@ -205,11 +205,7 @@ namespace MCGalaxy {
if (p.group.Permission < group.Permission)
continue;
}
Database.executeQuery("DELETE FROM `Zone" + p.level.name + "` WHERE Owner='" +
zn.Owner + "' AND SmallX='" + zn.smallX + "' AND SMALLY='" +
zn.smallY + "' AND SMALLZ='" + zn.smallZ + "' AND BIGX='" +
zn.bigX + "' AND BIGY='" + zn.bigY + "' AND BIGZ='" + zn.bigZ + "'");
Zones.Delete(p.level.name, zn);
if (toDel == null) toDel = new List<Zone>();
toDel.Add(zn);

43
Levels/Zones.cs Normal file
View File

@ -0,0 +1,43 @@
/*
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 MCGalaxy.SQL;
namespace MCGalaxy {
public static class Zones {
public static void Delete(string level, Level.Zone zn) {
ParameterisedQuery query = ParameterisedQuery.Create();
query.AddParam("@Owner", zn.Owner);
Database.executeQuery("DELETE FROM `Zone" + level + "` WHERE Owner=@Owner" +
" AND SmallX='" + zn.smallX + "' AND SMALLY='" +
zn.smallY + "' AND SMALLZ='" + zn.smallZ + "' AND BIGX='" +
zn.bigX + "' AND BIGY='" + zn.bigY + "' AND BIGZ='" + zn.bigZ + "'");
}
public static void Create(string level, Level.Zone zn) {
ParameterisedQuery query = ParameterisedQuery.Create();
query.AddParam("@Owner", zn.Owner);
Database.executeQuery(query, "INSERT INTO `Zone" + level +
"` (SmallX, SmallY, SmallZ, BigX, BigY, BigZ, Owner) VALUES ("
+ zn.smallX + ", " + zn.smallY + ", " + zn.smallZ + ", "
+ zn.bigX + ", " + zn.bigY + ", " + zn.bigZ + ", @Owner)");
}
}
}

View File

@ -303,7 +303,6 @@
<Compile Include="Commands\Moderation\CmdPromote.cs" />
<Compile Include="Commands\Moderation\CmdPUnload.cs" />
<Compile Include="Commands\Moderation\CmdRankInfo.cs" />
<Compile Include="Commands\Moderation\CmdRenameLvl.cs" />
<Compile Include="Commands\Moderation\CmdResetBot.cs" />
<Compile Include="Commands\Moderation\CmdRestart.cs" />
<Compile Include="Commands\Moderation\CmdRestoreSelection.cs" />
@ -400,6 +399,7 @@
<Compile Include="Commands\World\CmdPause.cs" />
<Compile Include="Commands\World\CmdPermissions.cs" />
<Compile Include="Commands\World\CmdPhysics.cs" />
<Compile Include="Commands\World\CmdRenameLvl.cs" />
<Compile Include="Commands\World\CmdRestore.cs" />
<Compile Include="Commands\World\CmdReveal.cs" />
<Compile Include="Commands\World\CmdSave.cs" />
@ -523,6 +523,7 @@
<Compile Include="Levels\Physics\TrainPhysics.cs" />
<Compile Include="Levels\Physics\TntPhysics.cs" />
<Compile Include="Levels\Physics\ZombiePhysics.cs" />
<Compile Include="Levels\Zones.cs" />
<Compile Include="Player\Chat.cs" />
<Compile Include="Player\Entities.cs" />
<Compile Include="Player\Group\Group.cs" />