Skip to content

Commit 2b3d43e

Browse files
committed
Merge branch 'master' of https://github.com/TorchAPI/Essentials
2 parents 17779e0 + fdbab7b commit 2b3d43e

4 files changed

Lines changed: 135 additions & 511 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/EssentialsConfig.cs‎

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -98,71 +98,6 @@ public int BackpackLimit
9898
[Display(Visible=false)]
9999
public ObservableCollection<ulong> KnownSteamIds { get; } = new ObservableCollection<ulong>();
100100

101-
private bool _packRespawn;
102-
[Display(Name = "Pack Respawn", GroupName = "Client Join Tweaks", Order = 1, Description = "Packs ships which the client could respawn at into the initial world send. Will significantly decrease time waiting for ships to sync from the respawn menu, at the cost of slightly increased server load during client join.")]
103-
public bool PackRespawn
104-
{
105-
get => _packRespawn;
106-
set => SetValue(ref _packRespawn, value);
107-
}
108-
109-
private int _maxRespawnSize;
110-
[Display(Name = "Max Packed Respawn Size", GroupName = "Client Join Tweaks", Order = 2, Description = "Maximum size, in total block count, of ships that can be packed into the world send. Useful if your players often have very large grids. Will slightly lower performance impact of Pack Respawn option, by forcing clients to wait for very large grids the old way.")]
111-
public int MaxPackedRespawnSize
112-
{
113-
get => _maxRespawnSize;
114-
set => SetValue(ref _maxRespawnSize, value);
115-
}
116-
117-
private string _loadingText;
118-
[Display(Name = "Loading Text", GroupName = "Client Join Tweaks", Order = 3, Description = "Text displayed on the loading screen while the client is joining.")]
119-
public string LoadingText
120-
{
121-
get => _loadingText;
122-
set => SetValue(ref _loadingText, string.IsNullOrEmpty(value) ? null : value);
123-
}
124-
125-
private bool _enableClientTweaks = false;
126-
127-
[Display(Name = "Enable", GroupName = "Client Join Tweaks", Order = 0, Description = "Enables the client join tweak system. None of the options in this section will work if this is unchecked.")]
128-
public bool EnableClientTweaks
129-
{
130-
get => _enableClientTweaks;
131-
set => SetValue(ref _enableClientTweaks, value);
132-
}
133-
134-
private bool _enableToolbarOverride;
135-
[Display(Name = "Override Default Toolbar", GroupName = "Client Join Tweaks", Order = 4, Description = "Allows you to set a default toolbar for new players on the server. You can set the toolbar ingame with the !admin set toolbar command. This will make your current toolbar the new default.")]
136-
public bool EnableToolbarOverride
137-
{
138-
get => _enableToolbarOverride;
139-
set => SetValue(ref _enableToolbarOverride, value);
140-
}
141-
142-
private CompressionLevel _compression = CompressionLevel.Optimal;
143-
[Display(Name = "Compression Level", GroupName = "Client Join Tweaks", Order = 5, Description = "Sets the level of compression applied to client data. Higher compression takes more CPU, but less network bandwidth. Recommended to leave this at 'Optimal'")]
144-
public CompressionLevel CompressionLevel
145-
{
146-
get => _compression;
147-
set => SetValue(ref _compression, value);
148-
}
149-
150-
private bool _asyncJoin;
151-
[Display(Name = "Async Join", GroupName = "Client Join Tweaks", Order = 6, Description = "Speeds up client joining by moving almost all of the logic out of the game thread. Disable this if you get 'CollectionModifiedException'")]
152-
public bool AsyncJoin
153-
{
154-
get => _asyncJoin;
155-
set => SetValue(ref _asyncJoin, value);
156-
}
157-
158-
private bool _packPlanets;
159-
[Display(Name = "Pack Planets", GroupName = "Client Join Tweaks", Order = 7, Description = "Packs planet data into initial world download. Can speed up spawning in some cases. CAUTION: Planet data is very large! You should use Compression Level 'Optimal' and the Async Join option!")]
160-
public bool PackPlanets
161-
{
162-
get => _packPlanets;
163-
set => SetValue(ref _packPlanets, value);
164-
}
165-
166101
private bool _cutGameTags;
167102
[Display(Name = "Cut Game Tags", GroupName = "Client Join Tweaks", Order = 8, Description = "Cuts mods and blocks limits from matchmaking server info. Prevents from 'error downloading session settings'.")]
168103
public bool CutGameTags

‎Essentials/EssentialsPlugin.cs‎

Lines changed: 80 additions & 24 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
{
@@ -50,10 +54,13 @@ public class EssentialsPlugin : TorchPluginBase, IWpfPlugin
5054
private HashSet<ulong> _motdOnce = new HashSet<ulong>();
5155
private PatchManager _pm;
5256
private PatchContext _context;
57+
5358

5459
public static EssentialsPlugin Instance { get; private set; }
5560
public PlayerAccountModule AccModule = new PlayerAccountModule();
5661
RanksAndPermissionsModule RanksAndPermissions = new RanksAndPermissionsModule();
62+
private static bool _initilized = false;
63+
5764

5865
/// <inheritdoc />
5966
public UserControl GetControl() => _control ?? (_control = new PropertyGrid(){DataContext=Config/*, IsEnabled = false*/});
@@ -141,13 +148,25 @@ private void SessionChanged(ITorchSession session, TorchSessionState state)
141148
});
142149
AutoCommands.Instance.Start();
143150
InfoModule.Init();
151+
_initilized = true;
152+
153+
144154
break;
155+
156+
145157
case TorchSessionState.Unloading:
146-
Log.Info("Unloading rank data into JSON");
147-
RanksAndPermissions.SaveRankData();
148-
mpMan.PlayerLeft -= ResetMotdOnce;
149-
mpMan.PlayerJoined -= MotdOnce;
150-
MyEntities.OnEntityAdd -= EntityAdded;
158+
159+
if (_initilized)
160+
{
161+
//Dont try and remove these unless server was actually fully initlized
162+
Log.Info("Unloading rank data into JSON");
163+
RanksAndPermissions.SaveRankData();
164+
mpMan.PlayerLeft -= ResetMotdOnce;
165+
mpMan.PlayerJoined -= MotdOnce;
166+
MyEntities.OnEntityAdd -= EntityAdded;
167+
}
168+
169+
151170
_bagTracker.Clear();
152171
_removalTracker.Clear();
153172
break;
@@ -261,51 +280,88 @@ private void MotdOnce(IPlayer player)
261280
if (_motdOnce.Contains(player.SteamId))
262281
return;
263282

