From a5c071b1d5368535d1899a534a2e14a63bf69406 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Sun, 13 Mar 2022 23:16:33 +0300 Subject: [PATCH] netframework support and some fixes --- .gitignore | 14 +++++++-- DTLib/DTLib.csproj | 27 +++++++++++++---- DTLib/Dtsod/DtsodBuilder.cs | 17 +++++++++++ DTLib/Dtsod/{V30 => }/DtsodDict.cs | 0 DTLib/Dtsod/DtsodV22.cs | 6 ++-- DTLib/Dtsod/DtsodV23.cs | 4 +-- DTLib/Dtsod/DtsodVersion.cs | 6 ++-- DTLib/Dtsod/DtsodVersionConverter.cs | 2 ++ DTLib/Dtsod/V30/DtsodV30.cs | 2 +- DTLib/Dtsod/V30/TypeHelper.cs | 2 +- DTLib/Experimental/Tester.cs | 3 +- DTLib/Filesystem/Path.cs | 44 +++++++++++++++------------- DTLib/Loggers/AsyncLogger.cs | 8 +++-- DTLib/Loggers/BaseLogger.cs | 2 +- DTLib/Loggers/DefaultLogger.cs | 12 ++++---- 15 files changed, 101 insertions(+), 48 deletions(-) create mode 100644 DTLib/Dtsod/DtsodBuilder.cs rename DTLib/Dtsod/{V30 => }/DtsodDict.cs (100%) diff --git a/.gitignore b/.gitignore index aa7d26f..1e6cd00 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,21 @@ -*.user +# Build results [Bb]in/ .bin/ +[Dd]ebug/ +[Rr]elease/ +[Rr]eleases/ [Oo]bj/ [Oo]ut/ [Ll]og/ [Ll]ogs/ + +# IDE files .vs/ +.vscode/ .vshistory/ .idea/ -.vscode/ \ No newline at end of file +.editorconfig +*.user + +#backups +.old*/ \ No newline at end of file diff --git a/DTLib/DTLib.csproj b/DTLib/DTLib.csproj index 834c39f..b094ab3 100644 --- a/DTLib/DTLib.csproj +++ b/DTLib/DTLib.csproj @@ -1,18 +1,33 @@  - net6.0 + net6.0;net48 + preview false disable portable DTLib - True + + False true False + Debug;Release;Release-net48 - - - - + + net48 + + + + + + + + + + + + + + diff --git a/DTLib/Dtsod/DtsodBuilder.cs b/DTLib/Dtsod/DtsodBuilder.cs new file mode 100644 index 0000000..4b9e31b --- /dev/null +++ b/DTLib/Dtsod/DtsodBuilder.cs @@ -0,0 +1,17 @@ +namespace DTLib.Dtsod; + +public class DtsodBuilder +{ + private StringBuilder b = new(); + + public DtsodBuilder AddProperty(string key, dynamic value) + { + + return this; + } + + + public string BuildToString() => b.ToString(); + + public DtsodV23 Build() => new DtsodV23(BuildToString()); +} \ No newline at end of file diff --git a/DTLib/Dtsod/V30/DtsodDict.cs b/DTLib/Dtsod/DtsodDict.cs similarity index 100% rename from DTLib/Dtsod/V30/DtsodDict.cs rename to DTLib/Dtsod/DtsodDict.cs diff --git a/DTLib/Dtsod/DtsodV22.cs b/DTLib/Dtsod/DtsodV22.cs index 615f4a1..b568d10 100644 --- a/DTLib/Dtsod/DtsodV22.cs +++ b/DTLib/Dtsod/DtsodV22.cs @@ -290,7 +290,7 @@ public class DtsodV22 : Dictionary, IDtsod break; default: throw new Exception($"Dtsod.Parse.ReadValue() error: value <{stringValue}> has wrong type"); - }; + } } // short; long; int else @@ -310,7 +310,7 @@ public class DtsodV22 : Dictionary, IDtsod break; } break; - }; + } } StringBuilder defaultValueBuilder = new(); @@ -344,7 +344,7 @@ public class DtsodV22 : Dictionary, IDtsod case ValueTypes.List: isList = true; break; - }; + } outType = type; return value; // строка, начинающаяся с # будет считаться комментом diff --git a/DTLib/Dtsod/DtsodV23.cs b/DTLib/Dtsod/DtsodV23.cs index ef4de0c..d514977 100644 --- a/DTLib/Dtsod/DtsodV23.cs +++ b/DTLib/Dtsod/DtsodV23.cs @@ -1,6 +1,4 @@ using System.Globalization; -using System.IO; -using System.Linq.Expressions; namespace DTLib.Dtsod; @@ -12,7 +10,7 @@ namespace DTLib.Dtsod; public class DtsodV23 : DtsodDict, IDtsod { - public DtsodVersion Version { get; } = DtsodVersion.V30; + public DtsodVersion Version { get; } = DtsodVersion.V23; public IDictionary ToDictionary() => this; public DtsodV23() : base() {} diff --git a/DTLib/Dtsod/DtsodVersion.cs b/DTLib/Dtsod/DtsodVersion.cs index a98e1de..a2a3b0c 100644 --- a/DTLib/Dtsod/DtsodVersion.cs +++ b/DTLib/Dtsod/DtsodVersion.cs @@ -4,6 +4,8 @@ public enum DtsodVersion : byte { V21 = 21, V22 = 22, - V23 = 23, - V30 = 30 + V23 = 23 +#if DEBUG + ,V30 = 30 +#endif } diff --git a/DTLib/Dtsod/DtsodVersionConverter.cs b/DTLib/Dtsod/DtsodVersionConverter.cs index 3685fa1..13d6bbc 100644 --- a/DTLib/Dtsod/DtsodVersionConverter.cs +++ b/DTLib/Dtsod/DtsodVersionConverter.cs @@ -8,7 +8,9 @@ public static class DtsodVersionConverter DtsodVersion.V21 => new DtsodV21(src.ToDictionary()), DtsodVersion.V22 => throw new NotImplementedException("Converting dtsods to V22 isn't implemented"), DtsodVersion.V23 => new DtsodV23(src.ToDictionary()), +#if DEBUG DtsodVersion.V30 => new DtsodV30(src.ToDictionary()), +#endif _ => throw new Exception($"DtsodVersionConverter.Convert() error: unknown target version <{targetVersion}>"), }; } diff --git a/DTLib/Dtsod/V30/DtsodV30.cs b/DTLib/Dtsod/V30/DtsodV30.cs index c843218..10411c8 100644 --- a/DTLib/Dtsod/V30/DtsodV30.cs +++ b/DTLib/Dtsod/V30/DtsodV30.cs @@ -260,7 +260,7 @@ public class DtsodV30 : DtsodDict, IDtsod StringBuilder b = new(); foreach (KeyValuePair pair in dtsod) { - + SerializeObject(pair.Key, pair.Value); } void SerializeObject(string name, dynamic inst) diff --git a/DTLib/Dtsod/V30/TypeHelper.cs b/DTLib/Dtsod/V30/TypeHelper.cs index f5fdbd2..85af153 100644 --- a/DTLib/Dtsod/V30/TypeHelper.cs +++ b/DTLib/Dtsod/V30/TypeHelper.cs @@ -110,5 +110,5 @@ public class TypeHelper : Type.GetType(str, false) ?? throw new Exception($"DtsodV30.Deserialize.ParseType() error: type {str} doesn't exists") }; - static internal T As(object inst) where T : class => inst as T; + internal static T As(object inst) where T : class => inst as T; } diff --git a/DTLib/Experimental/Tester.cs b/DTLib/Experimental/Tester.cs index d9e9fbb..cb5ba78 100644 --- a/DTLib/Experimental/Tester.cs +++ b/DTLib/Experimental/Tester.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Globalization; namespace DTLib.Experimental; @@ -12,6 +13,6 @@ public static class Tester operation(); clock.Stop(); double time=(double)(clock.ElapsedTicks)/Stopwatch.Frequency/repeats; - LogNoTime("y",$"operation ","b",op_name,"y"," lasted ","b",time.ToString(),"y"," seconds"); + LogNoTime("y",$"operation ","b",op_name,"y"," lasted ","b",time.ToString(CultureInfo.InvariantCulture),"y"," seconds"); } } \ No newline at end of file diff --git a/DTLib/Filesystem/Path.cs b/DTLib/Filesystem/Path.cs index 17142cd..3e25243 100644 --- a/DTLib/Filesystem/Path.cs +++ b/DTLib/Filesystem/Path.cs @@ -1,21 +1,25 @@ -namespace DTLib.Filesystem; - -static public class Path -{ - static public readonly char Sep = OperatingSystem.IsWindows() ? '\\' : '/'; - - public static string CorrectSeparator(string path) - { - if (Sep == '\\') - { - if (path.Contains('/')) - path = path.Replace('/', '\\'); - } - else if (Sep == '/') - { - if (path.Contains('\\')) - path = path.Replace('\\', '/'); - } - return path; - } +namespace DTLib.Filesystem; + +public static class Path +{ + public static readonly char Sep = Environment.OSVersion.Platform == PlatformID.Win32NT ? '\\' : '/'; + + public static string CorrectSeparator(this string path) + { + if (Sep == '\\') + { + if (path.Contains('/')) + path = path.Replace('/', '\\'); + } + else + { + if (path.Contains('\\')) + path = path.Replace('\\', '/'); + } + return path; + } + + // replaces wrong characters to use string as path + public static string NormalizeAsPath(this string str) => + str.Replace(':', '-').Replace(' ', '_'); } \ No newline at end of file diff --git a/DTLib/Loggers/AsyncLogger.cs b/DTLib/Loggers/AsyncLogger.cs index f1a7541..fd0d112 100644 --- a/DTLib/Loggers/AsyncLogger.cs +++ b/DTLib/Loggers/AsyncLogger.cs @@ -1,4 +1,6 @@ -namespace DTLib.Loggers; +using System.Globalization; + +namespace DTLib.Loggers; // вывод лога в консоль и файл public class AsyncLogger : BaseLogger @@ -12,8 +14,8 @@ public class AsyncLogger : BaseLogger { lock (statelocker) if (!IsEnabled) return; // добавление даты - if (msg.Length == 1) msg[0] = "[" + DateTime.Now.ToString() + "]: " + msg[0]; - else msg[1] = "[" + DateTime.Now.ToString() + "]: " + msg[1]; + if (msg.Length == 1) msg[0] = "[" + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "]: " + msg[0]; + else msg[1] = "[" + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "]: " + msg[1]; // перенос строки msg[msg.Length - 1] += '\n'; // вывод в консоль diff --git a/DTLib/Loggers/BaseLogger.cs b/DTLib/Loggers/BaseLogger.cs index f1b0bad..a0f7039 100644 --- a/DTLib/Loggers/BaseLogger.cs +++ b/DTLib/Loggers/BaseLogger.cs @@ -2,13 +2,13 @@ public abstract class BaseLogger { - public string Logfile { get; init; } public BaseLogger() { } public BaseLogger(string logfile) => (Logfile, WriteToFile) = (logfile,true); public BaseLogger(string dir, string programName) : this($"{dir}\\{programName}_{DateTime.Now}.log".Replace(':', '-').Replace(' ', '_')) { } + public string Logfile { get; init; } public bool IsEnabled { get; private set; } = false; public bool WriteToFile { get; private set; } = false; protected readonly object statelocker = new(); diff --git a/DTLib/Loggers/DefaultLogger.cs b/DTLib/Loggers/DefaultLogger.cs index 0c4e19e..d955750 100644 --- a/DTLib/Loggers/DefaultLogger.cs +++ b/DTLib/Loggers/DefaultLogger.cs @@ -1,4 +1,6 @@ -namespace DTLib.Loggers; +using System.Globalization; + +namespace DTLib.Loggers; // вывод лога в консоль и файл public class DefaultLogger : BaseLogger @@ -10,15 +12,15 @@ public class DefaultLogger : BaseLogger public override void Log(params string[] msg) { - lock (Logfile) if (!IsEnabled) return; - if (msg.Length == 1) msg[0] = "[" + DateTime.Now.ToString() + "]: " + msg[0]; - else msg[1] = "[" + DateTime.Now.ToString() + "]: " + msg[1]; + lock (statelocker) if (!IsEnabled) return; + if (msg.Length == 1) msg[0] = "[" + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "]: " + msg[0]; + else msg[1] = "[" + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "]: " + msg[1]; LogNoTime(msg); } public void LogNoTime(params string[] msg) { - lock (Logfile) if (!IsEnabled) return; + lock (statelocker) if (!IsEnabled) return; msg[msg.Length - 1] += '\n'; ColoredConsole.Write(msg); if (WriteToFile)