Example #1
0
        /// <inheritdoc />
        public void Init(IRuntime runtime)
        {
            if (Assembly.GetCallingAssembly().GetName().Name != "Rocket.Runtime")
            {
                throw new MethodAccessException();
            }

            this.runtime = runtime;

            logger             = runtime.Container.Resolve <ILogger>();
            eventManager       = runtime.Container.Resolve <IEventBus>();
            pluginManager      = runtime.Container.Resolve <IPluginManager>();
            commandHandler     = runtime.Container.Resolve <ICommandHandler>();
            taskScheduler      = runtime.Container.Resolve <ITaskScheduler>();
            permissionProvider = (ConfigurationPermissionProvider)runtime.Container.Resolve <IPermissionProvider>("default_permissions");
            playerManager      = (EcoPlayerManager)runtime.Container.Resolve <IPlayerManager>("eco");

            CheckConfig();

            Func <object, bool>         prePlayerJoin = _EmitPlayerPreJoin;
            Action <object>             playerJoin    = _EmitPlayerJoin;
            Action <object>             playerLeave   = _EmitPlayerLeave;
            Func <object, string, bool> playerChat    = _EmitPlayerChat;

            Type userType        = typeof(User);
            Type chatManagerType = typeof(ChatManager);

            userType.GetField("OnUserPreLogin").SetValue(null, prePlayerJoin);
            userType.GetField("OnUserLogin").SetValue(null, playerJoin);
            userType.GetField("OnUserLogout").SetValue(null, playerLeave);
            chatManagerType.GetField("OnUserChat").SetValue(null, playerChat);

            pluginManager.Init();

            eventManager.Emit(this, new ImplementationReadyEvent(this));

            logger.LogInformation($"Rocket has initialized under the server name {ServerName}!");

            while (true)
            {
                string input = System.Console.ReadLine();

                if (input == null)
                {
                    continue;
                }

                taskScheduler
                .ScheduleNextFrame(this, () =>
                {
                    if (input.StartsWith("/", StringComparison.InvariantCulture))
                    {
                        input = input.Remove(0, 1);
                    }

                    bool wasHandled = commandHandler.HandleCommand(Console, input, string.Empty);

                    if (!wasHandled)
                    {
                        logger.LogError("That command could not be found!");
                    }
                }, "Console Command");
            }
        }