public override void Use(Player p, string message) { if (message == "" || message.IndexOf(' ') == -1) { Help(p); return; } Level foundLevel = Level.Find(message.Split(' ')[0]); if (foundLevel == null) { Player.SendMessage(p, "Level not found"); return; } string newName = message.Split(' ')[1]; if (File.Exists("levels/" + newName + ".lvl")) { Player.SendMessage(p, "Level already exists."); return; } if (foundLevel == Server.mainLevel) { Player.SendMessage(p, "Cannot rename the main level."); return; } foundLevel.Unload(); try { File.Move("levels/" + foundLevel.name + ".lvl", "levels/" + newName + ".lvl"); File.Move("levels/" + foundLevel.name + ".lvl.backup", "levels/" + newName + ".lvl.backup"); try { File.Move("levels/level properties/" + foundLevel.name + ".properties", "levels/level properties/" + newName + ".properties"); } catch { } try { File.Move("levels/level properties/" + foundLevel.name, "levels/level properties/" + newName + ".properties"); } catch { } //Move and rename backups try { string foundLevelDir, newNameDir; for (int i = 1; ; i++) { foundLevelDir = @Server.backupLocation + "/" + foundLevel.name + "/" + i + "/"; newNameDir = @Server.backupLocation + "/" + newName + "/" + i + "/"; 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, //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}`", foundLevel.name.ToLower(), newName.ToLower())); } else { using (DatabaseTransactionHelper helper = SQLiteTransactionHelper.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 Portals{0} RENAME TO Portals{1}", foundLevel.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 Zone{0} RENAME TO Zone{1}", foundLevel.name.ToLower(), newName.ToLower())); helper.Commit(); } } try { Command.all.Find("load").Use(p, newName); } catch { } Player.GlobalMessage("Renamed " + foundLevel.name + " to " + newName); } catch (Exception e) { Player.SendMessage(p, "Error when renaming."); Server.ErrorLog(e); } }
internal static void fillDatabase(Stream stream) { //Backup using (FileStream backup = File.Create("backup.sql")) { CopyDatabase(new StreamWriter(backup)); } //Delete old List <string> tables = getTables(); foreach (string name in tables) { executeQuery(String.Format("DROP TABLE `{0}`", name)); } //Make new string script = new StreamReader(stream).ReadToEnd(); string[] cmds = script.Split(';'); // StringBuilder sb = new StringBuilder(); using (DatabaseTransactionHelper helper = DatabaseTransactionHelper.Create()) { foreach (string cmd in cmds) { String newCmd = cmd.Trim(" \r\n\t".ToCharArray()); int index = newCmd.ToUpper().IndexOf("CREATE TABLE"); if (index > -1) { newCmd = newCmd.Remove(0, index); //Do we have a primary key? try { if (Server.useMySQL) { int priIndex = newCmd.ToUpper().IndexOf(" PRIMARY KEY AUTOINCREMENT"); int priCount = " PRIMARY KEY AUTOINCREMENT".Length; int attIdx = newCmd.Substring(0, newCmd.Substring(0, priIndex - 1).LastIndexOfAny("` \n".ToCharArray())).LastIndexOfAny("` \n".ToCharArray()) + 1; int attIdxEnd = newCmd.IndexOfAny("` \n".ToCharArray(), attIdx) - 1; string attName = newCmd.Substring(attIdx, attIdxEnd - attIdx + 1).Trim(' ', '\n', '`', '\r'); //For speed, we just delete this, and add it to the attribute name, then delete the auto_increment clause. newCmd = newCmd.Remove(priIndex, priCount); newCmd = newCmd.Insert(newCmd.LastIndexOf(")"), ", PRIMARY KEY (`" + attName + "`)"); newCmd = newCmd.Insert(newCmd.IndexOf(',', priIndex), " AUTO_INCREMENT"); } else { int priIndex = newCmd.ToUpper().IndexOf(",\r\nPRIMARY KEY"); int priIndexEnd = newCmd.ToUpper().IndexOf(')', priIndex); int attIdx = newCmd.IndexOf("(", priIndex) + 1; int attIdxEnd = priIndexEnd - 1; string attName = newCmd.Substring(attIdx, attIdxEnd - attIdx + 1); newCmd = newCmd.Remove(priIndex, priIndexEnd - priIndex + 1); int start = newCmd.IndexOf(attName) + attName.Length; int end = newCmd.IndexOf(','); newCmd = newCmd.Remove(start, end - start); newCmd = newCmd.Insert(newCmd.IndexOf(attName) + attName.Length, " INTEGER PRIMARY KEY AUTOINCREMENT"); newCmd = newCmd.Replace(" auto_increment", "").Replace(" AUTO_INCREMENT", "").Replace("True", "1").Replace("False", "0"); } } catch (ArgumentOutOfRangeException) { } // If we don't, just ignore it. } //Run the command in the transaction. helper.Execute(newCmd.Replace(" unsigned", "").Replace(" UNSIGNED", "") + ";"); // sb.Append(newCmd).Append(";\n"); } helper.Commit(); } //Not sure if order matters. //AUTO_INCREMENT is changed to AUTOINCREMENT for MySQL -> SQLite //AUTOINCREMENT is changed to AUTO_INCREMENT for SQLite -> MySQL // All we should have in the script file is CREATE TABLE and INSERT INTO commands. //executeQuery(sb.ToString().Replace(" unsigned", "").Replace(" UNSIGNED", "")); }