예제 #1
0
    public void Save(bool useTemp, bool deleteTemp = true)
    {
        if (Data == null)
        {
            Debug.LogError("Tile Map Data is null, cannot save! Invalid loaded map!");
            return;
        }

        // Should be in editor mode for this to work correctly, but it theoretically also works even at runtime.
        string zipFilePath = Data.SavedZipPath;

        Debug.Log("Saving map '{0}' to {1}".Form(this, zipFilePath));

        string fileSaveDir = null;

        if (useTemp)
        {
            // Save to a temporary folder.
            fileSaveDir = Path.Combine(Path.GetTempPath(), Data.InternalName);
        }
        else
        {
            // Save to the extracted (unzipped) folder, and then zip it up anyway.
            fileSaveDir = Path.Combine(GameIO.UnzippedMapsDirectory, Data.InternalName);
        }

        // Save all map data to file.
        Debug.Log("Saving files to '{0}' ({1}) ...".Form(fileSaveDir, useTemp ? "temp" : "pers"));
        GameIO.EnsureDirectory(fileSaveDir);
        var fs = MapIO.StartWrite(Path.Combine(fileSaveDir, TILE_DATA_FILE));

        MapIO.WriteTileIDs(fs, TileIDs);
        MapIO.End(fs);

        // Save metadata.
        GameIO.ObjectToFile(this.Data, Path.Combine(fileSaveDir, METADATA_FILE));

        // Work out all the tile varieties.
        // TODO
        fs = MapIO.StartWrite(Path.Combine(fileSaveDir, TILE_VARIATION_FILE));
        MapIO.WriteTileVariations(fs, this.TileVariations);
        MapIO.End(fs);

        // Grab all the saved files, and zip the up into the save zip path.
        MapIO.Zip(fileSaveDir, zipFilePath);

        // Delete the temporary directory, just to be clean.
        if (useTemp && deleteTemp)
        {
            Directory.Delete(fileSaveDir, true);
        }

        Debug.Log("Finished saving, zipped to '{0}'".Form(zipFilePath));
    }
예제 #2
0
    public void Load(string mapInternalName, bool forceExtract)
    {
        var sw = new System.Diagnostics.Stopwatch();

        sw.Start();

        if (mapInternalName == null)
        {
            Debug.LogError("Map internal name is null, cannot load!");
            return;
        }

        string unzippedPath = GameIO.UnzippedMapsDirectory;

        if (!Directory.Exists(unzippedPath))
        {
            Debug.LogWarning("Unzipped map path ({0}) does not exist, creating it...".Form(unzippedPath));
            Directory.CreateDirectory(unzippedPath);
        }

        string zippedPath     = GameIO.ZippedMapsDirectory;
        string zippedFilePath = Path.Combine(zippedPath, mapInternalName + ".zip");
        bool   zippedExists   = false;

        if (!Directory.Exists(zippedPath))
        {
            Debug.LogWarning("Zipped map path ({0}) does not exist, why? Map will not load unless it has already been unzipped previously.".Form(zippedPath));
        }
        else
        {
            if (File.Exists(zippedFilePath))
            {
                zippedExists = true;
            }
        }

        string destinationUnzippedDirectory = Path.Combine(unzippedPath, mapInternalName);

        if (forceExtract)
        {
            if (!zippedExists)
            {
                Debug.LogError("Extract is required (forced), but map zipped file '{0}' could not be found. Cannot load map!".Form(zippedPath));
                return;
            }

            // Extract the zipped file to the unzipped folder.
            MapIO.UnZip(zippedFilePath, destinationUnzippedDirectory, true);
            Debug.Log("Unzipped map '{0}' to {1}, was forced ({2}).".Form(mapInternalName, destinationUnzippedDirectory, Directory.Exists(destinationUnzippedDirectory) ? "not necessary" : "necessary"));
        }
        else
        {
            // Check to see if an unzipped version exists, otherwise extract...
            if (Directory.Exists(destinationUnzippedDirectory))
            {
                // It does exist! Assume that the files are all correct and loaded properly...
                Debug.Log("Extract not forced, unzipped version of '{0}' found.".Form(mapInternalName));
            }
            else
            {
                // An unzipped version does not exist, extract it!
                if (!zippedExists)
                {
                    Debug.LogError("Extract is required, but map zipped file '{0}' could not be found. Cannot load map!".Form(zippedPath));
                    return;
                }

                // Extract the zipped file to the unzipped folder.
                MapIO.UnZip(zippedFilePath, destinationUnzippedDirectory, true);
                Debug.Log("Unzipped map '{0}' to {1}, was NOT forced (necessary).".Form(mapInternalName, destinationUnzippedDirectory));
            }
        }

        // Now the extracted version should exist.

        // Load map metadata...
        this.Data = GameIO.FileToObject <TileMapData>(Path.Combine(destinationUnzippedDirectory, METADATA_FILE));

        // Load map tile ID data.
        string dataFile = Path.Combine(destinationUnzippedDirectory, TILE_DATA_FILE);
        var    fs       = MapIO.StartRead(dataFile);
        var    data     = MapIO.ReadAllTileIDs(fs, this.Data.SizeInRegions);

        this.TileIDs = null;
        this.TileIDs = data;
        MapIO.End(fs);

        // Load map tile variations...
        string variationFile = Path.Combine(destinationUnzippedDirectory, TILE_VARIATION_FILE);

        fs = MapIO.StartRead(variationFile);
        var varData = MapIO.ReadAllTileVariations(fs, this.Data.SizeInRegions);

        this.TileVariations = null;
        this.TileVariations = varData;
        MapIO.End(fs);

        // Run GC
        Debug.Log("Running GC...");
        Memory.GC();
        Debug.Log("Done!");

        // Done!
        sw.Stop();
        Debug.Log("Done loading map '{0}' in {1} seconds.".Form(mapInternalName, sw.Elapsed.TotalSeconds.ToString("N2")));
    }