diff options
author | chai <215380520@qq.com> | 2023-10-18 10:16:32 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-10-18 10:16:32 +0800 |
commit | 4ccd4bc6d126e0e0f51a50aa10c85b9bf48b1210 (patch) | |
tree | 9ac931da935b59a8d7c57ff0b6d90b88a0e5a479 /ActiveRagdoll/Assets/ThirdParty/SerializableCallback |
+ init
Diffstat (limited to 'ActiveRagdoll/Assets/ThirdParty/SerializableCallback')
36 files changed, 1418 insertions, 0 deletions
diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/.gitignore b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/.gitignore new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/.gitignore diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor.meta new file mode 100644 index 0000000..ce6c62e --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: 39c1d273b3e79e346a281e4a542a63a6 +folderAsset: yes +timeCreated: 1513845239 +licenseType: Pro +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor/SerializableCallbackDrawer.cs b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor/SerializableCallbackDrawer.cs new file mode 100644 index 0000000..561796c --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor/SerializableCallbackDrawer.cs @@ -0,0 +1,327 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using UnityEditor; +using UnityEngine; +using Object = UnityEngine.Object; + +[CustomPropertyDrawer(typeof(TargetConstraintAttribute))] +[CustomPropertyDrawer(typeof(SerializableCallbackBase), true)] +public class SerializableCallbackDrawer : PropertyDrawer { + + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { + // Without this, you can't edit fields above the SerializedProperty + property.serializedObject.ApplyModifiedProperties(); + + // Indent label + label.text = " " + label.text; + +#if UNITY_2019_1_OR_NEWER + GUI.Box(position, ""); +#else + GUI.Box(position, "", (GUIStyle) + "flow overlay box"); +#endif + position.y += 4; + // Using BeginProperty / EndProperty on the parent property means that + // prefab override logic works on the entire property. + property.serializedObject.Update(); + EditorGUI.BeginProperty(position, label, property); + // Draw label + Rect pos = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); + + Rect targetRect = new Rect(pos.x, pos.y, pos.width, EditorGUIUtility.singleLineHeight); + + // Get target + SerializedProperty targetProp = property.FindPropertyRelative("_target"); + object target = targetProp.objectReferenceValue; + if (attribute != null && attribute is TargetConstraintAttribute) { + Type targetType = (attribute as TargetConstraintAttribute).targetType; + EditorGUI.ObjectField(targetRect, targetProp, targetType, GUIContent.none); + } else EditorGUI.PropertyField(targetRect, targetProp, GUIContent.none); + + if (target == null) { + Rect helpBoxRect = new Rect(position.x + 8, targetRect.max.y + EditorGUIUtility.standardVerticalSpacing, position.width - 16, EditorGUIUtility.singleLineHeight); + string msg = "Call not set. Execution will be slower."; + EditorGUI.HelpBox(helpBoxRect, msg, MessageType.Warning); + } else if (target is MonoScript) { + Rect helpBoxRect = new Rect(position.x + 8, targetRect.max.y + EditorGUIUtility.standardVerticalSpacing, position.width - 16, EditorGUIUtility.singleLineHeight + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing); + string msg = "Assign a GameObject, Component or a ScriptableObject, not a script."; + EditorGUI.HelpBox(helpBoxRect, msg, MessageType.Warning); + } else { + int indent = EditorGUI.indentLevel; + EditorGUI.indentLevel++; + + // Get method name + SerializedProperty methodProp = property.FindPropertyRelative("_methodName"); + string methodName = methodProp.stringValue; + + // Get args + SerializedProperty argProps = property.FindPropertyRelative("_args"); + Type[] argTypes = GetArgTypes(argProps); + + // Get dynamic + SerializedProperty dynamicProp = property.FindPropertyRelative("_dynamic"); + bool dynamic = dynamicProp.boolValue; + + // Get active method + MethodInfo activeMethod = GetMethod(target, methodName, argTypes); + + GUIContent methodlabel = new GUIContent("n/a"); + if (activeMethod != null) methodlabel = new GUIContent(PrettifyMethod(activeMethod)); + else if (!string.IsNullOrEmpty(methodName)) methodlabel = new GUIContent("Missing (" + PrettifyMethod(methodName, argTypes) + ")"); + + Rect methodRect = new Rect(position.x, targetRect.max.y + EditorGUIUtility.standardVerticalSpacing, position.width, EditorGUIUtility.singleLineHeight); + + // Method select button + pos = EditorGUI.PrefixLabel(methodRect, GUIUtility.GetControlID(FocusType.Passive), new GUIContent(dynamic ? "Method (dynamic)" : "Method")); + if (EditorGUI.DropdownButton(pos, methodlabel, FocusType.Keyboard)) { + MethodSelector(property); + } + + if (activeMethod != null && !dynamic) { + // Args + ParameterInfo[] activeParameters = activeMethod.GetParameters(); + Rect argRect = new Rect(position.x, methodRect.max.y + EditorGUIUtility.standardVerticalSpacing, position.width, EditorGUIUtility.singleLineHeight); + string[] types = new string[argProps.arraySize]; + for (int i = 0; i < types.Length; i++) { + SerializedProperty argProp = argProps.FindPropertyRelative("Array.data[" + i + "]"); + GUIContent argLabel = new GUIContent(ObjectNames.NicifyVariableName(activeParameters[i].Name)); + + EditorGUI.BeginChangeCheck(); + switch ((Arg.ArgType) argProp.FindPropertyRelative("argType").enumValueIndex) { + case Arg.ArgType.Bool: + EditorGUI.PropertyField(argRect, argProp.FindPropertyRelative("boolValue"), argLabel); + break; + case Arg.ArgType.Int: + EditorGUI.PropertyField(argRect, argProp.FindPropertyRelative("intValue"), argLabel); + break; + case Arg.ArgType.Float: + EditorGUI.PropertyField(argRect, argProp.FindPropertyRelative("floatValue"), argLabel); + break; + case Arg.ArgType.String: + EditorGUI.PropertyField(argRect, argProp.FindPropertyRelative("stringValue"), argLabel); + break; + case Arg.ArgType.Object: + SerializedProperty typeProp = argProp.FindPropertyRelative("_typeName"); + SerializedProperty objProp = argProp.FindPropertyRelative("objectValue"); + + if (typeProp != null) { + Type objType = Type.GetType(typeProp.stringValue, false); + EditorGUI.BeginChangeCheck(); + Object obj = objProp.objectReferenceValue; + obj = EditorGUI.ObjectField(argRect, argLabel, obj, objType, true); + if (EditorGUI.EndChangeCheck()) { + objProp.objectReferenceValue = obj; + } + } else { + EditorGUI.PropertyField(argRect, objProp, argLabel); + } + break; + } + if (EditorGUI.EndChangeCheck()) { + property.FindPropertyRelative("dirty").boolValue = true; + } + argRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; + } + } + EditorGUI.indentLevel = indent; + } + + // Set indent back to what it was + EditorGUI.EndProperty(); + property.serializedObject.ApplyModifiedProperties(); + } + + private class MenuItem { + public GenericMenu.MenuFunction action; + public string path; + public GUIContent label; + + public MenuItem(string path, string name, GenericMenu.MenuFunction action) { + this.action = action; + this.label = new GUIContent(path + '/' + name); + this.path = path; + } + } + void MethodSelector(SerializedProperty property) { + // Return type constraint + Type returnType = null; + // Arg type constraint + Type[] argTypes = new Type[0]; + + // Get return type and argument constraints + SerializableCallbackBase dummy = GetDummyFunction(property); + Type[] genericTypes = dummy.GetType().BaseType.GetGenericArguments(); + // SerializableEventBase is always void return type + if (dummy is SerializableEventBase) { + returnType = typeof(void); + if (genericTypes.Length > 0) { + argTypes = new Type[genericTypes.Length]; + Array.Copy(genericTypes, argTypes, genericTypes.Length); + } + } else { + if (genericTypes != null && genericTypes.Length > 0) { + // The last generic argument is the return type + returnType = genericTypes[genericTypes.Length - 1]; + if (genericTypes.Length > 1) { + argTypes = new Type[genericTypes.Length - 1]; + Array.Copy(genericTypes, argTypes, genericTypes.Length - 1); + } + } + } + + SerializedProperty targetProp = property.FindPropertyRelative("_target"); + + List<MenuItem> dynamicItems = new List<MenuItem>(); + List<MenuItem> staticItems = new List<MenuItem>(); + + List<Object> targets = new List<Object>() { targetProp.objectReferenceValue }; + + if (targets[0] is Component) { + targets = (targets[0] as Component).gameObject.GetComponents<Component>().ToList<Object>(); + targets.Add((targetProp.objectReferenceValue as Component).gameObject); + } else if (targets[0] is GameObject) { + targets = (targets[0] as GameObject).GetComponents<Component>().ToList<Object>(); + targets.Add(targetProp.objectReferenceValue as GameObject); + } + for (int c = 0; c < targets.Count; c++) { + Object t = targets[c]; + MethodInfo[] methods = targets[c].GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); + + for (int i = 0; i < methods.Length; i++) { + MethodInfo method = methods[i]; + + // Skip methods with wrong return type + if (returnType != null && method.ReturnType != returnType) continue; + // Skip methods with null return type + // if (method.ReturnType == typeof(void)) continue; + // Skip generic methods + if (method.IsGenericMethod) continue; + + Type[] parms = method.GetParameters().Select(x => x.ParameterType).ToArray(); + + // Skip methods with more than 4 args + if (parms.Length > 4) continue; + // Skip methods with unsupported args + if (parms.Any(x => !Arg.IsSupported(x))) continue; + + string methodPrettyName = PrettifyMethod(methods[i]); + staticItems.Add(new MenuItem(targets[c].GetType().Name + "/" + methods[i].DeclaringType.Name, methodPrettyName, () => SetMethod(property, t, method, false))); + + // Skip methods with wrong constrained args + if (argTypes.Length == 0 || !Enumerable.SequenceEqual(argTypes, parms)) continue; + + dynamicItems.Add(new MenuItem(targets[c].GetType().Name + "/" + methods[i].DeclaringType.Name, methods[i].Name, () => SetMethod(property, t, method, true))); + } + } + + // Construct and display context menu + GenericMenu menu = new GenericMenu(); + if (dynamicItems.Count > 0) { + string[] paths = dynamicItems.GroupBy(x => x.path).Select(x => x.First().path).ToArray(); + foreach (string path in paths) { + menu.AddItem(new GUIContent(path + "/Dynamic " + PrettifyTypes(argTypes)), false, null); + } + for (int i = 0; i < dynamicItems.Count; i++) { + menu.AddItem(dynamicItems[i].label, false, dynamicItems[i].action); + } + foreach (string path in paths) { + menu.AddItem(new GUIContent(path + "/ "), false, null); + menu.AddItem(new GUIContent(path + "/Static parameters"), false, null); + } + } + for (int i = 0; i < staticItems.Count; i++) { + menu.AddItem(staticItems[i].label, false, staticItems[i].action); + } + if (menu.GetItemCount() == 0) menu.AddDisabledItem(new GUIContent("No methods with return type '" + GetTypeName(returnType) + "'")); + menu.ShowAsContext(); + } + + string PrettifyMethod(string methodName, Type[] parmTypes) { + string parmnames = PrettifyTypes(parmTypes); + return methodName + "(" + parmnames + ")"; + } + + string PrettifyMethod(MethodInfo methodInfo) { + if (methodInfo == null) throw new ArgumentNullException("methodInfo"); + ParameterInfo[] parms = methodInfo.GetParameters(); + string parmnames = PrettifyTypes(parms.Select(x => x.ParameterType).ToArray()); + return GetTypeName(methodInfo.ReturnParameter.ParameterType) + " " + methodInfo.Name + "(" + parmnames + ")"; + } + + string PrettifyTypes(Type[] types) { + if (types == null) throw new ArgumentNullException("types"); + return string.Join(", ", types.Select(x => GetTypeName(x)).ToArray()); + } + + MethodInfo GetMethod(object target, string methodName, Type[] types) { + MethodInfo activeMethod = target.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, CallingConventions.Any, types, null); + return activeMethod; + } + + private Type[] GetArgTypes(SerializedProperty argsProp) { + Type[] types = new Type[argsProp.arraySize]; + for (int i = 0; i < argsProp.arraySize; i++) { + SerializedProperty argProp = argsProp.GetArrayElementAtIndex(i); + SerializedProperty typeNameProp = argProp.FindPropertyRelative("_typeName"); + if (typeNameProp != null) types[i] = Type.GetType(typeNameProp.stringValue, false); + if (types[i] == null) types[i] = Arg.RealType((Arg.ArgType) argProp.FindPropertyRelative("argType").enumValueIndex); + } + return types; + } + + private void SetMethod(SerializedProperty property, UnityEngine.Object target, MethodInfo methodInfo, bool dynamic) { + SerializedProperty targetProp = property.FindPropertyRelative("_target"); + targetProp.objectReferenceValue = target; + SerializedProperty methodProp = property.FindPropertyRelative("_methodName"); + methodProp.stringValue = methodInfo.Name; + SerializedProperty dynamicProp = property.FindPropertyRelative("_dynamic"); + dynamicProp.boolValue = dynamic; + SerializedProperty argProp = property.FindPropertyRelative("_args"); + ParameterInfo[] parameters = methodInfo.GetParameters(); + argProp.arraySize = parameters.Length; + for (int i = 0; i < parameters.Length; i++) { + argProp.FindPropertyRelative("Array.data[" + i + "].argType").enumValueIndex = (int) Arg.FromRealType(parameters[i].ParameterType); + argProp.FindPropertyRelative("Array.data[" + i + "]._typeName").stringValue = parameters[i].ParameterType.AssemblyQualifiedName; + } + property.FindPropertyRelative("dirty").boolValue = true; + property.serializedObject.ApplyModifiedProperties(); + property.serializedObject.Update(); + } + + private static string GetTypeName(Type t) { + if (t == typeof(int)) return "int"; + else if (t == typeof(float)) return "float"; + else if (t == typeof(string)) return "string"; + else if (t == typeof(bool)) return "bool"; + else if (t == typeof(void)) return "void"; + else return t.Name; + } + + public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { + float lineheight = EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight; + SerializedProperty targetProp = property.FindPropertyRelative("_target"); + SerializedProperty argProps = property.FindPropertyRelative("_args"); + SerializedProperty dynamicProp = property.FindPropertyRelative("_dynamic"); + float height = lineheight + lineheight; + if (targetProp.objectReferenceValue != null && targetProp.objectReferenceValue is MonoScript) height += lineheight; + else if (targetProp.objectReferenceValue != null && !dynamicProp.boolValue) height += argProps.arraySize * lineheight; + height += 8; + return height; + } + + private static SerializableCallbackBase GetDummyFunction(SerializedProperty prop) { + string stringValue = prop.FindPropertyRelative("_typeName").stringValue; + Type type = Type.GetType(stringValue, false); + SerializableCallbackBase result; + if (type == null) { + return null; + } else { + result = (Activator.CreateInstance(type) as SerializableCallbackBase); + } + return result; + } +} diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor/SerializableCallbackDrawer.cs.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor/SerializableCallbackDrawer.cs.meta new file mode 100644 index 0000000..2654dc4 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor/SerializableCallbackDrawer.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 65cd8a53f6f9cf04ea4aa8e2743322fd +timeCreated: 1513845239 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor/Siccity.SerializableCallback.Editor.asmdef b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor/Siccity.SerializableCallback.Editor.asmdef new file mode 100644 index 0000000..a68f07b --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor/Siccity.SerializableCallback.Editor.asmdef @@ -0,0 +1,17 @@ +{ + "name": "Siccity.SerializableCallback.Editor", + "references": [ + "GUID:11a2306c728a4b843a652ec6c2f142bc" + ], + "includePlatforms": [ + "Editor" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +}
\ No newline at end of file diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor/Siccity.SerializableCallback.Editor.asmdef.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor/Siccity.SerializableCallback.Editor.asmdef.meta new file mode 100644 index 0000000..3bd477a --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Editor/Siccity.SerializableCallback.Editor.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: a49fd1d25e23fd14e85d1cbf342bb06f +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/LICENSE.md b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/LICENSE.md new file mode 100644 index 0000000..4356107 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/LICENSE.md @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2017 Thor Brigsted + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/LICENSE.md.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/LICENSE.md.meta new file mode 100644 index 0000000..918fbad --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/LICENSE.md.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 664adef14643bfe44a13ce197b3a540c +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/README.md b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/README.md new file mode 100644 index 0000000..a9a7c62 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/README.md @@ -0,0 +1,49 @@ +### SerializableCallback +Lets you drag-and-drop methods with or without return values / parameters in the Unity inspector. +It uses expression trees and reflection to cache a delegate on first execution. + +Usage is identical to UnityEvent + + +```csharp +public class MyClass : MonoBehaviour { + //These fields are shown in the inspector + public SerializableCallback callback; // supports all non-void return types + public Condition condition; // supports bool return types only + public GetProduct getProduct; // supports MyProduct return types only + + void Start() { + // Callbacks can be invoked with or without parameters, and with different types + Debug.Log(callback.Invoke()); // returns object + Debug.Log(condition.Invoke()); // returns bool + Debug.Log(getProduct.Invoke(2)); // returns MyProduct + } + + // As with UnityEvents, custom callbacks must have a non-generic wrapper class marked as [Serializable] in order to be serialized by Unity + [Serializable] + public class Condition : SerializableCallback<bool> {} + + // Last generic type parameter is the return type, staying consistent with System.Func + [Serializable] + public class GetProduct : SerializableCallback<int, MyProduct> {} +} +``` + +| Performance (100000 iterations) | Time | +| -------------------------------------------- | --------- | +| bool Method(float) | 00.00304s | +| SerializedCallback<float, bool> (Persistent) | 00.01026s | +| SerializedCallback<float, bool> (Dynamic) | 00.00797s | + + +### Installing with Unity Package Manager +To install this project as a dependency using the Unity Package Manager, +add the following line to your project's `manifest.json`: + +``` +"com.github.siccity.serializablecallback": "git+https://github.com/Siccity/SerializableCallback.git" +``` + +Join the [Discord](https://discord.gg/qgPrHv4 "Join Discord server") server to leave feedback or get support. + + diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/README.md.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/README.md.meta new file mode 100644 index 0000000..b840547 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/README.md.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: b5a8c7667bbebba4ca6545be57800251 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime.meta new file mode 100644 index 0000000..0568598 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a6c274949c3a3494db6ab4e33e996353 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Attributes.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Attributes.meta new file mode 100644 index 0000000..384c282 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Attributes.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 4fd350e49301e4445a1cbe580fe9eec7 +folderAsset: yes +timeCreated: 1536132750 +licenseType: Pro +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Attributes/TargetConstraintAttribute.cs b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Attributes/TargetConstraintAttribute.cs new file mode 100644 index 0000000..e457e06 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Attributes/TargetConstraintAttribute.cs @@ -0,0 +1,12 @@ +using UnityEngine; +using System; + +/// <summary> Add to fields of your class extending SerializableCallbackBase<T,..> to limit which types can be assigned to it. </summary> +public class TargetConstraintAttribute : PropertyAttribute { + public Type targetType; + + /// <summary> Add to fields of your class extending SerializableCallbackBase<T,..> to limit which types can be assigned to it. </summary> + public TargetConstraintAttribute(Type targetType) { + this.targetType = targetType; + } +} diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Attributes/TargetConstraintAttribute.cs.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Attributes/TargetConstraintAttribute.cs.meta new file mode 100644 index 0000000..1345b58 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Attributes/TargetConstraintAttribute.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 4b9b7d20cf54ac6489b84d21c29a4c69 +timeCreated: 1536132736 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableCallback.cs b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableCallback.cs new file mode 100644 index 0000000..2819874 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableCallback.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class InvokableCallback<TReturn> : InvokableCallbackBase<TReturn> { + + public Func<TReturn> func; + + public TReturn Invoke() { + return func(); + } + + public override TReturn Invoke(params object[] args) { + return func(); + } + + /// <summary> Constructor </summary> + public InvokableCallback(object target, string methodName) { + if (target == null || string.IsNullOrEmpty(methodName)) { + func = () => default(TReturn); + } else { + func = (System.Func<TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<TReturn>), target, methodName); + } + } +} + +public class InvokableCallback<T0, TReturn> : InvokableCallbackBase<TReturn> { + + public Func<T0, TReturn> func; + + public TReturn Invoke(T0 arg0) { + return func(arg0); + } + + public override TReturn Invoke(params object[] args) { + // Convert from special "unity-nulls" to true null + if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null; + return func((T0) args[0]); + } + + /// <summary> Constructor </summary> + public InvokableCallback(object target, string methodName) { + if (target == null || string.IsNullOrEmpty(methodName)) { + func = x => default(TReturn); + } else { + func = (System.Func<T0, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, TReturn>), target, methodName); + } + } +} + +public class InvokableCallback<T0, T1, TReturn> : InvokableCallbackBase<TReturn> { + + public Func<T0, T1, TReturn> func; + + public TReturn Invoke(T0 arg0, T1 arg1) { + return func(arg0, arg1); + } + + public override TReturn Invoke(params object[] args) { + // Convert from special "unity-nulls" to true null + if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null; + if (args[1] is UnityEngine.Object && (UnityEngine.Object) args[1] == null) args[1] = null; + return func((T0) args[0], (T1) args[1]); + } + + /// <summary> Constructor </summary> + public InvokableCallback(object target, string methodName) { + if (target == null || string.IsNullOrEmpty(methodName)) { + func = (x, y) => default(TReturn); + } else { + func = (System.Func<T0, T1, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, TReturn>), target, methodName); + } + } +} + +public class InvokableCallback<T0, T1, T2, TReturn> : InvokableCallbackBase<TReturn> { + + public Func<T0, T1, T2, TReturn> func; + + public TReturn Invoke(T0 arg0, T1 arg1, T2 arg2) { + return func(arg0, arg1, arg2); + } + + public override TReturn Invoke(params object[] args) { + // Convert from special "unity-nulls" to true null + if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null; + if (args[1] is UnityEngine.Object && (UnityEngine.Object) args[1] == null) args[1] = null; + if (args[2] is UnityEngine.Object && (UnityEngine.Object) args[2] == null) args[2] = null; + return func((T0) args[0], (T1) args[1], (T2) args[2]); + } + + /// <summary> Constructor </summary> + public InvokableCallback(object target, string methodName) { + if (target == null || string.IsNullOrEmpty(methodName)) { + func = (x, y, z) => default(TReturn); + } else { + func = (System.Func<T0, T1, T2, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, T2, TReturn>), target, methodName); + } + } +} + +public class InvokableCallback<T0, T1, T2, T3, TReturn> : InvokableCallbackBase<TReturn> { + + public Func<T0, T1, T2, T3, TReturn> func; + + public TReturn Invoke(T0 arg0, T1 arg1, T2 arg2, T3 arg3) { + return func(arg0, arg1, arg2, arg3); + } + + public override TReturn Invoke(params object[] args) { + // Convert from special "unity-nulls" to true null + if (args[0] is UnityEngine.Object && (UnityEngine.Object) args[0] == null) args[0] = null; + if (args[1] is UnityEngine.Object && (UnityEngine.Object) args[1] == null) args[1] = null; + if (args[2] is UnityEngine.Object && (UnityEngine.Object) args[2] == null) args[2] = null; + if (args[3] is UnityEngine.Object && (UnityEngine.Object) args[3] == null) args[3] = null; + return func((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3]); + } + + /// <summary> Constructor </summary> + public InvokableCallback(object target, string methodName) { + if (target == null || string.IsNullOrEmpty(methodName)) { + func = (x, y, z, w) => default(TReturn); + } else { + func = (System.Func<T0, T1, T2, T3, TReturn>) System.Delegate.CreateDelegate(typeof(System.Func<T0, T1, T2, T3, TReturn>), target, methodName); + } + } +}
\ No newline at end of file diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableCallback.cs.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableCallback.cs.meta new file mode 100644 index 0000000..2468e0a --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableCallback.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: ca67a6ce15814b743b8e123c175ce649 +timeCreated: 1515587326 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableCallbackBase.cs b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableCallbackBase.cs new file mode 100644 index 0000000..e0cd2d1 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableCallbackBase.cs @@ -0,0 +1,3 @@ +public abstract class InvokableCallbackBase<TReturn> { + public abstract TReturn Invoke(params object[] args); +}
\ No newline at end of file diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableCallbackBase.cs.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableCallbackBase.cs.meta new file mode 100644 index 0000000..d5e608c --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableCallbackBase.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 9147523dbcc2f2f46878180063444518 +timeCreated: 1515583421 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableEvent.cs b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableEvent.cs new file mode 100644 index 0000000..dd9db65 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableEvent.cs @@ -0,0 +1,111 @@ +using System; + +public class InvokableEvent : InvokableEventBase { + + public System.Action action; + + public void Invoke() { + action(); + } + + public override void Invoke(params object[] args) { + action(); + } + + /// <summary> Constructor </summary> + public InvokableEvent(object target, string methodName) { + if (target == null || string.IsNullOrEmpty(methodName)) { + action = () => { }; + } else { + action = (System.Action) System.Delegate.CreateDelegate(typeof(System.Action), target, methodName); + } + } +} + +public class InvokableEvent<T0> : InvokableEventBase { + + public Action<T0> action; + + public void Invoke(T0 arg0) { + action(arg0); + } + + public override void Invoke(params object[] args) { + action((T0) args[0]); + } + + /// <summary> Constructor </summary> + public InvokableEvent(object target, string methodName) { + if (target == null || string.IsNullOrEmpty(methodName)) { + action = x => { }; + } else { + action = (System.Action<T0>) System.Delegate.CreateDelegate(typeof(System.Action<T0>), target, methodName); + } + } +} + +public class InvokableEvent<T0, T1> : InvokableEventBase { + + public Action<T0, T1> action; + + public void Invoke(T0 arg0, T1 arg1) { + action(arg0, arg1); + } + + public override void Invoke(params object[] args) { + action((T0) args[0], (T1) args[1]); + } + + /// <summary> Constructor </summary> + public InvokableEvent(object target, string methodName) { + if (target == null || string.IsNullOrEmpty(methodName)) { + action = (x, y) => { }; + } else { + action = (System.Action<T0, T1>) System.Delegate.CreateDelegate(typeof(System.Action<T0, T1>), target, methodName); + } + } +} + +public class InvokableEvent<T0, T1, T2> : InvokableEventBase { + + public Action<T0, T1, T2> action; + + public void Invoke(T0 arg0, T1 arg1, T2 arg2) { + action(arg0, arg1, arg2); + } + + public override void Invoke(params object[] args) { + action((T0) args[0], (T1) args[1], (T2) args[2]); + } + + /// <summary> Constructor </summary> + public InvokableEvent(object target, string methodName) { + if (target == null || string.IsNullOrEmpty(methodName)) { + action = (x, y, z) => { }; + } else { + action = (System.Action<T0, T1, T2>) System.Delegate.CreateDelegate(typeof(System.Action<T0, T1, T2>), target, methodName); + } + } +} + +public class InvokableEvent<T0, T1, T2, T3> : InvokableEventBase { + + public Action<T0, T1, T2, T3> action; + + public void Invoke(T0 arg0, T1 arg1, T2 arg2, T3 arg3) { + action(arg0, arg1, arg2, arg3); + } + + public override void Invoke(params object[] args) { + action((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3]); + } + + /// <summary> Constructor </summary> + public InvokableEvent(object target, string methodName) { + if (target == null || string.IsNullOrEmpty(methodName)) { + action = (x, y, z, w) => { }; + } else { + action = (System.Action<T0, T1, T2, T3>) System.Delegate.CreateDelegate(typeof(System.Action<T0, T1, T2, T3>), target, methodName); + } + } +} diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableEvent.cs.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableEvent.cs.meta new file mode 100644 index 0000000..7754aaa --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableEvent.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 51f8a42e6f95b694cb28ca372e32aef3 +timeCreated: 1515748293 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableEventBase.cs b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableEventBase.cs new file mode 100644 index 0000000..003ab7c --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableEventBase.cs @@ -0,0 +1,3 @@ +public abstract class InvokableEventBase { + public abstract void Invoke(params object[] args); +}
\ No newline at end of file diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableEventBase.cs.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableEventBase.cs.meta new file mode 100644 index 0000000..9696ff5 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/InvokableEventBase.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: e958262ec96656d459752d5df5faa52e +timeCreated: 1515748285 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableCallback.cs b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableCallback.cs new file mode 100644 index 0000000..e58fa3b --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableCallback.cs @@ -0,0 +1,124 @@ +using System; +using System.Linq.Expressions; +using System.Reflection; +using UnityEngine; + +public abstract class SerializableCallback<TReturn> : SerializableCallbackBase<TReturn> { + public TReturn Invoke() { + if (func == null) Cache(); + if (_dynamic) { + InvokableCallback<TReturn> call = func as InvokableCallback<TReturn>; + return call.Invoke(); + } else { + return func.Invoke(Args); + } + } + + protected override void Cache() { + if (_target == null || string.IsNullOrEmpty(_methodName)) { + func = new InvokableCallback<TReturn>(null, null); + } else { + if (_dynamic) { + func = new InvokableCallback<TReturn>(target, methodName); + } else { + func = GetPersistentMethod(); + } + } + } +} + +public abstract class SerializableCallback<T0, TReturn> : SerializableCallbackBase<TReturn> { + public TReturn Invoke(T0 arg0) { + if (func == null) Cache(); + if (_dynamic) { + InvokableCallback<T0, TReturn> call = func as InvokableCallback<T0, TReturn>; + return call.Invoke(arg0); + } else { + return func.Invoke(Args); + } + } + + protected override void Cache() { + if (_target == null || string.IsNullOrEmpty(_methodName)) { + func = new InvokableCallback<T0, TReturn>(null, null); + } else { + if (_dynamic) { + func = new InvokableCallback<T0, TReturn>(target, methodName); + } else { + func = GetPersistentMethod(); + } + } + } +} + +public abstract class SerializableCallback<T0, T1, TReturn> : SerializableCallbackBase<TReturn> { + public TReturn Invoke(T0 arg0, T1 arg1) { + if (func == null) Cache(); + if (_dynamic) { + InvokableCallback<T0, T1, TReturn> call = func as InvokableCallback<T0, T1, TReturn>; + return call.Invoke(arg0, arg1); + } else { + return func.Invoke(Args); + } + } + + protected override void Cache() { + if (_target == null || string.IsNullOrEmpty(_methodName)) { + func = new InvokableCallback<T0, T1, TReturn>(null, null); + } else { + if (_dynamic) { + func = new InvokableCallback<T0, T1, TReturn>(target, methodName); + } else { + func = GetPersistentMethod(); + } + } + } +} + +public abstract class SerializableCallback<T0, T1, T2, TReturn> : SerializableCallbackBase<TReturn> { + public TReturn Invoke(T0 arg0, T1 arg1, T2 arg2) { + if (func == null) Cache(); + if (_dynamic) { + InvokableCallback<T0, T1, T2, TReturn> call = func as InvokableCallback<T0, T1, T2, TReturn>; + return call.Invoke(arg0, arg1, arg2); + } else { + return func.Invoke(Args); + } + } + + protected override void Cache() { + if (_target == null || string.IsNullOrEmpty(_methodName)) { + func = new InvokableCallback<T0, T1, T2, TReturn>(null, null); + } else { + if (_dynamic) { + func = new InvokableCallback<T0, T1, T2, TReturn>(target, methodName); + } else { + func = GetPersistentMethod(); + } + } + } +} + +public abstract class SerializableCallback<T0, T1, T2, T3, TReturn> : SerializableCallbackBase<TReturn> { + public TReturn Invoke(T0 arg0, T1 arg1, T2 arg2, T3 arg3) { + if (func == null) Cache(); + if (_dynamic) { + InvokableCallback<T0, T1, T2, T3, TReturn> call = func as InvokableCallback<T0, T1, T2, T3, TReturn>; + return call.Invoke(arg0, arg1, arg2, arg3); + } else { + return func.Invoke(Args); + } + } + + protected override void Cache() { + if (_target == null || string.IsNullOrEmpty(_methodName)) { + func = new InvokableCallback<T0, T1, T2, T3, TReturn>(null, null); + } else { + if (_dynamic) { + func = new InvokableCallback<T0, T1, T2, T3, TReturn>(target, methodName); + } else { + func = GetPersistentMethod(); + } + } + } +}
\ No newline at end of file diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableCallback.cs.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableCallback.cs.meta new file mode 100644 index 0000000..0969bde --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableCallback.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: dc926a8278d45964e842b3ee4b9e593b +timeCreated: 1513845239 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableCallbackBase.cs b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableCallbackBase.cs new file mode 100644 index 0000000..34300d7 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableCallbackBase.cs @@ -0,0 +1,167 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using UnityEngine; +using Object = UnityEngine.Object; + +public abstract class SerializableCallbackBase<TReturn> : SerializableCallbackBase { + public InvokableCallbackBase<TReturn> func; + + public override void ClearCache() { + base.ClearCache(); + func = null; + } + + protected InvokableCallbackBase<TReturn> GetPersistentMethod() { + Type[] types = new Type[ArgRealTypes.Length + 1]; + Array.Copy(ArgRealTypes, types, ArgRealTypes.Length); + types[types.Length - 1] = typeof(TReturn); + + Type genericType = null; + switch (types.Length) { + case 1: + genericType = typeof(InvokableCallback<>).MakeGenericType(types); + break; + case 2: + genericType = typeof(InvokableCallback<,>).MakeGenericType(types); + break; + case 3: + genericType = typeof(InvokableCallback<, ,>).MakeGenericType(types); + break; + case 4: + genericType = typeof(InvokableCallback<, , ,>).MakeGenericType(types); + break; + case 5: + genericType = typeof(InvokableCallback<, , , ,>).MakeGenericType(types); + break; + default: + throw new ArgumentException(types.Length + "args"); + } + return Activator.CreateInstance(genericType, new object[] { target, methodName }) as InvokableCallbackBase<TReturn>; + } +} + +/// <summary> An inspector-friendly serializable function </summary> +[System.Serializable] +public abstract class SerializableCallbackBase : ISerializationCallbackReceiver { + + /// <summary> Target object </summary> + public Object target { get { return _target; } set { _target = value; ClearCache(); } } + /// <summary> Target method name </summary> + public string methodName { get { return _methodName; } set { _methodName = value; ClearCache(); } } + public object[] Args { get { return args != null ? args : args = _args.Select(x => x.GetValue()).ToArray(); } } + public object[] args; + public Type[] ArgTypes { get { return argTypes != null ? argTypes : argTypes = _args.Select(x => Arg.RealType(x.argType)).ToArray(); } } + public Type[] argTypes; + public Type[] ArgRealTypes { get { return argRealTypes != null ? argRealTypes : argRealTypes = _args.Select(x => Type.GetType(x._typeName)).ToArray(); } } + public Type[] argRealTypes; + public bool dynamic { get { return _dynamic; } set { _dynamic = value; ClearCache(); } } + + [SerializeField] protected Object _target; + [SerializeField] protected string _methodName; + [SerializeField] protected Arg[] _args; + [SerializeField] protected bool _dynamic; +#pragma warning disable 0414 + [SerializeField] private string _typeName; +#pragma warning restore 0414 + + [SerializeField] private bool dirty; + +#if UNITY_EDITOR + protected SerializableCallbackBase() { + _typeName = base.GetType().AssemblyQualifiedName; + } +#endif + + public virtual void ClearCache() { + argTypes = null; + args = null; + } + + public void SetMethod(Object target, string methodName, bool dynamic, params Arg[] args) { + _target = target; + _methodName = methodName; + _dynamic = dynamic; + _args = args; + ClearCache(); + } + + protected abstract void Cache(); + + public void OnBeforeSerialize() { +#if UNITY_EDITOR + if (dirty) { ClearCache(); dirty = false; } +#endif + } + + public void OnAfterDeserialize() { +#if UNITY_EDITOR + _typeName = base.GetType().AssemblyQualifiedName; +#endif + } +} + +[System.Serializable] +public struct Arg { + public enum ArgType { Unsupported, Bool, Int, Float, String, Object } + public bool boolValue; + public int intValue; + public float floatValue; + public string stringValue; + public Object objectValue; + public ArgType argType; + public string _typeName; + + public object GetValue() { + return GetValue(argType); + } + + public object GetValue(ArgType type) { + switch (type) { + case ArgType.Bool: + return boolValue; + case ArgType.Int: + return intValue; + case ArgType.Float: + return floatValue; + case ArgType.String: + return stringValue; + case ArgType.Object: + return objectValue; + default: + return null; + } + } + + public static Type RealType(ArgType type) { + switch (type) { + case ArgType.Bool: + return typeof(bool); + case ArgType.Int: + return typeof(int); + case ArgType.Float: + return typeof(float); + case ArgType.String: + return typeof(string); + case ArgType.Object: + return typeof(Object); + default: + return null; + } + } + + public static ArgType FromRealType(Type type) { + if (type == typeof(bool)) return ArgType.Bool; + else if (type == typeof(int)) return ArgType.Int; + else if (type == typeof(float)) return ArgType.Float; + else if (type == typeof(String)) return ArgType.String; + else if (typeof(Object).IsAssignableFrom(type)) return ArgType.Object; + else return ArgType.Unsupported; + } + + public static bool IsSupported(Type type) { + return FromRealType(type) != ArgType.Unsupported; + } +}
\ No newline at end of file diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableCallbackBase.cs.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableCallbackBase.cs.meta new file mode 100644 index 0000000..8d7d1db --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableCallbackBase.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 7281861ef496a34429213165496d8cf3 +timeCreated: 1513845239 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableEvent.cs b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableEvent.cs new file mode 100644 index 0000000..9a65ef6 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableEvent.cs @@ -0,0 +1,120 @@ +[System.Serializable] +public class SerializableEvent : SerializableEventBase { + public void Invoke() { + if (invokable == null) Cache(); + if (_dynamic) { + InvokableEvent call = invokable as InvokableEvent; + call.Invoke(); + } else { + invokable.Invoke(Args); + } + } + + protected override void Cache() { + if (_target == null || string.IsNullOrEmpty(_methodName)) { + invokable = new InvokableEvent(null, null); + } else { + if (_dynamic) { + invokable = new InvokableEvent(target, methodName); + } else { + invokable = GetPersistentMethod(); + } + } + } +} + +public abstract class SerializableEvent<T0> : SerializableEventBase { + public void Invoke(T0 arg0) { + if (invokable == null) Cache(); + if (_dynamic) { + InvokableEvent<T0> call = invokable as InvokableEvent<T0>; + call.Invoke(arg0); + } else { + invokable.Invoke(Args); + } + } + + protected override void Cache() { + if (_target == null || string.IsNullOrEmpty(_methodName)) { + invokable = new InvokableEvent<T0>(null, null); + } else { + if (_dynamic) { + invokable = new InvokableEvent<T0>(target, methodName); + } else { + invokable = GetPersistentMethod(); + } + } + } +} + +public abstract class SerializableEvent<T0, T1> : SerializableEventBase { + public void Invoke(T0 arg0, T1 arg1) { + if (invokable == null) Cache(); + if (_dynamic) { + InvokableEvent<T0, T1> call = invokable as InvokableEvent<T0, T1>; + call.Invoke(arg0, arg1); + } else { + invokable.Invoke(Args); + } + } + + protected override void Cache() { + if (_target == null || string.IsNullOrEmpty(_methodName)) { + invokable = new InvokableEvent<T0, T1>(null, null); + } else { + if (_dynamic) { + invokable = new InvokableEvent<T0, T1>(target, methodName); + } else { + invokable = GetPersistentMethod(); + } + } + } +} + +public abstract class SerializableEvent<T0, T1, T2> : SerializableEventBase { + public void Invoke(T0 arg0, T1 arg1, T2 arg2) { + if (invokable == null) Cache(); + if (_dynamic) { + InvokableEvent<T0, T1, T2> call = invokable as InvokableEvent<T0, T1, T2>; + call.Invoke(arg0, arg1, arg2); + } else { + invokable.Invoke(Args); + } + } + + protected override void Cache() { + if (_target == null || string.IsNullOrEmpty(_methodName)) { + invokable = new InvokableEvent<T0, T1, T2>(null, null); + } else { + if (_dynamic) { + invokable = new InvokableEvent<T0, T1, T2>(target, methodName); + } else { + invokable = GetPersistentMethod(); + } + } + } +} + +public abstract class SerializableEvent<T0, T1, T2, T3> : SerializableEventBase { + public void Invoke(T0 arg0, T1 arg1, T2 arg2, T3 arg3) { + if (invokable == null) Cache(); + if (_dynamic) { + InvokableEvent<T0, T1, T2, T3> call = invokable as InvokableEvent<T0, T1, T2, T3>; + call.Invoke(arg0, arg1, arg2, arg3); + } else { + invokable.Invoke(Args); + } + } + + protected override void Cache() { + if (_target == null || string.IsNullOrEmpty(_methodName)) { + invokable = new InvokableEvent<T0, T1, T2, T3>(null, null); + } else { + if (_dynamic) { + invokable = new InvokableEvent<T0, T1, T2, T3>(target, methodName); + } else { + invokable = GetPersistentMethod(); + } + } + } +}
\ No newline at end of file diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableEvent.cs.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableEvent.cs.meta new file mode 100644 index 0000000..5c5bdca --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableEvent.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 874a1a13ec4740a4387fe34d59931868 +timeCreated: 1515747693 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableEventBase.cs b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableEventBase.cs new file mode 100644 index 0000000..916d553 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableEventBase.cs @@ -0,0 +1,40 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System; + +public abstract class SerializableEventBase : SerializableCallbackBase { + public InvokableEventBase invokable; + + public override void ClearCache() { + base.ClearCache(); + invokable = null; + } + + protected InvokableEventBase GetPersistentMethod() { + Type[] types = new Type[ArgTypes.Length]; + Array.Copy(ArgTypes, types, ArgTypes.Length); + + Type genericType = null; + switch (types.Length) { + case 0: + genericType = typeof(InvokableEvent); + break; + case 1: + genericType = typeof(InvokableEvent<>).MakeGenericType(types); + break; + case 2: + genericType = typeof(InvokableEvent<,>).MakeGenericType(types); + break; + case 3: + genericType = typeof(InvokableEvent<, ,>).MakeGenericType(types); + break; + case 4: + genericType = typeof(InvokableEvent<, , ,>).MakeGenericType(types); + break; + default: + throw new ArgumentException(types.Length + "args"); + } + return Activator.CreateInstance(genericType, new object[] { target, methodName }) as InvokableEventBase; + } +}
\ No newline at end of file diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableEventBase.cs.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableEventBase.cs.meta new file mode 100644 index 0000000..0f5e8ab --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/SerializableEventBase.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: e5856b88cee94a64c95101c3f3de4754 +timeCreated: 1515747887 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Siccity.SerializableCallback.asmdef b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Siccity.SerializableCallback.asmdef new file mode 100644 index 0000000..209a051 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Siccity.SerializableCallback.asmdef @@ -0,0 +1,13 @@ +{ + "name": "Siccity.SerializableCallback", + "references": [], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +}
\ No newline at end of file diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Siccity.SerializableCallback.asmdef.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Siccity.SerializableCallback.asmdef.meta new file mode 100644 index 0000000..373b011 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Siccity.SerializableCallback.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 11a2306c728a4b843a652ec6c2f142bc +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Test.cs b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Test.cs new file mode 100644 index 0000000..2c258ba --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Test.cs @@ -0,0 +1,80 @@ +using System; +using System.Diagnostics; +using UnityEngine; + +public class Test : MonoBehaviour { + const int ITERATIONS = 100000; + public float f = 0.5f; + public string s; + public System.Func<float, bool> RegularDelegate; + public System.Func<float, bool> DynamicDelegate; + public Condition condition; + public SerializableEvent ev; + + void Start() { + RegularDelegate = TestMethod; + DynamicDelegate = (System.Func<float, bool>) System.Delegate.CreateDelegate(typeof(System.Func<float, bool>), this, "TestMethod"); + condition.Invoke(f); + } + + void Update() { + var method = Stopwatch.StartNew(); + bool methodb = false; + for (int i = 0; i < ITERATIONS; ++i) { + methodb = TestMethod(f); + } + method.Stop(); + + var regularDelegate = Stopwatch.StartNew(); + bool regularDelegateb = false; + for (int i = 0; i < ITERATIONS; ++i) { + regularDelegateb = RegularDelegate(f); + } + regularDelegate.Stop(); + + var dynamicDelegate = Stopwatch.StartNew(); + bool dynamicDelegateb = false; + for (int i = 0; i < ITERATIONS; ++i) { + dynamicDelegateb = DynamicDelegate(f); + } + dynamicDelegate.Stop(); + + var serializedDelegate = Stopwatch.StartNew(); + bool serializedDelegateb = false; + for (int i = 0; i < ITERATIONS; ++i) { + serializedDelegateb = condition.Invoke(f); + } + serializedDelegate.Stop(); + + var serializedEvent = Stopwatch.StartNew(); + for (int i = 0; i < ITERATIONS; ++i) { + ev.Invoke(); + } + serializedEvent.Stop(); + + UnityEngine.Debug.Log("Method: " + methodb + method.Elapsed); + UnityEngine.Debug.Log("RegularDelegate: " + regularDelegateb + regularDelegate.Elapsed); + UnityEngine.Debug.Log("DynamicDelegate: " + dynamicDelegateb + dynamicDelegate.Elapsed); + UnityEngine.Debug.Log("SerializedCallback: " + serializedDelegateb + serializedDelegate.Elapsed); + UnityEngine.Debug.Log("SerializedEvent: " + serializedEvent.Elapsed); + } + + public bool TestMethod(float f) { + return f > 0.5f; + } + + public bool TestMethod(string a) { + return string.IsNullOrEmpty(a); + } + + public bool TestMethod2(float f, string a) { + return f > 0.5f && string.IsNullOrEmpty(a); + } + + public void TestMethod2(string a) { + s = a; + } +} + +[Serializable] +public class Condition : SerializableCallback<float, bool> { }
\ No newline at end of file diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Test.cs.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Test.cs.meta new file mode 100644 index 0000000..273d481 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Test.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 933cfc79696bbd74ca51c70a8d61ae8d +timeCreated: 1514107717 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/package.json b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/package.json new file mode 100644 index 0000000..d02db75 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/package.json @@ -0,0 +1,11 @@ +{ + "name": "com.github.siccity.serializablecallback", + "description": "UnityEvent and System.Func had a child. Lets you drag-and-drop methods with or without return values / parameters in the Unity inspector. It uses expression trees and reflection to cache a delegate on first execution. Usage is identical to UnityEvent", + "version": "1.0.0", + "unity": "2018.1", + "displayName": "SerializableCallback", + "author": { + "name": "Thor Brigsted", + "url": "http://thorbrigsted.com/" + } +} diff --git a/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/package.json.meta b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/package.json.meta new file mode 100644 index 0000000..ff18c79 --- /dev/null +++ b/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/package.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 70b54a564f424a742af19bd2af6873c5 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: |