264-
SendMotd(p);
283+
SendMotd(p, true);
265284
_motdOnce.Add(player.SteamId);
266285
});
267286
break;
268287
}
269288
});
270289
}
271290

272-
public void SendMotd(MyPlayer player)
291+
public void SendMotd(MyPlayer player, bool onSessionChanged)
273292
{
274293
long playerId = player.Identity.IdentityId;
275294

276295
if (!string.IsNullOrEmpty(Config.MotdUrl) && !Config.NewUserMotdUrl)
277296
{
278-
if (MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl))
279-
MyVisualScriptLogicProvider.OpenSteamOverlay(Config.MotdUrl, playerId);
280-
else
281-
MyVisualScriptLogicProvider.OpenSteamOverlay($"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}", playerId);
297+
var url = MakeUrl(Config.MotdUrl);
298+
MyVisualScriptLogicProvider.OpenSteamOverlay(url, playerId);
299+
return;
282300
}
283301

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

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

290-
bool newUser = !Config.KnownSteamIds.Contains(id);
291-
if (newUser)
307+
bool isNewUser = !Config.KnownSteamIds.Contains(id);
308+
if (isNewUser)
309+
{
292310
Config.KnownSteamIds.Add(id);
293-
if (!string.IsNullOrEmpty(Config.MotdUrl) && newUser && Config.NewUserMotdUrl)
311+
}
312+
313+
if (!string.IsNullOrEmpty(Config.MotdUrl) && isNewUser && Config.NewUserMotdUrl)
314+
{
315+
var url = MakeUrl(Config.MotdUrl);
316+
MyVisualScriptLogicProvider.OpenSteamOverlay(url, playerId);
317+
return;
318+
}
319+
320+
if (isNewUser && !string.IsNullOrEmpty(Config.NewUserMotd))
294321
{
295-
if (MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl))
296-
MyVisualScriptLogicProvider.OpenSteamOverlay(Config.MotdUrl, playerId);
297-
else
298-
MyVisualScriptLogicProvider.OpenSteamOverlay($"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}", playerId);
322+
var msg = new DialogMessage(MySession.Static.Name, "New User Message Of The Day", Config.NewUserMotd.Replace("%player%", name));
323+
ModCommunication.SendMessageTo(msg, id);
324+
return;
299325
}
300326

301-
if (newUser && !string.IsNullOrEmpty(Config.NewUserMotd))
327+
if (!string.IsNullOrEmpty(Config.Motd))
302328
{
303-
ModCommunication.SendMessageTo(new DialogMessage(MySession.Static.Name, "New User Message Of The Day", Config.NewUserMotd.Replace("%player%", name)), id);
329+
var msg = new DialogMessage(MySession.Static.Name, "Message Of The Day", Config.Motd.Replace("%player%", name));
330+
ModCommunication.SendMessageTo(msg, id);
331+
return;
332+
}
304333

334+
if (!onSessionChanged) // otherwise default motd shows up twice on connection
335+
{
336+
var txt = GetDefaultMotdText();
337+
var msg = new DialogMessage(MySession.Static.Name, "Message Of The Day", txt);
338+
ModCommunication.SendMessageTo(msg, id);
339+
}
340+
}
341+
342+
static string MakeUrl(string url)
343+
{
344+
if (MyGuiSandbox.IsUrlWhitelisted(url)) return url;
345+
return $"https://steamcommunity.com/linkfilter/?url={url}";
346+
}
347+
348+
static string GetDefaultMotdText()
349+
{
350+
try
351+
{
352+
var motdDataStruct = typeof(MySpaceRespawnComponent).GetNestedType("MOTDData", BindingFlags.NonPublic);
353+
var motdConstructFunc = motdDataStruct.GetMethod("Construct", BindingFlags.Public | BindingFlags.Static);
354+
var motdMessageField = motdDataStruct.GetField("Message", BindingFlags.Public | BindingFlags.Instance);
355+
var motd = motdConstructFunc.Invoke(null, new object[0]);
356+
var motdMessage = motdMessageField.GetValue(motd) as string;
357+
return motdMessage;
305358
}
306-
else if (!string.IsNullOrEmpty(Config.Motd))
359+
catch (Exception e)
307360
{
308-
ModCommunication.SendMessageTo(new DialogMessage(MySession.Static.Name, "Message Of The Day", Config.Motd.Replace("%player%", name)), id);
361+
var tag = $"#{new Random().NextDouble() * 1000:0}";
362+
Log.Info($"Failed to load MotD. Tag: {tag}. Error:");
363+
Log.Error(e);
364+
return $"Failed to load MotD. Contact admin.\nAdmin: text search in log: {tag}";
309365
}
310366
}
311367

0 commit comments

Comments
 (0)