public void AddTimer(string name, float start, float delay, bool loop, EventHandler callback, object param = null) { if (callback == null) { DebugUtils.LogWarning("AddTimer - '{0}' timer's callback is null.", name); return; } if (pending_list_.ContainsKey(name)) { DebugUtils.LogWarning("AddTimer - '{0}' timer already exists.", name); return; } if (timer_list_.ContainsKey(name)) { if (!is_all_clear_ && !remove_list_.Contains(name)) { DebugUtils.LogWarning("AddTimer - '{0}' timer already exists.", name); return; } } pending_list_.Add(name, new Event(name, start, delay, loop, callback, param)); DebugUtils.DebugLog("AddTimer - '{0}' timer added.", name); }
void OnLoginCb(ILoginResult result) { if (result.Error != null) { DebugUtils.DebugLogError(result.Error); OnEventHandler(SnResultCode.kLoginFailed); } else if (!FB.IsLoggedIn) { DebugUtils.DebugLog("User cancelled login."); OnEventHandler(SnResultCode.kLoginFailed); } else { DebugUtils.DebugLog("Login successful!"); // AccessToken class will have session details var aToken = Facebook.Unity.AccessToken.CurrentAccessToken; // Print current access token's granted permissions StringBuilder perms = new StringBuilder(); perms.Append("Permissions: "); foreach (string perm in aToken.Permissions) { perms.AppendFormat("\"{0}\", ", perm); } DebugUtils.Log(perms.ToString()); // Reqests my info and profile picture FB.API("me?fields=id,name,picture.width(128).height(128)", HttpMethod.GET, OnMyProfileCb); OnEventHandler(SnResultCode.kLoggedIn); } }
// Callback-related functions void OnInitCb() { if (FB.IsInitialized) { // Signal an app activation App Event if (Application.isMobilePlatform) { FB.ActivateApp(); } if (FB.IsLoggedIn) { DebugUtils.DebugLog("Already logged in."); OnEventHandler(SnResultCode.kLoggedIn); } else { OnEventHandler(SnResultCode.kInitialized); } } else { Debug.LogWarning("Failed to Initialize the Facebook SDK"); } }
// Picture-related functions IEnumerator RequestPicture(UserInfo info) { WWW www = new WWW(info.url); yield return(www); if (www.texture != null) { info.picture = www.texture; DebugUtils.DebugLog("Gotten {0}'s profile picture.", info.name); } }
void OnInviteListCb(IGraphResult result) { try { Dictionary <string, object> json = Json.Deserialize(result.RawResult) as Dictionary <string, object>; if (json == null) { DebugUtils.DebugLogError("OnInviteListCb - json is null."); OnEventHandler(SnResultCode.kError); return; } object invitable_friends = null; json.TryGetValue("invitable_friends", out invitable_friends); if (invitable_friends == null) { DebugUtils.DebugLogError("OnInviteListCb - invitable_friends is null."); OnEventHandler(SnResultCode.kError); return; } invite_friends_.Clear(); List <object> list = ((Dictionary <string, object>)invitable_friends)["data"] as List <object>; foreach (object item in list) { Dictionary <string, object> info = item as Dictionary <string, object>; Dictionary <string, object> picture = ((Dictionary <string, object>)info["picture"])["data"] as Dictionary <string, object>; string url = picture["url"] as string; UserInfo user = new UserInfo(); user.id = info["id"] as string; user.name = info["name"] as string; user.url = url; invite_friends_.Add(user); DebugUtils.DebugLog(">> id:{0} name:{1} url:{2}", user.id, user.name, user.url); } DebugUtils.Log("Succeeded in getting the invite list."); OnEventHandler(SnResultCode.kInviteList); if (auto_request_picture_ && invite_friends_.Count > 0) { StartCoroutine(RequestPictureList(invite_friends_)); } } catch (Exception e) { DebugUtils.DebugLogError("Failure in OnInviteListCb: " + e.ToString()); } }
void OnFriendListCb(IGraphResult result) { try { Dictionary <string, object> json = Json.Deserialize(result.RawResult) as Dictionary <string, object>; if (json == null) { DebugUtils.DebugLogError("OnFriendListCb - json is null."); OnEventNotify(SNResultCode.kError); return; } // friend list object friend_list = null; json.TryGetValue("friends", out friend_list); if (friend_list == null) { DebugUtils.DebugLogError("OnInviteListCb - friend_list is null."); OnEventNotify(SNResultCode.kError); return; } friends_.Clear(); List <object> list = ((Dictionary <string, object>)friend_list)["data"] as List <object>; foreach (object item in list) { Dictionary <string, object> info = item as Dictionary <string, object>; Dictionary <string, object> picture = ((Dictionary <string, object>)info["picture"])["data"] as Dictionary <string, object>; UserInfo user = new UserInfo(); user.id = info["id"] as string; user.name = info["name"] as string; user.url = picture["url"] as string; friends_.Add(user); DebugUtils.DebugLog("> id:{0} name:{1} url:{2}", user.id, user.name, user.url); } DebugUtils.Log("Succeeded in getting the friend list. count:{0}", friends_.Count); OnEventNotify(SNResultCode.kFriendList); if (auto_request_picture_ && friends_.Count > 0) { StartCoroutine(RequestPictureList(friends_)); } } catch (Exception e) { DebugUtils.DebugLogError("Failure in OnFriendListCb: " + e.ToString()); } }
void PostCallback(IGraphResult result) { DebugUtils.DebugLog("PostCallback called."); if (result.Error != null) { DebugUtils.DebugLogError(result.Error); OnEventHandler(SnResultCode.kPostFailed); return; } DebugUtils.Log("Post successful!"); OnEventHandler(SnResultCode.kPosted); }
IEnumerator RequestPictureList(List <UserInfo> list) { if (list == null || list.Count <= 0) { yield break; } foreach (UserInfo user in list) { WWW www = new WWW(user.url); yield return(www); user.picture = www.texture; DebugUtils.DebugLog("Gotten {0}'s profile picture.", user.name); } }
public override void Init(params object[] param) { DebugUtils.DebugLog("FacebookConnector.Init called."); if (!FB.IsInitialized) { // Initialize the Facebook SDK FB.Init(OnInitCb, OnHideCb); } else { // Already initialized, signal an app activation App Event if (Application.isMobilePlatform) { FB.ActivateApp(); } } }
public static void Load(string path) { if (!File.Exists(path)) { DebugUtils.Log("Can't find a config file. path: {0}", path); return; } string text = File.ReadAllText(path); DebugUtils.DebugLog("{0} >> {1}", path, text); Dictionary <string, object> json = Json.Deserialize(text) as Dictionary <string, object>; if (json == null) { DebugUtils.Log("Deserialize json failed. json: {0}", text); return; } data_ = json; }
public void KillTimer(string name) { if (!timer_list_.ContainsKey(name) && !pending_list_.ContainsKey(name)) { return; } if (pending_list_.ContainsKey(name)) { pending_list_.Remove(name); } else if (!remove_list_.Contains(name)) { remove_list_.Add(name); } else { return; } DebugUtils.DebugLog("KillTimer - '{0}' timer removed.", name); }
void OnMyProfileCb(IGraphResult result) { DebugUtils.DebugLog("OnMyProfileCb called."); if (result.Error != null) { DebugUtils.DebugLogError(result.Error); OnEventHandler(SnResultCode.kError); return; } try { Dictionary <string, object> json = Json.Deserialize(result.RawResult) as Dictionary <string, object>; if (json == null) { DebugUtils.DebugLogError("OnMyProfileCb - json is null."); OnEventHandler(SnResultCode.kError); return; } // my profile my_info_.id = json["id"] as string; my_info_.name = json["name"] as string; DebugUtils.Log("Facebook id: {0}, name: {1}.", my_info_.id, my_info_.name); // my picture var picture = json["picture"] as Dictionary <string, object>; var data = picture["data"] as Dictionary <string, object>; my_info_.url = data["url"] as string; StartCoroutine(RequestPicture(my_info_)); OnEventHandler(SnResultCode.kMyProfile); } catch (Exception e) { DebugUtils.DebugLogError("Failure in OnMyProfileCb: " + e.ToString()); } }
public void LogInWithPublish(List <string> perms) { DebugUtils.DebugLog("Request facebook login with publish."); FB.LogInWithPublishPermissions(perms, OnLoginCb); }
public void LogInWithRead(List <string> perms) { DebugUtils.DebugLog("Request facebook login with read."); FB.LogInWithReadPermissions(perms, OnLoginCb); }
void OnHideCb(bool isGameShown) { DebugUtils.DebugLog("isGameShown: {0}", isGameShown); }
// Checks download file list private IEnumerator CheckFileList(List <DownloadFileInfo> list) { List <DownloadFileInfo> tmp_list = new List <DownloadFileInfo>(list); List <string> verify_file_list = new List <string>(); List <string> remove_list = new List <string>(); Queue <int> rnd_list = new Queue <int>(); bool verify_success = true; int rnd_index = -1; DateTime cached_time = File.GetLastWriteTime(target_path_ + kCachedFileName); check_time_ = DateTime.Now; delete_file_list_.Clear(); // Randomly check list if (cached_list_.Count > 0) { int max_count = cached_list_.Count; int count = Math.Min(Math.Max(1, max_count / 10), 10); System.Random rnd = new System.Random((int)DateTime.Now.Ticks); while (rnd_list.Count < count) { rnd_index = rnd.Next(1, max_count + 1) - 1; if (!rnd_list.Contains(rnd_index)) { rnd_list.Enqueue(rnd_index); } } DebugUtils.DebugLog("Random check file count is {0}", rnd_list.Count); rnd_index = rnd_list.Count > 0 ? rnd_list.Dequeue() : -1; } // Checks local files int index = 0; foreach (DownloadFileInfo file in cached_list_) { DownloadFileInfo item = list.Find(i => i.path == file.path); if (item != null) { string path = target_path_ + file.path; FileInfo info = new FileInfo(path); if (!File.Exists(path) || item.size != info.Length || item.hash != file.hash) { remove_list.Add(file.path); } else { string filename = Path.GetFileName(item.path); if (filename[0] == '_' || index == rnd_index || File.GetLastWriteTime(path).Ticks > cached_time.Ticks) { if (index == rnd_index) { rnd_index = rnd_list.Count > 0 ? rnd_list.Dequeue() : -1; } verify_file_list.Add(file.path); MD5Async.Compute(ref path, ref item, delegate(string p, DownloadFileInfo f, bool is_match) { if (VerifyCallback != null) { VerifyCallback(p); } verify_file_list.Remove(f.path); if (is_match) { list.Remove(f); } else { remove_list.Add(f.path); verify_success = false; } }); } else { list.Remove(item); } } } else { remove_list.Add(file.path); } ++index; } while (verify_file_list.Count > 0) { yield return(new WaitForSeconds(0.1f)); } RemoveCachedList(remove_list); DebugUtils.Log("Random validation has {0}", (verify_success ? "succeeded" : "failed")); // Checks all local files if (!verify_success) { foreach (DownloadFileInfo file in cached_list_) { DownloadFileInfo item = tmp_list.Find(i => i.path == file.path); if (item != null) { verify_file_list.Add(file.path); string path = target_path_ + file.path; MD5Async.Compute(ref path, ref item, delegate(string p, DownloadFileInfo f, bool is_match) { if (VerifyCallback != null) { VerifyCallback(p); } verify_file_list.Remove(f.path); if (!is_match) { remove_list.Add(f.path); if (!list.Contains(f)) { list.Add(f); } } }); } } while (verify_file_list.Count > 0) { yield return(new WaitForSeconds(0.1f)); } RemoveCachedList(remove_list); } TimeSpan span = new TimeSpan(DateTime.Now.Ticks - check_time_.Ticks); DebugUtils.Log("File check total time - {0:F2}s", span.TotalMilliseconds / 1000f); total_download_count_ = list.Count; foreach (DownloadFileInfo item in list) { total_download_size_ += item.size; } if (total_download_count_ > 0) { state_ = State.Ready; if (ReadyCallback != null) { ReadyCallback(total_download_count_, total_download_size_); } } else { DeleteLocalFiles(); UpdateCachedList(); state_ = State.Completed; DebugUtils.Log("All resources are up to date."); OnFinishedCallback(DownloadResult.SUCCESS); } }