Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Feb 15, 2023
1 parent 0a32822 commit 931d565
Show file tree
Hide file tree
Showing 73 changed files with 3,295 additions and 130 deletions.
2 changes: 1 addition & 1 deletion ref/ArchiSteamFarm
2 changes: 1 addition & 1 deletion ref/DirectoryPackages
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
23 changes: 23 additions & 0 deletions src/BD.WTTS.Client/App/IApplication.Clipboard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using AppResources = BD.WTTS.Client.Resources.Strings;

// ReSharper disable once CheckNamespace
namespace BD.WTTS;

partial interface IApplication
{
/// <summary>
/// 通用复制字符串到剪贴板,并在成功后显示 toast
/// </summary>
/// <param name="text"></param>
/// <param name="msgToast"></param>
/// <param name="showToast"></param>
/// <returns></returns>
static async Task CopyToClipboardAsync(string? text, string? msgToast = null, bool showToast = true)
{
if (!string.IsNullOrWhiteSpace(text))
{
await Clipboard2.SetTextAsync(text);
if (showToast) Toast.Show(msgToast ?? AppResources.CopyToClipboard);
}
}
}
17 changes: 17 additions & 0 deletions src/BD.WTTS.Client/App/IApplication.InitSettingSubscribe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ReSharper disable once CheckNamespace
namespace BD.WTTS;

partial interface IApplication
{
/// <summary>
/// 初始化设置项变更时监听
/// </summary>
void InitSettingSubscribe()
{
UISettings.Theme.Subscribe(x => Theme = (AppTheme)x);
UISettings.Language.Subscribe(ResourceService.ChangeLanguage);
}

/// <inheritdoc cref="IApplication.InitSettingSubscribe"/>
void PlatformInitSettingSubscribe() => InitSettingSubscribe();
}
242 changes: 242 additions & 0 deletions src/BD.WTTS.Client/App/IApplication.Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
using ASFNLogManager = ArchiSteamFarm.LogManager;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
using NInternalLogger = NLog.Common.InternalLogger;
using NLogLevel = NLog.LogLevel;
using NLogManager = NLog.LogManager;

// ReSharper disable once CheckNamespace
namespace BD.WTTS;

