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.

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