EmbeddedResources.CopyTo

This commit is contained in:
Timerix22 2023-02-22 03:56:17 +06:00
parent 11e5148cd2
commit d7aa28a65d
4 changed files with 42 additions and 49 deletions

View File

@ -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());
}

View File

@ -1,12 +1,12 @@
namespace DTLib.Dtsod; namespace DTLib.Dtsod;
public class DtsodDict<TKey, TVal> : IDictionary<TKey, TVal>, IDictionary public abstract class DtsodDict<TKey, TVal> : IDictionary<TKey, TVal>, IDictionary
{ {
// да, вместо собственной реализации интерфейса это ссылки на Dictionary // да, вместо собственной реализации интерфейса это ссылки на Dictionary
readonly Dictionary<TKey, TVal> baseDict; readonly Dictionary<TKey, TVal> baseDict;
public DtsodDict() => baseDict = new(); protected DtsodDict() => baseDict = new();
public DtsodDict(IDictionary<TKey, TVal> srcDict) => baseDict = new(srcDict); protected DtsodDict(IDictionary<TKey, TVal> srcDict) => baseDict = new(srcDict);
public virtual TVal this[TKey key] public virtual TVal this[TKey key]
@ -20,19 +20,13 @@ public class DtsodDict<TKey, TVal> : IDictionary<TKey, TVal>, IDictionary
public virtual bool TryGetValue(TKey key, out TVal value) => baseDict.TryGetValue(key, out value); 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)) if (!ContainsKey(key))
{ return false;
baseDict[key] = value; baseDict[key] = value;
return true; return true;
}
else return false;
}
public virtual void Append(ICollection<KeyValuePair<TKey, TVal>> anotherDtsod)
{
foreach (KeyValuePair<TKey, TVal> pair in anotherDtsod)
Add(pair.Key, pair.Value);
} }
public virtual void Add(TKey key, TVal value) => baseDict.Add(key, value); public virtual void Add(TKey key, TVal value) => baseDict.Add(key, value);
@ -40,6 +34,16 @@ public class DtsodDict<TKey, TVal> : IDictionary<TKey, TVal>, IDictionary
public virtual void Add(KeyValuePair<TKey, TVal> pair) public virtual void Add(KeyValuePair<TKey, TVal> pair)
=> ((ICollection<KeyValuePair<TKey, TVal>>)baseDict).Add(pair); => ((ICollection<KeyValuePair<TKey, TVal>>)baseDict).Add(pair);
public void Add(object key, object value)
{
((IDictionary) baseDict).Add(key, value);
}
public void Append(IEnumerable<KeyValuePair<TKey, TVal>> anotherDtsod)
{
foreach (KeyValuePair<TKey, TVal> pair in anotherDtsod)
Add(pair);
}
public void CopyTo(Array array, int index) public void CopyTo(Array array, int index)
{ {
@ -64,11 +68,6 @@ public class DtsodDict<TKey, TVal> : IDictionary<TKey, TVal>, IDictionary
set => ((IDictionary) baseDict)[key] = value; set => ((IDictionary) baseDict)[key] = value;
} }
public void Add(object key, object value)
{
((IDictionary) baseDict).Add(key, value);
}
public virtual void Clear() => baseDict.Clear(); public virtual void Clear() => baseDict.Clear();
public bool Contains(object key) public bool Contains(object key)
{ {

View File

@ -13,9 +13,9 @@ public class DtsodV23 : DtsodDict<string, dynamic>, IDtsod
public DtsodVersion Version { get; } = DtsodVersion.V23; public DtsodVersion Version { get; } = DtsodVersion.V23;
public IDictionary<string, dynamic> ToDictionary() => this; public IDictionary<string, dynamic> ToDictionary() => this;
public DtsodV23() : base() {} public DtsodV23() {}
public DtsodV23(IDictionary<string, dynamic> dict) : base(dict) {} public DtsodV23(IDictionary<string, dynamic> dict) : base(dict) {}
public DtsodV23(string serialized) => Append(Deserialize(serialized)); public DtsodV23(string serialized) => this.Append(Deserialize(serialized));
static DtsodV23 Deserialize(string _text) static DtsodV23 Deserialize(string _text)
{ {
@ -213,7 +213,7 @@ public class DtsodV23 : DtsodDict<string, dynamic>, IDtsod
} }
} }
} }
}; }
} }
object value = null; object value = null;
@ -296,9 +296,9 @@ public class DtsodV23 : DtsodDict<string, dynamic>, IDtsod
}; };
protected StringBuilder Serialize(DtsodV23 dtsod, ref int tabscount, StringBuilder b = null) protected StringBuilder Serialize(DtsodV23 dtsod, ref int tabscount, StringBuilder b = null)
{; {
tabscount++; tabscount++;
if (b is null) b = new StringBuilder(); b ??= new StringBuilder();
foreach (var pair in dtsod) foreach (var pair in dtsod)
{ {
b.Append('\t', tabscount).Append(pair.Key).Append(": "); b.Append('\t', tabscount).Append(pair.Key).Append(": ");

View File

@ -5,14 +5,18 @@ namespace DTLib.Filesystem;
public static class EmbeddedResources public static class EmbeddedResources
{ {
public static Stream GetResourceStream(string resourcePath, Assembly assembly = null) => public static Stream GetResourceStream(string resourcePath, Assembly assembly = null)
(assembly ?? Assembly.GetCallingAssembly()) {
.GetManifestResourceStream(resourcePath) assembly ??= Assembly.GetCallingAssembly();
?? throw new Exception($"embedded resource <{resourcePath}> not found"); 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) 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)); using var reader = new BinaryReader(GetResourceStream(resourcePath, assembly));
return reader.ReadBytes(int.MaxValue); return reader.ReadBytes(int.MaxValue);
} }
@ -20,8 +24,15 @@ public static class EmbeddedResources
public static string ReadText(string resourcePath, Assembly assembly = null) 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)); using var reader = new StreamReader(GetResourceStream(resourcePath, assembly));
return reader.ReadToEnd(); 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();
}
} }