Skip to content

Commit

Permalink
get lightmap logging working for halo 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
num0005 committed Jun 30, 2024
1 parent 81ecf91 commit b503f74
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 77 deletions.
102 changes: 57 additions & 45 deletions Launcher/ToolkitInterface/H2Toolkit.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using static ToolkitLauncher.ToolkitProfiles;
Expand Down Expand Up @@ -55,63 +56,73 @@ private static string GetLightmapQuality(LightmapArgs lightmapArgs)

public override async Task BuildLightmap(string scenario, string bsp, LightmapArgs args, ICancellableProgress<int>? progress)
{
string quality = GetLightmapQuality(args);

if (args.instanceCount > 1 && (Profile.IsMCC || Profile.CommunityTools)) // multi instance?
LogFolder = $"lightmaps_{Path.GetFileNameWithoutExtension(scenario)}";
try
{
if (progress is not null)
progress.MaxValue += 1 + args.instanceCount;
string quality = GetLightmapQuality(args);

async Task RunInstance(int index)
if (args.instanceCount > 1 && (Profile.IsMCC || Profile.CommunityTools)) // multi instance?
{
if (index == 0 && !Profile.IsH2Codez()) // not needed for H2Codez
if (progress is not null)
progress.MaxValue += 1 + args.instanceCount;

async Task RunInstance(int index)
{
if (index == 0 && !Profile.IsH2Codez()) // not needed for H2Codez
{
if (progress is not null)
progress.Status = "Delaying launch of zeroth instance";
await Task.Delay(1000 * 70, progress.GetCancellationToken());
}
Utility.Process.Result result = await RunLightmapWorker(
scenario,
bsp,
quality,
args.instanceCount,
index,
args.NoAssert,
progress.GetCancellationToken(),
args.outputSetting
);
if (result is not null && result.HasErrorOccured)
progress.Cancel($"Tool worker {index} has failed - exit code {result.ReturnCode}");
if (progress is not null)
progress.Status = "Delaying launch of zeroth instance";
await Task.Delay(1000 * 70, progress.GetCancellationToken());
progress.Report(1);
}

var instances = new List<Task>();
for (int i = args.instanceCount - 1; i >= 0; i--)
{
instances.Add(RunInstance(i));
}
Utility.Process.Result result = await RunLightmapWorker(
scenario,
bsp,
quality,
args.instanceCount,
index,
args.NoAssert,
progress.GetCancellationToken(),
args.outputSetting
);
if (result is not null && result.HasErrorOccured)
progress.Cancel($"Tool worker {index} has failed - exit code {result.ReturnCode}");
if (progress is not null)
progress.Status = $"Running {args.instanceCount} instances";
await Task.WhenAll(instances);
if (progress is not null)
progress.Status = "Merging output";

if (progress.IsCancelled)
return;

await RunMergeLightmap(scenario, bsp, args.instanceCount, args.NoAssert);
if (progress is not null)
progress.Report(1);
}

var instances = new List<Task>();
for (int i = args.instanceCount - 1; i >= 0; i--)
else
{
instances.Add(RunInstance(i));
Debug.Assert(args.instanceCount == 1); // should be one, otherwise we got bad args
if (progress is not null)
{
progress.DisableCancellation();
progress.MaxValue += 1;
}
await RunTool((args.NoAssert && Profile.IsMCC) ? ToolType.ToolFast : ToolType.Tool, new() { "lightmaps", scenario, bsp, quality });
if (progress is not null)
progress.Report(1);
}
if (progress is not null)
progress.Status = $"Running {args.instanceCount} instances";
await Task.WhenAll(instances);
if (progress is not null)
progress.Status = "Merging output";

await RunMergeLightmap(scenario, bsp, args.instanceCount, args.NoAssert);
if (progress is not null)
progress.Report(1);
}
else
} finally
{
Debug.Assert(args.instanceCount == 1); // should be one, otherwise we got bad args
if (progress is not null)
{
progress.DisableCancellation();
progress.MaxValue += 1;
}
await RunTool((args.NoAssert && Profile.IsMCC) ? ToolType.ToolFast : ToolType.Tool, new() { "lightmaps", scenario, bsp, quality });
if (progress is not null)
progress.Report(1);
LogFolder = null;
}
}

