JD2022-TU1/main/tools/apps/Packager/Tasks/RunOnIscTask.cs

98 lines
2.9 KiB
C#

using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading;
using JD.Core.ProjectManaging;
using Packager.Parameters;
using RunOnIsc;
using LogCategory = JD.LogService.LogCategory;
namespace Packager.Tasks
{
[DisplayName("Run On Isc")]
[Category("Extern Tools")]
public class RunOnIscTask: Task, IDisposable
{
[Input("Map List", IOCardinality.Multi, IOType.File)]
public ReferenceList MapList { get; set; }
[Output("Loading Logs", IOType.File)]
public ReferenceList LoadingLogs = new ReferenceList();
private const string OutputFormat = "{0}.log";
private readonly AutoResetEvent _signalContinueExecution = new AutoResetEvent(false);
private Worker _worker = null;
public RunOnIscTask()
{
MapList = new ReferenceList();
}
private RunOnIscParams InitParams(string mapFile)
{
var parameters = new RunOnIscParams
{
Root = DataPath.Value,
MapMinLaunchTime = 60,
AlwaysReportOpenFile = true,
MapFile = mapFile,
OutputDirectory = TaskOutputFolder,
OutputFormat = OutputFormat,
UseMapRun = false,
ExecutableFile = EnginePath.FindExecutable("UA_Engine.exe"),
Arguments = "nologos=1;nocrashreporter=1;filelogging=1;hiddenatboot=1;editor=1;silent=1;nosound=1"
};
return parameters;
}
private void WorkerLog(string message, LogCategory messageCategory)
{
LogMessage(message, messageCategory);
}
private void WorkerCompleted(int numMapsSuccess, int numMapsFailed)
{
_signalContinueExecution.Set();
if (numMapsFailed > 0) LogMessage(true, $"{numMapsFailed} failed maps");
_worker.Dispose();
_worker = null;
}
protected override void StartInternal(BuildConfiguration profile, cookerWrapper cooker)
{
var parameters = InitParams(CreateProxyFile(MapList.ToArray()));
_worker = new Worker(parameters);
_worker.OnLog += WorkerLog;
_worker.OnCompleted += WorkerCompleted;
var errorCode = _worker.Start();
_signalContinueExecution.WaitOne();
if(errorCode != 0)
{
LogMessage(true, "Failed to start the Worker for RunOnIscParams");
return;
}
foreach(var file in MapList)
{
var filename = string.Format(OutputFormat, Path.GetFileNameWithoutExtension(file));
LoadingLogs.Add(Path.Combine(TaskOutputFolder, filename));
}
}
public void Dispose()
{
_signalContinueExecution?.Dispose();
_worker?.Dispose();
}
}
}