|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.IO; |
| 4 | +using System.Reflection; |
| 5 | +using System.Runtime.Remoting.Contexts; |
4 | 6 | using System.Threading; |
5 | 7 | using System.Threading.Tasks; |
6 | 8 | using System.Windows.Controls; |
|
31 | 33 | using VRage.Game.ModAPI; |
32 | 34 | using VRageMath; |
33 | 35 | using Newtonsoft.Json; |
| 36 | +using SpaceEngineers.Game.World; |
| 37 | +using VRage.Network; |
34 | 38 |
|
35 | 39 | namespace Essentials |
36 | 40 | { |
@@ -259,51 +263,88 @@ private void MotdOnce(IPlayer player) |
259 | 263 | if (_motdOnce.Contains(player.SteamId)) |
260 | 264 | return; |
261 | 265 |
|
262 | | - SendMotd(p); |
| 266 | + SendMotd(p, true); |
263 | 267 | _motdOnce.Add(player.SteamId); |
264 | 268 | }); |
265 | 269 | break; |
266 | 270 | } |
267 | 271 | }); |
268 | 272 | } |
269 | 273 |
|
270 | | - public void SendMotd(MyPlayer player) |
| 274 | + public void SendMotd(MyPlayer player, bool onSessionChanged) |
271 | 275 | { |
272 | 276 | long playerId = player.Identity.IdentityId; |
273 | 277 |
|
274 | 278 | if (!string.IsNullOrEmpty(Config.MotdUrl) && !Config.NewUserMotdUrl) |
275 | 279 | { |
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; |
280 | 283 | } |
281 | 284 |
|
282 | 285 | 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; |
285 | 287 |
|
286 | 288 | string name = player.Identity?.DisplayName ?? "player"; |
287 | 289 |
|
288 | | - bool newUser = !Config.KnownSteamIds.Contains(id); |
289 | | - if (newUser) |
| 290 | + bool isNewUser = !Config.KnownSteamIds.Contains(id); |
| 291 | + if (isNewUser) |
| 292 | + { |
290 | 293 | Config.KnownSteamIds.Add(id); |
291 | | - if (!string.IsNullOrEmpty(Config.MotdUrl) && newUser && Config.NewUserMotdUrl) |
| 294 | + } |
| 295 | + |
| 296 | + if (!string.IsNullOrEmpty(Config.MotdUrl) && isNewUser && Config.NewUserMotdUrl) |
292 | 297 | { |
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; |
297 | 301 | } |
298 | 302 |
|
299 | | - if (newUser && !string.IsNullOrEmpty(Config.NewUserMotd)) |
| 303 | + if (isNewUser && !string.IsNullOrEmpty(Config.NewUserMotd)) |
300 | 304 | { |
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 | + } |
302 | 309 |
|
| 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; |
303 | 341 | } |
304 | | - else if (!string.IsNullOrEmpty(Config.Motd)) |
| 342 | + catch (Exception e) |
305 | 343 | { |
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}"; |
307 | 348 | } |
308 | 349 | } |
309 | 350 |
|
|
0 commit comments