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.

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