From d7aa28a65d99eafaccf6d495928fd3fd1fe48082 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 22 Feb 2023 03:56:17 +0600 Subject: [PATCH] EmbeddedResources.CopyTo --- DTLib.Dtsod/DtsodBuilder.cs | 17 ----------- DTLib.Dtsod/DtsodDict.cs | 41 +++++++++++++-------------- DTLib.Dtsod/DtsodV23.cs | 10 +++---- DTLib/Filesystem/EmbeddedResources.cs | 23 +++++++++++---- 4 files changed, 42 insertions(+), 49 deletions(-) delete mode 100644 DTLib.Dtsod/DtsodBuilder.cs diff --git a/DTLib.Dtsod/DtsodBuilder.cs b/DTLib.Dtsod/DtsodBuilder.cs deleted file mode 100644 index 4b9e31b..0000000 --- a/DTLib.Dtsod/DtsodBuilder.cs +++ /dev/null @@ -1,17 +0,0 @@ -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/DtsodDict.cs b/DTLib.Dtsod/DtsodDict.cs index 34a7398..164d12e 100644 --- a/DTLib.Dtsod/DtsodDict.cs +++ b/DTLib.Dtsod/DtsodDict.cs @@ -1,12 +1,12 @@ namespace DTLib.Dtsod; -public class DtsodDict : IDictionary, IDictionary +public abstract class DtsodDict : IDictionary, IDictionary { // да, вместо собственной реализации интерфейса это ссылки на Dictionary readonly Dictionary baseDict; - public DtsodDict() => baseDict = new(); - public DtsodDict(IDictionary srcDict) => baseDict = new(srcDict); + protected DtsodDict() => baseDict = new(); + protected DtsodDict(IDictionary srcDict) => baseDict = new(srcDict); public virtual TVal this[TKey key] @@ -20,26 +20,30 @@ public class DtsodDict : IDictionary, IDictionary public virtual bool TryGetValue(TKey key, out TVal value) => baseDict.TryGetValue(key, out value); - public virtual bool TrySetValue(TKey key, TVal value) + public bool TrySetValue(TKey key, TVal value) { - if (ContainsKey(key)) - { - baseDict[key] = value; - return true; - } - else return false; - } - public virtual void Append(ICollection> anotherDtsod) - { - foreach (KeyValuePair pair in anotherDtsod) - Add(pair.Key, pair.Value); - } + if (!ContainsKey(key)) + return false; + baseDict[key] = value; + return true; + } + public virtual void Add(TKey key, TVal value) => baseDict.Add(key, value); public virtual void Add(KeyValuePair pair) => ((ICollection>)baseDict).Add(pair); + public void Add(object key, object value) + { + ((IDictionary) baseDict).Add(key, value); + } + + public void Append(IEnumerable> anotherDtsod) + { + foreach (KeyValuePair pair in anotherDtsod) + Add(pair); + } public void CopyTo(Array array, int index) { @@ -64,11 +68,6 @@ public class DtsodDict : IDictionary, IDictionary set => ((IDictionary) baseDict)[key] = value; } - public void Add(object key, object value) - { - ((IDictionary) baseDict).Add(key, value); - } - public virtual void Clear() => baseDict.Clear(); public bool Contains(object key) { diff --git a/DTLib.Dtsod/DtsodV23.cs b/DTLib.Dtsod/DtsodV23.cs index 646d647..481552f 100644 --- a/DTLib.Dtsod/DtsodV23.cs +++ b/DTLib.Dtsod/DtsodV23.cs @@ -13,9 +13,9 @@ public class DtsodV23 : DtsodDict, IDtsod public DtsodVersion Version { get; } = DtsodVersion.V23; public IDictionary ToDictionary() => this; - public DtsodV23() : base() {} + public DtsodV23() {} public DtsodV23(IDictionary dict) : base(dict) {} - public DtsodV23(string serialized) => Append(Deserialize(serialized)); + public DtsodV23(string serialized) => this.Append(Deserialize(serialized)); static DtsodV23 Deserialize(string _text) { @@ -213,7 +213,7 @@ public class DtsodV23 : DtsodDict, IDtsod } } } - }; + } } object value = null; @@ -296,9 +296,9 @@ public class DtsodV23 : DtsodDict, IDtsod }; protected StringBuilder Serialize(DtsodV23 dtsod, ref int tabscount, StringBuilder b = null) - {; + { tabscount++; - if (b is null) b = new StringBuilder(); + b ??= new StringBuilder(); foreach (var pair in dtsod) { b.Append('\t', tabscount).Append(pair.Key).Append(": "); diff --git a/DTLib/Filesystem/EmbeddedResources.cs b/DTLib/Filesystem/EmbeddedResources.cs index 1512297..02b3407 100644 --- a/DTLib/Filesystem/EmbeddedResources.cs +++ b/DTLib/Filesystem/EmbeddedResources.cs @@ -5,14 +5,18 @@ namespace DTLib.Filesystem; public static class EmbeddedResources { - public static Stream GetResourceStream(string resourcePath, Assembly assembly = null) => - (assembly ?? Assembly.GetCallingAssembly()) - .GetManifestResourceStream(resourcePath) - ?? throw new Exception($"embedded resource <{resourcePath}> not found"); + public static Stream GetResourceStream(string resourcePath, Assembly assembly = null) + { + assembly ??= Assembly.GetCallingAssembly(); + return assembly.GetManifestResourceStream(resourcePath) + ?? throw new Exception($"embedded resource <{resourcePath}> not found in assembly {assembly.FullName}"); + } public static byte[] ReadBynary(string resourcePath, Assembly assembly = null) { - if (assembly is null) assembly = Assembly.GetCallingAssembly(); + // is required to get the Assembly called ReadBynary + // otherwise Assembly.GetCallingAssembly() in GetResourceStream will get DTLib assembly always + assembly ??= Assembly.GetCallingAssembly(); using var reader = new BinaryReader(GetResourceStream(resourcePath, assembly)); return reader.ReadBytes(int.MaxValue); } @@ -20,8 +24,15 @@ public static class EmbeddedResources public static string ReadText(string resourcePath, Assembly assembly = null) { - if (assembly is null) assembly = Assembly.GetCallingAssembly(); + assembly ??= Assembly.GetCallingAssembly(); using var reader = new StreamReader(GetResourceStream(resourcePath, assembly)); return reader.ReadToEnd(); } + + public static void CopyToFile(string resourcePath, string filePath, Assembly assembly = null) + { + assembly ??= Assembly.GetCallingAssembly(); + using var file = File.OpenWrite(filePath); + GetResourceStream(resourcePath, assembly).FluentCopyTo(file).Close(); + } } \ No newline at end of file