void Awake() { if ((isCalling & CALLING_AWAKE) == 0) { isCalling |= CALLING_AWAKE; // Make sure DNA is initialized and scripts are loaded if (dnaScript == null) { dnaScript = UnityEngine.Object.FindObjectOfType <DnaScript>(); if (dnaScript != null) { dnaScript.Initialize(); dnaScript.LoadScripts(); } else { throw new UnityException("No DnaScript component found in scene!"); } } dnaTypeDef = Dna.FindType(dnaComponentName); if (dnaTypeDef != 0) { dnaObject = Dna.CreateInstance(dnaTypeDef, this); ulong awakeDef = Dna.FindMethod(dnaTypeDef, "Awake"); if (awakeDef != 0) { Dna.Call(awakeDef, dnaObject); } } isCalling &= CALLING_AWAKE; } }
private static void ClearDeadReferences() { List <PTR> deadRefs = null; foreach (KeyValuePair <PTR, System.WeakReference> pair in dnaObjects) { DnaObject obj = pair.Value.Target as DnaObject; if (obj == null) { if (deadRefs == null) { deadRefs = new List <PTR>(); } deadRefs.Add(pair.Key); } } if (deadRefs != null) { for (int i = 0; i < deadRefs.Count; i++) { dnaObjects.Remove(deadRefs[i]); } } }
public static DnaObject WrapObject(byte *pPtr) { if (pPtr == null) { return(null); } System.WeakReference weak; DnaObject obj = null; if (dnaObjects.TryGetValue((PTR)pPtr, out weak)) { obj = weak.Target as DnaObject; if (obj != null) { return(obj); } } obj = new DnaObject(pPtr); dnaObjects.Add((PTR)pPtr, new System.WeakReference(obj)); // If there is a collection - clear dead references if (System.GC.CollectionCount(0) != _collectionCount) { ClearDeadReferences(); } return(obj); }
public static void Clear() { // Make sure we clear every single reference to a DNA object that Mono runtime may have. if (dnaObjects != null) { foreach (KeyValuePair <PTR, System.WeakReference> pair in dnaObjects) { DnaObject obj = pair.Value.Target as DnaObject; if (obj != null) { obj.dnaPtr = null; } } } dnaObjects = null; }
public static void Main() { if (LOAD_UNITY_DLLS) { foreach (string dllName in dllsToLoad) { System.Reflection.Assembly.LoadFile(UNITY_DLL_PATH + dllName); } } Dna.Init(10000000, assemblySearchPaths); Dna.Load("SpinCube.dll"); ulong typeDef = Dna.FindType("SpinCubeComponent"); DnaObject obj = Dna.CreateInstance(typeDef, null); Dna.Call("Testing", "Test"); }
public static void Main() { System.Reflection.Assembly.LoadFile("c:/Program Files/Unity/Editor/Data/Managed/UnityEngine/UnityEngine.dll"); System.Reflection.Assembly.LoadFile("c:/Program Files/Unity/Editor/Data/Managed/UnityEngine/UnityEngine.AnimationModule.dll"); System.Reflection.Assembly.LoadFile("c:/Program Files/Unity/Editor/Data/Managed/UnityEngine/UnityEngine.AudioModule.dll"); System.Reflection.Assembly.LoadFile("c:/Program Files/Unity/Editor/Data/Managed/UnityEngine/UnityEngine.CoreModule.dll"); System.Reflection.Assembly.LoadFile("c:/Program Files/Unity/Editor/Data/Managed/UnityEngine/UnityEngine.InputModule.dll"); System.Reflection.Assembly.LoadFile("c:/Program Files/Unity/Editor/Data/Managed/UnityEngine/UnityEngine.ParticleSystemModule.dll"); System.Reflection.Assembly.LoadFile("c:/Program Files/Unity/Editor/Data/Managed/UnityEngine/UnityEngine.Physics2DModule.dll"); System.Reflection.Assembly.LoadFile("c:/Program Files/Unity/Editor/Data/Managed/UnityEngine/UnityEngine.PhysicsModule.dll"); System.Reflection.Assembly.LoadFile("c:/Program Files/Unity/Editor/Data/Managed/UnityEngine/UnityEngine.UIModule.dll"); Dna.Init(10000000, assemblySearchPaths); Dna.Load("SpinCube.dll"); ulong typeDef = Dna.FindType("SpinCubeComponent"); DnaObject obj = Dna.CreateInstance(typeDef, null); Dna.Call("Testing", "Test"); }
/// <summary> /// Initializes the DNA script engine. /// </summary> /// <param name="memsize">The heap memory size to use (note: can not be expanded)</param> /// <param name="assemblySearchPaths">Array of assembly search paths to use when loading assemblies</param> public static void Init(int memsize = DEFAULT_MEM_SIZE, string[] assemblySearchPaths = null) { if (_isInitialized) { throw new System.InvalidOperationException("Dna has already been initialized. Use Dna.Reset() to reset the interpreter"); } if (assemblySearchPaths == null) { assemblySearchPaths = defaultAssemblySearchPaths; } #if UNITY_EDITOR string[] finalAssemblySearchPaths = new string[assemblySearchPaths.Length]; string unityDir = UnityEditor.EditorApplication.applicationContentsPath; string projectDir = System.IO.Path.GetDirectoryName(UnityEngine.Application.dataPath); for (int i = 0; i < assemblySearchPaths.Length; i++) { finalAssemblySearchPaths[i] = assemblySearchPaths[i] .Replace("${UNITY_DIR}", unityDir) .Replace("${PROJECT_DIR}", projectDir); } #else string[] finalAssemblySearchPaths = assemblySearchPaths; #endif Mem.Init(memsize); H.Init(); Sys.Init(); JIT.Init(); JIT_Execute.Init(); DnaObject.Init(); MetaData.Init(); MonoType.Init(); Generics.Init(); Serialization.Init(); Heap.Init(); Finalizer.Init(); InternalCall.Init(); CLIFile.Init(finalAssemblySearchPaths); Type.Init(); _isInitialized = true; }
/// <summary> /// Resets entire DNA environment to it's initial state, clearing all DnaObject references to null. /// </summary> public static void Reset() { Type.Clear(); CLIFile.Clear(); InternalCall.Clear(); Finalizer.Clear(); Heap.Clear(); Generics.Clear(); MonoType.Clear(); MetaData.Clear(); DnaObject.Clear(); JIT_Execute.Clear(); JIT.Clear(); Sys.Clear(); H.Clear(); Mem.Clear(); _isInitialized = false; }
/// <summary> /// Creates an instance of the given DNA object that also potentially wraps an existing mono base object. /// </summary> /// <param name="typeDef">The type to create.</param> /// <returns>The DNA wrapper object.</returns> public static DnaObject CreateInstance(ulong typeDef, object monoBaseObject = null) { return(DnaObject.CreateInstance((tMD_TypeDef *)typeDef, monoBaseObject)); }