public static async Task Main()
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;

            currentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionEventHandler);
            currentDomain.ProcessExit        += new EventHandler(SaveAll);

            // Force Globalization to en-US because we use periods instead of commas for decimals
            CultureInfo.CurrentCulture = new CultureInfo("en-US");

            // Load .env file
            string dotenv = Path.Combine(Paths.SOLUTION_DIR, ".env");

            if (!File.Exists(dotenv))
            {
                throw new ArgumentException(".env file not found!");
            }
            DotEnv.Load(dotenv);

            InitDatabase();

            // Load Mob AI files
            string mobAiSchema = Path.Combine(Paths.AI_DIR, "mob-ai.xsd");

            MobAIManager.Load(Paths.AI_DIR, mobAiSchema);

            // Initialize all metadata.
            await MetadataHelper.InitializeAll();

            IContainer loginContainer = LoginContainerConfig.Configure();

            using ILifetimeScope loginScope = loginContainer.BeginLifetimeScope();
            LoginServer loginServer = loginScope.Resolve <LoginServer>();

            loginServer.Start();

            IContainer gameContainer = GameContainerConfig.Configure();

            using ILifetimeScope gameScope = gameContainer.BeginLifetimeScope();
            GameServer = gameScope.Resolve <GameServer>();
            GameServer.Start();

            Logger.Info("Server Started.".ColorGreen());

            // Input commands to the server
            while (true)
            {
                string[] input = (Console.ReadLine() ?? string.Empty).Split(" ", 2);
                switch (input[0])
                {
                case "exit":
                case "quit":
                    GameServer.Stop();
                    loginServer.Stop();
                    return;

                case "send":
                    if (input.Length <= 1)
                    {
                        break;
                    }
                    string       packet  = input[1];
                    PacketWriter pWriter = new PacketWriter();
                    pWriter.Write(packet.ToByteArray());
                    Logger.Info(pWriter);

                    foreach (Session session in GetSessions(loginServer, GameServer))
                    {
                        Logger.Info($"Sending packet to {session}: {pWriter}");
                        session.Send(pWriter);
                    }

                    break;

                case "resolve":
                    PacketStructureResolver resolver = PacketStructureResolver.Parse(input[1]);
                    GameSession             first    = GameServer.GetSessions().Single();
                    resolver.Start(first);
                    break;

                default:
                    Logger.Info($"Unknown command:{input[0]} args:{(input.Length > 1 ? input[1] : "N/A")}");
                    break;
                }
            }
        }
Exemplo n.º 2
0
    public static async Task Main()
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;

        currentDomain.UnhandledException += UnhandledExceptionEventHandler;
        currentDomain.ProcessExit        += SaveAll;

        // Force Globalization to en-US because we use periods instead of commas for decimals
        CultureInfo.CurrentCulture = new("en-US");

        // Load .env file
        string dotenv = Path.Combine(Paths.SOLUTION_DIR, ".env");

        if (!File.Exists(dotenv))
        {
            throw new ArgumentException(".env file not found!");
        }
        DotEnv.Load(dotenv);

        DatabaseManager.Init();

        DateTimeOffset lastReset    = DatabaseManager.ServerInfo.GetLastDailyReset();
        DateTimeOffset now          = DateTimeOffset.UtcNow;
        DateTime       lastMidnight = new(now.Year, now.Month, now.Day, 0, 0, 0, 0);

        // Check if lastReset is before lastMidnight
        if (lastReset < lastMidnight)
        {
            DailyReset();
        }

        // Schedule daily reset and repeat every 24 hours
        TaskScheduler.Instance.ScheduleTask(0, 0, 24, DailyReset);

        // Load Mob AI files
        string mobAiSchema = Path.Combine(Paths.AI_DIR, "mob-ai.xsd");

        MobAIManager.Load(Paths.AI_DIR, mobAiSchema);

        // Initialize all metadata.
        await MetadataHelper.InitializeAll();

        IContainer loginContainer = LoginContainerConfig.Configure();

        using ILifetimeScope loginScope = loginContainer.BeginLifetimeScope();
        _loginServer = loginScope.Resolve <LoginServer>();
        _loginServer.Start();

        IContainer gameContainer = GameContainerConfig.Configure();

        using ILifetimeScope gameScope = gameContainer.BeginLifetimeScope();
        _gameServer = gameScope.Resolve <GameServer>();
        _gameServer.Start();

        Logger.Info("All Servers have been Started.".ColorGreen());

        // Input commands to the server
        while (true)
        {
            string[] input = (Console.ReadLine() ?? string.Empty).Split(" ", 2);
            switch (input[0])
            {
            case "exit":
            case "quit":
                _gameServer.Stop();
                _loginServer.Stop();
                return;

            case "send":
                if (input.Length <= 1)
                {
                    break;
                }
                string       packet  = input[1];
                PacketWriter pWriter = new();
                pWriter.WriteBytes(packet.ToByteArray());
                Logger.Info(pWriter);

                foreach (Session session in GetSessions(_loginServer, _gameServer))
                {
                    Logger.Info($"Sending packet to {session}: {pWriter}");
                    session.Send(pWriter);
                }

                break;

            case "resolve":
                // How to use inside the PacketStructureResolver class
                PacketStructureResolver resolver = PacketStructureResolver.Parse(input[1]);
                if (resolver is null)
                {
                    break;
                }
                GameSession first = _gameServer.GetSessions().Single();
                resolver.Start(first);
                break;

            default:
                Logger.Info($"Unknown command:{input[0]} args:{(input.Length > 1 ? input[1] : "N/A")}");
                break;
            }
        }
    }