diff options
author | chai <chaifix@163.com> | 2020-12-30 20:59:04 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2020-12-30 20:59:04 +0800 |
commit | e9ea621b93fbb58d9edfca8375918791637bbd52 (patch) | |
tree | 19ce3b1c1f2d51eda6878c9d0f2c9edc27f13650 /Client/Assembly-CSharp/AnnouncementPopUp.cs |
+init
Diffstat (limited to 'Client/Assembly-CSharp/AnnouncementPopUp.cs')
-rw-r--r-- | Client/Assembly-CSharp/AnnouncementPopUp.cs | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/Client/Assembly-CSharp/AnnouncementPopUp.cs b/Client/Assembly-CSharp/AnnouncementPopUp.cs new file mode 100644 index 0000000..42b96b0 --- /dev/null +++ b/Client/Assembly-CSharp/AnnouncementPopUp.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections; +using System.Net; +using Hazel; +using Hazel.Udp; +using UnityEngine; + +public class AnnouncementPopUp : MonoBehaviour +{ + private const uint AnnouncementVersion = 1U; + + private UdpClientConnection connection; + + private static AnnouncementPopUp.AnnounceState AskedForUpdate; + + public TextRenderer AnnounceText; + + private Announcement announcementUpdate; + + private enum AnnounceState + { + Fetching, + Failed, + Success, + Cached + } + + private static bool IsSuccess(AnnouncementPopUp.AnnounceState state) + { + return state == AnnouncementPopUp.AnnounceState.Success || state == AnnouncementPopUp.AnnounceState.Cached; + } + + public IEnumerator Init() + { + if (AnnouncementPopUp.AskedForUpdate != AnnouncementPopUp.AnnounceState.Fetching) + { + yield break; + } + yield return DestroyableSingleton<ServerManager>.Instance.WaitForServers(); + this.connection = new UdpClientConnection(new IPEndPoint(IPAddress.Parse(DestroyableSingleton<ServerManager>.Instance.OnlineNetAddress), 22024), IPMode.IPv4); + this.connection.DataReceived += this.Connection_DataReceived; + this.connection.Disconnected += this.Connection_Disconnected; + Announcement lastAnnouncement = SaveManager.LastAnnouncement; + try + { + MessageWriter messageWriter = MessageWriter.Get(SendOption.None); + messageWriter.WritePacked(1U); + messageWriter.WritePacked(lastAnnouncement.Id); + this.connection.ConnectAsync(messageWriter.ToByteArray(true), 5000); + messageWriter.Recycle(); + yield break; + } + catch + { + AnnouncementPopUp.AskedForUpdate = AnnouncementPopUp.AnnounceState.Failed; + yield break; + } + yield break; + } + + private void Connection_Disconnected(object sender, DisconnectedEventArgs e) + { + AnnouncementPopUp.AskedForUpdate = AnnouncementPopUp.AnnounceState.Failed; + this.connection.Dispose(); + this.connection = null; + } + + private void Connection_DataReceived(DataReceivedEventArgs e) + { + MessageReader message = e.Message; + try + { + if (message.Length > 4) + { + this.announcementUpdate = default(Announcement); + this.announcementUpdate.DateFetched = DateTime.UtcNow.DayOfYear; + this.announcementUpdate.Id = message.ReadPackedUInt32(); + this.announcementUpdate.AnnounceText = message.ReadString(); + AnnouncementPopUp.AskedForUpdate = AnnouncementPopUp.AnnounceState.Success; + } + else + { + AnnouncementPopUp.AskedForUpdate = AnnouncementPopUp.AnnounceState.Cached; + } + } + finally + { + message.Recycle(); + } + try + { + this.connection.Dispose(); + this.connection = null; + } + catch + { + } + } + + public IEnumerator Show() + { + float timer = 0f; + while (AnnouncementPopUp.AskedForUpdate == AnnouncementPopUp.AnnounceState.Fetching && this.connection != null && timer < 5f) + { + timer += Time.deltaTime; + yield return null; + } + if (!AnnouncementPopUp.IsSuccess(AnnouncementPopUp.AskedForUpdate)) + { + Announcement lastAnnouncement = SaveManager.LastAnnouncement; + if (lastAnnouncement.Id == 0U) + { + this.AnnounceText.Text = "Couldn't get announcement."; + } + else + { + this.AnnounceText.Text = "Couldn't get announcement. Last Known:\r\n" + lastAnnouncement.AnnounceText; + } + } + else if (this.announcementUpdate.Id != SaveManager.LastAnnouncement.Id) + { + if (AnnouncementPopUp.AskedForUpdate != AnnouncementPopUp.AnnounceState.Cached) + { + base.gameObject.SetActive(true); + } + if (this.announcementUpdate.Id == 0U) + { + this.announcementUpdate = SaveManager.LastAnnouncement; + this.announcementUpdate.DateFetched = DateTime.UtcNow.DayOfYear; + } + SaveManager.LastAnnouncement = this.announcementUpdate; + this.AnnounceText.Text = this.announcementUpdate.AnnounceText; + } + while (base.gameObject.activeSelf) + { + yield return null; + } + yield break; + } + + public void Close() + { + base.gameObject.SetActive(false); + } + + private void OnDestroy() + { + if (this.connection != null) + { + this.connection.Dispose(); + this.connection = null; + } + } +} |