Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Supported unregister the service by kill cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
emako committed Mar 29, 2023
1 parent b7a6300 commit 563bd1b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/GenshinSwitch.Fetch/Service/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ public static class MainService
{
public static bool ServiceEnabled { get; set; } = false;

public static void Kill()
{
using NamedPipeClientStream pipeClient = new(".", "GenshinSwitch.WindowsService", PipeDirection.InOut);
pipeClient.Connect(2000);
using StreamWriter writer = new(pipeClient);
writer.WriteLine(JsonConvert.SerializeObject(new
{
Command = MainServiceCommmand.Kill.GetHashCode(),
}));
writer.Flush();
return;
}

internal static void SetGameAccountRegisty(string key, string value, GameType type = GameType.CN)
{
using NamedPipeClientStream pipeClient = new(".", "GenshinSwitch.WindowsService", PipeDirection.InOut);
Expand Down
1 change: 1 addition & 0 deletions src/GenshinSwitch.Fetch/Service/MainServiceCommmand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public enum MainServiceCommmand
None = 0x00,
SetGameAccountRegisty = 0x01,
GetGameAccountRegisty = 0x02,
Kill = 0x88,
}
6 changes: 6 additions & 0 deletions src/GenshinSwitch.WindowsService/CommandRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ internal static class CommandRunner
obj.Key,
};
}
else if ((int)obj.Command == (int)MainServiceCommmand.Kill)
{
new AutoStartRegistyHelper().Disable();
Environment.Exit((int)MainServiceCommmand.Kill);
}
}
return null!;
}
Expand All @@ -70,6 +75,7 @@ file enum MainServiceCommmand
None = 0x00,
SetGameAccountRegisty = 0x01,
GetGameAccountRegisty = 0x02,
Kill = 0x88,
}

file enum GameType
Expand Down
13 changes: 12 additions & 1 deletion src/GenshinSwitch/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using GenshinSwitch.Core.Settings;
using GenshinSwitch.Fetch.Lazy;
using GenshinSwitch.Fetch.Muter;
using GenshinSwitch.Fetch.Service;
using GenshinSwitch.Helpers;
using GenshinSwitch.Models;
using GenshinSwitch.Models.Messages;
Expand Down Expand Up @@ -73,8 +74,9 @@ partial void OnDoubleClickBehaviorIndexChanged(int value)
}

[RelayCommand]
private void StopDoubleClickBehaviorService()
private async Task StopDoubleClickBehaviorServiceAsync()
{
#if false
try
{
foreach (Process proc in Process.GetProcessesByName("GenshinSwitch.WindowsService"))
Expand All @@ -93,6 +95,15 @@ private void StopDoubleClickBehaviorService()
{
Debug.WriteLine(e);
}
#endif
try
{
await Task.Run(MainService.Kill);
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}

[ObservableProperty]
Expand Down
2 changes: 1 addition & 1 deletion src/GenshinSwitch/Views/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@
<StackPanel Orientation="Horizontal">
<Button HorizontalAlignment="Right"
Command="{x:Bind ViewModel.StopDoubleClickBehaviorServiceCommand}"
Content="取消服务" Margin="0,0,5,0" ToolTipService.ToolTip="服务动作属于额外的选项功能,需要自行启动并自动注册,此处可以取消服务注册。"/>
Content="取消服务" Margin="0,0,5,0" ToolTipService.ToolTip="服务动作属于额外的选项功能,需要自行启动并自动注册,此处可以取消服务注册。需要服务已启动时才能取消服务注册。"/>
<ComboBox MinWidth="104"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Expand Down

0 comments on commit 563bd1b

Please # to comment.