码迷,mamicode.com
首页 > 其他好文 > 详细

Scut 的参数导入方式(有遗留疑问)

时间:2016-08-13 17:57:27      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

  先上一段代码:

        public EnvironmentSetting()
        {
            var appServer = GetServerSection();
            var protocol = GetProtocolSection();
            var cacheSection = GetCacheSection();
            var scriptSection = GetScriptSection();

            CacheGlobalPeriod = cacheSection.ShareExpirePeriod;
            CacheUserPeriod = cacheSection.PersonalExpirePeriod;

            ScriptSysAsmReferences = scriptSection.SysAssemblyReferences;
            ScriptAsmReferences = scriptSection.AssemblyReferences;
            GamePort = protocol.GamePort;
            GameIpAddress = string.IsNullOrEmpty(protocol.GameIpAddress) ? GetLocalIp() : protocol.GameIpAddress;

            try
            {
                if (!string.IsNullOrEmpty(appServer.EntityAssemblyName))
                {
                    EntityAssembly = Assembly.LoadFrom(appServer.EntityAssemblyName);
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Load entity assembly error:\"{0}\" {1}", appServer.EntityAssemblyName, ex);
            }
            ActionDispatcher = new ScutActionDispatcher();
            InitSerializer();
            Reset();
        }

  可以看到,配置都是通过 GetServerSection、GetProtocolSection 等API导入的。

  再来看一下 GetServerSction 的具体操作:

        private static AppServerSection GetServerSection()
        {
            return ConfigManager.Configger.GetFirstOrAddConfig<AppServerSection>();
        }
        public AppServerSection()
        {
            ProductCode = ConfigUtils.GetSetting("Product.Code", 1);
            ProductName = ConfigUtils.GetSetting("Product.Name", "Game");
            ProductServerId = ConfigUtils.GetSetting("Product.ServerId", 1);
            UserLoginDecodeKey = ConfigUtils.GetSetting("Product.ClientDesDeKey", "");
            ClientVersion = new Version(1, 0, 0);
            Version ver;
            if (Version.TryParse(ConfigUtils.GetSetting("Product.ClientVersion", "1.0.0"), out ver))
            {
                ClientVersion = ver;
            }

            PublishType = ConfigUtils.GetSetting("PublishType", "Release");
            ActionTimeOut = ConfigUtils.GetSetting("ActionTimeOut", 500);
            LanguageTypeName = ConfigUtils.GetSetting("Game.Language.TypeName", "Game.src.Locale.DefaultLanguage");


            ActionTypeName = ConfigUtils.GetSetting("Game.Action.TypeName");
            if (string.IsNullOrEmpty(ActionTypeName))
            {
                string assemblyName = ConfigUtils.GetSetting("Game.Action.AssemblyName", "GameServer.CsScript");
                if (!string.IsNullOrEmpty(assemblyName))
                {
                    ActionTypeName = assemblyName + ".Action.Action{0}," + assemblyName;
                }
            }
            ScriptTypeName = ConfigUtils.GetSetting("Game.Action.Script.TypeName", "Game.Script.Action{0}");
            EntityAssemblyName = ConfigUtils.GetSetting("Game.Entity.AssemblyName");
            DecodeFuncTypeName = ConfigUtils.GetSetting("Game.Script.DecodeFunc.TypeName", "");
            RemoteTypeName = ConfigUtils.GetSetting("Game.Remote.Script.TypeName", "Game.Script.Remote.{0}");
            AccountServerUrl = ConfigUtils.GetSetting("AccountServerUrl", "");
        }

  ConfigUtils 是控制 app.config 配置的。那么,我们可以很清楚地明白,Scut 的配置,如果在 app.config 中有该字段,则使用配置文件的值,如果没有,则使用程序默认值。

 

  那么,我们是否可以完全由外部配置来启动,并更加清晰地管理外部配置呢?

        static EnvironmentSetting()
        {
            bool result;
            try
            {
                result = ConfigManager.Intialize("appServerConfigger");
            }
            catch (Exception)
            {
                result = false;
            }
            if (!result)
            {
                try
                {
                    ConfigManager.GetConfigger<DefaultAppConfigger>();
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError("Configger init error:{0}", ex);
                }
            }
            LoadDecodeFunc();
        }
        public static bool Intialize(string sectionName)
        {
            lock (syncRoot)
            {
                var section = ConfigurationManager.GetSection(sectionName);
                if (section is IConfigger)
                {
                    var instance = section as IConfigger;
                    instance.Install();
                    _configgerSet.Add(instance);
                    _configger = instance;
                    return true;
                }
                return false;
            }
        }

  我们可以在 static EnvironmentSetting 中,直接从配置文件中读取全部配置:

ConfigManager.Intialize("appServerConfigger");
ConfigManager.Intialize("ProtocolConfigger");
ConfigManager.Intialize("CacheConfigger");
ConfigManager.Intialize("ScriptConfigger");

  同时,从 DataDefaultConfigger 派生出 appServerConfigger、ProtocolConfigger、CacheConfigger、ScriptConfigger。

  每个 Configger 还应包括相应的配置:AppServerSection、ProtocolSection、CacheSection、ScriptSection。

  每个 Configger 还应重写 LoadConfigData 接口,将数据从 .config 中读入 appServerConfigger 的 AppServerSection 中...

  最后,还应在 app.config 中添加所有的自定义配置节。

 

  在写 app.config 的过程中出现了问题:

  首先测试性地修改 appServerConfigger,添加测试变量:

    public class DefaultAppConfigger : DefaultDataConfigger
    {
        public int ProductCode { get; set; }
        ...
    }
  <configSections> 
    <section name="appServerConfigger" type="ZyGames.Framework.Game.Runtime.DefaultAppConfigger, ZyGames.Framework.Game" requirePermission="false"/>
  </configSections>

  <appServerConfigger>
    <add key="ProductCode" value="0"/>
  </appServerConfigger>

技术分享

  这样在 GetSection 时,执行 add 操作时就会抛出异常了。


 

Scut 的参数导入方式(有遗留疑问)

标签:

原文地址:http://www.cnblogs.com/Daniel-Liang/p/5768345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!