Skip to content

Commit

Permalink
Port #79 (add chat color customizability to groups) to 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
JavidPack committed Feb 20, 2022
1 parent e6687ba commit 0e64ea1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
21 changes: 20 additions & 1 deletion HEROsMod.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 7 additions & 1 deletion HEROsModNetwork/DatabaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using Terraria;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -497,6 +502,7 @@ public static List<Group> GetGroups()
{
Group group = new Group(dbGroup.name);
group.ID = dbGroup.ID;
group.Color = dbGroup.color;
group.ImportPermissions(dbGroup.permissions);
result.Add(group);
}
Expand Down
6 changes: 5 additions & 1 deletion HEROsModNetwork/Group.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -63,6 +64,8 @@ public bool IsAdmin
set { _isAdmin = value; }
}

public Color Color { get; set; }

public Group(string name)
{
ID = -1;
Expand All @@ -78,6 +81,7 @@ public Group(string name)
{
Network.DefaultGroup = this;
}
this.Color = new Color(255, 255, 255);
}

public bool HasPermission(string permissionName)
Expand Down
6 changes: 6 additions & 0 deletions HEROsModNetwork/LoginService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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)
//{
Expand All @@ -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));

Expand All @@ -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();
}

Expand All @@ -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++)
Expand Down
8 changes: 7 additions & 1 deletion HEROsModServices/GroupInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -140,6 +143,7 @@ public GroupManagementWindow()
AddChild(bNew);
AddChild(bDelete);
AddChild(dropdown);
AddChild(colorPicker);

this.Height = bApply.Position.Y + bApply.Height + spacing;
this.CenterToParent();
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 0e64ea1

Please sign in to comment.