Exemple #1
0
 public ApplicationManager(ILogger <ApplicationManager> logger, IMiddlewareActionHandler actionHandler, IEnumerable <IManagerCommand> commands,
                           ITranslationLookup translationLookup, IConfigurationHandler <CommandConfiguration> commandConfiguration,
                           IConfigurationHandler <ApplicationConfiguration> appConfigHandler, IGameServerInstanceFactory serverInstanceFactory,
                           IEnumerable <IPlugin> plugins, IParserRegexFactory parserRegexFactory, IEnumerable <IRegisterEvent> customParserEvents,
                           IEventHandler eventHandler, IScriptCommandFactory scriptCommandFactory, IDatabaseContextFactory contextFactory, IMetaService metaService,
                           IMetaRegistration metaRegistration, IScriptPluginServiceResolver scriptPluginServiceResolver, ClientService clientService, IServiceProvider serviceProvider,
                           ChangeHistoryService changeHistoryService, ApplicationConfiguration appConfig, PenaltyService penaltyService)
 {
     MiddlewareActionHandler = actionHandler;
     _servers               = new ConcurrentBag <Server>();
     MessageTokens          = new List <MessageToken>();
     ClientSvc              = clientService;
     PenaltySvc             = penaltyService;
     ConfigHandler          = appConfigHandler;
     StartTime              = DateTime.UtcNow;
     PageList               = new PageList();
     AdditionalEventParsers = new List <IEventParser>()
     {
         new BaseEventParser(parserRegexFactory, logger, _appConfig)
     };
     AdditionalRConParsers = new List <IRConParser>()
     {
         new BaseRConParser(serviceProvider.GetRequiredService <ILogger <BaseRConParser> >(), parserRegexFactory)
     };
     TokenAuthenticator           = new TokenAuthentication();
     _logger                      = logger;
     _metaService                 = metaService;
     _tokenSource                 = new CancellationTokenSource();
     _commands                    = commands.ToList();
     _translationLookup           = translationLookup;
     _commandConfiguration        = commandConfiguration;
     _serverInstanceFactory       = serverInstanceFactory;
     _parserRegexFactory          = parserRegexFactory;
     _customParserEvents          = customParserEvents;
     _eventHandler                = eventHandler;
     _scriptCommandFactory        = scriptCommandFactory;
     _metaRegistration            = metaRegistration;
     _scriptPluginServiceResolver = scriptPluginServiceResolver;
     _serviceProvider             = serviceProvider;
     _changeHistoryService        = changeHistoryService;
     _appConfig                   = appConfig;
     Plugins                      = plugins;
 }
Exemple #2
0
        public async Task Initialize(IManager manager, IScriptCommandFactory scriptCommandFactory, IScriptPluginServiceResolver serviceResolver)
        {
            await _onProcessing.WaitAsync();

            try
            {
                // for some reason we get an event trigger when the file is not finished being modified.
                // this must have been a change in .NET CORE 3.x
                // so if the new file is empty we can't process it yet
                if (new FileInfo(_fileName).Length == 0L)
                {
                    return;
                }

                bool firstRun = _scriptEngine == null;

                // it's been loaded before so we need to call the unload event
                if (!firstRun)
                {
                    await OnUnloadAsync();

                    foreach (string commandName in _registeredCommandNames)
                    {
                        manager.GetLogger(0).WriteDebug($"Removing plugin registered command \"{commandName}\"");
                        manager.RemoveCommandByName(commandName);
                    }

                    _registeredCommandNames.Clear();
                }

                successfullyLoaded = false;
                string script;

                using (var stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (var reader = new StreamReader(stream, Encoding.Default))
                    {
                        script = await reader.ReadToEndAsync();
                    }
                }

                _scriptEngine = new Engine(cfg =>
                                           cfg.AllowClr(new[]
                {
                    typeof(System.Net.Http.HttpClient).Assembly,
                    typeof(EFClient).Assembly,
                    typeof(Utilities).Assembly,
                    typeof(Encoding).Assembly
                })
                                           .CatchClrExceptions());

                _scriptEngine.Execute(script);
                _scriptEngine.SetValue("_localization", Utilities.CurrentLocalization);
                _scriptEngine.SetValue("_serviceResolver", serviceResolver);
                dynamic pluginObject = _scriptEngine.GetValue("plugin").ToObject();

                Author  = pluginObject.author;
                Name    = pluginObject.name;
                Version = (float)pluginObject.version;

                var commands = _scriptEngine.GetValue("commands");

                if (commands != JsValue.Undefined)
                {
                    try
                    {
                        foreach (var command in GenerateScriptCommands(commands, scriptCommandFactory))
                        {
                            manager.GetLogger(0).WriteDebug($"Adding plugin registered command \"{command.Name}\"");
                            manager.AddAdditionalCommand(command);
                            _registeredCommandNames.Add(command.Name);
                        }
                    }

                    catch (RuntimeBinderException e)
                    {
                        throw new PluginException($"Not all required fields were found: {e.Message}")
                              {
                                  PluginFile = _fileName
                              };
                    }
                }

                await OnLoadAsync(manager);

                try
                {
                    if (pluginObject.isParser)
                    {
                        IsParser = true;
                        IEventParser eventParser = (IEventParser)_scriptEngine.GetValue("eventParser").ToObject();
                        IRConParser  rconParser  = (IRConParser)_scriptEngine.GetValue("rconParser").ToObject();
                        manager.AdditionalEventParsers.Add(eventParser);
                        manager.AdditionalRConParsers.Add(rconParser);
                    }
                }

                catch (RuntimeBinderException) { }

                if (!firstRun)
                {
                    await OnLoadAsync(manager);
                }

                successfullyLoaded = true;
            }

            catch (JavaScriptException ex)
            {
                throw new PluginException($"An error occured while initializing script plugin: {ex.Error} (Line: {ex.Location.Start.Line}, Character: {ex.Location.Start.Column})")
                      {
                          PluginFile = _fileName
                      };
            }

            catch
            {
                throw;
            }

            finally
            {
                if (_onProcessing.CurrentCount == 0)
                {
                    _onProcessing.Release(1);
                }
            }
        }