Skip to content

Commit

Permalink
Merge branch 'upcoming' into upcoming-nuget-release
Browse files Browse the repository at this point in the history
  • Loading branch information
SignatureBeef committed Dec 9, 2021
2 parents 4fdb32f + 8204e2b commit c6bd0df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion OTAPI.Patcher/OTAPI.Patcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Version>3.0.29-alpha</Version>
<Version>3.0.30-alpha</Version>
<PreserveCompilationContext>true</PreserveCompilationContext>
<PlatformTarget>x64</PlatformTarget>
<RuntimeIdentifiers>win7-x64;osx.10.11-x64;ubuntu.16.04-x64</RuntimeIdentifiers>
Expand Down
30 changes: 23 additions & 7 deletions OTAPI.Scripts/TopLevelScripts/HookPlayerAnnouncements.Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ void HookPlayerAnnouncements(ModFwModder modder)
foreach (var prm in body.Method.Parameters)
body.GetILProcessor().InsertBefore(instr, Instruction.Create(OpCodes.Ldarg, prm));
instr.Operand = callback;
if (!(instr.Next.Operand is FieldReference fieldref && fieldref.Name == "dedServ"))
throw new Exception("Expected to replace dedServ calls with the player announce hook.");
body.GetILProcessor().Remove(instr.Next); // no need for this dedServ check
}
};
}
Expand All @@ -60,9 +65,17 @@ public static partial class Hooks
{
public static partial class NetMessage
{
[Flags]
public enum PlayerAnnounceResult : byte
{
SendToPlayer = 1,
WriteToConsole = 2,

Default = SendToPlayer | WriteToConsole,
}
public class PlayerAnnounceEventArgs : EventArgs
{
public HookResult? Result { get; set; }
public PlayerAnnounceResult Result { get; set; } = PlayerAnnounceResult.Default;

public int Index { get; set; }
public NetworkText Text { get; set; }
Expand All @@ -71,14 +84,15 @@ public class PlayerAnnounceEventArgs : EventArgs
public int Plr { get; set; }
public int ToWho { get; set; }
public int FromWh { get; set; }

public PlayerAnnounceEventArgs(NetworkText text) => Text = text;
}
public static event EventHandler<PlayerAnnounceEventArgs> PlayerAnnounce;
public static event EventHandler<PlayerAnnounceEventArgs>? PlayerAnnounce;

public static void InvokePlayerAnnounce(NetworkText text, Color color, int excludedPlayer, int plr, int toWho, int fromWho)
public static bool InvokePlayerAnnounce(NetworkText text, Color color, int excludedPlayer, int plr, int toWho, int fromWho)
{
var args = new PlayerAnnounceEventArgs()
var args = new PlayerAnnounceEventArgs(text)
{
Text = text,
Color = color,
ExcludedPlayer = excludedPlayer,
Plr = plr,
Expand All @@ -87,10 +101,12 @@ public static void InvokePlayerAnnounce(NetworkText text, Color color, int exclu
};
PlayerAnnounce?.Invoke(null, args);

if (args.Result != HookResult.Cancel)
if ((args.Result & PlayerAnnounceResult.SendToPlayer) != 0)
Terraria.Chat.ChatHelper.BroadcastChatMessage(args.Text, args.Color, args.ExcludedPlayer);

return (args.Result & PlayerAnnounceResult.WriteToConsole) != 0;
}
}
}
}
}
#endif

0 comments on commit c6bd0df

Please sign in to comment.