2

我目前正在开发游戏,我想知道是否可以在 C# 中制作自定义文件打包器/解包器/修改器

它可以将整个目录中的所有文件(包括具有自定义扩展名的单个文件中的子目录)打包,并且可以解压缩所有文件,但在打包时仍然具有相同的信息,并可以修改打包文件中的文件。

我知道我要求很多,但我不希望得到所有答案,所以如果您至少知道一个,请告诉我,或者如果有任何链接,请指导我。

谢谢,麻烦您了。

4

1 回答 1

2

要创建一个包文件并能够从中读取,您首先需要定义如何在该包文件中存储信息,因此您首先需要创建一个结构或一个类。

class FileEntry
{
  public int FileOffset { get; set; }
  public int FileSize { get; set; }
  public int Flags { get; set; }
  public String FileName { get; set; }
  public String FileDirectory { get; set; }
}

用于创建

您最好标记您的文件,以便您可以验证它是正确的文件类型,即。将前 4 个字节设置为“PACK”之类的内容(您应该使用StreamWriter.

您需要打开要打包的目录树的根目录,并遍历所有子目录和文件,并为找到的每个文件创建一个FileEntry并将其存储在列表中。您应该写入文件的下一件事将是一个 32 位 int,它指定包中的文件总数(Count列表的属性)。

You would then want to iterate through your list of FileEntry and write another tag to specify a file start, such as "FILE", then the 32bit int of FileSize, 32bit int of Flags, and then, a 32bit it of the string length of FileName as FileName is dynamic and can be of any length, then write out the FileName string its self, and the same with FileDirectory length and FileDirectory string. After you have written FileDirectory string, you would then want to write the file data its self, note that this is not stored within the file entry as you probably dont want all this data to be stored within memory.

Once you have done that for everything in your FileEntry list, you can then close the file, and that will be all those files packed up neatly into a single custom pack file.

For Reading

We will essentially be doing the same thing, but we will be reading, rather than writing, and you will have to record an offset value so that you are able to track where a new file starts so that you can read its data out later on.

You would first need to read in the first 4 bytes of the file and test that they are set as "PACK", if they are we can continue as this is our file. Next, read in the next 4 bytes and covert to a 32bit integer so we can get the total number of files contained within this pack.

You can then run a for loop, something like for(int i=0; i<g_max_files; i++) to make things easier while we are looking for our FileEntry's, remembering to increment our offset every time we read anything via our StreamReader. The first thing we check in our loop is the first 4 bytes after the file out match "FILE". If they match we then read out in 4 byte (32bit) chunks, FileSize, then Flags and then FileName's length, once we know FileName's length, we can read out FileName its self and repeat with FileDirectory. If you have been increasing the offset specified earlier, you should now have the file position in bytes of where the file data starts for this particular file, you can now create a new FileEntry and set all of the properties and add it to your file list and allow the loop to continue back to the next file.

Modifying

With modifying, if you are going to be INCREASING the file size of any of the files within the pack, you are going to have to recreate the pack, if you are decreasing the file size of any of the files, you can simply alter the FileSize int and pad the existing file data within the pack with null's.

If you are looking to leave the existing files as they are, but only add to the end, you are simply doing a modification of both reading, and creating. You just add on to the end of the pack you already have by using the creating part of this, and alter the total files int at the top of the file.

Hope this was helpful.

于 2013-03-30T12:12:20.340 回答