Skip to content

Debugging BWAPI CLI (C# Wrapper)

MarcoMeter edited this page May 12, 2016 · 1 revision

Debugging the AiModule, based on the BWAPI-CLI, is quite a challenge. These three ways worked for me so far:

BWAPI Draw Functions

The Game class of BWAPI features multiple functions to render information. Text, simple shapes and lines can be used to debug the game state.

Like this line of code renders a line indicating a unit's target:

Game.DrawLineMap(unit.Position.X, unit.Position.Y, target.Position.X, target.Position.Y, Color.Green);

Logging

Since there is no console involved in the process of executing the AiModule, writing logs to a file is one alternative. I achieved this by using the System.IO classes for writing log files for each match and session:

/// <summary>
/// Write line to logFile.log
/// </summary>
/// <param name="line">Content</param>
public static void WriteLine(string line)
{
    string fileName = @"\player" + Game.Self.Id.ToString() + "_match" + TrainingModule.GetMatchNumber() + ".log";

    // Create a logs folder
    if (!Directory.Exists(@"bwapi-data\AI\cs\logs"))
    {
        Directory.CreateDirectory(@"bwapi-data\AI\cs\logs");
    }

    // Create a new subfolder for the session
    if (!isSubDirLogsInit)
    {
        subDirLogs = basePathLogs + System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff") + "_player" + Game.Self.Id.ToString();
        Directory.CreateDirectory(subDirLogs);
        isSubDirLogsInit = true;
    }

    // Create and append to the log file
    using (StreamWriter file = new StreamWriter(subDirLogs + fileName, true))
    {
        file.WriteLine(System.DateTime.Now.TimeOfDay.ToString("") + "   " + line);
    }
}

Attaching the Visual Studio Debugger

While StarCraft executes the injected code, exceptions (e.g. NullRefs, OutOfBounds) are posted like chat messages. Information about class, function, line, are not printed at all. In order to find the location of mayhem, I recommend to attach Visual Studio's debugger. The dll and its pdb file have to be in the same location for the debugger to work. After StarCraft was launched, using the ChaosLauncher, you can use the "Attach to process..." to attach the debugger to StarCraft.exe . This works for the released binary as well. In the case of building the project in Debug mode, you can condifure the bwapi.ini to wait for a debugger to be attached.

pause_dbg = ON

Since I'm running two local instances of StarCraft, I can just debug one instance at a time. The pause in the menu, which is for waiting for the debugger to be attached, can't be used as well. One instance still gonna wait for the debugger.