Skip to content

Table Builders

BlazingTwist edited this page Jan 17, 2023 · 1 revision

TextTableBuilder

Use this to create tables for logging.

Example:

TextTableBuilder tableBuilder = new TextTableBuilder();
tableBuilder.Row("Column1", "Column2", "Column3");
tableBuilder.ThinRowSeparator();
tableBuilder.Row("A", "B", "C");
tableBuilder.Row("1", "2", "3");
tableBuilder.EndTable();
Console.WriteLine(tableBuilder.BuildTable("  "));

Results in

  | Column1  | Column2  | Column3  |
  |----------+----------+----------|
  | A        | B        | C        |
  | 1        | 2        | 3        |
  \================================/

InstructionTableBuilder

Useful for logging instructions.

Example:

private static class TargetClass {

	private static void TargetMethod() {
		Console.WriteLine("msg0");
		if (Condition1()) {
			Console.WriteLine("msg1");
		} else {
			Console.WriteLine("msg2");
		}
		Console.WriteLine("msg3");
	}

}

public static class PatcherClass {

	[HarmonyTranspiler, HarmonyPatch(declaringType: typeof(TargetClass), methodName: "TargetMethod")]
	private static IEnumerable<CodeInstruction> ExampleTranspiler(IEnumerable<CodeInstruction> codeInstructions) {
		IEnumerable<CodeInstruction> instructions = codeInstructions.ToList();
		Console.WriteLine(InstructionTableBuilder.BuildTable(instructions));
		return instructions;
	}

}

Results in

  | Labels  | OpCode   | Operand                        |
  |---------+----------+--------------------------------|
  |         | ldstr    | msg0                           |
  |         | call     | Void WriteLine(System.String)  |
  |         | call     | Boolean Condition1()           |
  |         | brfalse  | 1                              |
  |         | ldstr    | msg1                           |
  |         | call     | Void WriteLine(System.String)  |
  |         | br       | 2                              |
  | 1       | ldstr    | msg2                           |
  |         | call     | Void WriteLine(System.String)  |
  | 2       | ldstr    | msg3                           |
  |         | call     | Void WriteLine(System.String)  |
  |         | ret      | null                           |
  \=====================================================/

Clone this wiki locally