private IEnumerator DownloadIASData(bool cachedDataLoaded = false) { // Wait for an active internet connection while (Application.internetReachability == NetworkReachability.NotReachable) { yield return(null); } ProjectManager.AdvLog("IAS downloading data.."); // Iterate through each JSON file for (int jsonFileId = 0; jsonFileId < jsonUrls.Length; jsonFileId++) { // Download the JSON file WWW wwwJSON = new WWW(jsonUrls[jsonFileId]); // Wait for the JSON data to be downloaded yield return(wwwJSON); // Check for any errors if (!string.IsNullOrEmpty(wwwJSON.error)) { Analytics.LogError("IAS", "JSON download error! " + wwwJSON.error); yield break; } else if (wwwJSON.text.Contains("There was an error")) { Analytics.LogError("IAS", "JSON download error! Serverside system error!"); yield break; } else if (string.IsNullOrEmpty(wwwJSON.text)) { Analytics.LogError("IAS", "JSON download error! Empty JSON!"); yield break; } JsonFileData tempAdvertData = JsonUtility.FromJson <JsonFileData>(wwwJSON.text); // Dispose of the wwwJSON data (clear it from memory) wwwJSON.Dispose(); if (tempAdvertData == null) { Analytics.LogError("IAS", "Temp advert data was null!"); yield break; } if (!DoesSlotFileIdExist(jsonFileId)) { advertData.Add(new AdJsonFileData()); } bool needToReloadAdSlots = !cachedDataLoaded; bool needToRandomizeSlot = false; // We're currently only using the slots, not containers for (int i = 0; i < tempAdvertData.slots.Count; i++) { JsonSlotData curSlot = tempAdvertData.slots[i]; // We'll be converting the slot id (e.g 1a, 1c or 2f) into just number and just character values int slotInt; char slotChar; // Attempt to extract the slot int from the slot id if (!int.TryParse(Regex.Replace(curSlot.slotid, "[^0-9]", ""), out slotInt)) { Analytics.LogError("IAS", "Failed to parse slot int from '" + curSlot.slotid + "'"); yield break; } // Attempt to extract the slot character from the slot id if (!char.TryParse(Regex.Replace(curSlot.slotid, "[^a-z]", ""), out slotChar)) { Analytics.LogError("IAS", "Failed to parse slot char from '" + curSlot.slotid + "'"); yield break; } // If this slot doesn't exist yet create a new slot for it if (!DoesSlotIntExist(jsonFileId, slotInt)) { advertData[jsonFileId].slotInts.Add(new AdSlotData(slotInt, new List <AdData>())); } // Get the index in the list for slotInt int slotDataIndex = GetSlotIndex(jsonFileId, slotInt); if (slotDataIndex < 0) { Analytics.LogError("IAS", "Failed to get slotDataIndex!"); yield break; } // Make sure this slot char isn't repeated in the json file within this slot int for some reason if (!DoesSlotCharExist(jsonFileId, slotInt, slotChar)) { advertData[jsonFileId].slotInts[slotDataIndex].advert.Add(new AdData(slotChar)); needToRandomizeSlot = true; } int slotAdIndex = GetAdIndex(jsonFileId, slotInt, slotChar); if (slotAdIndex < 0) { Analytics.LogError("IAS", "Failed to get slotAdIndex! Could not find " + slotInt + ", " + slotChar.ToString()); yield break; } AdData curAdData = advertData[jsonFileId].slotInts[slotDataIndex].advert[slotAdIndex]; // Extract the bundleId of the advert #if UNITY_ANDROID // Regex extracts the id GET request from the URL which is the package name of the game // (replaces everything that does NOT match id=blahblah END or NOT match id=blahblah AMERPERSAND string packageName = Regex.Match(curSlot.adurl, "(?<=id=)((?!(&|\\?)).)*").Value; #elif UNITY_IOS // IOS we just need to grab the name after the hash in the URL string packageName = Regex.Match(curSlot.adurl, "(?<=.*#).*").Value; #else // For other platforms we should be fine to just use the full URL for package name comparisons as we'll be using .Compare // And other platforms won't include any other referral bundle ids in their URLs string packageName = curSlot.adurl; #endif string imageFileType = Regex.Match(curSlot.imgurl, "(?<=/uploads/adverts/.*)\\.[A-z]*[^(\\?|\")]").Value; curAdData.fileName = curSlot.slotid + imageFileType; curAdData.isSelf = packageName.Contains(bundleId); curAdData.isActive = curSlot.active; curAdData.isInstalled = IsPackageInstalled(packageName); curAdData.adUrl = curSlot.adurl; curAdData.packageName = packageName; curAdData.imgUrl = curSlot.imgurl; if (curAdData.newUpdateTime < curSlot.updatetime || curAdData.newUpdateTime == 0L) { needToReloadAdSlots = true; } curAdData.newUpdateTime = curSlot.updatetime; // I'm not pre-downloading all the images here because it takes quite a long time to download even on our fast ethernet connection (~15 seconds) // So I think it's best to download the images (if needed) when the ads are called to be refreshed } if (needToRandomizeSlot) { RandomizeAdSlots(jsonFileId); } if (needToReloadAdSlots) { RefreshActiveAdSlots(jsonFileId); } } SaveIASData(); ProjectManager.AdvLog("IAS Done"); }