-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerHandler.cs
More file actions
140 lines (132 loc) · 4.73 KB
/
ServerHandler.cs
File metadata and controls
140 lines (132 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using BTD_Mod_Helper.Extensions;
using Il2CppAssets.Scripts.Unity;
using Il2CppAssets.Scripts.Unity.UI_New.InGame;
using JetBrains.Annotations;
using MelonLoader;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Octokit;
using Octokit.Internal;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using static BloonFactory.UI.BloonBrowserMenuPanel;
namespace BloonFactory
{
internal static class ServerHandler
{
public const string URL = "https://server.bloonfactory.org/";
private static HttpClient client = new HttpClient();
internal static async Task<PageUpdateRequest> RequestPageUpdate()
{
HttpResponseMessage response = await client.GetAsync(URL + "getPage");
response.EnsureSuccessStatusCode();
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
var obj = JsonConvert.DeserializeObject<PageUpdateRequest>(Encoding.UTF8.GetString(bytes), new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.None });
return obj;
}
internal static async Task<BloonTemplate> DownloadTemplate(Guid guid)
{
HttpResponseMessage response = await client.GetAsync(URL + $"getTemplate={guid.ToString()}");
MelonLogger.Msg($"Requested template from server ({guid.ToString()})");
response.EnsureSuccessStatusCode();
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
try
{
var obj = JsonConvert.DeserializeObject<BloonTemplate>(Encoding.UTF8.GetString(bytes), SerializationHandler.Settings);
return obj;
}
catch (Exception ex)
{
MelonLogger.Msg(ex);
return null;
}
}
internal static async Task<(bool success, string errorCode)> UploadTemplate(BloonTemplate template, BloonCategory category, string description)
{
string creator = Game.Player.LiNKAccount?.DisplayName;
if (string.IsNullOrEmpty(creator))
{
MelonLogger.Msg("You must be logged into a NK account to upload a template.");
return (false, "You must be logged into a NK account to upload a template.");
}
MelonLogger.Msg($"Uploading template to server ({template.Name})");
UploadTemplateRequest request = new UploadTemplateRequest()
{
Name = template.Name,
Guid = template.Guid,
Creator = creator,
Category = (byte)category,
Description = description,
Version = ModHelperData.Version,
TemplateJson = JsonConvert.SerializeObject(template, SerializationHandler.Settings)
};
HttpResponseMessage response = await client.PostAsync(URL + "uploadTemplate", new StringContent(JsonConvert.SerializeObject(request)));
if (!response.IsSuccessStatusCode)
{
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
return (false, Encoding.UTF8.GetString(bytes));
}
return (true, "");
}
}
public class PageUpdateRequest
{
public List<BloonBrowserEntry> Data;
}
public class UploadTemplateRequest
{
public string Name;
public Guid Guid;
public string Creator;
public byte Category;
public string Description;
public string Version = "1.0.0";
public string TemplateJson;
}
public class BloonBrowserEntry
{
public string Name;
public Guid Guid;
public string Creator;
public byte Category;
public string Description;
public string Version = "1.0.0";
public DateTime UploadTime = DateTime.Now;
public int Downloads = 0;
[JsonIgnore]
public BloonCategory CategoryEnum => (BloonCategory)Category;
}
public enum BloonCategory : byte
{
Boss,
VanillaPlus,
Modded
}
public static class CategoryExtensions
{
public static string[] BloonCategoryNames =>
[
"Boss",
"Vanilla+",
"Modded"
];
public static string ToFriendlyString(this BloonCategory category)
{
return category switch
{
BloonCategory.Boss => "Boss",
BloonCategory.VanillaPlus => "Vanilla+",
BloonCategory.Modded => "Modded",
_ => "Unknown",
};
}
}
}