Skip to content

Commit

Permalink
一些函数返回Task以保留调用栈,Textractor不存在时报错
Browse files Browse the repository at this point in the history
  • Loading branch information
imba-tjd committed Nov 2, 2021
1 parent 5aafbed commit d7c92c0
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 20 deletions.
7 changes: 5 additions & 2 deletions MisakaTranslator-WPF/GuidePages/Hook/ChooseGamePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ private void ConfirmButton_Click(object sender, RoutedEventArgs e)
Common.textHooker = new TextHookHandle(SameNameGameProcessList);
}

Common.textHooker.Init(!(bool)x64GameCheckBox.IsChecked);

if(!Common.textHooker.Init(!(bool)x64GameCheckBox.IsChecked))
{
HandyControl.Controls.MessageBox.Show(Application.Current.Resources["MainWindow_TextractorError_Hint"].ToString());
return;
}

Common.GameID = -1;
string filepath = ProcessHelper.FindProcessPath(GamePid);
Expand Down
26 changes: 14 additions & 12 deletions MisakaTranslator-WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private void Back_MouseLeftButtonDown(object sender, System.Windows.Input.MouseB
GameInfoDrawer.IsOpen = true;
}

private void StartTranslateByGid(int gid) {
private async Task StartTranslateByGid(int gid) {
var pidList = new List<Process>();

foreach (var p in Process.GetProcesses())
Expand Down Expand Up @@ -256,7 +256,11 @@ private void StartTranslateByGid(int gid) {

Common.textHooker = pidList.Count == 1 ? new TextHookHandle(pidList[0].Id) : new TextHookHandle(pidList);

Common.textHooker.Init(!gameInfoList[gid].Isx64);
if(!Common.textHooker.Init(!gameInfoList[gid].Isx64))
{
HandyControl.Controls.MessageBox.Show(Application.Current.Resources["MainWindow_TextractorError_Hint"].ToString());
return;
}
Common.textHooker.HookCodeList.Add(gameInfoList[gid].Hookcode);
Common.textHooker.HookCode_Custom = gameInfoList[gid].HookCodeCustom;

Expand All @@ -269,12 +273,10 @@ private void StartTranslateByGid(int gid) {
Common.textHooker.MisakaCodeList = null;
//2020-06-08 大部分情况无重复码的游戏不会hook到很多,不进行去多余hook
//Common.textHooker.DetachUnrelatedHookWhenDataRecv = Convert.ToBoolean(Common.appSettings.AutoDetach);
Common.textHooker.StartHook(Convert.ToBoolean(Common.appSettings.AutoHook));
var task1 = Task.Run(async delegate
{
await Task.Delay(3000);
Common.textHooker.Auto_AddHookToGame();
});
await Common.textHooker.StartHook(Convert.ToBoolean(Common.appSettings.AutoHook));

await Task.Delay(3000);
Common.textHooker.Auto_AddHookToGame();

var tw = new TranslateWindow();
tw.Show();
Expand All @@ -290,7 +292,7 @@ private async void StartBtn_Click(object sender, RoutedEventArgs e) {
res?.WaitForInputIdle(5000);
GameInfoDrawer.IsOpen = false;
await Task.Delay(2000);
StartTranslateByGid(gid);
await StartTranslateByGid(gid);
}

/// <summary>
Expand Down Expand Up @@ -326,7 +328,7 @@ private async void LEStartBtn_Click(object sender, RoutedEventArgs e) {
res?.WaitForInputIdle(5000);
GameInfoDrawer.IsOpen = false;
await Task.Delay(2000);
StartTranslateByGid(gid);
await StartTranslateByGid(gid);
}

private void BlurWindow_Closing(object sender, CancelEventArgs e) {
Expand Down Expand Up @@ -369,13 +371,13 @@ private void Language_MenuItem_Click(object sender, RoutedEventArgs e) {
}
}

private void AutoStart_BtnClick(object sender, RoutedEventArgs e) {
private async void AutoStart_BtnClick(object sender, RoutedEventArgs e) {
var res = GetGameListHasProcessGame_PID_ID();
if (res == -1) {
Growl.ErrorGlobal(Application.Current.Resources["MainWindow_AutoStartError_Hint"].ToString());
}
else {
StartTranslateByGid(res);
await StartTranslateByGid(res);
}
}

Expand Down
10 changes: 5 additions & 5 deletions MisakaTranslator-WPF/TranslateWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ private void TranslateText(string repairedText, bool isRenew = false)
UpdateSource(repairedText);

// 分别获取两个翻译结果
TranslateApiSubmitSync(repairedText, 1, isRenew);
TranslateApiSubmitSync(repairedText, 2, isRenew);
TranslateApiSubmitASync(repairedText, 1, isRenew);
TranslateApiSubmitASync(repairedText, 2, isRenew);
}
}

Expand Down Expand Up @@ -494,7 +494,7 @@ private void UpdateSource(string repairedText)
/// <param name="repairedText">原文</param>
/// <param name="tranResultIndex">翻译框序号</param>
/// <param name="isRenew">是否是重新获取翻译</param>
private async void TranslateApiSubmitSync(string repairedText, int tranResultIndex, bool isRenew = false)
private async Task TranslateApiSubmitASync(string repairedText, int tranResultIndex, bool isRenew = false)
{
//4.翻译前预处理
string beforeString = _beforeTransHandle.AutoHandle(repairedText);
Expand Down Expand Up @@ -730,8 +730,8 @@ private void RenewOCR_Item_Click(object sender, RoutedEventArgs e)
}
else
{
TranslateApiSubmitSync(_currentsrcText, 1, true);
TranslateApiSubmitSync(_currentsrcText, 2, true);
TranslateApiSubmitASync(_currentsrcText, 1, true);
TranslateApiSubmitASync(_currentsrcText, 2, true);
}
}

Expand Down
1 change: 1 addition & 0 deletions MisakaTranslator-WPF/lang/en-US.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<sys:String x:Key="MainWindow_AutoStartError_Hint">Game process with saved settings is not found!</sys:String>
<sys:String x:Key="MainWindow_GlobalOCRError_Hint">Global OCR shortcut registration failed!</sys:String>
<sys:String x:Key="MainWindow_StartError_Hint">Game process not found!</sys:String>
<sys:String x:Key="MainWindow_TextractorError_Hint">Textractor doesn't exist or failed to start.</sys:String>

<sys:String x:Key="SettingsMenu_WinName">Preferences</sys:String>
<sys:String x:Key="SettingsMenu_About">MisakaTranslator</sys:String>
Expand Down
1 change: 1 addition & 0 deletions MisakaTranslator-WPF/lang/zh-CN.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<sys:String x:Key="MainWindow_AutoStartError_Hint">未在当前进程列表中找到可以自动开始翻译的游戏!</sys:String>
<sys:String x:Key="MainWindow_GlobalOCRError_Hint">全局OCR热键注册失败!</sys:String>
<sys:String x:Key="MainWindow_StartError_Hint">未找到游戏进程!</sys:String>
<sys:String x:Key="MainWindow_TextractorError_Hint">Textractor不存在或启动失败</sys:String>


<sys:String x:Key="SettingsMenu_WinName">MisakaTranslator本体设置</sys:String>
Expand Down
2 changes: 1 addition & 1 deletion TextHookLibrary/TextHookHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public async void CloseTextractor() {
/// <summary>
/// 开始注入,会判断是否智能注入
/// </summary>
public async void StartHook(bool AutoHook = false) {
public async Task StartHook(bool AutoHook = false) {
if (HandleMode == 1) {
await AttachProcess(GamePID);
}
Expand Down

0 comments on commit d7c92c0

Please sign in to comment.