unrealscript中有两个与属性自动配置相关的关键字:

config/globalconfig

当把它们应用于属性时,对象在创建后,该属性的初始值会被自动设置为相对应ini文件中的值。

举例来说,如有一个类:

class HNet extends Object
config(game)
native(net);
//var globalconfig string host;
var config string host;
function test() { `Log("HNet test, host is:"@host); }

声明一个带config修饰的属性host,

那么在创建该类实例时,其host会自动设为配置在DefaultGame.ini中的值,其section为【包名.类名】,key就是属性名【host】:

[HGame.HNet]
host=hz.19v5.com

如果ini中没有配置的话,默认值就是空了(或者说该类型自身的默认值)

那么globalconfig区别何在呢?这就与子类的变化有关了

假设有一个子类:

class HHttp extends HNet
config(Net)
; function test()
{
`Log("HHttp test, host is:"@host);
}

它有自己的config文件,那么它的host属性,就会去自己的ini中寻找,上例中就是DefaultNet.ini了,

[HGame.HHttp]
host=baidu.com

如果它里面也配置了相应字段,那么在HHttp的实例中,host的值会是baidu.com,而非hz.19v5.com。

而如果作为基类的HNet有这样一种需求:认为其host属性的初始值很重要,不应该被子类乱改,那么就可以改用globalconfig:

用globalconfig修饰的属性,一定会在它的当前类绑定的ini文件中寻找初始值,而无视实际所处子类的ini变更。

在上例中如果host前使用的是globalconfig,那么即使是HHttp类的实例,其host初始值也仍是hz.19v5.com。

功能基本如上所述,下面归纳一下代码中的实现逻辑:

1、首先,UnObjBas.h里定了对应这两个修饰符的标记:

#define CPF_Config       DECLARE_UINT64(0x0000000000004000) // Property should be loaded/saved as permanent profile.
#define CPF_GlobalConfig DECLARE_UINT64(0x0000000000040000) // Load config from base class, not subclass.

2、在编译脚本时UnSrcCom.cpp,检测到属性声明中有这两修饰符时,会贴上相应标记:

        FToken Specifier;
GetToken(Specifier);
if( Specifier.Matches(NAME_Const) )
{
Flags |= CPF_Const;
IsVariable = TRUE;
}
else if( Specifier.Matches(NAME_Config) )
{
Flags |= CPF_Config;
IsVariable = TRUE;
}
else if( Specifier.Matches(NAME_GlobalConfig) )
{
Flags |= CPF_GlobalConfig | CPF_Config;
IsVariable = TRUE;
}

3、生成的UClass结构里两个关键属性:字段表和默认对象

UField*          Children
UObject*          ClassDefaultObject

在脚本里声明的每个属性都是一个UProperty变量,而它正是UField的子类:

//
// An UnrealScript variable.
//
class UProperty : public UField
{
DECLARE_ABSTRACT_CASTED_CLASS_INTRINSIC(UProperty,UField,,Core,CASTCLASS_UProperty)
DECLARE_WITHIN(UField) // Persistent variables.
INT ArrayDim;
INT ElementSize;
QWORD PropertyFlags;
WORD RepOffset;
WORD RepIndex;

另外,“默认对象”ClassDefaultObject是unrealscript里的一个特殊概念,每个UClass类都有一个,其作用是:

a、在类加载时,先把ClassDefaultObject创建好,并把各属性初始值(defaultproperties块里的、config文件里的)设置好;
b、以后每次创建实例对象时,都从ClassDefaultObject上拷贝各属性的初始值。

4、创建ClassDefaultObject的地方:

native类:也就是每个UClass初始化时Register自己的地方 纯脚本类:很明显,就是从package里加载数据,刚好加载到一个类时

5、ClassDefaultObject读取初始值的地方:

与脚本类的创建不远,同在LoadAllObjects里。

LoadConfig里的逻辑很复杂,但总括起来就两点:去哪里找数据和怎么读,各自都依据情形有大量衍生。但基本的读数据过程都是:

