Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Auros/SiraUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
Auros committed Apr 1, 2022
2 parents 25bc3e8 + a2ce738 commit ed56651
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 5 deletions.
35 changes: 34 additions & 1 deletion SiraUtil/Objects/InternalRedecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace SiraUtil.Objects
{
internal class InternalRedecorator
{
private const string NewContextPrefabMethodName = "ByNewContextPrefab";
private const string ContainerFieldName = "_container";
private static readonly MethodInfo _getType = typeof(object).GetMethod(nameof(object.GetType));
private static readonly MethodInfo _prefabInitializingField = SymbolExtensions.GetMethodInfo(() => PrefabInitializing(null!, null!, null!, null!));
private static readonly MethodInfo _newPrefabMethod = typeof(FactoryFromBinderBase).GetMethod(nameof(FactoryFromBinderBase.FromComponentInNewPrefab));
Expand Down Expand Up @@ -52,6 +54,30 @@ protected static IEnumerable<CodeInstruction> Redecorate(IEnumerable<CodeInstruc
}
}

[HarmonyPatch(typeof(MultiplayerLobbyInstaller), nameof(MultiplayerLobbyInstaller.InstallBindings))]
internal class MultiplayerLobby
{
[HarmonyTranspiler]
protected static IEnumerable<CodeInstruction> Redecorate(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> codes = instructions.ToList();
InternalRedecorator.Redecorate(ref codes);
return codes;
}
}

[HarmonyPatch(typeof(MultiplayerPlayersManager), nameof(MultiplayerPlayersManager.BindPlayerFactories))]
internal class MultiplayerPlayerFactories
{
[HarmonyTranspiler]
protected static IEnumerable<CodeInstruction> Redecorate(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> codes = instructions.ToList();
InternalRedecorator.Redecorate(ref codes);
return codes;
}
}

private static UnityEngine.Object PrefabInitializing(UnityEngine.Object originalPrefab, DiContainer container, string fieldName, Type mainType)
{
IEnumerable<RedecoratorRegistration> registrations = container.AncestorContainers[0].Resolve<List<RedecoratorRegistration>>().Where(rr => rr.ContainerType == mainType && rr.Contract == fieldName).OrderByDescending(rr => rr.Priority);
Expand Down Expand Up @@ -83,7 +109,7 @@ internal static void Redecorate(ref List<CodeInstruction> codes)

for (int i = 0; i < codes.Count - 1; i++)
{
if (codes[i].opcode == OpCodes.Ldfld && (codes[i + 1].Calls(_newPrefabMethod) || (codes.Count > i + 4 && codes[i + 4].Calls(_newPrefabMethod)))) // uhhh for teranary operators :PogOh:
if (codes[i].opcode == OpCodes.Ldfld && (codes[i + 1].Calls(_newPrefabMethod) || (codes[i + 1].opcode == OpCodes.Callvirt && ((MethodInfo)codes[i + 1].operand).Name == NewContextPrefabMethodName) || (codes.Count > i + 4 && codes[i + 4].Calls(_newPrefabMethod)))) // uhhh for teranary operators :PogOh:
{
if (containerOpcode is null && containerOperand is null)
{
Expand All @@ -96,6 +122,13 @@ internal static void Redecorate(ref List<CodeInstruction> codes)
containerOperand = codes[c].operand;
break;
}

if (codes[c].opcode == OpCodes.Ldfld && ((FieldInfo)codes[c].operand).Name == ContainerFieldName)
{
containerOpcode = OpCodes.Ldfld;
containerOperand = codes[c].operand;
break;
}
}

// We are doing a manual install.
Expand Down
20 changes: 20 additions & 0 deletions SiraUtil/Objects/Multiplayer/ConnectedPlayerDuelRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace SiraUtil.Objects.Multiplayer
{
/// <summary>
/// Registers a redecorator for the connected player in the duel.
/// </summary>
public sealed class ConnectedPlayerDuelRegistration : TemplateRedecoratorRegistration<MultiplayerConnectedPlayerFacade, MultiplayerPlayersManager>
{
/// <summary>
/// Creates a new redecorator registration.
/// </summary>
/// <param name="redecorateCall">This is called when the object is being redecorated.</param>
/// <param name="priority">The redecoration priority.</param>
/// <param name="chain">Whether to chain this redecoration with others. Every redecoration is now aggregated.
/// The chain will start if the highest priority object has chaining enabled and will stop once a registration
/// in the aggregate has chaining disabled.</param>
public ConnectedPlayerDuelRegistration(Func<MultiplayerConnectedPlayerFacade, MultiplayerConnectedPlayerFacade> redecorateCall, int priority = 0, bool chain = true) : base("_connectedPlayerDuelControllerPrefab", redecorateCall, priority, chain) { }
}
}
20 changes: 20 additions & 0 deletions SiraUtil/Objects/Multiplayer/ConnectedPlayerRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace SiraUtil.Objects.Multiplayer
{
/// <summary>
/// Registers a redecorator for the connected player.
/// </summary>
public sealed class ConnectedPlayerRegistration : TemplateRedecoratorRegistration<MultiplayerConnectedPlayerFacade, MultiplayerPlayersManager>
{
/// <summary>
/// Creates a new redecorator registration.
/// </summary>
/// <param name="redecorateCall">This is called when the object is being redecorated.</param>
/// <param name="priority">The redecoration priority.</param>
/// <param name="chain">Whether to chain this redecoration with others. Every redecoration is now aggregated.
/// The chain will start if the highest priority object has chaining enabled and will stop once a registration
/// in the aggregate has chaining disabled.</param>
public ConnectedPlayerRegistration(Func<MultiplayerConnectedPlayerFacade, MultiplayerConnectedPlayerFacade> redecorateCall, int priority = 0, bool chain = true) : base("_connectedPlayerControllerPrefab", redecorateCall, priority, chain) { }
}
}
20 changes: 20 additions & 0 deletions SiraUtil/Objects/Multiplayer/LobbyAvatarPlaceRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace SiraUtil.Objects.Multiplayer
{
/// <summary>
/// Registers a redecorator for the lobby avatar place.
/// </summary>
public sealed class LobbyAvatarPlaceRegistration : TemplateRedecoratorRegistration<MultiplayerLobbyAvatarPlace, MultiplayerLobbyInstaller>
{
/// <summary>
/// Creates a new redecorator registration.
/// </summary>
/// <param name="redecorateCall">This is called when the object is being redecorated.</param>
/// <param name="priority">The redecoration priority.</param>
/// <param name="chain">Whether to chain this redecoration with others. Every redecoration is now aggregated.
/// The chain will start if the highest priority object has chaining enabled and will stop once a registration
/// in the aggregate has chaining disabled.</param>
public LobbyAvatarPlaceRegistration(Func<MultiplayerLobbyAvatarPlace, MultiplayerLobbyAvatarPlace> redecorateCall, int priority = 0, bool chain = true) : base("_multiplayerAvatarPlacePrefab", redecorateCall, priority, chain) { }
}
}
20 changes: 20 additions & 0 deletions SiraUtil/Objects/Multiplayer/LobbyAvatarRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace SiraUtil.Objects.Multiplayer
{
/// <summary>
/// Registers a redecorator for the lobby avatar controller.
/// </summary>
public sealed class LobbyAvatarRegistration : TemplateRedecoratorRegistration<MultiplayerLobbyAvatarController, MultiplayerLobbyInstaller>
{
/// <summary>
/// Creates a new redecorator registration.
/// </summary>
/// <param name="redecorateCall">This is called when the object is being redecorated.</param>
/// <param name="priority">The redecoration priority.</param>
/// <param name="chain">Whether to chain this redecoration with others. Every redecoration is now aggregated.
/// The chain will start if the highest priority object has chaining enabled and will stop once a registration
/// in the aggregate has chaining disabled.</param>
public LobbyAvatarRegistration(Func<MultiplayerLobbyAvatarController, MultiplayerLobbyAvatarController> redecorateCall, int priority = 0, bool chain = true) : base("_multiplayerLobbyAvatarControllerPrefab", redecorateCall, priority, chain) { }
}
}
20 changes: 20 additions & 0 deletions SiraUtil/Objects/Multiplayer/LocalActivePlayerDuelRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace SiraUtil.Objects.Multiplayer
{
/// <summary>
/// Registers a redecorator for the active local player in the duel environment.
/// </summary>
public sealed class LocalActivePlayerDuelRegistration : TemplateRedecoratorRegistration<MultiplayerLocalActivePlayerFacade, MultiplayerPlayersManager>
{
/// <summary>
/// Creates a new redecorator registration.
/// </summary>
/// <param name="redecorateCall">This is called when the object is being redecorated.</param>
/// <param name="priority">The redecoration priority.</param>
/// <param name="chain">Whether to chain this redecoration with others. Every redecoration is now aggregated.
/// The chain will start if the highest priority object has chaining enabled and will stop once a registration
/// in the aggregate has chaining disabled.</param>
public LocalActivePlayerDuelRegistration(Func<MultiplayerLocalActivePlayerFacade, MultiplayerLocalActivePlayerFacade> redecorateCall, int priority = 0, bool chain = true) : base("_activeLocalPlayerDuelControllerPrefab", redecorateCall, priority, chain) { }
}
}
20 changes: 20 additions & 0 deletions SiraUtil/Objects/Multiplayer/LocalActivePlayerRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace SiraUtil.Objects.Multiplayer
{
/// <summary>
/// Registers a redecorator for the active local player.
/// </summary>
public sealed class LocalActivePlayerRegistration : TemplateRedecoratorRegistration<MultiplayerLocalActivePlayerFacade, MultiplayerPlayersManager>
{
/// <summary>
/// Creates a new redecorator registration.
/// </summary>
/// <param name="redecorateCall">This is called when the object is being redecorated.</param>
/// <param name="priority">The redecoration priority.</param>
/// <param name="chain">Whether to chain this redecoration with others. Every redecoration is now aggregated.
/// The chain will start if the highest priority object has chaining enabled and will stop once a registration
/// in the aggregate has chaining disabled.</param>
public LocalActivePlayerRegistration(Func<MultiplayerLocalActivePlayerFacade, MultiplayerLocalActivePlayerFacade> redecorateCall, int priority = 0, bool chain = true) : base("_activeLocalPlayerControllerPrefab", redecorateCall, priority, chain) { }
}
}
20 changes: 20 additions & 0 deletions SiraUtil/Objects/Multiplayer/LocalInactivePlayerRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace SiraUtil.Objects.Multiplayer
{
/// <summary>
/// Registers a redecorator for the inactive local player.
/// </summary>
public sealed class LocalInactivePlayerRegistration : TemplateRedecoratorRegistration<MultiplayerLocalInactivePlayerFacade, MultiplayerPlayersManager>
{
/// <summary>
/// Creates a new redecorator registration.
/// </summary>
/// <param name="redecorateCall">This is called when the object is being redecorated.</param>
/// <param name="priority">The redecoration priority.</param>
/// <param name="chain">Whether to chain this redecoration with others. Every redecoration is now aggregated.
/// The chain will start if the highest priority object has chaining enabled and will stop once a registration
/// in the aggregate has chaining disabled.</param>
public LocalInactivePlayerRegistration(Func<MultiplayerLocalInactivePlayerFacade, MultiplayerLocalInactivePlayerFacade> redecorateCall, int priority = 0, bool chain = true) : base("_inactiveLocalPlayerControllerPrefab", redecorateCall, priority, chain) { }
}
}
4 changes: 2 additions & 2 deletions SiraUtil/Tools/FPFC/SimpleCameraController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public bool WillPitchExceedBounds(CameraState target, float rotationLerpPct)

public void UpdateTransform(Transform t)
{
t.eulerAngles = new Vector3(pitch, yaw, roll);
t.position = new Vector3(x, y, z);
t.localEulerAngles = new Vector3(pitch, yaw, roll);
t.localPosition = new Vector3(x, y, z);
}

public void Read(CameraState newState, float rotationLerpPct)
Expand Down
3 changes: 1 addition & 2 deletions SiraUtil/Tools/FPFC/SmoothCameraListener.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using IPA.Utilities;
using SiraUtil.Affinity;
using SiraUtil.Affinity;

namespace SiraUtil.Tools.FPFC
{
Expand Down

0 comments on commit ed56651

Please sign in to comment.