Expand Down Expand Up @@ -143,6 +154,7 @@ private async Task RunMergeLightmap(string scenario, string bsp, int workerCount

private async Task<Utility.Process.Result> RunLightmapWorker(string scenario, string bsp, string quality, int workerCount, int index, bool useFast, CancellationToken cancelationToken, OutputMode output)
{
this.LogFileSuffix = $"_{index}";
if (Profile.IsMCC)
{
bool wereWeExperts = Profile.ElevatedToExpert;
Expand Down
2 changes: 1 addition & 1 deletion Launcher/ToolkitInterface/HRToolkit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async Task<StageResult> RunStage(string stage)
/// <returns></returns>
public override async Task ImportModel(string path, ModelCompile importType, bool phantomFix, bool h2SelectionLogic, bool renderPRT, bool FPAnim, string characterFPPath, string weaponFPPath, bool accurateRender, bool verboseAnim, bool uncompressedAnim, bool skyRender, bool PDARender, bool resetCompression, bool autoFBX, bool genShaders)
{
List<string> args = new List<string>();
List<string> args = new();
if (importType.HasFlag(ModelCompile.render))
{
// Generate shaders if requested
Expand Down
81 changes: 73 additions & 8 deletions Launcher/ToolkitInterface/ToolkitBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,14 @@ protected virtual string ToLocalPath(string path)
/// <returns></returns>
public async Task RunCustomToolCommand(string command)
{
await Utility.Process.StartProcessWithShell(BaseDirectory, GetToolExecutable(ToolType.Tool), Utility.Process.EscapeArgList(GetArgsToPrepend()) + " " + command);
await Utility.Process.StartProcessWithShell(BaseDirectory, GetToolExecutable(ToolType.Tool), Utility.Process.EscapeArgList(GetArgsToPrepend(noWindow: false)) + " " + command);
}

/// <summary>
/// Get the args to prepend to every invokaing of a game tool
/// </summary>
/// <returns>List with all the args to add</returns>
private List<string> GetArgsToPrepend()
private List<string> GetArgsToPrepend(bool noWindow)
{
List<string> args = new();

Expand Down Expand Up @@ -383,7 +383,7 @@ private List<string> GetArgsToPrepend()
args.Add("-expert_mode");
}

if (Profile.Batch)
if (Profile.Batch || noWindow)
{
args.Add("-batch");
}
Expand Down Expand Up @@ -457,9 +457,64 @@ static protected OutputMode GetMoreSilentMode(OutputMode mode)
}
}

readonly private AsyncLocal<string?> _log_folder = new();
readonly private AsyncLocal<string?> _log_file_suffix = new();

public string? LogFolder
{
get
{
return _log_folder.Value;
}
set
{
_log_folder.Value = value;
}
}

public string? LogFileSuffix
{
get
{
return _log_file_suffix.Value;
}
set
{
_log_file_suffix.Value = value;
}
}


public Action<Utility.Process.Result>? ToolFailure { get; set; }

private string GetLogFileName(List<string>? args)
{
string report_folder = Path.Combine(BaseDirectory, "reports", "Osoyoos");
string? log_subfolder = LogFolder;
string? log_suffix = LogFileSuffix;

if (log_subfolder != null)
report_folder = Path.Combine(report_folder, log_subfolder);

string filename;

if (args is not null && args.Count > 0)
{
filename = "tool_" + args[0];
}
else
{
filename = "tool";
}

if (log_suffix is not null)
{
filename += log_suffix;
}

return Path.Combine(report_folder, filename) + ".log";
}

/// <summary>
/// Run a tool from the toolkit with arguments
/// </summary>
Expand All @@ -471,7 +526,7 @@ static protected OutputMode GetMoreSilentMode(OutputMode mode)
/// <returns>Results of running the tool if possible</returns>
public async Task<Utility.Process.Result?> RunTool(ToolType tool, List<string>? args = null, OutputMode? outputMode = null, bool lowPriority = false, CancellationToken cancellationToken = default)
{
Utility.Process.Result? result = await RunToolInternal(tool, args, outputMode, cancellationToken, lowPriority);
Utility.Process.Result? result = await RunToolInternal(tool, args, outputMode, lowPriority, cancellationToken);
if (result is not null && result.ReturnCode != 0 && ToolFailure is not null)
ToolFailure(result);
return result;
Expand All @@ -480,21 +535,31 @@ static protected OutputMode GetMoreSilentMode(OutputMode mode)
/// <summary>
/// Implementation of <c>RunTool</c>
/// </summary>
private async Task<Utility.Process.Result?> RunToolInternal(ToolType tool, List<string>? args, OutputMode? outputMode, CancellationToken cancellationToken, bool lowPriority)
private async Task<Utility.Process.Result?> RunToolInternal(ToolType tool, List<string>? args, OutputMode? outputMode, bool lowPriority, CancellationToken cancellationToken)
{
bool has_window = outputMode != OutputMode.slient && outputMode != OutputMode.logToDisk;
bool enabled_log = outputMode == OutputMode.logToDisk;

// always include the prepend args
List<string> full_args = GetArgsToPrepend();
List<string> full_args = GetArgsToPrepend(has_window);
if (args is not null)
full_args.AddRange(args);

string tool_path = GetToolExecutable(tool);
if (outputMode is null)
outputMode = GetDefaultOutputMode(tool, args);

string? log_path = null;

if (enabled_log)
{
log_path = GetLogFileName(args);
}

if (outputMode == OutputMode.keepOpen)
return await Utility.Process.StartProcessWithShell(BaseDirectory, tool_path, full_args, cancellationToken, lowPriority);
return await Utility.Process.StartProcessWithShell(BaseDirectory, tool_path, full_args, lowPriority, cancellationToken);
else
return await Utility.Process.StartProcess(BaseDirectory, tool_path, full_args, cancellationToken, lowPriority);
return await Utility.Process.StartProcess(BaseDirectory, executable: tool_path, args: full_args, lowPriority: lowPriority, logFileName: log_path, noWindow: !has_window, cancellationToken: cancellationToken);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Launcher/Utility/PRTInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private static async Task<bool> DoUpdate(string prt_install_path, GitHubReleases
await File.WriteAllBytesAsync(redist_executable_path, redist_package, progress.GetCancellationToken());
progress.Status = "Installing redist package!";

await Process.StartProcess(temp_folder, redist_executable_path, new(), progress.GetCancellationToken(), admin:true);
await Process.StartProcess(temp_folder, redist_executable_path, new(), admin: true, cancellationToken: progress.GetCancellationToken());

progress.Complete = true;
progress.Status = IsRedistInstalled() ? "Installed redist package!" : "Failed to install redist package!";
Expand Down
Loading

0 comments on commit b503f74

Please sign in to comment.