Esempio n. 1
0
    //=====================================================

    void LoadLiveUpdate()
    {
        string DocPath  = PreHelpers.GetFileFolderPath();
        string FilePath = DocPath + "wfs2.zip";

        if (System.IO.File.Exists(FilePath) == false)
        {
            return;
        }

        // Read file
        FileStream fs = null;

        try
        {
            fs = new FileStream(FilePath, FileMode.Open);
        }
        catch
        {
            Debug.Log("GameData file open exception: " + FilePath);
        }

        if (fs != null)
        {
            try
            {
                // Read zip file
                ZipFile zf       = new ZipFile(fs);
                int     numFiles = 0;

                if (zf.TestArchive(true) == false)
                {
                    Debug.Log("Zip file failed integrity check!");
                    zf.IsStreamOwner = false;
                    zf.Close();
                    fs.Close();
                }
                else
                {
                    foreach (ZipEntry zipEntry in zf)
                    {
                        // Ignore directories
                        if (!zipEntry.IsFile)
                        {
                            continue;
                        }

                        String entryFileName = zipEntry.Name;

                        // Skip .DS_Store files
                        if (entryFileName.Contains("DS_Store"))
                        {
                            continue;
                        }

                        Debug.Log("Unpacking zip file entry: " + entryFileName);

                        byte[] buffer    = new byte[4096];                            // 4K is optimum
                        Stream zipStream = zf.GetInputStream(zipEntry);

                        // Manipulate the output filename here as desired.
                        string fullZipToPath = PreHelpers.GetFileFolderPath() + Path.GetFileName(entryFileName);

                        // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
                        // of the file, but does not waste memory.
                        // The "using" will close the stream even if an exception occurs.
                        using (FileStream streamWriter = System.IO.File.Create(fullZipToPath))
                        {
                            StreamUtils.Copy(zipStream, streamWriter, buffer);
                        }
                        numFiles++;
                    }

                    zf.IsStreamOwner = false;
                    zf.Close();
                    fs.Close();

                    // If we've unpacked the local files (at least 13) then start using local spreadsheets data
                    Debug.Log("Zip updated with " + numFiles + " files (needs 13)");
                    if (numFiles >= 13)
                    {
                        PlayerPrefs.SetInt(PreHelpers.GetLiveDataVersionString(), 1);
                    }

                    // Reload managers
                    TextManager.Reload();
                    SettingsManager.Reload();
                    TradingCardItemsManager.Reload();
                    ClothingItemsManager.Reload();
                    FairyItemsManager.Reload();
                    WildMagicItemsManager.Reload();
                    NPCItemsManager.Reload();
                }
            }
            catch
            {
                Debug.Log("Zip file error!");
            }

            // Remove zip file
            Debug.Log("Removing zip file");
            System.IO.File.Delete(FilePath);
        }
    }