[324] | 1 | using System.Collections.Generic;
|
---|
[345] | 2 | using AllocsFixes.PersistentData;
|
---|
[391] | 3 | using JetBrains.Annotations;
|
---|
[369] | 4 | using Platform.Steam;
|
---|
[230] | 5 |
|
---|
[325] | 6 | namespace 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) {
|
---|
[371] | 23 | PersistentContainer.Instance.Players [_cInfo.InternalId, true].Update (_playerDataFile);
|
---|
[230] | 24 | }
|
---|
| 25 |
|
---|
[391] | 26 | private void PlayerSpawning (ClientInfo _cInfo, int _chunkViewDim, PlayerProfile _playerProfile) {
|
---|
[369] | 27 | string owner = null;
|
---|
| 28 | if (_cInfo.PlatformId is UserIdentifierSteam identifierSteam) {
|
---|
| 29 | owner = identifierSteam.OwnerId.ToString ();
|
---|
[345] | 30 | }
|
---|
[369] | 31 |
|
---|
| 32 | Log.Out ("Player connected" +
|
---|
| 33 | ", entityid=" + _cInfo.entityId +
|
---|
| 34 | ", name=" + _cInfo.playerName +
|
---|
| 35 | ", pltfmid=" + (_cInfo.PlatformId?.CombinedString ?? "<unknown>") +
|
---|
| 36 | ", crossid=" + (_cInfo.CrossplatformId?.CombinedString ?? "<unknown/none>") +
|
---|
| 37 | ", steamOwner=" + (owner ?? "<unknown/none>") +
|
---|
| 38 | ", ip=" + _cInfo.ip
|
---|
| 39 | );
|
---|
[230] | 40 | }
|
---|
[325] | 41 |
|
---|
[391] | 42 | private void PlayerDisconnected (ClientInfo _cInfo, bool _bShutdown) {
|
---|
[371] | 43 | Player p = PersistentContainer.Instance.Players [_cInfo.InternalId, false];
|
---|
[369] | 44 | if (p != null) {
|
---|
| 45 | p.SetOffline ();
|
---|
| 46 | } else {
|
---|
| 47 | Log.Out ("Disconnected player not found in client list...");
|
---|
| 48 | }
|
---|
[345] | 49 |
|
---|
[369] | 50 | PersistentContainer.Instance.Save ();
|
---|
[230] | 51 | }
|
---|
| 52 |
|
---|
[391] | 53 | private void PlayerSpawned (ClientInfo _cInfo, RespawnType _respawnReason, Vector3i _spawnPos) {
|
---|
[371] | 54 | PersistentContainer.Instance.Players [_cInfo.InternalId, true].SetOnline (_cInfo);
|
---|
[369] | 55 | PersistentContainer.Instance.Save ();
|
---|
[345] | 56 | }
|
---|
| 57 |
|
---|
[391] | 58 | private const string testChatAnswer =
|
---|
[345] | 59 | " [ff0000]I[-] [ff7f00]W[-][ffff00]A[-][80ff00]S[-] [00ffff]H[-][0080ff]E[-][0000ff]R[-][8b00ff]E[-]";
|
---|
| 60 |
|
---|
[391] | 61 | private bool ChatMessage (ClientInfo _cInfo, EChatType _type, int _senderId, string _msg, string _mainName,
|
---|
[325] | 62 | bool _localizeMain, List<int> _recipientEntityIds) {
|
---|
[345] | 63 | if (string.IsNullOrEmpty (_msg) || !_msg.EqualsCaseInsensitive ("/alloc")) {
|
---|
| 64 | return true;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | if (_cInfo != null) {
|
---|
[369] | 68 | Log.Out ("Sent chat hook reply to {0}", _cInfo.InternalId);
|
---|
[391] | 69 | _cInfo.SendPackage (NetPackageManager.GetPackage<NetPackageChat> ().Setup (EChatType.Whisper, -1, testChatAnswer, "", false, null));
|
---|
[345] | 70 | } else {
|
---|
| 71 | Log.Error ("ChatHookExample: Argument _cInfo null on message: {0}", _msg);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | return false;
|
---|
[238] | 75 | }
|
---|
[230] | 76 | }
|
---|
[325] | 77 | }
|
---|