您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

247 行
9.3 KiB

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<string> 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<UrlResponse>(json);
return urlResponse.url;
}
return null;
}
public static async Task<string> 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<UrlResponse>(json);
return urlResponse.url;
}
return null;
}
public static async Task<TemplateResponse> 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<TemplateResponse>(json);
return templateResponse;
}
else
{
Debug.LogError(response.ToString());
}
return null;
}
public static async Task<CloudrenderingResponse> 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<CloudrenderingResponse>(json);
return crResponse;
}
else
{
Debug.LogError(response.ToString());
}
return null;
}
public static async Task<bool> 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<string> 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<UserResponse>(json);
return userResponse?.id;
}
else
{
Debug.LogError(response.ToString());
}
return null;
}
public static async Task<TemplateResponse> 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<TemplateResponse>(json);
return templateResponse;
}
else
{
Debug.LogError(response.ToString());
}
return null;
}
public static async Task<TemplateResponse> 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<TemplateResponse>(json);
return templateResponse;
}
else
{
Debug.LogError(response.ToString());
}
return null;
}
public static async Task<CloudrenderingResponse> 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<CloudrenderingResponse>(json);
return crResponse;
}
else
{
Debug.LogError(response.ToString());
}
return null;
}
public static async Task<bool> PublishPrepare(
string userId,
string templateId,
string cloudrenderingId,
string serverType,
string crUrl,
string crPath,
string crFileName,
long crSize,
string uosUrl,
string uosPath,
List<EnvironmentVariable> environmentVariables,
List<GameServerPort> gameServerPorts,
bool firstPublish)
{
Dictionary<string, string> envVerbs = new Dictionary<string, string>();
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;
}
}
}