---
title: "Mogre学习笔记（六）"
author: "zzkathy"
date: "2008-08-06T15:56:00Z"
tags: []
slug: "mogre"
draft: false
lastmod: "2008-08-06T15:56:00Z"
id: 391734
url: "https://zzkathy.iteye.com/blog/391734"
---

**Section 6: load configurations manually**

有时候由于某些原因，会需要去掉OGRE项目的配置文件，这时我们就需要在代码中hardcode，以定义这些资源。

我们在新建Root对象的时候，如果不设置参数，那么它会默认读取resource.cfg和plugins.cfg中的配置信息。因此，我们新建Root对象的时候应该使用如下代码：

new Root("", "");

这样，我们就可以在之后的代码中手动定义了，首先是定义resource.cfg：

ResourceGroupManager.Singleton.AddResourceLocation(archName, typeName, secName);

archName是需要加载的路径；typeName指定种类，多用FileSystem和Zip（大小写敏感）；secName指定ResourceGroup。我们可以hardcode这些配置，当然，我们也可以从文件中读入，给出示例代码如下：

ConfigFile cf = new ConfigFile();  
cf.Load("resources.cfg", "\\t:=", true);  
ConfigFile.SectionIterator seci = cf.GetSectionIterator();  
String secName, typeName, archName;

while (seci.MoveNext())  
{  
secName = seci.CurrentKey;  
ConfigFile.SettingsMultiMap settings = seci.Current;  
foreach (KeyValuePair&lt;string, string&gt; pair in settings)  
{  
typeName = pair.Key;  
archName = pair.Value;  
ResourceGroupManager.Singleton.AddResourceLocation(archName, typeName, secName);  
}  
}

然后是加载plugins.cfg，Root有一个LoadPlugin方法，例如加载Direct3D9：

Root.Singleton.LoadPlugin("RenderSystem\_Direct3D9");

这样，我们就把所需的配置文件定义好和插件手动加载完了。之后调用ResourceGroupManager的InitialiseAllResourceGroups方法加载：

ResourceGroupManager.Singleton.InitialiseAllResourceGroups();

但是，有些时候，我们可能会想在运行时加载一个Zip包，比如加载一个场景或模型，那么我们需要在加载以前添加代码：

ResourceGroupManager.Singleton.AddResourceLocation("model/model.zip", "Zip", "General");

但是，在实践中，却发现无法装载\*.material文件，需要手动解析，因此我们将\*.material文件解压，解析的代码如下：

FileStream fs = new FileStream(material, FileMode.Open);  
DataStream ds = new ManagedDataStream(fs);  
MaterialManager.Singleton.ParseScript(new DataStreamPtr(ds), "General");  
ds.Close();  
fs.Close();

这样，我们就完成了手动动态加载。