Skip to content

Commit

Permalink
refactor: ReSharper
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-wos committed May 31, 2024
1 parent 240f59e commit b83f930
Show file tree
Hide file tree
Showing 13 changed files with 466 additions and 483 deletions.
12 changes: 6 additions & 6 deletions Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ public partial class Sessions
public override string ModuleAuthor => "github.com/oscar-wos/Sessions";
public override string ModuleVersion => "1.3.0";

public required IDatabase _database;
public readonly Ip _ip = new();
public ServerSQL? _server;
public Dictionary<int, PlayerSQL> _players = [];
public CounterStrikeSharp.API.Modules.Timers.Timer? _timer;
public required IDatabase Database;
public readonly Ip Ip = new();
public Server? Server;
public Dictionary<int, Player> Players = [];
public CounterStrikeSharp.API.Modules.Timers.Timer? Timer;
}

public enum MessageType : int
public enum MessageType
{
Chat = 0,
TeamChat = 1,
Expand Down
31 changes: 11 additions & 20 deletions IDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ namespace Sessions;

public interface IDatabase
{
string BuildConnectionString(SessionsConfig config);
Task CreateTablesAsync();

Task<ServerSQL> GetServerAsync(string serverIp, ushort serverPort);
Task<MapSQL> GetMapAsync(string mapName);
Task<PlayerSQL> GetPlayerAsync(ulong steamId);
Task<SessionSQL> GetSessionAsync(int playerId, int serverId, int mapId, string ip);
Task<AliasSQL?> GetAliasAsync(int playerId);
Task<Server> GetServerAsync(string serverIp, ushort serverPort);
Task<Map> GetMapAsync(string mapName);
Task<Player> GetPlayerAsync(ulong steamId);
Task<Session> GetSessionAsync(int playerId, int serverId, int mapId, string ip);
Task<Alias?> GetAliasAsync(int playerId);

void UpdateSeen(int playerId);
void UpdateSessions(List<int> playerIds, List<long> sessionIds);
Expand All @@ -21,27 +20,19 @@ public interface IDatabase
void InsertMessage(long sessionId, int playerId, MessageType messageType, string message);
}

public interface IDatabaseFactory
public class DatabaseFactory
{
IDatabase Database { get; }
}

public class DatabaseFactory : IDatabaseFactory
{
private readonly IDatabase _database;
private readonly ILogger _logger;

public DatabaseFactory(SessionsConfig config)
{
CheckConfig(config);

var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
_logger = loggerFactory.CreateLogger<DatabaseFactory>();
ILogger logger = loggerFactory.CreateLogger<DatabaseFactory>();

_database = config.DatabaseType switch
Database = config.DatabaseType switch
{
"postgresql" => new PostgreService(config, _logger),
"mysql" => new SqlService(config, _logger),
"postgresql" => new PostgresService(config, logger),
"mysql" => new SqlService(config, logger),
_ => throw new InvalidOperationException("Database type is not supported"),
};
}
Expand All @@ -54,5 +45,5 @@ private static void CheckConfig(SessionsConfig config)
throw new InvalidOperationException("Database is not set in the configuration file");
}

public IDatabase Database => _database;
public IDatabase Database { get; }
}
19 changes: 1 addition & 18 deletions IDatabaseQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface IDatabaseQueries
string InsertMessage { get; }
}

public abstract class Queries : IDatabaseQueries
public abstract class Queries
{
public abstract string CreateServers { get; }
public abstract string CreateMaps { get; }
Expand All @@ -36,23 +36,6 @@ public abstract class Queries : IDatabaseQueries
public abstract string CreateAliases { get; }
public abstract string CreateMessages { get; }

public abstract string SelectServer { get; }
public abstract string InsertServer { get; }

public abstract string SelectMap { get; }
public abstract string InsertMap { get; }

public abstract string SelectPlayer { get; }
public abstract string InsertPlayer { get; }

public abstract string InsertSession { get; }
public abstract string UpdateSession { get; }
public abstract string UpdateSeen { get; }

public abstract string SelectAlias { get; }
public abstract string InsertAlias { get; }
public abstract string InsertMessage { get; }

public IEnumerable<string> GetCreateQueries()
{
yield return CreateServers;
Expand Down
17 changes: 6 additions & 11 deletions Ip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,21 @@ namespace Sessions;

public class Ip
{
private delegate nint CNetworkSystem_UpdatePublicIp(nint a1);
private CNetworkSystem_UpdatePublicIp? _networkSystemUpdatePublicIp;
private readonly nint _networkSystem;

public Ip()
{
_networkSystem = NativeAPI.GetValveInterface(0, "NetworkSystemVersion001");
}
private delegate nint CNetworkSystemUpdatePublicIp(nint a1);
private CNetworkSystemUpdatePublicIp? _networkSystemUpdatePublicIp;
private readonly nint _networkSystem = NativeAPI.GetValveInterface(0, "NetworkSystemVersion001");

public string GetPublicIp()
{
unsafe
{
if (_networkSystemUpdatePublicIp == null)
{
nint funcPtr = *(nint*)(*(nint*)_networkSystem + 256);
_networkSystemUpdatePublicIp = Marshal.GetDelegateForFunctionPointer<CNetworkSystem_UpdatePublicIp>(funcPtr);
var funcPtr = *(nint*)(*(nint*)_networkSystem + 256);
_networkSystemUpdatePublicIp = Marshal.GetDelegateForFunctionPointer<CNetworkSystemUpdatePublicIp>(funcPtr);
}

byte* ipBytes = (byte*)(_networkSystemUpdatePublicIp(_networkSystem) + 4);
var ipBytes = (byte*)(_networkSystemUpdatePublicIp(_networkSystem) + 4);
return $"{ipBytes[0]}.{ipBytes[1]}.{ipBytes[2]}.{ipBytes[3]}";
}
}
Expand Down
Loading

0 comments on commit b83f930

Please sign in to comment.