示例#1
0
        public static SystemConfig LoadFromXml(Stream stream, bool isEncrypted)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            using (XmlTextReader reader = new XmlTextReader(stream))
            {
                if (reader != null)
                {
                    SystemConfig config = new SystemConfig();
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element && reader.Name == XmlElementConfig)
                        {
                            reader.MoveToAttribute(XmlAttribute);
                            string attribute = reader.ReadContentAsString();
                            switch (attribute)
                            {
                            case "MaxConnectionCount":
                                reader.MoveToAttribute(XmlValue);
                                config._maxConnectionCount = (ushort)reader.ReadContentAsInt();
                                break;

                            case "AdminServerListenIP":
                                reader.MoveToAttribute(XmlValue);
                                config._adminServerListenIP = reader.ReadContentAsString();
                                break;

                            case "AdminServerListenPort":
                                reader.MoveToAttribute(XmlValue);
                                config._adminServerListenPort = reader.ReadContentAsInt();
                                break;

                            case "SecondaryAdminServerListenPort":
                                reader.MoveToAttribute(XmlValue);
                                config._secondaryAdminServerListenPort = reader.ReadContentAsInt();
                                break;

                            case "AdminServerUploadFileRootPath":
                                reader.MoveToAttribute(XmlValue);
                                config._adminServerUploadFileRootPath = reader.ReadContentAsString();

                                if (!config._adminServerUploadFileRootPath.EndsWith(@"\"))
                                {
                                    config._adminServerUploadFileRootPath += @"\";
                                }

                                //若该文件夹不存在,则创建它
                                if (Directory.Exists(config._adminServerUploadFileRootPath) == false)
                                {
                                    Directory.CreateDirectory(config._adminServerUploadFileRootPath);
                                }

                                break;

                            case "DbConnectionString":
                            {
                                reader.MoveToAttribute(XmlValue);
                                string readConnectionString = reader.ReadContentAsString();
                                if (isEncrypted)
                                {
                                    try
                                    {
                                        config._dbConnectionString = CryptographyManager.TheInstance.PredefinedDecrypt(readConnectionString, Encoding.Default);
                                    }
                                    catch (Exception ex)
                                    {
                                        throw new Exception("Decrypt SystemConfig Entry \"DbConnectionString\" Failed.", ex);
                                    }
                                }
                                else
                                {
                                    config._dbConnectionString = readConnectionString;
                                }
                            }
                            break;

                            case "DefaultDbName":
                                reader.MoveToAttribute(XmlValue);
                                config._defaultDbName = reader.ReadContentAsString();
                                break;

                            case "DefaultEncoding":
                                reader.MoveToAttribute(XmlValue);
                                config._defaultEncoding = Encoding.GetEncoding(reader.ReadContentAsString());
                                break;

                            case "DefaultNewLine":
                            {
                                reader.MoveToAttribute(XmlValue);
                                string newLine = reader.ReadContentAsString();
                                if (newLine == @"\n")
                                {
                                    config._defaultNewLine = "\n";
                                }
                                else if (newLine == @"\r\n")
                                {
                                    config._defaultNewLine = "\r\n";
                                }
                            }
                            break;

                            case "LogDir":
                                reader.MoveToAttribute(XmlValue);
                                config._logDir = reader.ReadContentAsString();

                                if (!config._logDir.EndsWith(@"\"))
                                {
                                    config._logDir += @"\";
                                }

                                //若该文件夹不存在,则创建它
                                if (Directory.Exists(config._logDir) == false)
                                {
                                    Directory.CreateDirectory(config._logDir);
                                }

                                break;

                            case "LogEnabled":
                                reader.MoveToAttribute(XmlValue);
                                config._logEnabled = reader.ReadContentAsBoolean();
                                break;

                            case "ListenForConfigGuardReturnPort":
                                reader.MoveToAttribute(XmlValue);
                                config._listenForConfigGuardReturnPort = reader.ReadContentAsInt();
                                break;

                            case "ConfigGuardPort":
                                reader.MoveToAttribute(XmlValue);
                                config._configGuardPort = reader.ReadContentAsInt();
                                break;

                            case "NeedGuardAuthorization":
                                reader.MoveToAttribute(XmlValue);
                                config._needGuardAuthorization = reader.ReadContentAsBoolean();
                                break;

                            case "GameSettingDir":
                                reader.MoveToAttribute(XmlValue);
                                config._gameSettingDir = reader.ReadContentAsString();

                                if (!config._gameSettingDir.EndsWith(@"\"))
                                {
                                    config._gameSettingDir += @"\";
                                }

                                //若该文件夹不存在,则创建它
                                if (Directory.Exists(config._gameSettingDir) == false)
                                {
                                    Directory.CreateDirectory(config._gameSettingDir);
                                }

                                break;

                            case "GuardAuthorizationTimeout":
                                reader.MoveToAttribute(XmlValue);
                                config._guardAuthorizationTimeout = reader.ReadContentAsLong();
                                break;

                            case "EnablePaysys":
                                reader.MoveToAttribute(XmlValue);
                                config._enablePaysys = reader.ReadContentAsBoolean();
                                break;

                            case "DBDefaultIP":
                                reader.MoveToAttribute(XmlValue);
                                config.dbDefaultIP = reader.ReadContentAsString();
                                break;

                            case "DefaultGameDbName":
                                reader.MoveToAttribute(XmlValue);
                                config.defaultGameDbName = reader.ReadContentAsString();
                                break;

                            case "PaySysDbConnectionString":
                            {
                                reader.MoveToAttribute(XmlValue);
                                string readConnectionString = reader.ReadContentAsString();
                                if (config._enablePaysys)
                                {
                                    if (isEncrypted)
                                    {
                                        try
                                        {
                                            config._paySysDbConnectionString = CryptographyManager.TheInstance.PredefinedDecrypt(readConnectionString, Encoding.Default);
                                        }
                                        catch (Exception ex)
                                        {
                                            throw new Exception("Decrypt SystemConfig Entry \"PaySysDbConnectionString\" Failed.", ex);
                                        }
                                    }
                                    else
                                    {
                                        config._paySysDbConnectionString = readConnectionString;
                                    }
                                }
                            }
                            break;

                            case "PreloadGameData":
                                reader.MoveToAttribute(XmlValue);
                                config._preloadGameData = reader.ReadContentAsBoolean();
                                break;

                            case "ServerListFileDir":
                                reader.MoveToAttribute(XmlValue);
                                config._serverListFileDir = reader.ReadContentAsString();

                                if (!config._serverListFileDir.EndsWith(@"\"))
                                {
                                    config._serverListFileDir += @"\";
                                }

                                //若该文件夹不存在,则创建它
                                if (Directory.Exists(config._serverListFileDir) == false)
                                {
                                    Directory.CreateDirectory(config._serverListFileDir);
                                }

                                break;

                            case "ServerListFileName":
                                reader.MoveToAttribute(XmlValue);
                                config._serverListFileName = reader.ReadContentAsString();
                                break;

                            case "PakFileName":
                                reader.MoveToAttribute(XmlValue);
                                config._pakFileName = reader.ReadContentAsString();
                                break;

                            case "ServerListCRCFileName":
                                reader.MoveToAttribute(XmlValue);
                                config._serverListCRCFileName = reader.ReadContentAsString();
                                break;

                            case "ServerListServerGroupMaxPlayer":
                                reader.MoveToAttribute(XmlValue);
                                config._serverListServerGroupMaxPlayer = reader.ReadContentAsInt();
                                break;

                            case "SMSUrl":
                                reader.MoveToAttribute(XmlValue);
                                config._smsUrl = reader.ReadContentAsString();
                                break;

                            case "SMSCommand":
                                reader.MoveToAttribute(XmlValue);
                                config._smsCommand = reader.ReadContentAsString();
                                break;

                            case "WebSiteVersion":
                                reader.MoveToAttribute(XmlValue);
                                config.websiteVersion = reader.ReadContentAsString();
                                break;

                            case "GroupPlayerCountDir":
                                reader.MoveToAttribute(XmlValue);
                                config._groupPlayerCountDir = reader.ReadContentAsString();
                                if (!config._groupPlayerCountDir.EndsWith(@"\"))
                                {
                                    config._groupPlayerCountDir += @"\";
                                }

                                //若该文件夹不存在,则创建它
                                if (Directory.Exists(config._groupPlayerCountDir) == false)
                                {
                                    Directory.CreateDirectory(config._groupPlayerCountDir);
                                }
                                break;

                            case "GroupPlayerCountFileName":
                                reader.MoveToAttribute(XmlValue);
                                config._groupPlayerCountFileName = reader.ReadContentAsString();
                                break;

                            case "GroupPlayerCountKickInterval":
                                reader.MoveToAttribute(XmlValue);
                                config._groupPlayerCountKickInterval = reader.ReadContentAsInt();
                                break;

                            default:
                                throw new Exception("Unknown attribute '" + attribute + "' found in xml stream.");
                            }
                        }
                    }
                    return(config);
                }
            }

            return(null);
        }
示例#2
0
		public static SystemConfig LoadFromXml(Stream stream, bool isEncrypted)
		{
			if (stream == null)
			{
				throw new ArgumentNullException("stream");
			}

			using (XmlTextReader reader = new XmlTextReader(stream))
			{
				if (reader != null)
				{
					SystemConfig config = new SystemConfig();
					while (reader.Read())
					{
						if (reader.NodeType == XmlNodeType.Element && reader.Name == XmlElementConfig)
						{
							reader.MoveToAttribute(XmlAttribute);
							string attribute = reader.ReadContentAsString();
							switch (attribute)
							{
								case "MaxConnectionCount":
									reader.MoveToAttribute(XmlValue);
									config._maxConnectionCount = (ushort)reader.ReadContentAsInt();
									break;
                                case "AdminServerListenIP":
                                    reader.MoveToAttribute(XmlValue);
                                    config._adminServerListenIP = reader.ReadContentAsString();
                                    break;
								case "AdminServerListenPort":
									reader.MoveToAttribute(XmlValue);
									config._adminServerListenPort = reader.ReadContentAsInt();
									break;                                
								case "SecondaryAdminServerListenPort":
									reader.MoveToAttribute(XmlValue);
									config._secondaryAdminServerListenPort = reader.ReadContentAsInt();
									break;
								case "AdminServerUploadFileRootPath":
									reader.MoveToAttribute(XmlValue);
									config._adminServerUploadFileRootPath = reader.ReadContentAsString();

                                    if (!config._adminServerUploadFileRootPath.EndsWith(@"\")) config._adminServerUploadFileRootPath += @"\";

                                    //若该文件夹不存在,则创建它
                                    if (Directory.Exists(config._adminServerUploadFileRootPath) == false)
                                    {
                                        Directory.CreateDirectory(config._adminServerUploadFileRootPath);
                                    }

									break;
								case "DbConnectionString":
									{
										reader.MoveToAttribute(XmlValue);
										string readConnectionString = reader.ReadContentAsString();
										if (isEncrypted)
										{
											try
											{
												config._dbConnectionString = CryptographyManager.TheInstance.PredefinedDecrypt(readConnectionString, Encoding.Default);
											}
											catch (Exception ex)
											{
												throw new Exception("Decrypt SystemConfig Entry \"DbConnectionString\" Failed.", ex);
											}
										}
										else
										{
											config._dbConnectionString = readConnectionString;
										}
									}
									break;
								case "DefaultDbName":
									reader.MoveToAttribute(XmlValue);
									config._defaultDbName = reader.ReadContentAsString();
									break;
								case "DefaultEncoding":
									reader.MoveToAttribute(XmlValue);
									config._defaultEncoding = Encoding.GetEncoding(reader.ReadContentAsString());
									break;
								case "DefaultNewLine":
									{
										reader.MoveToAttribute(XmlValue);
										string newLine = reader.ReadContentAsString();
										if (newLine == @"\n")
										{
											config._defaultNewLine = "\n";
										}
										else if (newLine == @"\r\n")
										{
											config._defaultNewLine = "\r\n";
										}
									}
									break;
								case "LogDir":
									reader.MoveToAttribute(XmlValue);
									config._logDir = reader.ReadContentAsString();

                                    if (!config._logDir.EndsWith(@"\")) config._logDir+=@"\";

                                    //若该文件夹不存在,则创建它
                                    if (Directory.Exists(config._logDir) == false)
                                    {
                                        Directory.CreateDirectory(config._logDir);
                                    }

									break;
								case "LogEnabled":
									reader.MoveToAttribute(XmlValue);
									config._logEnabled = reader.ReadContentAsBoolean();
									break;
								case "ListenForConfigGuardReturnPort":
									reader.MoveToAttribute(XmlValue);
									config._listenForConfigGuardReturnPort = reader.ReadContentAsInt();
									break;
								case "ConfigGuardPort":
									reader.MoveToAttribute(XmlValue);
									config._configGuardPort = reader.ReadContentAsInt();
									break;
								case "NeedGuardAuthorization":
									reader.MoveToAttribute(XmlValue);
									config._needGuardAuthorization = reader.ReadContentAsBoolean();
									break;
								case "GameSettingDir":
									reader.MoveToAttribute(XmlValue);
                                    config._gameSettingDir = reader.ReadContentAsString();

                                    if (!config._gameSettingDir.EndsWith(@"\")) config._gameSettingDir += @"\";

                                    //若该文件夹不存在,则创建它
                                    if (Directory.Exists(config._gameSettingDir) == false)
                                    {
                                        Directory.CreateDirectory(config._gameSettingDir);
                                    }
									
									break;
								case "GuardAuthorizationTimeout":
									reader.MoveToAttribute(XmlValue);
									config._guardAuthorizationTimeout = reader.ReadContentAsLong();
									break;
                                case "EnablePaysys":
                                    reader.MoveToAttribute(XmlValue);
                                    config._enablePaysys = reader.ReadContentAsBoolean();
                                    break;
                                case "DBDefaultIP":
                                    reader.MoveToAttribute(XmlValue);
                                    config.dbDefaultIP = reader.ReadContentAsString();
                                    break;
                                case "DefaultGameDbName":
                                    reader.MoveToAttribute(XmlValue);
                                    config.defaultGameDbName = reader.ReadContentAsString();
                                    break;
								case "PaySysDbConnectionString":
									{
										reader.MoveToAttribute(XmlValue);
										string readConnectionString = reader.ReadContentAsString();
                                        if (config._enablePaysys)
                                        {
                                            if (isEncrypted)
                                            {
                                                try
                                                {
                                                    config._paySysDbConnectionString = CryptographyManager.TheInstance.PredefinedDecrypt(readConnectionString, Encoding.Default);
                                                }
                                                catch (Exception ex)
                                                {
                                                    throw new Exception("Decrypt SystemConfig Entry \"PaySysDbConnectionString\" Failed.", ex);
                                                }
                                            }
                                            else
                                            {
                                                config._paySysDbConnectionString = readConnectionString;
                                            }
                                        }										
									}
									break;
								case "PreloadGameData":
									reader.MoveToAttribute(XmlValue);
									config._preloadGameData = reader.ReadContentAsBoolean();
									break;
                                case "ServerListFileDir":
                                    reader.MoveToAttribute(XmlValue);
                                    config._serverListFileDir = reader.ReadContentAsString();
                                    
                                    if (!config._serverListFileDir.EndsWith(@"\")) config._serverListFileDir += @"\";

                                    //若该文件夹不存在,则创建它
                                    if (Directory.Exists(config._serverListFileDir) == false)
                                    {
                                        Directory.CreateDirectory(config._serverListFileDir);
                                    }

                                    break;
                                case "ServerListFileName":
                                    reader.MoveToAttribute(XmlValue);
                                    config._serverListFileName = reader.ReadContentAsString();
                                    break;
                                case "PakFileName":
                                    reader.MoveToAttribute(XmlValue);
                                    config._pakFileName = reader.ReadContentAsString();
                                    break;
                                case "ServerListCRCFileName":
                                    reader.MoveToAttribute(XmlValue);
                                    config._serverListCRCFileName = reader.ReadContentAsString();
                                    break;
                                case "ServerListServerGroupMaxPlayer":
                                    reader.MoveToAttribute(XmlValue);
                                    config._serverListServerGroupMaxPlayer = reader.ReadContentAsInt();
                                    break;
                                case "SMSUrl":
                                    reader.MoveToAttribute(XmlValue);
                                    config._smsUrl = reader.ReadContentAsString();
                                    break;
                                case "SMSCommand":
                                    reader.MoveToAttribute(XmlValue);
                                    config._smsCommand = reader.ReadContentAsString();
                                    break;
                                case "WebSiteVersion":
                                    reader.MoveToAttribute(XmlValue);
                                    config.websiteVersion = reader.ReadContentAsString();
                                    break;
                                case "GroupPlayerCountDir":
                                    reader.MoveToAttribute(XmlValue);
                                    config._groupPlayerCountDir = reader.ReadContentAsString();
                                    if (!config._groupPlayerCountDir.EndsWith(@"\")) config._groupPlayerCountDir += @"\";

                                    //若该文件夹不存在,则创建它
                                    if (Directory.Exists(config._groupPlayerCountDir) == false)
                                    {
                                        Directory.CreateDirectory(config._groupPlayerCountDir);
                                    }
                                    break;
                                case "GroupPlayerCountFileName":
                                    reader.MoveToAttribute(XmlValue);
                                    config._groupPlayerCountFileName = reader.ReadContentAsString();
                                    break;
                                case "GroupPlayerCountKickInterval":
                                    reader.MoveToAttribute(XmlValue);
                                    config._groupPlayerCountKickInterval = reader.ReadContentAsInt();
                                    break;
								default:
									throw new Exception("Unknown attribute '" + attribute + "' found in xml stream.");
							}
						}
					}
					return config;
				}
			}

			return null;
		}