public void uploadTexture(Texture2D tex, Action <object, Callback> action) { string b64string = System.Convert.ToBase64String(tex.EncodeToPNG()); Callback callback = callbackPool.getCallback(CallbackType.DISPOSABLE); callback.action = action; string eval = @" var data = new FormData(); data.append('imgdata','BASE64_STRING') $.ajax({ url : 'saveimage.php', data : data, cache : false, contentType : false, processData : false, type : 'POST', success : function(data){ var url = window.location.href; var fullPath=url.substring(0, url.lastIndexOf('/') + 1)+data; callback(CALLBACK_ID, fullPath); } }); " .Replace("BASE64_STRING", b64string) .Replace("CALLBACK_ID", "" + callback.id); Debug2.LogDebug("uploadTexture external evaluation: \n" + eval); Application.ExternalEval(eval); }
void setResolutionToActualSizeAndSendEvent() { Debug2.LogDebug("setResolutionToActualSizeAndSendEvent current resolution "+ Screen.currentResolution); lastWidth = Screen.width; lastHeight = Screen.height; lastFullScreen = Screen.fullScreen; if (Screen.width < PropertiesSingleton.instance.screen.minWidth || Screen.height < PropertiesSingleton.instance.screen.minHeight) { scaleScreenAndSendEvent(); } else { IntVector2 resolution; if (Screen.fullScreen) { Resolution maxRes = Screen.resolutions[Screen.resolutions.Length - 1]; Screen.SetResolution(maxRes.width, maxRes.height, true); Debug2.LogDebug("trying to change to full screen"); resolution = new IntVector2(maxRes.width, maxRes.height); } else { Screen.SetResolution(Screen.width, Screen.height, false); resolution = new IntVector2(Screen.width, Screen.height); } sendEventOnResolutionChange(resolution, new Vector2(1.0f, 1.0f)); } }
public void onPictureSave(Texture2D texture, string pictureName) { vkc.api("photos.getAlbums", new Dictionary <string, object>(), delegate(object arg1, Callback arg2) { Debug2.LogDebug("im at takeScreenshot=====\n" + Json.Serialize(arg1)); Dictionary <string, object> resultDict = arg1 as Dictionary <string, object>; if (!resultDict.ContainsKey("response")) { Debug2.LogError("vk api error \n" + Json.Serialize(arg1)); return; } Dictionary <string, object> response = resultDict["response"] as Dictionary <string, object>; List <object> items = response["items"] as List <object>; long albumId = -1; for (int i = 0; i < items.Count; i++) { Dictionary <string, object> album = items[i] as Dictionary <string, object>; if (((string)album["title"]).Equals(albumName)) { albumId = (long)album["id"]; break; } } string wallText = wallMessage.Replace("PICTURE_NAME", pictureName); if (albumId == -1) { createAlbumAndPostScreenShot(texture, wallText); } else { postScreenShot(texture, wallText, albumId); } }); }
public void initializeVKApi(string key = "") { if (!String.IsNullOrEmpty(key)) { privateKey = key; } if (initialized) { Debug2.LogWarning("vk api already initialized, init method will be ignored"); } else { initialized = true; callbackPool = CallbackPool.instance; callbackPool.initialize(); initVKApi(privateKey, delegate(object obj, Callback callback){ inputData = HTTPUtility.ParseQueryString((string)obj); Debug2.LogDebug("viewer id =" + inputData["viewer_id"]); callbackPool.releaseDisposableCallback(callbackOnInitError); if (onApiReady != null) { onApiReady(true); } Debug2.LogDebug("VK api is ready"); }, delegate(object obj, Callback callback){ onApiReady(false); callbackPool.releaseDisposableCallback(callBackOnInitDone); Debug2.LogError("problem with vk initialization\n" + Json.Serialize(obj)); }); } }
public void callMethod(string methodName, params object[] parameters) { string jsParams = ""; for (int i = 0; i < parameters.Length; i++) { if (parameters[i] is string) { jsParams += ", \"" + (string)parameters[i] + "\""; } else if (parameters[i] is bool) { jsParams += ", " + ((bool)parameters[i]).ToString().ToLower(); } else if (parameters[i] is int) { jsParams += ", " + parameters[i]; } } string eval = @" VK.callMethod('METHOD_NAME'PARAMETERS); " .Replace("METHOD_NAME", methodName) .Replace("PARAMETERS", jsParams); Debug2.LogDebug("callMethod external evaluation \n" + eval); Application.ExternalEval(eval); }
void takeScreenshot() { vkc.api("photos.getAlbums", new Dictionary <string, object>(), delegate(object arg1, Callback arg2) { Debug2.LogDebug("im at takeScreenshot=====\n" + Json.Serialize(arg1)); Dictionary <string, object> resultDict = arg1 as Dictionary <string, object>; if (!resultDict.ContainsKey("response")) { Debug2.LogError("vk api error \n" + Json.Serialize(arg1)); return; } Dictionary <string, object> response = resultDict["response"] as Dictionary <string, object>; List <object> items = response["items"] as List <object>; long albumId = -1; for (int i = 0; i < items.Count; i++) { Dictionary <string, object> album = items[i] as Dictionary <string, object>; if (((string)album["title"]).Equals(albumName)) { albumId = (long)album["id"]; break; } } if (albumId == -1) { createAlbumAndPostScreenShot(); } else { postScreenShot(albumId); } }); }
public void callbackHandler(string resultString) { Debug2.LogDebug("callbackHandler fired with resutlString: \n" + resultString); Dictionary <string, object> resultObj = Json.Deserialize(resultString) as Dictionary <string, object>; long callbackId = (long)resultObj["id"]; Debug2.LogDebug("callbackId=" + callbackId); object result = resultObj.ContainsKey("object") ? resultObj["object"] : null; Callback callback = callbackDict[callbackId]; if (callback.action != null) { callback.action(result, callback); } else { Debug2.LogError("callback " + callbackId + " is empty"); } if (callback.type == CallbackType.DISPOSABLE) { disposableCallbacks.Remove(callback); enqueCallback(callback); } }
public void reset() { Debug2.LogDebug("reset callback id=" + id); type = CallbackType.UNKNOWN; mailruEventId = UNSET_HTML_EVENT_ID; action = null; }
public void callMailruByCallbackAndParams(string functionName, Action <object, Callback> action, params object[] parameters) { Callback callback = callbackPool.getCallback(CallbackType.DISPOSABLE); callback.action = action; string initialization = ""; string functionParams = ""; for (int i = 0; i < parameters.Length; i++) { initialization += @" paramNUM=PARAMETER_JSON; " .Replace("NUM", "" + i) .Replace("PARAMETER_JSON", Json.Serialize(parameters[i])); functionParams += ", paramNUM".Replace("NUM", "" + i); } string eval = @" INITIALIZATION function _callbackCALLBACK_ID(result){ callback(CALLBACK_ID, result); } FUNCTION_NAME(_callbackCALLBACK_ID ADDITIONAL_PARAMETERS); " .Replace("INITIALIZATION", initialization) .Replace("CALLBACK_ID", "" + callback.id) .Replace("FUNCTION_NAME", functionName) .Replace("ADDITIONAL_PARAMETERS", functionParams); Debug2.LogDebug("callMailruByCallbackAndParams external evaluation: \n" + eval); Application.ExternalEval(eval); }
void createAlbumAndSavePictureOnSucess(string pictureName, Texture2D texture) { Dictionary <string, object> paramsObject = new Dictionary <string, object>(); paramsObject.Add("name", albumName); MRUController.instance.callMailruByObjectMailruListenerAndCallback( "mailru.common.photos.createAlbum", paramsObject, "mailru.common.events.createAlbum", delegate(object obj, Callback callback){ Dictionary <string, object> result = obj as Dictionary <string, object>; string status = (string)result["status"]; if (status.Equals("closed")) { Debug2.LogDebug("album window has closed without creation"); CallbackPool.instance.releasePermanentCallback(callback); } else if (status.Equals("createSuccess")) { string aid = (string)result["aid"]; Debug2.LogDebug("album created id = " + aid); CallbackPool.instance.releasePermanentCallback(callback); savePicture(aid, pictureName, texture); } }); }
public void onNewPictureOpen(SheetObject sheetObject) { string text = "Colorus | " + sheetObject.nameKey.Localized(); string eval = "document.title=\"TEXT\";".Replace("TEXT", text); Debug2.LogDebug(" onNewPictureOpen evaluation eval=\n" + eval); Application.ExternalEval(eval); }
void sendEventOnResolutionChange(IntVector2 resolution, Vector2 scale) { Debug2.LogDebug("sendEventOnResolutionChange resolution = " + resolution.ToString() + " scale =" + scale); if (onResolutionChange != null) { onResolutionChange(resolution, scale); } }
public void setMailruEventId(string parameters) { Debug2.LogDebug("setMailruEventId params=" + parameters); Dictionary <string, object> result = Json.Deserialize(parameters) as Dictionary <string, object>; long callbackId = (long)result["callbackId"]; long mailruEventId = (long)result["mailruEventId"]; callbackDict[callbackId].mailruEventId = mailruEventId; }
void scaleScreenAndSendEvent() { Debug2.LogDebug("scaleScreenAndSendEvent "); float ratio; Resolution maxRes = Screen.resolutions[Screen.resolutions.Length - 1]; if (Screen.fullScreen) { ratio = (float)maxRes.width / (float)maxRes.height; } else { ratio = (float)Screen.width / (float)Screen.height; } float normalRatio = PropertiesSingleton.instance.screen.normalAspect; IntVector2 newResolution; if (ratio < normalRatio) { float newWidth = ratio * (float)PropertiesSingleton.instance.screen.minHeight; float newHeight = PropertiesSingleton.instance.screen.minHeight; if (newWidth < PropertiesSingleton.instance.screen.minWidth) { newWidth = PropertiesSingleton.instance.screen.minWidth; newHeight = PropertiesSingleton.instance.screen.minWidth / ratio; } newResolution = new IntVector2(newWidth, newHeight); } else { float newWidth = PropertiesSingleton.instance.screen.minWidth; float newHeight = PropertiesSingleton.instance.screen.minWidth / ratio; if (newHeight < PropertiesSingleton.instance.screen.minHeight) { newHeight = PropertiesSingleton.instance.screen.minHeight; newWidth = newHeight * ratio; } newResolution = new IntVector2(newWidth, newHeight); } Vector2 scale; if (Screen.fullScreen) { scale = new Vector2((float)maxRes.width / (float)newResolution.x, (float)maxRes.height / (float)newResolution.y); } else { scale = new Vector2((float)Screen.width / (float)newResolution.x, (float)Screen.height / (float)newResolution.y); } Screen.SetResolution(newResolution.x, newResolution.y, Screen.fullScreen); sendEventOnResolutionChange(newResolution, scale); }
public void initialize() { Debug2.LogDebug("initializing CallbackPool"); if (initialized) { return; } initialized = true; initHtmlJS(); initCallbackQueue(); }
public void removeTextureFromServer(string imgFullPath) { string eval = @" var img='IMG_FULL_PATH'; var fileName=img.substring(img.lastIndexOf('/')+1); $.get( 'removeimage.php', { imageName: fileName} ); " .Replace("IMG_FULL_PATH", imgFullPath); Debug2.LogDebug("removeTextureFromServer external evaluation: \n" + eval); Application.ExternalEval(eval); }
void initHtmlJS() { string commandStringify = @" JSON.stringify = JSON.stringify || function (obj) { var t = typeof (obj); if (t != ""object"" || obj === null) { // simple data type if (t == ""string"") obj = '""'+obj+'""'; return String(obj); } else { // recurse array or object var n, v, json = [], arr = (obj && obj.constructor == Array); for (n in obj) { v = obj[n]; t = typeof(v); if (t == ""string"") v = '""'+v+'""'; else if (t == ""object"" && v !== null) v = JSON.stringify(v); json.push((arr ? """" : '""' + n + '"":') + String(v)); } return (arr ? ""["" : ""{"") + String(json) + (arr ? ""]"" : ""}""); } }; " ; string commandCallback = @" function callback(id, obj){ var result=new Object(); result.id=id; result.object=obj; var resultString=JSON.stringify(result); u.getUnity().SendMessage('OBJECT_NAME','callbackHandler',resultString); }; " .Replace("OBJECT_NAME", gameObject.name); string commandUpdateCallbackId = @" function updateCallbackId(callbackId, mailruEventId){ var result=new Object(); result.callbackId=callbackId; result.mailruEventId=mailruEventId; var resultString=JSON.stringify(result); console.log('sending ids to callback'+callbackId+' mruId'+mailruEventId); u.getUnity().SendMessage('OBJECT_NAME','setMailruEventId',resultString); }; " .Replace("OBJECT_NAME", gameObject.name); Debug2.LogDebug("1"); Application.ExternalEval(commandStringify); Debug2.LogDebug("2"); Application.ExternalEval(commandCallback); Debug2.LogDebug("3"); Application.ExternalEval(commandUpdateCallbackId); Debug2.LogDebug("4"); }
public void callMailruByParams(string functionName, params string[] parameters) { string functionParmas = string.Join("', '", parameters); functionParmas = (functionParmas.Length > 0) ? "'" + functionParmas + "'" : ""; string eval = @" FUNCTION_NAME(PARAMETERS); " .Replace("FUNCTION_NAME", functionName) .Replace("PARAMETERS", functionParmas); Debug2.LogDebug("callMailruByParams external evaluation: \n" + eval); Application.ExternalEval(eval); }
public void initializeMailRuApi() { callbackPool = CallbackPool.instance; callbackPool.initialize(); initMailruApi(privateKey, delegate(object obj, Callback callback){ if (onApiReady != null) { onApiReady(obj); } mailruSession = obj as Dictionary <string, object>; Debug2.LogDebug("mail ru api ready"); }); }
public void windowConfirm(string text, Action <object, Callback> onDone) { Callback callback = callbackPool.getCallback(CallbackType.DISPOSABLE); callback.action = onDone; string eval = @" var result=confirm('TEXT'); callback(CALLBACK_ID, result); " .Replace("TEXT", text.Replace("'", "\"")).Replace("CALLBACK_ID", "" + callback.id); Debug2.LogDebug("windowConfirm external evaluation:\n " + eval); Application.ExternalEval(eval); }
IEnumerator takeScreenShot() { yield return(new WaitForEndOfFrame()); Texture2D texture = getScreenTexture(); #if !UNITY_WEBPLAYER System.IO.File.WriteAllBytes("./123321test.png", texture.EncodeToPNG()); #endif MRUController.instance.uploadTexture(texture, delegate(object incomeObj, Callback callback){ Dictionary <string, object> paramObj = new Dictionary <string, object>() { { "url", (string)incomeObj }, { "aid", 2 }, { "set_as_cover", false }, { "name", "testupload picture" }, { "tags", "unity3d, js, png" }, { "theme", "6" } }; MRUController.instance.callMailruByObjectMailruListenerAndCallback( "mailru.common.photos.upload", paramObj, "mailru.common.events.upload", delegate(object result, Callback mruCallback){ Dictionary <string, object> resultObj = result as Dictionary <string, object>; string status = (string)resultObj["status"]; if (status.Equals("uploadSuccess")) { Debug2.LogDebug("status ok"); string imgFullPath = (string)(resultObj["originalProps"] as Dictionary <string, object>)["url"]; MRUController.instance.removeTextureFromServer(imgFullPath); CallbackPool.instance.releasePermanentCallback(mruCallback); } else if (status.Equals("closed")) { Debug2.LogDebug("user closed window"); string imgFullPath = (string)(resultObj["originalProps"] as Dictionary <string, object>)["url"]; MRUController.instance.removeTextureFromServer(imgFullPath); CallbackPool.instance.releasePermanentCallback(mruCallback); } else if (!status.Equals("opened")) { Debug2.LogError("unkonwn status +" + status); } Debug2.LogDebug("current status =" + status); } ); }); }
public static void initMailruApi(string privateKey, Action <object, Callback> onInitDone) { Callback c = CallbackPool.instance.getCallback(CallbackType.DISPOSABLE); c.action = onInitDone; string eval = @" mailru.loader.require('api', function() { mailru.app.init('PRIVATE_KEY'); callback(CALLBACK_ID,mailru.session); }); " .Replace("PRIVATE_KEY", privateKey).Replace("CALLBACK_ID", "" + c.id); Debug2.LogDebug("initMailruApi external evaluation: \n" + eval); Application.ExternalEval(eval); }
void getAlbums() { MRUController.instance.callMailruByCallback("mailru.common.photos.getAlbums", delegate(object result, Callback callback) { Debug2.LogDebug("getAlbums resul:\n" + Json.Serialize(result)); List <object> albums = result as List <object>; albumsMetadata = new List <AlbumMetadata>(); foreach (object item in albums) { var album = item as Dictionary <string, object>; albumsMetadata.Add(new AlbumMetadata((string)album["title"], (string)album["cover_url"])); } downloadPictures(); }); }
public void callMailruByCallback(string functionName, Action <object, Callback> action) { Callback callback = callbackPool.getCallback(CallbackType.DISPOSABLE); callback.action = action; string eval = @" function _callbackCALLBACK_ID(result){ callback(CALLBACK_ID, result); } FUNCTION_NAME(_callbackCALLBACK_ID); " .Replace("FUNCTION_NAME", functionName) .Replace("CALLBACK_ID", "" + callback.id); Debug2.LogDebug("callMailruByCallback external evaluation: \n" + eval); Application.ExternalEval(eval); }
void createAlbumAndPostScreenShot() { Dictionary <string, object> parameters = new Dictionary <string, object>(); parameters["title"] = albumName; vkc.api("photos.createAlbum", parameters, delegate(object arg1, Callback arg2) { Debug2.LogDebug("im at createAlbumAndPostScreenShot=====\n" + Json.Serialize(arg1)); Dictionary <string, object> resultDict = arg1 as Dictionary <string, object>; if (!resultDict.ContainsKey("response")) { Debug2.LogError("vk api error \n" + Json.Serialize(arg1)); return; } Dictionary <string, object> response = resultDict["response"] as Dictionary <string, object>; long aid = (long)response["id"]; postScreenShot(aid); }); }
public void api(string methodName, object paramObject, Action <object, Callback> callback) { Callback c = callbackPool.getCallback(CallbackType.DISPOSABLE); c.action = callback; string eval = @" function _callbackCALLBACK_ID(result){ callback(CALLBACK_ID, result); } var params=PARAMS_JSON_STRING; VK.api(""METHOD_NAME"",params,_callbackCALLBACK_ID); " .Replace("CALLBACK_ID", "" + c.id) .Replace("METHOD_NAME", methodName) .Replace("PARAMS_JSON_STRING", Json.Serialize(paramObject)); Debug2.LogDebug("vk.api external evaluation: \n" + eval); Application.ExternalEval(eval); }
void savePicture(string aid, string pictureName, Texture2D texture) { MRUController.instance.uploadTexture(texture, delegate(object incomeObj, Callback callback){ Dictionary <string, object> paramObj = new Dictionary <string, object>() { { "url", (string)incomeObj }, { "aid", aid }, { "set_as_cover", false }, { "name", pictureName }, { "tags", "colorus, раскраска" }, { "theme", "14" } }; MRUController.instance.callMailruByObjectMailruListenerAndCallback( "mailru.common.photos.upload", paramObj, "mailru.common.events.upload", delegate(object result, Callback mruCallback){ Dictionary <string, object> resultObj = result as Dictionary <string, object>; string status = (string)resultObj["status"]; if (status.Equals("uploadSuccess")) { Debug2.LogDebug("status ok"); string imgFullPath = (string)(resultObj["originalProps"] as Dictionary <string, object>)["url"]; MRUController.instance.removeTextureFromServer(imgFullPath); CallbackPool.instance.releasePermanentCallback(mruCallback); } else if (status.Equals("closed")) { Debug2.LogDebug("user closed window"); string imgFullPath = (string)(resultObj["originalProps"] as Dictionary <string, object>)["url"]; MRUController.instance.removeTextureFromServer(imgFullPath); CallbackPool.instance.releasePermanentCallback(mruCallback); } else if (!status.Equals("opened")) { Debug2.LogError("unkonwn status +" + status); } Debug2.LogDebug("current status =" + status); } ); }); }
void initVKApi(string key, Action <object, Callback> onInitDone, Action <object, Callback> onInitError) { callBackOnInitDone = callbackPool.getCallback(CallbackType.DISPOSABLE); callBackOnInitDone.action = onInitDone; callbackOnInitError = callbackPool.getCallback(CallbackType.DISPOSABLE); callbackOnInitError.action = onInitError; string eval = @" function _callbackCALLBACK_ID(result){ result=location.search; callback(CALLBACK_ID, result); } VK.init(function() { _callbackCALLBACK_ID(); }, function() { callback(CALLBACK_ON_ERROR_ID); }, '5.1'); " .Replace("CALLBACK_ID", "" + callBackOnInitDone.id) .Replace("CALLBACK_ON_ERROR_ID", "" + callbackOnInitError.id); Debug2.LogDebug("initVKApi external evaluation: \n" + eval); Application.ExternalEval(eval); }
void createNewAblum() { Dictionary <string, object> paramsObject = new Dictionary <string, object>(); paramsObject.Add("name", text); MRUController.instance.callMailruByObjectMailruListenerAndCallback( "mailru.common.photos.createAlbum", paramsObject, "mailru.common.events.createAlbum", delegate(object obj, Callback callback){ Dictionary <string, object> result = obj as Dictionary <string, object>; string status = (string)result["status"]; if (status.Equals("opened")) { Debug2.LogDebug("album window has opened"); } else if (status.Equals("closed")) { Debug2.LogDebug("album window has closed without creation. Need to unsubscribe from event"); CallbackPool.instance.releasePermanentCallback(callback); } else if (status.Equals("createSuccess")) { Debug2.LogDebug("album created id = " + (string)result["aid"] + " Need to unsubscribe from event"); CallbackPool.instance.releasePermanentCallback(callback); if (albumsMetadata != null) { getAlbums(); } } else { Debug2.LogWarning("unknown status [" + status + "]"); } }); }
public void releasePermanentCallback(Callback callback) { Debug2.LogDebug("releasing callback id=" + callback.id + " mruListenerId=" + callback.mailruEventId); if (permanentCallback.Contains(callback)) { if (callback.mailruEventId != Callback.UNSET_HTML_EVENT_ID) { string eval = "mailru.events.remove(CALLBACK_ID);" .Replace("CALLBACK_ID", "" + callback.mailruEventId); Debug2.LogDebug("releasing callback js:\n" + eval); Application.ExternalEval(eval); } else { Debug2.LogError("something wrong callback id=" + callback.id + " permanent but doesnt have htmlEventId to unsubscribe it from mail ru events"); } permanentCallback.Remove(callback); enqueCallback(callback); } else { Debug2.LogError("permanent callback doesn't contain callback with id= " + callback.id); } }