private IEnumerator LoadSetCards(UnityCardGame cardGame)
        {
            if (cardGame == null)
            {
                cardGame = Current;
            }

            var setCardsLoaded = false;

            foreach (Set set in cardGame.Sets.Values)
            {
                if (string.IsNullOrEmpty(set.CardsUrl))
                {
                    continue;
                }
                if (!setCardsLoaded)
                {
                    Messenger.Show(string.Format(SetCardsLoadingMessage, cardGame.Name));
                }
                setCardsLoaded = true;
                string setCardsFilePath = Path.Combine(cardGame.SetsDirectoryPath,
                                                       UnityExtensionMethods.GetSafeFileName(set.Code + UnityExtensionMethods.JsonExtension));
                if (!File.Exists(setCardsFilePath))
                {
                    yield return(UnityExtensionMethods.SaveUrlToFile(set.CardsUrl, setCardsFilePath));
                }
                if (File.Exists(setCardsFilePath))
                {
                    cardGame.LoadCards(setCardsFilePath, set.Code);
                }
                else
                {
                    Debug.LogError(LoadErrorMessage + set.CardsUrl);
                    yield break;
                }
            }

            if (!string.IsNullOrEmpty(cardGame.Error))
            {
                Debug.LogError(LoadErrorMessage + cardGame.Error);
            }
            else if (setCardsLoaded)
            {
                Messenger.Show(string.Format(SetCardsLoadedMessage, cardGame.Name));
            }
        }
        private void CreateDefaultCardGames()
        {
#if UNITY_ANDROID && !UNITY_EDITOR
            UnityExtensionMethods.ExtractAndroidStreamingAssets(UnityCardGame.GamesDirectoryPath);
#elif UNITY_WEBGL
            if (!Directory.Exists(UnityCardGame.GamesDirectoryPath))
            {
                Directory.CreateDirectory(UnityCardGame.GamesDirectoryPath);
            }
            string standardPlayingCardsDirectory =
                UnityCardGame.GamesDirectoryPath + "/" + Tags.StandardPlayingCardsDirectoryName;
            if (!Directory.Exists(standardPlayingCardsDirectory))
            {
                Directory.CreateDirectory(standardPlayingCardsDirectory);
            }
            File.WriteAllText(standardPlayingCardsDirectory + "/" + Tags.StandardPlayingCardsJsonFileName, Tags.StandPlayingCardsJsonFileContent);
            string dominoesDirectory = UnityCardGame.GamesDirectoryPath + "/" + Tags.DominoesDirectoryName;
            if (!Directory.Exists(dominoesDirectory))
            {
                Directory.CreateDirectory(dominoesDirectory);
            }
            File.WriteAllText(dominoesDirectory + "/" + Tags.DominoesJsonFileName, Tags.DominoesJsonFileContent);
            StartCoroutine(UnityExtensionMethods.SaveUrlToFile(Tags.DominoesCardBackUrl, dominoesDirectory + "/CardBack.png"));
            string mahjongDirectory = UnityCardGame.GamesDirectoryPath + "/" + Tags.MahjongDirectoryName;
            if (!Directory.Exists(mahjongDirectory))
            {
                Directory.CreateDirectory(mahjongDirectory);
            }
            File.WriteAllText(mahjongDirectory + "/" + Tags.MahjongJsonFileName, Tags.MahjongJsonFileContent);
            StartCoroutine(UnityExtensionMethods.SaveUrlToFile(Tags.MahjongCardBackUrl, mahjongDirectory + "/CardBack.png"));
            string arcmageDirectory = UnityCardGame.GamesDirectoryPath + "/" + Tags.ArcmageDirectoryName;
            if (!Directory.Exists(arcmageDirectory))
            {
                Directory.CreateDirectory(arcmageDirectory);
            }
            File.WriteAllText(arcmageDirectory + "/" + Tags.ArcmageJsonFileName, Tags.ArcmageJsonFileContent);
            StartCoroutine(UnityExtensionMethods.SaveUrlToFile(Tags.ArcmageCardBackUrl, arcmageDirectory + "/CardBack.png"));
#else
            UnityExtensionMethods.CopyDirectory(Application.streamingAssetsPath, UnityCardGame.GamesDirectoryPath);
#endif
        }
Beispiel #3
0
        public IEnumerator Download()
        {
            if (IsDownloading)
            {
                yield break;
            }
            IsDownloading = true;

            string initialDirectory = GameFolderPath;

            yield return(UnityExtensionMethods.SaveUrlToFile(AutoUpdateUrl, ConfigFilePath));

            try
            {
                if (!IsLoaded)
                {
                    JsonConvert.PopulateObject(File.ReadAllText(ConfigFilePath), this);
                }
            }
            catch (Exception e)
            {
                Error        += e.Message;
                IsDownloading = false;
                yield break;
            }

            // If the name of the game doesn't match our initial game name, we need to move to the new directory and delete the old one
            if (!initialDirectory.Equals(GameFolderPath))
            {
                // Downloading the config file twice is slightly inefficient, but it shouldn't cause a noticeable difference for the user
                yield return(UnityExtensionMethods.SaveUrlToFile(AutoUpdateUrl, ConfigFilePath));

                Directory.Delete(initialDirectory, true);
            }

            for (int page = AllCardsUrlPageCountStartIndex; page < AllCardsUrlPageCountStartIndex + AllCardsUrlPageCount; page++)
            {
                string cardsUrl = AllCardsUrl;
                if (AllCardsUrlPageCount > 1 && string.IsNullOrEmpty(AllCardsUrlPostBodyContent))
                {
                    cardsUrl += AllCardsUrlPageIdentifier + page;
                }
                string cardsFile = CardsFilePath;
                if (page != AllCardsUrlPageCountStartIndex)
                {
                    cardsFile += page.ToString();
                }
                if (AllCardsUrlZipped)
                {
                    cardsFile += UnityExtensionMethods.ZipExtension;
                }
                string jsonBody = null;
                if (!string.IsNullOrEmpty(AllCardsUrlPostBodyContent))
                {
                    jsonBody  = "{" + AllCardsUrlPostBodyContent;
                    jsonBody += AllCardsUrlPageIdentifier + page;
                    jsonBody += "}";
                }
                yield return(UnityExtensionMethods.SaveUrlToFile(cardsUrl, cardsFile, jsonBody));

                // Sometimes, we need to get the AllCardsUrlPageCount from the first page of AllCardsUrl
                if (page == AllCardsUrlPageCountStartIndex && !string.IsNullOrEmpty(AllCardsUrlPageCountIdentifier))
                {
                    LoadCards(page);
                }
            }

            string setsFilePath = SetsFilePath + (AllSetsUrlZipped ? UnityExtensionMethods.ZipExtension : string.Empty);

            if (!string.IsNullOrEmpty(AllSetsUrl))
            {
                yield return(UnityExtensionMethods.SaveUrlToFile(AllSetsUrl, setsFilePath));
            }

            yield return(UnityExtensionMethods.SaveUrlToFile(BackgroundImageUrl, BackgroundImageFilePath));

            yield return(UnityExtensionMethods.SaveUrlToFile(CardBackImageUrl, CardBackImageFilePath));

            foreach (GameBoardUrl boardUrl in GameBoardUrls)
            {
                yield return(UnityExtensionMethods.SaveUrlToFile(boardUrl.Url, GameBoardsFilePath + "/" + boardUrl.Id + "." + GameBoardFileType));
            }

            foreach (DeckUrl deckUrl in DeckUrls)
            {
                yield return(UnityExtensionMethods.SaveUrlToFile(deckUrl.Url, DecksFilePath + "/" + deckUrl.Name + "." + DeckFileType));
            }

            if (!IsLoaded)
            {
                Load(true);
            }

            IsDownloading = false;
        }
        public IEnumerator Download()
        {
            if (IsDownloading)
            {
                yield break;
            }
            IsDownloading = true;

            // We should always first get the *Game:Name*.json file and read it before doing anything else
            DownloadProgress = 0f / (7f + AllCardsUrlPageCount);
            DownloadStatus   = "Downloading: CardGameDef...";
            yield return(UnityExtensionMethods.SaveUrlToFile(AutoUpdateUrl, GameFilePath));

            ReadProperties();
            if (!HasReadProperties)
            {
                // ReadProperties() should have already populated the Error
                IsDownloading = false;
                HasDownloaded = false;
                yield break;
            }

            DownloadProgress = 1f / (7f + AllCardsUrlPageCount);
            DownloadStatus   = "Downloading: Banner";
            if (!string.IsNullOrEmpty(BannerImageUrl))
            {
                yield return(UnityExtensionMethods.SaveUrlToFile(BannerImageUrl, BannerImageFilePath));
            }

            DownloadProgress = 2f / (7f + AllCardsUrlPageCount);
            DownloadStatus   = "Downloading: CardBack";
            if (!string.IsNullOrEmpty(CardBackImageUrl))
            {
                yield return(UnityExtensionMethods.SaveUrlToFile(CardBackImageUrl, CardBackImageFilePath));
            }

            DownloadProgress = 3f / (7f + AllCardsUrlPageCount);
            DownloadStatus   = "Downloading: Boards";
            foreach (GameBoardUrl boardUrl in GameBoardUrls)
            {
                yield return(UnityExtensionMethods.SaveUrlToFile(boardUrl.Url, GameBoardsFilePath + "/" + boardUrl.Id + "." + GameBoardFileType));
            }

            DownloadProgress = 4f / (7f + AllCardsUrlPageCount);
            DownloadStatus   = "Downloading: Decks";
            foreach (DeckUrl deckUrl in DeckUrls)
            {
                yield return(UnityExtensionMethods.SaveUrlToFile(deckUrl.Url, DecksFilePath + "/" + deckUrl.Name + "." + DeckFileType));
            }

            DownloadProgress = 5f / (7f + AllCardsUrlPageCount);
            DownloadStatus   = "Downloading: AllSets.json";
            string setsFilePath = SetsFilePath + (AllSetsUrlZipped ? UnityExtensionMethods.ZipExtension : string.Empty);

            if (!string.IsNullOrEmpty(AllSetsUrl))
            {
                yield return(UnityExtensionMethods.SaveUrlToFile(AllSetsUrl, setsFilePath));
            }
            if (AllSetsUrlZipped)
            {
                UnityExtensionMethods.ExtractZip(setsFilePath, GameDirectoryPath);
            }
            if (AllSetsUrlWrapped)
            {
                UnityExtensionMethods.UnwrapFile(SetsFilePath);
            }

            if (!string.IsNullOrEmpty(AllCardsUrl))
            {
                for (int page = AllCardsUrlPageCountStartIndex; page < AllCardsUrlPageCountStartIndex + AllCardsUrlPageCount; page++)
                {
                    DownloadProgress = (6f + page - AllCardsUrlPageCountStartIndex) / (7f + AllCardsUrlPageCount - AllCardsUrlPageCountStartIndex);
                    DownloadStatus   = $"Downloading: Cards: {page,5} / {AllCardsUrlPageCountStartIndex + AllCardsUrlPageCount}";
                    string cardsUrl = AllCardsUrl;
                    if (AllCardsUrlPageCount > 1 && string.IsNullOrEmpty(AllCardsUrlPostBodyContent))
                    {
                        cardsUrl += AllCardsUrlPageIdentifier + page;
                    }
                    string cardsFile = CardsFilePath;
                    if (page != AllCardsUrlPageCountStartIndex)
                    {
                        cardsFile += page.ToString();
                    }
                    if (AllCardsUrlZipped)
                    {
                        cardsFile += UnityExtensionMethods.ZipExtension;
                    }
                    string jsonBody = null;
                    if (!string.IsNullOrEmpty(AllCardsUrlPostBodyContent))
                    {
                        jsonBody  = "{" + AllCardsUrlPostBodyContent;
                        jsonBody += AllCardsUrlPageIdentifier + page;
                        jsonBody += "}";
                    }
                    yield return(UnityExtensionMethods.SaveUrlToFile(cardsUrl, cardsFile, jsonBody));

                    if (AllCardsUrlZipped)
                    {
                        UnityExtensionMethods.ExtractZip(cardsFile, GameDirectoryPath);
                    }
                    if (AllCardsUrlWrapped)
                    {
                        UnityExtensionMethods.UnwrapFile(cardsFile.EndsWith(UnityExtensionMethods.ZipExtension)
                            ? cardsFile.Remove(cardsFile.Length - UnityExtensionMethods.ZipExtension.Length) : cardsFile);
                    }
                    // Sometimes, we need to get the AllCardsUrlPageCount from the first page of AllCardsUrl
                    if (page == AllCardsUrlPageCountStartIndex && !string.IsNullOrEmpty(AllCardsUrlPageCountIdentifier))
                    {
                        LoadCards(page);
                    }
                }
            }

            IsDownloading  = false;
            DownloadStatus = "Complete!";
            HasDownloaded  = true;
            HasLoaded      = false;
        }