mirror of
https://github.com/originalnicodr/CinematicUnityExplorer.git
synced 2026-10-11 09:58:02 +08:00
IGCS Support for CUE (#72)
* Initial work on supporting IGCS
* feat: Added callback when starting session, adding missing func.
* chore: tidy up math and cleanup.
* chore: tidy up some verbose code
* Added more callbacks, fixed math.
* fix: use queue command system instead of directly writing to the camera.
* lock camera while igcsdof is active.
* build system shenanigans
Now we can build the project just fine by building separately the C
project and the C# project.
* small syntax changes.
learning c# and stuff
* Refactor executeCameraCommand and improve thread-safety in FreeCamPanel
* woops, correct order of arguments for MessageBoxA
* fix: move the ourCamera check where it's actually needed.
* chore: formatting c file
* rewrite: move all igcsdof logic to its own class.
Since CUE *must* work without igcsdof, I thought it would be convenient
to handle all the logic regarding igcsdof in its own class. The
initialization is a bit of a mess because since most of what it's on
FreeCam is static I had to do some trickery.
I wanted to specifically avoid calling LoadLibrary every time so I
thought on only initializing one and having an internal flag either the
thing loaded correctly or not (`isValid`).
* fix: Pin callbacks to avoid garbage collection
* chore: removed unused code
* chore: fix typo.
* nit: more idiomatic C# usage.
Just applied what rider suggested =)
* fix: remove unnecesary GCHandle, store delegates.
* feat: Update IGCS dof camera status accordingly.
Before, we just left igcsdof always enabled, now we handle the cases
properly, avoiding the posibility of the user using igcsdof with the
freecam disabled.
* nit: fix comments and added some to UnityIGCSConnector.
* feat: Get the transform reference earlier for perf™️
* fix: Properly handle an initialization error.
* chore: remove overly verbose debugging
* fix: make sure we start with a clear queue after we end a session.
This was what maybe was causing the misaligning after you cancel a
session since we never checked if the queue was emptied by the code.
* fix: properly cast a function pointer to use what it returns.
This commit is contained in:
1 parent
e6df5f9c7b
commit
f557d3de88
7 files changed
+457
-1
No files matched your search
@@ -0,0 +1,129 @@
|
||||
using MonoMod.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using UnityExplorer;
|
||||
|
||||
namespace CinematicUnityExplorer.Cinematic
|
||||
{
|
||||
// StepCommand is basically the offset of step_left and step_up, what IGCS sends to move the camera.
|
||||
using StepCommand = Mono.CSharp.Tuple<float, float>;
|
||||
|
||||
internal static class NativeMethods
|
||||
{
|
||||
[DllImport("kernel32.dll")]
|
||||
public static extern IntPtr LoadLibrary(string dll);
|
||||
|
||||
[DllImport("kernel32.dll")]
|
||||
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
|
||||
}
|
||||
public class UnityIGCSConnector
|
||||
{
|
||||
// UnityIGCSConnector.dll definitions.
|
||||
private delegate void MoveCameraCallback(float step_left, float step_up, float fov, int from_start);
|
||||
private delegate void SessionCallback();
|
||||
private delegate IntPtr U_IGCS_Initialize(MoveCameraCallback callback, SessionCallback start_cb, SessionCallback end_cb);
|
||||
|
||||
// Store the initial position when a session start in IGCSDof.
|
||||
Mono.CSharp.Tuple<Vector3, Quaternion> position = null;
|
||||
|
||||
private readonly bool isValid = false;
|
||||
private bool _isActive = false;
|
||||
public bool IsActive => isValid && _isActive;
|
||||
|
||||
// Delegate holder for the MoveCamera, StartSession and EndSession.
|
||||
private readonly List<System.Delegate> delegates = new();
|
||||
|
||||
// Since some games use multi-threaded, in order to make sure we're only moving things during
|
||||
// the main thread is executing, we use this Queue to enqueue the move commands and dequeue them in the Update function.
|
||||
// This object *must* be used with a Lock.
|
||||
private readonly Queue<StepCommand> commands = new();
|
||||
|
||||
private IntPtr CameraStatus = IntPtr.Zero;
|
||||
// In order to avoid allocations on every Update call, we create this buffer to allocate once
|
||||
// and copy from here the CameraStatus (because Marshal.Copy requires a buffer, urgh).
|
||||
private readonly byte[] CameraStatusBuffer = new byte[] { 0x0 };
|
||||
|
||||
public void UpdateFreecamStatus(bool enabled)
|
||||
{
|
||||
if (CameraStatus == IntPtr.Zero) return;
|
||||
|
||||
CameraStatusBuffer[0] = enabled ? (byte)0x1 : (byte)0x0;
|
||||
Marshal.Copy(CameraStatusBuffer, 0, CameraStatus, 1);
|
||||
}
|
||||
|
||||
public void ExecuteCameraCommand(Camera cam)
|
||||
{
|
||||
var transform = cam.transform;
|
||||
if (!_isActive || position == null)
|
||||
{
|
||||
position = new(transform.position, transform.rotation);
|
||||
}
|
||||
|
||||
if (!_isActive || position == null) { return; }
|
||||
|
||||
StepCommand c = null;
|
||||
|
||||
lock (commands)
|
||||
{
|
||||
if (commands.Count <= 0) return;
|
||||
c = commands.Dequeue();
|
||||
}
|
||||
|
||||
transform.position = position.Item1;
|
||||
transform.rotation = position.Item2;
|
||||
transform.Translate(c.Item1, c.Item2, 0.0f);
|
||||
}
|
||||
|
||||
private void MoveCamera(float stepLeft, float stepUp, float fov, int fromStart)
|
||||
{
|
||||
lock (commands)
|
||||
{
|
||||
commands.Enqueue(new StepCommand(stepLeft, stepUp));
|
||||
}
|
||||
}
|
||||
private void StartSession()
|
||||
{
|
||||
_isActive = true;
|
||||
}
|
||||
|
||||
private void EndSession()
|
||||
{
|
||||
position = null;
|
||||
_isActive = false;
|
||||
|
||||
lock (commands)
|
||||
commands.Clear();
|
||||
}
|
||||
|
||||
public UnityIGCSConnector()
|
||||
{
|
||||
var lib = NativeMethods.LoadLibrary(@"UnityIGCSConnector.dll");
|
||||
if (lib == IntPtr.Zero)
|
||||
{
|
||||
ExplorerCore.LogWarning("UnityIGCSConnector.dll was not found so IGCSDof will not be available");
|
||||
return;
|
||||
}
|
||||
|
||||
var func = NativeMethods.GetProcAddress(lib, @"U_IGCS_Initialize");
|
||||
if (func == IntPtr.Zero)
|
||||
{
|
||||
throw new EntryPointNotFoundException("Failed to find 'U_IGCS_Initialize' which means you can have a corrupt UnityIGCSConnector.dll.");
|
||||
}
|
||||
|
||||
var initFunc = (U_IGCS_Initialize)Marshal.GetDelegateForFunctionPointer(func, typeof(U_IGCS_Initialize));
|
||||
|
||||
delegates.Add(new MoveCameraCallback(MoveCamera));
|
||||
delegates.Add(new SessionCallback(StartSession));
|
||||
delegates.Add(new SessionCallback(EndSession));
|
||||
|
||||
CameraStatus = initFunc((MoveCameraCallback)delegates[0], (SessionCallback)delegates[1], (SessionCallback)delegates[2]);
|
||||
if (CameraStatus == IntPtr.Zero)
|
||||
throw new InvalidDataException("IGCSDof returned an invalid pointer which means something went wrong");
|
||||
|
||||
isValid = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,9 @@ using UnityExplorer.Inspectors;
|
||||
using UniverseLib.Input;
|
||||
using UniverseLib.UI;
|
||||
using UniverseLib.UI.Models;
|
||||
using System.Runtime.InteropServices;
|
||||
using CinematicUnityExplorer.Cinematic;
|
||||
|
||||
#if UNHOLLOWER
|
||||
using UnhollowerRuntimeLib;
|
||||
#endif
|
||||
@@ -76,9 +79,12 @@ namespace UnityExplorer.UI.Panels
|
||||
|
||||
private static FreecamCursorUnlocker freecamCursorUnlocker = null;
|
||||
|
||||
public static UnityIGCSConnector connector = new();
|
||||
|
||||
internal static void BeginFreecam()
|
||||
{
|
||||
inFreeCamMode = true;
|
||||
connector.UpdateFreecamStatus(true);
|
||||
|
||||
previousMousePosition = IInputManager.MousePosition;
|
||||
|
||||
@@ -177,6 +183,7 @@ namespace UnityExplorer.UI.Panels
|
||||
internal static void EndFreecam()
|
||||
{
|
||||
inFreeCamMode = false;
|
||||
connector.UpdateFreecamStatus(false);
|
||||
|
||||
if (usingGameCamera)
|
||||
{
|
||||
@@ -254,6 +261,9 @@ namespace UnityExplorer.UI.Panels
|
||||
if (positionInput.Component.isFocused)
|
||||
return;
|
||||
|
||||
if (connector.IsActive)
|
||||
return;
|
||||
|
||||
lastSetCameraPosition = ourCamera.transform.position;
|
||||
positionInput.Text = ParseUtility.ToStringForInput<Vector3>(lastSetCameraPosition);
|
||||
}
|
||||
@@ -672,7 +682,7 @@ namespace UnityExplorer.UI.Panels
|
||||
|
||||
Transform transform = FreeCamPanel.ourCamera.transform;
|
||||
|
||||
if (!FreeCamPanel.blockFreecamMovementToggle.isOn && !FreeCamPanel.cameraPathMover.playingPath){
|
||||
if (!FreeCamPanel.blockFreecamMovementToggle.isOn && !FreeCamPanel.cameraPathMover.playingPath && !FreeCamPanel.connector.IsActive) {
|
||||
ProcessInput();
|
||||
}
|
||||
|
||||
@@ -692,6 +702,8 @@ namespace UnityExplorer.UI.Panels
|
||||
FreeCamPanel.followObjectLastRotation = FreeCamPanel.followObject.transform.rotation;
|
||||
}
|
||||
|
||||
FreeCamPanel.connector.ExecuteCameraCommand(FreeCamPanel.ourCamera);
|
||||
|
||||
FreeCamPanel.UpdatePositionInput();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user