Skip to content

Commit 05ade45

Browse files
committed
Merge branch 'master' of https://github.com/TorchAPI/Essentials
2 parents 2ef1a1f + 6911eb1 commit 05ade45

2 files changed

Lines changed: 61 additions & 20 deletions

File tree

‎Essentials/Commands/PlayerModule.cs‎

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

‎Essentials/EssentialsPlugin.cs‎

Lines changed: 60 additions & 19 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;
@@ -31,6 +33,8 @@
3133
using VRage.Game.ModAPI;
3234
using VRageMath;
3335
using Newtonsoft.Json;
36+
using SpaceEngineers.Game.World;
37+
using VRage.Network;
3438

3539
namespace Essentials
3640
{
@@ -259,51 +263,88 @@ private void MotdOnce(IPlayer player)
259263
if (_motdOnce.Contains(player.SteamId))
260264
return;
261265

262-
SendMotd(p);
266+
SendMotd(p, true);
263267
_motdOnce.Add(player.SteamId);
264268
});
265269
break;
266270
}
267271
});
268272
}
269273

270-
public void SendMotd(MyPlayer player)
274+
public void SendMotd(MyPlayer player, bool onSessionChanged)
271275
{
272276
long playerId = player.Identity.IdentityId;
273277

274278
if (!string.IsNullOrEmpty(Config.MotdUrl) && !Config.NewUserMotdUrl)
275279
{
276-
if (MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl))
277-
MyVisualScriptLogicProvider.OpenSteamOverlay(Config.MotdUrl, playerId);
278-
else
279-
MyVisualScriptLogicProvider.OpenSteamOverlay($"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}", playerId);
280+
var url = MakeUrl(Config.MotdUrl);
281+
MyVisualScriptLogicProvider.OpenSteamOverlay(url, playerId);
282+
return;
280283
}
281284

282285
var id = player.Client.SteamUserId;
283-
if (id <= 0) //can't remember if this returns 0 or -1 on error.
284-
return;
286+
if (id <= 0) return;
285287

286288
string name = player.Identity?.DisplayName ?? "player";
287289

288-
bool newUser = !Config.KnownSteamIds.Contains(id);
289-
if (newUser)
290+
bool isNewUser = !Config.KnownSteamIds.Contains(id);
291+
if (isNewUser)
292+
{
290293
Config.KnownSteamIds.Add(id);
291-
if (!string.IsNullOrEmpty(Config.MotdUrl) && newUser && Config.NewUserMotdUrl)
294+
}
295+
296+
if (!string.IsNullOrEmpty(Config.MotdUrl) && isNewUser && Config.NewUserMotdUrl)
292297
{
293-
if (MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl))
294-
MyVisualScriptLogicProvider.OpenSteamOverlay(Config.MotdUrl, playerId);
295-
else
296-
MyVisualScriptLogicProvider.OpenSteamOverlay($"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}", playerId);
298+
var url = MakeUrl(Config.MotdUrl);
299+
MyVisualScriptLogicProvider.OpenSteamOverlay(url, playerId);
300+
return;
297301
}
298302

299-
if (newUser && !string.IsNullOrEmpty(Config.NewUserMotd))
303+
if (isNewUser && !string.IsNullOrEmpty(Config.NewUserMotd))
300304
{
301-
ModCommunication.SendMessageTo(new DialogMessage(MySession.Static.Name, "New User Message Of The Day", Config.NewUserMotd.Replace("%player%", name)), id);
305+
var msg = new DialogMessage(MySession.Static.Name, "New User Message Of The Day", Config.NewUserMotd.Replace("%player%", name));
306+
ModCommunication.SendMessageTo(msg, id);
307+
return;
308+
}
302309

310+
if (!string.IsNullOrEmpty(Config.Motd))
311+
{
312+
var msg = new DialogMessage(MySession.Static.Name, "Message Of The Day", Config.Motd.Replace("%player%", name));
313+
ModCommunication.SendMessageTo(msg, id);
314+
return;
315+
}
316+
317+
if (!onSessionChanged) // otherwise default motd shows up twice on connection
318+
{
319+
var txt = GetDefaultMotdText();
320+
var msg = new DialogMessage(MySession.Static.Name, "Message Of The Day", txt);
321+
ModCommunication.SendMessageTo(msg, id);
322+
}
323+
}
324+
325+
static string MakeUrl(string url)
326+
{
327+
if (MyGuiSandbox.IsUrlWhitelisted(url)) return url;
328+
return $"https://steamcommunity.com/linkfilter/?url={url}";
329+
}
330+
331+
static string GetDefaultMotdText()
332+
{
333+
try
334+
{
335+
var motdDataStruct = typeof(MySpaceRespawnComponent).GetNestedType("MOTDData", BindingFlags.NonPublic);
336+
var motdConstructFunc = motdDataStruct.GetMethod("Construct", BindingFlags.Public | BindingFlags.Static);
337+
var motdMessageField = motdDataStruct.GetField("Message", BindingFlags.Public | BindingFlags.Instance);
338+
var motd = motdConstructFunc.Invoke(null, new object[0]);
339+
var motdMessage = motdMessageField.GetValue(motd) as string;
340+
return motdMessage;
303341
}
304-
else if (!string.IsNullOrEmpty(Config.Motd))
342+
catch (Exception e)
305343
{
306-
ModCommunication.SendMessageTo(new DialogMessage(MySession.Static.Name, "Message Of The Day", Config.Motd.Replace("%player%", name)), id);
344+
var tag = $"#{new Random().NextDouble() * 1000:0}";
345+
Log.Info($"Failed to load MotD. Tag: {tag}. Error:");
346+
Log.Error(e);
347+
return $"Failed to load MotD. Contact admin.\nAdmin: text search in log: {tag}";
307348
}
308349
}
309350

0 commit comments

Comments
 (0)