Files
FishmanTheMurloc 0c02808626 使用TorchSharp重写RodNet,以利后续优化 (#1613)
* 使用TorchSharp重写RodNet,以利后续优化

* 增加一个外部torch加载配置来代替直接的依赖,如配置不生效则使用原先手搓的算法

* BgiOnnxFactory取消单例,改为在App服务类中注册为单例,由此修复了一堆单元测试

* BgiOnnxFactory中几个静态方法改为成员方法以和App解耦;因不再有多个mat源供消耗,FishBite中文字块算法不再改动传入的mat,使得后续串联的算法不受其影响

* 将BehavioursTests中临时的配置读取方式改为读取主项目编译环境中的json文件;新建单元测试的README

* 将RodNet算法更新到 010006a44c 的版本;RodNet中关于torch库推理和直接数学计算的校验移至单元测试

* 更新RodNet算法至最新:add5672731

* 注释调试用的代码
2025-06-01 15:16:54 +08:00

51 lines
1.7 KiB
C#

using BetterGenshinImpact.GameTask.AutoFishing;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using TorchSharp;
namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
{
public class TorchFixture
{
private readonly Lazy<TorchLoader> torch = new Lazy<TorchLoader>();
public bool UseTorch
{
get
{
return torch.Value.UseTorch;
}
}
}
internal class TorchLoader
{
public TorchLoader()
{
// 需要读取主项目编译目录中的配置
string configFullPath = Path.Combine(Path.GetFullPath(@"..\..\..\..\..\"), @"BetterGenshinImpact\bin\x64\Debug\net8.0-windows10.0.22621.0\User\config.json");
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddJsonFile(configFullPath, optional: false).Build();
AutoFishingConfig autoFishingConfig = configurationRoot.GetRequiredSection("autoFishingConfig").Get<AutoFishingConfig>() ?? throw new ArgumentNullException();
try
{
NativeLibrary.Load(autoFishingConfig.TorchDllFullPath);
if (torch.TryInitializeDeviceType(DeviceType.CUDA))
{
torch.set_default_device(new torch.Device(DeviceType.CUDA));
}
UseTorch = true;
}
catch (Exception e) when (e is DllNotFoundException || e is NotSupportedException)
{
UseTorch = false;
}
}
public bool UseTorch { get; private set; }
}
}