Skip to content

Commit

Permalink
fix: 修复在没有pipeline文件夹时无法创建文件的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SweetSmellFox committed Dec 24, 2024
1 parent 7147ad6 commit 9f791a7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
4 changes: 0 additions & 4 deletions MFAWPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,5 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
<None Remove="NLog.config" />
<Resource Include="Resource\base\model\ocr\det.onnx" />
<Resource Include="Resource\base\model\ocr\keys.txt" />
<Resource Include="Resource\base\model\ocr\README.md" />
<Resource Include="Resource\base\model\ocr\rec.onnx" />
</ItemGroup>
</Project>
67 changes: 36 additions & 31 deletions Utils/MaaProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,10 @@ public static string HandleStringsWithVariables(string content)
};
RegisterCustomRecognitionsAndActions(tasker);
if (!DataSet.GetData("EnableGPU", true))
{
tasker.Resource.SetOptionInferenceDevice(InferenceDevice.CPU);
LoggerService.LogInfo("已禁用GPU加速!");
}
tasker.Utility.SetOptionSaveDraw(DataSet.GetData("EnableSaveDraw", false));
return tasker;
}
Expand Down Expand Up @@ -868,53 +871,55 @@ static List<MetadataReference> GetMetadataReferences()
public static IEnumerable<CustomValue<object>> LoadAndInstantiateCustomClasses(string directory, string[] interfacesToImplement)
{
var customClasses = new List<CustomValue<object>>();
var csFiles = Directory.GetFiles(directory, "*.cs");

var references = GetMetadataReferences();

foreach (var filePath in csFiles)
if (Path.Exists(directory))
{
var name = Path.GetFileNameWithoutExtension(filePath);
LoggerService.LogInfo("Trying to parse " + name);
string code = File.ReadAllText(filePath);
var csFiles = Directory.GetFiles(directory, "*.cs");

var syntaxTree = CSharpSyntaxTree.ParseText(code);
var compilation = CSharpCompilation.Create("DynamicAssembly")
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddSyntaxTrees(syntaxTree)
.AddReferences(references);
var references = GetMetadataReferences();

using (var ms = new MemoryStream())
foreach (var filePath in csFiles)
{
EmitResult result = compilation.Emit(ms);
if (!result.Success)
var name = Path.GetFileNameWithoutExtension(filePath);
LoggerService.LogInfo("Trying to parse " + name);
string code = File.ReadAllText(filePath);

var syntaxTree = CSharpSyntaxTree.ParseText(code);
var compilation = CSharpCompilation.Create("DynamicAssembly")
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddSyntaxTrees(syntaxTree)
.AddReferences(references);

using (var ms = new MemoryStream())
{
var failures = result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error);
foreach (var diagnostic in failures)
EmitResult result = compilation.Emit(ms);
if (!result.Success)
{
LoggerService.LogError($"{diagnostic.Id}: {diagnostic.GetMessage()}");
var failures = result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error);
foreach (var diagnostic in failures)
{
LoggerService.LogError($"{diagnostic.Id}: {diagnostic.GetMessage()}");
}
continue;
}
continue;
}

ms.Seek(0, SeekOrigin.Begin);
var assembly = Assembly.Load(ms.ToArray());
ms.Seek(0, SeekOrigin.Begin);
var assembly = Assembly.Load(ms.ToArray());

foreach (var type in assembly.GetTypes())
{
foreach (var iface in interfacesToImplement)
foreach (var type in assembly.GetTypes())
{
if (type.GetInterfaces().Any(i => i.Name == iface))
foreach (var iface in interfacesToImplement)
{
var instance = Activator.CreateInstance(type);
if (instance != null)
customClasses.Add(new CustomValue<object>(name, instance));
if (type.GetInterfaces().Any(i => i.Name == iface))
{
var instance = Activator.CreateInstance(type);
if (instance != null)
customClasses.Add(new CustomValue<object>(name, instance));
}
}
}
}
}
}

return customClasses;
}

Expand Down
14 changes: 8 additions & 6 deletions Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ private bool LoadTask()
{
foreach (var resourcePath in MaaProcessor.CurrentResources)
{
if (!Path.Exists($"{resourcePath}/pipeline/"))
break;
var jsonFiles = Directory.GetFiles($"{resourcePath}/pipeline/", "*.json");
var taskDictionaryA = new Dictionary<string, TaskModel>();
foreach (var file in jsonFiles)
Expand All @@ -264,12 +266,12 @@ private bool LoadTask()

if (taskDictionary.Count == 0)
{
string? directoryPath = Path.GetDirectoryName($"{MaaProcessor.ResourceBase}/pipeline");
if (!string.IsNullOrWhiteSpace(directoryPath) && !Directory.Exists(directoryPath))

if (!string.IsNullOrWhiteSpace($"{MaaProcessor.ResourceBase}/pipeline") && !Directory.Exists($"{MaaProcessor.ResourceBase}/pipeline"))
{
try
{
Directory.CreateDirectory(directoryPath);
Directory.CreateDirectory($"{MaaProcessor.ResourceBase}/pipeline");
}
catch (Exception ex)
{
Expand Down Expand Up @@ -517,7 +519,7 @@ public async void AutoDetectDevice()
if (!string.IsNullOrWhiteSpace(emulatorConfig))
{
var extractedNumber = ExtractNumberFromEmulatorConfig(emulatorConfig);

foreach (var device in devices)
{
if (TryGetIndexFromConfig(device.Config, out int index))
Expand Down Expand Up @@ -607,7 +609,7 @@ public async void AutoDetectDevice()
if (win32Controller?.Win32 != null)
{
var filteredWindows = windows.Where(win => !string.IsNullOrWhiteSpace(win.Name) || !string.IsNullOrWhiteSpace(win.ClassName)).ToList();

if (!string.IsNullOrWhiteSpace(win32Controller.Win32.WindowRegex))
{
var windowRegex = new Regex(win32Controller.Win32.WindowRegex);
Expand All @@ -625,7 +627,7 @@ public async void AutoDetectDevice()
? windows.FindIndex(win => win.ClassName.Equals(filteredWindowsByClass.First().ClassName))
: 0;
}

if (win32Controller.Win32.Input != null)
{
int result = 0;
Expand Down

0 comments on commit 9f791a7

Please sign in to comment.