1 | using System.Collections.Generic;
|
---|
2 | using AllocsFixes.PersistentData;
|
---|
3 | using JetBrains.Annotations;
|
---|
4 | using Platform.Steam;
|
---|
5 |
|
---|
6 | namespace 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 () {
|
---|
19 | PersistentContainer.Load ();
|
---|
20 | }
|
---|
21 |
|
---|
22 | private static void SavePlayerData (ClientInfo _cInfo, PlayerDataFile _playerDataFile) {
|
---|
23 | PersistentContainer.Instance.Players.GetOrCreate (_cInfo.InternalId, _cInfo.PlatformId, _cInfo.CrossplatformId).Update (_cInfo, _playerDataFile);
|
---|
24 | }
|
---|
25 |
|
---|
26 | private static void PlayerSpawning (ClientInfo _cInfo, int _chunkViewDim, PlayerProfile _playerProfile) {
|
---|
27 | string owner = null;
|
---|
28 | if (_cInfo.PlatformId is UserIdentifierSteam identifierSteam) {
|
---|
29 | owner = identifierSteam.OwnerId.ToString ();
|
---|
30 | }
|
---|
31 |
|
---|
32 | Log.Out (
|
---|
33 | $"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}"
|
---|
34 | );
|
---|
35 | }
|
---|
36 |
|
---|
37 | private static void PlayerDisconnected (ClientInfo _cInfo, bool _bShutdown) {
|
---|
38 | Player p = PersistentContainer.Instance.Players.GetByInternalId (_cInfo.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 (ClientInfo _cInfo, RespawnType _respawnReason, Vector3i _spawnPos) {
|
---|
49 | PersistentContainer.Instance.Players.GetOrCreate (_cInfo.InternalId, _cInfo.PlatformId, _cInfo.CrossplatformId).SetOnline (_cInfo);
|
---|
50 | PersistentContainer.Instance.Save ();
|
---|
51 | }
|
---|
52 |
|
---|
53 | private const string ANSWER = " [ff0000]I[-] [ff7f00]W[-][ffff00]A[-][80ff00]S[-] [00ffff]H[-][0080ff]E[-][0000ff]R[-][8b00ff]E[-]";
|
---|
54 |
|
---|
55 | private static bool ChatMessage (ClientInfo _cInfo, EChatType _type, int _senderId, string _msg, string _mainName,
|
---|
56 | bool _localizeMain, List<int> _recipientEntityIds) {
|
---|
57 | if (string.IsNullOrEmpty (_msg) || !_msg.EqualsCaseInsensitive ("/alloc")) {
|
---|
58 | return true;
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (_cInfo != null) {
|
---|
62 | Log.Out ($"Sent chat hook reply to {_cInfo.InternalId}");
|
---|
63 | _cInfo.SendPackage (NetPackageManager.GetPackage<NetPackageChat> ().Setup (EChatType.Whisper, -1, ANSWER, "", false, null));
|
---|
64 | } else {
|
---|
65 | Log.Error ($"ChatHookExample: Argument _cInfo null on message: {_msg}");
|
---|
66 | }
|
---|
67 |
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|