Skip to content

Commit 4266ef2

Browse files
committed
feat(cli): add cli configurationan
add more doc in readme add icon
1 parent 9a6a54c commit 4266ef2

14 files changed

Lines changed: 103 additions & 56 deletions

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
![Windows Presentation Framework](https://img.shields.io/badge/Windows_Presentation_Framework-blue)
66
[![wakatime](https://wakatime.com/badge/github/c0nstexpr/Turnbind.svg)](https://wakatime.com/badge/github/c0nstexpr/Turnbind)
77

8-
Turnbind is a WPF application for CS surf turn binds
8+
Turnbind is a WPF application for CS surf turn binds, requirs [.NET 8 Runtime](https://dotnet.microsoft.com/download/dotnet/8.0):
99
- Designed to be a simple and easy to use tool for surfers.
1010
- Not depent on any CS plugins, command, etc. All simulations depend on native Windows API.
1111
- Require run as admin
@@ -16,7 +16,17 @@ Inspired by [Conturn](https://github.com/t5mat/conturn)
1616

1717
<br/>
1818

19-
## Getting Started
19+
## Features
20+
- *Available for All Window App*
21+
Customizable process name allow user to enable functionality in any window application.
22+
- *Multi Profiles*
23+
Use different bind settings for various environments
24+
- *Multi Key for Single Bind*
25+
Support multiple key rather than single key. For example, use "A+F" means press A and F in sequence will trigger turn instruction, and release any of them will cancel turning or continue previous turning.
26+
- *Adjust Turn Speed in Pixels*
27+
- *Adjust Turn Precision by Setting Instruction Interval*
28+
29+
## Dev Getting Started
2030

2131
### Prerequisites
2232

@@ -28,4 +38,4 @@ Inspired by [Conturn](https://github.com/t5mat/conturn)
2838

2939
## License
3040

31-
This project is licensed under the [MIT](https://mit-license.org/) license
41+
Project is licensed under [MIT](https://mit-license.org/)

Turnbind/Action/BindControl.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void OnFocuse(bool focused)
5353

5454
void OnActive(bool active)
5555
{
56-
var turnAction = App.GetService<TurnAction>();
56+
var turnAction = App.GetRequiredService<TurnAction>();
5757

5858
if (!active)
5959
{
@@ -75,13 +75,13 @@ void OnActive(bool active)
7575

7676
if (focusedDisposable is { }) return;
7777

78-
focusedDisposable = App.GetService<InputAction>().SubscribeKeys(Keys).Subscribe(OnActive);
78+
focusedDisposable = App.GetRequiredService<InputAction>().SubscribeKeys(Keys).Subscribe(OnActive);
7979
}
8080

8181
m_disposble = new CompositeDisposable()
8282
{
8383
Disposable.Create(() => OnFocuse(false)),
84-
App.GetService<ProcessWindowAction>().Focused.Subscribe(OnFocuse)
84+
App.GetRequiredService<ProcessWindowAction>().Focused.Subscribe(OnFocuse)
8585
};
8686
}
8787
}

Turnbind/Action/InputAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Turnbind.Action;
1212

1313
public sealed partial class InputAction : IDisposable
1414
{
15-
readonly ILogger<InputAction> m_logger = App.GetService<ILogger<InputAction>>();
15+
readonly ILogger<InputAction> m_logger = App.GetRequiredService<ILogger<InputAction>>();
1616

1717
public record struct KeyState(InputKey Key, bool Pressed);
1818

Turnbind/Action/ProcessWindowAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Turnbind;
1010

1111
partial class ProcessWindowAction : IDisposable
1212
{
13-
readonly ILogger<ProcessWindowAction> m_log = App.GetService<ILogger<ProcessWindowAction>>();
13+
readonly ILogger<ProcessWindowAction> m_log = App.GetRequiredService<ILogger<ProcessWindowAction>>();
1414

1515
public string? ProcessName { get; set; }
1616

Turnbind/Action/TurnAction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private struct Input
2828
[LibraryImport("user32", SetLastError = true)]
2929
private static partial uint SendInput(uint inputs, [In] Input[] input, int size);
3030

31-
readonly ILogger<TurnAction> m_log = App.GetService<ILogger<TurnAction>>();
31+
readonly ILogger<TurnAction> m_log = App.GetRequiredService<ILogger<TurnAction>>();
3232

3333
public TimeSpan Interval
3434
{
@@ -38,7 +38,7 @@ public TimeSpan Interval
3838
{
3939
m_pixelPerPeriod = m_pixelPerPeriod / m_timer.Period.TotalSeconds * value.TotalSeconds;
4040
m_timer.Period = value;
41-
m_log.LogInformation("Turn Interval changed to {} ms", value.Milliseconds);
41+
m_log.LogInformation("Turn Interval changed to {ms} ms", value.Milliseconds);
4242
}
4343
}
4444

Turnbind/App.xaml.cs

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Windows;
22
using System.Windows.Threading;
33

4-
using Microsoft.Extensions.Configuration;
54
using Microsoft.Extensions.DependencyInjection;
65
using Microsoft.Extensions.Hosting;
76

@@ -15,44 +14,21 @@
1514
using Microsoft.Extensions.Logging;
1615
using System.Diagnostics;
1716
using System.Runtime.InteropServices;
17+
using Microsoft.Extensions.Configuration;
1818

1919
namespace Turnbind;
2020

2121
public partial class App : Application
2222
{
23-
static readonly LogTextBlock m_logTextBlock = new();
24-
25-
static readonly IHost m_host = Host.CreateDefaultBuilder()
26-
.ConfigureAppConfiguration(c => c.SetBasePath(AppContext.BaseDirectory))
27-
.UseSerilog(
28-
(context, services, loggerConfiguration) => loggerConfiguration
29-
.Enrich.FromLogContext()
30-
.Enrich.WithExceptionDetails()
31-
.WriteTo.Console()
32-
.WriteTo.RichTextBox(m_logTextBlock.LogTextBox)
33-
.WriteTo.File(
34-
new RenderedCompactJsonFormatter(),
35-
$"logs.json",
36-
fileSizeLimitBytes: 1_000_000,
37-
rollOnFileSizeLimit: true
38-
)
39-
)
40-
.ConfigureServices(
41-
(_, services) =>
42-
{
43-
services.AddSingleton<InputAction>();
44-
services.AddSingleton<ProcessWindowAction>();
45-
services.AddSingleton<TurnAction>();
46-
services.AddSingleton(Settings.Load() ?? new());
47-
services.AddSingleton<MainWindowViewModel>();
48-
services.AddSingleton(m_logTextBlock);
49-
}
50-
)
51-
.Build();
23+
IHost m_host = Host.CreateDefaultBuilder().Build();
24+
25+
public static new App Current => (App)Application.Current;
5226

53-
readonly ILogger<App> m_log = GetService<ILogger<App>>();
27+
public static T? GetService<T>() where T : class => Current.m_host.Services.GetService<T>();
5428

55-
public static T GetService<T>() where T : class => m_host.Services.GetRequiredService<T>();
29+
public static T GetRequiredService<T>() where T : class => Current.m_host.Services.GetRequiredService<T>();
30+
31+
public static IConfiguration Configuration => GetRequiredService<IConfiguration>();
5632

5733
[LibraryImport("kernel32", SetLastError = true)]
5834
[return: MarshalAs(UnmanagedType.Bool)]
@@ -61,7 +37,52 @@ public partial class App : Application
6137
void OnStartup(object sender, StartupEventArgs e)
6238
{
6339
SetPriorityClass(Process.GetCurrentProcess().Handle, 0x00000080);
40+
41+
var builder = Host.CreateApplicationBuilder(e.Args);
42+
var config = builder.Configuration;
43+
44+
{
45+
var services = builder.Services;
46+
LogTextBlock? logTextBlock = null;
47+
48+
if (config["Console"] is { })
49+
{
50+
logTextBlock = new LogTextBlock();
51+
services.AddSingleton(logTextBlock);
52+
}
53+
54+
services.AddSerilog(
55+
loggerConfiguration =>
56+
{
57+
loggerConfiguration.Enrich.FromLogContext()
58+
.Enrich.WithExceptionDetails()
59+
.WriteTo.Console()
60+
.WriteTo.File(
61+
new RenderedCompactJsonFormatter(),
62+
$"logs.json",
63+
fileSizeLimitBytes: 1_000_000,
64+
rollOnFileSizeLimit: true
65+
);
66+
67+
if (logTextBlock is { })
68+
loggerConfiguration.WriteTo.RichTextBox(logTextBlock.LogTextBox);
69+
}
70+
)
71+
.AddSingleton<InputAction>()
72+
.AddSingleton<ProcessWindowAction>()
73+
.AddSingleton<TurnAction>()
74+
.AddSingleton(Settings.Load() ?? new())
75+
.AddSingleton<MainWindowViewModel>();
76+
}
77+
78+
if (Enum.TryParse<LogLevel>(config["LogLevel"], out var level))
79+
builder.Logging.SetMinimumLevel(level);
80+
81+
m_host = builder.Build();
6482
m_host.Start();
83+
84+
var c = Configuration;
85+
Console.WriteLine(c.ToString());
6586
}
6687

6788
void OnExit(object sender, ExitEventArgs e) => m_host.StopAsync()
@@ -70,7 +91,7 @@ void OnExit(object sender, ExitEventArgs e) => m_host.StopAsync()
7091

7192
void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
7293
{
73-
m_log.LogError(e.Exception, "Unhandled exception");
94+
GetRequiredService<ILogger<App>>().LogError(e.Exception, "Unhandled exception");
7495
MessageBox.Show(e.Exception.ToString(), "Unhandled exception", MessageBoxButton.OK, MessageBoxImage.Error);
7596
}
7697
}

Turnbind/Assets/logo-icon.ico

10.9 KB
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"Turnbind": {
4+
"commandName": "Project",
5+
"commandLineArgs": "--Console --LogLevel Debug"
6+
}
7+
}
8+
}

Turnbind/Turnbind.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<UseWPF>true</UseWPF>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
99
<ApplicationManifest>app.manifest</ApplicationManifest>
10+
<ApplicationIcon>Assets/logo-icon.ico</ApplicationIcon>
11+
<PackageIcon></PackageIcon>
1012
</PropertyGroup>
1113

1214
<ItemGroup>
@@ -27,6 +29,6 @@
2729

2830
<Page Include="App.xaml" />
2931

30-
<Resource Include="Assets\logo-icon.png" />
32+
<Resource Include="Assets/*" />
3133
</ItemGroup>
3234
</Project>

Turnbind/View/ConsoleWindow.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ namespace Turnbind.View;
44

55
public partial class ConsoleWindow : FluentWindow
66
{
7-
readonly LogTextBlock m_logTextBlock = App.GetService<LogTextBlock>();
7+
readonly LogTextBlock m_logTextBlock = App.GetRequiredService<LogTextBlock>();
88

99
public ConsoleWindow()
1010
{
1111
InitializeComponent();
12-
DockPanel.Children.Add(App.GetService<LogTextBlock>());
12+
DockPanel.Children.Add(App.GetRequiredService<LogTextBlock>());
1313
}
1414

1515
protected override void OnClosed(EventArgs e)

0 commit comments

Comments
 (0)