2021-02-06 22:03:10 +00:00
|
|
|
|
using OWML.Common;
|
2021-06-19 11:21:08 +00:00
|
|
|
|
using OWML.Utils;
|
2021-04-11 16:05:02 +00:00
|
|
|
|
using QSB.Player.TransformSync;
|
2021-01-26 14:07:17 +00:00
|
|
|
|
using QuantumUNET;
|
2021-02-06 22:03:10 +00:00
|
|
|
|
using System;
|
2021-02-09 10:03:13 +00:00
|
|
|
|
using System.Reflection;
|
2021-01-26 14:07:17 +00:00
|
|
|
|
using UnityEngine;
|
2020-08-16 15:15:36 +00:00
|
|
|
|
|
|
|
|
|
namespace QSB.Utility
|
|
|
|
|
{
|
2021-01-26 14:07:17 +00:00
|
|
|
|
public static class Extensions
|
2020-12-02 21:23:01 +00:00
|
|
|
|
{
|
2021-04-16 10:40:13 +00:00
|
|
|
|
// UNITY
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public static void Show(this GameObject gameObject) => SetVisibility(gameObject, true);
|
2020-08-16 15:15:36 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public static void Hide(this GameObject gameObject) => SetVisibility(gameObject, false);
|
2020-08-16 15:15:36 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
private static void SetVisibility(GameObject gameObject, bool isVisible)
|
|
|
|
|
{
|
|
|
|
|
var renderers = gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
|
|
|
|
|
foreach (var renderer in renderers)
|
|
|
|
|
{
|
|
|
|
|
renderer.enabled = isVisible;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-23 07:54:55 +00:00
|
|
|
|
|
2021-04-20 07:36:07 +00:00
|
|
|
|
public static Quaternion TransformRotation(this Transform transform, Quaternion localRotation)
|
|
|
|
|
=> transform.rotation * localRotation;
|
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public static GameObject InstantiateInactive(this GameObject original)
|
|
|
|
|
{
|
|
|
|
|
original.SetActive(false);
|
2021-02-06 22:03:10 +00:00
|
|
|
|
var copy = UnityEngine.Object.Instantiate(original);
|
2020-12-02 21:23:01 +00:00
|
|
|
|
original.SetActive(true);
|
|
|
|
|
return copy;
|
|
|
|
|
}
|
2020-08-23 07:54:55 +00:00
|
|
|
|
|
2020-12-14 21:20:53 +00:00
|
|
|
|
public static Transform InstantiateInactive(this Transform original) =>
|
|
|
|
|
original.gameObject.InstantiateInactive().transform;
|
2021-01-26 14:07:17 +00:00
|
|
|
|
|
|
|
|
|
// QNET
|
2021-03-09 16:43:41 +00:00
|
|
|
|
public static uint GetPlayerId(this QNetworkConnection connection)
|
2021-01-26 14:07:17 +00:00
|
|
|
|
{
|
2021-06-11 20:19:08 +00:00
|
|
|
|
if (connection == null)
|
|
|
|
|
{
|
2021-06-23 20:46:52 +00:00
|
|
|
|
DebugLog.ToConsole($"Error - Trying to get player id of null QNetworkConnection.", MessageType.Error);
|
2021-06-11 20:19:08 +00:00
|
|
|
|
return uint.MaxValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var playerController = connection.PlayerControllers[0];
|
|
|
|
|
if (playerController == null)
|
|
|
|
|
{
|
2021-06-23 20:46:52 +00:00
|
|
|
|
DebugLog.ToConsole($"Error - Player Controller of {connection.address} is null.", MessageType.Error);
|
2021-06-11 20:19:08 +00:00
|
|
|
|
return uint.MaxValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var go = playerController.Gameobject;
|
|
|
|
|
if (go == null)
|
|
|
|
|
{
|
2021-06-23 20:46:52 +00:00
|
|
|
|
DebugLog.ToConsole($"Error - GameObject of {playerController.UnetView.NetId.Value} is null.", MessageType.Error);
|
2021-06-11 20:19:08 +00:00
|
|
|
|
return uint.MaxValue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 14:07:17 +00:00
|
|
|
|
var controller = go.GetComponent<PlayerTransformSync>();
|
2021-06-11 20:19:08 +00:00
|
|
|
|
if (controller == null)
|
|
|
|
|
{
|
2021-06-23 20:46:52 +00:00
|
|
|
|
DebugLog.ToConsole($"Error - No PlayerTransformSync found on {go.name}", MessageType.Error);
|
2021-06-11 20:19:08 +00:00
|
|
|
|
return uint.MaxValue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 16:43:41 +00:00
|
|
|
|
return controller.NetId.Value;
|
2021-01-26 14:07:17 +00:00
|
|
|
|
}
|
2021-02-06 22:03:10 +00:00
|
|
|
|
|
|
|
|
|
// C#
|
|
|
|
|
public static void SafeInvoke(this MulticastDelegate multicast, params object[] args)
|
|
|
|
|
{
|
|
|
|
|
foreach (var del in multicast.GetInvocationList())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
del.DynamicInvoke(args);
|
|
|
|
|
}
|
2021-02-09 10:03:13 +00:00
|
|
|
|
catch (TargetInvocationException ex)
|
2021-02-06 22:03:10 +00:00
|
|
|
|
{
|
2021-02-09 10:03:13 +00:00
|
|
|
|
DebugLog.ToConsole($"Error invoking delegate! Message : {ex.InnerException.Message} Stack Trace : {ex.InnerException.StackTrace}", MessageType.Error);
|
2021-02-06 22:03:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-25 23:07:53 +00:00
|
|
|
|
|
2021-03-25 23:11:19 +00:00
|
|
|
|
public static float Map(this float value, float inputFrom, float inputTo, float outputFrom, float outputTo)
|
2021-03-25 23:07:53 +00:00
|
|
|
|
=> ((value - inputFrom) / (inputTo - inputFrom) * (outputTo - outputFrom)) + outputFrom;
|
2021-04-13 20:09:26 +00:00
|
|
|
|
|
|
|
|
|
public static void CallBase<ThisType, BaseType>(this ThisType obj, string methodName)
|
|
|
|
|
where ThisType : BaseType
|
|
|
|
|
{
|
2021-06-19 11:21:08 +00:00
|
|
|
|
var method = typeof(BaseType).GetAnyMethod(methodName);
|
2021-06-19 11:18:12 +00:00
|
|
|
|
if (method == null)
|
|
|
|
|
{
|
2021-06-23 20:46:52 +00:00
|
|
|
|
DebugLog.ToConsole($"Error - Couldn't find method {methodName} in {typeof(BaseType).FullName}!", MessageType.Error);
|
2021-06-19 11:18:12 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 20:09:26 +00:00
|
|
|
|
var functionPointer = method.MethodHandle.GetFunctionPointer();
|
2021-06-19 11:18:12 +00:00
|
|
|
|
if (functionPointer == null)
|
|
|
|
|
{
|
2021-06-23 20:46:52 +00:00
|
|
|
|
DebugLog.ToConsole($"Error - Function pointer for {methodName} in {typeof(BaseType).FullName} is null!", MessageType.Error);
|
2021-06-19 11:18:12 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 20:09:26 +00:00
|
|
|
|
var methodAction = (Action)Activator.CreateInstance(typeof(Action), obj, functionPointer);
|
|
|
|
|
methodAction();
|
|
|
|
|
}
|
2021-05-15 10:25:47 +00:00
|
|
|
|
|
|
|
|
|
// OW
|
|
|
|
|
|
2021-06-19 10:26:05 +00:00
|
|
|
|
public static Vector3 GetRelativeAngularVelocity(this OWRigidbody baseBody, OWRigidbody relativeBody)
|
2021-06-13 19:49:14 +00:00
|
|
|
|
=> baseBody.GetAngularVelocity() - relativeBody.GetAngularVelocity();
|
2020-12-14 21:20:53 +00:00
|
|
|
|
}
|
2021-01-26 14:07:17 +00:00
|
|
|
|
}
|