Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- using VRC.Core.BestHTTP;
- using VRC.Core.BestHTTP.Authentication;
- using VRC.Core.BestHTTP.Cookies;
- using VRC.Core.BestHTTP.JSON;
- namespace VRC.Core
- {
- // Token: 0x02000015 RID: 21
- public static class API
- {
- // Token: 0x06000077 RID: 119 RVA: 0x00003E70 File Offset: 0x00002070
- public static void SetOrganization(string s)
- {
- API.API_ORGANIZATION = s;
- }
- // Token: 0x06000078 RID: 120 RVA: 0x00003E78 File Offset: 0x00002078
- public static string GetOrganization()
- {
- return API.API_ORGANIZATION;
- }
- // Token: 0x06000079 RID: 121 RVA: 0x00003E80 File Offset: 0x00002080
- public static bool IsReady()
- {
- return API.API_ORGANIZATION != null && API.API_ONLINE_MODE != ApiOnlineMode.Uninitialized;
- }
- // Token: 0x0600007A RID: 122 RVA: 0x00003E9C File Offset: 0x0000209C
- public static T FromCacheOrNew<T>(string id, float maxCacheAge = -1f) where T : ApiModel, ApiCacheObject, new()
- {
- T t = Activator.CreateInstance<T>();
- t.id = id;
- T t2 = t;
- if (!ApiCache.Fetch<T>(id, ref t2, maxCacheAge))
- {
- ApiCache.Save(id, t2, false);
- }
- return t2;
- }
- // Token: 0x0600007B RID: 123 RVA: 0x00003EDC File Offset: 0x000020DC
- public static T CreateFromJson<T>(Dictionary<string, object> json) where T : ApiModel, ApiCacheObject, new()
- {
- T result;
- try
- {
- T t = Activator.CreateInstance<T>();
- string str = null;
- if (!t.SetApiFieldsFromJson(json, ref str))
- {
- Debug.LogError(typeof(T).Name + ": Unable to CreateFromJson: " + str);
- result = (T)((object)null);
- }
- else
- {
- result = t;
- }
- }
- catch (Exception ex)
- {
- Debug.LogError(string.Concat(new string[]
- {
- typeof(T).Name,
- ": Unable to CreateFromJson: ",
- ex.Message,
- "\n",
- ex.StackTrace
- }));
- result = (T)((object)null);
- }
- return result;
- }
- // Token: 0x0600007C RID: 124 RVA: 0x00003FB0 File Offset: 0x000021B0
- public static T Clone<T>(ApiModel model) where T : ApiModel, ApiCacheObject, new()
- {
- return model.Clone(typeof(T), null) as T;
- }
- // Token: 0x0600007D RID: 125 RVA: 0x00003FD0 File Offset: 0x000021D0
- public static T Fetch<T>(string id, Action<ApiContainer> onSuccess = null, Action<ApiContainer> onFailure = null, bool disableCache = false) where T : ApiModel, ApiCacheObject, new()
- {
- API.<Fetch>c__AnonStorey7<T> <Fetch>c__AnonStorey = new API.<Fetch>c__AnonStorey7<T>();
- <Fetch>c__AnonStorey.onSuccess = onSuccess;
- API.<Fetch>c__AnonStorey7<T> <Fetch>c__AnonStorey2 = <Fetch>c__AnonStorey;
- T model = Activator.CreateInstance<T>();
- model.id = id;
- <Fetch>c__AnonStorey2.model = model;
- if (!disableCache && ApiCache.Fetch<T>(id, ref <Fetch>c__AnonStorey.model, 3600f))
- {
- if (<Fetch>c__AnonStorey.onSuccess != null)
- {
- UpdateDelegator.Dispatch(delegate
- {
- <Fetch>c__AnonStorey.onSuccess(new ApiModelContainer<T>(<Fetch>c__AnonStorey.model));
- });
- }
- return <Fetch>c__AnonStorey.model;
- }
- <Fetch>c__AnonStorey.model.Fetch(<Fetch>c__AnonStorey.onSuccess, onFailure, null, false);
- return <Fetch>c__AnonStorey.model;
- }
- // Token: 0x0600007E RID: 126 RVA: 0x00004064 File Offset: 0x00002264
- public static void Delete<T>(string id, Action<ApiContainer> onSuccess = null, Action<ApiContainer> onFailure = null) where T : ApiModel, ApiCacheObject, new()
- {
- T t = Activator.CreateInstance<T>();
- t.id = id;
- T t2 = t;
- t2.Delete(onSuccess, onFailure);
- }
- // Token: 0x0600007F RID: 127 RVA: 0x00004098 File Offset: 0x00002298
- public static void Save(ApiModel model, Action<ApiContainer> onSuccess = null, Action<ApiContainer> onFailure = null)
- {
- model.Save(onSuccess, onFailure);
- }
- // Token: 0x06000080 RID: 128 RVA: 0x000040A4 File Offset: 0x000022A4
- public static void Save(IEnumerable<ApiModel> models, Action<ApiContainer> onSuccess = null, Action<ApiContainer> onFailure = null)
- {
- foreach (ApiModel apiModel in models)
- {
- apiModel.Save(onSuccess, onFailure);
- }
- }
- // Token: 0x06000081 RID: 129 RVA: 0x00004104 File Offset: 0x00002304
- public static void Post(ApiModel model, Action<ApiContainer> onSuccess = null, Action<ApiContainer> onFailure = null)
- {
- model.Post(onSuccess, onFailure, null);
- }
- // Token: 0x06000082 RID: 130 RVA: 0x00004110 File Offset: 0x00002310
- public static void Post(IEnumerable<ApiModel> models, Action<ApiContainer> onSuccess = null, Action<ApiContainer> onFailure = null)
- {
- foreach (ApiModel apiModel in models)
- {
- apiModel.Post(onSuccess, onFailure, null);
- }
- }
- // Token: 0x17000006 RID: 6
- // (get) Token: 0x06000083 RID: 131 RVA: 0x00004170 File Offset: 0x00002370
- public static string DeviceID
- {
- get
- {
- return SystemInfo.deviceUniqueIdentifier;
- }
- }
- // Token: 0x06000084 RID: 132 RVA: 0x00004178 File Offset: 0x00002378
- public static List<string> GetIds(IEnumerable<ApiModel> models)
- {
- List<string> list = new List<string>();
- foreach (ApiModel apiModel in models)
- {
- list.Add(apiModel.id);
- }
- return list;
- }
- // Token: 0x06000085 RID: 133 RVA: 0x000041E4 File Offset: 0x000023E4
- public static string GetAssetPlatformString()
- {
- RuntimePlatform platform = Application.platform;
- if (platform == RuntimePlatform.WindowsPlayer || platform == RuntimePlatform.WindowsEditor)
- {
- return "standalonewindows";
- }
- return "unknownplatform";
- }
- // Token: 0x06000086 RID: 134 RVA: 0x00004210 File Offset: 0x00002410
- public static void SetApiUrlFromEnvironment(ApiServerEnvironment env)
- {
- API.SetApiUrl(API.GetApiUrlForEnvironment(env));
- }
- // Token: 0x06000087 RID: 135 RVA: 0x00004220 File Offset: 0x00002420
- public static string GetApiUrlForEnvironment(ApiServerEnvironment env)
- {
- switch (env)
- {
- case ApiServerEnvironment.Dev:
- return "https://dev-api.vrchat.cloud/api/1/";
- case ApiServerEnvironment.Beta:
- return "https://beta-api.vrchat.cloud/api/1/";
- case ApiServerEnvironment.Release:
- return "https://api.vrchat.cloud/api/1/";
- default:
- Debug.LogError("Unknown server environment! " + env.ToString());
- return string.Empty;
- }
- }
- // Token: 0x06000088 RID: 136 RVA: 0x00004278 File Offset: 0x00002478
- public static void SetApiUrl(string url)
- {
- API.API_URL = url;
- }
- // Token: 0x06000089 RID: 137 RVA: 0x00004280 File Offset: 0x00002480
- public static string GetApiUrl()
- {
- return API.API_URL;
- }
- // Token: 0x0600008A RID: 138 RVA: 0x00004288 File Offset: 0x00002488
- public static bool IsDevApi()
- {
- return API.GetApiUrl() == "https://dev-api.vrchat.cloud/api/1/";
- }
- // Token: 0x0600008B RID: 139 RVA: 0x0000429C File Offset: 0x0000249C
- public static void SendGetRequest(string target, ApiContainer responseContainer = null, Dictionary<string, object> requestParams = null, bool disableCache = false, float cacheLifetime = 3600f)
- {
- API.SendRequest(target, HTTPMethods.Get, responseContainer, requestParams, true, true, disableCache, cacheLifetime);
- }
- // Token: 0x0600008C RID: 140 RVA: 0x000042B8 File Offset: 0x000024B8
- public static void SendPostRequest(string target, ApiContainer responseContainer = null, Dictionary<string, object> requestParams = null)
- {
- API.SendRequest(target, HTTPMethods.Post, responseContainer, requestParams, true, true, false, 3600f);
- }
- // Token: 0x0600008D RID: 141 RVA: 0x000042D8 File Offset: 0x000024D8
- public static void SendPutRequest(string target, ApiContainer responseContainer = null, Dictionary<string, object> requestParams = null)
- {
- API.SendRequest(target, HTTPMethods.Put, responseContainer, requestParams, true, true, false, 3600f);
- }
- // Token: 0x0600008E RID: 142 RVA: 0x000042F8 File Offset: 0x000024F8
- public static void SendDeleteRequest(string target, ApiContainer responseContainer = null, Dictionary<string, object> requestParams = null)
- {
- API.SendRequest(target, HTTPMethods.Delete, responseContainer, requestParams, true, true, false, 3600f);
- }
- // Token: 0x0600008F RID: 143 RVA: 0x00004318 File Offset: 0x00002518
- public static List<T> ConvertJsonListToModelList<T>(List<object> json, ref string error, float dataTimestamp) where T : ApiModel, new()
- {
- if (json == null)
- {
- return null;
- }
- List<T> result;
- try
- {
- List<T> list = new List<T>();
- foreach (object obj in json)
- {
- Dictionary<string, object> fields = obj as Dictionary<string, object>;
- T item = Activator.CreateInstance<T>();
- if (!item.SetApiFieldsFromJson(fields, ref error))
- {
- return null;
- }
- list.Add(item);
- }
- result = list;
- }
- catch (Exception ex)
- {
- error = "An exception was caught when filling the models: " + ex.Message + "\n" + ex.StackTrace;
- result = null;
- }
- return result;
- }
- // Token: 0x06000090 RID: 144 RVA: 0x00004408 File Offset: 0x00002608
- public static void SendRequest(string endpoint, HTTPMethods method, ApiContainer responseContainer = null, Dictionary<string, object> requestParams = null, bool needsAPIKey = true, bool authenticationRequired = true, bool disableCache = false, float cacheLifetime = 3600f)
- {
- string text = (!disableCache) ? "cyan" : "red";
- Logger.Log(string.Concat(new object[]
- {
- "<color=",
- text,
- ">Dispatch ",
- method,
- " ",
- endpoint,
- (requestParams == null) ? string.Empty : (" params: " + Json.Encode(requestParams)),
- " disableCache: ",
- disableCache.ToString(),
- "</color>"
- }), DebugLevel.API);
- UpdateDelegator.Dispatch(delegate
- {
- API.SendRequestInternal(endpoint, method, responseContainer, requestParams, needsAPIKey, authenticationRequired, disableCache, cacheLifetime);
- });
- }
- // Token: 0x06000091 RID: 145 RVA: 0x00004514 File Offset: 0x00002714
- private static void SendRequestInternal(string endpoint, HTTPMethods method, ApiContainer responseContainer = null, Dictionary<string, object> requestParams = null, bool needsAPIKey = true, bool authenticationRequired = true, bool disableCache = false, float cacheLifetime = 3600f)
- {
- if (responseContainer == null)
- {
- responseContainer = new ApiContainer();
- }
- if (API.API_ONLINE_MODE == ApiOnlineMode.Offline)
- {
- API.SendOfflineRequest(endpoint, method, responseContainer, requestParams);
- return;
- }
- if (API.API_ONLINE_MODE == ApiOnlineMode.Uninitialized)
- {
- Debug.LogError("Api Web Request send before online mode is initialized.");
- }
- string apiUrl = API.GetApiUrl();
- Action action = delegate()
- {
- string uri = apiUrl + endpoint;
- UriBuilder uriBuilder = new UriBuilder(uri);
- if (!string.IsNullOrEmpty(API.ApiKey))
- {
- API.AppendQuery(ref uriBuilder, "apiKey=" + API.ApiKey);
- }
- if (API.API_ORGANIZATION == null)
- {
- throw new Exception("ApiModel does not have it's organization set!");
- }
- API.AppendQuery(ref uriBuilder, "organization=" + API.API_ORGANIZATION);
- string text = null;
- if (requestParams != null)
- {
- if (method == HTTPMethods.Get)
- {
- foreach (KeyValuePair<string, object> keyValuePair in requestParams)
- {
- string str;
- if (keyValuePair.Value is string)
- {
- str = (keyValuePair.Value as string);
- }
- else if (typeof(List<>).IsAssignableFrom(keyValuePair.Value.GetType()))
- {
- str = Json.Encode((keyValuePair.Value as IList).Cast<object>().ToArray<object>());
- }
- else
- {
- str = Json.Encode(keyValuePair.Value);
- }
- API.AppendQuery(ref uriBuilder, keyValuePair.Key + "=" + str);
- }
- }
- else
- {
- text = Json.Encode(requestParams);
- }
- }
- string uriPath = uriBuilder.Uri.PathAndQuery;
- bool useCache = !disableCache && method == HTTPMethods.Get;
- ApiCache.CachedResponse cachedResponse = (!useCache) ? null : ApiCache.GetOrClearCachedResponse(uriBuilder.Uri.PathAndQuery, cacheLifetime);
- if (cachedResponse != null)
- {
- Logger.Log(string.Concat(new object[]
- {
- "<color=cyan>Using cached ",
- method,
- " request to ",
- uriBuilder.Uri,
- "</color>"
- }), DebugLevel.API);
- try
- {
- if (responseContainer.OnComplete(true, uriBuilder.Uri.PathAndQuery, 200, string.Empty, () => cachedResponse.Data, () => cachedResponse.DataAsText, cachedResponse.Timestamp))
- {
- responseContainer.OnSuccess(responseContainer);
- }
- else
- {
- Debug.LogError("Something went wrong re-serving data from cache.");
- }
- }
- catch (Exception exception)
- {
- Debug.LogException(exception);
- }
- }
- else if (method == HTTPMethods.Get && API.activeRequests.ContainsKey(uriPath))
- {
- Logger.Log(string.Concat(new object[]
- {
- "<color=cyan>Piggy-backing ",
- method,
- " request to ",
- uriBuilder.Uri,
- "</color>"
- }), DebugLevel.API);
- OnRequestFinishedDelegate originalCallback = API.activeRequests[uriPath].Callback;
- API.activeRequests[uriPath].Callback = delegate(HTTPRequest req, HTTPResponse resp)
- {
- if (API.activeRequests.ContainsKey(uriPath))
- {
- API.activeRequests.Remove(uriPath);
- }
- if (originalCallback != null)
- {
- originalCallback(req, resp);
- }
- try
- {
- APIResponseHandler.HandleReponse(0, req, resp, responseContainer, 2, useCache);
- }
- catch (Exception exception2)
- {
- Debug.LogException(exception2);
- }
- };
- }
- else
- {
- int requestId = ++API.lastRequestId;
- Logger.Log(string.Concat(new object[]
- {
- "<color=lightblue>[",
- requestId,
- "] Sending ",
- method,
- " request to ",
- uriBuilder.Uri,
- "</color>"
- }), DebugLevel.API);
- HTTPRequest httprequest = new HTTPRequest(uriBuilder.Uri, delegate(HTTPRequest req, HTTPResponse resp)
- {
- if (API.activeRequests.ContainsKey(uriPath))
- {
- API.activeRequests.Remove(uriPath);
- }
- APIResponseHandler.HandleReponse(requestId, req, resp, responseContainer, 2, useCache);
- });
- if (method == HTTPMethods.Get)
- {
- API.activeRequests.Add(uriPath, httprequest);
- }
- httprequest.AddHeader("X-Requested-With", "XMLHttpRequest");
- httprequest.AddHeader("X-MacAddress", API.DeviceID);
- httprequest.AddHeader("Content-Type", (method != HTTPMethods.Get) ? "application/json" : "application/x-www-form-urlencoded");
- httprequest.AddHeader("Origin", "vrchat.com");
- httprequest.MethodType = method;
- httprequest.Credentials = (ApiCredentials.GetWebCredentials() as Credentials);
- if (authenticationRequired && ApiCredentials.GetAuthToken() != null)
- {
- List<Cookie> cookies = httprequest.Cookies;
- cookies.Add(new Cookie("auth", ApiCredentials.GetAuthToken()));
- httprequest.Cookies = cookies;
- }
- httprequest.ConnectTimeout = TimeSpan.FromSeconds(20.0);
- httprequest.Timeout = TimeSpan.FromSeconds(20.0);
- if (!string.IsNullOrEmpty(text))
- {
- httprequest.RawData = Encoding.UTF8.GetBytes(text);
- }
- httprequest.DisableCache = true;
- httprequest.Send();
- }
- string key = endpoint.ToLower().Split(new char[]
- {
- '?'
- })[0];
- if (!API.EndpointAccessTimes.ContainsKey(key))
- {
- API.EndpointAccessTimes.Add(key, new API.EndpointAccessEntry
- {
- count = 1,
- time = Time.realtimeSinceStartup
- });
- }
- else
- {
- API.EndpointAccessTimes[key].time = Time.realtimeSinceStartup;
- API.EndpointAccessTimes[key].count++;
- }
- };
- if (needsAPIKey && !API.IsOffline())
- {
- API.FetchApiKey(action, null);
- }
- else
- {
- action();
- }
- }
- // Token: 0x06000092 RID: 146 RVA: 0x000045EC File Offset: 0x000027EC
- private static void AppendQuery(ref UriBuilder baseUri, string queryToAppend)
- {
- if (baseUri.Query != null && baseUri.Query.Length > 1)
- {
- baseUri.Query = baseUri.Query.Substring(1) + "&" + queryToAppend;
- }
- else
- {
- baseUri.Query = queryToAppend;
- }
- }
- // Token: 0x06000093 RID: 147 RVA: 0x00004644 File Offset: 0x00002844
- private static void FetchApiKey(Action onSuccess = null, Action<string> onError = null)
- {
- Action onInit = delegate()
- {
- if (RemoteConfig.IsInitialized())
- {
- API.ApiKey = RemoteConfig.GetString("clientApiKey");
- if (string.IsNullOrEmpty(API.ApiKey))
- {
- Logger.LogError("Could not fetch client api key - unknown error.", DebugLevel.Always);
- if (onError != null)
- {
- onError("Could not fetch client api key - unknown error.");
- }
- }
- else if (onSuccess != null)
- {
- onSuccess();
- }
- }
- else
- {
- Logger.LogWarning("Could not fetch client api key - config not initialized.", DebugLevel.Always);
- if (onError != null)
- {
- onError("Could not fetch client api key - config not initialized.");
- }
- }
- };
- if (string.IsNullOrEmpty(API.ApiKey))
- {
- RemoteConfig.Init(onInit, null);
- }
- else if (onSuccess != null)
- {
- onSuccess();
- }
- }
- // Token: 0x06000094 RID: 148 RVA: 0x000046A4 File Offset: 0x000028A4
- public static void SetOnlineMode(bool online, string organization = null)
- {
- if (!online)
- {
- API.API_ONLINE_MODE = ApiOnlineMode.Offline;
- string text = Resources.Load<TextAsset>("offline").text;
- object obj = Json.Decode(text);
- API.offlineQueries = (obj as List<object>);
- }
- else
- {
- API.API_ONLINE_MODE = ApiOnlineMode.Online;
- }
- if (!string.IsNullOrEmpty(organization))
- {
- API.SetOrganization(organization);
- }
- }
- // Token: 0x06000095 RID: 149 RVA: 0x000046FC File Offset: 0x000028FC
- public static bool IsOffline()
- {
- return API.API_ONLINE_MODE == ApiOnlineMode.Offline;
- }
- // Token: 0x06000096 RID: 150 RVA: 0x00004708 File Offset: 0x00002908
- public static ApiServerEnvironment GetServerEnvironmentForApiUrl()
- {
- return API.GetServerEnvironmentForApiUrl(API.API_URL);
- }
- // Token: 0x06000097 RID: 151 RVA: 0x00004714 File Offset: 0x00002914
- public static ApiServerEnvironment GetServerEnvironmentForApiUrl(string url)
- {
- if (API.GetApiUrl() == "https://api.vrchat.cloud/api/1/")
- {
- return ApiServerEnvironment.Release;
- }
- if (API.GetApiUrl() == "https://beta-api.vrchat.cloud/api/1/")
- {
- return ApiServerEnvironment.Beta;
- }
- if (API.GetApiUrl() == "https://dev-api.vrchat.cloud/api/1/")
- {
- return ApiServerEnvironment.Dev;
- }
- Debug.LogError("GetServerEnvironmentForApiUrl: unknown api url: " + url);
- return ApiServerEnvironment.Release;
- }
- // Token: 0x06000098 RID: 152 RVA: 0x00004774 File Offset: 0x00002974
- private static void SendOfflineRequest(string endpoint, HTTPMethods method, ApiContainer responseContainer = null, Dictionary<string, object> requestParams = null)
- {
- foreach (object obj in API.offlineQueries)
- {
- Dictionary<string, object> dictionary = obj as Dictionary<string, object>;
- if (dictionary["url"].ToString() == endpoint)
- {
- object result = dictionary["result"];
- string s = Json.Encode(result);
- byte[] data = Encoding.UTF8.GetBytes(s);
- responseContainer.OnComplete(true, endpoint, 200, string.Empty, () => data, () => Json.Encode(result), -1f);
- if (!responseContainer.IsValid)
- {
- if (responseContainer.OnError != null)
- {
- responseContainer.OnError(responseContainer);
- }
- }
- else if (responseContainer.OnSuccess != null)
- {
- responseContainer.OnSuccess(responseContainer);
- }
- }
- }
- Debug.LogError("Query used by application in offline mode not found - " + endpoint);
- responseContainer.Error = "query not found in offline results - " + endpoint;
- if (responseContainer.OnError != null)
- {
- responseContainer.OnError(responseContainer);
- }
- }
- // Token: 0x04000043 RID: 67
- public const string devApiUrl = "https://dev-api.vrchat.cloud/api/1/";
- // Token: 0x04000044 RID: 68
- public const string betaApiUrl = "https://beta-api.vrchat.cloud/api/1/";
- // Token: 0x04000045 RID: 69
- public const string releaseApiUrl = "https://api.vrchat.cloud/api/1/";
- // Token: 0x04000046 RID: 70
- public const float ResponseCacheLifetime = 3600f;
- // Token: 0x04000047 RID: 71
- public const int MAX_RETRY_COUNT = 2;
- // Token: 0x04000048 RID: 72
- public static string API_URL = "https://api.vrchat.cloud/api/1/";
- // Token: 0x04000049 RID: 73
- public static string API_ORGANIZATION = "vrchat";
- // Token: 0x0400004A RID: 74
- public static ApiOnlineMode API_ONLINE_MODE = ApiOnlineMode.Uninitialized;
- // Token: 0x0400004B RID: 75
- public static string ApiKey;
- // Token: 0x0400004C RID: 76
- public static Dictionary<string, API.EndpointAccessEntry> EndpointAccessTimes = new Dictionary<string, API.EndpointAccessEntry>();
- // Token: 0x0400004D RID: 77
- private static int lastRequestId = -1;
- // Token: 0x0400004E RID: 78
- private static Dictionary<string, HTTPRequest> activeRequests = new Dictionary<string, HTTPRequest>();
- // Token: 0x0400004F RID: 79
- private static List<object> offlineQueries;
- // Token: 0x02000016 RID: 22
- public class EndpointAccessEntry
- {
- // Token: 0x04000050 RID: 80
- public float time;
- // Token: 0x04000051 RID: 81
- public int count;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement