forked from cbluoss/OpenHacknet
/
MessageBoardDaemon.cs
352 lines (333 loc) · 15.8 KB
/
MessageBoardDaemon.cs
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System;
using System.Collections.Generic;
using Hacknet.Gui;
using Hacknet.UIUtils;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Hacknet
{
internal class MessageBoardDaemon : AuthenticatingDaemon
{
private const int THREAD_PREVIEW_HEIGHT = 415;
private static readonly Color UsernameColor = new Color(17, 119, 67);
private static readonly Color ImplicationColor = new Color(56, 184, 131);
private static Dictionary<MessageBoardPostImage, Texture2D> Images;
public string BoardName = "/el/ - Digital Security";
private readonly string boardsListingString =
"[a/b/c/d/e/f/g/gif/h/hr/k/m/o/p/r/s/t/u/v/vg/vr/w/wg][i/ic][r9k][s4s][cm/hm/lgbt/y][3/adv/an/asp/cgl/ck/co/diy/fa/fit/gd/hc/int/jp/lit/mlp/mu/n/out/po/pol/sci/soc/sp/tg/toy/trv/tv/vp/wsg/x][rs]";
private int CurrentThreadHeight = 100;
private Folder rootFolder;
private MessageBoardState state = MessageBoardState.Board;
private Vector2 ThreadScrollPosition = Vector2.Zero;
private Folder threadsFolder;
private readonly ScrollableSectionedPanel threadsPanel;
public List<string> ThreadsToAdd = new List<string>();
private MessageBoardThread viewingThread;
public MessageBoardDaemon(Computer c, OS os)
: base(c, "Message Board", os)
{
if (Images == null)
{
Images = new Dictionary<MessageBoardPostImage, Texture2D>();
Images.Add(MessageBoardPostImage.Academic, os.content.Load<Texture2D>("Sprites/Academic_Logo"));
Images.Add(MessageBoardPostImage.Sun, os.content.Load<Texture2D>("Sprites/Sun"));
Images.Add(MessageBoardPostImage.Snake, os.content.Load<Texture2D>("Sprites/Snake"));
Images.Add(MessageBoardPostImage.Circle, os.content.Load<Texture2D>("CircleOutline"));
Images.Add(MessageBoardPostImage.Duck, os.content.Load<Texture2D>("Sprites/Duck"));
Images.Add(MessageBoardPostImage.Page, os.content.Load<Texture2D>("Sprites/Page"));
Images.Add(MessageBoardPostImage.Speech, os.content.Load<Texture2D>("Sprites/SpeechBubble"));
Images.Add(MessageBoardPostImage.Mod, os.content.Load<Texture2D>("Sprites/Hammer"));
Images.Add(MessageBoardPostImage.Chip, os.content.Load<Texture2D>("Sprites/Chip"));
}
threadsPanel = new ScrollableSectionedPanel(415, GuiData.spriteBatch.GraphicsDevice);
}
public override void initFiles()
{
base.initFiles();
rootFolder = new Folder("ImageBoard");
threadsFolder = new Folder("Threads");
rootFolder.folders.Add(threadsFolder);
comp.files.root.folders.Add(rootFolder);
for (var index = 0; index < ThreadsToAdd.Count; ++index)
AddThread(ThreadsToAdd[index]);
ThreadsToAdd.Clear();
}
public override void loadInit()
{
base.loadInit();
rootFolder = comp.files.root.searchForFolder("ImageBoard");
threadsFolder = rootFolder.searchForFolder("Threads");
}
public override string getSaveString()
{
return "<MessageBoard name=\"" + name + "\"/>";
}
public void AddThread(string threadData)
{
if (threadsFolder == null)
{
ThreadsToAdd.Add(threadData);
}
else
{
string str;
do
{
str = Utils.getRandomByte().ToString("000") + Utils.getRandomByte().ToString("000") +
Utils.getRandomByte().ToString("000") + ".tm";
} while (threadsFolder.searchForFile(str) != null);
threadsFolder.files.Add(new FileEntry(threadData, str));
}
}
public MessageBoardThread ParseThread(string threadData)
{
var strArray = threadData.Split(new string[3]
{
"------------------------------------------\r\n",
"------------------------------------------\n",
"------------------------------------------"
}, StringSplitOptions.None);
var str1 = strArray[0].Replace("\n", "").Replace("\r", "");
var messageBoardThread = new MessageBoardThread
{
id = str1,
posts = new List<MessageBoardPost>()
};
for (var index = 1; index < strArray.Length; ++index)
{
if (strArray[index].Length > 1)
{
var messageBoardPost = new MessageBoardPost();
var str2 = strArray[index];
if (strArray[index].StartsWith("#"))
{
var str3 = strArray[index].Substring(1, strArray[index].IndexOf('\n'));
try
{
var messageBoardPostImage =
(MessageBoardPostImage) Enum.Parse(typeof (MessageBoardPostImage), str3);
messageBoardPost.img = messageBoardPostImage;
str2 = strArray[index].Substring(strArray[index].IndexOf('\n') + 1);
}
catch (ArgumentException ex)
{
messageBoardPost.img = MessageBoardPostImage.None;
}
}
else
messageBoardPost.img = MessageBoardPostImage.None;
messageBoardPost.text = str2;
messageBoardThread.posts.Add(messageBoardPost);
}
}
return messageBoardThread;
}
public void ViewThread(MessageBoardThread thread, int width, int margin, int ImageSize, int headerOffset)
{
CurrentThreadHeight = 20;
for (var index = 0; index < thread.posts.Count; ++index)
CurrentThreadHeight += MeasurePost(thread.posts[index], width, margin, ImageSize, headerOffset) +
margin*2;
state = MessageBoardState.Thread;
viewingThread = thread;
ThreadScrollPosition = Vector2.Zero;
}
public override void navigatedTo()
{
base.navigatedTo();
state = MessageBoardState.Board;
}
public override void draw(Rectangle bounds, SpriteBatch sb)
{
base.draw(bounds, sb);
var height = 60;
var dest = new Rectangle(bounds.X, bounds.Y, bounds.Width, height);
DrawHeader(sb, dest);
var rectangle = bounds;
rectangle.Y += height;
rectangle.Height -= height + 1;
switch (state)
{
case MessageBoardState.Thread:
DrawFullThreadView(sb, viewingThread, rectangle);
break;
default:
threadsPanel.NumberOfPanels = threadsFolder.files.Count;
threadsPanel.Draw(
(index, drawArea, sBatch) =>
DrawThread(ParseThread(threadsFolder.files[index].data), sBatch, drawArea, true), sb,
rectangle);
break;
}
}
private void DrawHeader(SpriteBatch sb, Rectangle dest)
{
var num1 = 4;
sb.Draw(Utils.white, new Rectangle(dest.X + num1, dest.Y + 4, dest.Width - num1*2, 1), Color.White*0.5f);
var position = new Vector2(dest.X + num1, dest.Y + 5);
var index = 0;
var num2 = 7;
while (position.X + (double) (num2*2) < dest.X + dest.Width && index < boardsListingString.Length)
{
sb.DrawString(GuiData.detailfont, string.Concat(boardsListingString[index]), position, Color.White*0.6f);
++index;
position.X += num2;
}
sb.DrawString(GuiData.detailfont, "]", position, Color.White*0.8f);
try
{
if (BoardName == null)
BoardName = name;
sb.DrawString(GuiData.font, BoardName, new Vector2(dest.X + num1, dest.Y + 19), os.highlightColor);
}
catch (Exception ex)
{
}
if (state != MessageBoardState.Board)
{
var width = 200;
if (Button.doButton(1931655802, dest.X + dest.Width - width - 6, dest.Y + dest.Height/2 - 4, width, 24,
"Back to Board", os.highlightColor))
state = MessageBoardState.Board;
}
sb.Draw(Utils.white, new Rectangle(dest.X + num1, dest.Y + dest.Height - 2, dest.Width - num1*2, 1),
Color.White*0.5f);
}
private void DrawFullThreadView(SpriteBatch sb, MessageBoardThread thread, Rectangle dest)
{
var rectangle = dest;
rectangle.Height = CurrentThreadHeight + 50;
var flag = CurrentThreadHeight > dest.Height;
if (flag)
{
ScrollablePanel.beginPanel(1931655001, rectangle, ThreadScrollPosition);
rectangle.X = rectangle.Y = 0;
}
DrawThread(thread, GuiData.spriteBatch, rectangle, false);
if (!flag)
return;
float maxScroll = Math.Max(dest.Height, CurrentThreadHeight - dest.Height);
ThreadScrollPosition = ScrollablePanel.endPanel(1931655001, ThreadScrollPosition, dest, maxScroll, false);
}
private void DrawThread(MessageBoardThread thread, SpriteBatch sb, Rectangle bounds, bool isPreview = false)
{
var num1 = 4;
var margin = 8;
var num2 = 450;
var height = 36;
var num3 = 20;
var ImageSize = 80;
var ThreadFooterSize = 30;
var font = GuiData.tinyfont;
var dest = new Rectangle(bounds.X + margin, bounds.Y + margin, num2, height);
var num4 = bounds.Y;
var list = thread.posts;
if (isPreview)
list = GetLastPostsToFitHeight(thread, 415 - ThreadFooterSize, bounds.Width, margin, ImageSize, num3,
ThreadFooterSize, int.MaxValue);
for (var index = 0; index < list.Count; ++index)
{
var messageBoardPost = list[index];
var width = bounds.Width - 4*margin;
if (messageBoardPost.img != MessageBoardPostImage.None)
width -= ImageSize;
var text1 = Utils.SmartTwimForWidth(messageBoardPost.text, width, font);
var vector2 = font.MeasureString(text1);
vector2.Y *= 1.3f;
dest.Y = num4;
var val2 = (int) vector2.X + margin*4;
if (messageBoardPost.img != MessageBoardPostImage.None)
val2 += ImageSize + margin;
dest.Width = Math.Max(num2, val2);
dest.Height = (int) vector2.Y + 2*margin;
dest.Width = Math.Max(dest.Width, ImageSize + 2*margin);
if (messageBoardPost.img != MessageBoardPostImage.None)
dest.Height = Math.Max(dest.Height, ImageSize + 2*margin);
dest.Height += num3 + margin*2;
DrawPost(text1, messageBoardPost.img, dest, margin, ImageSize, num3, sb, font);
num4 += dest.Height + num1;
if (index == 0 && isPreview)
{
var thread1 = thread;
var text2 = "[+] " + (thread.posts.Count - list.Count) + " posts and image replies omitted";
var text3 = "Click here to view.";
sb.DrawString(GuiData.tinyfont, text2, new Vector2(dest.X, num4), os.lightGray);
if (Button.doButton(17839000 + thread.id.GetHashCode(), dest.X + 290, num4 - 1, 160, 17, text3,
new Color?()))
{
Console.WriteLine("clicked " + index);
ViewThread(thread1, bounds.Width, margin, ImageSize, num3);
}
num4 += 16 + num1;
}
}
sb.Draw(Utils.white,
new Rectangle(bounds.X + margin, bounds.Y + bounds.Height - 6, bounds.Width - margin*2, 1),
Color.White*0.5f);
}
private int MeasurePost(MessageBoardPost post, int width, int margin, int ImageSize, int postHeaderOffset)
{
var font = GuiData.tinyfont;
var num = postHeaderOffset;
var width1 = width - 4*margin;
if (post.img != MessageBoardPostImage.None)
width1 -= ImageSize;
var text = Utils.SmartTwimForWidth(post.text, width1, font);
var vector2 = font.MeasureString(text);
vector2.Y *= 1.3f;
return Math.Max(num + (int) vector2.Y + 2*margin, ImageSize + 2*margin);
}
private void DrawPost(string text, MessageBoardPostImage img, Rectangle dest, int margin, int ImageSize,
int postheaderOffset, SpriteBatch sb, SpriteFont font)
{
sb.Draw(Utils.white, dest, os.highlightColor*0.2f);
var vector2 = new Vector2(dest.X + margin, dest.Y + margin);
sb.Draw(Utils.white,
new Rectangle((int) vector2.X, (int) vector2.Y, postheaderOffset - 5, postheaderOffset - 5),
os.indentBackgroundColor);
sb.DrawString(GuiData.smallfont, "Anonymous", vector2 + new Vector2(postheaderOffset, -2f), UsernameColor);
sb.DrawString(GuiData.detailfont, "01/01/1970(Thu)00:00 UTC+0:0", vector2 + new Vector2(112f, 3f),
Utils.SlightlyDarkGray);
vector2.Y += postheaderOffset;
if (img != MessageBoardPostImage.None && Images.ContainsKey(img))
{
var destinationRectangle = new Rectangle(dest.X + margin, dest.Y + margin + postheaderOffset, ImageSize,
ImageSize);
sb.Draw(Images[img], destinationRectangle, Color.White);
vector2.X += ImageSize + margin + margin;
}
var strArray = text.Split(Utils.newlineDelim);
var num = font.MeasureString(strArray[0]).Y;
for (var index = 0; index < strArray.Length; ++index)
sb.DrawString(font, strArray[index], vector2 + new Vector2(0.0f, index*(num + 2f)),
strArray[index].StartsWith(">") ? ImplicationColor : Color.White);
}
private List<MessageBoardPost> GetLastPostsToFitHeight(MessageBoardThread thread, int height, int width,
int margin, int ImageSize, int PostHeaderOffset, int ThreadFooterSize, int maxOPSize = 2147483647)
{
var list = new List<MessageBoardPost>();
var num1 = ThreadFooterSize + margin*4;
var val2 = MeasurePost(thread.posts[0], width, margin, ImageSize, PostHeaderOffset);
var num2 = Math.Min(maxOPSize, val2);
list.Add(thread.posts[0]);
var num3 = num1 + num2;
for (var index = thread.posts.Count - 1; index >= 0; --index)
{
var num4 = MeasurePost(thread.posts[index], width, margin, ImageSize, PostHeaderOffset);
if (num3 + num4 < height)
{
list.Insert(1, thread.posts[index]);
num3 += num4;
}
else
break;
}
return list;
}
private enum MessageBoardState
{
Thread,
Board
}
}
}