Skip to content

Commit d045c3c

Browse files
YuriYuri
authored andcommitted
!motd for default settings (TorchAPI#194)
1 parent cad939f commit d045c3c

2 files changed

Lines changed: 58 additions & 21 deletions

File tree

‎Essentials/Commands/PlayerModule.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public void Unban(string nameOrSteamId)
174174
[Permission(MyPromoteLevel.None)]
175175
public void Motd()
176176
{
177-
Plugin.SendMotd((MyPlayer)Context.Player);
177+
Plugin.SendMotd((MyPlayer)Context.Player, false);
178178
}
179179
}
180180
}

‎Essentials/EssentialsPlugin.cs‎

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Reflection;
5+
using System.Runtime.Remoting.Contexts;
46
using System.Threading;
57
using System.Threading.Tasks;
68
using System.Windows.Controls;
@@ -25,6 +27,8 @@
2527
using VRage.Game.Entity;
2628
using VRageMath;
2729
using Newtonsoft.Json;
30+
using SpaceEngineers.Game.World;
31+
using VRage.Network;
2832

2933
namespace Essentials
3034
{
@@ -262,51 +266,84 @@ private void MotdOnce(IPlayer player)
262266
if (_motdOnce.Contains(player.SteamId))
263267
return;
264268

265-
SendMotd(p);
269+
SendMotd(p, true);
266270
_motdOnce.Add(player.SteamId);
267271
});
268272
break;
269273
}
270274
});
271275
}
272276

273-
public void SendMotd(MyPlayer player)
277+
public void SendMotd(MyPlayer player, bool onSessionChanged)
274278
{
275279
long playerId = player.Identity.IdentityId;
276280

281+
var motdUrl = MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl)
282+
? Config.MotdUrl
283+
: $"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}";
284+
277285
if (!string.IsNullOrEmpty(Config.MotdUrl) && !Config.NewUserMotdUrl)
278286
{
279-
if (MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl))
280-
MyVisualScriptLogicProvider.OpenSteamOverlay(Config.MotdUrl, playerId);
281-
else
282-
MyVisualScriptLogicProvider.OpenSteamOverlay($"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}", playerId);
287+
MyVisualScriptLogicProvider.OpenSteamOverlay(motdUrl, playerId);
288+
return;
283289
}
284290

285291
var id = player.Client.SteamUserId;
286-
if (id <= 0) //can't remember if this returns 0 or -1 on error.
287-
return;
288-
292+
if (id <= 0) return;
293+
289294
string name = player.Identity?.DisplayName ?? "player";
290295

291-
bool newUser = !Config.KnownSteamIds.Contains(id);
292-
if (newUser)
296+
bool isNewUser = !Config.KnownSteamIds.Contains(id);
297+
if (isNewUser)
298+
{
293299
Config.KnownSteamIds.Add(id);
294-
if (!string.IsNullOrEmpty(Config.MotdUrl) && newUser && Config.NewUserMotdUrl)
300+
}
301+
302+
if (!string.IsNullOrEmpty(Config.MotdUrl) && isNewUser && Config.NewUserMotdUrl)
303+
{
304+
MyVisualScriptLogicProvider.OpenSteamOverlay(motdUrl, playerId);
305+
return;
306+
}
307+
308+
if (isNewUser && !string.IsNullOrEmpty(Config.NewUserMotd))
295309
{
296-
if (MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl))
297-
MyVisualScriptLogicProvider.OpenSteamOverlay(Config.MotdUrl, playerId);
298-
else
299-
MyVisualScriptLogicProvider.OpenSteamOverlay($"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}", playerId);
310+
var msg = new DialogMessage(MySession.Static.Name, "New User Message Of The Day", Config.NewUserMotd.Replace("%player%", name));
311+
ModCommunication.SendMessageTo(msg, id);
312+
return;
300313
}
301314

302-
if (newUser && !string.IsNullOrEmpty(Config.NewUserMotd))
315+
if (!string.IsNullOrEmpty(Config.Motd))
303316
{
304-
ModCommunication.SendMessageTo(new DialogMessage(MySession.Static.Name, "New User Message Of The Day", Config.NewUserMotd.Replace("%player%", name)), id);
317+
var msg = new DialogMessage(MySession.Static.Name, "Message Of The Day", Config.Motd.Replace("%player%", name));
318+
ModCommunication.SendMessageTo(msg, id);
319+
return;
320+
}
305321

322+
if (!onSessionChanged) // otherwise default motd shows up twice on connection
323+
{
324+
var txt = GetDefaultMotdText();
325+
var msg = new DialogMessage(MySession.Static.Name, "Message Of The Day", txt);
326+
ModCommunication.SendMessageTo(msg, id);
327+
}
328+
}
329+
330+
static string GetDefaultMotdText()
331+
{
332+
try
333+
{
334+
var motdDataStruct = typeof(MySpaceRespawnComponent).GetNestedType("MOTDData", BindingFlags.NonPublic);
335+
var motdConstructFunc = motdDataStruct.GetMethod("Construct", BindingFlags.Public | BindingFlags.Static);
336+
var motdMessageField = motdDataStruct.GetField("Message", BindingFlags.Public | BindingFlags.Instance);
337+
var motd = motdConstructFunc.Invoke(null, new object[0]);
338+
var motdMessage = motdMessageField.GetValue(motd) as string;
339+
return motdMessage;
306340
}
307-
else if (!string.IsNullOrEmpty(Config.Motd))
341+
catch (Exception e)
308342
{
309-
ModCommunication.SendMessageTo(new DialogMessage(MySession.Static.Name, "Message Of The Day", Config.Motd.Replace("%player%", name)), id);
343+
var tag = $"#{new Random().NextDouble() * 1000:0}";
344+
Log.Info($"Failed to load MotD. Tag: {tag}. Error:");
345+
Log.Error(e);
346+
return $"Failed to load MotD. Contact admin.\nAdmin: text search in log: {tag}";
310347
}
311348
}
312349

0 commit comments

Comments
 (0)