Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vr-fix #76

Merged
merged 5 commits into from
Feb 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion OWML.Launcher/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace OWML.Launcher
{
public class App
{
private const string Version = "0.3.37";
private const string Version = "0.3.38";

private readonly IOwmlConfig _owmlConfig;
private readonly IModConsole _writer;
Expand Down
98 changes: 67 additions & 31 deletions OWML.Patcher/VrPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using BsDiff;
Expand All @@ -13,6 +14,13 @@ public class VRPatcher
private readonly IModConsole _writer;
private readonly SHA256 _sha;

private static readonly string[] PluginFilenames = { "openvr_api.dll", "OVRPlugin.dll" };
private static readonly string[] PatchChecksums =
{
"cacc71fcb141d936f1b59e57bf10dc52e8edb3481988379f7d95ecb65c4d3c90",
"d3979abb3b0d2468c3e03e2ee862d5297f5885bd9fc8f3b16cb16805e010d097"
};

public VRPatcher(IOwmlConfig owmlConfig, IModConsole writer)
{
_owmlConfig = owmlConfig;
Expand All @@ -22,74 +30,80 @@ public VRPatcher(IOwmlConfig owmlConfig, IModConsole writer)

public void PatchVR(bool enableVR)
{
CopyFiles();
PatchGlobalManager(enableVR);
}

private void CopyFiles()
{
var filenames = new[] { "openvr_api.dll", "OVRPlugin.dll" };
foreach (var filename in filenames)
if (enableVR)
{
var from = $"{_owmlConfig.OWMLPath}VR/{filename}";
var to = $"{_owmlConfig.PluginsPath}/{filename}";
if (File.Exists(from))
{
File.Copy(from, to, true);
}
else
{
_writer.WriteLine("Error: file not found: " + from);
}
AddPluginFiles();
}
else
{
RemovePluginFiles();
}
}

private void PatchGlobalManager(bool enableVR)
{
var originalPath = _owmlConfig.DataPath + "/globalgamemanagers";
if (!File.Exists(originalPath))
var currentPath = _owmlConfig.DataPath + "/globalgamemanagers";
if (!File.Exists(currentPath))
{
_writer.WriteLine("Error: can't find " + originalPath);
_writer.WriteLine("Error: can't find " + currentPath);
return;
}

var checksum = CalculateChecksum(originalPath);
var backupPath = $"{_owmlConfig.DataPath}/globalgamemanagers.{checksum}.bak";
var vrPath = $"{_owmlConfig.DataPath}/globalgamemanagers.{checksum}.vr";

var currentChecksum = CalculateChecksum(currentPath);
_writer.WriteLine("Current checksum: " + currentChecksum);

var backupPath = $"{_owmlConfig.DataPath}/globalgamemanagers.{currentChecksum}.bak";
if (!File.Exists(backupPath))
{
_writer.WriteLine("Taking backup of globalgamemanagers.");
File.Copy(originalPath, backupPath, true);
File.Copy(currentPath, backupPath, true);
}

var vrPath = $"{_owmlConfig.DataPath}/globalgamemanagers.{currentChecksum}.vr";
if (enableVR && !File.Exists(vrPath))
{
_writer.WriteLine("Patching globalgamemanagers for VR...");
var patchPath = $"{_owmlConfig.OWMLPath}VR/{checksum}";
if (File.Exists(patchPath))
if (PatchChecksums.Contains(currentChecksum))
{
ApplyPatch(originalPath, vrPath, patchPath);
var patchPath = $"{_owmlConfig.OWMLPath}VR/{currentChecksum}";
ApplyPatch(currentPath, vrPath, patchPath);
}
else
{
_writer.WriteLine($"Error: invalid checksum: {checksum}. VR patch for this version of Outer Wilds is not yet supported by OWML.");
return;
var patchedChecksum = PatchChecksums.FirstOrDefault(checksum =>
CalculateChecksum($"{_owmlConfig.DataPath}/globalgamemanagers.{checksum}.vr") == currentChecksum);
if (!string.IsNullOrEmpty(patchedChecksum))
{
_writer.WriteLine("Already patched! Original checksum: " + patchedChecksum);
vrPath = $"{_owmlConfig.DataPath}/globalgamemanagers.{patchedChecksum}.vr";
}
else
{
_writer.WriteLine($"Error: invalid checksum: {currentChecksum}. " +
"VR patch for this version of Outer Wilds is not yet supported by OWML.");
return;
}
}
}

var copyFrom = enableVR ? vrPath : backupPath;
File.Copy(copyFrom, originalPath, true);
File.Copy(copyFrom, currentPath, true);
}

private string CalculateChecksum(string filePath)
{
if (!File.Exists(filePath))
{
return null;
}
var bytes = File.ReadAllBytes(filePath);
var hash = _sha.ComputeHash(bytes);
var sb = new StringBuilder();
foreach (var b in hash)
{
sb.Append(b.ToString("x2").ToLower());
}
return sb.ToString();
}

Expand All @@ -109,5 +123,27 @@ private void ApplyPatch(string oldFile, string newFile, string patchFile)
}
}

private void AddPluginFiles()
{
foreach (var filename in PluginFilenames)
{
var from = $"{_owmlConfig.OWMLPath}VR/{filename}";
var to = $"{_owmlConfig.PluginsPath}/{filename}";
File.Copy(from, to, true);
}
}

private void RemovePluginFiles()
{
foreach (var filename in PluginFilenames)
{
var path = $"{_owmlConfig.PluginsPath}/{filename}";
if (File.Exists(path))
{
File.Delete(path);
}
}
}

}
}
2 changes: 1 addition & 1 deletion OWML.SampleMods/OWML.EnableDebugMode/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"name": "EnableDebugMode",
"uniqueName": "Alek.EnableDebugMode",
"version": "0.2",
"owmlVersion": "0.3.37"
"owmlVersion": "0.3.38"
}
2 changes: 1 addition & 1 deletion OWML.SampleMods/OWML.LoadCustomAssets/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"name": "LoadCustomAssets",
"uniqueName": "Alek.LoadCustomAssets",
"version": "0.5",
"owmlVersion": "0.3.37"
"owmlVersion": "0.3.38"
}