using System; using System.Collections; using System.IO; using Cute; using UnityEngine; using UnityEngine.Networking; using Wizard; using Wizard.ErrorDialog; public class Twitter : MonoBehaviour { private const string UNITY_CLASS = "com.unity3d.player.UnityPlayer"; private const string UNITY_ACTIVITY = "currentActivity"; private const string TWITTER_URL = "http://twitter.com/intent/tweet?text={0}&url={1}&hashtags={2}"; private const string SAVED_IMAGE_NAME = "image_saved.png"; private const int ERROR_CODE_UNKNOWN = 102; public const int REQUEST_CODE = 555; private const float TIMEOUT = 20f; private GenerateDeckImageTask _task; private const string OBJECT_NAME = "TwitterShareObject"; private void Start() { base.gameObject.name = "TwitterShareObject"; } public static void Logout(Action callback = null) { } public void TweetWithoutImage(string text, string url, string tags) { Post(text, url, tags, null); } public void TweetWithScreenshot(string text, string url, string tags) { ScreenCapture.CaptureScreenshot("image_saved.png"); string imageFilename = GetImagePath(); UIManager.GetInstance().createInSceneCenterLoading(notBlack: true); StartCoroutine(CheckFileExistsAndExecute(delegate { Post(text, url, tags, imageFilename); UIManager.GetInstance().closeInSceneCenterLoading(); }, imageFilename, delegate(bool isTimeout) { CloseLoadingAndShowErrorDialog(isTimeout); })); } public void TweetWithDownloadImage(string text, string url, string tags, string imageUrl, Action callback = null) { string imageFilename = GetImagePath(); UIManager.GetInstance().createInSceneCenterLoading(notBlack: true); Action action = delegate { StartCoroutine(CheckFileExistsAndExecute(delegate { Post(text, url, tags, imageFilename); UIManager.GetInstance().closeInSceneCenterLoading(); if (callback != null) { callback(); } }, imageFilename, delegate(bool isTimeout) { CloseLoadingAndShowErrorDialog(isTimeout); })); }; StartCoroutine(DownloadImage(imageUrl, imageFilename, action, CloseLoadingAndShowErrorDialog)); } public void TweetWithSavedImage(string text, string hashtags, string imagePath, Action callback = null) { UIManager.GetInstance().createInSceneCenterLoading(notBlack: true); StartCoroutine(CheckFileExistsAndExecute(delegate { Post(text, "", hashtags, imagePath); UIManager.GetInstance().closeInSceneCenterLoading(); if (callback != null) { callback(); } }, imagePath, delegate(bool isTimeout) { CloseLoadingAndShowErrorDialog(isTimeout); })); } private static string MakeIntentUrl(string text, string url, string tags) { return $"http://twitter.com/intent/tweet?text={UnityWebRequest.EscapeURL(text)}&url={UnityWebRequest.EscapeURL(url)}&hashtags={UnityWebRequest.EscapeURL(tags)}"; } private static string FormatText(string text, string url, string tags) { if (!string.IsNullOrEmpty(url)) { text = text + " " + url; } if (!string.IsNullOrEmpty(tags)) { string[] array = tags.Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries); foreach (string text2 in array) { text = text + " #" + text2; } } return text; } private void ShowDialogUrl(string text, string url, string tags) { bool isBackKeyEnable = GameMgr.GetIns().GetInputMgr().isBackKeyEnable; GameMgr.GetIns().GetInputMgr().isBackKeyEnable = true; SystemText systemText = Wizard.Data.SystemText; DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose(); dialogBase.SetTitleLabel(systemText.Get("Card_0161")); dialogBase.SetText(systemText.Get("Common_0208")); dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn); dialogBase.SetButtonText(systemText.Get("Dia_Web_001_Button")); dialogBase.onPushButton1 = delegate { BrowserURL.Open(MakeIntentUrl(text, url, tags)); }; dialogBase.OnClose = delegate { GameMgr.GetIns().GetInputMgr().isBackKeyEnable = isBackKeyEnable; }; } public void Post(string text, string url, string tags, string imageFilename) { ShowDialogUrl(text, url, tags); } private IEnumerator DownloadImage(string url, string filename, Action action, Action onError) { using UnityWebRequest request = UnityWebRequestTexture.GetTexture(url); yield return request.SendWebRequest(); bool isTimeout = false; float startDownloadTime = Time.time; while (!request.isDone && !isTimeout) { isTimeout = Time.time - startDownloadTime > 20f; yield return null; } if (request.error != null || isTimeout) { onError(isTimeout); request.Dispose(); yield break; } Texture2D content = DownloadHandlerTexture.GetContent(request); request.Dispose(); byte[] bytes = content.EncodeToPNG(); UnityEngine.Object.Destroy(content); File.WriteAllBytes(filename, bytes); action(); } private IEnumerator CheckFileExistsAndExecute(Action onFileExists, string filePath, Action onError) { bool isFileTimeout = false; float startWaitTime = Time.time; while (!File.Exists(filePath) && !isFileTimeout) { isFileTimeout = Time.time - startWaitTime > 20f; yield return null; } if (File.Exists(filePath)) { onFileExists(); } else { onError(obj: false); } } private static string GetImagePath() { return "" + "image_saved.png"; } private void CloseLoadingAndShowErrorDialog(bool isTimeout) { UIManager.GetInstance().closeInSceneCenterLoading(); if (isTimeout) { Dialog.Create("TIMEOUT_NORETRY"); } else { Dialog.Create(102); } } public void TweetDataFromPortal(int[] cardIds, ClassSet classSet, GenerateDeckCodeTask.SubmitDeckType submitType, int[] phantomCardIdList, string rotationId) { GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON); StartTweet(cardIds, classSet, submitType, phantomCardIdList, rotationId); } private void StartTweet(int[] cardIds, ClassSet classSet, GenerateDeckCodeTask.SubmitDeckType submitType, int[] phantomCardIdList, string rotationId) { UIManager.GetInstance().createInSceneCenterLoading(); Action callbackOnSuccess = delegate { if (CardBasePrm.ClanTypeIsUseable(classSet.SubClass)) { _task = new GenerateDeckImageTask(); _task.SetParameter((int)classSet.MainClass, (int)classSet.SubClass, submitType, cardIds, phantomCardIdList); StartCoroutine(Toolbox.NetworkManager.Connect(_task, GetImageResponse, null, null, encrypt: false)); } else { _task = new GenerateDeckImageTask(); _task.SetParameter((int)classSet.MainClass, submitType, cardIds, rotationId, phantomCardIdList); StartCoroutine(Toolbox.NetworkManager.Connect(_task, GetImageResponse, null, null, encrypt: false)); } }; GenerateDeckImageMaintenanceTask task = new GenerateDeckImageMaintenanceTask(); StartCoroutine(Toolbox.NetworkManager.Connect(task, callbackOnSuccess)); } private void GetImageResponse(NetworkTask.ResultCode error) { string imageFilename = GetImagePath(); File.WriteAllBytes(imageFilename, _task.ImageBytes); string text = _task.TwitterMessage; _task = null; StartCoroutine(CheckFileExistsAndExecute(delegate { Post(text, "", "", imageFilename); UIManager.GetInstance().closeInSceneCenterLoading(); }, imageFilename, delegate(bool isTimeout) { CloseLoadingAndShowErrorDialog(isTimeout); })); } public void OnResult(string result) { bool isBackKeyEnable = GameMgr.GetIns().GetInputMgr().isBackKeyEnable; GameMgr.GetIns().GetInputMgr().isBackKeyEnable = true; DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose(); dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn); SystemText systemText = Wizard.Data.SystemText; dialogBase.SetTitleLabel(systemText.Get("Dia_Share_005")); string[] array = result.Split(','); if (array.Length <= 1 || int.Parse(array[1]) == 555) { if (int.Parse(array[0]) == 1) { dialogBase.SetText(systemText.Get("Dia_Share_001")); } else { dialogBase.SetText(systemText.Get("Dia_Share_002")); } dialogBase.OnClose = delegate { GameMgr.GetIns().GetInputMgr().isBackKeyEnable = isBackKeyEnable; }; } } }