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)); }); } }
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); } }); }
void onWebContextReadyListener(object obj, Callback cb) { Debug2.Log("im here from callback"); string pageName = (string)obj; initData = ""; if (pageName.StartsWith("mru")) { webType = WebType.MAIL_RU; initData = mailRuKey; } else if (pageName.StartsWith("vk")) { webType = WebType.VK; initData = vkKey; } else if (pageName.StartsWith("fb")) { webType = WebType.FB; } else if (pageName.StartsWith("online")) { webType = WebType.ONLINE; } else { Debug2.LogError("unknown page"); } ready = true; activeStrategy = supportedWeb[webType]; activeStrategy.onStart(initData); }
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 Callback getCallback(CallbackType callbackType) { Callback callback; if (callbackQueue.Count > 0) { callback = callbackQueue.Dequeue(); callback.reset(); } else { callback = createNewCallback(); } callback.type = callbackType; switch (callbackType) { case CallbackType.DISPOSABLE: disposableCallbacks.Add(callback); break; case CallbackType.PERMANENT: permanentCallback.Add(callback); break; default: Debug2.LogError("Wrong callbackType" + callbackType.ToString()); break; } return(callback); }
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 static void Remove(ITickable tickable) { if (Tasks.Contains(tickable)) { Tasks.Remove(tickable); } else if (FixedTasks.Contains(tickable)) { FixedTasks.Remove(tickable); } else { Debug2.LogError("Itickable kaldırılamadı"); } }
public void onStart(string key) { vkc = VKController.instance; vkc.initializeVKApi(key); vkc.onApiReady += delegate(bool status){ if (status) { ready = true; } else { Debug2.LogError("can't initialize api"); } }; }
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); } ); }); }
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); }); }
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 initCallbackQueue() { if (initPoolSize > 0) { callbackDict = new Dictionary <long, Callback>(); callbackQueue = new Queue <Callback>(); disposableCallbacks = new List <Callback>(); permanentCallback = new List <Callback>(); currentPoolSize = 0; int i = initPoolSize; while (i-- > 0) { Callback c = createNewCallback(); callbackQueue.Enqueue(c); } } else { Debug2.LogError("pool size must be greater than 0"); } }
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); } }
void postScreenShot(long albumId) { StartCoroutine(getScreenShotFromScreen(delegate(Texture2D tex) { Dictionary <string, object> getUploadServParams = new Dictionary <string, object>(); getUploadServParams["album_id"] = (long)albumId; vkc.api("photos.getUploadServer", getUploadServParams, delegate(object arg1, Callback arg2){ Debug2.LogDebug("im at postScreenShot getWallUploadServer=====\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>; string upload_url = (string)response["upload_url"]; WWWForm form = new WWWForm(); form.AddBinaryData("photo", tex.EncodeToPNG(), "PHOTO_NAME.png", "image/png"); StartCoroutine(CallbackOnWWWResponse(upload_url, form, delegate(string postResult){ Debug2.LogDebug("post success \n" + postResult); Dictionary <string, object> postResultDict = Json.Deserialize(postResult) as Dictionary <string, object>; Dictionary <string, object> parameters = new Dictionary <string, object>(); parameters["album_id"] = albumId; parameters["server"] = postResultDict["server"]; parameters["photos_list"] = postResultDict["photos_list"]; parameters["hash"] = postResultDict["hash"]; parameters["caption"] = "test upload to vk server"; vkc.api("photos.save", parameters, delegate(object psarg1, Callback psarg2) { Debug2.LogDebug("im at postScreenShot getWallUploadServer=====\n" + Json.Serialize(psarg1)); Dictionary <string, object> psresultDict = psarg1 as Dictionary <string, object>; if (!psresultDict.ContainsKey("response")) { Debug2.LogError("vk api error \n" + Json.Serialize(psarg1)); } else { Debug2.LogDebug("photos post success \n" + Json.Serialize(psarg1)); long photoId = (long)((psresultDict["response"] as List <object>)[0] as Dictionary <string, object>)["id"]; vkc.windowConfirm("ваша картинка успешно сохранена\\n" + "в альбоме \"" + albumName + "\"\\n" + "хотите добавить ее на стену?", delegate(object confirmObject, Callback confirmCallback) { bool result = (bool)confirmObject; Debug2.LogDebug("confirmation result = " + result); if (result) { Dictionary <string, object> wallProps = new Dictionary <string, object>(); wallProps["owner_id"] = vkc.inputData["viewer_id"]; wallProps["message"] = "тестовая запись на стену"; wallProps["attachments"] = "photo" + vkc.inputData["viewer_id"] + "_" + photoId; vkc.api("wall.post", wallProps, delegate(object wallPostObj, Callback wallPostCallback) { if (!(wallPostObj as Dictionary <string, object>).ContainsKey("response")) { Debug2.LogError("problem with wall.post upload " + Json.Serialize(wallPostObj)); } }); } }); } }); }, delegate(string postError){ Debug2.LogError("problem with post photo\n" + postError); } )); }); })); }
void postScreenShot(Texture2D tex, string wallText, long albumId) { Dictionary <string, object> getUploadServParams = new Dictionary <string, object>(); getUploadServParams["album_id"] = (long)albumId; vkc.api("photos.getUploadServer", getUploadServParams, delegate(object arg1, Callback arg2){ Debug2.LogDebug("im at postScreenShot getWallUploadServer=====\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>; string upload_url = (string)response["upload_url"]; vkc.uploadTexture(tex, upload_url, "colorus", delegate(string postResult){ Debug2.LogDebug("post success \n" + postResult); Dictionary <string, object> postResultDict = Json.Deserialize(postResult) as Dictionary <string, object>; Dictionary <string, object> parameters = new Dictionary <string, object>(); parameters["album_id"] = albumId; parameters["server"] = postResultDict["server"]; parameters["photos_list"] = postResultDict["photos_list"]; parameters["hash"] = postResultDict["hash"]; parameters["caption"] = "test upload to vk server"; vkc.api("photos.save", parameters, delegate(object psarg1, Callback psarg2) { Debug2.LogDebug("im at postScreenShot getWallUploadServer=====\n" + Json.Serialize(psarg1)); Dictionary <string, object> psresultDict = psarg1 as Dictionary <string, object>; if (!psresultDict.ContainsKey("response")) { Debug2.LogError("vk api error \n" + Json.Serialize(psarg1)); } else { Debug2.LogDebug("photos post success \n" + Json.Serialize(psarg1)); long photoId = (long)((psresultDict["response"] as List <object>)[0] as Dictionary <string, object>)["id"]; vkc.windowConfirm("ваша картинка успешно сохранена\\n" + "в альбоме \"" + albumName + "\"\\n" + "хотите добавить ее на стену?", delegate(object confirmObject, Callback confirmCallback) { bool result = (bool)confirmObject; Debug2.LogDebug("confirmation result = " + result); if (result) { WebContext.instance.hideApplication(); Dictionary <string, object> wallProps = new Dictionary <string, object>(); wallProps["owner_id"] = vkc.inputData["viewer_id"]; wallProps["message"] = wallText; wallProps["attachments"] = "photo" + vkc.inputData["viewer_id"] + "_" + photoId; vkc.api("wall.post", wallProps, delegate(object wallPostObj, Callback wallPostCallback) { if (!(wallPostObj as Dictionary <string, object>).ContainsKey("response")) { Debug2.LogError("problem with wall.post upload " + Json.Serialize(wallPostObj)); } WebContext.instance.showApplication(); }); } }); } }); }, delegate(string postError){ Debug2.LogError("problem with post photo\n" + postError); } ); }); }