public void InitStaticImpl(Type t, Type defaultImpl, Action <object> act) { lock (mLock) { ImplInfo impl; if (!mImpls.TryGetValue(t.FullName, out impl)) { impl = new ImplInfo { ImplFullTypeName = defaultImpl.FullName, DefaultAssemblyName = defaultImpl.Assembly.Location }; mImpls.Add(t.FullName, impl); Logger.Info("Static instance or type " + t.GetType() + " can update."); } var type = GetNewType(impl); #if DEBUG if (!s_CheckedTypes.Contains(type)) { TypeIsStateless(type); s_CheckedTypes.Add(type); } #endif try { if (!mHotfixed.Contains(type)) { var v = Activator.CreateInstance(type); CallHotfixStart(type, v); act(v); mHotfixed.Add(type); } } catch (Exception ex) { Logger.Error(ex, "CreateInstance static for type {0} failed.", type.FullName); } mStaticInstances.Add(new StaticImplInfo { WrapperType = t, AssignOperator = act }); } }
public void InitImpl <T>(T t, Type defaultImpl) where T : IUpdatable { lock (mLock) { var ttype = t.GetType(); ImplInfo impl; if (!mImpls.TryGetValue(ttype.FullName, out impl)) { impl = new ImplInfo { ImplFullTypeName = defaultImpl.FullName, DefaultAssemblyName = defaultImpl.Assembly.Location }; mImpls.Add(ttype.FullName, impl); Logger.Info("Instance or type " + t.GetType() + " can update."); } var type = GetNewType(impl); #if DEBUG if (!s_CheckedTypes.Contains(type)) { TypeIsStateless(type); s_CheckedTypes.Add(type); } #endif try { if (!mHotfixed.Contains(type)) { var v = Activator.CreateInstance(type); CallHotfixStart(type, v); t.SetImpl(v); mHotfixed.Add(type); } } catch (Exception ex) { Logger.Error(ex, "CreateInstance for type {0} failed.", type.FullName); } mInstances.Add(t); } }
private Type GetNewType(ImplInfo impl) { lock (mLock) { Assembly ass = null; try { if (!string.IsNullOrEmpty(impl.PluginAssemblyName)) { var file = Path.GetFullPath(mFileMap[impl.PluginAssemblyName]); ass = Assembly.LoadFile(file); Logger.Info("Load type from new Plugin {0}, type {1}", file, impl.ImplFullTypeName); } else { ass = Assembly.LoadFile(impl.DefaultAssemblyName); Logger.Info("Can not load type from Plugin, type {0}", impl.ImplFullTypeName); } } catch (Exception ex) { Logger.Error(ex, "load assebly error plugin path: {0} default path: {1}", Path.GetFullPath(Path.Combine("../Plugin/", mFileMap[impl.PluginAssemblyName])), impl.DefaultAssemblyName); } if (ass != null) { Logger.Info("Load ass, [{0}]", ass.ToString()); return(ass.GetType(impl.ImplFullTypeName)); } Logger.Error("Can not load ass, {0}", Path.GetFullPath(mFileMap[impl.PluginAssemblyName])); return(null); } }
public void Init(string serverName) { lock (mLock) { try { if (!Directory.Exists("../Plugin/")) { try { Directory.CreateDirectory("../Plugin/"); } catch { // 如果删除不掉,或者找不到文件夹,那就算了,可能其他进程或者线程已经给删了 } } if (!File.Exists("../Plugin/Plugin.config")) { return; } mServerName = serverName; var tempFile = Path.GetTempFileName(); File.Copy("../Plugin/Plugin.config", tempFile, true); var lines = File.ReadAllLines(tempFile); File.Delete(tempFile); foreach (var line in lines) { if (line.StartsWith("//")) { continue; } var strs = line.Trim().Split(' ', '\t', ','); if (strs.Length != 3) { continue; } ImplInfo info; if (mImpls.TryGetValue(strs[0], out info)) { info.PluginAssemblyName = strs[1].Trim(); info.ImplFullTypeName = strs[2].Trim(); } else { mImpls[strs[0]] = new ImplInfo { PluginAssemblyName = strs[1].Trim(), ImplFullTypeName = strs[2].Trim() }; } } var files = Directory.GetFiles("../Plugin/", "*.dll", SearchOption.TopDirectoryOnly); foreach (var file in files) { var fileName = Path.GetTempFileName(); File.Copy(file, fileName, true); mFileMap[Path.GetFileName(file)] = fileName; } } catch (Exception ex) { Logger.Error(ex, "Init update manager error {0}", serverName); throw; } } }