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.

147 lines
4.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. using IPA.Logging;
  2. using IPA.Updating.Converters;
  3. using IPA.Utilities;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Converters;
  6. using SemVer;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using Version = SemVer.Version;
  14. namespace IPA.Updating.ModsaberML
  15. {
  16. class ApiEndpoint
  17. {
  18. #if DEBUG && UPDATETEST
  19. public const string ApiBase = "file://Z:/Users/aaron/Source/Repos/IPA-Reloaded-BeatSaber/IPA.Tests/";
  20. public const string GetApprovedEndpoint = "updater_test.json";
  21. #else
  22. public const string ApiBase = "https://www.modsaber.ml/";
  23. public const string GetApprovedEndpoint = "registry/{0}/{1}";
  24. #endif
  25. class HexArrayConverter : JsonConverter
  26. {
  27. public override bool CanConvert(Type objectType)
  28. {
  29. return objectType == typeof(byte[]);
  30. }
  31. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  32. {
  33. if (reader.TokenType == JsonToken.Null)
  34. {
  35. return null;
  36. }
  37. if (reader.TokenType == JsonToken.String)
  38. {
  39. try
  40. {
  41. return LoneFunctions.StringToByteArray((string)reader.Value);
  42. }
  43. catch (Exception ex)
  44. {
  45. throw new Exception(string.Format("Error parsing version string: {0}", reader.Value), ex);
  46. }
  47. }
  48. throw new Exception(string.Format("Unexpected token or value when parsing hex string. Token: {0}, Value: {1}", reader.TokenType, reader.Value));
  49. }
  50. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  51. {
  52. if (value == null)
  53. {
  54. writer.WriteNull();
  55. }
  56. else
  57. {
  58. if (!(value is byte[]))
  59. {
  60. throw new JsonSerializationException("Expected byte[] object value");
  61. }
  62. writer.WriteValue(LoneFunctions.ByteArrayToString(value as byte[]));
  63. }
  64. }
  65. }
  66. [Serializable]
  67. public class Mod
  68. {
  69. #pragma warning disable CS0649
  70. [JsonProperty("name")]
  71. public string Name;
  72. [JsonProperty("version"),
  73. JsonConverter(typeof(SemverVersionConverter))]
  74. public Version Version;
  75. [JsonProperty("approved")]
  76. public bool Approved;
  77. [JsonProperty("title")]
  78. public string Title;
  79. [JsonProperty("gameVersion"),
  80. JsonConverter(typeof(SemverVersionConverter))]
  81. public Version GameVersion;
  82. [JsonProperty("author")]
  83. public string Author;
  84. #pragma warning restore CS0649
  85. [Serializable]
  86. public class PlatformFile
  87. {
  88. [JsonProperty("hash"),
  89. JsonConverter(typeof(HexArrayConverter))]
  90. public byte[] Hash = new byte[20];
  91. [JsonProperty("files", ItemConverterType = typeof(HexArrayConverter))]
  92. public Dictionary<string, byte[]> FileHashes = new Dictionary<string, byte[]>();
  93. [JsonProperty("url")]
  94. public string DownloadPath = null;
  95. public override string ToString()
  96. {
  97. return $"{LoneFunctions.ByteArrayToString(Hash)}@{DownloadPath}({string.Join(",",FileHashes.Select(o=>$"\"{o.Key}\":\"{LoneFunctions.ByteArrayToString(o.Value)}\""))})";
  98. }
  99. }
  100. [Serializable]
  101. public class FilesObject
  102. {
  103. [JsonProperty("steam")]
  104. public PlatformFile Steam = null;
  105. [JsonProperty("oculus")]
  106. public PlatformFile Oculus = null;
  107. }
  108. [JsonProperty("files")]
  109. public FilesObject Files = null;
  110. public class Dependency
  111. {
  112. public string Name = null;
  113. public Range VersionRange = null;
  114. }
  115. [JsonProperty("dependsOn", ItemConverterType = typeof(ModsaberDependencyConverter))]
  116. public Dependency[] Dependencies = new Dependency[0];
  117. [JsonProperty("oldVersions", ItemConverterType = typeof(SemverVersionConverter))]
  118. public Version[] OldVersions = new Version[0];
  119. public override string ToString()
  120. {
  121. return $"{{\"{Title} ({Name})\"v{Version} for {GameVersion} by {Author} with \"{Files.Steam}\" and \"{Files.Oculus}\"}}";
  122. }
  123. }
  124. }
  125. }