|
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; |
|
25 | 27 | using VRage.Game.Entity; |
26 | 28 | using VRageMath; |
27 | 29 | using Newtonsoft.Json; |
| 30 | +using SpaceEngineers.Game.World; |
| 31 | +using VRage.Network; |
28 | 32 |
|
29 | 33 | namespace Essentials |
30 | 34 | { |
@@ -262,51 +266,84 @@ private void MotdOnce(IPlayer player) |
262 | 266 | if (_motdOnce.Contains(player.SteamId)) |
263 | 267 | return; |
264 | 268 |
|
265 | | - SendMotd(p); |
| 269 | + SendMotd(p, true); |
266 | 270 | _motdOnce.Add(player.SteamId); |
267 | 271 | }); |
268 | 272 | break; |
269 | 273 | } |
270 | 274 | }); |
271 | 275 | } |
272 | 276 |
|
273 | | - public void SendMotd(MyPlayer player) |
| 277 | + public void SendMotd(MyPlayer player, bool onSessionChanged) |
274 | 278 | { |
275 | 279 | long playerId = player.Identity.IdentityId; |
276 | 280 |
|
| 281 | + var motdUrl = MyGuiSandbox.IsUrlWhitelisted(Config.MotdUrl) |
| 282 | + ? Config.MotdUrl |
| 283 | + : $"https://steamcommunity.com/linkfilter/?url={Config.MotdUrl}"; |
| 284 | + |
277 | 285 | if (!string.IsNullOrEmpty(Config.MotdUrl) && !Config.NewUserMotdUrl) |
278 | 286 | { |
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; |
283 | 289 | } |
284 | 290 |
|
285 | 291 | 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 | + |
289 | 294 | string name = player.Identity?.DisplayName ?? "player"; |
290 | 295 |
|
291 | | - bool newUser = !Config.KnownSteamIds.Contains(id); |
292 | | - if (newUser) |
| 296 | + bool isNewUser = !Config.KnownSteamIds.Contains(id); |
| 297 | + if (isNewUser) |
| 298 | + { |
293 | 299 | 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)) |
295 | 309 | { |
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; |
300 | 313 | } |
301 | 314 |
|
302 | | - if (newUser && !string.IsNullOrEmpty(Config.NewUserMotd)) |
| 315 | + if (!string.IsNullOrEmpty(Config.Motd)) |
303 | 316 | { |
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 | + } |
305 | 321 |
|
| 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; |
306 | 340 | } |
307 | | - else if (!string.IsNullOrEmpty(Config.Motd)) |
| 341 | + catch (Exception e) |
308 | 342 | { |
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}"; |
310 | 347 | } |
311 | 348 | } |
312 | 349 |
|
|
0 commit comments