[107] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[111] | 3 | using System.Text.RegularExpressions;
|
---|
[203] | 4 | using Steamworks;
|
---|
[107] | 5 |
|
---|
[130] | 6 | namespace AllocsFixes
|
---|
[107] | 7 | {
|
---|
[130] | 8 | public class CommonMappingFunctions
|
---|
[107] | 9 | {
|
---|
[130] | 10 | public static ConnectionManager GetConnectionManager ()
|
---|
| 11 | {
|
---|
| 12 | return ConnectionManager.Instance;
|
---|
| 13 | }
|
---|
[107] | 14 |
|
---|
[130] | 15 | public static GameManager GetGameManager ()
|
---|
| 16 | {
|
---|
| 17 | return GetConnectionManager ().gameManager;
|
---|
| 18 | }
|
---|
[107] | 19 |
|
---|
[130] | 20 | public static string GetPlayerName (ClientInfo _ci)
|
---|
| 21 | {
|
---|
| 22 | try {
|
---|
| 23 | int entityId = GetConnectionManager ().mapClientToEntity [_ci.clientId];
|
---|
| 24 | return GetGameManager ().World.playerEntities.dict [entityId].EntityName;
|
---|
| 25 | } catch (Exception e) {
|
---|
| 26 | Log.Out ("Error getting player name for ClientInfo: " + e);
|
---|
| 27 | }
|
---|
| 28 | return null;
|
---|
[107] | 29 | }
|
---|
| 30 |
|
---|
[130] | 31 | public static EntityPlayer GetEntityPlayer (ClientInfo _ci)
|
---|
| 32 | {
|
---|
| 33 | try {
|
---|
| 34 | int entityId = GetConnectionManager ().mapClientToEntity [_ci.clientId];
|
---|
| 35 | return GetGameManager ().World.playerEntities.dict [entityId];
|
---|
| 36 | } catch (Exception e) {
|
---|
| 37 | Log.Out ("Error getting entity player for ClientInfo: " + e);
|
---|
| 38 | }
|
---|
| 39 | return null;
|
---|
[107] | 40 | }
|
---|
| 41 |
|
---|
[130] | 42 | public static string GetSteamID (ClientInfo _ci)
|
---|
| 43 | {
|
---|
[203] | 44 | return Steam.Authentication.Server.GetPlayerId (GetPlayerName (_ci));
|
---|
[130] | 45 | }
|
---|
[107] | 46 |
|
---|
[130] | 47 | public static string GetSteamID (string _playerName)
|
---|
| 48 | {
|
---|
[203] | 49 | return Steam.Authentication.Server.GetPlayerId (_playerName);
|
---|
[130] | 50 | }
|
---|
[128] | 51 |
|
---|
[130] | 52 | public static int GetClientID (ClientInfo _ci)
|
---|
| 53 | {
|
---|
| 54 | if (_ci != null) {
|
---|
| 55 | if (GetConnectionManager ().connectedClients.ContainsKey (_ci.clientId))
|
---|
| 56 | return _ci.clientId;
|
---|
| 57 | }
|
---|
| 58 | return -1;
|
---|
[110] | 59 | }
|
---|
[107] | 60 |
|
---|
[130] | 61 | public static int GetEntityID (ClientInfo _ci)
|
---|
| 62 | {
|
---|
| 63 | try {
|
---|
| 64 | ConnectionManager cm = GetConnectionManager ();
|
---|
[107] | 65 |
|
---|
[130] | 66 | if (cm.mapClientToEntity.ContainsKey (_ci.clientId))
|
---|
| 67 | return cm.mapClientToEntity [_ci.clientId];
|
---|
| 68 | else
|
---|
| 69 | return -1;
|
---|
| 70 | } catch (Exception e) {
|
---|
| 71 | Log.Out ("Error getting entity ID for ClientInfo: " + e);
|
---|
| 72 | }
|
---|
| 73 | return -1;
|
---|
[107] | 74 | }
|
---|
| 75 |
|
---|
[130] | 76 | public static ClientInfo GetClientInfoFromEntityID (int _entityId)
|
---|
| 77 | {
|
---|
| 78 | try {
|
---|
| 79 | ConnectionManager cm = GetConnectionManager ();
|
---|
[107] | 80 |
|
---|
[130] | 81 | if (cm.mapClientToEntity.ContainsValue (_entityId)) {
|
---|
| 82 | foreach (KeyValuePair<int, int> kvp in cm.mapClientToEntity) {
|
---|
| 83 | if (kvp.Value == _entityId) {
|
---|
| 84 | return cm.connectedClients [kvp.Key];
|
---|
| 85 | }
|
---|
[107] | 86 | }
|
---|
| 87 | }
|
---|
[130] | 88 |
|
---|
| 89 | return null;
|
---|
| 90 | } catch (Exception e) {
|
---|
| 91 | Log.Out ("Error getting ClientInfo for entity ID: " + e);
|
---|
[107] | 92 | }
|
---|
| 93 | return null;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[130] | 96 | public static ClientInfo GetClientInfoFromClientID (int _clientId)
|
---|
| 97 | {
|
---|
| 98 | try {
|
---|
| 99 | ConnectionManager cm = GetConnectionManager ();
|
---|
[107] | 100 |
|
---|
[130] | 101 | if (cm.connectedClients.ContainsKey (_clientId))
|
---|
| 102 | return cm.connectedClients [_clientId];
|
---|
| 103 | else
|
---|
| 104 | return null;
|
---|
| 105 | } catch (Exception e) {
|
---|
| 106 | Log.Out ("Error getting ClientInfo for client ID: " + e);
|
---|
| 107 | }
|
---|
| 108 | return null;
|
---|
[107] | 109 | }
|
---|
| 110 |
|
---|
[130] | 111 | public static ClientInfo GetClientInfoFromPlayerName (string _playerName, bool ignoreColorcodes)
|
---|
| 112 | {
|
---|
| 113 | try {
|
---|
| 114 | ConnectionManager cm = GetConnectionManager ();
|
---|
[107] | 115 |
|
---|
[130] | 116 | _playerName = _playerName.ToLower ();
|
---|
[111] | 117 | if (ignoreColorcodes) {
|
---|
[130] | 118 | _playerName = Regex.Replace (_playerName, "\\[[0-9a-fA-F]{6}\\]", "");
|
---|
[111] | 119 | }
|
---|
[130] | 120 | foreach (ClientInfo ci in cm.connectedClients.Values) {
|
---|
| 121 | string curName = GetPlayerName (ci).ToLower ();
|
---|
| 122 | if (ignoreColorcodes) {
|
---|
| 123 | curName = Regex.Replace (curName, "\\[[0-9a-fA-F]{6}\\]", "");
|
---|
| 124 | }
|
---|
| 125 | if (curName.Equals (_playerName)) {
|
---|
| 126 | return ci;
|
---|
| 127 | }
|
---|
[107] | 128 | }
|
---|
[130] | 129 | } catch (Exception e) {
|
---|
| 130 | Log.Out ("Error getting ClientInfo for player name: " + e);
|
---|
[107] | 131 | }
|
---|
[130] | 132 | return null;
|
---|
[107] | 133 | }
|
---|
| 134 |
|
---|
[130] | 135 | public static ClientInfo GetClientInfoFromNameOrID (string _nameOrId, bool ignoreColorcodes)
|
---|
| 136 | {
|
---|
| 137 | try {
|
---|
[144] | 138 | long tempLong;
|
---|
| 139 | if (_nameOrId.Length == 17 && long.TryParse (_nameOrId, out tempLong)) {
|
---|
| 140 | return GetClientInfoFromSteamID (_nameOrId);
|
---|
| 141 | } else {
|
---|
| 142 | int entityId = -1;
|
---|
| 143 | if (int.TryParse (_nameOrId, out entityId)) {
|
---|
| 144 | ClientInfo ci = GetClientInfoFromEntityID (entityId);
|
---|
| 145 | if (ci != null)
|
---|
| 146 | return ci;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | return GetClientInfoFromPlayerName (_nameOrId, ignoreColorcodes);
|
---|
[130] | 150 | }
|
---|
| 151 | } catch (Exception e) {
|
---|
[144] | 152 | Log.Out ("Error getting ClientInfo for steam ID / entity ID / player name \"" + _nameOrId + "\": " + e);
|
---|
[107] | 153 | }
|
---|
[130] | 154 | return null;
|
---|
[107] | 155 | }
|
---|
| 156 |
|
---|
[130] | 157 | public static ClientInfo GetClientInfoFromSteamID (string _steamId)
|
---|
| 158 | {
|
---|
| 159 | try {
|
---|
[211] | 160 | foreach (string name in Steam.Authentication.Server.usersToIDs.Keys) {
|
---|
| 161 | string curId = string.Empty + Steam.Authentication.Server.usersToIDs[name].steamID.m_SteamID;
|
---|
[130] | 162 | if (curId.Equals (_steamId)) {
|
---|
[211] | 163 | return GetClientInfoFromPlayerName (name, false);
|
---|
[130] | 164 | }
|
---|
[107] | 165 | }
|
---|
[130] | 166 | } catch (Exception e) {
|
---|
| 167 | Log.Out ("Error getting ClientInfo for steam ID: " + e);
|
---|
[107] | 168 | }
|
---|
[130] | 169 | return null;
|
---|
[107] | 170 | }
|
---|
[130] | 171 |
|
---|
[107] | 172 | }
|
---|
| 173 | }
|
---|