使用std.zip 编写文件打包管理器:)
这里有D语言的专区啊。真是不错。。我也随便发一贴好了。。关于std.zip 包,这个包使用std.zlib作为其zip支持,且使用OO的方法封装了zip的压缩和解压缩,非常方便。这里对其方法就不详细讲解了。可以去看D主页的说明。我们可以使用这个东西,从zip文件(也可以伪装成。dat哦)里面加载文件数据,如果使用sdl的话可以用sdl的rwops结构加载数据就可以在zip文件里面加载资源了。
我把我的写的ResourceLoader贴出来。由于我是D的新手可能写的不好,请大家多原谅。。
module YDGE.Engine.ResourceLoader;
import std.stdio;
import std.file;
import std.zip;
class ResourceLoader
{
// constructor
public
{
this ()
{
}
}
// functions
public
{
bool addArchive(char [] fileName)
{
// if the file is already in list
if ((fileName in dataList) != null)
return false;
// try to open the file
void [] data;
ZipArchive archive;
try
{
data = read(fileName);
}
catch (Exception e)
{
writefln("Error in opening file :", fileName);
return false;
}
try
{
archive = new ZipArchive(data);
}
catch (Exception e)
{
writefln("Error in create zip archive :",fileName);
return false;
}
// insert the file
dataList = archive;
return true;
}
bool removeArchive(char [] fileName)
{
// if the file is not in the list
if ((fileName in dataList) == null)
return false;
dataList.remove(fileName);
return true;
}
ubyte [] loadResource(char [] fileName)
{
ubyte [] data;
// try to load the real file first
try
{
data = cast (ubyte[]) (read(fileName));
}
catch (Exception e)
{
// load from the archives
ZipArchive [] archives = dataList.values;
int i;
for (i = 0; i < archives.length; i++)
{
bool find = false;
foreach (ArchiveMember am; archives.directory)
{
if (am.name == fileName)
{
data = (archives.expand(am));
find = true;
break;
}
}
if (find == true)
break;
}
if (i == archives.length)
data = null;
}
return data;
}
ubyte [] loadResource(char [] archiveName,char [] fileName)
{
if ((archiveName in dataList) == null)
{
// try to add the archive to the list
if (addArchive(archiveName) == false)
return null;
}
ZipArchive archive = dataList;
foreach (ArchiveMember am; archive.directory)
{
if (am.name == fileName)
{
return (archive.expand(am));
}
}
return null;
}
}
// data
private
{
ZipArchive ] dataList;
}
}
话说……那啥……最近看到的一个ZIP的那个……好烦- -
相比这个好多了- -~
感觉就和JAVA那种感觉了,都被做到内置里面去了…………~~ 不错的例子,学习学习。
ps.
这就是类库的威力啊,可惜d在这方面作得还不是很好。
语法使用上和c差不多,类写起来比c++方便得多得多。
所以才看到d的时候,就毫不犹豫的选择了它。
反正我不是专业程序员,不在乎主流不主流,只在乎自己爽不爽。 我記得在BMax上為了讓文件直接讀到Memory Stream是件多麼繁瑣的過程...
我目前是用PhysicsFS處理這工作(因為BMax直接支持gcc),
但我還不知道如何用虛擬文件系統處理緩衝。 booST貌似有一个文件系统FILESYSTEM,不过偶没有看过,
另外在游戏开发的时候为什么需要使用VFS呢?把所有的文件打为一个数据包,然后就直接读取是不是就可以了?? 所有资源打一个超大的包,std.steam来读入后,用指针来指示具体文件的偏移位置? 那个大概都是单个目录的巴,估计VFS主要用来划分资源的层次……………… 我觉得单目录也没什么不好的。
把里面的文件命名成这样 pic0001.jpg,snd0001.ogg,就和用pic,snd两个目录来区分差不多了。
页:
[1]