You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

141 lines
4.7 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
namespace Sog
{
// 动态加载服务器逻辑代码
public static class ScriptLoader
{
public class MyAssemblyLoadContext : System.Runtime.Loader.AssemblyLoadContext
{
protected override Assembly Load(AssemblyName assemblyName)
{
return Assembly.Load(assemblyName);
}
}
public static IScript LoadServerLogicDll(string strDllFile, string scriptTypeName)
{
TraceLog.Debug("ScriptLoader.LoadServerLogicDll file {0} type {1}", strDllFile, scriptTypeName);
//IAssemblyLoadContext
try
{
MyAssemblyLoadContext context = new MyAssemblyLoadContext();
FileStream dllStream = new FileStream(strDllFile + ".dll", FileMode.Open, FileAccess.Read);
FileStream pdbStream = new FileStream(strDllFile + ".pdb", FileMode.Open, FileAccess.Read);
Assembly asm = context.LoadFromStream(dllStream, pdbStream);
dllStream.Dispose();
pdbStream.Dispose();
//AssemblyName asmName = System.Runtime.Loader.AssemblyLoadContext.GetAssemblyName(strDllFile);
//Assembly asm = Assembly.Load(asmName);
if (asm == null)
{
TraceLog.Error("ScriptLoader Load ServerDll {0} failed", strDllFile);
}
else
{
TraceLog.Debug("ScriptLoader Load ServerDll success");
//用类型的命名空间和类获得类型
object scriptImp = asm.CreateInstance(scriptTypeName);
IScript scriptObj = scriptImp as IScript;
//是不是没有基础IScript?
if (scriptObj == null)
{
TraceLog.Error("ScriptLoader CreateInstance, class {0} failed", scriptTypeName);
}
else
{
TraceLog.Debug("ScriptLoader CreateInstance class {0} success", scriptTypeName);
}
//if(testReloadFailed)
//{
// TraceLog.Error("ScriptLoader LoadServerLogicDll, class {0} failed (testReloadFailed)", scriptTypeName);
// return null;
//}
return scriptObj;
}
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
return null;
}
public static IScriptHotfixCheck LoadServerLogicDllHotfixCheck(string strDllFile)
{
TraceLog.Debug("ScriptLoader.LoadServerLogicDllHotfixCheck file {0}", strDllFile);
try
{
MyAssemblyLoadContext context = new MyAssemblyLoadContext();
FileStream dllStream = new FileStream(strDllFile + ".dll", FileMode.Open, FileAccess.Read);
FileStream pdbStream = new FileStream(strDllFile + ".pdb", FileMode.Open, FileAccess.Read);
Assembly asm = context.LoadFromStream(dllStream, pdbStream);
dllStream.Dispose();
pdbStream.Dispose();
if (asm == null)
{
TraceLog.Error("ScriptLoader.LoadServerLogicDllHotfixCheck Load ServerDll {0} failed", strDllFile);
}
else
{
TraceLog.Debug("ScriptLoader.LoadServerLogicDllHotfixCheck Load ServerDll success");
//用类型的命名空间和类获得类型, 这个写死就可以了,所有服务器dll里的内容是完全一样的
string scriptTypeName = "Sog.ServerScriptHotfixCheck";
object scriptImp = asm.CreateInstance(scriptTypeName);
IScriptHotfixCheck scriptObj = scriptImp as IScriptHotfixCheck;
//是不是没有基础IScript?
if (scriptObj == null)
{
TraceLog.Error("ScriptLoader.LoadServerLogicDllHotfixCheck CreateInstance, class {0} failed", scriptTypeName);
}
else
{
TraceLog.Debug("ScriptLoader.LoadServerLogicDllHotfixCheck CreateInstance class {0} success", scriptTypeName);
}
return scriptObj;
}
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
return null;
}
}
}