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.

123 lines
4.3 KiB

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