    for ( UProperty* Property = ConfigClass->PropertyLink; Property; Property = Property->PropertyLinkNext )
{ const UBOOL bGlobalConfig = (Property->PropertyFlags&CPF_GlobalConfig) != ;
UClass* OwnerClass = Property->GetOwnerClass(); UClass* BaseClass = bGlobalConfig ? OwnerClass : ConfigClass;
if ( !bPerObject )
{
ClassSection = BaseClass->GetPathName();
} // globalconfig properties should always use the owning class's config file
// specifying a value for InFilename will override this behavior (as it does with normal properties)
const FString& PropFileName = (bGlobalConfig && InFilename == NULL) ? OwnerClass->GetConfigName() : Filename; FString Key = Property->GetName();
debugfSuppressed(NAME_DevSave, TEXT(" Loading value for %s from [%s]"), *Key, *ClassSection);
UArrayProperty* Array = Cast<UArrayProperty>( Property );
if( Array == NULL )
{
for( INT i=; i<Property->ArrayDim; i++ )
{
if( Property->ArrayDim!= )
{
Key = FString::Printf(TEXT("%s[%i]"), *Property->GetName(), i);
} FString Value;
if( GConfig->GetString( *ClassSection, *Key, Value, *PropFileName ) )
{
if (Property->ImportText(*Value, (BYTE*)this + Property->Offset + i*Property->ElementSize, PPF_ConfigOnly, this) == NULL)
{
// this should be an error as the properties from the .ini / .int file are not correctly being read in and probably are affecting things in subtle ways
warnf(NAME_Error, TEXT("LoadConfig (%s): import failed for %s in: %s"), *GetPathName(), *Property->GetName(), *Value);
}
          }
       }
     }
     else
       ……
   ……
   }
}

也就是遍历每个属性ConfigClass->PropertyLink,逐个解析其配置源、类型(单个还是数组)等问题后,然后一个Property->ImportText,就把数据读进来了。

注意这里对bGlobalConfig的判断:

 const FString& PropFileName = (bGlobalConfig && InFilename == NULL) ? OwnerClass->GetConfigName() : Filename;

也就是据此决定了从哪个类的配置文件中去获取数据,实现了上面提到的功能。

6、普通实例创建时从ClassDefaultObject中复制属性数据

也就是在UObject::InitProperties中,完成必要属性(有设定过初值的)的复制:

// Construct anything required.
if( DefaultsClass && DefaultData )
{
for( UProperty* P=DefaultsClass->ConstructorLink; P; P=P->ConstructorLinkNext )
{
if( P->Offset < DefaultsCount )
{
// skip if SourceOwnerObject != NULL and this is a transient property - in this
// situation, the new value for the property has already been copied from the class defaults
// in the block of code above
if ( SubobjectRoot == NULL || !P->HasAnyPropertyFlags(CPF_Transient|CPF_DuplicateTransient) )
{
// the heap memory allocated at this address is owned the source object, so
// zero out the existing data so that the UProperty code doesn't attempt to
// de-allocate it before allocating the memory for the new value
appMemzero( Data + P->Offset, P->GetSize() );//bad for bools, but not a real problem because they aren't constructed!!
P->CopyCompleteValue( Data + P->Offset, DefaultData + P->Offset, SubobjectRoot ? SubobjectRoot : DestObject, DestObject, InstanceGraph );
}
}
}
}

总结:通过以上6步,实现了unrealscript class里带config/globalconfig修饰符属性的自动从配置中加载的功能。

