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

274 行
10 KiB

using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using MetaCity.BundleKit.Editor.Utils;
using UnityEngine;
namespace MetaCity.BundleKit.Editor
{
public class MetacityClient
{
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_u3d = "http://universe.u3d.cn/api/v2";
private static readonly string baseUri = baseUri_test;
private static string UploadUrl = "/template/official?uploadbundle=true";
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;
}
// 返回当前url
public static string GetUploadUrl(WebUrl testmode)
{
string org = "/api/v2";
string url = baseUri_test.Replace(org, UploadUrl);
if (testmode == WebUrl.MetacityInt)
{
url = baseUri_int.Replace(org, UploadUrl);
}
else if (testmode == WebUrl.U3D)
{
url = baseUri_u3d.Replace(org, UploadUrl);
}
//将baseUri中的/api/v2替换为/template/official?uploadbundle=true
return url;
}
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<List<AssetBundleResponse>> GetAssetBundleByTemplateId(string templateId)
{
HttpResponseMessage response = await client.GetAsync(string.Format("{0}/artifact/template/{1}", baseUri, templateId));
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
List<AssetBundleResponse> abResponses = JsonConvert.DeserializeObject<List<AssetBundleResponse>>(json);
return abResponses;
}
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;
}
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> Publish(
string templateId,
string cloudrenderingId,
string serverType,
List<AssetBundle> assetBundles)
{
var data = new PublishInput()
{
templateId = templateId,
cloudrenderingId = cloudrenderingId,
serverType = serverType,
assetBundles = assetBundles
};
var jsonInput = JsonTools.ToJson(data);
HttpContent content = new StringContent(jsonInput, Encoding.UTF8, "application/json");
HttpResponseMessage response =
await client.PostAsync(string.Format("{0}/publish", baseUri), content);
if (response.IsSuccessStatusCode)
{
return true;
}
else
{
Debug.LogError(response.ToString());
}
return false;
}
public static async Task<bool> PublishAssetBundles(List<AssetBundle> assetBundles)
{
var data = new PublishAssetBundleInput()
{
assetBundles = assetBundles
};
var jsonInput = JsonTools.ToJson(data);
HttpContent content = new StringContent(jsonInput, Encoding.UTF8, "application/json");
HttpResponseMessage response =
await client.PostAsync(string.Format("{0}/publish/assetbundles", baseUri), content);
if (response.IsSuccessStatusCode)
{
return true;
}
else
{
Debug.LogError(response.ToString());
}
return false;
}
}
}