2022-01-28 20:49:07 -08:00
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using Mirror;
|
2022-01-14 21:18:05 -08:00
|
|
|
|
using OWML.Common;
|
2021-02-06 22:03:10 +00:00
|
|
|
|
using System;
|
2021-12-10 23:28:42 -08:00
|
|
|
|
using System.Collections.Generic;
|
2021-07-06 20:08:26 -03:00
|
|
|
|
using System.Linq;
|
2021-02-09 10:03:13 +00:00
|
|
|
|
using System.Reflection;
|
2021-01-26 14:07:17 +00:00
|
|
|
|
using UnityEngine;
|
2021-12-25 21:02:21 -08:00
|
|
|
|
using Object = UnityEngine.Object;
|
2020-08-16 17:15:36 +02: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-12-25 21:34:44 -08:00
|
|
|
|
#region UNITY
|
|
|
|
|
|
2021-04-20 08:36:07 +01: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-12-25 21:02:21 -08:00
|
|
|
|
var copy = Object.Instantiate(original);
|
2020-12-02 21:23:01 +00:00
|
|
|
|
original.SetActive(true);
|
|
|
|
|
return copy;
|
|
|
|
|
}
|
2020-08-23 09:54:55 +02: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
|
|
|
|
|
2021-12-25 21:34:44 -08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2022-01-14 21:18:05 -08:00
|
|
|
|
#region MIRROR
|
|
|
|
|
|
2022-01-24 11:53:51 -08:00
|
|
|
|
public static uint GetPlayerId(this NetworkConnection conn)
|
|
|
|
|
{
|
2022-01-24 17:52:46 -08:00
|
|
|
|
if (!conn.identity)
|
2022-01-24 11:53:51 -08:00
|
|
|
|
{
|
2022-01-27 19:10:39 -08:00
|
|
|
|
// wtf
|
|
|
|
|
DebugLog.ToConsole($"Error - GetPlayerId on {conn.address} has no identity\n{Environment.StackTrace}", MessageType.Error);
|
2022-01-24 11:53:51 -08:00
|
|
|
|
return uint.MaxValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return conn.identity.netId;
|
|
|
|
|
}
|
2021-12-25 21:34:44 -08:00
|
|
|
|
|
2022-01-14 22:07:32 -08:00
|
|
|
|
public static void SpawnWithServerAuthority(this GameObject go) =>
|
|
|
|
|
NetworkServer.Spawn(go, NetworkServer.localConnection);
|
2021-08-19 16:37:29 +01:00
|
|
|
|
|
2021-12-25 21:34:44 -08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region C#
|
|
|
|
|
|
2021-02-06 22:03:10 +00:00
|
|
|
|
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
|
|
|
|
{
|
2022-01-18 01:16:24 -08:00
|
|
|
|
DebugLog.ToConsole($"Error invoking delegate! {ex.InnerException}", MessageType.Error);
|
2021-02-06 22:03:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-25 23:07:53 +00:00
|
|
|
|
|
2021-12-27 21:04:57 +00:00
|
|
|
|
public static float Map(this float value, float inputFrom, float inputTo, float outputFrom, float outputTo, bool clamp)
|
|
|
|
|
{
|
2022-01-27 14:37:40 -08:00
|
|
|
|
var mappedValue = (value - inputFrom) / (inputTo - inputFrom) * (outputTo - outputFrom) + outputFrom;
|
2021-12-27 21:04:57 +00:00
|
|
|
|
|
|
|
|
|
return clamp
|
|
|
|
|
? Mathf.Clamp(mappedValue, outputTo, outputFrom)
|
|
|
|
|
: mappedValue;
|
|
|
|
|
}
|
2021-07-06 19:51:12 -03:00
|
|
|
|
|
2021-12-14 23:57:21 -08:00
|
|
|
|
public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
|
2021-07-07 09:02:23 +01:00
|
|
|
|
{
|
|
|
|
|
foreach (var item in enumerable)
|
2021-07-07 09:09:34 +01:00
|
|
|
|
{
|
2021-07-07 09:02:23 +01:00
|
|
|
|
action(item);
|
2021-07-07 09:09:34 +01:00
|
|
|
|
}
|
2021-07-07 09:02:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 23:57:21 -08:00
|
|
|
|
public static int IndexOf<T>(this T[] array, T value) => Array.IndexOf(array, value);
|
|
|
|
|
|
2021-12-18 17:52:48 -08:00
|
|
|
|
public static bool IsInRange<T>(this IList<T> list, int index) => index >= 0 && index < list.Count;
|
2021-12-14 20:24:02 -08:00
|
|
|
|
|
2021-07-07 09:12:35 +01:00
|
|
|
|
public static void RaiseEvent<T>(this T instance, string eventName, params object[] args)
|
2021-07-07 09:02:23 +01:00
|
|
|
|
{
|
2021-12-27 13:50:03 -08:00
|
|
|
|
const BindingFlags flags = BindingFlags.Instance
|
|
|
|
|
| BindingFlags.Static
|
|
|
|
|
| BindingFlags.Public
|
|
|
|
|
| BindingFlags.NonPublic
|
|
|
|
|
| BindingFlags.DeclaredOnly;
|
2021-11-20 19:49:50 +00:00
|
|
|
|
if (typeof(T)
|
2022-01-25 19:42:22 -08:00
|
|
|
|
.GetField(eventName, flags)?
|
|
|
|
|
.GetValue(instance) is not MulticastDelegate multiDelegate)
|
2021-07-07 09:02:23 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 14:37:40 -08:00
|
|
|
|
multiDelegate.GetInvocationList().ForEach(dl => dl.DynamicInvoke(args));
|
2021-07-07 09:02:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 23:28:42 -08:00
|
|
|
|
public static IEnumerable<Type> GetDerivedTypes(this Type type) => type.Assembly.GetTypes()
|
|
|
|
|
.Where(x => !x.IsInterface && !x.IsAbstract && type.IsAssignableFrom(x));
|
|
|
|
|
|
2022-01-15 02:27:22 -08:00
|
|
|
|
public static Guid ToGuid(this int value)
|
|
|
|
|
{
|
|
|
|
|
var bytes = new byte[16];
|
|
|
|
|
BitConverter.GetBytes(value).CopyTo(bytes, 0);
|
|
|
|
|
return new Guid(bytes);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 19:42:22 -08:00
|
|
|
|
public static void Try(this object self, string description, Action action)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
action();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"{self} - error {description} : {e}", MessageType.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-28 20:49:07 -08:00
|
|
|
|
public static async UniTask Try(this object self, string description, Func<UniTask> func)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await func();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"{self} - error {description} : {e}", MessageType.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 21:34:44 -08:00
|
|
|
|
#endregion
|
2020-12-14 21:20:53 +00:00
|
|
|
|
}
|
2021-01-26 14:07:17 +00:00
|
|
|
|
}
|