partial interface IApplication
{
#region Logger

const LogLevel DefaultLoggerMinLevel = AssemblyInfo.Debuggable ? LogLevel.Debug : LogLevel.Error;

public static readonly NLogLevel DefaultNLoggerMinLevel = ConvertLogLevel(DefaultLoggerMinLevel);

/// <summary>
/// Convert log level to NLog variant.
/// </summary>
/// <param name="logLevel">level to be converted.</param>
/// <returns></returns>
static NLogLevel ConvertLogLevel(LogLevel logLevel) => logLevel switch
{
// https://github.com/NLog/NLog.Extensions.Logging/blob/v1.7.0/src/NLog.Extensions.Logging/Logging/NLogLogger.cs#L416
LogLevel.Trace => NLogLevel.Trace,
LogLevel.Debug => NLogLevel.Debug,
LogLevel.Information => NLogLevel.Info,
LogLevel.Warning => NLogLevel.Warn,
LogLevel.Error => NLogLevel.Error,
LogLevel.Critical => NLogLevel.Fatal,
LogLevel.None => NLogLevel.Off,
_ => NLogLevel.Debug,
};

static void SetNLoggerMinLevel(LogLevel logLevel) => SetNLoggerMinLevel(ConvertLogLevel(logLevel));

static void SetNLoggerMinLevel(NLogLevel logLevel)
{
NLogManager.GlobalThreshold = logLevel;
NInternalLogger.LogLevel = logLevel;
}

public static void TrySetLoggerMinLevel(LogLevel logLevel)
{
try
{
var o = LoggerFilterOptions;
if (o != null) o.MinLevel = logLevel;
}
catch
{
}
SetNLoggerMinLevel(ConvertLogLevel(logLevel));
}

static LoggerFilterOptions? _LoggerFilterOptions;

/// <summary>
/// 日志过滤选项
/// </summary>
static LoggerFilterOptions? LoggerFilterOptions
{
get
{
if (_LoggerFilterOptions != null) return _LoggerFilterOptions;
return DI.Get_Nullable<IOptions<LoggerFilterOptions>>()?.Value;
}
set => _LoggerFilterOptions = value;
}

/// <summary>
/// 配置 NLog 提供程序,仅初始化时调用
/// </summary>
/// <returns></returns>
public static (LogLevel minLevel, Action<ILoggingBuilder> cfg) ConfigureLogging()
{
var minLevel = DefaultLoggerMinLevel;

void _(ILoggingBuilder builder)
{
builder.ClearProviders();

builder.SetMinimumLevel(minLevel);
SetNLoggerMinLevel(minLevel);

// 可以多个日志提供同时用,比如还可以在 Win 平台再添加一个 Windows 事件日志

builder.AddNLog(NLogManager.Configuration); // 添加 NLog 日志
}

return (minLevel, (Action<ILoggingBuilder>)_);
}

/// <summary>
/// 日志最低等级
/// </summary>
public static LogLevel LoggerMinLevel
{
get
{
var o = LoggerFilterOptions;
if (o == null)
{
o = new();
LoggerFilterOptions = o;
}
return o.MinLevel;
}

set
{
var o = LoggerFilterOptions;
if (o != null)
{
o.MinLevel = value;
}
SetNLoggerMinLevel(value);
}
}

/// <summary>
/// 启用日志
/// </summary>
public static bool EnableLogger
{
get => LoggerMinLevel > LogLevel.None;
set
{
LoggerMinLevel = value ? DefaultLoggerMinLevel : LogLevel.None;
}
}

#endregion

#region LogHelper

/// <summary>
/// 日志存放文件夹路径
/// </summary>
public static string LogDirPath { get; private set; } = string.Empty;

/// <summary>
/// 日志存放文件夹路径(ASF)
/// </summary>
public static string LogDirPathASF { get; private set; } = string.Empty;

#if DEBUG
/// <summary>
/// 日志文件夹是否存放在缓存文件夹中,通常日志文件夹将存放在基目录上,因某些平台基目录为只读,则只能放在缓存文件夹中
/// </summary>
[Obsolete("only True.", true)]
public static bool LogUnderCache => true;
#endif

public const string LogDirName = "Logs";

public static void InitLogDir(string? alias = null, bool isTrace = false)
{
if (!string.IsNullOrEmpty(LogDirPath)) return;

//var devicePlatform = DeviceInfo2.Platform();
//LogUnderCache = devicePlatform switch
//{
// Platform.Windows => DesktopBridge.IsRunningAsUwp,
// Platform.Linux or Platform.Android or Platform.Apple or Platform.UWP => true,
// _ => throw new ArgumentOutOfRangeException(nameof(devicePlatform), devicePlatform, null),
//};

//var logDirPath = Path.Combine(/*LogUnderCache ?*/
// IOPath.CacheDirectory /*:*/
// /*IOPath.BaseDirectory*/,
// LogDirName);
var logDirPath = Path.Combine(IOPath.CacheDirectory, LogDirName);
IOPath.DirCreateByNotExists(logDirPath);
if (isTrace) StartWatchTrace.Record("InitLogDir.IO");
var logDirPath_ = logDirPath + Path.DirectorySeparatorChar;

NInternalLogger.LogFile = logDirPath_ + "internal-nlog" + alias + ".txt";
NInternalLogger.LogLevel = NLogLevel.Error;
var objConfig = new LoggingConfiguration();
var defMinLevel = DefaultNLoggerMinLevel;
var logfile = new FileTarget("logfile")
{
FileName = logDirPath_ + "nlog-all-${shortdate}" + alias + ".log",
Layout = "${longdate}|${level}|${logger}|${message} |${all-event-properties} ${exception:format=tostring}",
ArchiveAboveSize = 10485760,
MaxArchiveFiles = 14,
MaxArchiveDays = 7,
};
objConfig.AddTarget(logfile);
InitializeTarget(objConfig, logfile, defMinLevel);
if (isTrace) StartWatchTrace.Record("InitLogDir.CreateCfg");
NLogManager.Configuration = objConfig;

LogDirPathASF = ASFPathHelper.GetLogDirectory(logDirPath_);
IArchiSteamFarmService.InitCoreLoggers = () =>
{
if (ASFNLogManager.Configuration != null) return;
LoggingConfiguration config = new();
FileTarget fileTarget = new("File")
{
ArchiveFileName = ASFPathHelper.GetNLogArchiveFileName(LogDirPathASF),
ArchiveNumbering = ArchiveNumberingMode.Rolling,
ArchiveOldFileOnStartup = true,
CleanupFileName = false,
ConcurrentWrites = false,
DeleteOldFileOnStartup = true,
FileName = ASFPathHelper.GetNLogFileName(LogDirPathASF),
Layout = ASFPathHelper.NLogGeneralLayout,
ArchiveAboveSize = 10485760,
MaxArchiveFiles = 10,
MaxArchiveDays = 7,
};
InitializeTarget(config, fileTarget, NLogLevel.Debug);
var historyTarget = new HistoryTarget("History")
{
Layout = ASFPathHelper.NLogGeneralLayout,
MaxCount = 20,
};
InitializeTarget(config, historyTarget, NLogLevel.Debug);
ASFNLogManager.Configuration = config;
};

LogDirPath = logDirPath;
static void InitializeTarget(LoggingConfiguration config, Target target, NLogLevel minLevel)
{
config.AddTarget(target);
if (minLevel < NLogLevel.Warn)
{
config.LoggingRules.Add(new LoggingRule("Microsoft*", target) { FinalMinLevel = NLogLevel.Warn });
config.LoggingRules.Add(new LoggingRule("Microsoft.Hosting.Lifetime*", target) { FinalMinLevel = NLogLevel.Info });
config.LoggingRules.Add(new LoggingRule("System*", target) { FinalMinLevel = NLogLevel.Warn });
}
config.LoggingRules.Add(new LoggingRule("*", minLevel, target));
}

Log.LoggerFactory = () => new LoggerFactory(new[] { new NLogLoggerProvider() });
}

#endregion
}
5 changes: 5 additions & 0 deletions src/BD.WTTS.Client/App/IApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ static bool IsDesktop()
/// </summary>
object CurrentPlatformUIHost { get; }

