Skip to content

Commit

Permalink
perf: update mmmojo utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ZGGSONG committed Sep 16, 2024
1 parent 90dd05a commit 2139d20
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ packages/

.DS_Store

publish/
publish/
artifacts/
2 changes: 1 addition & 1 deletion sample/WeChatOcr.Sample/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Windows.Input;
using ScreenGrab.Extensions;
using Wpf.Ui.Tray.Controls;
using System.Windows.Media.Imaging;
using System.Text;

namespace WeChatOcr.Sample;
Expand Down Expand Up @@ -108,6 +107,7 @@ private void Ocr_Click(object sender, RoutedEventArgs e)
ResultTb.Text = "Please wait for the previous OCR operation to complete.";
return;
}
_tcs = new TaskCompletionSource<string>();
try
{
var bytes = ImageUtilities.ConvertBitmap2Bytes(bitmap, ImageFormat.Png);
Expand Down
7 changes: 4 additions & 3 deletions src/WeChatOcr/ImageOcr.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace WeChatOcr;

Expand All @@ -14,7 +15,7 @@ public ImageOcr(string? path = default)
var wechatDir = Utilities.GetWeChatDir(path) ??
throw new Exception($"get wechat path failed: {path ?? "NULL"}");

Utilities.CopyMmmojoDll(wechatDir, AppDomain.CurrentDomain.BaseDirectory);
Utilities.CopyMmmojoDll(wechatDir);

var ocrPtr = GCHandle.ToIntPtr(GCHandle.Alloc(_ocrManager));
_ocrManager = (GCHandle.FromIntPtr(ocrPtr).Target as OcrManager)!;
Expand Down Expand Up @@ -45,7 +46,7 @@ public void Run(string imagePath, Action<string, WeChatOcrResult?>? callback)
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Debug.WriteLine(ex.Message);
return;
}
}
Expand Down
57 changes: 44 additions & 13 deletions src/WeChatOcr/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ public partial class Utilities
/// 微信默认安装目录
/// </summary>
private const string WeChatDefaultPath = @"C:\Program Files\Tencent\WeChat";
private const string MmmojoDll = "mmmojo.dll";
private const string Mmmojo64Dll = "mmmojo_64.dll";

private const string MmMojoDll = "mmmojo.dll";
private const string MmMojo64Dll = "mmmojo_64.dll";

/// <summary>
/// 获取微信安装目录
Expand Down Expand Up @@ -67,17 +68,47 @@ public static void WriteBytesToFile(string filePath, byte[] bytes)
/// <summary>
/// 复制微信安装目录下的mmmojo.dll、mmmojo_64.dll到软件目录
/// </summary>
/// <param name="wechatDir"></param>
/// <param name="targetPath">目标目录</param>
public static void CopyMmmojoDll(string wechatDir, string targetPath)
/// <param name="wechatFullDir">
/// 微信完整路径包含版本号
/// * C:\Program Files\Tencent\WeChat\[3.9.12.11]
/// </param>
public static void CopyMmmojoDll(string wechatFullDir)
{
var targetPath = AppDomain.CurrentDomain.BaseDirectory;
var mmMojoFullPath = Path.Combine(wechatFullDir, MmMojoDll);
var mmMojo64FullPath = Path.Combine(wechatFullDir, MmMojo64Dll);
var targetMmMojoFullPath = Path.Combine(targetPath, MmMojoDll);
var targetMmMojo64FullPath = Path.Combine(targetPath, MmMojo64Dll);
if (!File.Exists(targetMmMojoFullPath))
File.Copy(mmMojoFullPath, targetMmMojoFullPath);
if (!File.Exists(targetMmMojo64FullPath))
File.Copy(mmMojo64FullPath, targetMmMojo64FullPath);
}

/// <summary>
/// 移除软件目录下的mmmojo.dll、mmmojo_64.dll
/// </summary>
/// <param name="error"></param>
public static bool RemoveMmmojoDll(out string error)
{
var mmmojoFullPath = Path.Combine(wechatDir, MmmojoDll);
var mmmojo64FullPath = Path.Combine(wechatDir, Mmmojo64Dll);
var targetMmmojoFullPath = Path.Combine(targetPath, MmmojoDll);
var targetMmmojo64FullPath = Path.Combine(targetPath, Mmmojo64Dll);
if (!File.Exists(targetMmmojoFullPath))
File.Copy(mmmojoFullPath, targetMmmojoFullPath);
if (!File.Exists(targetMmmojo64FullPath))
File.Copy(mmmojo64FullPath, targetMmmojo64FullPath);
var ret = true;
error = string.Empty;
var targetPath = AppDomain.CurrentDomain.BaseDirectory;
var targetMmMojoFullPath = Path.Combine(targetPath, MmMojoDll);
var targetMmMojo64FullPath = Path.Combine(targetPath, MmMojo64Dll);
try
{
if (File.Exists(targetMmMojoFullPath))
File.Delete(targetMmMojoFullPath);
if (File.Exists(targetMmMojo64FullPath))
File.Delete(targetMmMojo64FullPath);
}
catch (Exception ex)
{
error = ex.Message;
ret = false;
}

return ret;
}
}

0 comments on commit 2139d20

Please sign in to comment.