From 5c612be82bfda1a9003dccc8a5aea85dd28ccb4e Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 27 Sep 2021 19:34:59 +1000 Subject: [PATCH] Move PlayerDB folder creating methods to only be called once on server startup and on nick/login/logout setting methods --- MCGalaxy/Database/PlayerDB.cs | 22 +++++--- MCGalaxy/Network/Utils/BufferedBlockSender.cs | 52 +++++++++++-------- MCGalaxy/Player/Player.Login.cs | 5 +- MCGalaxy/Server/Server.cs | 2 + 4 files changed, 50 insertions(+), 31 deletions(-) diff --git a/MCGalaxy/Database/PlayerDB.cs b/MCGalaxy/Database/PlayerDB.cs index 9719487d8..aa524c9df 100644 --- a/MCGalaxy/Database/PlayerDB.cs +++ b/MCGalaxy/Database/PlayerDB.cs @@ -47,27 +47,24 @@ namespace MCGalaxy.DB { } public static void SetNick(string name, string nick) { + EnsureDirectoriesExist(); using (StreamWriter sw = new StreamWriter("players/" + name + "DB.txt", false)) sw.WriteLine("Nick = " + nick); } public static string GetLoginMessage(Player p) { - if (!Directory.Exists("text/login")) - Directory.CreateDirectory("text/login"); - string path = LoginPath(p.name); if (File.Exists(path)) return File.ReadAllText(path); - // Unix is case sensitive (older files used correct casing of name) + + // Filesystem is case sensitive (older files used correct casing of name) path = "text/login/" + p.name + ".txt"; return File.Exists(path) ? File.ReadAllText(path) : "connected"; } public static string GetLogoutMessage(Player p) { if (p.name == null) return "disconnected"; - if (!Directory.Exists("text/logout")) - Directory.CreateDirectory("text/logout"); - + string path = LogoutPath(p.name); if (File.Exists(path)) return File.ReadAllText(path); @@ -76,6 +73,7 @@ namespace MCGalaxy.DB { } static void SetMessage(string path, string msg) { + EnsureDirectoriesExist(); if (msg.Length > 0) { File.WriteAllText(path, msg); } else if (File.Exists(path)) { @@ -165,5 +163,15 @@ namespace MCGalaxy.DB { "WHERE Name LIKE @0 ESCAPE '#' LIMIT 101" + suffix, "%" + name.Replace("_", "#_") + "%"); } + + + public static void EnsureDirectoriesExist() { + if (!Directory.Exists("text/login")) + Directory.CreateDirectory("text/login"); + if (!Directory.Exists("text/logout")) + Directory.CreateDirectory("text/logout"); + if (!Directory.Exists("players")) + Directory.CreateDirectory("players"); + } } } \ No newline at end of file diff --git a/MCGalaxy/Network/Utils/BufferedBlockSender.cs b/MCGalaxy/Network/Utils/BufferedBlockSender.cs index e816671ab..f9805f728 100644 --- a/MCGalaxy/Network/Utils/BufferedBlockSender.cs +++ b/MCGalaxy/Network/Utils/BufferedBlockSender.cs @@ -19,12 +19,13 @@ using System; using BlockID = System.UInt16; using BlockRaw = System.Byte; -namespace MCGalaxy.Network { +namespace MCGalaxy.Network +{ /// Helper class for efficiently sending many block changes. /// Sends block changes as either a single CPE BulkBlockUpdate packet, /// or 256 SetBlock packets combined as a single byte array to reduce overhead. - public sealed class BufferedBlockSender { - + public sealed class BufferedBlockSender + { int[] indices = new int[256]; BlockID[] blocks = new BlockID[256]; int count = 0; @@ -32,19 +33,19 @@ namespace MCGalaxy.Network { public Player player; public BufferedBlockSender() { } - /// Constructs a bulk sender that will send block changes to all players on that level. + /// Constructs a bulk sender that will send block changes to all players on that level public BufferedBlockSender(Level level) { this.level = level; } - /// Constructs a bulk sender that will only send block changes to that player. + /// Constructs a bulk sender that will only send block changes to that player public BufferedBlockSender(Player player) { this.player = player; this.level = player.level; } - /// Adds a block change to list of buffered changes. - /// When buffer limit is reached, calls Flush(), resetting buffered list. + /// Adds a block change to list of buffered changes + /// This method automatically calls Flush() when buffer limit is reached public void Add(int index, BlockID block) { indices[count] = index; if (Block.IsPhysicsType(block)) { @@ -57,7 +58,7 @@ namespace MCGalaxy.Network { if (count == 256) Flush(); } - /// Sends buffered block changes to target player(s). + /// Sends buffered block changes to target player(s) public void Flush() { if (count == 0) return; @@ -67,27 +68,29 @@ namespace MCGalaxy.Network { } void SendLevel() { - byte[] bulk = null, normal = null, noBlockDefs = null, classic = null, ext = null, extBulk = null; + byte[] bulk = null, normal = null, classic = null, ext = null, extBulk = null; Player[] players = PlayerInfo.Online.Items; - foreach (Player p in players) { + foreach (Player p in players) + { if (p.level != level) continue; + byte[] packet = MakePacket(p, ref bulk, ref normal, - ref noBlockDefs, ref classic, ref ext, ref extBulk); + ref classic, ref ext, ref extBulk); p.Socket.Send(packet, SendFlags.LowPriority); } } void SendPlayer() { - byte[] bulk = null, normal = null, noBlockDefs = null, classic = null, ext = null, extBulk = null; + byte[] bulk = null, normal = null,classic = null, ext = null, extBulk = null; byte[] packet = MakePacket(player, ref bulk, ref normal, - ref noBlockDefs, ref classic, ref ext, ref extBulk); + ref classic, ref ext, ref extBulk); player.Socket.Send(packet, SendFlags.LowPriority); } #region Packet construction byte[] MakePacket(Player p, ref byte[] bulk, ref byte[] normal, - ref byte[] noBlockDefs, ref byte[] classic, ref byte[] ext, ref byte[] extBulk) { + ref byte[] classic, ref byte[] ext, ref byte[] extBulk) { #if TEN_BIT_BLOCKS if (p.hasExtBlocks) { if (p.hasBulkBlockUpdate && count >= 150) { @@ -125,13 +128,15 @@ namespace MCGalaxy.Network { data[0] = Opcode.CpeBulkBlockUpdate; data[1] = (byte)(count - 1); - for (int i = 0, j = 2; i < count; i++) { + for (int i = 0, j = 2; i < count; i++) + { int index = indices[i]; data[j++] = (byte)(index >> 24); data[j++] = (byte)(index >> 16); data[j++] = (byte)(index >> 8); data[j++] = (byte)index; } - for (int i = 0, j = 2 + 256 * sizeof(int); i < count; i++) { + for (int i = 0, j = 2 + 256 * sizeof(int); i < count; i++) + { data[j++] = (BlockRaw)blocks[i]; } @@ -148,7 +153,8 @@ namespace MCGalaxy.Network { byte[] MakeExt() { byte[] data = new byte[count * 9]; - for (int i = 0, j = 0; i < count; i++) { + for (int i = 0, j = 0; i < count; i++) + { int index = indices[i]; int x = (index % level.Width); int y = (index / level.Width) / level.Length; @@ -171,12 +177,14 @@ namespace MCGalaxy.Network { byte[] data = new byte[2 + 256 * 5]; data[0] = Opcode.CpeBulkBlockUpdate; data[1] = (byte)(count - 1); - for (int i = 0, j = 2; i < count; i++) { + for (int i = 0, j = 2; i < count; i++) + { int index = indices[i]; data[j++] = (byte)(index >> 24); data[j++] = (byte)(index >> 16); data[j++] = (byte)(index >> 8); data[j++] = (byte)index; } - for (int i = 0, j = 2 + 256 * sizeof(int); i < count; i++) { + for (int i = 0, j = 2 + 256 * sizeof(int); i < count; i++) + { #if TEN_BIT_BLOCKS BlockID block = blocks[i]; data[j++] = block <= 511 ? (BlockRaw)block : level.GetFallback(block); @@ -189,7 +197,8 @@ namespace MCGalaxy.Network { byte[] MakeNormal() { byte[] data = new byte[count * 8]; - for (int i = 0, j = 0; i < count; i++) { + for (int i = 0, j = 0; i < count; i++) + { int index = indices[i]; int x = (index % level.Width); int y = (index / level.Width) / level.Length; @@ -211,7 +220,8 @@ namespace MCGalaxy.Network { byte[] MakeLimited(byte[] fallback) { byte[] data = new byte[count * 8]; - for (int i = 0, j = 0; i < count; i++) { + for (int i = 0, j = 0; i < count; i++) + { int index = indices[i]; int x = (index % level.Width); int y = (index / level.Width) / level.Length; diff --git a/MCGalaxy/Player/Player.Login.cs b/MCGalaxy/Player/Player.Login.cs index 5e4922d98..e4d0308e1 100644 --- a/MCGalaxy/Player/Player.Login.cs +++ b/MCGalaxy/Player/Player.Login.cs @@ -62,7 +62,8 @@ namespace MCGalaxy // fix for classicube java client, doesn't reply if only send EnvMapAppearance with version 2 Send(Packet.ExtEntry(CpeExt.EnvMapAppearance, 1)); - foreach (CpeExtension ext in Extensions) { + foreach (CpeExtension ext in Extensions) + { Send(Packet.ExtEntry(ext.Name, ext.ServerVersion)); } } @@ -105,8 +106,6 @@ namespace MCGalaxy Server.Background.QueueOnce(ShowAltsTask, name, TimeSpan.Zero); CheckState(); - if (!Directory.Exists("players")) - Directory.CreateDirectory("players"); PlayerDB.LoadNick(this); Game.Team = Team.TeamIn(this); SetPrefix(); diff --git a/MCGalaxy/Server/Server.cs b/MCGalaxy/Server/Server.cs index cacf1dbd8..5923b5818 100644 --- a/MCGalaxy/Server/Server.cs +++ b/MCGalaxy/Server/Server.cs @@ -24,6 +24,7 @@ using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Threading; using MCGalaxy.Commands; +using MCGalaxy.DB; using MCGalaxy.Drawing; using MCGalaxy.Eco; using MCGalaxy.Events.ServerEvents; @@ -141,6 +142,7 @@ namespace MCGalaxy { EnsureDirectoryExists("ranks"); RankInfo.EnsureExists(); Ban.EnsureExists(); + PlayerDB.EnsureDirectoriesExist(); EnsureDirectoryExists("extra"); EnsureDirectoryExists(Paths.WaypointsDir);