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.

223 lines
7.1 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
6 years ago
6 years ago
6 years ago
6 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using IPA.JsonConverters;
  5. using IPA.Utilities;
  6. using Newtonsoft.Json;
  7. using SemVer;
  8. using Version = SemVer.Version;
  9. namespace IPA.Updating.BeatMods
  10. {
  11. class ApiEndpoint
  12. {
  13. public const string BeatModBase = "https://beatmods.com";
  14. public const string ApiBase = BeatModBase + "/api/v1/mod";
  15. public const string GetModInfoEndpoint = "?name={0}&version={1}";
  16. public const string GetModsByName = "?name={0}";
  17. class HexArrayConverter : JsonConverter
  18. {
  19. public override bool CanConvert(Type objectType)
  20. {
  21. return objectType == typeof(byte[]);
  22. }
  23. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  24. {
  25. if (reader.TokenType == JsonToken.Null)
  26. {
  27. return null;
  28. }
  29. if (reader.TokenType == JsonToken.String)
  30. {
  31. try
  32. {
  33. return Utils.StringToByteArray((string)reader.Value);
  34. }
  35. catch (Exception ex)
  36. {
  37. throw new Exception($"Error parsing version string: {reader.Value}", ex);
  38. }
  39. }
  40. throw new Exception(
  41. $"Unexpected token or value when parsing hex string. Token: {reader.TokenType}, Value: {reader.Value}");
  42. }
  43. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  44. {
  45. if (value == null)
  46. {
  47. writer.WriteNull();
  48. }
  49. else
  50. {
  51. if (!(value is byte[]))
  52. {
  53. throw new JsonSerializationException("Expected byte[] object value");
  54. }
  55. writer.WriteValue(Utils.ByteArrayToString((byte[]) value));
  56. }
  57. }
  58. }
  59. [Serializable]
  60. public class Mod
  61. {
  62. #pragma warning disable CS0649
  63. /// <summary>
  64. /// Will be a useless string of characters. Do not use.
  65. /// </summary>
  66. [JsonProperty("_id")]
  67. public string Id;
  68. [JsonProperty("required")]
  69. public bool Required;
  70. [JsonProperty("name")]
  71. public string Name;
  72. [JsonProperty("version"),
  73. JsonConverter(typeof(SemverVersionConverter))]
  74. public Version Version;
  75. [Serializable]
  76. public class AuthorType
  77. {
  78. [JsonProperty("username")]
  79. public string Name;
  80. [JsonProperty("_id")]
  81. public string Id;
  82. public override string ToString() => Name;
  83. }
  84. [JsonProperty("author")]
  85. public AuthorType Author;
  86. /*[Serializable]
  87. public class DetailsData
  88. {
  89. [JsonProperty("author")]
  90. public AuthorType Author;
  91. [JsonProperty("title")]
  92. public string Title;
  93. [JsonProperty("description")]
  94. public string Description;
  95. [JsonProperty("published")]
  96. public string Published;
  97. }
  98. [JsonProperty("details")]
  99. public DetailsData Details;*/
  100. [JsonProperty("status")]
  101. public string Status;
  102. public const string ApprovedStatus = "approved";
  103. [JsonProperty("description")]
  104. public string Description;
  105. [JsonProperty("category")]
  106. public string Category;
  107. [JsonProperty("link")]
  108. public Uri Link;
  109. #pragma warning restore CS0649
  110. /*[Serializable]
  111. public class PlatformFile
  112. {
  113. [JsonProperty("hash"),
  114. JsonConverter(typeof(HexArrayConverter))]
  115. public byte[] Hash = new byte[20];
  116. [JsonProperty("files", ItemConverterType = typeof(HexArrayConverter))]
  117. public Dictionary<string, byte[]> FileHashes = new Dictionary<string, byte[]>();
  118. [JsonProperty("url")]
  119. public string DownloadPath;
  120. public override string ToString() =>
  121. $"{Utils.ByteArrayToString(Hash)}@{DownloadPath}({string.Join(",", FileHashes.Select(o => $"\"{o.Key}\":\"{Utils.ByteArrayToString(o.Value)}\""))})";
  122. }
  123. [Serializable]
  124. public class FilesObject
  125. {
  126. [JsonProperty("steam")]
  127. public PlatformFile Steam;
  128. [JsonProperty("oculus")]
  129. public PlatformFile Oculus;
  130. }
  131. [JsonProperty("files")]
  132. public FilesObject Files;*/
  133. [Serializable]
  134. public class DownloadsObject
  135. {
  136. public const string TypeUniversal = "universal";
  137. public const string TypeSteam = "steam";
  138. public const string TypeOculus = "oculus";
  139. [JsonProperty("type")]
  140. public string Type;
  141. [JsonProperty("url")]
  142. public string Path;
  143. [Serializable]
  144. public class HashObject
  145. {
  146. [JsonProperty("hash"), JsonConverter(typeof(HexArrayConverter))]
  147. public byte[] Hash;
  148. [JsonProperty("file")]
  149. public string File;
  150. }
  151. /// <summary>
  152. /// Hashes stored are MD5
  153. /// </summary>
  154. [JsonProperty("hashMd5")]
  155. public HashObject[] Hashes;
  156. }
  157. [JsonProperty("downloads")]
  158. public DownloadsObject[] Downloads;
  159. /*public class Dependency
  160. {
  161. public string Name = null;
  162. public Range VersionRange = null;
  163. }
  164. [Serializable]
  165. public class LinksType
  166. {
  167. [JsonProperty("dependencies", ItemConverterType = typeof(ModSaberDependencyConverter))]
  168. public Dependency[] Dependencies = new Dependency[0];
  169. [JsonProperty("conflicts", ItemConverterType = typeof(ModSaberDependencyConverter))]
  170. public Dependency[] Conflicts = new Dependency[0];
  171. }
  172. [JsonProperty("links")]
  173. public LinksType Links;
  174. [JsonProperty("oldVersions", ItemConverterType = typeof(SemverVersionConverter))]
  175. public Version[] OldVersions = new Version[0];*/
  176. [JsonProperty("dependencies")]
  177. public Mod[] Dependencies;
  178. public override string ToString()
  179. {
  180. return $"{{\"{Name}\"v{Version} by {Author} files for {string.Join(", ", Downloads.Select(d => d.Type))}}}";
  181. }
  182. }
  183. }
  184. }