Beispiel #1
0
        /// <summary> Loads configuration settings from a file called botsettings.txt </summary>
        private void LoadConfig()
        {
            try {
                config = new Config("botsettings.txt");
                if (!config.Exists())
                {
                    using (StreamWriter sw = new StreamWriter("botsettings.txt")) {
                        sw.WriteLine("UseRemoteServer: false");
                        sw.WriteLine("RemotePort: ");
                        sw.WriteLine("RemotePassword: "******"CommandsRequireOperator: true");
                        sw.WriteLine("ReconnectAfterKick: true");
                        sw.WriteLine("SaveMap: false");
                        sw.WriteLine("Operators:");
                        sw.WriteLine("#And now, a little explaination on what all these mean.");
                        sw.WriteLine("#UseRemoteServer - Allows remote clients to connect and perform actions on the bot / chat through it. By default, this is disabled." +
                                     "If you choose to use the remote function, you may need to forward the port and/or add an exception to your firewall.");
                        sw.WriteLine("#RemotePort - The port the server will listen on for remote clients. It is fine to leave this blank if UseRemoteServer is false.");
                        sw.WriteLine("#RemotePassword - The password to use for verifying remote clients. " +
                                     "If UseRemoteServer is true and this is blank, the password will be set to \"password\". ");
                        sw.WriteLine("#CommandsRequireOperators - This determines whether bot commands require the person who called them to be in the operators file." +
                                     "Usually you would want this to be true.");
                        sw.WriteLine("#ReconnectAfterKick - This determines if the bot will reconnect after being kicked. Note that if the bot receives a kick packet before" +
                                     "a ServerIdentification packet, it will abort, and assume it has been banned from connecting.");
                        sw.WriteLine("#SaveMap - This determines if the bot will save the map when the chunk packets are sent to it." +
                                     "If this is true, it will be saved as a fCraft compatible map. (Large maps of 512 x 512 x 512 " +
                                     "can use up to ~150 megabytes of RAM when saving, so be wary. After saving, memory usage should return to normal.");
                        sw.WriteLine("#Operators: Comma separated list of operators, with no spaces. (e.g. test,test1,test2)");
                        Events.RaiseConfigCreating(new ConfigCreatingEventArgs(config, sw));
                    }
                }

                config.Load();
                if (!config.TryParseValueOrDefault("useremoteserver", false, out UseRemoteServer))
                {
                    Log(LogType.Warning, "Couldn't load value for useremoteserver from config. Setting to default value of false");
                }
                if (UseRemoteServer)
                {
                    int remotePort;
                    if (!config.TryParseValueOrDefault("remoteport", 25561, out remotePort))
                    {
                        Log(LogType.Warning, "Couldn't load value for remoteport from config. Setting to default value of 25561");
                    }
                    string remotePassword;
                    config.TryGetRawValue("remotepassword", out remotePassword);
                    if (String.IsNullOrEmpty(remotePassword))
                    {
                        remotePassword = "******";
                        Log(LogType.Warning, "Couldn't load value for remotepassword from config. Setting to default value of \"password\"");
                    }

                    server = new Server();
                    server.Start(this, remotePort, remotePassword);
                }

                if (!config.TryParseValueOrDefault("commandsrequireoperator", true, out _requiresop))
                {
                    Log(LogType.Warning, "Couldn't load value for commandsrequireoperator from config. Setting to default value of true");
                }
                if (!config.TryParseValueOrDefault("reconnectafterkick", true, out _reconnectonkick))
                {
                    Log(LogType.Warning, "Couldn't load value for reconnectafterkick from config. Setting to default value of true");
                }
                if (!config.TryParseValueOrDefault("savemap", false, out _savemap))
                {
                    Log(LogType.Warning, "Couldn't load value for savemap from config. Setting to default value of false");
                }
                string rawUsers;                 // Comma separated.
                config.TryGetRawValue("operators", out rawUsers);
                if (String.IsNullOrEmpty(rawUsers))
                {
                    Log(LogType.Warning, "Couldn't load value for operators from config. Setting to default value of empty.");
                }
                else
                {
                    string[] users      = rawUsers.Split(',');
                    bool     fixedNames = false;
                    for (int i = 0; i < users.Length; i++)
                    {
                        if (users[i].IndexOf(' ') != -1)
                        {
                            fixedNames = true;
                            users[i]   = users[i].Replace(" ", String.Empty);
                        }
                    }
                    if (fixedNames)
                    {
                        config.AddOrUpdateValue("operators", String.Join(",", users));
                        Log(LogType.BotActivity, "Fixed up spaces in the list of operators.");
                        config.Save();
                    }
                    Users.AddRange(users);
                }

                Events.RaiseConfigLoading(new ConfigLoadingEventArgs(config));
            } catch (Exception e) {
                Log(LogType.Error, "Couldn't load config:", e.ToString());
            }
        }