[324] | 1 | using System.Collections.Generic;
|
---|
[345] | 2 | using AllocsFixes.PersistentData;
|
---|
[455] | 3 | using JetBrains.Annotations;
|
---|
[369] | 4 | using Platform.Steam;
|
---|
[230] | 5 |
|
---|
[325] | 6 | namespace AllocsFixes {
|
---|
[455] | 7 | [UsedImplicitly]
|
---|
[324] | 8 | public class API : 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 |
|
---|
[506] | 18 | private static void GameAwake (ref ModEvents.SGameStartDoneData _data) {
|
---|
[369] | 19 | PersistentContainer.Load ();
|
---|
[230] | 20 | }
|
---|
| 21 |
|
---|
[506] | 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);
|
---|
[230] | 24 | }
|
---|
| 25 |
|
---|
[506] | 26 | private static void PlayerSpawning (ref ModEvents.SPlayerSpawningData _data) {
|
---|
[369] | 27 | string owner = null;
|
---|
[506] | 28 | if (_data.ClientInfo.PlatformId is UserIdentifierSteam identifierSteam) {
|
---|
[369] | 29 | owner = identifierSteam.OwnerId.ToString ();
|
---|
[345] | 30 | }
|
---|
[369] | 31 |
|
---|
[455] | 32 | Log.Out (
|
---|
[506] | 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}"
|
---|
[369] | 34 | );
|
---|
[230] | 35 | }
|
---|
[325] | 36 |
|
---|
[506] | 37 | private static void PlayerDisconnected (ref ModEvents.SPlayerDisconnectedData _data) {
|
---|
| 38 | Player p = PersistentContainer.Instance.Players.GetByInternalId (_data.ClientInfo.InternalId);
|
---|
[369] | 39 | if (p != null) {
|
---|
| 40 | p.SetOffline ();
|
---|
| 41 | } else {
|
---|
| 42 | Log.Out ("Disconnected player not found in client list...");
|
---|
| 43 | }
|
---|
[345] | 44 |
|
---|
[369] | 45 | PersistentContainer.Instance.Save ();
|
---|
[230] | 46 | }
|
---|
| 47 |
|
---|
[506] | 48 | private static void PlayerSpawned (ref ModEvents.SPlayerSpawnedInWorldData _data) {
|
---|
| 49 | if (_data.ClientInfo == null) {
|
---|
[488] | 50 | return;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[506] | 53 | PersistentContainer.Instance.Players.GetOrCreate (_data.ClientInfo.InternalId, _data.ClientInfo.PlatformId, _data.ClientInfo.CrossplatformId).SetOnline (_data.ClientInfo);
|
---|
[369] | 54 | PersistentContainer.Instance.Save ();
|
---|
[345] | 55 | }
|
---|
| 56 |
|
---|
[455] | 57 | private const string ANSWER = " [ff0000]I[-] [ff7f00]W[-][ffff00]A[-][80ff00]S[-] [00ffff]H[-][0080ff]E[-][0000ff]R[-][8b00ff]E[-]";
|
---|
[345] | 58 |
|
---|
[506] | 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;
|
---|
[345] | 62 | }
|
---|
| 63 |
|
---|
[506] | 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));
|
---|
[345] | 67 | } else {
|
---|
[506] | 68 | Log.Error ($"ChatHookExample: Argument _cInfo null on message: {_data.Message}");
|
---|
[345] | 69 | }
|
---|
| 70 |
|
---|
[506] | 71 | return ModEvents.EModEventResult.StopHandlersAndVanilla;
|
---|
[238] | 72 | }
|
---|
[230] | 73 | }
|
---|
[325] | 74 | }
|
---|