diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22ab1aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,109 @@ +# Build Folders (you can keep bin if you'd like, to store dlls and pdbs) +[Bb]in/ +[Oo]bj/ + +# mstest test results +TestResults + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates +.vs/ + +# Build results +[Dd]ebug/ +[Rr]elease/ +x64/ +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.log +*.vspscc +*.vssscc +.builds + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper* + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Publish Web Output +*.Publish.xml + +# NuGet Packages Directory +packages + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +[Bb]in +[Oo]bj +sql +TestResults +[Tt]est[Rr]esult* +*.Cache +ClientBin +[Ss]tyle[Cc]op.* +~$* +*.dbmdl +Generated_Code #added for RIA/Silverlight projects + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML diff --git a/AutoBroadcast.sln b/AutoBroadcast.sln index cf576e5..21ce615 100644 --- a/AutoBroadcast.sln +++ b/AutoBroadcast.sln @@ -1,6 +1,6 @@  -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual C# Express 2010 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoBroadcast", "AutoBroadcast\AutoBroadcast.csproj", "{85F20ADC-C2D1-416D-B7FF-9433C75A8F01}" EndProject Global diff --git a/AutoBroadcast/ABConfig.cs b/AutoBroadcast/ABConfig.cs index 8df61a8..38d3f2c 100644 --- a/AutoBroadcast/ABConfig.cs +++ b/AutoBroadcast/ABConfig.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; -using System.IO; +using System.IO; using Newtonsoft.Json; -using TShockAPI; namespace AutoBroadcast { @@ -20,37 +17,33 @@ public static ABConfig Read(string file) { if (!File.Exists(file)) { - ABConfig.WriteExample(file); + WriteExample(file); } return JsonConvert.DeserializeObject(File.ReadAllText(file)); } public static void WriteExample(string file) { - File.WriteAllText(file, @"{ - ""Broadcasts"": [ - { - ""Name"": ""Example Broadcast"", - ""Enabled"": false, - ""Messages"": [ - ""This is an example broadcast"", - ""It will broadcast every 5 minutes"", - ""Broadcasts can also execute commands"", - ""/time noon"" - ], - ""ColorRGB"": [ - 255.0, - 0.0, - 0.0 - ], - ""Interval"": 300, - ""StartDelay"": 60, - ""Groups"": [], - ""TriggerWords"": [], - ""TriggerToWholeGroup"": false - } - ] -}"); + var Ex = new Broadcast(); + Ex.Name = "Example Broadcast"; + Ex.Enabled = false; + Ex.Messages = new string[] + { + "This is an example broadcast", + "It will broadcast every 5 minutes", + "Broadcasts can also execute commands", + "/time noon" + }; + Ex.ColorRGB = new int[] { 255, 0, 0 }; + Ex.Interval = 300; + Ex.StartDelay = 60; + + var Conf = new ABConfig + { + Broadcasts = new Broadcast[] { Ex } + }; + + Conf.Write(file); } } @@ -59,7 +52,7 @@ public class Broadcast public string Name = string.Empty; public bool Enabled = false; public string[] Messages = new string[0]; - public float[] ColorRGB = new float[3]; + public int[] ColorRGB = new int[3]; public int Interval = 0; public int StartDelay = 0; public string[] Groups = new string[0]; diff --git a/AutoBroadcast/AutoBroadcast.cs b/AutoBroadcast/AutoBroadcast.cs index 88c38d7..e16a37c 100644 --- a/AutoBroadcast/AutoBroadcast.cs +++ b/AutoBroadcast/AutoBroadcast.cs @@ -2,30 +2,31 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Timers; using Terraria; using TerrariaApi.Server; using TShockAPI; namespace AutoBroadcast { - [ApiVersion(1, 14)] + [ApiVersion(2, 1)] public class AutoBroadcast : TerrariaPlugin { public override string Name { get { return "AutoBroadcast"; } } - public override string Author { get { return "Scavenger"; } } + public override string Author { get { return "Scavenger & Simon311"; } } public override string Description { get { return "Automatically Broadcast a Message or Command every x seconds"; } } public override Version Version { get { return Assembly.GetExecutingAssembly().GetName().Version; } } public string ConfigPath { get { return Path.Combine(TShock.SavePath, "AutoBroadcastConfig.json"); } } public ABConfig Config = new ABConfig(); - public DateTime LastCheck = DateTime.UtcNow; public AutoBroadcast(Main Game) : base(Game) { } + static readonly Timer Update = new Timer(1000); + public override void Initialize() { - ServerApi.Hooks.GameInitialize.Register(this, OnInitialize); - ServerApi.Hooks.GameUpdate.Register(this, OnUpdate); + ServerApi.Hooks.GameInitialize.Register(this, OnInitialize, -5); ServerApi.Hooks.ServerChat.Register(this, OnChat); } @@ -34,15 +35,16 @@ protected override void Dispose(bool Disposing) if (Disposing) { ServerApi.Hooks.GameInitialize.Deregister(this, OnInitialize); - ServerApi.Hooks.GameUpdate.Deregister(this, OnUpdate); ServerApi.Hooks.ServerChat.Deregister(this, OnChat); + Update.Elapsed -= OnUpdate; + Update.Stop(); } base.Dispose(Disposing); } public void OnInitialize(EventArgs args) { - Commands.ChatCommands.Add(new Command("abroadcast", autobc, "autobc")); + Commands.ChatCommands.Add(new Command("abroadcast", AutoBC, "autobc")); try { @@ -51,11 +53,13 @@ public void OnInitialize(EventArgs args) catch (Exception ex) { Config = new ABConfig(); - Log.ConsoleError("[AutoBroadcast] An exception occurred while parsing the AutoBroadcast config!\n{0}".SFormat(ex.ToString())); + TShock.Log.ConsoleError("[AutoBroadcast] An exception occurred while parsing the AutoBroadcast config!\n{0}".SFormat(ex.ToString())); } + Update.Elapsed += OnUpdate; + Update.Start(); } - public void autobc(CommandArgs args) + public void AutoBC(CommandArgs args) { try { @@ -66,144 +70,118 @@ public void autobc(CommandArgs args) { Config = new ABConfig(); args.Player.SendWarningMessage("An exception occurred while parsing the AutoBroadcast config! check logs for more details!"); - Log.Error("[AutoBroadcast] An exception occurred while parsing tbe AutoBroadcast config!\n{0}".SFormat(ex.ToString())); + TShock.Log.Error("[AutoBroadcast] An exception occurred while parsing tbe AutoBroadcast config!\n{0}".SFormat(ex.ToString())); } } #region Chat public void OnChat(ServerChatEventArgs args) { - string[] Groups = new string[0]; - string[] Messages = new string[0]; - float[] Colour = new float[0]; - var PlayerGroup = TShock.Players[args.Who].Group.Name; + var PlayerGroup = TShock.Players[args.Who]?.Group?.Name; - lock (Config.Broadcasts) - foreach (var broadcast in Config.Broadcasts) - { - if (broadcast == null || !broadcast.Enabled || - (broadcast.TriggerToWholeGroup && !broadcast.Groups.Contains(PlayerGroup))) - { - continue; - } + if (string.IsNullOrWhiteSpace(PlayerGroup)) + { + return; + } + + foreach (var broadcast in Config.Broadcasts) + { + if (broadcast == null || !broadcast.Enabled || !broadcast.Groups.Contains(PlayerGroup)) { continue; } - foreach (string Word in broadcast.TriggerWords) + foreach (string Word in broadcast.TriggerWords) + { + if (args.Text.Contains(Word)) { - if (args.Text.Contains(Word)) + if (broadcast.TriggerToWholeGroup && broadcast.Groups.Length > 0) { - if (broadcast.TriggerToWholeGroup && broadcast.Groups.Length > 0) - { - Groups = broadcast.Groups; - } - Messages = broadcast.Messages; - Colour = broadcast.ColorRGB; - break; + BroadcastToGroups(broadcast.Groups, broadcast); } + else BroadcastToPlayer(args.Who, broadcast); + break; } } - - if (Groups.Length > 0) - { - BroadcastToGroups(Groups, Messages, Colour); - } - else - { - BroadcastToPlayer(args.Who, Messages, Colour); } } #endregion #region Update - public void OnUpdate(EventArgs args) + public void OnUpdate(object Sender, EventArgs e) { - if ((DateTime.UtcNow - LastCheck).TotalSeconds >= 1) + if (Main.worldID == 0) { return; } + + foreach (Broadcast broadcast in Config.Broadcasts) { - LastCheck = DateTime.UtcNow; - int NumBroadcasts = 0; - lock (Config.Broadcasts) - NumBroadcasts = Config.Broadcasts.Length; - for (int i = 0; i < NumBroadcasts; i++) + if (broadcast == null || !broadcast.Enabled || broadcast.Interval < 1) { - string[] Groups = new string[0]; - string[] Messages = new string[0]; - float[] Colour = new float[0]; + continue; + } - lock (Config.Broadcasts) - { - if (Config.Broadcasts[i] == null || !Config.Broadcasts[i].Enabled || Config.Broadcasts[i].Interval < 1) - { - continue; - } - if (Config.Broadcasts[i].StartDelay > 0) - { - Config.Broadcasts[i].StartDelay--; - continue; - } - Config.Broadcasts[i].StartDelay = Config.Broadcasts[i].Interval;// Start Delay used as Interval Countdown - Groups = Config.Broadcasts[i].Groups; - Messages = Config.Broadcasts[i].Messages; - Colour = Config.Broadcasts[i].ColorRGB; - } + if (broadcast.StartDelay > 0) + { + broadcast.StartDelay--; + continue; + } - if (Groups.Length > 0) - { - BroadcastToGroups(Groups, Messages, Colour); - } - else - { - BroadcastToAll(Messages, Colour); - } + broadcast.StartDelay = broadcast.Interval; // Start Delay used as Interval Countdown + + if (broadcast.Groups.Length > 0) + { + BroadcastToGroups(broadcast.Groups, broadcast); + } + else + { + BroadcastToAll(broadcast); } } } #endregion - public static void BroadcastToGroups(string[] Groups, string[] Messages, float[] Colour) + public static void BroadcastToGroups(string[] Groups, Broadcast broadcast) { - foreach (string Line in Messages) + foreach (string Line in broadcast.Messages) { - if (Line.StartsWith("/")) + if (Line.StartsWith(TShock.Config.CommandSpecifier) || Line.StartsWith(TShock.Config.CommandSilentSpecifier)) { Commands.HandleCommand(TSPlayer.Server, Line); } else { - lock (TShock.Players) - foreach (var player in TShock.Players) + foreach (var player in TShock.Players) + { + if (player?.Group != null && Groups.Contains(player.Group.Name)) { - if (player != null && Groups.Contains(player.Group.Name)) - { - player.SendMessage(Line, (byte)Colour[0], (byte)Colour[1], (byte)Colour[2]); - } + player.SendMessage(Line, (byte)broadcast.ColorRGB[0], (byte)broadcast.ColorRGB[1], (byte)broadcast.ColorRGB[2]); } + } } } } - public static void BroadcastToAll(string[] Messages, float[] Colour) + + public static void BroadcastToAll(Broadcast broadcast) { - foreach (string Line in Messages) + foreach (string Line in broadcast.Messages) { - if (Line.StartsWith("/")) + if (Line.StartsWith(TShock.Config.CommandSpecifier) || Line.StartsWith(TShock.Config.CommandSilentSpecifier)) { Commands.HandleCommand(TSPlayer.Server, Line); } else { - TSPlayer.All.SendMessage(Line, (byte)Colour[0], (byte)Colour[1], (byte)Colour[2]); + TSPlayer.All.SendMessage(Line, (byte)broadcast.ColorRGB[0], (byte)broadcast.ColorRGB[1], (byte)broadcast.ColorRGB[2]); } } } - public static void BroadcastToPlayer(int plr, string[] Messages, float[] Colour) + public static void BroadcastToPlayer(int Who, Broadcast broadcast) { - foreach (string Line in Messages) + foreach (string Line in broadcast.Messages) { - if (Line.StartsWith("/")) + if (Line.StartsWith(TShock.Config.CommandSpecifier) || Line.StartsWith(TShock.Config.CommandSilentSpecifier)) { Commands.HandleCommand(TSPlayer.Server, Line); } - else lock(TShock.Players) + else { - TShock.Players[plr].SendMessage(Line, (byte)Colour[0], (byte)Colour[1], (byte)Colour[2]); + TShock.Players[Who]?.SendMessage(Line, (byte)broadcast.ColorRGB[0], (byte)broadcast.ColorRGB[1], (byte)broadcast.ColorRGB[2]); } } } diff --git a/AutoBroadcast/AutoBroadcast.csproj b/AutoBroadcast/AutoBroadcast.csproj index 6d1bf6c..88f109c 100644 --- a/AutoBroadcast/AutoBroadcast.csproj +++ b/AutoBroadcast/AutoBroadcast.csproj @@ -1,5 +1,5 @@  - + Debug x86 @@ -10,7 +10,7 @@ Properties AutoBroadcast AutoBroadcast - v4.0 + v4.5.1 512 @@ -20,10 +20,12 @@ ..\Build\ + false ..\Build\ true + false @@ -33,13 +35,20 @@ ..\Refs\Newtonsoft.Json.dll + False + + + ..\Refs\OTAPI.dll + False ..\Refs\TerrariaServer.exe + False ..\Refs\TShockAPI.dll + False diff --git a/AutoBroadcast/Properties/AssemblyInfo.cs b/AutoBroadcast/Properties/AssemblyInfo.cs index 369b1eb..807df13 100644 --- a/AutoBroadcast/Properties/AssemblyInfo.cs +++ b/AutoBroadcast/Properties/AssemblyInfo.cs @@ -1,36 +1,16 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. [assembly: AssemblyTitle("AutoBroadcast")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("Scavenger3")] +[assembly: AssemblyCompany("Scavenger3 & Simon311")] [assembly: AssemblyProduct("AutoBroadcast")] -[assembly: AssemblyCopyright("Copyright \u00A9 Scavenger3 2013")] +[assembly: AssemblyCopyright("Copyright \u00A9 Scavenger3 & Simon311 2020")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. [assembly: ComVisible(false)] -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("84db775e-14d9-4bab-aed1-c98df5cb8086")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.5.1")] -[assembly: AssemblyFileVersion("1.5.1")] +[assembly: AssemblyVersion("1.6")] +[assembly: AssemblyFileVersion("1.6")] diff --git a/Build/AutoBroadcast.dll b/Build/AutoBroadcast.dll index 694d439..7577a09 100644 Binary files a/Build/AutoBroadcast.dll and b/Build/AutoBroadcast.dll differ diff --git a/Refs/Newtonsoft.Json.dll b/Refs/Newtonsoft.Json.dll index 282f3b3..77a5d89 100644 Binary files a/Refs/Newtonsoft.Json.dll and b/Refs/Newtonsoft.Json.dll differ diff --git a/Refs/OTAPI.dll b/Refs/OTAPI.dll new file mode 100644 index 0000000..a220e1d Binary files /dev/null and b/Refs/OTAPI.dll differ diff --git a/Refs/System.dll b/Refs/System.dll deleted file mode 100644 index d7ad7de..0000000 Binary files a/Refs/System.dll and /dev/null differ diff --git a/Refs/TShockAPI.dll b/Refs/TShockAPI.dll index c9733a6..02a1bf3 100644 Binary files a/Refs/TShockAPI.dll and b/Refs/TShockAPI.dll differ diff --git a/Refs/TerrariaServer.exe b/Refs/TerrariaServer.exe index 21deb1e..c386adf 100644 Binary files a/Refs/TerrariaServer.exe and b/Refs/TerrariaServer.exe differ