Example #1
0
        /// <summary>
        /// Loads the MasterServer configuration from the config file.
        /// If the file is not found it will create one with the help of the user.
        /// </summary>
        private void LoadConfig()
        {
            //Console.Write("Enter path of config file (Press enter if you want to use default path): ");
            //string tempPath = Console.ReadLine();
            //if (tempPath.Length > 0)
            //{
            //    defaultFilePath = tempPath;
            //    Console.WriteLine("Default path");
            //}

            string path = defaultConfigFilePath + defaultConfigFileName;

            Console.WriteLine($"Reading config file from: \"{path}\"...");
            if (File.Exists(path))
            {
                Console.WriteLine("Config file found.");
                string[] configs = File.ReadAllLines(path);

                foreach (var config in configs)
                {
                    if (config.Length > 0 && config[0] != '#')
                    {
                        string[] configDetails = config.Trim().Split('=');
                        switch (configDetails[0].ToLower())
                        {
                        case "authoryserverauthstring":
                            defaultAuthoryServerAuthString = configDetails[1];
                            break;

                        case "runninglocalhost":
                            isServerRunningLocalhost = bool.Parse(configDetails[1]);
                            break;

                        case "serverport":
                            defaultServerPort = int.Parse(configDetails[1]);
                            break;

                        case "mapsfolderpath":
                            defaultMapsFolderPath = configDetails[1];
                            break;

                        case "masterserverauthstring":
                            defaultMasterServerAuthString = configDetails[1];
                            break;

                        case "masterserverhost":
                            defaultMasterServerHost = configDetails[1];
                            break;

                        case "masterserverport":
                            defaultMasterServerPort = int.Parse(configDetails[1]);
                            break;
                        }
                    }
                }

                Console.WriteLine("Config file read: OK.");
            }
            else
            {
                try
                {
                    Console.WriteLine("Config file not found.");

                    Console.WriteLine("Setting up server configuration...");

                    if (defaultMapsFolderPath.Length == 0)
                    {
                        Console.Write("Please enter Maps folder path: ");
                        defaultMapsFolderPath = Console.ReadLine();
                    }

                    if (defaultAuthoryServerAuthString.Length == 0)
                    {
                        Console.Write("Please enter the Server Auth String: ");
                        defaultAuthoryServerAuthString = Console.ReadLine();
                    }

                    Console.WriteLine("Will the MasterServer run on the same network(localhost)? (Y/y for yes)");
                    isServerRunningLocalhost = (Console.ReadLine()?.Trim().ToLower()[0] == 'y' ? true : false);

                    if (defaultServerPort == 0)
                    {
                        Console.Write("Please enter Server Port: ");
                        defaultServerPort = int.Parse(Console.ReadLine());
                    }

                    if (defaultMasterServerAuthString.Length == 0)
                    {
                        Console.Write("Please enter MasterServer Auth String: ");
                        defaultMasterServerHost = Console.ReadLine();
                    }

                    if (defaultMasterServerHost.Length == 0)
                    {
                        Console.Write("Please enter MasterServer Host: ");
                        defaultMasterServerHost = Console.ReadLine();
                    }

                    if (defaultMasterServerPort == 0)
                    {
                        Console.Write("Please enter MasterServer Port: ");
                        defaultMasterServerPort = int.Parse(Console.ReadLine());
                    }

                    Console.WriteLine("Save configuration? (Y/y for yes)");
                    if (Console.ReadLine()?.Trim().ToLower()[0] == 'y')
                    {
                        Console.WriteLine("Saving server configuration...");

                        string[] defaultContent = new string[1] {
                            "" +
                            "#Server details:" +
                            $"\nMapsFolderPath={defaultMapsFolderPath}" +
                            $"\nServerAuthString={defaultAuthoryServerAuthString}" +
                            $"\nServerRunningLocalHost={isServerRunningLocalhost}" +
                            $"\nServerPort={defaultServerPort}" +
                            "\n#MasterServer details:" +
                            $"\nMasterServerAuthString={defaultMasterServerAuthString}" +
                            $"\nMasterServerHost={defaultMasterServerHost}" +
                            $"\nMasterServerPort={defaultMasterServerPort}"
                        };

                        File.WriteAllLines(path, defaultContent);

                        Console.WriteLine("Server configuration saved.");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    LoadConfig();
                }
            }
        }