summaryrefslogtreecommitdiff
path: root/Client/Assembly-CSharp/AnnouncementPopUp.cs
blob: 42b96b07280213ccbd79b20195d1e766f11f8288 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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;
		}
	}
}