Skip to content

Commit

Permalink
Merge pull request #137 from amazingalek/dev
Browse files Browse the repository at this point in the history
* Removed TAIcheat and BSpatch
* Fixed asset loading bug
* Improved error messages
* OpenVR flag
* Improved input handling
* Cleanup
  • Loading branch information
amazingalek authored Jun 30, 2020
2 parents bd3f7cc + bad06c5 commit 8c5d124
Show file tree
Hide file tree
Showing 39 changed files with 450 additions and 1,321 deletions.
2 changes: 2 additions & 0 deletions OWML.Common/IModInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{
public interface IModInputHandler
{
IModInputTextures Textures { get; }

IModInputCombination RegisterCombination(IModBehaviour mod, string name, string combination);
void UnregisterCombination(IModInputCombination combo);
bool IsPressedExact(IModInputCombination combo);
Expand Down
10 changes: 10 additions & 0 deletions OWML.Common/IModInputTextures.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using UnityEngine;

namespace OWML.Common
{
public interface IModInputTextures
{
Texture2D KeyTexture(string key);
Texture2D KeyTexture(KeyCode key);
}
}
1 change: 1 addition & 0 deletions OWML.Common/OWML.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Compile Include="IModAssets.cs" />
<Compile Include="IModAsset.cs" />
<Compile Include="IModInputCombination.cs" />
<Compile Include="IModInputTextures.cs" />
<Compile Include="IModInputHandler.cs" />
<Compile Include="IModInteraction.cs" />
<Compile Include="Menus\IModLayoutButton.cs" />
Expand Down
31 changes: 22 additions & 9 deletions OWML.Launcher/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class App
private readonly OWPatcher _owPatcher;
private readonly VRPatcher _vrPatcher;

private const string VrArgument = " -vrmode openvr";

public App(IOwmlConfig owmlConfig, IModManifest owmlManifest, IModConsole writer, IModFinder modFinder,
OutputListener listener, PathFinder pathFinder, OWPatcher owPatcher, VRPatcher vrPatcher)
{
Expand Down Expand Up @@ -55,9 +57,10 @@ public void Run(string[] args)

ShowModList(mods);

PatchGame(mods);
var hasVrMod = HasVrMod(mods);
PatchGame(hasVrMod);

StartGame(args);
StartGame(args, hasVrMod);

if (hasPortArgument)
{
Expand Down Expand Up @@ -142,13 +145,18 @@ private void OnOutput(string s)
}
}

private void PatchGame(IList<IModData> mods)
private bool HasVrMod(IList<IModData> mods)
{
_owPatcher.PatchGame();

var vrMod = mods.FirstOrDefault(x => x.Config.RequireVR && x.Config.Enabled);
var enableVR = vrMod != null;
_writer.WriteLine(enableVR ? $"{vrMod.Manifest.UniqueName} requires VR." : "No mods require VR.");
var hasVrMod = vrMod != null;
_writer.WriteLine(hasVrMod ? $"{vrMod.Manifest.UniqueName} requires VR." : "No mods require VR.");
return hasVrMod;
}

private void PatchGame(bool enableVR)
{
_owPatcher.PatchGame();
_vrPatcher.PatchVR(enableVR);
try
{
_vrPatcher.PatchVR(enableVR);
Expand All @@ -159,12 +167,17 @@ private void PatchGame(IList<IModData> mods)
}
}

private void StartGame(string[] args)
private void StartGame(string[] args, bool enableVR)
{
_writer.WriteLine("Starting game...");
try
{
Process.Start($"{_owmlConfig.GamePath}/OuterWilds.exe", string.Join(" ", args));
var gameArgs = string.Join(" ", args);
if (enableVR)
{
gameArgs += VrArgument;
}
Process.Start($"{_owmlConfig.GamePath}/OuterWilds.exe", gameArgs);
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion OWML.Launcher/OWML.Config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"gamePath": "C:/Program Files/Epic Games/OuterWilds",
"verbose": false,
"combinationsBlockInput" : false
"combinationsBlockInput": false
}
2 changes: 1 addition & 1 deletion OWML.Launcher/OWML.Manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"author": "Alek",
"name": "OWML",
"uniqueName": "Alek.OWML",
"version": "0.3.57",
"version": "0.4.0",
"description": "The mod loader and mod framework for Outer Wilds"
}
3 changes: 2 additions & 1 deletion OWML.ModHelper.Assets/ModAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private IEnumerator LoadMesh(ObjectAsset modAsset, string objectPath)
var meshFilter = modAsset.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
yield return new WaitForEndOfFrame();
modAsset.SetAsset(modAsset.gameObject);
modAsset.SetMeshFilter(meshFilter);
}

private IEnumerator LoadTexture(ObjectAsset modAsset, string imagePath)
Expand All @@ -111,6 +111,7 @@ private IEnumerator LoadTexture(ObjectAsset modAsset, string imagePath)
}
var meshRenderer = modAsset.AddComponent<MeshRenderer>();
meshRenderer.material.mainTexture = texture;
modAsset.SetMeshRenderer(meshRenderer);
}

