No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SkAdNetworkJsonParser.cs 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEngine;
  6. namespace UnityEngine.Advertisements.Editor {
  7. internal class SkAdNetworkJsonParser : ISkAdNetworkParser {
  8. [Serializable]
  9. public class SkAdNetworkIdArray {
  10. public List<SkAdNetworkInfo> skadnetwork_ids;
  11. }
  12. [Serializable]
  13. public class SkAdNetworkInfo {
  14. public string skadnetwork_id;
  15. }
  16. public string GetExtension() {
  17. return SkAdNetworkFileExtension.JSON;
  18. }
  19. public HashSet<string> ParseSource(ISkAdNetworkSource source) {
  20. var foundIds = new HashSet<string>();
  21. try {
  22. string jsonData;
  23. using (var stream = source.Open()) {
  24. if (stream == null) {
  25. Debug.LogWarning($"[Unity SKAdNetwork Parser] Unable to parse SKAdNetwork file: {source.Path}");
  26. return foundIds;
  27. }
  28. jsonData = new StreamReader(stream).ReadToEnd();
  29. }
  30. SkAdNetworkIdArray skAdNetworkCompanyInfo = null;
  31. try {
  32. skAdNetworkCompanyInfo = JsonUtility.FromJson<SkAdNetworkIdArray>(jsonData);
  33. } catch (Exception) { }
  34. //Fallback to try and see if this is a JSONObject which contains an array element called skadnetwork_ids instead of the expected JSONArray
  35. if (skAdNetworkCompanyInfo?.skadnetwork_ids == null || skAdNetworkCompanyInfo.skadnetwork_ids.Count == 0) {
  36. var updatedJson = "{\"skadnetwork_ids\":" + jsonData + "}";
  37. skAdNetworkCompanyInfo = JsonUtility.FromJson<SkAdNetworkIdArray>(updatedJson);
  38. }
  39. if (skAdNetworkCompanyInfo?.skadnetwork_ids == null) {
  40. Debug.LogWarning($"[Unity SKAdNetwork Parser] Unable to parse SKAdNetwork file: {source.Path}");
  41. return foundIds;
  42. }
  43. foundIds.UnionWith(skAdNetworkCompanyInfo.skadnetwork_ids.Select(t => t.skadnetwork_id).Where(t => t != null));
  44. } catch (Exception) {
  45. Debug.LogWarning($"[Unity SKAdNetwork Parser] Unable to parse SKAdNetwork file: {source.Path}");
  46. }
  47. return foundIds;
  48. }
  49. }
  50. }