From 5d77e4cfd374ad88880e714cb00a9357557672f9 Mon Sep 17 00:00:00 2001 From: Timerix Date: Tue, 7 Sep 2021 20:14:36 +0300 Subject: [PATCH] few fixes --- Color.cs | 2 +- DTLib.csproj | 1 + Dtsod/DtsodV21.cs | 42 ++++++++++++++++++++++++++---------------- Network/FSP.cs | 41 +++++++++++++++++++++++++---------------- 4 files changed, 53 insertions(+), 33 deletions(-) diff --git a/Color.cs b/Color.cs index db09e38..e7c4691 100644 --- a/Color.cs +++ b/Color.cs @@ -2,7 +2,7 @@ namespace DTLib { - public class Color + public abstract class Color { public record RGBA(byte R, byte G, byte B, byte A) { diff --git a/DTLib.csproj b/DTLib.csproj index b995282..1ff8a22 100644 --- a/DTLib.csproj +++ b/DTLib.csproj @@ -31,6 +31,7 @@ + diff --git a/Dtsod/DtsodV21.cs b/Dtsod/DtsodV21.cs index 1b11779..250afa9 100644 --- a/Dtsod/DtsodV21.cs +++ b/Dtsod/DtsodV21.cs @@ -95,13 +95,13 @@ namespace DTLib.Dtsod Dictionary parsed = new(); int i = 0; for (; i < text.Length; i++) ReadName(); - if (debug) LogNoTime("g", $"Parse returns {parsed.Keys.Count} keys\n"); + DebugNoTime("g", $"Parse returns {parsed.Keys.Count} keys\n"); return parsed; // СЛОМАНО /*void ReadCommentLine() { - for (; i < text.Length && text[i] != '\n'; i++) if (debug) LogNoTime("gray", text[i].ToString()); + for (; i < text.Length && text[i] != '\n'; i++) DebugNoTime("gray", text[i].ToString()); }*/ void ReadName() @@ -111,7 +111,7 @@ namespace DTLib.Dtsod dynamic value = null; StringBuilder defaultNameBuilder = new(); - if (debug) LogNoTime("m", "ReadName\n"); + DebugNoTime("m", "ReadName\n"); for (; i < text.Length; i++) { switch (text[i]) @@ -125,7 +125,7 @@ namespace DTLib.Dtsod i++; string name = defaultNameBuilder.ToString(); value = ReadValue(); - if (debug) LogNoTime("c", $"parsed.Add({name}, {value} { value.GetType() })\n"); + DebugNoTime("c", $"parsed.Add({name}, {value} { value.GetType() })\n"); if (isListElem) { if (!parsed.ContainsKey(name)) parsed.Add(name, new List()); @@ -141,14 +141,14 @@ namespace DTLib.Dtsod throw new Exception("Parse.ReadName() error: unexpected '}' at " + i + " char"); // если $ перед названием параметра поставить, значение value добавится в лист с названием name case '$': - if (debug) LogNoTime("w", text[i].ToString()); + DebugNoTime("w", text[i].ToString()); if (defaultNameBuilder.ToString().Length != 0) throw new Exception("Parse.ReadName() error: unexpected '$' at " + i + " char"); isListElem = true; break; case ';': throw new Exception("Parse.ReadName() error: unexpected ';' at " + i + " char"); default: - if (debug) LogNoTime("w", text[i].ToString()); + DebugNoTime("w", text[i].ToString()); defaultNameBuilder.Append(text[i]); break; } @@ -167,11 +167,11 @@ namespace DTLib.Dtsod valueBuilder.Append('"'); for (; text[i] != '"' || text[i - 1] == '\\'; i++) { - if (debug) LogNoTime("gray", text[i].ToString()); + DebugNoTime("gray", text[i].ToString()); valueBuilder.Append(text[i]); } valueBuilder.Append('"'); - if (debug) LogNoTime("gray", text[i].ToString()); + DebugNoTime("gray", text[i].ToString()); type = ValueType.String; return valueBuilder.ToString(); } @@ -183,7 +183,7 @@ namespace DTLib.Dtsod StringBuilder valueBuilder = new(); for (; text[i] != ']'; i++) { - if (debug) LogNoTime("c", text[i].ToString()); + DebugNoTime("c", text[i].ToString()); switch (text[i]) { case ' ': @@ -206,7 +206,7 @@ namespace DTLib.Dtsod ParseValueToRightType(valueBuilder.ToString()); output.Add(value); } - if (debug) LogNoTime("c", text[i].ToString()); + DebugNoTime("c", text[i].ToString()); type = ValueType.List; return output; } @@ -218,7 +218,7 @@ namespace DTLib.Dtsod i++; for (; balance != 0; i++) { - if (debug) LogNoTime("y", text[i].ToString()); + DebugNoTime("y", text[i].ToString()); switch (text[i]) { case '"': @@ -226,12 +226,12 @@ namespace DTLib.Dtsod break; case '}': balance--; - if (debug) LogNoTime("b", $"\nbalance -- = {balance}\n"); + DebugNoTime("b", $"\nbalance -- = {balance}\n"); if (balance != 0) valueBuilder.Append(text[i]); break; case '{': balance++; - if (debug) LogNoTime("b", $"\nbalance ++ = {balance}\n"); + DebugNoTime("b", $"\nbalance ++ = {balance}\n"); valueBuilder.Append(text[i]); break; default: @@ -247,7 +247,7 @@ namespace DTLib.Dtsod void ParseValueToRightType(string stringValue) { - if (debug) LogNoTime("b", $"\nParseValueToRightType({stringValue})\n"); + DebugNoTime("b", $"\nParseValueToRightType({stringValue})\n"); switch (stringValue) { @@ -300,10 +300,10 @@ namespace DTLib.Dtsod } StringBuilder defaultValueBuilder = new(); - if (debug) LogNoTime("m", "\nReadValue\n"); + DebugNoTime("m", "\nReadValue\n"); for (; i < text.Length; i++) { - if (debug) LogNoTime("b", text[i].ToString()); + DebugNoTime("b", text[i].ToString()); switch (text[i]) { case ' ': @@ -343,5 +343,15 @@ namespace DTLib.Dtsod throw new Exception("Dtsod.Parse.ReadValue error: wtf it's the end of function"); } } + + + void Debug(params string[] msg) + { + if (debug) Log(msg); + } + void DebugNoTime(params string[] msg) + { + if (debug) LogNoTime(msg); + } } } diff --git a/Network/FSP.cs b/Network/FSP.cs index 4001bc7..0100036 100644 --- a/Network/FSP.cs +++ b/Network/FSP.cs @@ -14,7 +14,7 @@ namespace DTLib.Network public class FSP { Socket mainSocket; - public bool debug = false; + static public bool debug = false; public FSP(Socket _mainSocket) => mainSocket = _mainSocket; public uint BytesDownloaded = 0; @@ -28,7 +28,7 @@ namespace DTLib.Network // скачивает файл с помощью FSP протокола public void DownloadFile(string filePath_server, string filePath_client) { - if (debug) Log("b", $"requesting file download: {filePath_server}\n"); + Debug("b", $"requesting file download: {filePath_server}\n"); mainSocket.SendPackage("requesting file download".ToBytes()); mainSocket.SendPackage(filePath_server.ToBytes()); DownloadFile(filePath_client); @@ -52,7 +52,7 @@ namespace DTLib.Network PackageRecieved(BytesDownloaded); });*/ // получение файла - + for (byte n = 0; packagesCount < fullPackagesCount; packagesCount++) { buffer = mainSocket.GetPackage(); @@ -76,13 +76,13 @@ namespace DTLib.Network //speedCounter.Stop(); fileStream.Flush(); fileStream.Close(); - if (debug) Log(new string[] { "g", $" downloaded {BytesDownloaded} of {Filesize} bytes\n" }); + Debug(new string[] { "g", $" downloaded {BytesDownloaded} of {Filesize} bytes\n" }); } public byte[] DownloadFileToMemory(string filePath_server) { BytesDownloaded = 0; - if (debug) Log("b", $"requesting file download: {filePath_server}\n"); + Debug("b", $"requesting file download: {filePath_server}\n"); mainSocket.SendPackage("requesting file download".ToBytes()); mainSocket.SendPackage(filePath_server.ToBytes()); using var fileStream = new System.IO.MemoryStream(); @@ -117,14 +117,14 @@ namespace DTLib.Network //speedCounter.Stop(); byte[] output = fileStream.GetBuffer(); fileStream.Close(); - if (debug) Log(new string[] { "g", $" downloaded {BytesDownloaded} of {Filesize} bytes\n" }); + Debug(new string[] { "g", $" downloaded {BytesDownloaded} of {Filesize} bytes\n" }); return output; } // отдаёт файл с помощью FSP протокола public void UploadFile(string filePath) { - if (debug) Log("b", $"uploading file {filePath}\n"); + Debug("b", $"uploading file {filePath}\n"); using var fileStream = File.OpenRead(filePath); Filesize = File.GetSize(filePath).ToUInt(); var fileHash = new Hasher().HashFile(filePath); @@ -160,32 +160,32 @@ namespace DTLib.Network } //speedCounter.Stop(); fileStream.Close(); - if (debug) Log(new string[] { "g", $" uploaded {BytesUploaded} of {Filesize} bytes\n" }); + Debug(new string[] { "g", $" uploaded {BytesUploaded} of {Filesize} bytes\n" }); } public void DownloadByManifest(string dirOnServer, string dirOnClient, bool overwrite = false, bool delete_excess = false) { if (!dirOnClient.EndsWith("\\")) dirOnClient += "\\"; if (!dirOnServer.EndsWith("\\")) dirOnServer += "\\"; - if (debug) Log("b", "downloading manifest <", "c", dirOnServer + "manifest.dtsod", "b", ">\n"); + Debug("b", "downloading manifest <", "c", dirOnServer + "manifest.dtsod", "b", ">\n"); var manifest = new DtsodV22(DownloadFileToMemory(dirOnServer + "manifest.dtsod").ToStr()); - if (debug) Log("g", $"found {manifest.Values.Count} files in manifest\n"); + Debug("g", $"found {manifest.Values.Count} files in manifest\n"); var hasher = new Hasher(); foreach (string fileOnServer in manifest.Keys) { string fileOnClient = dirOnClient + fileOnServer; - if (debug) Log("b", "file <", "c", fileOnClient, "b", ">... "); + Debug("b", "file <", "c", fileOnClient, "b", ">... "); if (!File.Exists(fileOnClient)) { - if (debug) LogNoTime("y", "doesn't exist\n"); + DebugNoTime("y", "doesn't exist\n"); DownloadFile(dirOnServer + fileOnServer, fileOnClient); } else if (overwrite && hasher.HashFile(fileOnClient).HashToString() != manifest[fileOnServer]) { - if (debug) LogNoTime("y", "outdated\n"); + DebugNoTime("y", "outdated\n"); DownloadFile(dirOnServer + fileOnServer, fileOnClient); } - else if (debug) LogNoTime("g", "without changes\n"); + else DebugNoTime("g", "without changes\n"); } // удаление лишних файлов if (delete_excess) @@ -195,7 +195,7 @@ namespace DTLib.Network { if (!manifest.ContainsKey(file.Remove(0, dirOnClient.Length))) { - if (debug) Log("y", $"deleting excess file: {file}\n"); + Debug("y", $"deleting excess file: {file}\n"); File.Delete(file); } } @@ -204,7 +204,7 @@ namespace DTLib.Network { if (Directory.Exists(dir) && Directory.GetAllFiles(dir).Count == 0) { - if (debug) Log("y", $"deleting empty dir: {dir}\n"); + Debug("y", $"deleting empty dir: {dir}\n"); Directory.Delete(dir); } }*/ @@ -230,5 +230,14 @@ namespace DTLib.Network File.WriteAllText(dir + "manifest.dtsod", manifestBuilder.ToString()); Log($"g", $" manifest of {dir} created\n"); } + + void Debug(params string[] msg) + { + if (debug) Log(msg); + } + void DebugNoTime(params string[] msg) + { + if (debug) LogNoTime(msg); + } } }