Index: src/config/config.cs =================================================================== --- src/config/config.cs (revision 32) +++ src/config/config.cs (working copy) @@ -17,40 +17,222 @@ */ namespace Drive_LFSS.Config_ { - using System.Configuration; + using System.Collections.Generic; + using System.IO; + using System; + public static class Config + { + public struct ServerConfigStruct + { + public ServerConfigStruct(string _serverName, string _serverIP, ushort _portNumber, string _adminPass, char _commandPrefix, byte _insimOptionMask, ushort _netUpdateInterval, ushort _serverUpdateInterval) + { + serverName = _serverName; + serverIP = _serverIP; + portNumber = _portNumber; + adminPass = _adminPass; + commandPrefix = _commandPrefix; + insimOptionMask = _insimOptionMask; + netUpdateInterval = _netUpdateInterval; + serverUpdateInterval = _netUpdateInterval; + } + private string serverName; + private string serverIP; + private ushort portNumber; + private string adminPass; + private char commandPrefix; + private byte insimOptionMask; + private ushort netUpdateInterval; + private ushort serverUpdateInterval; + } + private const string CONFIG_FILE = "dlfss.cfg"; + private const string CONFIG_VERSION = "07282008"; - sealed class Config - { - - //System.Configuration.ConfigXmlDocument - //ConfigurationFileReader.ParmProcessor - // Delete the file if it exists. - /* public config() + private static byte processPriority = 0; + private static uint playerSaveInterval = 0; + private static uint DLFSSSUpdateRate = 0; + private static int logDisable = 0; //mask variable + + private static Dictionary serverList = new Dictionary(); + + public static bool Initialize() { - if (!File.Exists(configPath)) + if (!File.Exists(CONFIG_FILE)) { - // Create the file. - using (FileStream fileStream = File.Create(configPath)) + Program.log.error("Unable to find the config file, path : " + CONFIG_FILE + "\r\n"); + return false; + } + + using (StreamReader sr = new StreamReader(CONFIG_FILE)) + { + ushort lineNumber = 0; + string lineReaded = ""; + + while (!sr.EndOfStream) { - Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file."); + lineReaded = sr.ReadLine(); + lineNumber ++; - // AddToReceiveQueud some information to the file. - fileStream.Write(info, 0, info.Length); + if (!lineReaded.StartsWith("#") && lineReaded.Trim(' ') != "") + { + if (lineReaded.IndexOf('=') != -1) + { + if (!ValidLineSetting(lineReaded, lineNumber)) { return false; } + } + } } } - // Open the stream and read it back. - using (FileStream fs = File.OpenRead(path)) + + return true; + } + private static bool ValidLineSetting(string _line, ushort _lineNumber) + { + string[] args = _line.Substring(0, _line.IndexOf('=')).Trim(' ').Split(new string[] { "." }, 3, StringSplitOptions.RemoveEmptyEntries); + + if (args.Length > 1) { - byte[] b = new byte[1024]; - UTF8Encoding temp = new UTF8Encoding(true); - - while (fs.Read(b, 0, b.Length) > 0) + switch (args[0]) { - Console.WriteLine(temp.GetString(b)); + case "LFSServer": return DispatchServerOptions(_line, _lineNumber); + default: + { + Program.log.error("Error in config file at line : " + _lineNumber + ", unknow config option : " + args[0] + "\r\n"); + return false; + } } } - }*/ + else + { + switch (args[0]) + { + case "ConfVersion": if (!ValidConfVersion(_line)) { return false; }; break; + case "ProcessPriority": if (!SetProcessPriority(_line, _lineNumber)) { return false; }; break; + case "PlayerSaveInterval": if (!SetPlayerSaveInterval(_line, _lineNumber)) { return false; }; break; + case "DLFSSSUpdateRate": if (!SetDLFSSSUpdateRate(_line, _lineNumber)) { return false; }; break; + case "LogDisable": if (!SetLogDisable(_line, _lineNumber)) { return false; }; break; + default : + { + Program.log.error("Error in config file at line : " + _lineNumber + ", unknow config option : " + args[0] + "\r\n"); + return false; + } + } + } + + return true; + } + private static bool DispatchServerOptions(string _line, ushort _lineNumber) + { + string[] args = _line.Substring(0, _line.IndexOf('=')).Trim(' ').Split(new string[] { "." }, 3, StringSplitOptions.RemoveEmptyEntries); + + switch (args[2]) + { + case "ConnectionInfo": if (!CreateServer(_line, _lineNumber, args[1])) { return false; }; break; + default: + { + Program.log.error("Error in config file at line : " + _lineNumber + ", unknow server option config : " + args[2] + "\r\n"); + return false; + } + } + + return true; + } + private static bool CreateServer(string _line, ushort _lineNumber, string _serverName) + { + string tempoConf = _line.Substring(_line.IndexOf('=') + 1, _line.Length - (_line.IndexOf('=') + 1)).Trim(' '); + string[] args = tempoConf.Split(new string[] { ";" }, 7, StringSplitOptions.RemoveEmptyEntries); + + if (args.Length != 7) + { + Program.log.error("Error in config file at line : " + _lineNumber + ", incorrect parameter : " + tempoConf + "\r\n"); + return false; + } + + try + { + serverList.Add(serverList.Count + 1, new ServerConfigStruct(_serverName, args[0], Convert.ToUInt16(args[1]), args[2], Convert.ToChar(args[3]), Convert.ToByte(args[4]), Convert.ToUInt16(args[5]), Convert.ToUInt16(args[6]))); + //Program.log.normal("CreateServer name : " + _serverName + ", value : " + tempoConf + "\r\n"); + return true; + } + catch (Exception _exception) + { + Program.log.error("Error in config file at line : " + _lineNumber + ", incorrect parameter : " + tempoConf + "\r\n"); + return false; + } + } + private static bool ValidConfVersion(string _line) + { + if (CONFIG_VERSION == _line.Substring(_line.IndexOf('=') + 1, _line.Length - (_line.IndexOf('=') + 1)).Trim(' ')) + return true; + else + { + Program.log.error("Error wrong version of config file, current version is : " + CONFIG_VERSION + "\r\n"); + return false; + } + } + private static bool SetProcessPriority(string _line, ushort _lineNumber) + { + string valueToConvert = _line.Substring(_line.IndexOf('=') + 1, _line.Length - (_line.IndexOf('=') + 1)).Trim(' '); + + try + { + processPriority = Convert.ToByte(valueToConvert); + //Program.log.normal("SetProcessPriority value : " + processPriority + "\r\n"); + return true; + } + catch (Exception _exception) + { + Program.log.error("Error in config file at line : " + _lineNumber + ", incorrect parameter : " + valueToConvert + "\r\n"); + return false; + } + + } + private static bool SetPlayerSaveInterval(string _line, ushort _lineNumber) + { + string valueToConvert = _line.Substring(_line.IndexOf('=') + 1, _line.Length - (_line.IndexOf('=') + 1)).Trim(' '); + + try + { + playerSaveInterval = Convert.ToUInt32(valueToConvert); + //Program.log.normal("SetPlayerSaveInterval value : " + playerSaveInterval + "\r\n"); + return true; + } + catch (Exception _exception) + { + Program.log.error("Error in config file at line : " + _lineNumber + ", incorrect parameter : " + valueToConvert + "ms \r\n"); + return false; + } + } + private static bool SetDLFSSSUpdateRate(string _line, ushort _lineNumber) + { + string valueToConvert = _line.Substring(_line.IndexOf('=') + 1, _line.Length - (_line.IndexOf('=') + 1)).Trim(' '); + + try + { + DLFSSSUpdateRate = Convert.ToUInt32(valueToConvert); + //Program.log.normal("SetDLFSSSUpdateRate value : " + DLFSSSUpdateRate + "\r\n"); + return true; + } + catch (Exception _exception) + { + Program.log.error("Error in config file at line : " + _lineNumber + ", incorrect parameter : " + valueToConvert + "ms \r\n"); + return false; + } + } + private static bool SetLogDisable(string _line, ushort _lineNumber) + { + string valueToConvert = _line.Substring(_line.IndexOf('=') + 1, _line.Length - (_line.IndexOf('=') + 1)).Trim(' '); + + try + { + logDisable = Convert.ToInt32(valueToConvert); + return true; + } + catch (Exception _exception) + { + Program.log.error("Error in config file at line : " + _lineNumber + ", incorrect parameter : " + valueToConvert + "\r\n"); + return false; + } + } } } Index: src/drive_lfss/drive_lfss.cs =================================================================== --- src/drive_lfss/drive_lfss.cs (revision 32) +++ src/drive_lfss/drive_lfss.cs (working copy) @@ -24,6 +24,7 @@ namespace Drive_LFSS { + using Drive_LFSS.Config_; using Drive_LFSS.Definition_; using Drive_LFSS.Log_; using Drive_LFSS.CommandConsole_; @@ -68,12 +69,24 @@ { Console.ForegroundColor = ConsoleColor.Red; Console.Write("Can't Initialize the Log System, Will now QUIT.!\r\n\r\n"); + System.Threading.Thread.Sleep(10000); CommandConsole.Exec("exit"); return; } log = new sLog(); log.normal("Log System Initialized...\r\n\r\n"); + if (!Config.Initialize()) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.Write("Can't Initialize the Config, Will now QUIT.!\r\n\r\n"); + System.Threading.Thread.Sleep(10000); + CommandConsole.Exec("exit"); + return; + } + log.normal("Config Initialized...\r\n\r\n"); + + //Write Startup Banner WriteBanner();