unreal3对象属性自动从配置文件中加载的机制的更多相关文章

  1. Winform中设置DevExpress的RadioGroup的items从配置文件中加载

    场景 DevExpress的RadioGroup的items选项如果是不确定的话,需要其从配置文件中加载. 实现 在项目目录下新建Config文件夹,文件夹下新建xml配置文件. <?xml v ...

  2. Prism 学习:从配置文件中加载 Module

    之前我们已经了解过,如何从指定的目录中来加载 Module(原文),现在我们来看,如何从应用程序的配置文件中来加载 Module.以这种方式来加载 Module 的优点包括:1. 被加载的 Modul ...

  3. spring 配置参数从配置文件中加载到PropertiesFactoryBean 和配置参数从数据库加载到PropertiesFactoryBean 的实现,及项目中的相关应用

    1.加载.properties文件中的配置参数加载到PropertiesFactoryBean容器中 <bean id="configProperties" class=&q ...

  4. Spring中查看加载配置文件中 加载类的个数及详情

    断点到: org.springframework.beans.factory.support.DefaultListableBeanFactory#getBeanDefinitionCount 显示该 ...

  5. C#构造方法(函数) C#方法重载 C#字段和属性 MUI实现上拉加载和下拉刷新 SVN常用功能介绍(二) SVN常用功能介绍(一) ASP.NET常用内置对象之——Server sql server——子查询 C#接口 字符串的本质 AJAX原生JavaScript写法

    C#构造方法(函数)   一.概括 1.通常创建一个对象的方法如图: 通过  Student tom = new Student(); 创建tom对象,这种创建实例的形式被称为构造方法. 简述:用来初 ...

  6. Flexigrid从对象中加载数据

    (有问题,在找…………) Flexigrid是用来动态加载数据的一种比较好(老)的Jquery表插件,然后有些时候,我们需要其从本地或者jQuery对象中加载数据,比如有这么个需求,页面显示中有两个表 ...

  7. web 中加载配置文件

    1.web.xml中配置   <!-- 加载配置文件 -->   <listener>      <description>ServletContextListen ...

  8. web.xml中如何设置配置文件的加载路径

    web应用程序通过Tomcat等容器启动时,会首先加载web.xml文件,通常我们工程中的各种配置文件,如日志.数据库.spring的文件等都在此时被加载,下面是两种常用的配置文件加载路径,即配置文件 ...

  9. Spring中加载xml配置文件的六种方式

    Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog  因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...

随机推荐

  1. C#微信json结构接收参数 转载

    http://blog.csdn.net/u010773333/article/details/48524155 发素材的时间要上传资源故此要用json格式数据,需要转化. 微信服务器交互基本上都是j ...

  2. DataTable排序(转)

    DataTable 排序   DataRow[] rows = dataTable1.Select("", "ord asc");   DataTable t ...

  3. python_day3

    一.set的基本操作 >>> s1=set("abc123abc") >>> s1 #把字符串转化为set,去重 set([']) >&g ...

  4. petapoco IsNew

    // Check if a poco represents a new record public bool IsNew(string primaryKeyName, object poco) { v ...

  5. Change the Windows 7 Taskbar Thumbnail and List Mode

    Manually in Registry Editor 1. Open the Start Menu, then type regedit in the search boxand press Ent ...

  6. Redis的Python客户端redis-py的初步使用

    1. Redis的安装 sudo pip install redis sudo pip install hiredis Parser可以控制如何解析redis响应的内容.redis-py包含两个Par ...

  7. 什么是Mbps、Kbps、bps、kb、mb及其换算和区别

    Mbps 即 Milionbit pro second(百万位每秒): Kbps 即 Kilobit pro second(千位每秒): bps 即 bit pro second(位每秒): 速度单位 ...

  8. apache相关

    http://hw1287789687.iteye.com/blog/2212292 http://enable-cors.org/server_apache.html http://blog.sin ...

  9. 函数的定义和声明以及this

    this = $(this)[0]; var person = { name : "lisa", age : "20", init : function(){ ...

  10. 利用should.js进行测试

    nodejs 环境 , 安装should.js包 (npm install should) var should = require('should'); //正确1, 错误0 100 precent ...