【问题标题】:Saving/Loading multiple chunks into one file?将多个块保存/加载到一个文件中?
【发布时间】:2016-07-17 05:37:59
【问题描述】:

所以我正在做的一个项目是一个基于体素的游戏,并且会有大的 3d 数组块,问题在于保存它们,我似乎无法弄清楚如何将数组的多个块保存到一个文件中并且仅在需要时从文件中加载所需的块。关于如何去做这件事的任何想法?理想情况下,我可以制作多个较小的文件,但这可能不方便。

【问题讨论】:

    标签: c# monogame file-storage


    【解决方案1】:

    您可以在文件开头使用 Header 来告诉程序块在文件中的位置

    类似

    Chunk1=256,Chunk2=512
    

    等等。

    或者您可以使用 BinaryFormatter 存储块

    using System;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace Helpr
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] Chunks = new int[10 * 10]; //Created this dummy chunk array to show that it works
                for(int x = 0; x < 10; ++x)     
                {
                    for(int y = 0; y < 10; ++y)
                    {
                        Chunks[x + (10 * y)] = x * y;       //Set the data
                    }
                }
    
                SaveChunks(Chunks, "Chunks.world"); //Save the chunks with a file name to refer too later
    
                for(int i = 0; i < 100; ++i)
                {
                    Console.WriteLine(Chunks[i] + " "); //Write the data to the console so we can compare
                }
    
                Console.WriteLine(" ");
    
                Chunks = (int[])LoadChunks("Chunks.world"); //Load the file back into the chunk
    
                for (int i = 0; i < 100; ++i)
                {
                    Console.WriteLine(Chunks[i] + " "); //Log to see if it has worked
                }
    
                Console.ReadKey();                      //Pause so you can even read it!
            }
    
            public static void SaveChunks(object Chunks, string filePath)
            {
                BinaryFormatter bf = new BinaryFormatter();                             //Create a BinaryFormatter to change the chunks into a binary string
                FileStream fs = new FileStream(filePath, FileMode.Create);              //Create a filestream to create a new file to store the string
    
                try
                {
                    bf.Serialize(fs, Chunks);                                           //Serialize the data into a file
                }
                catch (Exception e)                                                     //if an error occurs Post it!
                {
                    Console.WriteLine("Failed to create Chunk file! " + e);
                }
                finally
                {
                    fs.Close();                                                         //Then close the filestream to prevent memory leaks
                }
            }
    
            public static object LoadChunks(string filePath)
            {
                BinaryFormatter bf = new BinaryFormatter();                             //Create a BinaryFormatter to convert the binary string to an object
                FileStream fs = new FileStream(filePath, FileMode.Open);                //Create a filestream to create a new file to provide the string
    
                try
                {
                    return bf.Deserialize(fs);                                          //Deserialize the data into an object
                }
                catch (Exception e)                                                     //Catch any errors if they occur
                {
                    Console.WriteLine("Chunk load failed! " + e);
                    return null;
                }
                finally
                {
                    fs.Close();                                                         //Then close the filestream to prevent memory leaks
                }
            }
        }
    }
    

    关键的加载和保存部分在这里

    public static void SaveChunks(object Chunks, string filePath)
            {
                BinaryFormatter bf = new BinaryFormatter();                             //Create a BinaryFormatter to change the chunks into a binary string
                FileStream fs = new FileStream(filePath, FileMode.Create);              //Create a filestream to create a new file to store the string
    
                try
                {
                    bf.Serialize(fs, Chunks);                                           //Serialize the data into a file
                }
                catch (Exception e)                                                     //if an error occurs Post it!
                {
                    Console.WriteLine("Failed to create Chunk file! " + e);
                }
                finally
                {
                    fs.Close();                                                         //Then close the filestream to prevent memory leaks
                }
            }
    
            public static object LoadChunks(string filePath)
            {
                BinaryFormatter bf = new BinaryFormatter();                             //Create a BinaryFormatter to convert the binary string to an object
                FileStream fs = new FileStream(filePath, FileMode.Open);                //Create a filestream to create a new file to provide the string
    
                try
                {
                    return bf.Deserialize(fs);                                          //Deserialize the data into an object
                }
                catch (Exception e)                                                     //Catch any errors if they occur
                {
                    Console.WriteLine("Chunk load failed! " + e);
                    return null;
                }
                finally
                {
                    fs.Close();                                                         //Then close the filestream to prevent memory leaks
                }
            }
    

    希望这会有所帮助!

    【讨论】:

    • 但是 BinaryFormatter 确实会产生 larrrrrge 文件大小
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多