知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070614
点击下面网址查看原文:
http://www.d-programming-language-china.org
by: Yuki
from: http://bbs.rpgchina.com/read-htm-tid-27472.html
这里有D语言的专区啊。真是不错。。我也随便发一贴好了。。
关于std.zip 包,这个包使用std.zlib作为其zip支持,且使用OO的方法封装了zip的压缩和解压缩,非常方便。这里对其方法就不详细讲解了。可以去看D主页的说明。我们可以使用这个东西,从zip文件(也可以伪装成。dat哦)里面加载文件数据,如果使用sdl的话可以用sdl的rwops结构加载数据就可以在zip文件里面加载资源了。
我把我的写的ResourceLoader贴出来。由于我是D的新手可能写的不好,请大家多原谅。?/p>
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[fileName] = 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[archiveName];
foreach (ArchiveMember am; archive.directory)
{
if (am.name == fileName)
{
return (archive.expand(am));
}
}
return null;
}
}
// data
private
{
ZipArchive [char []] dataList;
}
}
( lastupdate:20070614 最新文章请访问http://www.d-programming-language-china.org )关于一大步成功社区:
yidabu提倡在交流中学习,在分享中提高
收集感兴趣的知识,写下心得,通过网络与别人一起分享
理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的
网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台