Skip to content

Commit

Permalink
Add AreaData script patching support
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed Mar 19, 2021
1 parent 9ddedf6 commit 9da11e7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
16 changes: 16 additions & 0 deletions OpenKh.Patcher/PatcherProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ private static void PatchFile(Context context, AssetFile assetFile, Stream strea
case "kh2msg":
PatchKh2Msg(context, assetFile.Source, stream);
break;
case "areadatascript":
PatchAreaDataScript(context, assetFile.Source, stream);
break;
}

stream.SetLength(stream.Position);
Expand Down Expand Up @@ -257,5 +260,18 @@ private static void PatchKh2Msg(Context context, List<AssetFile> sources, Stream

Msg.WriteOptimized(stream.SetPosition(0), msgs);
}

private static void PatchAreaDataScript(Context context, List<AssetFile> sources, Stream stream)
{
var scripts = Kh2.Ard.AreaDataScript.Read(stream).ToDictionary(x => x.ProgramId, x => x);
foreach (var source in sources)
{
var programsInput = File.ReadAllText(context.GetSourceModAssetPath(source.Name));
foreach (var newScript in Kh2.Ard.AreaDataScript.Compile(programsInput))
scripts[newScript.ProgramId] = newScript;
}

Kh2.Ard.AreaDataScript.Write(stream.SetPosition(0), scripts.Values);
}
}
}
50 changes: 50 additions & 0 deletions OpenKh.Tests/Patcher/PatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,56 @@ public void MergeKh2MsgTest()
});
}

[Fact]
public void MergeKh2AreaDataScriptTest()
{
var patcher = new PatcherProcessor();
var patch = new Metadata
{
Assets = new List<AssetFile>
{
new AssetFile
{
Name = "map.script",
Method = "areadatascript",
Source = new List<AssetFile>
{
new AssetFile
{
Name = "map.txt",
}
}
},
}
};

File.Create(Path.Combine(AssetsInputDir, "map.script")).Using(stream =>
{
var compiledProgram = Kh2.Ard.AreaDataScript.Compile("Program 1\nSpawn \"1111\"");
Kh2.Ard.AreaDataScript.Write(stream, compiledProgram);
});
File.Create(Path.Combine(ModInputDir, "map.txt")).Using(stream =>
{
var writer = new StreamWriter(stream);
writer.WriteLine("Program 2");
writer.WriteLine("Spawn \"2222\"");
writer.Flush();
});

patcher.Patch(AssetsInputDir, ModOutputDir, patch, ModInputDir);

AssertFileExists(ModOutputDir, "map.script");
File.OpenRead(Path.Combine(ModOutputDir, "map.script")).Using(stream =>
{
var scripts = Kh2.Ard.AreaDataScript.Read(stream);
var decompiled = Kh2.Ard.AreaDataScript.Decompile(scripts);
decompiled.Contains("Program 1");
decompiled.Contains("Spawn \"1111\"");
decompiled.Contains("Program 2");
decompiled.Contains("Spawn \"2222\"");
});
}

[Fact]
public void ProcessMultipleTest()
{
Expand Down

0 comments on commit 9da11e7

Please sign in to comment.