/// <summary> /// 反初始化指定的应用 /// </summary> /// <param name="app">要反初始化的应用</param> private void UninitializeApp(CQPSimulatorApp app) { // 获取动态库实例 CQPDynamicLibrary library = app.Library; if (library.IsInitialized && library.IsExit == false) { if (library.IsEnable) { DisableApp(app); } IEnumerable <AppEvent> events = GetEvents(app, AppEventType.CQExit); foreach (AppEvent appEvent in events) { try { library.InvokeCQExit(appEvent); // 忽略返回值 } catch (Exception ex) { Logger.Instance.Error(library.AppInfo.Name, $"{ex.Message}"); } } } }
/// <summary> /// 获取函数处理过程 /// </summary> /// <param name="authCode">应用授权码</param> /// <param name="funcName">函数名称</param> /// <param name="objs">函数参数列表</param> /// <returns>返回值</returns> public object GetProcess(int authCode, [CallerMemberName] string funcName = null, params object[] objs) { // 获取方法 MethodInfo method = typeof(CQPExport).GetMethod(funcName, BindingFlags.Static | BindingFlags.Public); if (method == null) { throw new MissingMethodException(nameof(CQPExport), funcName); } // 获取应用实例 CQPSimulatorApp app = this.CQPApps.Where(temp => temp.AuthCode == authCode).FirstOrDefault(); bool isAuth = false; if (app == null) { Logger.Instance.Error($"检测到非法的 Api 调用, 已阻止. 请确保调用的 Api 使用了 Initialize 下发的授权码"); } else { isAuth = true; // 默认允许调用 // 获取方法是否标记了应用权限校验 CQPAuthAttribute auth = method.GetCustomAttribute <CQPAuthAttribute> (); if (auth != null) { // 校验权限 isAuth = app.Library.AppInfo.Auth.Contains(auth.AppAuth); } } return(new CommandRouteInvoker(this, app, isAuth).GetCommandHandle(funcName, objs).Execute()); }
/// <summary> /// 启用指定的应用 /// </summary> /// <param name="app">要启用的应用</param> public void EnableApp(CQPSimulatorApp app) { CQPDynamicLibrary library = app.Library; if (!library.IsEnable) { if (!library.IsInitialized) { InitializeApp(app); } IEnumerable <AppEvent> events = GetEvents(app, AppEventType.CQAppEnable); foreach (AppEvent appEvent in events) { try { library.InvokeCQAppEnable(appEvent); // 忽略返回值 } catch (Exception ex) { Logger.Instance.Error(library.AppInfo.Name, $"{ex.Message}"); } } } }
/// <summary> /// 获取指定事件按照 ID 和优先级排序后的结果 /// </summary> /// <param name="events">要获取的事件源的应用</param> /// <param name="type">事件类型</param> /// <returns>事件列表</returns> private static IEnumerable <AppEvent> GetEvents(CQPSimulatorApp app, AppEventType type) { return(from temp in app.Library.AppInfo.Events where temp.Type == type orderby temp.Id orderby temp.Priority select temp); }
/// <summary> /// 初始化指定的应用 /// </summary> /// <param name="app">要初始化的应用</param> private void InitializeApp(CQPSimulatorApp app) { // 获取动态库实例 CQPDynamicLibrary library = app.Library; if (!library.IsInitialized) { string appId = app.AppId; // 检查返回码 if (library.AppInfo.ResultCode != 1) { library.Dispose(); Logger.Instance.Warning(CQPErrorCode.TYPE_APP_LOAD_FAIL, CQPErrorCode.ERROR_JSON_PARSE_FAIL, "{0} 加载失败! 错误: 应用信息Json串解析失败,请检查Json串是否正确", appId); } // 检查 Api 版本 if (library.AppInfo.ApiVersion != (int)this.ApiType) { library.Dispose(); Logger.Instance.Warning(CQPErrorCode.TYPE_APP_LOAD_FAIL, CQPErrorCode.ERROR_API_OLD, "{0} 加载失败!错误:Api版本过旧,请获取新版应用或联系作者更新", appId); } // 检查应用返回信息 try { string appInfo = library.InvokeAppInfo() ?? string.Empty; // 解析返回信息 Match match = AppInfoRegex.Match(appInfo); if (!match.Success) { library.Dispose(); Logger.Instance.Warning(CQPErrorCode.TYPE_APP_LOAD_FAIL, CQPErrorCode.ERROR_APPINFO_PARSE_FAIL, "{0} 加载失败!错误:AppInfo返回信息解析错误", appId); } // 解析 Api版本 if (int.Parse(match.Groups[1].Value) != (int)this.ApiType) { library.Dispose(); Logger.Instance.Warning(CQPErrorCode.TYPE_APP_LOAD_FAIL, CQPErrorCode.ERROR_APPINFO_NOT_LOAD, "{0} 加载失败!错误:AppInfo返回的Api版本不支持直接加载,仅支持Api版本为9(及以上)的应用直接加载", appId); } // 解析 AppID if (!appId.Equals(match.Groups[2].Value)) { library.Dispose(); Logger.Instance.Warning(CQPErrorCode.TYPE_APP_LOAD_FAIL, CQPErrorCode.ERROR_APPINFO_DIFFERENT, "{0} 加载失败!错误:{0}目录名的AppID与AppInfo返回的AppID不同", appId); } } catch (Exception) { library.Dispose(); Logger.Instance.Warning(CQPErrorCode.TYPE_APP_LOAD_FAIL, CQPErrorCode.ERROR_APPINFO_NOT_FOUNT, "{0} 加载失败!错误:AppInfo函数不存在或错误", appId); } // 传递 AuthCode try { if (library.InvokeInitialize(app.AuthCode) != 0) { Logger.Instance.Warning(CQPErrorCode.TYPE_APP_LOAD_FAIL, CQPErrorCode.ERROR_AUTHCODE_FAIL, "{0} 加载失败!错误:Api授权接收函数(Initialize)返回值非0", appId); } } catch (Exception ex) { Logger.Instance.Error(library.AppInfo.Name, $"{ex.Message}"); } } // 初始化应用 if (!library.IsStartup) { IEnumerable <AppEvent> events = GetEvents(app, AppEventType.CQStartup); foreach (AppEvent appEvent in events) { try { app.Library.InvokeCQStartup(appEvent); // 忽略返回值 } catch (Exception ex) { Logger.Instance.Error(library.AppInfo.Name, $"{ex.Message}"); } } } }