Init.cs 2.7 KB
Newer Older
1
using System;
2 3 4 5 6 7
using System.IO;
using System.Reflection;
using Model;
using ILRuntime.CLR.Method;
using ILRuntime.CLR.TypeSystem;
using ILRuntime.Runtime.Enviorment;
8
using UnityEngine;
T
tanghai 已提交
9

10
namespace Model
T
tanghai 已提交
11
{
12
	public class Init: MonoBehaviour
T
tanghai 已提交
13
	{
14
		public static Init Instance;
15

16 17 18 19 20 21 22 23 24 25 26
		public ILRuntime.Runtime.Enviorment.AppDomain AppDomain = new ILRuntime.Runtime.Enviorment.AppDomain();

		private readonly object[] param0 = new object[0];

		private IMethod start;

		private IMethod update;

		private IMethod onApplicationQuit;

		private void Start()
27
		{
28 29
			try
			{
30 31 32 33
				Instance = this;

				this.RegisterAssembly();
				this.RegisterILAdapter();
T
tanghai 已提交
34
				this.RegisterDelegate();
35 36 37 38 39 40 41 42 43
				this.RegisterRedirection();

				IType hotfixInitType = AppDomain.LoadedTypes["Hotfix.HotfixInit"];
				start = hotfixInitType.GetMethod("Start", 0);
				update = hotfixInitType.GetMethod("Update", 0);
				onApplicationQuit = hotfixInitType.GetMethod("OnApplicationQuit", 0);

				// 进入热更新层
				this.AppDomain.Invoke(this.start, null, param0);
44 45 46 47 48
			}
			catch (Exception e)
			{
				Log.Error(e.ToString());
			}
49
		}
50

51 52
		private void Update()
		{
53 54
			ObjectEvents.Instance.Update();

55 56 57
			this.AppDomain.Invoke(this.update, null, this.param0);
		}

58 59
		private void OnApplicationQuit()
		{
60 61 62 63 64 65
			this.AppDomain.Invoke(this.onApplicationQuit, null, this.param0);
		}

		public void RegisterAssembly()
		{
			GameObject code = (GameObject)Resources.Load("Code");
T
tanghai 已提交
66 67
			byte[] assBytes = code.GetComponent<ReferenceCollector>().Get<TextAsset>("Hotfix.dll").bytes;
			byte[] mdbBytes = code.GetComponent<ReferenceCollector>().Get<TextAsset>("Hotfix.pdb").bytes;
68 69 70 71 72 73 74 75 76 77

			using (MemoryStream fs = new MemoryStream(assBytes))
			using (MemoryStream p = new MemoryStream(mdbBytes))
			{
				AppDomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider());
			}
		}

		public unsafe void RegisterRedirection()
		{
78 79
			var mi = typeof(Log).GetMethod("Debug", new System.Type[] { typeof(string) });
			this.AppDomain.RegisterCLRMethodRedirection(mi, ILRedirection.LogDebug);
80 81
		}

T
tanghai 已提交
82 83 84 85 86 87 88
		public void RegisterDelegate()
		{
			AppDomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
			AppDomain.DelegateManager.RegisterMethodDelegate<System.Byte[], System.Int32, System.Int32>();

		}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
		public void RegisterILAdapter()
		{
			Assembly assembly = typeof(Init).Assembly;

			foreach (Type type in assembly.GetTypes())
			{
				object[] attrs = type.GetCustomAttributes(typeof(ILAdapterAttribute), false);
				if (attrs.Length == 0)
				{
					continue;
				}
				object obj = Activator.CreateInstance(type);
				CrossBindingAdaptor adaptor = obj as CrossBindingAdaptor;
				if (adaptor == null)
				{
					continue;
				}
				AppDomain.RegisterCrossBindingAdaptor(adaptor);
			}
108
		}
T
tanghai 已提交
109
	}
T
tanghai 已提交
110
}