Skip to content

Commit 0e64ea1

Browse files
committed
Port #79 (add chat color customizability to groups) to 1.4
1 parent e6687ba commit 0e64ea1

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

HEROsMod.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using HEROsMod.HEROsModNetwork;
1+
using On.Terraria.GameContent.NetModules;
2+
using HEROsMod.HEROsModNetwork;
23
using HEROsMod.HEROsModServices;
34
using HEROsMod.UIKit;
45
using Microsoft.Xna.Framework;
@@ -11,10 +12,12 @@
1112
using System.Linq;
1213
using System.Reflection;
1314
using Terraria;
15+
using Terraria.Chat;
1416
using Terraria.GameContent;
1517
using Terraria.Localization;
1618
using Terraria.ModLoader;
1719
using Terraria.UI;
20+
using ReLogic.Content.Sources;
1821

1922
// TODO, freeze is bypassable.
2023
// TODO, regions prevent all the chest movement and right click.
@@ -62,6 +65,8 @@ public override void Load()
6265
{
6366
ModUtils.DebugText("Load:\n" + e.Message + "\n" + e.StackTrace + "\n");
6467
}
68+
// Intercept DeserializeAsServer method
69+
NetTextModule.DeserializeAsServer += NetTextModule_DeserializeAsServer;
6570
}
6671

6772
internal static string HeroText(string key)
@@ -122,6 +127,20 @@ public override void Unload()
122127
ModUtils.previousInventoryItems = null;
123128
translations = null;
124129
instance = null;
130+
NetTextModule.DeserializeAsServer -= NetTextModule_DeserializeAsServer;
131+
}
132+
133+
private bool NetTextModule_DeserializeAsServer(NetTextModule.orig_DeserializeAsServer orig, Terraria.GameContent.NetModules.NetTextModule self, BinaryReader reader, int senderPlayerId)
134+
{
135+
long savedPosition = reader.BaseStream.Position;
136+
ChatMessage message = ChatMessage.Deserialize(reader);
137+
reader.BaseStream.Position = savedPosition;
138+
139+
Color chatColor = Network.Players[senderPlayerId].Group?.Color ?? new Color(255, 255, 255);
140+
Terraria.Net.NetPacket packet = Terraria.GameContent.NetModules.NetTextModule.SerializeServerMessage(NetworkText.FromLiteral(message.Text), chatColor, (byte)senderPlayerId);
141+
Terraria.Net.NetManager.Instance.Broadcast(packet);
142+
143+
return true;
125144
}
126145

127146
public override void PostSetupContent()

HEROsModNetwork/DatabaseController.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Newtonsoft.Json;
44
using System;
55
using System.Collections.Generic;
6+
using System.ComponentModel;
67
using System.IO;
78
using System.Linq;
89
using Terraria;
@@ -38,6 +39,9 @@ public class DatabaseGroup
3839
{
3940
public int ID;
4041
public string name;
42+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
43+
[DefaultValue(typeof(Color), "255, 255, 255, 255")]
44+
public Color color;
4145

4246
//public byte[] permissions;
4347
public string[] permissions;
@@ -455,7 +459,7 @@ private static bool HasDefaultGroup()
455459
public static void AddGroup(ref Group group)
456460
{
457461
int newid = GetAvailableGroupID();
458-
DatabaseGroup newGroup = new DatabaseGroup() { name = group.Name, ID = newid };
462+
DatabaseGroup newGroup = new DatabaseGroup() { name = group.Name, ID = newid, color = group.Color };
459463
database.groups.Add(newGroup);
460464

461465
group.ID = newid;
@@ -486,6 +490,7 @@ public static void SetGroupPermissions(Group group)
486490
if (g != null)
487491
{
488492
g.permissions = group.Permissions.Where(x => x.Value).Select(x => x.Key).ToArray();//group.ExportPermissions();
493+
g.color = group.Color;
489494
}
490495
SaveSetting(jsonDatabaseFilename);
491496
}
@@ -497,6 +502,7 @@ public static List<Group> GetGroups()
497502
{
498503
Group group = new Group(dbGroup.name);
499504
group.ID = dbGroup.ID;
505+
group.Color = dbGroup.color;
500506
group.ImportPermissions(dbGroup.permissions);
501507
result.Add(group);
502508
}

HEROsModNetwork/Group.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Microsoft.Xna.Framework;
2+
using System;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
@@ -63,6 +64,8 @@ public bool IsAdmin
6364
set { _isAdmin = value; }
6465
}
6566

67+
public Color Color { get; set; }
68+
6669
public Group(string name)
6770
{
6871
ID = -1;
@@ -78,6 +81,7 @@ public Group(string name)
7881
{
7982
Network.DefaultGroup = this;
8083
}
84+
this.Color = new Color(255, 255, 255);
8185
}
8286

8387
public bool HasPermission(string permissionName)

