/// <summary> /// Creates new instance of WurmApiManager. /// All WurmApi services are thread safe and independent of execution context (i.e. synchronization context, async handling). /// Always call Dispose on the Manager, before closing app or dropping the instance. This will ensure proper cleanup and internal cache consistency. /// Not calling Dispose without terminating hosting process, may result in resource leaks. /// </summary> /// <param name="dataDirPath"> /// Directory path, where this instance of WurmApiManager can store data caches. Defaults to \WurmApi in the library DLL location. /// If relative path is provided, it will also be in relation to DLL location. /// </param> /// <param name="logger"> /// An optional implementation of ILogger, where all WurmApi errors and warnings can be forwarded. /// Defaults to no logging. /// Providing this parameter is highly is recommended. /// </param> /// <param name="eventMarshaller"> /// An optional event marshaller, used to marshal all events in a specific way (for example to GUI thread). /// Defaults to running events on ThreadPool threads. /// Providing this parameter will greatly simplify usage in any application, that has synchronization context. /// </param> /// <param name="installDirectory"> /// An optional Wurm Game Client directory path provider. /// Defaults to autodetection. /// </param> /// <param name="config"> /// Optional extra configuration options. /// </param> /// <returns></returns> public static IWurmApi Create(string dataDirPath = null, ILogger logger = null, IWurmApiEventMarshaller eventMarshaller = null, IWurmInstallDirectory installDirectory = null, WurmApiConfig config = null) { if (dataDirPath == null) { dataDirPath = "WurmApi"; } if (!Path.IsPathRooted(dataDirPath)) { var codebase = typeof(WurmApiFactory).Assembly.GetAssemblyDllDirectoryAbsolutePath(); dataDirPath = Path.Combine(codebase, dataDirPath); } if (logger == null) { logger = new LoggerStub(); } if (installDirectory == null) { installDirectory = new WurmInstallDirectory(); } if (config == null) { config = new WurmApiConfig(); } return new WurmApiManager(new WurmApiDataDirectory(dataDirPath, true), installDirectory, logger, eventMarshaller, config); }
private static Func <Ninject.Activation.IContext, IWurmApi> BuildWurmApiFactory(IKernel kernel) { return(context => { IWurmAssistantConfig config = kernel.Get <IWurmAssistantConfig>(); IWurmApiLoggerFactory loggerFactory = kernel.Get <IWurmApiLoggerFactory>(); IWurmApiEventMarshaller eventMarshaller = kernel.Get <IWurmApiEventMarshaller>(); IWurmAssistantDataDirectory wurmAssistantDataDirectory = kernel.Get <IWurmAssistantDataDirectory>(); if (string.IsNullOrWhiteSpace(config.WurmGameClientInstallDirectory)) { throw new InvalidOperationException("Unknown path to Wurm Game Client installation folder."); } IWurmClientInstallDirectory wurmInstallDirectory = new WurmInstallDirectoryOverride(config.WurmGameClientInstallDirectory); ServerInfoManager serverInfoManager = kernel.Get <ServerInfoManager>(); var wurmApiConfig = new WurmApiConfig { Platform = Platform.Windows, ClearAllCaches = config.DropAllWurmApiCachesToggle, WurmUnlimitedMode = config.WurmUnlimitedMode }; serverInfoManager.UpdateWurmApiConfigDictionary(wurmApiConfig.ServerInfoMap); var wurmApiDataDir = new DirectoryInfo(Path.Combine(wurmAssistantDataDirectory.DirectoryPath, "WurmApi")); var wurmApi = AldursLab.WurmApi.WurmApiFactory.Create( new WurmApiCreationOptions() { DataDirPath = wurmApiDataDir.FullName, WurmApiLogger = loggerFactory.Create(), WurmApiEventMarshaller = eventMarshaller, WurmClientInstallDirectory = wurmInstallDirectory, WurmApiConfig = wurmApiConfig }); config.DropAllWurmApiCachesToggle = false; var validator = new WurmClientValidator(wurmApi, config); if (!validator.SkipOnStart) { var issues = validator.Validate(); if (issues.Any()) { validator.ShowSummaryWindow(issues); } } return wurmApi; }); }
public InternalEventInvoker([NotNull] IInternalEventAggregator eventAggregator, [NotNull] IWurmApiLogger logger, [NotNull] IWurmApiEventMarshaller eventMarshaller) { if (eventAggregator == null) throw new ArgumentNullException(nameof(eventAggregator)); if (logger == null) throw new ArgumentNullException(nameof(logger)); if (eventMarshaller == null) throw new ArgumentNullException(nameof(eventMarshaller)); this.eventAggregator = eventAggregator; this.logger = logger; this.eventMarshaller = eventMarshaller; LoopDelayMillis = 100; schedulingTask = new Task(RunSchedule, TaskCreationOptions.LongRunning); schedulingTask.Start(); }
/// <summary> /// </summary> /// <param name="job"> /// Delegate to execute after receiving signals. /// </param> /// <param name="eventMarshaller">Optional thread marshaller of the events.</param> public RepeatableThreadedOperation([NotNull] Action job, IWurmApiEventMarshaller eventMarshaller = null) { this.eventMarshaller = eventMarshaller; if (job == null) { throw new ArgumentNullException(nameof(job)); } task = new Task(() => { while (true) { autoResetEvent.WaitOne(); if (exit) { break; } try { job(); Task.Run(() => operationCompletedAtLeastOnceAwaiter.TrySetResult(true)); } catch (Exception exception) { try { OnOperationFaulted(new ExceptionEventArgs(exception)); } catch (Exception) { // nothing more to be done here } } if (exit) { break; } } }, TaskCreationOptions.LongRunning); task.Start(); }
/// <summary> /// </summary> /// <param name="job"> /// Delegate to execute after receiving signals. /// </param> /// <param name="eventMarshaller">Optional thread marshaller of the events.</param> public RepeatableThreadedOperation([NotNull] Action job, IWurmApiEventMarshaller eventMarshaller = null) { this.eventMarshaller = eventMarshaller; if (job == null) throw new ArgumentNullException("job"); task = new Task(() => { while (true) { autoResetEvent.WaitOne(); if (exit) { break; } try { job(); operationCompletedAtLeastOnceAwaiter.TrySetResult(true); } catch (Exception exception) { try { OnOperationFaulted(new ExceptionEventArgs(exception)); } catch (Exception) { // nothing more to be done here } } if (exit) { break; } } }, TaskCreationOptions.LongRunning); task.Start(); }
/// <summary> /// Public factory constructor. /// </summary> internal WurmApiManager( WurmApiDataDirectory dataDirectory, IWurmClientInstallDirectory installDirectory, IWurmApiLogger wurmApiLogger, IWurmApiEventMarshaller publicEventMarshaller, WurmApiConfig wurmApiConfig) { var threadPoolMarshaller = new ThreadPoolMarshaller(wurmApiLogger); if (publicEventMarshaller == null) { publicEventMarshaller = threadPoolMarshaller; } IHttpWebRequests httpRequests = new HttpWebRequests(); ConstructSystems(dataDirectory.FullPath, installDirectory, httpRequests, wurmApiLogger, publicEventMarshaller, threadPoolMarshaller, wurmApiConfig); }
void ConstructSystems(string wurmApiDataDirectoryFullPath, IWurmClientInstallDirectory installDirectory, IHttpWebRequests httpWebRequests, IWurmApiLogger logger, IWurmApiEventMarshaller publicEventMarshaller, IWurmApiEventMarshaller internalEventMarshaller, WurmApiConfig wurmApiConfig) { IWurmApiConfig internalWurmApiConfig = wurmApiConfig.CreateCopy(); LogFileStreamReaderFactory logFileStreamReaderFactory = Wire(new LogFileStreamReaderFactory(internalWurmApiConfig)); Wire(installDirectory); Wire(httpWebRequests); if (logger == null) { logger = new WurmApiLoggerStub(); } PublicEventInvoker publicEventInvoker = Wire(new PublicEventInvoker(publicEventMarshaller, logger)); TaskManager taskManager = Wire(new TaskManager(logger)); InternalEventAggregator internalEventAggregator = Wire(new InternalEventAggregator()); InternalEventInvoker internalEventInvoker = Wire(new InternalEventInvoker(internalEventAggregator, logger, internalEventMarshaller)); WurmPaths paths = Wire(new WurmPaths(installDirectory, wurmApiConfig)); WurmServerGroups serverGroups = Wire(new WurmServerGroups(internalWurmApiConfig.ServerInfoMap)); WurmServerList serverList = Wire(new WurmServerList(internalWurmApiConfig.ServerInfoMap)); WurmLogDefinitions logDefinitions = Wire(new WurmLogDefinitions()); WurmConfigDirectories configDirectories = Wire(new WurmConfigDirectories(paths, internalEventAggregator, taskManager, logger)); WurmCharacterDirectories characterDirectories = Wire(new WurmCharacterDirectories(paths, internalEventAggregator, taskManager, logger)); WurmLogFiles logFiles = Wire(new WurmLogFiles(characterDirectories, logger, logDefinitions, internalEventAggregator, internalEventInvoker, taskManager, paths)); WurmLogsMonitor logsMonitor = Wire(new WurmLogsMonitor(logFiles, logger, publicEventInvoker, internalEventAggregator, characterDirectories, internalEventInvoker, taskManager, logFileStreamReaderFactory)); var heuristicsDataDirectory = Path.Combine(wurmApiDataDirectoryFullPath, "WurmLogsHistory"); if (internalWurmApiConfig.ClearAllCaches) { ClearDir(heuristicsDataDirectory, logger); } WurmLogsHistory logsHistory = Wire(new WurmLogsHistory(logFiles, logger, heuristicsDataDirectory, logFileStreamReaderFactory, wurmApiConfig)); WurmConfigs wurmConfigs = Wire(new WurmConfigs(configDirectories, logger, publicEventInvoker, internalEventAggregator, taskManager)); WurmAutoruns autoruns = Wire(new WurmAutoruns(wurmConfigs, characterDirectories, logger)); var wurmServerHistoryDataDirectory = Path.Combine(wurmApiDataDirectoryFullPath, "WurmServerHistory"); if (internalWurmApiConfig.ClearAllCaches) { ClearDir(wurmServerHistoryDataDirectory, logger); } WurmServerHistory wurmServerHistory = Wire(new WurmServerHistory(wurmServerHistoryDataDirectory, logsHistory, serverList, logger, logsMonitor, logFiles, internalEventAggregator, serverGroups, wurmApiConfig)); var wurmServersDataDirectory = Path.Combine(wurmApiDataDirectoryFullPath, "WurmServers"); if (internalWurmApiConfig.ClearAllCaches) { ClearDir(wurmServersDataDirectory, logger); } WurmServers wurmServers = Wire(new WurmServers(logsHistory, logsMonitor, serverList, httpWebRequests, wurmServersDataDirectory, characterDirectories, wurmServerHistory, logger, serverGroups, wurmApiConfig)); WurmCharacters characters = Wire(new WurmCharacters(characterDirectories, wurmConfigs, wurmServers, wurmServerHistory, logger, taskManager, logsMonitor, publicEventInvoker, internalEventAggregator, paths, logsHistory, serverGroups)); HttpWebRequests = httpWebRequests; Autoruns = autoruns; Characters = characters; Configs = wurmConfigs; LogDefinitions = logDefinitions; LogsHistory = logsHistory; LogsMonitor = logsMonitor; Servers = wurmServers; WurmLogFiles = logFiles; WurmServerHistory = wurmServerHistory; WurmCharacterDirectories = characterDirectories; WurmConfigDirectories = configDirectories; InternalEventAggregator = internalEventAggregator; Paths = paths; ServerGroups = serverGroups; Logger = logger; ServersList = serverList; }