Files
better-genshin-impact/BetterGenshinImpact/GameTask/AutoFishing/AutoFishingTaskParam.cs
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

95 lines
4.1 KiB
C#

using BetterGenshinImpact.GameTask.Model;
using System;
using System.Collections.Generic;
using System.Text;
using BetterGenshinImpact.Helpers;
using Microsoft.ClearScript;
using System.Globalization;
using System.Runtime.InteropServices;
using TorchSharp;
namespace BetterGenshinImpact.GameTask.AutoFishing
{
public class AutoFishingTaskParam : BaseTaskParam
{
public AutoFishingTaskParam(int wholeProcessTimeoutSeconds, int throwRodTimeOutTimeoutSeconds, FishingTimePolicy fishingTimePolicy, bool saveScreenshotOnKeyTick, CultureInfo? cultureInfo, bool useTorch) : base(cultureInfo)
{
WholeProcessTimeoutSeconds = wholeProcessTimeoutSeconds;
ThrowRodTimeOutTimeoutSeconds = throwRodTimeOutTimeoutSeconds;
FishingTimePolicy = fishingTimePolicy;
SaveScreenshotOnKeyTick = saveScreenshotOnKeyTick;
UseTorch = useTorch;
}
public int WholeProcessTimeoutSeconds { get; set; }
public int ThrowRodTimeOutTimeoutSeconds { get; set; }
public FishingTimePolicy FishingTimePolicy { get; set; }
public bool SaveScreenshotOnKeyTick { get; set; }
public bool UseTorch { get; set; }
/// <summary>
/// 从JS请求参数构建任务参数
/// </summary>
/// <param name="config"></param>
/// <returns></returns>
public static AutoFishingTaskParam BuildFromSoloTaskConfig(object? config)
{
if (config == null)
{
return BuildFromConfig(TaskContext.Instance().Config.AutoFishingConfig);
}
var autoFishingConfig = TaskContext.Instance().Config.AutoFishingConfig;
var jsObject = (ScriptObject)config;
var wholeProcessTimeoutSeconds = ScriptObjectConverter.GetValue(jsObject, "wholeProcessTimeoutSeconds", autoFishingConfig.WholeProcessTimeoutSeconds);
var throwRodTimeOutTimeoutSeconds = ScriptObjectConverter.GetValue(jsObject, "throwRodTimeOutTimeoutSeconds", autoFishingConfig.AutoThrowRodTimeOut);
var fishingTimePolicy = (FishingTimePolicy)ScriptObjectConverter.GetValue(jsObject, "fishingTimePolicy", (int)autoFishingConfig.FishingTimePolicy);
var saveScreenshotOnKeyTick = ScriptObjectConverter.GetValue(jsObject, "saveScreenshotOnKeyTick", false);
bool useTorch;
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;
}
return new AutoFishingTaskParam(wholeProcessTimeoutSeconds, throwRodTimeOutTimeoutSeconds, fishingTimePolicy, saveScreenshotOnKeyTick, null, useTorch);
}
/// <summary>
/// 从配置文件构建任务参数
/// </summary>
/// <param name="config"></param>
/// <param name="saveScreenshotOnKeyTick"></param>
/// <returns></returns>
public static AutoFishingTaskParam BuildFromConfig(AutoFishingConfig config, bool saveScreenshotOnKeyTick = false)
{
CultureInfo cultureInfo = new CultureInfo(TaskContext.Instance().Config.OtherConfig.GameCultureInfoName);
bool useTorch;
try
{
NativeLibrary.Load(config.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;
}
return new AutoFishingTaskParam(config.WholeProcessTimeoutSeconds, config.AutoThrowRodTimeOut, config.FishingTimePolicy, saveScreenshotOnKeyTick, cultureInfo, useTorch);
}
}
}