Skip to content

Commit

Permalink
add metadata to extract
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaPiggy committed Sep 14, 2024
1 parent 839ae86 commit 1ca59ad
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions Winch/Util/DialogueUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,11 @@ internal static void Inject()
var newProgram = new Program();

Program oldProgram = Traverse.Create(runner.Dialogue).Field("program").GetValue<Program>();
var _lineMetadata = Traverse.Create(runner.yarnProject.lineMetadata).Field("_lineMetadata").GetValue<SerializedDictionary<string, string>>();

if (exportYarnProgram)
{
WriteYarnProgramToText("YarnProgramVanilla", oldProgram);
WriteYarnProgramToText("YarnProgramVanilla", oldProgram, _lineMetadata);
}

foreach (var nodeName in oldProgram.Nodes)
Expand All @@ -295,11 +296,11 @@ internal static void Inject()

runner.Dialogue.SetProgram(newProgram);

var _lineMetadata = Traverse.Create(runner.yarnProject.lineMetadata).Field("_lineMetadata").GetValue<SerializedDictionary<string, string>>();

foreach (var metadataEntry in metadata)
{
_lineMetadata.Add(metadataEntry.Key, string.Join(" ", metadataEntry.Value));
var metadataValue = string.Join(" ", metadataEntry.Value);
if (!string.IsNullOrWhiteSpace(metadataValue))
_lineMetadata.Add(metadataEntry.Key, metadataValue);
}

foreach (var instruction in instructions)
Expand All @@ -309,7 +310,7 @@ internal static void Inject()

if (exportYarnProgram)
{
WriteYarnProgramToText("YarnProgramModded", newProgram);
WriteYarnProgramToText("YarnProgramModded", newProgram, _lineMetadata);
}
}
catch (Exception ex)
Expand Down Expand Up @@ -368,32 +369,33 @@ public DredgeInstruction(string nodeID, int index, string label, Yarn.Instructio

internal static string GetProgramFileLocation(string fileName) => Path.Combine(WinchCore.WinchInstallLocation, $"{fileName}.txt");

internal static void WriteYarnProgramToText(string fileName, Program program)
internal static void WriteYarnProgramToText(string fileName, Program program, SerializedDictionary<string, string> lineMetadata)
{
var path = GetProgramFileLocation(fileName);
File.WriteAllText(path, YarnProgramToText(program));
File.WriteAllText(path, YarnProgramToText(program, lineMetadata));
WinchCore.Log.Debug("Yarn program written to " + path);
}

internal static string YarnProgramToText(Program program)
internal static string YarnProgramToText(Program program, SerializedDictionary<string, string> lineMetadata)
{
var stringified = $"Program {program.Name}:";
foreach (var nodePair in program.Nodes)
{
var node = nodePair.Value;
stringified += $"\n\n Node {nodePair.Key} {node.SourceTextStringID}:\n\n{InstructionsToText(node)}";
stringified += $"\n\n Node {nodePair.Key} {node.SourceTextStringID}:\n\n{InstructionsToText(node, lineMetadata)}";
}
return stringified;
}

internal static string InstructionsToText(this Yarn.Node node)
internal static string InstructionsToText(this Yarn.Node node, SerializedDictionary<string, string> lineMetadata)
{
return string.Join("\n", node.Instructions.Select((instruction, i) =>
{
var operands = string.Join(" ", instruction.Operands.Select(operand => OperandToText(operand)));
var labels = node.Labels.Where(label => label.Value == i).Select(label => label.Key);
var stringifiedLabels = labels.Count() > 0 ? $" [{string.Join(", ", labels)}]" : string.Empty;
return $" {i} {instruction.Opcode} {operands}{stringifiedLabels}";
var metadata = instruction.Opcode == Instruction.Types.OpCode.RunLine ? (" (" + lineMetadata.GetValueOrDefault(instruction.Operands.FirstOrDefault().StringValue) + ")") : string.Empty;
return $" {i} {instruction.Opcode} {operands}{stringifiedLabels}{metadata}";
}));
}

Expand All @@ -417,7 +419,8 @@ internal static void WriteYarnProgramCommand(CommandArg[] args)
{
DredgeDialogueRunner runner = GameManager.Instance.DialogueRunner;
Program program = Traverse.Create(runner.Dialogue).Field("program").GetValue<Program>();
WriteYarnProgramToText("YarnProgram", program);
var _lineMetadata = Traverse.Create(runner.yarnProject.lineMetadata).Field("_lineMetadata").GetValue<SerializedDictionary<string, string>>();
WriteYarnProgramToText("YarnProgram", program, _lineMetadata);
Terminal.Buffer.HandleLog("Yarn program written to " + GetProgramFileLocation("YarnProgram"), TerminalLogType.Message);
}
}

0 comments on commit 1ca59ad

Please sign in to comment.