From 7373fecdd0702221954c2190a40b80c5015ad991 Mon Sep 17 00:00:00 2001
From: imba-tjd <109224573@qq.com>
Date: Mon, 28 Jun 2021 16:45:35 +0800
Subject: [PATCH] =?UTF-8?q?TextHook=EF=BC=9A=E4=BB=A5U16=E7=BC=96=E7=A0=81?=
=?UTF-8?q?=E5=86=99=E5=85=A5CLI?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Directory.Build.props | 1 +
.../GuidePages/Hook/ChooseGamePage.xaml.cs | 2 +-
MisakaTranslator-WPF/IAppSettings.cs | 6 ++
MisakaTranslator-WPF/MainWindow.xaml.cs | 2 +-
.../DictionaryPages/MecabDictPage.xaml.cs | 1 -
.../SettingsPages/HookSettingsPage.xaml | 32 +++++--
.../SettingsPages/HookSettingsPage.xaml.cs | 85 +++++++++++++++++++
MisakaTranslator-WPF/lang/en-US.xaml | 2 +-
MisakaTranslator-WPF/lang/zh-CN.xaml | 2 +-
TextHookLibrary/NativeMethods.txt | 2 +
TextHookLibrary/TextHookHandle.cs | 61 ++++++++-----
11 files changed, 163 insertions(+), 33 deletions(-)
diff --git a/Directory.Build.props b/Directory.Build.props
index 0d8c6927..7159642e 100644
--- a/Directory.Build.props
+++ b/Directory.Build.props
@@ -7,6 +7,7 @@
AnyCPU
11.0
2.12.2.0
+ true
diff --git a/MisakaTranslator-WPF/GuidePages/Hook/ChooseGamePage.xaml.cs b/MisakaTranslator-WPF/GuidePages/Hook/ChooseGamePage.xaml.cs
index f6d11db3..880dd5eb 100644
--- a/MisakaTranslator-WPF/GuidePages/Hook/ChooseGamePage.xaml.cs
+++ b/MisakaTranslator-WPF/GuidePages/Hook/ChooseGamePage.xaml.cs
@@ -54,7 +54,7 @@ private void ConfirmButton_Click(object sender, RoutedEventArgs e)
Common.textHooker = new TextHookHandle(SameNameGameProcessList);
}
- if(!Common.textHooker.Init(!(bool)x64GameCheckBox.IsChecked))
+ if (!Common.textHooker.Init((bool)x64GameCheckBox.IsChecked ? Common.appSettings.Textractor_Path64 : Common.appSettings.Textractor_Path32))
{
HandyControl.Controls.MessageBox.Show(Application.Current.Resources["MainWindow_TextractorError_Hint"].ToString());
return;
diff --git a/MisakaTranslator-WPF/IAppSettings.cs b/MisakaTranslator-WPF/IAppSettings.cs
index 54320919..c0a58748 100644
--- a/MisakaTranslator-WPF/IAppSettings.cs
+++ b/MisakaTranslator-WPF/IAppSettings.cs
@@ -455,6 +455,12 @@ string ForegroundHex
[Option(Alias = "Mecab.dicPath", DefaultValue = "")]
string Mecab_dicPath { get; set; }
+
+ [Option(Alias = "Textractor.Path32", DefaultValue = "")]
+ string Textractor_Path32 { get; set; }
+
+ [Option(Alias = "Textractor.Path64", DefaultValue = "")]
+ string Textractor_Path64 { get; set; }
}
public interface IRepeatRepairSettings
diff --git a/MisakaTranslator-WPF/MainWindow.xaml.cs b/MisakaTranslator-WPF/MainWindow.xaml.cs
index 6afb0dda..33bfd787 100644
--- a/MisakaTranslator-WPF/MainWindow.xaml.cs
+++ b/MisakaTranslator-WPF/MainWindow.xaml.cs
@@ -250,7 +250,7 @@ private async Task StartTranslateByGid(int gid) {
Common.textHooker = pidList.Count == 1 ? new TextHookHandle(pidList[0].Id) : new TextHookHandle(pidList);
- if(!Common.textHooker.Init(!gameInfoList[gid].Isx64))
+ if (!Common.textHooker.Init(gameInfoList[gid].Isx64 ? Common.appSettings.Textractor_Path64 : Common.appSettings.Textractor_Path32))
{
HandyControl.Controls.MessageBox.Show(Application.Current.Resources["MainWindow_TextractorError_Hint"].ToString());
return;
diff --git a/MisakaTranslator-WPF/SettingsPages/DictionaryPages/MecabDictPage.xaml.cs b/MisakaTranslator-WPF/SettingsPages/DictionaryPages/MecabDictPage.xaml.cs
index bfd49d05..0bd73bd6 100644
--- a/MisakaTranslator-WPF/SettingsPages/DictionaryPages/MecabDictPage.xaml.cs
+++ b/MisakaTranslator-WPF/SettingsPages/DictionaryPages/MecabDictPage.xaml.cs
@@ -17,7 +17,6 @@ public MecabDictPage()
private void ChoosePathBtn_Click(object sender, RoutedEventArgs e)
{
-
FolderBrowserDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
diff --git a/MisakaTranslator-WPF/SettingsPages/HookSettingsPage.xaml b/MisakaTranslator-WPF/SettingsPages/HookSettingsPage.xaml
index 3d625cab..21427165 100644
--- a/MisakaTranslator-WPF/SettingsPages/HookSettingsPage.xaml
+++ b/MisakaTranslator-WPF/SettingsPages/HookSettingsPage.xaml
@@ -13,12 +13,32 @@
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/MisakaTranslator-WPF/SettingsPages/HookSettingsPage.xaml.cs b/MisakaTranslator-WPF/SettingsPages/HookSettingsPage.xaml.cs
index e96691f8..a5935122 100644
--- a/MisakaTranslator-WPF/SettingsPages/HookSettingsPage.xaml.cs
+++ b/MisakaTranslator-WPF/SettingsPages/HookSettingsPage.xaml.cs
@@ -1,4 +1,5 @@
using System;
+using System.IO;
using System.Windows;
using System.Windows.Controls;
@@ -15,6 +16,9 @@ public HookSettingsPage()
AutoHookCheckBox.IsChecked = Convert.ToBoolean(Common.appSettings.AutoHook);
AutoDetachCheckBox.IsChecked = Convert.ToBoolean(Common.appSettings.AutoDetach);
+
+ Path32Box.Text = Common.appSettings.Textractor_Path32;
+ Path64Box.Text = Common.appSettings.Textractor_Path64;
}
private void AutoHookCheckBox_Click(object sender, RoutedEventArgs e)
@@ -39,5 +43,86 @@ private void Button_Click(object sender, RoutedEventArgs e)
}
}
+ private void ChoosePath32Btn_Click(object sender, RoutedEventArgs e)
+ {
+ System.Windows.Forms.FolderBrowserDialog dialog = new();
+ if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+ {
+ CheckCLI(dialog.SelectedPath, true);
+ }
+ }
+
+ private void ChoosePath64Btn_Click(object sender, RoutedEventArgs e)
+ {
+ System.Windows.Forms.FolderBrowserDialog dialog = new();
+ if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+ {
+ CheckCLI(dialog.SelectedPath, false);
+ }
+ }
+
+ private void CheckCLI(string path, bool isX86)
+ {
+ if (File.Exists($@"{path}\TextractorCLI.exe"))
+ {
+ if (path.Contains("x86"))
+ {
+ Path32Box.Text = $@"{path}\TextractorCLI.exe";
+ Common.appSettings.Textractor_Path32 = Path32Box.Text;
+ if (File.Exists($@"{path.Replace("x86", "x64")}\TextractorCLI.exe"))
+ {
+ Path64Box.Text = $@"{path.Replace("x86", "x64")}\TextractorCLI.exe";
+ Common.appSettings.Textractor_Path64 = Path64Box.Text;
+ }
+ }
+ else if (path.Contains("x64"))
+ {
+ Path64Box.Text = $@"{path}\TextractorCLI.exe";
+ Common.appSettings.Textractor_Path64 = Path64Box.Text;
+ if (File.Exists($@"{path.Replace("x64", "x86")}\TextractorCLI.exe"))
+ {
+ Path32Box.Text = $@"{path.Replace("x64", "x86")}\TextractorCLI.exe";
+ Common.appSettings.Textractor_Path32 = Path32Box.Text;
+ }
+ }
+ else
+ {
+ if (isX86)
+ {
+ Path32Box.Text = $@"{path}\TextractorCLI.exe";
+ Common.appSettings.Textractor_Path32 = Path32Box.Text;
+ }
+ else
+ {
+ Path64Box.Text = $@"{path}\TextractorCLI.exe";
+ Common.appSettings.Textractor_Path64 = Path64Box.Text;
+ }
+ }
+ }
+ else if (File.Exists($@"{path}\x86\TextractorCLI.exe"))
+ {
+ Path32Box.Text = $@"{path}\x86\TextractorCLI.exe";
+ Common.appSettings.Textractor_Path32 = Path32Box.Text;
+ if (File.Exists($@"{path}\x64\TextractorCLI.exe"))
+ {
+ Path64Box.Text = $@"{path}\x64\TextractorCLI.exe";
+ Common.appSettings.Textractor_Path64 = Path64Box.Text;
+ }
+ }
+ else if (File.Exists($@"{path}\x64\TextractorCLI.exe"))
+ {
+ Path64Box.Text = $@"{path}\x64\TextractorCLI.exe";
+ Common.appSettings.Textractor_Path64 = Path64Box.Text;
+ if (File.Exists($@"{path}\x86\TextractorCLI.exe"))
+ {
+ Path32Box.Text = $@"{path}\x86\TextractorCLI.exe";
+ Common.appSettings.Textractor_Path32 = Path32Box.Text;
+ }
+ }
+ else
+ {
+ MessageBox.Show("找不到TextractorCLI.exe\nCan't find TextractorCLI.exe", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
+ }
+ }
}
}
\ No newline at end of file
diff --git a/MisakaTranslator-WPF/lang/en-US.xaml b/MisakaTranslator-WPF/lang/en-US.xaml
index e34f4eea..2145f885 100644
--- a/MisakaTranslator-WPF/lang/en-US.xaml
+++ b/MisakaTranslator-WPF/lang/en-US.xaml
@@ -153,7 +153,7 @@
TesseractCli Parameters
Select
- This is the text hook method (Textractor) setting. Textractor was recoded and runs along with the Translator. There is no need to change its path.
+ This is Textractor setting. You need to choose the path of its folder after installing or extracting it.
Enable smart hook
Once enabled, Translator hooks the process that uses the biggest memory among multiple processes with the same name.
Note: This approach is slightly more efficient, but may not find the text.
diff --git a/MisakaTranslator-WPF/lang/zh-CN.xaml b/MisakaTranslator-WPF/lang/zh-CN.xaml
index 0b63dcca..296f271f 100644
--- a/MisakaTranslator-WPF/lang/zh-CN.xaml
+++ b/MisakaTranslator-WPF/lang/zh-CN.xaml
@@ -150,7 +150,7 @@
自定义参数
选择
- 在这里进行文本Hook注入方式(Textractor)的相关设置;Textractor模块已经根据需要重写过并随本程序一起运行,您不需要更改它的位置
+ 在这里进行Textractor的相关设置;您需要选择安装或解压后的文件夹位置
开启智能注入
开启智能注入后,遇到某些游戏存在多个同名进程的情况下,将自动选择占用内存最大的进程注入
注意:这种方式可以略微提高效率,但可能导致无法寻找到文本
diff --git a/TextHookLibrary/NativeMethods.txt b/TextHookLibrary/NativeMethods.txt
index 372a9137..f901f31d 100644
--- a/TextHookLibrary/NativeMethods.txt
+++ b/TextHookLibrary/NativeMethods.txt
@@ -1,3 +1,5 @@
// function
+Windows.Win32.System.Console.AllocConsole
+Windows.Win32.System.Console.FreeConsole
Windows.Win32.System.DataExchange.ChangeClipboardChain
Windows.Win32.System.DataExchange.SetClipboardViewer
diff --git a/TextHookLibrary/TextHookHandle.cs b/TextHookLibrary/TextHookHandle.cs
index 1592c3c8..5cc74412 100644
--- a/TextHookLibrary/TextHookHandle.cs
+++ b/TextHookLibrary/TextHookHandle.cs
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
+using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using Windows.Win32;
namespace TextHookLibrary {
///
@@ -132,36 +134,51 @@ public TextHookHandle() {
/// 初始化Textractor,建立CLI与本软件间的通信
///
/// 成功返回真,失败返回假
- public bool Init(bool x86 = true) {
- string Path = Environment.CurrentDirectory + @"\lib\TextHook\" + (x86 ? "x86" : "x64");//打开对应的Textractor
- string CurrentPath = Environment.CurrentDirectory;
- try {
- Environment.CurrentDirectory = Path;//更改当前工作目录保证TextractorCLI正常运行
- }
-#pragma warning disable 168
- catch (System.IO.DirectoryNotFoundException ex) {
-#pragma warning restore 168
+ public bool Init(string path)
+ {
+ if(!File.Exists(path))
+ {
return false;
}
- ProcessTextractor = new Process();
- ProcessTextractor.StartInfo.FileName = "TextractorCLI.exe";
- ProcessTextractor.StartInfo.CreateNoWindow = true;
- ProcessTextractor.StartInfo.UseShellExecute = false;
- ProcessTextractor.StartInfo.StandardOutputEncoding = Encoding.Unicode;
- ProcessTextractor.StartInfo.RedirectStandardInput = true;
- ProcessTextractor.StartInfo.RedirectStandardOutput = true;
+ ProcessTextractor = new Process()
+ {
+ StartInfo = new ProcessStartInfo()
+ {
+ FileName = path,
+ CreateNoWindow = true,
+ UseShellExecute = false,
+#if NETCOREAPP
+ StandardInputEncoding = new UnicodeEncoding(false, false),
+#endif
+ StandardOutputEncoding = Encoding.Unicode,
+ RedirectStandardInput = true,
+ RedirectStandardOutput = true,
+ WorkingDirectory = Path.GetDirectoryName(path)
+ },
+ };
+
ProcessTextractor.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
- try {
+ try
+ {
+#if NETFRAMEWORK
+ // .NET Framework根据Console.InputEncoding编码在Start()中创建输入流
+ Console.InputEncoding = new UnicodeEncoding(false, false);
+#endif
bool res = ProcessTextractor.Start();
+#if NETFRAMEWORK
+ // Console.InputEncoding修改为非UTF16编码需要创建控制台
+ PInvoke.AllocConsole();
+ Console.InputEncoding = Encoding.Default;
+ PInvoke.FreeConsole();
+#endif
ProcessTextractor.BeginOutputReadLine();
- Environment.CurrentDirectory = CurrentPath;//打开后即可恢复原目录
return res;
}
-#pragma warning disable 168
- catch (System.ComponentModel.Win32Exception ex) {
-#pragma warning restore 168
- Environment.CurrentDirectory = CurrentPath;//恢复原目录
+ catch (System.ComponentModel.Win32Exception)
+ {
+ ProcessTextractor.Dispose();
+ ProcessTextractor = null;
return false;
}
}