public void ProcessPatches(IEnumerable <KeyValuePair <string, IPatch> > patches, Paths paths) { foreach (KeyValuePair <string, IPatch> keyValuePair in patches) { IPatch patch = keyValuePair.Value; string assemblyName = patch.PatchTarget.Name; string patchFullName = patch.GetType().FullName; PatchStatus status = new PatchStatus(patchFullName, Path.GetDirectoryName(keyValuePair.Key)); if (!_assemblies.TryGetValue(assemblyName, out AssemblyDefinition definition)) { definition = AssemblyDefinition.ReadAssembly(Path.Combine(paths.ManagedFolderPath, assemblyName + ".dll")); _assemblies.Add(assemblyName, definition); } try { PatchLoaderStatusInfo.Statuses.Add(status.PatchName, status); _logger.Info($"Executing patch {assemblyName} of {keyValuePair.Key}"); _assemblies[assemblyName] = patch.Execute(definition, new WithPrefixLogger(_logger, assemblyName), Path.GetDirectoryName(keyValuePair.Key)); } catch (Exception e) { _logger.Exception(e, "Patch caused an exception"); status.SetError(e.ToString()); } } foreach (KeyValuePair <string, AssemblyDefinition> keyValuePair in _assemblies) { _logger.Info($"Loading assembly: {keyValuePair.Key}"); LoadAssemblyAndDispose(keyValuePair.Value); } }
void downloadJob_OnJobTransferredEvent(object sender, JobNotificationEventArgs e) { try { // complete the job _downloadJob.Complete(); CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider(); ICodeCompiler compiler = provider.CreateCompiler(); CompilerParameters cpar = new CompilerParameters(); cpar.GenerateInMemory = true; cpar.GenerateExecutable = false; cpar.ReferencedAssemblies.Add( Process.GetCurrentProcess().MainModule.FileName ); cpar.ReferencedAssemblies.Add("System.dll"); cpar.ReferencedAssemblies.Add("System.Drawing.dll"); cpar.ReferencedAssemblies.Add("System.Windows.Forms.dll"); cpar.ReferencedAssemblies.Add("RSIWarrior.Common.dll"); cpar.ReferencedAssemblies.Add("RSIWarrior.AutoUpdate.dll"); CompilerResults cres = compiler.CompileAssemblyFromFile( cpar, LocalPatchFile ); foreach (CompilerError ce in cres.Errors) { throw new Exception(ce.ErrorText); } if (cres.Errors.Count == 0 && cres.CompiledAssembly != null) { Type[] exportedTypes = cres.CompiledAssembly.GetExportedTypes(); foreach (Type type in exportedTypes) { if (type.GetInterface("RSIWarrior.AutoUpdate.IPatch") != null && !type.IsAbstract) { IPatch patch = (IPatch)Activator.CreateInstance(type); patch.Execute(); } } } } catch (Exception ex) { } finally { try { File.Delete(LocalPatchFile); } catch { } } }