Skip to content

Commit

Permalink
VR patch improvements (#290)
Browse files Browse the repository at this point in the history
* Simplyfied finding char sequences
  • Loading branch information
TAImatem authored Aug 1, 2020
1 parent aba7ea5 commit 76c495c
Showing 1 changed file with 12 additions and 43 deletions.
55 changes: 12 additions & 43 deletions OWML.Patcher/VRFilePatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class VRFilePatcher
private const int RemovedBytes = 2;
// String that comes right before the bytes we want to patch.
private const string PatchZoneText = "Assets/Scenes/PostCreditScene.unity";
private const int PatchStartZoneOffset = 6;
private const int PatchStartZoneOffset = 5;
private const int BuildSettingsSector = 10;//count from zero

public VRFilePatcher(IModConsole writer, BinaryPatcher binaryPatcher)
Expand Down Expand Up @@ -45,60 +45,29 @@ public void Patch()

private int FindPatchStartOffset(byte[] sectorBytes)
{
var patchZoneBytes = Encoding.ASCII.GetBytes(PatchZoneText);
var patchZoneMatch = 0;
for (var i = 0; i < sectorBytes.Length; i++)
var sectorString = Encoding.ASCII.GetString(sectorBytes);
var position = sectorString.IndexOf(PatchZoneText);
if (position < 0)
{
var fileByte = sectorBytes[i];
var patchZoneByte = patchZoneBytes[patchZoneMatch];
if (fileByte == patchZoneByte)
{
patchZoneMatch++;
}
else
{
patchZoneMatch = 0;
}
if (patchZoneMatch == patchZoneBytes.Length)
{
return i + PatchStartZoneOffset;
}
throw new Exception("Could not find patch zone in globalgamemanagers. This probably means the VR patch needs to be updated.");
}
throw new Exception("Could not find patch zone in globalgamemanagers. This probably means the VR patch needs to be updated.");
return position + PatchZoneText.Length + PatchStartZoneOffset;
}

private bool FindExistingPatch(byte[] sectorBytes, int startIndex)
{
var existingPatchBytes = Encoding.ASCII.GetBytes(EnabledVRDevice);
var existingPatchMatch = 0;

for (var i = startIndex; i < sectorBytes.Length; i++)
{
var fileByte = sectorBytes[i];
var existingPatchByte = existingPatchBytes[existingPatchMatch];
if (fileByte == existingPatchByte)
{
existingPatchMatch++;
}
else
{
existingPatchMatch = 0;
}
if (existingPatchMatch == existingPatchBytes.Length)
{
return true;
}
}
return false;
var zoneString = Encoding.ASCII.GetString(sectorBytes.Skip(startIndex).ToArray());
return zoneString.IndexOf(EnabledVRDevice) >= 0;
}

private byte[] CreatePatchFileBytes(byte[] fileBytes, int patchStartIndex)
{
// First byte is the number of elements in the array.
var vrDevicesDeclarationBytes = new byte[] { 1, 0, 0, 0, (byte)EnabledVRDevice.Length, 0, 0, 0 };
var stringsCountBytes = BitConverter.GetBytes(1);
var stringLengthBytes = BitConverter.GetBytes(EnabledVRDevice.Length);
var vrDevicesSizeBytes = stringsCountBytes.Concat(stringLengthBytes).ToArray();

// Bytes that need to be inserted into the file.
var patchBytes = vrDevicesDeclarationBytes.Concat(Encoding.ASCII.GetBytes(EnabledVRDevice)).ToArray();
var patchBytes = vrDevicesSizeBytes.Concat(Encoding.ASCII.GetBytes(EnabledVRDevice)).ToArray();

return _binaryPatcher.PatchSectionBytes(fileBytes, patchBytes, patchStartIndex, RemovedBytes, BuildSettingsSector);
}
Expand Down

0 comments on commit 76c495c

Please sign in to comment.