DeploymentMode DeploymentMode => DeploymentMode.SCD;

/// <inheritdoc cref="IPlatformService.SetBootAutoStart(bool, string)"/>
static void SetBootAutoStart(bool isAutoStart) => IPlatformService.Instance.SetBootAutoStart(isAutoStart, Constants.HARDCODED_APP_NAME);

bool IsAvaloniaUI() => false;

bool IsMAUI() => false;
Expand Down
12 changes: 12 additions & 0 deletions src/BD.WTTS.Client/Attributes/AuthorizeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// ReSharper disable once CheckNamespace
namespace BD.WTTS;

/// <summary>
/// 指定应用此特性的类需要指定的授权
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public sealed class AuthorizeAttribute : Attribute
{
public static bool HasAuthorize(object obj)
=> obj.GetType().GetCustomAttributes<AuthorizeAttribute>(true).Any();
}
3 changes: 2 additions & 1 deletion src/BD.WTTS.Client/BD.WTTS.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<ItemGroup>
<PackageReference Include="BD.Common" />
<PackageReference Include="BD.Common.Area" />
<PackageReference Include="BD.Common.Security" />
<PackageReference Include="BD.Common.Mvvm" />
<PackageReference Include="BD.Common.Primitives" />
<PackageReference Include="BD.Common.Primitives.ApiResponse" />
Expand All @@ -64,6 +65,7 @@
<PackageReference Include="AutoMapper" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" />
<PackageReference Include="SharpZipLib" />
<PackageReference Include="Ae.DNS.Client" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows' OR $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'macos' OR $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst' OR $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == ''">
Expand Down Expand Up @@ -97,7 +99,6 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Services\Implementation\Steam\" />
<Folder Include="UI\ViewModels\Pages\Accelerator\" />
<Folder Include="UI\ViewModels\Pages\AccountSwitch\" />
<Folder Include="UI\ViewModels\Pages\ArchiSteamFarm\" />
Expand Down
43 changes: 43 additions & 0 deletions src/BD.WTTS.Client/Enums/Accelerator/ProxyMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace BD.WTTS.Enums;

/// <summary>
/// 代理模式
/// </summary>
public enum ProxyMode : byte
{
/// <summary>
/// 通过注入驱动实现 DNS 拦截进行代理(Windows Only)
/// </summary>
[Description("ProxyMode_DNSIntercept")]
DNSIntercept,

/// <summary>
/// 修改 Hosts 文件进行代理(Desktop Only)
/// </summary>
[Description("ProxyMode_Hosts")]
Hosts,

/// <summary>
/// 系统代理模式(Desktop Only)
/// </summary>
[Description("ProxyMode_System")]
System,

/// <summary>
/// VPN 代理模式(虚拟网卡)
/// </summary>
[Description("ProxyMode_VPN")]
VPN,

/// <summary>
/// 仅代理模式
/// </summary>
[Description("ProxyMode_ProxyOnly")]
ProxyOnly,

/// <summary>
/// PAC代理模式(Desktop Only)
/// </summary>
[Description("ProxyMode_PAC")]
PAC,
}
Loading

0 comments on commit 931d565

Please sign in to comment.