Skip to content

Commit

Permalink
Add CGameCtnChallenge option
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBang1112 committed Dec 23, 2024
1 parent 5cec525 commit 5dd4c52
Showing 1 changed file with 137 additions and 10 deletions.
147 changes: 137 additions & 10 deletions Src/ManiaAPI.NadeoAPI.Extensions.Gbx/NadeoServicesExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using GBX.NET.Engines.Game;
using GBX.NET;
using GBX.NET.Engines.Game;
using ManiaAPI.NadeoAPI.JsonContexts;
using System.Diagnostics;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using TmEssentials;

namespace ManiaAPI.NadeoAPI.Extensions.Gbx;
Expand Down Expand Up @@ -33,9 +32,7 @@ public static async Task<MapInfo> UploadMapAsync(this INadeoServices services, S

using var content = CreateContent(bufferedStream, fileName);

using var response = await services.SendAsync(HttpMethod.Post, "maps/", content, cancellationToken);

return await response.Content.ReadFromJsonAsync(NadeoAPIJsonContext.Default.MapInfo, cancellationToken) ?? throw new Exception("This shouldn't be null.");
return await UploadMapAsync(services, content, cancellationToken);
}

/// <summary>
Expand Down Expand Up @@ -69,6 +66,67 @@ public static async Task<MapInfo> UploadMapAsync(this INadeoServices services, s
return await UploadMapAsync(services, stream, Path.GetFileName(filePath), cancellationToken);
}

/// <summary>
/// Does not work with <see cref="AuthorizationMethod.DedicatedServer"/>.
/// </summary>
/// <param name="services"></param>
/// <param name="mapGbx"></param>
/// <param name="settings"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="FormatException">Author login is invalid.</exception>
/// <exception cref="NadeoAPIResponseException"></exception>
public static async Task<MapInfo> UploadMapAsync(this INadeoServices services, Gbx<CGameCtnChallenge> mapGbx, GbxWriteSettings settings = default, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(mapGbx);

if (string.IsNullOrWhiteSpace(mapGbx.FilePath))
{
throw new ArgumentException("The file path in Gbx is invalid.", nameof(mapGbx));
}

await using var stream = new MemoryStream();
mapGbx.Save(stream, settings);
stream.Position = 0;

using var content = CreateContent(stream, Path.GetFileName(mapGbx.FilePath), mapGbx.Node);

return await UploadMapAsync(services, content, cancellationToken);
}

/// <summary>
/// Does not work with <see cref="AuthorizationMethod.DedicatedServer"/>.
/// </summary>
/// <param name="services"></param>
/// <param name="map"></param>
/// <param name="fileName"></param>
/// <param name="settings"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="FormatException">Author login is invalid.</exception>
/// <exception cref="NadeoAPIResponseException"></exception>
public static async Task<MapInfo> UploadMapAsync(this INadeoServices services, CGameCtnChallenge map, string fileName, GbxWriteSettings settings = default, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(map);
ArgumentNullException.ThrowIfNull(fileName);

await using var stream = new MemoryStream();
map.Save(stream, settings);
stream.Position = 0;

using var content = CreateContent(stream, fileName, map);

return await UploadMapAsync(services, content, cancellationToken);
}

private static async Task<MapInfo> UploadMapAsync(INadeoServices services, MultipartFormDataContent content, CancellationToken cancellationToken)
{
using var response = await services.SendAsync(HttpMethod.Post, "maps/", content, cancellationToken);
return await response.Content.ReadFromJsonAsync(NadeoAPIJsonContext.Default.MapInfo, cancellationToken) ?? throw new Exception("This shouldn't be null.");
}

/// <summary>
/// Does not work with <see cref="AuthorizationMethod.DedicatedServer"/>.
/// </summary>
Expand All @@ -90,9 +148,7 @@ public static async Task<MapInfo> UpdateMapAsync(this INadeoServices services, G

using var content = CreateContent(bufferedStream, fileName);

using var response = await services.SendAsync(HttpMethod.Post, $"maps/{mapId}", content, cancellationToken);

return await response.Content.ReadFromJsonAsync(NadeoAPIJsonContext.Default.MapInfo, cancellationToken) ?? throw new Exception("This shouldn't be null.");
return await UpdateMapAsync(services, mapId, content, cancellationToken);
}

