using UnityEngine;
using System.Collections.Generic;
using System;
using System.Runtime.InteropServices;
public class gc_manager : MonoBehaviour {
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32.dll", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
static Action mono_gc_disable;
static Action mono_gc_enable;
static bool mono_gc_loaded=false;
static bool load_mono_gc() {
if (mono_gc_loaded) {
return true;
}
unsafe {
//extracted from mono.pdb using dbh.exe (using the "enum *!*mono_gc_*" command)
//note: for the 64 bit editor, there is only a 64 bit version of mono.pdb, so you need to also download the 32 bit editor to update this for 32 bit standalone builds
// (you also need to decide which version of the dll to use; this can be done by comparing the mono_gc_collect offset with the two offsets for the 32 bit and 64 bit dlls)
int offset_mono_gc_disable=0x1b100;
int offset_mono_gc_enable=0x1b108;
int offset_mono_gc_collect=0x1b0b4; //this is used to verify that mono.dll hasn't changed
IntPtr mono_module=GetModuleHandle("mono.dll");
IntPtr func_ptr_mono_gc_collect=new IntPtr(mono_module.ToInt64()+offset_mono_gc_collect);
IntPtr expected_func_ptr_mono_gc_collect=GetProcAddress(mono_module, "mono_gc_collect");
if (func_ptr_mono_gc_collect!=expected_func_ptr_mono_gc_collect) {
//if you see this error, you need to update the "offset_mono_gc_" variables above
Debug.Log("Cannot load gc functions. Expected collect at "+ func_ptr_mono_gc_collect.ToInt64() +" Actual at "+ func_ptr_mono_gc_collect.ToInt64() +" Module root "+ mono_module.ToInt64());
return false;
}
mono_gc_enable=(Action)Marshal.GetDelegateForFunctionPointer(new IntPtr(mono_module.ToInt64()+offset_mono_gc_enable), typeof(Action));
mono_gc_disable=(Action)Marshal.GetDelegateForFunctionPointer(new IntPtr(mono_module.ToInt64()+offset_mono_gc_disable), typeof(Action));
}
mono_gc_loaded=true;
return true;
}
};