private IEnumerator WaitForSubscriptionList(WWW www)
        {
            bool timedOut = false;

            while (!www.isDone)
            {
                if (timer > timeout)
                {
                    timedOut = true;
                    break;
                }

                timer += Time.deltaTime;
                yield return(null);
            }

            if (timedOut)
            {
                GM.Instance.logger.Error("Could not connect to Winnitron Network. Request timed out after " + timeout + "s.");
                EndSync();
            }

            yield return(www);



            if (www.error == null)
            {
                GM.Instance.logger.Info(this, "fetched playlists: " + www.text);
                var data = JSON.Parse(www.text);
                foreach (JSONNode playlistData in data["playlists"].AsArray)
                {
                    playlists.Add(new Playlist(playlistData, gamesDir));
                }

                // delete unsubscribed playlists
                SluggedItem.DeleteExtraDirectories(gamesDir, playlists);

                foreach (Playlist playlist in playlists)
                {
                    SyncText("Initializing games in " + playlist.title);

                    playlist.DeleteRemovedGames();


                    // Only download games that have had a new version uploaded
                    // since the last sync.
                    ArrayList gamesToDownload = playlist.GamesToDownload();
                    foreach (Game game in gamesToDownload)
                    {
                        GM.Instance.logger.Info(this, "Downloading: " + game.title);

                        //Start the downloadin'!

                        WWW download = new WWW(game.downloadURL);

                        while (!download.isDone)
                        {
                            int progress = Mathf.FloorToInt(download.progress * 100);
                            SyncText("Downloading " + game.title + " %" + progress);
                            yield return(null);
                        }


                        //Download complete!

                        if (!string.IsNullOrEmpty(download.error))
                        {
                            // error!
                            GM.Instance.logger.Error("Error downloading '" + download.url + "': " + download.error);
                            yield return(SyncText("Error downloading " + game.title + "!", 3));
                        }
                        else
                        {
                            //Things downloaded fine!  Do fun stuff now pls thx.

                            yield return(SyncText("Unzipping " + game.title + "...", 0.1f));

                            Directory.CreateDirectory(game.installDirectory);
                            string zipFile = Path.Combine(game.installDirectory, game.slug + ".zip");
                            File.WriteAllBytes(zipFile, download.bytes);

                            FastZip zip = new FastZip();
                            ZipConstants.DefaultCodePage = 0;

                            //Starts the unzip coroutine and waits till it's done
                            zip.ExtractZip(zipFile, game.installDirectory, null);

                            // Download the image
                            WWW imageDownload = new WWW(game.imageURL);
                            while (!imageDownload.isDone)
                            {
                                SyncText("Downloading cover image");
                                yield return(null);
                            }

                            System.Uri uri           = new System.Uri(game.imageURL);
                            string     imageFilename = Path.GetFileName(uri.AbsolutePath);
                            File.WriteAllBytes(Path.Combine(game.installDirectory, imageFilename), imageDownload.bytes);

                            File.Delete(zipFile);
                        }
                    }

                    // Re-write metadata for all games in case that info changes
                    // even without a new file uploaded.
                    foreach (Game game in playlist.games)
                    {
                        game.WriteMetadataFile();
                    }
                }

                SyncText("Collecting data for launcher...");

                EndSync();
            }
            else
            {
                GM.Instance.logger.Error("Error fetching playlists: " + www.error);
                EndSync();
            }
        }
Beispiel #2
0
 public void DeleteRemovedGames()
 {
     SluggedItem.DeleteExtraDirectories(Path.Combine(parentDirectory, slug), games);
 }