---
title: "Clyde学习笔记三（Config）"
author: "kevindude"
date: "2012-07-19T16:28:40Z"
tags: []
categories: ["编程语言"]
slug: "clyde-config"
draft: false
lastmod: "2012-07-19T16:28:40Z"
id: 1599470
url: "https://kevindude.iteye.com/blog/1599470"
---

在clyde中，几乎所有的外部资源都是以config的形式存在。每个config都有一个name来唯一标识这个config。ManagedConfig是所有config的基类，它是一个抽象类，定义了一些最基础的方法，ParameterizedConfig是它直接的子类。

 

**1. ParameterizedConfig**

 

ParameterizedConfig引入了parameter这一概念，利用parameter可以实现对config的个性化定制，这样可以复用绝大多数的config，并且减少config的数量。比如你有一个model，这个model在不同的场景需要应用不同的贴图，那么与其制作2个不同的config，不如利用parameter这一特性，在resource editor中选择创建一个direct parameter，给它一个名字叫“Texture”，然后把鼠标移到material mapping这一栏，按下ctrl-shift-C，复制material mapping属性的路径到剪贴板，然后再把此路径粘贴到刚才创建的parameter的path中。完成后，以后在代码中，可以这样来引用：

 

new Model(ctx, "flag.dat", "Texture", "texture.png");

![](http://dl.iteye.com/upload/attachment/0071/0100/8bd97c91-c7dd-3297-a235-ccb1fabf46f0.png)

 

“Texture”是parameter的name，“texture.png”是parameter的值。在其内部是以一个argumentMap来实现的。

 

 

```
/** The arguments applied to the configuration, if any. */
    @DeepOmit
    protected transient ArgumentMap _args;
```

 

以此也引申出base和derived概念。

 

 

```
    /** The instance from which the configuration is derived, if any (used to prevent the base
     * from being garbage-collected). */
    @DeepOmit
    protected transient ParameterizedConfig _base;

   /** Maps arguments to derived instances. */
    @DeepOmit
    protected transient SoftCache<ArgumentMap, ParameterizedConfig> _derived;
```

 

**2. ConfigReference**

 

ConfigReference以name和argumentMap属性保存了对一个config的引用，因为我们已经知道每个config都有一个name，但是这个name指向的config包括base和derived部分，所以以name和argumentMap可以唯一确定一个config的值。

 

 

```
    /** The name of the referenced configuration. */
    @Intern
    protected String _name;

    /** The arguments of the reference, mapped by name. */
    protected ArgumentMap _arguments = new ArgumentMap();
```

 

如果这个configReference是可以被编辑的，即标注有@Editable，在config editor中可以被编辑，并且在编辑时可以看到保存在configReference中的parameter的值。而且这个configreference是可以被嵌套的，比如说modelConfig中有materialMapping，materialMapping中有一个configReference&lt;MaterialConfig&gt;，在MaterialConfig中有一个configReference&lt;TextureConfig&gt;，那么在创建一个materialMapping的时候可以编辑一个MaterialConfig，在编辑MaterialConfig时可以再继续编辑TextureConfig。

 

**3. ConfigGroup**

 

在clyde中，几乎所有的配置都保存为config，都是从同一个基类中派生出来，但是从应用的角度来讲，一般可以分成2类，一类是所谓的managed config，像角色或者场景中的对象，它们在ConfigManager启动的时候被加载进来，保存在文件系统的rsrc/config路径下，并且在classpath中的rsrc/config路径下有一个manager.properties文件，一个典型配置文件看起来如下：

 

 

```
types = global, model, user_interface, scene

global.classes = com.threerings.tudey.config.ActorConfig, \
  com.threerings.tudey.config.AreaConfig, \
  com.threerings.tudey.config.BehaviorConfig, \
  com.threerings.opengl.gui.config.CursorConfig, \
  com.threerings.tudey.config.EffectConfig, \
  com.threerings.opengl.gui.config.FontConfig, \
  com.threerings.tudey.config.GroundConfig, \
  com.threerings.opengl.material.config.MaterialConfig, \

model.classes = com.threerings.opengl.gui.config.CursorConfig, \
  com.threerings.opengl.gui.config.FontConfig, \
  com.threerings.opengl.material.config.MaterialConfig, \

user_interface.classes = com.threerings.opengl.gui.config.CursorConfig, \
  com.threerings.opengl.gui.config.FontConfig, \
  com.threerings.opengl.gui.config.StyleConfig

scene.classes = com.threerings.tudey.config.ActorConfig, \
  com.threerings.tudey.config.AreaConfig, \
  com.threerings.tudey.config.EffectConfig, \
  com.threerings.tudey.config.GroundConfig, \

resource.classes = com.threerings.opengl.model.config.ModelConfig, \
  com.threerings.opengl.model.config.AnimationConfig, \
  com.threerings.opengl.gui.config.UserInterfaceConfig
```

 

在这里，每个config class对应的不再是一个单个的config，而是一个config group，每一个config group在文件系统rsrc/config路径下会有一个对应的.dat文件，在configManager启动时，会默认的读取type为global下每个config class对应的.dat文件，载入相应的config group。这些config可以在config editor中被编辑。

![](http://dl.iteye.com/upload/attachment/0071/0167/5a360e5d-aed5-3a30-a776-8bb552d01251.png)

在上图可以看到，global标签页对应的是global type，group对应的是每一个config class，在每个group下有多个config，这些config还可以以目录的形式组织起来。

 

除了managed config之外，还有单个形式的config，比如model或者animation，这些config只有在需要用到的时候才会被载入，model和animation是在resource editor中编辑的。