diff --git a/HEROsMod.cs b/HEROsMod.cs index 808bf13..8b601d2 100644 --- a/HEROsMod.cs +++ b/HEROsMod.cs @@ -1,4 +1,5 @@ -using HEROsMod.HEROsModNetwork; +using On.Terraria.GameContent.NetModules; +using HEROsMod.HEROsModNetwork; using HEROsMod.HEROsModServices; using HEROsMod.UIKit; using Microsoft.Xna.Framework; @@ -11,10 +12,12 @@ using System.Linq; using System.Reflection; using Terraria; +using Terraria.Chat; using Terraria.GameContent; using Terraria.Localization; using Terraria.ModLoader; using Terraria.UI; +using ReLogic.Content.Sources; // TODO, freeze is bypassable. // TODO, regions prevent all the chest movement and right click. @@ -62,6 +65,8 @@ public override void Load() { ModUtils.DebugText("Load:\n" + e.Message + "\n" + e.StackTrace + "\n"); } + // Intercept DeserializeAsServer method + NetTextModule.DeserializeAsServer += NetTextModule_DeserializeAsServer; } internal static string HeroText(string key) @@ -122,6 +127,20 @@ public override void Unload() ModUtils.previousInventoryItems = null; translations = null; instance = null; + NetTextModule.DeserializeAsServer -= NetTextModule_DeserializeAsServer; + } + + private bool NetTextModule_DeserializeAsServer(NetTextModule.orig_DeserializeAsServer orig, Terraria.GameContent.NetModules.NetTextModule self, BinaryReader reader, int senderPlayerId) + { + long savedPosition = reader.BaseStream.Position; + ChatMessage message = ChatMessage.Deserialize(reader); + reader.BaseStream.Position = savedPosition; + + Color chatColor = Network.Players[senderPlayerId].Group?.Color ?? new Color(255, 255, 255); + Terraria.Net.NetPacket packet = Terraria.GameContent.NetModules.NetTextModule.SerializeServerMessage(NetworkText.FromLiteral(message.Text), chatColor, (byte)senderPlayerId); + Terraria.Net.NetManager.Instance.Broadcast(packet); + + return true; } public override void PostSetupContent() diff --git a/HEROsModNetwork/DatabaseController.cs b/HEROsModNetwork/DatabaseController.cs index dc48a16..7f00a4c 100644 --- a/HEROsModNetwork/DatabaseController.cs +++ b/HEROsModNetwork/DatabaseController.cs @@ -3,6 +3,7 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; using System.Linq; using Terraria; @@ -38,6 +39,9 @@ public class DatabaseGroup { public int ID; public string name; + [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] + [DefaultValue(typeof(Color), "255, 255, 255, 255")] + public Color color; //public byte[] permissions; public string[] permissions; @@ -455,7 +459,7 @@ private static bool HasDefaultGroup() public static void AddGroup(ref Group group) { int newid = GetAvailableGroupID(); - DatabaseGroup newGroup = new DatabaseGroup() { name = group.Name, ID = newid }; + DatabaseGroup newGroup = new DatabaseGroup() { name = group.Name, ID = newid, color = group.Color }; database.groups.Add(newGroup); group.ID = newid; @@ -486,6 +490,7 @@ public static void SetGroupPermissions(Group group) if (g != null) { g.permissions = group.Permissions.Where(x => x.Value).Select(x => x.Key).ToArray();//group.ExportPermissions(); + g.color = group.Color; } SaveSetting(jsonDatabaseFilename); } @@ -497,6 +502,7 @@ public static List GetGroups() { Group group = new Group(dbGroup.name); group.ID = dbGroup.ID; + group.Color = dbGroup.color; group.ImportPermissions(dbGroup.permissions); result.Add(group); } diff --git a/HEROsModNetwork/Group.cs b/HEROsModNetwork/Group.cs index 6e0d68d..52b5e89 100644 --- a/HEROsModNetwork/Group.cs +++ b/HEROsModNetwork/Group.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Xna.Framework; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -63,6 +64,8 @@ public bool IsAdmin set { _isAdmin = value; } } + public Color Color { get; set; } + public Group(string name) { ID = -1; @@ -78,6 +81,7 @@ public Group(string name) { Network.DefaultGroup = this; } + this.Color = new Color(255, 255, 255); } public bool HasPermission(string permissionName) diff --git a/HEROsModNetwork/LoginService.cs b/HEROsModNetwork/LoginService.cs index b52672b..2f218e4 100644 --- a/HEROsModNetwork/LoginService.cs +++ b/HEROsModNetwork/LoginService.cs @@ -345,6 +345,7 @@ public static void SendGroupList(int playerNumber) { Writer.Write(Network.Groups[i].Name); Writer.Write(Network.Groups[i].ID); + Writer.WriteRGB(Network.Groups[i].Color); byte[] permissions = Network.Groups[i].ExportPermissions(); Writer.Write(permissions.Length); Writer.Write(permissions); @@ -364,6 +365,7 @@ private static void ProcessGroupList(ref BinaryReader reader) string groupName = reader.ReadString(); Group group = new Group(groupName); group.ID = reader.ReadInt32(); + group.Color = reader.ReadRGB(); int permissionsLength = reader.ReadInt32(); group.ImportPermissions(reader.ReadBytes(permissionsLength)); Network.Groups.Add(group); @@ -395,6 +397,7 @@ public static void SendPlayerPermissions(int playerNumber) Writer.Write(group.Name); Writer.Write(group.ID); Writer.Write(group.IsAdmin); + Writer.WriteRGB(group.Color); byte[] permissions = group.ExportPermissions(); //if(CTF.CaptureTheFlag.GameInProgress) //{ @@ -420,6 +423,7 @@ private static void ProcessGroupPermissions(ref BinaryReader reader) group.IsAdmin = true; //group.MakeAdmin(); } + group.Color = reader.ReadRGB(); int permissionsLength = reader.ReadInt32(); group.ImportPermissions(reader.ReadBytes(permissionsLength)); @@ -435,6 +439,7 @@ public static void RequestSetGroupPermissions(Group group) byte[] permissions = group.ExportPermissions(); Writer.Write(permissions.Length); Writer.Write(permissions); + Writer.WriteRGB(group.Color); Network.SendDataToServer(); } @@ -447,6 +452,7 @@ private static void ProcessSetGroupPermissionsRequest(ref BinaryReader reader, i Group group = Network.GetGroupByID(id); int permissionsLength = reader.ReadInt32(); group.ImportPermissions(reader.ReadBytes(permissionsLength)); + group.Color = reader.ReadRGB(); DatabaseController.SetGroupPermissions(group); for (int i = 0; i < Network.Players.Length; i++) diff --git a/HEROsModServices/GroupInspector.cs b/HEROsModServices/GroupInspector.cs index 76660fb..b6c0243 100644 --- a/HEROsModServices/GroupInspector.cs +++ b/HEROsModServices/GroupInspector.cs @@ -78,6 +78,7 @@ internal class GroupManagementWindow : UIWindow //Group group; private UIDropdown dropdown = new UIDropdown(); + private UIColorPicker colorPicker = new UIKit.UIColorPicker(); private UIScrollView checkboxContainer = new UIScrollView(); @@ -108,8 +109,10 @@ public GroupManagementWindow() dropdown.X = label.X + label.Width + 4; dropdown.Y = label.Y; dropdown.Width = 200; + colorPicker.X = dropdown.X + dropdown.Width + spacing; + colorPicker.Y = dropdown.Y; checkboxContainer.X = spacing; - checkboxContainer.Y = dropdown.Y + dropdown.Height + spacing; + checkboxContainer.Y = colorPicker.Y + colorPicker.Height + spacing; checkboxContainer.Width = this.Width - spacing * 2; checkboxContainer.Height = 150; AddChild(checkboxContainer); @@ -140,6 +143,7 @@ public GroupManagementWindow() AddChild(bNew); AddChild(bDelete); AddChild(dropdown); + AddChild(colorPicker); this.Height = bApply.Position.Y + bApply.Height + spacing; this.CenterToParent(); @@ -171,6 +175,7 @@ private void bApply_onLeftClick(object sender, EventArgs e) { HEROsModNetwork.Group group = new HEROsModNetwork.Group(dropdown.GetItem(dropdown.SelectedItem)); group.ID = HEROsModNetwork.Network.Groups[dropdown.SelectedItem].ID; + group.Color = colorPicker.Color; group.ImportPermissions(ExportPermissions()); HEROsModNetwork.LoginService.RequestSetGroupPermissions(group); } @@ -205,6 +210,7 @@ public void RefreshGroupPermissions() } if (checkboxContainer.ChildCount > 0) checkboxContainer.ContentHeight = checkboxContainer.GetLastChild().Y + checkboxContainer.GetLastChild().Height + spacing; + colorPicker.Color = HEROsModNetwork.Network.Groups[dropdown.SelectedItem].Color; } private byte[] ExportPermissions()