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.Instance.WaitForServers(); this.connection = new UdpClientConnection(new IPEndPoint(IPAddress.Parse(DestroyableSingleton.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; } } }