source: binary-improvements/7dtd-server-fixes/src/API.cs@ 506

Last change on this file since 506 was 506, checked in by alloc, 3 days ago

28_36_50: 2.0 compatibility changes

File size: 3.1 KB
Line 
1using System.Collections.Generic;
2using AllocsFixes.PersistentData;
3using JetBrains.Annotations;
4using Platform.Steam;
5
6namespace AllocsFixes {
7 [UsedImplicitly]
8 public class API : IModApi {
9 public void InitMod (Mod _modInstance) {
10 ModEvents.GameStartDone.RegisterHandler (GameAwake);
11 ModEvents.SavePlayerData.RegisterHandler (SavePlayerData);
12 ModEvents.PlayerSpawning.RegisterHandler (PlayerSpawning);
13 ModEvents.PlayerDisconnected.RegisterHandler (PlayerDisconnected);
14 ModEvents.PlayerSpawnedInWorld.RegisterHandler (PlayerSpawned);
15 ModEvents.ChatMessage.RegisterHandler (ChatMessage);
16 }
17
18 private static void GameAwake (ref ModEvents.SGameStartDoneData _data) {
19 PersistentContainer.Load ();
20 }
21
22 private static void SavePlayerData (ref ModEvents.SSavePlayerDataData _data) {
23 PersistentContainer.Instance.Players.GetOrCreate (_data.ClientInfo.InternalId, _data.ClientInfo.PlatformId, _data.ClientInfo.CrossplatformId).Update (_data.ClientInfo, _data.PlayerDataFile);
24 }
25
26 private static void PlayerSpawning (ref ModEvents.SPlayerSpawningData _data) {
27 string owner = null;
28 if (_data.ClientInfo.PlatformId is UserIdentifierSteam identifierSteam) {
29 owner = identifierSteam.OwnerId.ToString ();
30 }
31
32 Log.Out (
33 $"Player connected, entityid={_data.ClientInfo.entityId}, name={_data.ClientInfo.playerName}, pltfmid={_data.ClientInfo.PlatformId?.CombinedString ?? "<unknown>"}, crossid={_data.ClientInfo.CrossplatformId?.CombinedString ?? "<unknown/none>"}, steamOwner={owner ?? "<unknown/none>"}, ip={_data.ClientInfo.ip}"
34 );
35 }
36
37 private static void PlayerDisconnected (ref ModEvents.SPlayerDisconnectedData _data) {
38 Player p = PersistentContainer.Instance.Players.GetByInternalId (_data.ClientInfo.InternalId);
39 if (p != null) {
40 p.SetOffline ();
41 } else {
42 Log.Out ("Disconnected player not found in client list...");
43 }
44
45 PersistentContainer.Instance.Save ();
46 }
47
48 private static void PlayerSpawned (ref ModEvents.SPlayerSpawnedInWorldData _data) {
49 if (_data.ClientInfo == null) {
50 return;
51 }
52
53 PersistentContainer.Instance.Players.GetOrCreate (_data.ClientInfo.InternalId, _data.ClientInfo.PlatformId, _data.ClientInfo.CrossplatformId).SetOnline (_data.ClientInfo);
54 PersistentContainer.Instance.Save ();
55 }
56
57 private const string ANSWER = " [ff0000]I[-] [ff7f00]W[-][ffff00]A[-][80ff00]S[-] [00ffff]H[-][0080ff]E[-][0000ff]R[-][8b00ff]E[-]";
58
59 private static ModEvents.EModEventResult ChatMessage (ref ModEvents.SChatMessageData _data) {
60 if (string.IsNullOrEmpty (_data.Message) || !_data.Message.EqualsCaseInsensitive ("/alloc")) {
61 return ModEvents.EModEventResult.Continue;
62 }
63
64 if (_data.ClientInfo != null) {
65 Log.Out ($"Sent chat hook reply to {_data.ClientInfo.InternalId}");
66 _data.ClientInfo.SendPackage (NetPackageManager.GetPackage<NetPackageChat> ().Setup (EChatType.Whisper, -1, ANSWER, null, EMessageSender.Server, GeneratedTextManager.BbCodeSupportMode.Supported));
67 } else {
68 Log.Error ($"ChatHookExample: Argument _cInfo null on message: {_data.Message}");
69 }
70
71 return ModEvents.EModEventResult.StopHandlersAndVanilla;
72 }
73 }
74}
Note: See TracBrowser for help on using the repository browser.