private static ModuleParameters BuildFromManifest(ManifestV1 manifest, ModuleManager module) { var builtModuleParameters = new ModuleParameters { _manifest = manifest, // TODO: Change manager registers so that they only need an instance of the ExternalModule and not specific params _settingsManager = SettingsManager.GetModuleInstance(module), _contentsManager = ContentsManager.GetModuleInstance(module), _directoriesManager = DirectoriesManager.GetModuleInstance(module), _gw2ApiManager = Gw2ApiManager.GetModuleInstance(module) }; if (builtModuleParameters._gw2ApiManager == null) { /* Indicates a conflict of user granted permissions and module required permissions * How this could happen (without manually modifying settings): * 1. User approves all required permissions for a module. * 2. The user enables the module. * 3. The user updates the module to a version that has a new required permission which haven't been explicitly approved. */ // TODO: Show a popup instead that just asks the user if the new permission(s) is/are okay Logger.Warn("An attempt was made to enable the module {module} before all of the required API permissions have been approved.", module.ToString()); return(null); } return(builtModuleParameters); }
private void ComposeModuleFromFileSystemReader(string dllName, ModuleParameters parameters) { if (_moduleAssembly == null) { try { if (!this.DataReader.FileExists(dllName)) { Logger.Warn("Module {module} does not contain assembly DLL {dll}", this.Manifest.GetDetailedName(), dllName); return; } _moduleAssembly = LoadPackagedAssembly(dllName); if (_moduleAssembly == null) { Logger.Warn("Module {module} failed to load assembly DLL {dll}.", this.Manifest.GetDetailedName(), dllName); return; } } catch (ReflectionTypeLoadException ex) { Logger.Warn(ex, "Module {module} failed to load due to a type exception. Ensure that you are using the correct version of the Module", this.Manifest.GetDetailedName()); return; } catch (BadImageFormatException ex) { Logger.Warn(ex, "Module {module} failed to load. Check that it is a compiled module.", this.Manifest.GetDetailedName()); return; } catch (Exception ex) { Logger.Warn(ex, "Module {module} failed to load due to an unexpected error.", this.Manifest.GetDetailedName()); return; } } var catalog = new AssemblyCatalog(_moduleAssembly); var container = new CompositionContainer(catalog); container.ComposeExportedValue("ModuleParameters", parameters); _forceAllowDependency = true; try { container.SatisfyImportsOnce(this); } catch (CompositionException ex) { Logger.Warn(ex, "Module {module} failed to be composed.", this.Manifest.GetDetailedName()); } catch (FileNotFoundException ex) { Logger.Warn(ex, "Module {module} failed to load a dependency.", this.Manifest.GetDetailedName()); } _forceAllowDependency = false; }
public bool TryEnable() { if (this.Enabled || this.IsModuleAssemblyStateDirty) { return(false); } var moduleParams = ModuleParameters.BuildFromManifest(this.Manifest, this); if (moduleParams != null) { string packagePath = this.Manifest.Package.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) ? this.Manifest.Package : $"{this.Manifest.Package}.dll"; if (this.DataReader.FileExists(packagePath)) { ComposeModuleFromFileSystemReader(packagePath, moduleParams); if (this.ModuleInstance != null) { if (!_dirtyNamespaces.Contains(this.Manifest.Namespace)) { _dirtyNamespaces.Add(this.Manifest.Namespace); } this.Enabled = true; this.ModuleInstance.DoInitialize(); this.ModuleInstance.DoLoad(); this.ModuleEnabled?.Invoke(this, EventArgs.Empty); } } else { Logger.Error($"Assembly '{packagePath}' could not be loaded from {this.DataReader.GetType().Name}."); } } this.State.Enabled = this.Enabled; GameService.Settings.Save(); return(this.Enabled); }
private static ModuleParameters BuildFromManifest(ManifestV1 manifest, ModuleManager module) { var builtModuleParameters = new ModuleParameters(); builtModuleParameters._manifest = manifest; // TODO: Change manager registers so that they only need an instance of the ExternalModule and not specific params builtModuleParameters._settingsManager = SettingsManager.GetModuleInstance(module); builtModuleParameters._contentsManager = ContentsManager.GetModuleInstance(module); builtModuleParameters._directoriesManager = DirectoriesManager.GetModuleInstance(module); builtModuleParameters._gw2ApiManager = GameService.Gw2Api.RegisterGw2ApiConnection(manifest, module.State.UserEnabledPermissions ?? new TokenPermission[0]); if (builtModuleParameters._gw2ApiManager == null) { // Indicates a conflict of user granted permissions and module required permissions return(null); } return(builtModuleParameters); }