Skip to content

Commit

Permalink
!motd for default settings (TorchAPI#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri authored and Yuri committed Sep 9, 2021
1 parent cad939f commit d045c3c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Essentials/Commands/PlayerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void Unban(string nameOrSteamId)
[Permission(MyPromoteLevel.None)]
public void Motd()
{
Plugin.SendMotd((MyPlayer)Context.Player);
Plugin.SendMotd((MyPlayer)Context.Player, false);
}
}
}
77 changes: 57 additions & 20 deletions Essentials/EssentialsPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Remoting.Contexts;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
Expand All @@ -25,6 +27,8 @@
using VRage.Game.Entity;
using VRageMath;
using Newtonsoft.Json;
using SpaceEngineers.Game.World;
using VRage.Network;

namespace Essentials
{
Expand Down Expand Up @@ -262,51 +266,84 @@ private void MotdOnce(IPlayer player)
if (_motdOnce.Contains(player.SteamId))
return;

SendMotd(p);
SendMotd(p, true);
_motdOnce.Add(player.SteamId);
});
break;
}
});
}

public void SendMotd(MyPlayer player)
public void SendMotd(MyPlayer player, bool onSessionChanged)
{
long playerId = player.Identity.IdentityId;

var motdUrl = MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl)
? Config.MotdUrl
: $"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}";

if (!string.IsNullOrEmpty(Config.MotdUrl) && !Config.NewUserMotdUrl)
{
if (MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl))
MyVisualScriptLogicProvider.OpenSteamOverlay(Config.MotdUrl, playerId);
else
MyVisualScriptLogicProvider.OpenSteamOverlay($"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}", playerId);
MyVisualScriptLogicProvider.OpenSteamOverlay(motdUrl, playerId);
return;
}

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

if (id <= 0) return;

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

bool newUser = !Config.KnownSteamIds.Contains(id);
if (newUser)
bool isNewUser = !Config.KnownSteamIds.Contains(id);
if (isNewUser)
{
Config.KnownSteamIds.Add(id);
if (!string.IsNullOrEmpty(Config.MotdUrl) && newUser && Config.NewUserMotdUrl)
}

if (!string.IsNullOrEmpty(Config.MotdUrl) && isNewUser && Config.NewUserMotdUrl)
{
MyVisualScriptLogicProvider.OpenSteamOverlay(motdUrl, playerId);
return;
}

if (isNewUser && !string.IsNullOrEmpty(Config.NewUserMotd))
{
if (MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl))
MyVisualScriptLogicProvider.OpenSteamOverlay(Config.MotdUrl, playerId);
else
MyVisualScriptLogicProvider.OpenSteamOverlay($"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}", playerId);
var msg = new DialogMessage(MySession.Static.Name, "New User Message Of The Day", Config.NewUserMotd.Replace("%player%", name));
ModCommunication.SendMessageTo(msg, id);
return;
}

if (newUser && !string.IsNullOrEmpty(Config.NewUserMotd))
if (!string.IsNullOrEmpty(Config.Motd))
{
ModCommunication.SendMessageTo(new DialogMessage(MySession.Static.Name, "New User Message Of The Day", Config.NewUserMotd.Replace("%player%", name)), id);
var msg = new DialogMessage(MySession.Static.Name, "Message Of The Day", Config.Motd.Replace("%player%", name));
ModCommunication.SendMessageTo(msg, id);
return;
}

if (!onSessionChanged) // otherwise default motd shows up twice on connection
{
var txt = GetDefaultMotdText();
var msg = new DialogMessage(MySession.Static.Name, "Message Of The Day", txt);
ModCommunication.SendMessageTo(msg, id);
}
}

static string GetDefaultMotdText()
{
try
{
var motdDataStruct = typeof(MySpaceRespawnComponent).GetNestedType("MOTDData", BindingFlags.NonPublic);
var motdConstructFunc = motdDataStruct.GetMethod("Construct", BindingFlags.Public | BindingFlags.Static);
var motdMessageField = motdDataStruct.GetField("Message", BindingFlags.Public | BindingFlags.Instance);
var motd = motdConstructFunc.Invoke(null, new object[0]);
var motdMessage = motdMessageField.GetValue(motd) as string;
return motdMessage;
}
else if (!string.IsNullOrEmpty(Config.Motd))
catch (Exception e)
{
ModCommunication.SendMessageTo(new DialogMessage(MySession.Static.Name, "Message Of The Day", Config.Motd.Replace("%player%", name)), id);
var tag = $"#{new Random().NextDouble() * 1000:0}";
Log.Info($"Failed to load MotD. Tag: {tag}. Error:");
Log.Error(e);
return $"Failed to load MotD. Contact admin.\nAdmin: text search in log: {tag}";
}
}

Expand Down

0 comments on commit d045c3c

Please sign in to comment.