void callApiAsync(String targentStep) { if (AppStoreOnboardApi.tokenInfo.access_token == null) { UnityOAuth.GetAuthorizationCodeAsync(AppStoreOnboardApi.oauthClientId, (response) => { if (response.AuthCode != null) { string authcode = response.AuthCode; UnityWebRequest request = AppStoreOnboardApi.GetAccessToken(authcode); TokenInfo tokenInfoResp = new TokenInfo(); ReqStruct reqStruct = new ReqStruct(); reqStruct.request = request; reqStruct.resp = tokenInfoResp; reqStruct.targetStep = targentStep; requestQueue.Enqueue(reqStruct); } else { Debug.Log("Failed: " + response.Exception.ToString()); isOperationRunning = false; } }); } else { UnityWebRequest request = AppStoreOnboardApi.GetUserId(); UserIdResponse userIdResp = new UserIdResponse(); ReqStruct reqStruct = new ReqStruct(); reqStruct.request = request; reqStruct.resp = userIdResp; reqStruct.targetStep = targentStep; requestQueue.Enqueue(reqStruct); } }
private static ReqStruct GetReq(ref Socket sock) { //Dim sock As Socket = serv.Accept() byte[] a = new byte[sock.ReceiveBufferSize]; int cnt = sock.Receive(a, sock.ReceiveBufferSize, SocketFlags.None); Array.Resize(ref a, cnt); string data = Encoding.GetEncoding("iso-8859-1").GetString(a); ReqStruct ret = new ReqStruct(); ret.req = new Request(data); ret.sock = sock; return(ret); }
private void asyncReq(Socket sk) { string ansStr = null; byte[] ans = { }; ReqStruct reqst = default(ReqStruct); byte[] data = new byte[512]; try { reqst = GetReq(ref sk); } catch (Exception ex) { BGW_GTS.ReportProgress(1, "Got wrong request, probably not from a DS. Error was : " + ex.Message); return; } Socket sock = reqst.sock; Request req = reqst.req; string pid = req.getvars["pid"]; byte[] bin = null; if (!CB_GTS_SendPKMN.Checked) { currentDataFileName = ""; } else { } //bin = GetBin(currentDataFileName); if (req.getvars.Count == 1) { ansStr = RandStr(32); } else { byte[] encrypted_data = System.Convert.FromBase64String(req.getvars["data"].Replace("-", "+").Replace("_", "/")); data = Decrypt_data(encrypted_data); data = data.ToList().GetRange(4, data.Length - 4).ToArray(); switch (req.action) { case "info": BGW_GTS.ReportProgress(1, sk.RemoteEndPoint.ToString() + " has entered the GTS."); ans = new byte[] { 1, 0 }; break; case "setProfile": ans = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }; BGW_GTS.ReportProgress(1, "Profile from " + sk.RemoteEndPoint.ToString()); break; case "result": if (currentDataFileName == string.Empty) { Randomizer(); } if (File.Exists(currentDataFileName)) { ans = GetBin(currentDataFileName); //BGW_GTS.ReportProgress(1, "Result - True"); } else { Randomizer(); ans = GetBin(currentDataFileName); //ans = new byte[] { 4, 0 }; // * A Pokémon is present in the GTS. //BGW_GTS.ReportProgress(1, "Result - False"); //ans = new byte[] { 5, 0 }; // * There is no Pokémon stored. } break; case "get": if (File.Exists(currentDataFileName)) { ans = File.ReadAllBytes(currentDataFileName); BGW_GTS.ReportProgress(1, "Get"); } break; case "return": BGW_GTS.ReportProgress(1, "Return"); break; case "delete": if (File.Exists(currentDataFileName)) { if (currentDataFileName == string.Empty) { Randomizer(); } bin = GetBin(currentDataFileName); FileInfo fi = new FileInfo(currentDataFileName); string pkmSpecie = Pokemon.dPKMSpecies[BitConverter.ToUInt16(bin, 236)]; BGW_GTS.ReportProgress(1, " * Sending " + pkmSpecie + " ( " + fi.Name + " ) to " + sk.RemoteEndPoint.ToString()); Randomizer(); PostSendChanger(); } break; case "post": ans = new byte[] { 0xc, 0 }; break; case "post_finish": ans = new byte[] { 1, 0 }; break; case "search": Pokemon.InitializeDictionaries(); byte[] postdata = data.ToList().GetRange(0, 2).ToArray(); UInt16 pkmnnr = BitConverter.ToUInt16(postdata, 0); string pkmnname = Pokemon.dPKMSpecies[pkmnnr]; BGW_GTS.ReportProgress(1, "Searched for: #" + pkmnnr + " " + pkmnname); break; } Encoding E = Encoding.GetEncoding("iso-8859-1"); ansStr = E.GetString(ans); } SendResp(ref sock, ansStr); }
void CheckRequestUpdate() { if (requestQueue.Count <= 0) { return; } ReqStruct reqStruct = requestQueue.Dequeue(); UnityWebRequest request = reqStruct.request; GeneralResponse resp = reqStruct.resp; if (request != null && request.isDone) { if (request.error != null) { Debug.LogError(request.error); isOperationRunning = false; } else { if (request.downloadHandler.text.Contains(AppStoreOnboardApi.tokenExpiredInfo)) { UnityWebRequest newRequest = AppStoreOnboardApi.RefreshToken(); TokenInfo tokenInfoResp = new TokenInfo(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = tokenInfoResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else { if (resp.GetType() == typeof(TokenInfo)) { resp = JsonUtility.FromJson <TokenInfo> (request.downloadHandler.text); AppStoreOnboardApi.tokenInfo.access_token = ((TokenInfo)resp).access_token; if (AppStoreOnboardApi.tokenInfo.refresh_token == null || AppStoreOnboardApi.tokenInfo.refresh_token == "") { AppStoreOnboardApi.tokenInfo.refresh_token = ((TokenInfo)resp).refresh_token; } UnityWebRequest newRequest = AppStoreOnboardApi.GetUserId(); UserIdResponse userIdResp = new UserIdResponse(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = userIdResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else if (resp.GetType() == typeof(UserIdResponse)) { resp = JsonUtility.FromJson <UserIdResponse> (request.downloadHandler.text); AppStoreOnboardApi.userId = ((UserIdResponse)resp).sub; UnityWebRequest newRequest = AppStoreOnboardApi.GetOrgId(Application.cloudProjectId); OrgIdResponse orgIdResp = new OrgIdResponse(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = orgIdResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else if (resp.GetType() == typeof(OrgIdResponse)) { resp = JsonUtility.FromJson <OrgIdResponse> (request.downloadHandler.text); AppStoreOnboardApi.orgId = ((OrgIdResponse)resp).org_foreign_key; UnityWebRequest newRequest = AppStoreOnboardApi.GetOrgRoles(); OrgRoleResponse orgRoleResp = new OrgRoleResponse(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = orgRoleResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else if (resp.GetType() == typeof(OrgRoleResponse)) { resp = JsonUtility.FromJson <OrgRoleResponse> (request.downloadHandler.text); List <string> roles = ((OrgRoleResponse)resp).roles; if (roles.Contains("owner")) { ownerAuthed = true; if (reqStruct.targetStep == STEP_GET_CLIENT) { UnityWebRequest newRequest = AppStoreOnboardApi.GetUnityClientInfo(Application.cloudProjectId); UnityClientResponseWrapper clientRespWrapper = new UnityClientResponseWrapper(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = clientRespWrapper; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else if (reqStruct.targetStep == STEP_UPDATE_CLIENT) { UnityClientInfo unityClientInfo = new UnityClientInfo(); unityClientInfo.ClientId = unityClientID.stringValue; string callbackUrl = callbackUrl_in_memory; // read xiaomi from user input XiaomiSettings xiaomi = new XiaomiSettings(); xiaomi.appId = xiaomiAppID.stringValue; xiaomi.appKey = xiaomiAppKey.stringValue; xiaomi.appSecret = appSecret_in_memory; UnityWebRequest newRequest = AppStoreOnboardApi.UpdateUnityClient(Application.cloudProjectId, unityClientInfo, xiaomi, callbackUrl); UnityClientResponse clientResp = new UnityClientResponse(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = clientResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else if (reqStruct.targetStep == STEP_UPDATE_CLIENT_SECRET) { string clientId = unityClientID.stringValue; UnityWebRequest newRequest = AppStoreOnboardApi.UpdateUnityClientSecret(clientId); UnityClientResponse clientResp = new UnityClientResponse(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = clientResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } } else if (roles.Contains("user") || roles.Contains("manager")) { ownerAuthed = false; if (reqStruct.targetStep == STEP_GET_CLIENT) { UnityWebRequest newRequest = AppStoreOnboardApi.GetUnityClientInfo(Application.cloudProjectId); UnityClientResponseWrapper clientRespWrapper = new UnityClientResponseWrapper(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = clientRespWrapper; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else { Debug.LogError("Permision denied."); isOperationRunning = false; } } else { Debug.LogError("Permision denied."); isOperationRunning = false; } } else if (resp.GetType() == typeof(UnityClientResponseWrapper)) { string raw = "{ \"array\": " + request.downloadHandler.text + "}"; resp = JsonUtility.FromJson <UnityClientResponseWrapper> (raw); // only one element in the list if (((UnityClientResponseWrapper)resp).array.Length > 0) { UnityClientResponse unityClientResp = ((UnityClientResponseWrapper)resp).array [0]; unityClientID.stringValue = unityClientResp.client_id; unityClientKey.stringValue = unityClientResp.client_secret; unityClientRSAPublicKey.stringValue = unityClientResp.channel.publicRSAKey; clientSecret_in_memory = unityClientResp.channel.channelSecret; callbackUrl_in_memory = unityClientResp.channel.callbackUrl; callbackUrl_last = callbackUrl_in_memory; foreach (ThirdPartySettingsResponse thirdPartySetting in unityClientResp.channel.thirdPartySettings) { if (thirdPartySetting.appType.Equals(AppStoreOnboardApi.xiaomiAppType, StringComparison.InvariantCultureIgnoreCase)) { xiaomiAppID.stringValue = thirdPartySetting.appId; xiaomiAppKey.stringValue = thirdPartySetting.appKey; appSecret_in_memory = thirdPartySetting.appSecret; appId_last = xiaomiAppID.stringValue; appKey_last = xiaomiAppKey.stringValue; appSecret_last = appSecret_in_memory; } } AppStoreOnboardApi.updateRev = unityClientResp.rev; Debug.Log("Unity Client Refreshed. Finished: " + reqStruct.targetStep); AppStoreOnboardApi.loaded = true; isOperationRunning = false; serializedObject.ApplyModifiedProperties(); this.Repaint(); AssetDatabase.SaveAssets(); } else { // no client found, generate one. if (ownerAuthed) { UnityClientInfo unityClientInfo = new UnityClientInfo(); string callbackUrl = callbackUrl_in_memory; // read xiaomi from user input XiaomiSettings xiaomi = new XiaomiSettings(); xiaomi.appId = xiaomiAppID.stringValue; xiaomi.appKey = xiaomiAppKey.stringValue; xiaomi.appSecret = appSecret_in_memory; UnityWebRequest newRequest = AppStoreOnboardApi.GenerateUnityClient(Application.cloudProjectId, unityClientInfo, xiaomi, callbackUrl); UnityClientResponse clientResp = new UnityClientResponse(); ReqStruct newReqStruct = new ReqStruct(); newReqStruct.request = newRequest; newReqStruct.resp = clientResp; newReqStruct.targetStep = reqStruct.targetStep; requestQueue.Enqueue(newReqStruct); } else { Debug.LogError("Permision denied."); isOperationRunning = false; } } } else if (resp.GetType() == typeof(UnityClientResponse)) { resp = JsonUtility.FromJson <UnityClientResponse> (request.downloadHandler.text); unityClientID.stringValue = ((UnityClientResponse)resp).client_id; unityClientKey.stringValue = ((UnityClientResponse)resp).client_secret; unityClientRSAPublicKey.stringValue = ((UnityClientResponse)resp).channel.publicRSAKey; clientSecret_in_memory = ((UnityClientResponse)resp).channel.channelSecret; callbackUrl_in_memory = ((UnityClientResponse)resp).channel.callbackUrl; callbackUrl_last = callbackUrl_in_memory; foreach (ThirdPartySettingsResponse thirdPartySetting in ((UnityClientResponse)resp).channel.thirdPartySettings) { if (thirdPartySetting.appType.Equals(AppStoreOnboardApi.xiaomiAppType, StringComparison.InvariantCultureIgnoreCase)) { xiaomiAppID.stringValue = thirdPartySetting.appId; xiaomiAppKey.stringValue = thirdPartySetting.appKey; appSecret_in_memory = thirdPartySetting.appSecret; appId_last = xiaomiAppID.stringValue; appKey_last = xiaomiAppKey.stringValue; appSecret_last = appSecret_in_memory; } } AppStoreOnboardApi.updateRev = ((UnityClientResponse)resp).rev; Debug.Log("Unity Client Refreshed. Finished: " + reqStruct.targetStep); AppStoreOnboardApi.loaded = true; isOperationRunning = false; serializedObject.ApplyModifiedProperties(); this.Repaint(); AssetDatabase.SaveAssets(); } } } } else { requestQueue.Enqueue(reqStruct); } }
private static ReqStruct GetReq(ref Socket sock) { //Dim sock As Socket = serv.Accept() byte[] a = new byte[sock.ReceiveBufferSize]; int cnt = sock.Receive(a, sock.ReceiveBufferSize, SocketFlags.None); Array.Resize(ref a, cnt); string data = Encoding.GetEncoding("iso-8859-1").GetString(a); ReqStruct ret = new ReqStruct(); ret.req = new Request(data); ret.sock = sock; return ret; }
private void asyncReq(Socket sk) { string ansStr = null; byte[] ans = { }; ReqStruct reqst = default(ReqStruct); byte[] data = new byte[512]; try { reqst = GetReq(ref sk); } catch (Exception ex) { BGW_GTS.ReportProgress(1, "Got wrong request, probably not from a DS. Error was : " + ex.Message); return; } Socket sock = reqst.sock; Request req = reqst.req; string pid = req.getvars["pid"]; //BGW_GTS.ReportProgress(1, "Client " + sock.RemoteEndPoint.ToString()); //BGW_GTS.ReportProgress(1, "Requesting : " + req.RequestedPage()); byte[] bin = null; if (!CB_GTS_SendPKMN.Checked) { currentDataFileName = ""; } else { if (CB_GTS_SendPKMN.Checked && RB_GTS_SendOne.Checked && currentDataFileName == "") { currentDataFileName = TB_GTS_SendPKMN.Text; } else if (CB_GTS_SendPKMN.Checked && RB_GTS_SendFolder.Checked) { do { if (PKMfromFolder.Count > 0) { currentDataFileName = PKMfromFolder[0]; if (!File.Exists(currentDataFileName)) { PKMfromFolder.RemoveAt(0); BGW_GTS.ReportProgress(1, "The file doesn't exist any more: " + currentDataFileName); } //FileInfo FI = new FileInfo(currentDataFileName); } else { currentDataFileName = ""; BGW_GTS.ReportProgress(3); } } while (!File.Exists(currentDataFileName) && PKMfromFolder.Count > 0); } } bin = GetBin(currentDataFileName); if (req.getvars.Count == 1) { ansStr = RandStr(32); } else { byte[] encrypted_data = System.Convert.FromBase64String(req.getvars["data"].Replace("-", "+").Replace("_", "/")); data = Decrypt_data(encrypted_data); data = data.ToList().GetRange(4, data.Length - 4).ToArray(); switch (req.action) { case "info": BGW_GTS.ReportProgress(1, "Someone has entered the GTS..."); ans = new byte[] { 1, 0 }; break; case "setProfile": ans = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }; break; case "result": if (File.Exists(currentDataFileName)) { //ans = File.ReadAllBytes(currentDataFileName); ans = bin; //ans = new byte[] { 4, 0 }; // * A Pokémon is present in the GTS. } else { //ans = new byte[] { 4, 0 }; // * A Pokémon is present in the GTS. ans = new byte[] { 5, 0 }; // * There is no Pokémon stored. } break; case "get": if (File.Exists(currentDataFileName)) { ans = File.ReadAllBytes(currentDataFileName); } break; case "return": case "delete": if (File.Exists(currentDataFileName)) { string pkmSpecie = Pokemon.dPKMSpecies[BitConverter.ToUInt16(bin, 236)]; BGW_GTS.ReportProgress(1, " * Sending " + pkmSpecie + " back to the game..."); //File.Delete(currentDataFileName); if (CB_GTS_SendPKMN.Checked && RB_GTS_SendFolder.Checked && PKMfromFolder.Contains(currentDataFileName)) { PKMfromFolder.Remove(currentDataFileName); do { if (PKMfromFolder.Count > 0) { currentDataFileName = PKMfromFolder[0]; FileInfo FI = new FileInfo(currentDataFileName); int actCount = (CountPKMfromFolder - PKMfromFolder.Count) + 1; BGW_GTS.ReportProgress(1, "Next Pkm (" + actCount + " of " + CountPKMfromFolder + "): " + FI.Name); if (!File.Exists(currentDataFileName)) { BGW_GTS.ReportProgress(1, "The file doesn't exist any more: " + currentDataFileName); PKMfromFolder.RemoveAt(0); } } else { currentDataFileName = ""; BGW_GTS.ReportProgress(1, "All pokemon from the folder have been sent."); BGW_GTS.ReportProgress(3); } } while (!File.Exists(currentDataFileName) && PKMfromFolder.Count > 0); } } ans = new byte[] { 1, 0 }; break; case "post": try { byte[] binData = data.ToList().GetRange(0, 236).ToArray(); byte[] postData = data.ToList().GetRange(236, data.Length - 236).ToArray(); byte[] pkmData = Pokemon.DecryptPokemon(binData); string pkmSpecie = Pokemon.dPKMSpecies[BitConverter.ToUInt16(postData, 0)]; byte pkmLevel = postData[3]; string pkmGenderName = null; switch (postData[2]) { case 1: pkmGenderName = "male"; break; case 2: pkmGenderName = "female"; break; default: pkmGenderName = "genderless"; break; } string filename = Application.StartupPath + "\\PKMs\\" + DateTime.Now.Ticks + "_" + pkmSpecie + "_" + pid + ".pkm"; File.WriteAllBytes(filename, pkmData); //File.WriteAllBytes("Current\current_" & pid & ".pkm", pkmData) //File.WriteAllBytes(currentDataFileName, data); BGW_GTS.ReportProgress(1, " * A " + pkmSpecie + " (lvl " + pkmLevel + ", " + pkmGenderName + ") was deposited and converted to .pkm."); BGW_GTS.ReportProgress(1, " * Filename : " + filename); if (CB_GTS_SendAfterReceive.Checked) { BGW_GTS.ReportProgress(2, filename); //TB_GTS_SendPKMN.Text = filename; currentDataFileName = filename; } currentDataFileName = filename; bin = GetBin(currentDataFileName); if (!CB_GTS_Reject.Checked) { ans = new byte[] { 1, 0 } } ; else { ans = new byte[] { 0xc, 0 } }; } catch { BGW_GTS.ReportProgress(1, "Error, sending '0xc'."); ans = new byte[] { 0xc, 0 }; } break; case "post_finish": ans = new byte[] { 1, 0 }; break; case "search": Pokemon.InitializeDictionaries(); byte[] postdata = data.ToList().GetRange(0, 2).ToArray(); UInt16 pkmnnr = BitConverter.ToUInt16(postdata, 0); string pkmnname = Pokemon.dPKMSpecies[pkmnnr]; BGW_GTS.ReportProgress(1, "Searched for: #" + pkmnnr + " " + pkmnname); break; } Encoding E = Encoding.GetEncoding("iso-8859-1"); ansStr = E.GetString(ans); } SendResp(ref sock, ansStr); }