using System.Collections.Generic; using Newtonsoft.Json; using System.Net.Http; using System.Text; using System.Threading.Tasks; using UnityEngine; namespace Metacity.Publish.Editor { public class MetacityInterface { public static HttpClient client = new HttpClient(); private static readonly string baseUri_int = "https://metacity-int.unity.cn/api/v2"; private static readonly string baseUri_test = "http://localhost:4200/api/v2"; private static readonly string baseUri = baseUri_test; private static string _token; public static async Task GetUploadUrlAsync(string filename) { HttpResponseMessage response = await client.GetAsync(string.Format("{0}/resource/upload-url?file={1}", baseUri_int, filename)); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); UrlResponse urlResponse = JsonConvert.DeserializeObject(json); return urlResponse.url; } return null; } public static async Task GetDownloadUrlAsync(string filename) { HttpResponseMessage response = await client.GetAsync(string.Format("{0}/resource/download-url?file={1}", baseUri_int, filename)); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); UrlResponse urlResponse = JsonConvert.DeserializeObject(json); return urlResponse.url; } return null; } public static async Task GetTemplateById(string templateId) { HttpResponseMessage response = await client.GetAsync(string.Format("{0}/template/id/{1}", baseUri, templateId)); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); TemplateResponse templateResponse = JsonConvert.DeserializeObject(json); return templateResponse; } else { Debug.LogError(response.ToString()); } return null; } public static async Task GetCloudrenderingById(string cloudrenderingId) { HttpResponseMessage response = await client.GetAsync(string.Format("{0}/cloudrendering/{1}", baseUri, cloudrenderingId)); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); CloudrenderingResponse crResponse = JsonConvert.DeserializeObject(json); return crResponse; } else { Debug.LogError(response.ToString()); } return null; } public static async Task CheckAccessToken() { HttpResponseMessage response = await client.GetAsync(string.Format("{0}/users/me", baseUri)); if (response.IsSuccessStatusCode) { return true; } Debug.LogError("This access token is out of data or invalid."); return false; } public static async Task GetUserId() { HttpResponseMessage response = await client.GetAsync(string.Format("{0}/users/me", baseUri)); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); UserResponse userResponse = JsonConvert.DeserializeObject(json); return userResponse?.id; } else { Debug.LogError(response.ToString()); } return null; } public static async Task CreateTemplate(string name, string description, string banner, string platform = "cloudrendering") { var data = new TemplateInput { name = name, description = description, banner = banner, platform = platform, official = false, state = "Draft" }; var jsonInput = JsonTools.ToJson(data); HttpContent content = new StringContent(jsonInput,Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync( string.Format("{0}/template", baseUri), content); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); TemplateResponse templateResponse = JsonConvert.DeserializeObject(json); return templateResponse; } else { Debug.LogError(response.ToString()); } return null; } public static async Task UpdateTemplateById(TemplateResponse oldTemplate, string templateId, string name, string description, string banner, string activeId, string platform = "cloudrendering") { var data = new TemplateInput { name = name, description = description, banner = banner, platform = platform, activeId = activeId, official = oldTemplate.official, state = oldTemplate.state, uosId = oldTemplate.uosId }; var jsonInput = JsonTools.ToJson(data); HttpContent content = new StringContent(jsonInput,Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync( string.Format("{0}/template/{1}", baseUri, templateId), content); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); TemplateResponse templateResponse = JsonConvert.DeserializeObject(json); return templateResponse; } else { Debug.LogError(response.ToString()); } return null; } public static async Task CreateCloudrendering(string name, string description, string template, string version = "0.0.1") { var data = new CreateCRInput() { name = name, description = description, version = version, template = template }; var jsonInput = JsonTools.ToJson(data); HttpContent content = new StringContent(jsonInput,Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync( string.Format("{0}/cloudrendering", baseUri), content); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); CloudrenderingResponse crResponse = JsonConvert.DeserializeObject(json); return crResponse; } else { Debug.LogError(response.ToString()); } return null; } public static async Task PublishPrepare( string userId, string templateId, string cloudrenderingId, string serverType, string crUrl, string crPath, string crFileName, long crSize, string uosUrl, string uosPath, List environmentVariables, List gameServerPorts, bool firstPublish) { Dictionary envVerbs = new Dictionary(); foreach (var variable in environmentVariables) { envVerbs.Add(variable.name, variable.value); } var data = new PublishInput() { userId = userId, templateId = templateId, cloudrenderingId = cloudrenderingId, serverType = serverType, crUrl = crUrl, crPath = crPath, crFileName = crFileName, crSize = crSize, uosUrl = uosUrl, uosPath = uosPath, environmentVariables = envVerbs, gameServerPorts = gameServerPorts, firstPublish = firstPublish }; var jsonInput = JsonTools.ToJson(data); HttpContent content = new StringContent(jsonInput, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(string.Format("{0}/publish/prepare", baseUri), content); if (response.IsSuccessStatusCode) { return true; } else { Debug.LogError(response.ToString()); } return false; } } }