HEROsModNetwork/LoginService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ public static void SendGroupList(int playerNumber)
345345
{
346346
Writer.Write(Network.Groups[i].Name);
347347
Writer.Write(Network.Groups[i].ID);
348+
Writer.WriteRGB(Network.Groups[i].Color);
348349
byte[] permissions = Network.Groups[i].ExportPermissions();
349350
Writer.Write(permissions.Length);
350351
Writer.Write(permissions);
@@ -364,6 +365,7 @@ private static void ProcessGroupList(ref BinaryReader reader)
364365
string groupName = reader.ReadString();
365366
Group group = new Group(groupName);
366367
group.ID = reader.ReadInt32();
368+
group.Color = reader.ReadRGB();
367369
int permissionsLength = reader.ReadInt32();
368370
group.ImportPermissions(reader.ReadBytes(permissionsLength));
369371
Network.Groups.Add(group);
@@ -395,6 +397,7 @@ public static void SendPlayerPermissions(int playerNumber)
395397
Writer.Write(group.Name);
396398
Writer.Write(group.ID);
397399
Writer.Write(group.IsAdmin);
400+
Writer.WriteRGB(group.Color);
398401
byte[] permissions = group.ExportPermissions();
399402
//if(CTF.CaptureTheFlag.GameInProgress)
400403
//{
@@ -420,6 +423,7 @@ private static void ProcessGroupPermissions(ref BinaryReader reader)
420423
group.IsAdmin = true;
421424
//group.MakeAdmin();
422425
}
426+
group.Color = reader.ReadRGB();
423427
int permissionsLength = reader.ReadInt32();
424428
group.ImportPermissions(reader.ReadBytes(permissionsLength));
425429

@@ -435,6 +439,7 @@ public static void RequestSetGroupPermissions(Group group)
435439
byte[] permissions = group.ExportPermissions();
436440
Writer.Write(permissions.Length);
437441
Writer.Write(permissions);
442+
Writer.WriteRGB(group.Color);
438443
Network.SendDataToServer();
439444
}
440445

@@ -447,6 +452,7 @@ private static void ProcessSetGroupPermissionsRequest(ref BinaryReader reader, i
447452
Group group = Network.GetGroupByID(id);
448453
int permissionsLength = reader.ReadInt32();
449454
group.ImportPermissions(reader.ReadBytes(permissionsLength));
455+
group.Color = reader.ReadRGB();
450456
DatabaseController.SetGroupPermissions(group);
451457

452458
for (int i = 0; i < Network.Players.Length; i++)

HEROsModServices/GroupInspector.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ internal class GroupManagementWindow : UIWindow
7878

7979
//Group group;
8080
private UIDropdown dropdown = new UIDropdown();
81+
private UIColorPicker colorPicker = new UIKit.UIColorPicker();
8182

8283
private UIScrollView checkboxContainer = new UIScrollView();
8384

@@ -108,8 +109,10 @@ public GroupManagementWindow()
108109
dropdown.X = label.X + label.Width + 4;
109110
dropdown.Y = label.Y;
110111
dropdown.Width = 200;
112+
colorPicker.X = dropdown.X + dropdown.Width + spacing;
113+
colorPicker.Y = dropdown.Y;
111114
checkboxContainer.X = spacing;
112-
checkboxContainer.Y = dropdown.Y + dropdown.Height + spacing;
115+
checkboxContainer.Y = colorPicker.Y + colorPicker.Height + spacing;
113116
checkboxContainer.Width = this.Width - spacing * 2;
114117
checkboxContainer.Height = 150;
115118
AddChild(checkboxContainer);
@@ -140,6 +143,7 @@ public GroupManagementWindow()
140143
AddChild(bNew);
141144
AddChild(bDelete);
142145
AddChild(dropdown);
146+
AddChild(colorPicker);
143147

144148
this.Height = bApply.Position.Y + bApply.Height + spacing;
145149
this.CenterToParent();
@@ -171,6 +175,7 @@ private void bApply_onLeftClick(object sender, EventArgs e)
171175
{
172176
HEROsModNetwork.Group group = new HEROsModNetwork.Group(dropdown.GetItem(dropdown.SelectedItem));
173177
group.ID = HEROsModNetwork.Network.Groups[dropdown.SelectedItem].ID;
178+
group.Color = colorPicker.Color;
174179
group.ImportPermissions(ExportPermissions());
175180
HEROsModNetwork.LoginService.RequestSetGroupPermissions(group);
176181
}
@@ -205,6 +210,7 @@ public void RefreshGroupPermissions()
205210
}
206211
if (checkboxContainer.ChildCount > 0)
207212
checkboxContainer.ContentHeight = checkboxContainer.GetLastChild().Y + checkboxContainer.GetLastChild().Height + spacing;
213+
colorPicker.Color = HEROsModNetwork.Network.Groups[dropdown.SelectedItem].Color;
208214
}
209215

210216
private byte[] ExportPermissions()

0 commit comments

Comments
 (0)