private IEnumerator LoadMesh(MeshAsset modAsset, string objectPath)
Expand Down
23 changes: 23 additions & 0 deletions OWML.ModHelper.Assets/ObjectAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,28 @@ namespace OWML.ModHelper.Assets
{
public class ObjectAsset : ModAsset<GameObject>
{
private MeshRenderer _meshRenderer;
private MeshFilter _meshFilter;

public void SetMeshRenderer(MeshRenderer meshRenderer)
{
_meshRenderer = meshRenderer;
SetAssetIfComplete();
}

public void SetMeshFilter(MeshFilter meshFilter)
{
_meshFilter = meshFilter;
SetAssetIfComplete();
}

private void SetAssetIfComplete()
{
if (_meshFilter != null && _meshRenderer != null)
{
SetAsset(gameObject);
}
}

}
}
9 changes: 7 additions & 2 deletions OWML.ModHelper.Events/HarmonyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,21 @@ private HarmonyInstance CreateInstance()
private MethodInfo GetMethod<T>(string methodName)
{
var targetType = typeof(T);
MethodInfo result = null;
try
{
_logger.Log($"Getting method {methodName} of {targetType.Name}");
return targetType.GetAnyMethod(methodName);
result = targetType.GetAnyMethod(methodName);
}
catch (Exception ex)
{
_console.WriteLine($"Exception while getting method {methodName} of {targetType.Name}: {ex}");
return null;
}
if (result == null)
{
_console.WriteLine($"Error: Original method {methodName} of class {targetType} not found");
}
return result;
}

public void AddPrefix<T>(string methodName, Type patchType, string patchMethodName)
Expand Down
2 changes: 1 addition & 1 deletion OWML.ModHelper.Events/ModEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void SubscribeToEvent<T>(Common.Events ev)
var type = typeof(T);
if (IsSubscribedTo(type, ev))
{
_console.WriteLine($"Warning: already subscribed to {ev} of {type.Name}");
_logger.Log($"Already subscribed to {ev} of {type.Name}");
return;
}
AddToEventList(_subscribedEvents, type, ev);
Expand Down
13 changes: 5 additions & 8 deletions OWML.ModHelper.Input/BindingChangeListener.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using OWML.Common;
using OWML.Common;
using UnityEngine;

namespace OWML.ModHelper.Input
Expand All @@ -14,13 +11,13 @@ public class BindingChangeListener : MonoBehaviour
internal void Initialize(ModInputHandler inputHandler, IModEvents events)
{
_inputHandler = inputHandler;
events.Subscribe<TitleScreenManager>(Common.Events.AfterStart);
events.Subscribe<TitleScreenManager>(Events.AfterStart);
events.OnEvent += OnEvent;
}

