Init.cs 2.8 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
				Instance = this;

T
tanghai 已提交
32 33
				AssemblyManager.Instance.Add("Model", typeof(Model.Init).Assembly);

34 35
				this.RegisterAssembly();
				this.RegisterILAdapter();
T
tanghai 已提交
36
				this.RegisterDelegate();
37 38
				this.RegisterRedirection();

39
				IType hotfixInitType = AppDomain.LoadedTypes["Hotfix.Init"];
40 41 42 43 44 45
				start = hotfixInitType.GetMethod("Start", 0);
				update = hotfixInitType.GetMethod("Update", 0);
				onApplicationQuit = hotfixInitType.GetMethod("OnApplicationQuit", 0);

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

53 54 55
		private void Update()
		{
			this.AppDomain.Invoke(this.update, null, this.param0);
56 57

			ObjectEvents.Instance.Update();
58 59
		}

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

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

			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()
		{
80
			MethodInfo mi = typeof(Log).GetMethod("Debug", new Type[] { typeof(string) });
81
			this.AppDomain.RegisterCLRMethodRedirection(mi, ILRedirection.LogDebug);
82 83
		}

T
tanghai 已提交
84 85 86
		public void RegisterDelegate()
		{
			AppDomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
87
			AppDomain.DelegateManager.RegisterMethodDelegate<byte[], int, int>();
T
tanghai 已提交
88 89 90

		}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		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);
			}
110
		}
T
tanghai 已提交
111
	}
T
tanghai 已提交
112
}