-
Notifications
You must be signed in to change notification settings - Fork 592
Expand file tree
/
Copy pathPaperSystem.cs
More file actions
558 lines (471 loc) · 20.2 KB
/
Copy pathPaperSystem.cs
File metadata and controls
558 lines (471 loc) · 20.2 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
using System.Linq;
using Content.Shared.Administration.Logs;
using Content.Shared.UserInterface;
using Content.Shared.Database;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Random.Helpers;
using Content.Shared.Popups;
using Content.Shared.Tag;
using Content.Shared.Verbs; // Umbra - Pen signing
using Robust.Shared.Player;
using Robust.Shared.Audio.Systems;
using static Content.Shared.Paper.PaperComponent;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
// Starlight-start
using Content.Shared.IdentityManagement;
using Content.Shared.IdentityManagement.Components;
using Content.Shared.Mind.Components;
using Content.Shared.Roles;
// Starlight-end
namespace Content.Shared.Paper;
public sealed class PaperSystem : EntitySystem
{
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly IPrototypeManager _protoMan = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedInteractionSystem _interaction = default!;
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
[Dependency] private readonly TagSystem _tagSystem = default!;
[Dependency] private readonly SharedUserInterfaceSystem _uiSystem = default!;
[Dependency] private readonly MetaDataSystem _metaSystem = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedIdentitySystem _identitySystem = default!; // Starlight-edit
private static readonly ProtoId<TagPrototype> WriteIgnoreStampsTag = "WriteIgnoreStamps";
private static readonly ProtoId<TagPrototype> WriteTag = "Write";
private EntityQuery<PaperComponent> _paperQuery;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PaperComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<PaperComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<PaperComponent, BeforeActivatableUIOpenEvent>(BeforeUIOpen);
SubscribeLocalEvent<PaperComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<PaperComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<PaperComponent, PaperInputTextMessage>(OnInputTextMessage);
SubscribeLocalEvent<RandomPaperContentComponent, MapInitEvent>(OnRandomPaperContentMapInit);
SubscribeLocalEvent<ActivateOnPaperOpenedComponent, PaperWriteEvent>(OnPaperWrite);
// Umbra - Signing alt verb event listener.
SubscribeLocalEvent<PaperComponent, GetVerbsEvent<AlternativeVerb>>(AddSignVerb);
SubscribeLocalEvent<PaperComponent, PaperSignatureRequestMessage>(OnSignatureRequest); // Starlight-edit
_paperQuery = GetEntityQuery<PaperComponent>();
}
private void OnMapInit(Entity<PaperComponent> entity, ref MapInitEvent args)
{
if (!string.IsNullOrEmpty(entity.Comp.Content))
{
SetContent(entity, Loc.GetString(entity.Comp.Content));
}
}
private void OnInit(Entity<PaperComponent> entity, ref ComponentInit args)
{
entity.Comp.Mode = PaperAction.Read;
UpdateUserInterface(entity);
if (TryComp<AppearanceComponent>(entity, out var appearance))
{
if (entity.Comp.Content != "")
_appearance.SetData(entity, PaperVisuals.Status, PaperStatus.Written, appearance);
if (entity.Comp.StampState != null)
_appearance.SetData(entity, PaperVisuals.Stamp, entity.Comp.StampState, appearance);
}
}
private void BeforeUIOpen(Entity<PaperComponent> entity, ref BeforeActivatableUIOpenEvent args)
{
entity.Comp.Mode = PaperAction.Read;
UpdateUserInterface(entity);
}
private void OnExamined(Entity<PaperComponent> entity, ref ExaminedEvent args)
{
if (!args.IsInDetailsRange)
return;
using (args.PushGroup(nameof(PaperComponent)))
{
if (entity.Comp.Content != "")
{
args.PushMarkup(
Loc.GetString(
"paper-component-examine-detail-has-words",
("paper", entity)
)
);
}
if (entity.Comp.StampedBy.Count > 0)
{
// Umbra: Separate into stamps and signatures.
var stamps = entity.Comp.StampedBy.FindAll(s => s.Type == StampType.RubberStamp);
var signatures = entity.Comp.StampedBy.FindAll(s => s.Type == StampType.Signature);
// Umbra: If we have stamps, render them.
if (stamps.Count > 0)
{
var joined = string.Join(", ", stamps.Select(s => Loc.GetString(s.StampedName)));
args.PushMarkup(
Loc.GetString(
"paper-component-examine-detail-stamped-by",
("paper", entity),
("stamps", joined)
)
);
}
// Umbra: Ditto for signatures.
if (signatures.Count > 0)
{
var joined = string.Join(", ", signatures.Select(s => s.StampedName));
args.PushMarkup(
Loc.GetString(
"paper-component-examine-detail-signed-by",
("paper", entity),
("stamps", joined)
)
);
}
}
}
}
private void OnInteractUsing(Entity<PaperComponent> entity, ref InteractUsingEvent args)
{
// only allow editing if there are no stamps or when using a cyberpen
var editable = entity.Comp.StampedBy.Count == 0 || _tagSystem.HasTag(args.Used, WriteIgnoreStampsTag);
if (_tagSystem.HasTag(args.Used, WriteTag))
{
if (editable)
{
if (entity.Comp.EditingDisabled)
{
var paperEditingDisabledMessage = Loc.GetString("paper-tamper-proof-modified-message");
_popupSystem.PopupClient(paperEditingDisabledMessage, entity, args.User);
args.Handled = true;
return;
}
var ev = new PaperWriteAttemptEvent(entity.Owner);
RaiseLocalEvent(args.User, ref ev);
if (ev.Cancelled)
{
if (ev.FailReason is not null)
{
var fileWriteMessage = Loc.GetString(ev.FailReason);
_popupSystem.PopupClient(fileWriteMessage, entity.Owner, args.User);
}
args.Handled = true;
return;
}
var writeEvent = new PaperWriteEvent(args.User, entity);
RaiseLocalEvent(args.Used, ref writeEvent);
entity.Comp.Mode = PaperAction.Write;
_uiSystem.OpenUi(entity.Owner, PaperUiKey.Key, args.User);
UpdateUserInterface(entity);
}
args.Handled = true;
return;
}
// If a stamp, attempt to stamp paper
if (TryComp<StampComponent>(args.Used, out var stampComp) &&
TryStamp(entity, GetStampInfo(stampComp), stampComp.StampState))
{
// successfully stamped, play popup
var stampPaperOtherMessage = Loc.GetString("paper-component-action-stamp-paper-other",
("user", args.User),
("target", args.Target),
("stamp", args.Used));
_popupSystem.PopupEntity(stampPaperOtherMessage,
args.User,
Filter.PvsExcept(args.User, entityManager: EntityManager),
true);
var stampPaperSelfMessage = Loc.GetString("paper-component-action-stamp-paper-self",
("target", args.Target),
("stamp", args.Used));
_popupSystem.PopupClient(stampPaperSelfMessage, args.User, args.User);
_audio.PlayPredicted(stampComp.Sound, entity, args.User);
UpdateUserInterface(entity);
}
}
private static StampDisplayInfo GetStampInfo(StampComponent stamp)
{
return new StampDisplayInfo
{
StampedName = stamp.StampedName,
StampedColor = stamp.StampedColor
};
}
private void OnInputTextMessage(Entity<PaperComponent> entity, ref PaperInputTextMessage args)
{
var ev = new PaperWriteAttemptEvent(entity.Owner);
RaiseLocalEvent(args.Actor, ref ev);
if (ev.Cancelled)
return;
if (args.Text.Length <= entity.Comp.ContentSize)
{
SetContent(entity, args.Text);
var paperStatus = string.IsNullOrWhiteSpace(args.Text) ? PaperStatus.Blank : PaperStatus.Written;
if (TryComp<AppearanceComponent>(entity, out var appearance))
_appearance.SetData(entity, PaperVisuals.Status, paperStatus, appearance);
if (TryComp(entity, out MetaDataComponent? meta))
_metaSystem.SetEntityDescription(entity, "", meta);
_adminLogger.Add(LogType.Chat,
LogImpact.Low,
$"{ToPrettyString(args.Actor):player} has written on {ToPrettyString(entity):entity} the following text: {args.Text}");
_audio.PlayPvs(entity.Comp.Sound, entity);
}
entity.Comp.Mode = PaperAction.Read;
UpdateUserInterface(entity);
}
private void OnRandomPaperContentMapInit(Entity<RandomPaperContentComponent> ent, ref MapInitEvent args)
{
if (!_paperQuery.TryComp(ent, out var paperComp))
{
Log.Warning($"{ToPrettyString(ent)} has a {nameof(RandomPaperContentComponent)} but no {nameof(PaperComponent)}!");
RemCompDeferred(ent, ent.Comp);
return;
}
var dataset = _protoMan.Index(ent.Comp.Dataset);
// Intentionally not using the Pick overload that directly takes a LocalizedDataset,
// because we want to get multiple attributes from the same pick.
var pick = _random.Pick(dataset.Values);
// Name
_metaSystem.SetEntityName(ent, Loc.GetString(pick));
// Description
_metaSystem.SetEntityDescription(ent, Loc.GetString($"{pick}.desc"));
// Content
SetContent((ent, paperComp), Loc.GetString($"{pick}.content"));
// Our work here is done
RemCompDeferred(ent, ent.Comp);
}
private void OnPaperWrite(Entity<ActivateOnPaperOpenedComponent> entity, ref PaperWriteEvent args)
{
_interaction.UseInHandInteraction(args.User, entity);
}
/// <summary>
/// Accepts the name and state to be stamped onto the paper, returns true if successful.
/// </summary>
public bool TryStamp(Entity<PaperComponent> entity, StampDisplayInfo stampInfo, string spriteStampState)
{
if (!entity.Comp.StampedBy.Contains(stampInfo))
{
entity.Comp.StampedBy.Add(stampInfo);
// Starlight-start: Clean unfilled form and signature tags when stamping to finalize the document
var cleanedContent = CleanUnfilledTags(entity.Comp.Content);
if (cleanedContent != entity.Comp.Content)
SetContent(entity, cleanedContent);
// Starlight-end
Dirty(entity);
if (entity.Comp.StampState == null && TryComp<AppearanceComponent>(entity, out var appearance))
{
entity.Comp.StampState = spriteStampState;
// Would be nice to be able to display multiple sprites on the paper
// but most of the existing images overlap
_appearance.SetData(entity, PaperVisuals.Stamp, entity.Comp.StampState, appearance);
}
}
return true;
}
// BEGIN OF UMBRA ADDITIONS
// Umbra: Send paper signing alt verb to the client if applicable.
// Based on LockSystem.cs for alt-click behavior.
private void AddSignVerb(Entity<PaperComponent> uid, ref GetVerbsEvent<AlternativeVerb> args)
{
if (!args.CanAccess || !args.CanInteract)
return;
// Pens have a `Write` tag.
if (!args.Using.HasValue || !_tagSystem.HasTag(args.Using.Value, "Write"))
return;
EntityUid user = args.User;
AlternativeVerb verb = new()
{
Act = () =>
{
TrySign(uid, user);
},
Text = Loc.GetString("paper-component-verb-sign"),
Priority = 4
// Icon = Don't have an icon yet. Todo for later.
};
args.Verbs.Add(verb);
}
// Umbra: Actual signature code.
public bool TrySign(Entity<PaperComponent> paper, EntityUid signer)
{
// Generate display information.
StampDisplayInfo info = new StampDisplayInfo
{
StampedName = Name(signer),
StampedColor = Color.FromHex("#333333"),
Type = StampType.Signature,
Font = "/Fonts/_Starlight/Signature.ttf" // 🌟Starlight🌟
};
// Try stamp with the info, return false if failed.
if (TryStamp(paper, info, "paper_stamp-generic"))
{
_popupSystem.PopupClient(
Loc.GetString(
"paper-component-action-signed-self",
("target", paper)
),
signer,
signer
);
_popupSystem.PopupEntity(
Loc.GetString(
"paper-component-action-signed-other",
("user", signer),
("target", paper)
),
paper,
Filter.PvsExcept(signer, entityManager: EntityManager),
true
);
_audio.PlayPvs(paper.Comp.Sound, paper);
_adminLogger.Add(LogType.Verb,
LogImpact.Low,
$"{ToPrettyString(signer):player} has signed {ToPrettyString(paper):paper}.");
UpdateUserInterface(paper);
// #region Starlight
var eve = new PaperSignedEvent(signer);
RaiseLocalEvent(paper, ref eve);
// #endregion
return true;
}
return false;
}
// END OF UMBRA ADDITIONS
/// <summary>
/// Copy any stamp information from one piece of paper to another.
/// </summary>
public void CopyStamps(Entity<PaperComponent?> source, Entity<PaperComponent?> target)
{
if (!Resolve(source, ref source.Comp) || !Resolve(target, ref target.Comp))
return;
target.Comp.StampedBy = new List<StampDisplayInfo>(source.Comp.StampedBy);
target.Comp.StampState = source.Comp.StampState;
Dirty(target);
if (TryComp<AppearanceComponent>(target, out var appearance))
{
// delete any stamps if the stamp state is null
_appearance.SetData(target, PaperVisuals.Stamp, target.Comp.StampState ?? "", appearance);
}
}
public void SetContent(EntityUid entity, string content)
{
if (!TryComp<PaperComponent>(entity, out var paper))
return;
SetContent((entity, paper), content);
}
public void SetContent(Entity<PaperComponent> entity, string content)
{
entity.Comp.Content = content;
Dirty(entity);
UpdateUserInterface(entity);
if (!TryComp<AppearanceComponent>(entity, out var appearance))
return;
var status = string.IsNullOrWhiteSpace(content)
? PaperStatus.Blank
: PaperStatus.Written;
_appearance.SetData(entity, PaperVisuals.Status, status, appearance);
}
private void UpdateUserInterface(Entity<PaperComponent> entity)
{
_uiSystem.SetUiState(entity.Owner, PaperUiKey.Key, new PaperBoundUserInterfaceState(entity.Comp.Content, entity.Comp.StampedBy, entity.Comp.Mode)); // Starlight-edit
}
# region Starlight
private void OnSignatureRequest(Entity<PaperComponent> entity, ref PaperSignatureRequestMessage args)
{
var signature = GetPlayerSignature(args.Actor);
var newText = ReplaceNthSignatureTag(entity.Comp.Content, args.SignatureIndex, signature);
SetContent(entity, newText);
_adminLogger.Add(LogType.Chat, LogImpact.Low,
$"{ToPrettyString(args.Actor):player} signed {ToPrettyString(entity):entity} with signature: {signature}");
}
/// <summary>
/// Gets the player's signature using the identity system, including rank, name, and role.
/// </summary>
private string GetPlayerSignature(EntityUid player)
{
var name = string.Empty;
var rank = string.Empty;
var role = string.Empty;
// Get the identity entity (ID card, etc.)
var identityEntity = player;
if (TryComp<IdentityComponent>(player, out var identity) &&
identity.IdentityEntitySlot.ContainedEntity is { } idEntity)
{
identityEntity = idEntity;
}
// Get name from identity or fallback to entity name
name = MetaData(identityEntity).EntityName;
// Get role from mind system
if (TryComp<MindContainerComponent>(player, out var mindContainer) &&
mindContainer.Mind != null)
{
var roleSystem = EntityManager.System<SharedRoleSystem>();
var roleInfo = roleSystem.MindGetAllRoleInfo((mindContainer.Mind.Value, null));
if (roleInfo.Count > 0)
{
role = Loc.GetString(roleInfo[0].Name);
}
}
// Format: "Rank Name, Role" or fallback combinations
var signature = string.Empty;
if (!string.IsNullOrEmpty(rank) && !string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(role))
{
signature = $"{rank} {name}, {role}";
}
else if (!string.IsNullOrEmpty(rank) && !string.IsNullOrEmpty(name))
{
signature = $"{rank} {name}";
}
else if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(role))
{
signature = $"{name}, {role}";
}
else
{
signature = name;
}
return signature;
}
/// <summary>
/// Replaces the nth occurrence of [signature] tag with replacement text.
/// </summary>
private static string ReplaceNthSignatureTag(string text, int index, string replacement)
{
const string signatureTag = "[signature]";
var currentIndex = 0;
var pos = 0;
while (pos < text.Length)
{
var foundPos = text.IndexOf(signatureTag, pos);
if (foundPos == -1) break;
if (currentIndex == index)
{
return text.Substring(0, foundPos) + replacement + text.Substring(foundPos + signatureTag.Length);
}
currentIndex++;
pos = foundPos + signatureTag.Length;
}
return text;
}
/// <summary>
/// Removes any unfilled [form] and [signature] tags, and converts [check] tags to ☐.
/// Called when the paper is stamped to finalize the document.
/// </summary>
/// <param name="text">The paper text to clean</param>
/// <returns>Text with unfilled tags cleaned</returns>
private static string CleanUnfilledTags(string text)
{
return text.Replace("[form]", string.Empty)
.Replace("[signature]", string.Empty)
.Replace("[check]", "☐");
}
# endregion
}
/// <summary>
/// Event fired when using a pen on paper, opening the UI.
/// </summary>
[ByRefEvent]
public record struct PaperWriteEvent(EntityUid User, EntityUid Paper);
/// <summary>
/// Cancellable event for attempting to write on a piece of paper.
/// </summary>
/// <param name="paper">The paper that the writing will take place on.</param>
[ByRefEvent]
public record struct PaperWriteAttemptEvent(EntityUid Paper, string? FailReason = null, bool Cancelled = false);