private void OnEvent(MonoBehaviour behaviour, Common.Events ev)
private void OnEvent(MonoBehaviour behaviour, Events ev)
{
if (behaviour.GetType() == typeof(TitleScreenManager) && ev == Common.Events.AfterStart)
if (behaviour.GetType() == typeof(TitleScreenManager) && ev == Events.AfterStart)
{
_updateInputsNext = true;
}
Expand All @@ -29,7 +26,7 @@ private void OnEvent(MonoBehaviour behaviour, Common.Events ev)
private void Start()
{
DontDestroyOnLoad(gameObject);
GlobalMessenger.AddListener("KeyBindingsChanged", new Callback(PrepareForUpdate));
GlobalMessenger.AddListener("KeyBindingsChanged", PrepareForUpdate);
}

private void PrepareForUpdate()
Expand Down
112 changes: 7 additions & 105 deletions OWML.ModHelper.Input/ModInputCombination.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using OWML.Common;
using UnityEngine;
Expand All @@ -8,11 +7,6 @@ namespace OWML.ModHelper.Input
{
public class ModInputCombination : IModInputCombination
{
private const int GamePadKeyDiff = 20;
private const int MaxUsefulKey = 350;
private const int MaxComboLength = 7;
private const string XboxPrefix = "xbox_";

public float LastPressedMoment { get; private set; }
public bool IsFirst { get; private set; }
public float PressDuration => LastPressedMoment - _firstPressedMoment;
Expand All @@ -24,8 +18,8 @@ public class ModInputCombination : IModInputCombination

private bool _isPressed;
private float _firstPressedMoment;
private List<KeyCode> _singles = new List<KeyCode>();
private List<long> _hashes = new List<long>();
private readonly List<KeyCode> _singles = new List<KeyCode>();
private readonly List<long> _hashes;

internal ModInputCombination(IModManifest mod, string name, string combination)
{
Expand All @@ -34,115 +28,23 @@ internal ModInputCombination(IModManifest mod, string name, string combination)
_hashes = StringToHashes(combination);
}

private KeyCode StringToKeyCodeKeyboard(string keyboardKey)
{
if (keyboardKey == "control" || keyboardKey == "ctrl")
{
return KeyCode.LeftControl;
}
if (keyboardKey == "shift")
{
return KeyCode.LeftShift;
}
if (keyboardKey == "alt")
{
return KeyCode.LeftAlt;
}
var code = (KeyCode)Enum.Parse(typeof(KeyCode), keyboardKey, true);
return Enum.IsDefined(typeof(KeyCode), code) ? code : KeyCode.None;
}

private KeyCode StringToKeyCodeGamepad(string gamepadKey)
{
var gamepadcodeCode = (JoystickButton)Enum.Parse(typeof(JoystickButton), gamepadKey, true);
return (Enum.IsDefined(typeof(JoystickButton), gamepadcodeCode)) ?
InputTranslator.GetButtonKeyCode(gamepadcodeCode) : KeyCode.None;
}

private KeyCode StringToKeyCodeXbox(string xboxKey)
{
switch (xboxKey[0])
{
case 'A':
return InputTranslator.GetButtonKeyCode(JoystickButton.FaceDown);
case 'B':
return InputTranslator.GetButtonKeyCode(JoystickButton.FaceRight);
case 'X':
return InputTranslator.GetButtonKeyCode(JoystickButton.FaceLeft);
case 'Y':
return InputTranslator.GetButtonKeyCode(JoystickButton.FaceUp);
default:
return StringToKeyCodeGamepad(xboxKey);
}
}

private KeyCode StringToKeyCode(string key)
{
var trimmedKey = key.Trim();
return trimmedKey.Contains(XboxPrefix) ? StringToKeyCodeXbox(trimmedKey.Substring(XboxPrefix.Length))
: StringToKeyCodeKeyboard(trimmedKey);
}

private int[] StringToKeyArray(string stringCombination)
{
var keyCombination = new int[MaxComboLength];
var i = 0;
foreach (var key in stringCombination.Trim().ToLower().Split('+'))
{
var code = StringToKeyCode(key);
if ((int)code >= MaxUsefulKey)
{
code -= (((int)code - MaxUsefulKey + GamePadKeyDiff) / GamePadKeyDiff) * GamePadKeyDiff;
}
if (code == KeyCode.None)
{
keyCombination[0] = (int)RegistrationCode.InvalidCombination;
return keyCombination;
}
if (i >= MaxComboLength)
{
keyCombination[0] = (int)RegistrationCode.CombinationTooLong;
return keyCombination;
}
keyCombination[i] = (int)code;
i++;
}
Array.Sort(keyCombination);
return keyCombination;
}

private long StringToHash(string stringCombination)
{
var keyCombination = StringToKeyArray(stringCombination);
if (keyCombination[0] < 0)
{
return keyCombination[0];
}
long hash = 0;
for (var i = 0; i < MaxComboLength; i++)
{
hash = hash * MaxUsefulKey + keyCombination[i];
}
return hash;
}

private List<long> StringToHashes(string combinations)
{
var hashes = new List<long>();
foreach (var combo in combinations.Split('/'))
{
var hash = StringToHash(combo);
var hash = ModInputLibrary.StringToHash(combo);
if (hash <= 0)
{
hashes.Clear();
hashes.Add(hash);
return hashes;
}
hashes.Add(hash);
if (hash < MaxUsefulKey)
if (hash < ModInputLibrary.MaxUsefulKey)
{
_singles.Add((KeyCode)hash);
}
}
}
return hashes;
}
Expand All @@ -161,4 +63,4 @@ public void InternalSetPressed(bool isPressed = true)
_isPressed = isPressed;
}
}
}
}
Loading

0 comments on commit 8c5d124

Please sign in to comment.