2018-03-21 20:56:56 +08:00
|
|
|
using System;
|
2018-11-02 00:14:18 +08:00
|
|
|
using System.IO;
|
2018-03-13 14:54:15 +08:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Sockets;
|
2020-11-28 05:39:56 +08:00
|
|
|
using NLog;
|
2018-03-13 14:54:15 +08:00
|
|
|
|
|
|
|
namespace BililiveRecorder.Core
|
|
|
|
{
|
2018-03-24 11:07:43 +08:00
|
|
|
public static class Utils
|
2018-03-13 14:54:15 +08:00
|
|
|
{
|
2018-03-24 11:07:43 +08:00
|
|
|
internal static byte[] ToBE(this byte[] b)
|
2018-03-13 14:54:15 +08:00
|
|
|
{
|
2018-10-30 23:41:38 +08:00
|
|
|
if (BitConverter.IsLittleEndian)
|
|
|
|
{
|
|
|
|
return b.Reverse().ToArray();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return b;
|
|
|
|
}
|
2018-03-13 14:54:15 +08:00
|
|
|
}
|
|
|
|
|
2018-03-24 11:07:43 +08:00
|
|
|
internal static void ReadB(this NetworkStream stream, byte[] buffer, int offset, int count)
|
2018-03-13 14:54:15 +08:00
|
|
|
{
|
|
|
|
if (offset + count > buffer.Length)
|
2018-10-30 23:41:38 +08:00
|
|
|
{
|
2018-03-13 14:54:15 +08:00
|
|
|
throw new ArgumentException();
|
2018-10-30 23:41:38 +08:00
|
|
|
}
|
|
|
|
|
2018-03-13 14:54:15 +08:00
|
|
|
int read = 0;
|
|
|
|
while (read < count)
|
|
|
|
{
|
|
|
|
var available = stream.Read(buffer, offset, count - read);
|
|
|
|
if (available == 0)
|
|
|
|
{
|
|
|
|
throw new ObjectDisposedException(null);
|
|
|
|
}
|
|
|
|
read += available;
|
|
|
|
offset += available;
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 00:12:32 +08:00
|
|
|
|
2020-04-26 13:00:10 +08:00
|
|
|
internal static string RemoveInvalidFileName(this string name, bool ignore_slash = false)
|
2018-11-02 00:14:18 +08:00
|
|
|
{
|
|
|
|
foreach (char c in Path.GetInvalidFileNameChars())
|
|
|
|
{
|
2020-04-26 13:00:10 +08:00
|
|
|
if (ignore_slash && (c == '\\' || c == '/'))
|
|
|
|
continue;
|
2018-11-02 00:14:18 +08:00
|
|
|
name = name.Replace(c, '_');
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2020-11-28 13:02:57 +08:00
|
|
|
public static bool CopyPropertiesTo<T>(this T source, T target) where T : class
|
2018-03-20 00:12:32 +08:00
|
|
|
{
|
2020-11-28 13:02:57 +08:00
|
|
|
if (source == null || target == null || source == target) { return false; }
|
|
|
|
foreach (var p in source.GetType().GetProperties())
|
2018-03-20 00:12:32 +08:00
|
|
|
{
|
2018-11-01 23:40:50 +08:00
|
|
|
if (Attribute.IsDefined(p, typeof(DoNotCopyProperty)))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-28 13:02:57 +08:00
|
|
|
var val = p.GetValue(source);
|
|
|
|
if (val == null || !val.Equals(p.GetValue(target)))
|
2018-10-30 23:41:38 +08:00
|
|
|
{
|
2020-11-28 13:02:57 +08:00
|
|
|
p.SetValue(target, val);
|
2018-10-30 23:41:38 +08:00
|
|
|
}
|
2018-03-20 00:12:32 +08:00
|
|
|
}
|
2018-11-01 23:40:50 +08:00
|
|
|
return true;
|
2018-03-20 00:12:32 +08:00
|
|
|
}
|
2018-03-21 20:56:56 +08:00
|
|
|
|
2018-11-01 23:40:50 +08:00
|
|
|
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
|
|
|
|
public class DoNotCopyProperty : Attribute { }
|
|
|
|
|
2018-03-24 11:07:43 +08:00
|
|
|
internal static void Log(this Logger logger, int id, LogLevel level, string message, Exception exception = null)
|
2018-03-21 20:56:56 +08:00
|
|
|
{
|
|
|
|
var log = new LogEventInfo()
|
|
|
|
{
|
|
|
|
Level = level,
|
|
|
|
Message = message,
|
|
|
|
Exception = exception,
|
|
|
|
};
|
|
|
|
log.Properties["roomid"] = id;
|
|
|
|
logger.Log(log);
|
|
|
|
}
|
2018-10-30 23:41:38 +08:00
|
|
|
|
|
|
|
private static string _useragent;
|
|
|
|
internal static string UserAgent
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(_useragent))
|
|
|
|
{
|
|
|
|
string version = typeof(Utils).Assembly.GetName().Version.ToString();
|
2020-05-01 07:38:11 +08:00
|
|
|
_useragent = $"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36 BililiveRecorder/{version} (+https://github.com/Bililive/BililiveRecorder;bliverec@danmuji.org)";
|
2018-10-30 23:41:38 +08:00
|
|
|
}
|
|
|
|
return _useragent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:54:15 +08:00
|
|
|
}
|
|
|
|
}
|