快速轻松地从一个文件加载所需内容? [英] Load needed content from one file fast and easily?

查看:110
本文介绍了快速轻松地从一个文件加载所需内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,有一个包含很多内容的游戏,比如汽车模型。
我不想让它们一直存在RAM中以节省内存并且只想在需要时加载它们。当每辆车有一个文件时这很容易 - 但我最后会有这么多文件,而且项目很难分享等等。

Imagine there is a game with a lot of content, like car models. I don't want to have them in the RAM all the time to save memory and want to load them only when needed. This would be easily when having one file per car - but I would have so many files at the end and the project would become hard to share etc.

但是当我存储时一切都变成了一个文件,我不知道它在哪里。
有没有办法将所有内容存储到一个文件中并像每个内容条目一样轻松导航到内容?我唯一可以想象的是保存第二个文件中的字节位置(或内容文件中的标题),但我对此解决方案非常不确定。

But when I store everything into one file, I don't know where what is. Is there a way storing everything into one file and navigate to the content as easily as if I do one file per content entry? Only thing I can imagine is saving the byte positions where something begins in a 2nd file (or as header in the content file) but I'm very unsure about that solution.

推荐答案

一种简单的方法是在你编写文件时跟踪(在内存中)存在的东西。然后,在文件的末尾编写索引。因此,您的文件如下所示:

A simple way to do it is when you write the file you keep track (in memory) where stuff is. Then, at the end of the file you write an index. So your file looks like this:

offset 0: data for object 1
offset 0x0473: data for object 2
offset 0x1034: data for object 3
etc.

你可以写这个文件使用 BinaryWriter 。在编写每个对象之前,查询 writer.BaseStream.Position 以获取文件的当前位置。您将该信息保存在内存中(只是对象名称及其位置)。

You can write this file using BinaryWriter. Before you write each object, you query writer.BaseStream.Position to get the current position of the file. You save that information in memory (just the object name and its position).

当您完成所有对象的编写后,您将保存当前位置:

When you're done writing all of the objects, you save the current position:

indexPosition = writer.BaseStream.Position;

然后在文件末尾写下索引:

Then write the index at the end of the file:

name: "object 1", position: 0
name: "object 2", position: 0x0473
etc.

写一个空的索引条目来表示对象的结束:

Write an empty index entry to signify the end of objects:

name: "", position: 0xFFFFFFFF

最后一件事你要写的是文件末尾的索引位置:

And the last thing you do is write the index position at the end of the file:

writer.Write(indexPosition);

现在,您可以使用 BinaryReader打开文件。寻找文件末尾减去8个字节,在那里读取长整数。这为您提供了索引的位置。寻找索引并开始向前读取索引条目,直到找到位置为0xFFFFFFFF的索引条目。

Now, you can open the file with a BinaryReader. Seek to end-of-file minus 8 bytes, read the long integer there. That gives you the position of the index. Seek to the index and start reading index entries forward until you get to one that has a position of 0xFFFFFFFF.

现在你的索引在内存中。您可以创建一个 Dictionary< string,long> ,这样,给定一个对象名称,您就可以在文件中找到它的位置。获得位置,寻找并阅读对象。

You now have the index in memory. You can create a Dictionary<string, long> so that, given an object name, you can find its position in the file. Get the position, seek there, and read the object.

当我们在90年代后期编写游戏时,这种情况非常普遍。你仍然可以做到这一点,尽管你最好还是选择一个简单的数据库。

This kind of thing was pretty common when we were writing games in the late '90s. You can still do it, although you're probably better off going with a simple database.

这篇关于快速轻松地从一个文件加载所需内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