From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Scripting/ScriptingExportUtility.h | 265 +++++++++++++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100644 Runtime/Scripting/ScriptingExportUtility.h (limited to 'Runtime/Scripting/ScriptingExportUtility.h') diff --git a/Runtime/Scripting/ScriptingExportUtility.h b/Runtime/Scripting/ScriptingExportUtility.h new file mode 100644 index 0000000..3061cfb --- /dev/null +++ b/Runtime/Scripting/ScriptingExportUtility.h @@ -0,0 +1,265 @@ +#ifndef _SCRIPTINGEXPORTUTILITY_H_ +#define _SCRIPTINGEXPORTUTILITY_H_ + +#if ENABLE_SCRIPTING + +#include "Runtime/Scripting/ScriptingManager.h" +#include "Runtime/Scripting/ScriptingUtility.h" +#include "Runtime/Scripting/ICallString.h" +#include "Runtime/Scripting/Scripting.h" + + +template +inline +ScriptingArrayPtr CreateScriptingArray (ScriptingClassPtr klass, int count) +{ +#if ENABLE_MONO + //here to force template argument being used on caller, so the callsite will be compatible + //with the flash version of CreateScriptingArray, which actually needs to know what will be in the array beforehand. + //TODO: find a cleaner way of enforcing usage: +# if DEBUGMODE + int nativeSize = sizeof(T); + int monoSize = mono_class_array_element_size(klass); + DebugAssert(nativeSize == monoSize || !count); + (void)monoSize; // silence unused variable warning + nativeSize++; +# endif + return mono_array_new(mono_domain_get(),klass,count); + +#elif UNITY_FLASH + UInt8* memoryblob = (UInt8*) malloc(sizeof(T) * count + sizeof(int)); + *(int*)memoryblob = count; + return (ScriptingArrayPtr) memoryblob; +#elif UNITY_WINRT + return ScriptingArrayPtr(GetWinRTObjectInstantiation()->CreateScriptingArrayGC(klass->metroType, count)); +#endif +} + +template +inline +ScriptingArrayPtr CreateScriptingArray2D (ScriptingClassPtr klass, int count1, int count2) +{ +#if ENABLE_MONO + return mono_array_new_2d (count1, count2, klass); +#elif UNITY_FLASH + FatalErrorMsg("ToDo:CreateScriptingArray2D"); +#elif UNITY_WINRT + return ScriptingArrayPtr(GetWinRTObjectInstantiation()->CreateScriptingArray2DGC(klass->metroType, count1, count2)); +#endif +} +template +inline +ScriptingArrayPtr CreateScriptingArray3D (ScriptingClassPtr klass, int count1, int count2, int count3) +{ +#if ENABLE_MONO + return mono_array_new_3d (count1, count2, count3, klass); +#elif UNITY_FLASH + FatalErrorMsg("ToDo:CreateScriptingArray3D"); +#elif UNITY_WINRT + return ScriptingArrayPtr(GetWinRTObjectInstantiation()->CreateScriptingArray3DGC(klass->metroType, count1, count2, count3)); +#endif +} + + +inline +ScriptingArrayPtr CreateEmptyStructArray (ScriptingClassPtr klass) +{ + return CreateScriptingArray(klass,0); //using int as random template argument, as it doesn't matter, since size=0 +} + +template +inline +ScriptingArrayPtr CreateScriptingArray (const T* data, int count, ScriptingClassPtr klass) +{ + if (data == NULL) + count = 0; + + ScriptingArrayPtr array = CreateScriptingArray(klass,count); + memcpy(Scripting::GetScriptingArrayStart(array), data, sizeof(T) * count ); + return array; +} + +template +ScriptingArrayPtr CreateScriptingArrayFromUnityObjects (const T& container, int classID) +{ + ScriptingClassPtr klass = GetScriptingManager().ClassIDToScriptingClass (classID); + + ScriptingArrayPtr array = CreateScriptingArray(klass,container.size()); + typename T::const_iterator j = container.begin(); + for (int i=0;i inline +ScriptingArrayPtr CreateScriptingArrayFromUnityObjects(T& unityobjects,ScriptingClassPtr classForArray) +{ + ScriptingArrayPtr array = CreateScriptingArray (classForArray , unityobjects.size ()); + for (int i=0;i +inline +ScriptingArrayPtr CreateScriptingArrayStride (const void* data, int count, ScriptingClassPtr klass, int inputStride) +{ + if (data == NULL) + count = 0; + + ScriptingArrayPtr array = CreateScriptingArray (klass, count); + UInt8* src = (UInt8*)data; + UInt8* dst = (UInt8*)Scripting::GetScriptingArrayStart (array); + for (int i=0; i +void ScriptingStructArrayToVector (ScriptingArrayPtr source, U &dest, TConverter converter) +{ + dest.clear(); + if (source != SCRIPTING_NULL) + { + int len = GetScriptingArraySize(source); + dest.resize (len); + for (int i = 0; i < len;i++) + converter (Scripting::GetScriptingArrayElement(source, i), dest[i]); + } +} + +template +void ScriptingStructArrayToDynamicArray (ScriptingArrayPtr source, U &dest, TConverter converter) +{ + dest.clear(); + if (source != SCRIPTING_NULL) + { + int len = GetScriptingArraySize(source); + dest.resize_initialized (len); + for (int i = 0; i < len;i++) + converter (Scripting::GetScriptingArrayElement(source, i), dest[i]); + } +} + +template +void ScriptingClassArrayToVector (ScriptingArrayPtr source, U &dest, void (*converter) (T2 &source, T &dest)) +{ + dest.clear(); + if (source != SCRIPTING_NULL) + { + int len = GetScriptingArraySize(source); + dest.resize (len); + for (int i = 0; i < len;i++) + { + T2 nativeSourceObject; + ScriptingObjectPtr element = Scripting::GetScriptingArrayElementNoRef(source, i); + Scripting::RaiseIfNull(element); + MarshallManagedStructIntoNative(element, &nativeSourceObject); + converter (nativeSourceObject, dest[i]); + } + } +} + +template +std::vector ScriptingClassArrayToVector (ScriptingArrayPtr source, void (*converter) (T2 &source, T &dest)) +{ + std::vector dest; + ScriptingClassArrayToVector (source, dest, converter); + return dest; +} + + +template +ScriptingArrayPtr VectorToScriptingClassArray (const U &source, ScriptingClassPtr klass, void (*converter) (const T &source, T2 &dest)) +{ + // ToDo: if all good, remove mono pass, and use unified pass +#if ENABLE_MONO + return VectorToMonoClassArray(source, klass, converter); +#else + ScriptingArrayPtr arr = CreateScriptingArray(klass, source.size()); + for (int i = 0; i < source.size();i++) + { + T2 obj; + converter (source[i], obj); + ScriptingObjectPtr managedObject = CreateScriptingObjectFromNativeStruct(klass, obj); + Scripting::SetScriptingArrayElement(arr, i, managedObject); + } + return arr; +#endif +} + +template +ScriptingArrayPtr VectorToScriptingStructArray (const U &source, ScriptingClassPtr klass, TConverter converter) +{ + // ToDo: if all good, remove mono pass, and use unified pass +#if ENABLE_MONO + return VectorToMonoStructArray(source, klass, converter); +#else + ScriptingArrayPtr arr = CreateScriptingArray(klass, source.size()); + for (int i = 0; i < source.size();i++) + { + T2 obj; + converter (source[i], obj); + Scripting::SetScriptingArrayElement(arr, i, obj); + } + return arr; +#endif +} + + +template +void ScriptingArrayToDynamicArray(ScriptingArrayPtr a, dynamic_array& dest) +{ + Scripting::RaiseIfNull (a); + int len = GetScriptingArraySize(a); + dest.resize_uninitialized (len); + for (int i = 0; i < len; i++) + dest[i] = Scripting::GetScriptingArrayElement(a, i); +} + +template +void ScriptingArrayToDynamicArray(ScriptingArrayPtr a, dynamic_array& dest, void (*converter) (T2 &source, T &dest)) +{ + Scripting::RaiseIfNull (a); + int len = GetScriptingArraySize(a); + dest.resize_uninitialized (len); + for (int i = 0; i < len; i++) + converter (Scripting::GetScriptingArrayElement (a, i), dest[i]); +} + +template +ScriptingArrayPtr DynamicArrayToScriptingStructArray (const dynamic_array &source, ScriptingClassPtr klass, void (*converter) (const T &source, T2 &dest)) { + ScriptingArrayPtr arr = CreateScriptingArray (klass, source.size()); + for (int i = 0; i < source.size();i++) + converter (source[i], Scripting::GetScriptingArrayElement (arr, i)); + return arr; +} + +inline +std::string GetStringFromArray(ScriptingArrayPtr a, int i) +{ +#if UNITY_WINRT + return ConvertStringToUtf8(safe_cast(Scripting::GetScriptingArrayStringElementNoRefImpl(a, i))); +#elif UNITY_FLASH + // not supported yet + return ""; +#else + return MonoStringToCpp(Scripting::GetScriptingArrayStringElementNoRefImpl(a, i)); +#endif +} + +inline +bool GetBoolFromArray(ScriptingArrayPtr a, int i) +{ +#if UNITY_WINRT + return safe_cast(Scripting::GetScriptingArrayObjectElementNoRefImpl(a, i)); +#else + return Scripting::GetScriptingArrayObjectElementNoRefImpl(a, i); +#endif +} + +#endif +#endif -- cgit v1.1-26-g67d0