/// <summary>
Expand Down Expand Up @@ -124,16 +180,87 @@ public static async Task<MapInfo> UpdateMapAsync(this INadeoServices services, G
/// <exception cref="NadeoAPIResponseException"></exception>
public static async Task<MapInfo> UpdateMapAsync(this INadeoServices services, Guid mapId, string filePath, CancellationToken cancellationToken = default)
{
ArgumentException.ThrowIfNullOrEmpty(filePath);

await using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous);
return await UpdateMapAsync(services, mapId, stream, Path.GetFileName(filePath), cancellationToken);
}

/// <summary>
/// Does not work with <see cref="AuthorizationMethod.DedicatedServer"/>.
/// </summary>
/// <param name="services"></param>
/// <param name="mapId"></param>
/// <param name="mapGbx"></param>
/// <param name="settings"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="FormatException">Author login is invalid.</exception>
/// <exception cref="NadeoAPIResponseException"></exception>
public static async Task<MapInfo> UpdateMapAsync(this INadeoServices services, Guid mapId, Gbx<CGameCtnChallenge> mapGbx, GbxWriteSettings settings = default, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(mapGbx);

if (string.IsNullOrWhiteSpace(mapGbx.FilePath))
{
throw new ArgumentException("The file path in Gbx is invalid.", nameof(mapGbx));
}

await using var stream = new MemoryStream();
mapGbx.Save(stream, settings);
stream.Position = 0;

using var content = CreateContent(stream, Path.GetFileName(mapGbx.FilePath), mapGbx.Node);

return await UpdateMapAsync(services, mapId, content, cancellationToken);
}

/// <summary>
/// Does not work with <see cref="AuthorizationMethod.DedicatedServer"/>.
/// </summary>
/// <param name="services"></param>
/// <param name="mapId"></param>
/// <param name="map"></param>
/// <param name="fileName"></param>
/// <param name="settings"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="FormatException">Author login is invalid.</exception>
/// <exception cref="NadeoAPIResponseException"></exception>
public static async Task<MapInfo> UpdateMapAsync(this INadeoServices services, Guid mapId, CGameCtnChallenge map, string fileName, GbxWriteSettings settings = default, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(map);
ArgumentNullException.ThrowIfNull(fileName);

await using var stream = new MemoryStream();
map.Save(stream, settings);
stream.Position = 0;

using var content = CreateContent(stream, fileName, map);

return await UpdateMapAsync(services, mapId, content, cancellationToken);
}

private static async Task<MapInfo> UpdateMapAsync(INadeoServices services, Guid mapId, MultipartFormDataContent content, CancellationToken cancellationToken)
{
using var response = await services.SendAsync(HttpMethod.Post, $"maps/{mapId}", content, cancellationToken);
return await response.Content.ReadFromJsonAsync(NadeoAPIJsonContext.Default.MapInfo, cancellationToken) ?? throw new Exception("This shouldn't be null.");
}


private static MultipartFormDataContent CreateContent(BufferedStream bufferedStream, string fileName)
{
var map = GBX.NET.Gbx.ParseHeaderNode<CGameCtnChallenge>(bufferedStream);

bufferedStream.Position = 0;

return CreateContent(bufferedStream, fileName, map);
}

private static MultipartFormDataContent CreateContent(Stream stream, string fileName, CGameCtnChallenge map)
{
return new MultipartFormDataContent
{
{ new StringContent(map.AuthorTime?.TotalMilliseconds.ToString() ?? "-1"), "authorScore" },
Expand All @@ -147,7 +274,7 @@ private static MultipartFormDataContent CreateContent(BufferedStream bufferedStr
{ new StringContent(map.MapUid), "mapUid" },
{ new StringContent(map.MapName), "name" },
{ new StringContent(@"{""isPlayable"":true}", Encoding.UTF8, "application/json"), "nadeoservices-core-parameters" },
{ new StreamContent(bufferedStream), "data", fileName }
{ new StreamContent(stream), "data", fileName }
};
}
}

0 comments on commit 5dd4c52

Please sign in to comment.