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

Last change on this file since 445 was 443, checked in by alloc, 18 months ago

23_26_40 (A21)

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