source: binary-improvements2/7dtd-server-fixes/src/ModApi.cs@ 403

Last change on this file since 403 was 403, checked in by alloc, 21 months ago

Made server-fixes no longer throw errors when running the mod on a listen server

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