Example #1
0
    public static WorldChunk LoadChunk(int x, int z)
    {
        string savesDirPath = Path.Combine (Application.persistentDataPath, GameSettings.LoadedConfig.SavesPath);
        bool savesDirExists = System.IO.Directory.Exists (savesDirPath);
        if (!savesDirExists) {
            return null;
        }

        string activeWorldDirPath = Path.Combine (savesDirPath, GameSettings.LoadedConfig.ActiveWorldDir);
        bool activeWorldDirExists = System.IO.Directory.Exists (activeWorldDirPath);
        if (!activeWorldDirExists) {
            return null;
        }

        string chunksDirPath = Path.Combine (activeWorldDirPath, WSerializer.chunksDirName);
        bool chunksDirExists = System.IO.Directory.Exists (chunksDirPath);
        if (!chunksDirExists) {
            return null;
        }

        string chunkFilePath = Path.Combine (chunksDirPath, WSerializer.chunkFilePrefix + x + "-" + z + ".sav");
        bool chunkFileExists = System.IO.File.Exists (chunkFilePath);
        if (!chunkFileExists) {
            return null;
        }

        XmlSerializer serializer = new XmlSerializer (typeof(SerializableChunk));
        FileStream stream = new FileStream (chunkFilePath, FileMode.Open);
        SerializableChunk loadedChunk = serializer.Deserialize (stream) as SerializableChunk;
        stream.Close ();

        WorldChunk restoredChunk = new WorldChunk (x, z);
        restoredChunk.restoreChunkData (loadedChunk);
        Debug.Log("Chunk: ("+x+", "+z+") was restored from file.");

        return restoredChunk;
    }