some changes

This commit is contained in:
Timerix 2021-10-03 00:13:34 +03:00
parent d352bc69ea
commit ced9f72279
6 changed files with 26 additions and 93 deletions

63
.gitattributes vendored
View File

@ -1,63 +0,0 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp
###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary
###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary
###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain

1
.gitignore vendored
View File

@ -35,6 +35,7 @@ bld/
# Visual Studio 2015/2017 cache/options directory
.vs/
.vshistory/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

View File

@ -39,7 +39,7 @@ namespace DTLib.Filesystem
return output;
}
public static string ReadAllText(string file) => ReadAllBytes(file).ToStr();
public static string ReadAllText(string file) => SimpleConverter.ToString(ReadAllBytes(file));
public static void WriteAllBytes(string file, byte[] content)
{

View File

@ -69,7 +69,7 @@ namespace DTLib.Network
Mutex.Execute(() =>
{
BytesDownloaded = 0;
Filesize = mainSocket.GetPackage().ToStr().ToUInt();
Filesize = SimpleConverter.ToString(mainSocket.GetPackage()).ToUInt();
mainSocket.SendPackage("ready".ToBytes());
int packagesCount = 0;
byte[] buffer = new byte[5120];
@ -144,7 +144,7 @@ namespace DTLib.Network
if (!dirOnClient.EndsWith("\\")) dirOnClient += "\\";
if (!dirOnServer.EndsWith("\\")) dirOnServer += "\\";
Debug("b", "downloading manifest <", "c", dirOnServer + "manifest.dtsod", "b", ">\n");
var manifest = new DtsodV23(DownloadFileToMemory(dirOnServer + "manifest.dtsod").ToStr());
var manifest = new DtsodV23(SimpleConverter.ToString(DownloadFileToMemory(dirOnServer + "manifest.dtsod")));
Debug("g", $"found {manifest.Values.Count} files in manifest\n");
var hasher = new Hasher();
foreach (string fileOnServer in manifest.Keys)

View File

@ -21,7 +21,7 @@ namespace DTLib.Network
if (packageSize == 0 && socket.Available >= 2)
{
socket.Receive(data, data.Length, 0);
packageSize = data.BytesToInt();
packageSize = data.ToInt();
}
if (packageSize != 0 && socket.Available >= packageSize)
{
@ -40,7 +40,7 @@ namespace DTLib.Network
if (data.Length > 65536) throw new Exception($"SendPackage() error: package is too big ({data.Length} bytes)");
if (data.Length == 0) throw new Exception($"SendPackage() error: package has zero size");
var list = new List<byte>();
byte[] packageSize = data.Length.IntToBytes();
byte[] packageSize = data.Length.ToBytes();
if (packageSize.Length == 1) list.Add(0);
list.AddRange(packageSize);
list.AddRange(data);
@ -51,7 +51,7 @@ namespace DTLib.Network
// получает пакет и выбрасывает исключение, если пакет не соответствует образцу
public static void GetAnswer(this Socket socket, string answer)
{
var rec = socket.GetPackage().ToStr();
var rec = SimpleConverter.ToString(socket.GetPackage());
if (rec != answer) throw new Exception($"GetAnswer() error: invalid answer: <{rec}>");
}

View File

@ -9,24 +9,6 @@ namespace DTLib
//
public static class SimpleConverter
{
public static Encoding UTF8 = new UTF8Encoding(false);
// байты в кодировке UTF8 в строку
public static string ToStr(this byte[] bytes) => UTF8.GetString(bytes);
public static string ToStr(this List<byte> bytes) => UTF8.GetString(bytes.ToArray());
// хеш в виде массива байт в строку (хеш изначально не в кодировке UTF8, так что метод выше не работает с ним)
public static string HashToString(this byte[] hash)
{
var builder = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
builder.Append(hash[i].ToString("x2"));
}
return builder.ToString();
}
// строку в байты
public static byte[] ToBytes(this string str) => UTF8.GetBytes(str);
// эти методы работают как надо, в отличии от стандартных, которые иногда дуркуют
public static bool StartsWith(this byte[] source, byte[] startsWith)
@ -88,14 +70,14 @@ namespace DTLib
public static sbyte ToSByte<T>(this T input) => Convert.ToSByte(input);
public static bool ToBool<T>(this T input) => Convert.ToBoolean(input);
public static int BytesToInt(this byte[] bytes)
public static int ToInt(this byte[] bytes)
{
int output = 0;
for (ushort i = 0; i < bytes.Length; i++) output = output * 256 + bytes[i];
return output;
}
public static byte[] IntToBytes(this int num)
public static byte[] ToBytes(this int num)
{
List<byte> output = new();
while (num != 0)
@ -106,15 +88,30 @@ namespace DTLib
output.Reverse();
return output.ToArray();
}
public static byte[] ToBytes(this string str) => UTF8.GetBytes(str);
public static string AutoBuild(params object[] parts)
public static Encoding UTF8 = new UTF8Encoding(false);
// байты в кодировке UTF8 в строку
public static string ToString(this byte[] bytes) => UTF8.GetString(bytes);
// хеш в виде массива байт в строку (хеш изначально не в кодировке UTF8, так что метод выше не работает с ним)
public static string HashToString(this byte[] hash)
{
var builder = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
builder.Append(hash[i].ToString("x2"));
}
return builder.ToString();
}
public static string MergeToString(params object[] parts)
{
StringBuilder builder = new();
for (int i = 0; i < parts.Length; i++)
builder.Append(parts[i].ToString());
return builder.ToString();
}
public static string MergeToString<T>(this IEnumerable<T> collection, string separator)
{
StringBuilder builder = new();
@ -130,9 +127,7 @@ namespace DTLib
{
StringBuilder builder = new();
foreach (T elem in collection)
{
builder.Append(elem.ToString());
}
return builder.ToString();
}