Skip to content

Fix warnings #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Uchu.Auth/Handlers/LoginHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public async Task LoginRequestHandler(ClientLoginInfoPacket packet, IRakConnecti

var info = new ServerLoginInfoPacket
{
CharacterInstanceAddress = Server.Host,
CharacterInstanceAddress = UchuServer.Host,
CharacterInstancePort = ushort.MaxValue,
ChatInstanceAddress = Server.Host,
ChatInstanceAddress = UchuServer.Host,
ChatInstancePort = 2004
};

var characterSpecification = await Server.Api.RunCommandAsync<InstanceInfoResponse>(
Server.MasterApi, $"instance/basic?t={(int) ServerType.Character}"
var characterSpecification = await UchuServer.Api.RunCommandAsync<InstanceInfoResponse>(
UchuServer.MasterApi, $"instance/basic?t={(int) ServerType.Character}"
).ConfigureAwait(false);

if (!characterSpecification.Success)
Expand Down Expand Up @@ -72,7 +72,7 @@ public async Task LoginRequestHandler(ClientLoginInfoPacket packet, IRakConnecti
}
else
{
var key = Server.SessionCache.CreateSession(user.Id);
var key = UchuServer.SessionCache.CreateSession(user.Id);

info.LoginCode = LoginCode.Success;
info.UserKey = key;
Expand Down
20 changes: 10 additions & 10 deletions Uchu.Char/Handlers/CharacterHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ private static async Task SendCharacterList(IRakConnection connection, long user
[PacketHandler]
public async Task CharacterList(CharacterListRequest packet, IRakConnection connection)
{
var session = Server.SessionCache.GetSession(connection.EndPoint);
var session = UchuServer.SessionCache.GetSession(connection.EndPoint);
await SendCharacterList(connection, session.UserId);
}

[PacketHandler]
public async Task CharacterCreate(CharacterCreateRequest packet, IRakConnection connection)
{
var session = Server.SessionCache.GetSession(connection.EndPoint);
var session = UchuServer.SessionCache.GetSession(connection.EndPoint);

uint shirtLot;
uint pantsLot;
Expand All @@ -99,9 +99,9 @@ public async Task CharacterCreate(CharacterCreateRequest packet, IRakConnection
pantsLot = (uint) (pants != null ? pants.Id : 2508); // Select 'Bright Red Pants' if not found.
}

var first = (await Server.Resources.ReadTextAsync("names/minifigname_first.txt")).Split('\n');
var middle = (await Server.Resources.ReadTextAsync("names/minifigname_middle.txt")).Split('\n');
var last = (await Server.Resources.ReadTextAsync("names/minifigname_last.txt")).Split('\n');
var first = (await UchuServer.Resources.ReadTextAsync("names/minifigname_first.txt")).Split('\n');
var middle = (await UchuServer.Resources.ReadTextAsync("names/minifigname_middle.txt")).Split('\n');
var last = (await UchuServer.Resources.ReadTextAsync("names/minifigname_last.txt")).Split('\n');

var name = (
first[packet.Predefined.First] +
Expand Down Expand Up @@ -213,7 +213,7 @@ public async Task DeleteCharacter(CharacterDeleteRequest packet, IRakConnection
[PacketHandler]
public async Task RenameCharacter(CharacterRenameRequest packet, IRakConnection connection)
{
var session = Server.SessionCache.GetSession(connection.EndPoint);
var session = UchuServer.SessionCache.GetSession(connection.EndPoint);

await using var ctx = new UchuContext();

Expand Down Expand Up @@ -246,7 +246,7 @@ public async Task RenameCharacter(CharacterRenameRequest packet, IRakConnection
[PacketHandler]
public async Task JoinWorld(JoinWorldRequest packet, IRakConnection connection)
{
Server.SessionCache.SetCharacter(connection.EndPoint, packet.CharacterId);
UchuServer.SessionCache.SetCharacter(connection.EndPoint, packet.CharacterId);

await using var ctx = new UchuContext();
var character = await ctx.Characters.FirstAsync(c => c.Id == packet.CharacterId);
Expand All @@ -260,11 +260,11 @@ public async Task JoinWorld(JoinWorldRequest packet, IRakConnection connection)
var _ = Task.Run(async () =>
{
// Request world server.
var server = await ServerHelper.RequestWorldServerAsync(Server, requestZone);
var server = await ServerHelper.RequestWorldServerAsync(UchuServer, requestZone);
if (server == default)
{
// If there's no server available, error
var session = Server.SessionCache.GetSession(connection.EndPoint);
var session = UchuServer.SessionCache.GetSession(connection.EndPoint);
await SendCharacterList(connection, session.UserId);

return;
Expand All @@ -273,7 +273,7 @@ public async Task JoinWorld(JoinWorldRequest packet, IRakConnection connection)
// Send to world server.
connection.Send(new ServerRedirectionPacket
{
Address = Server.Host,
Address = UchuServer.Host,
Port = (ushort) server.Port
});
});
Expand Down
8 changes: 4 additions & 4 deletions Uchu.Core/Api/InstanceCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ namespace Uchu.Core.Api
{
public class InstanceCommands
{
public Server Server { get; }
public UchuServer UchuServer { get; }

public InstanceCommands(Server server)
public InstanceCommands(UchuServer uchuServer)
{
Server = server;
UchuServer = uchuServer;
}

[ApiCommand("server/verify")]
public object ReadySetupCallback()
{
var response = new ReadyCallbackResponse();

if (Server.Id == Guid.Empty)
if (UchuServer.Id == Guid.Empty)
{
response.FailedReason = "invalid id";

Expand Down
41 changes: 25 additions & 16 deletions Uchu.Core/Cache/DatabaseCache.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Uchu.Core.Resources;

namespace Uchu.Core
{
Expand All @@ -23,7 +25,6 @@ public string CreateSession(long userId)
using var ctx = new UchuContext();

var key = GenerateKey();

ctx.SessionCaches.Add(new SessionCache
{
Key = key,
Expand All @@ -50,45 +51,50 @@ public void CreateSession(long userId, string key)

public void SetCharacter(IPEndPoint endpoint, long characterId)
{
if (endpoint == null)
throw new ArgumentNullException(nameof(endpoint),
ResourceStrings.DatabaseCache_SetCharacter_NullEndpointException);

using var ctx = new UchuContext();

var key = _keys[endpoint.ToString()];

var session = ctx.SessionCaches.First(s => s.Key == key);

session.CharacterId = characterId;

ctx.SaveChanges();
}

public void SetZone(IPEndPoint endpoint, ZoneId zone)
{
if (endpoint == null)
throw new ArgumentNullException(nameof(endpoint),
ResourceStrings.DatabaseCache_SetZone_NullEndpointException);

using var ctx = new UchuContext();

var key = _keys[endpoint.ToString()];

var session = ctx.SessionCaches.First(s => s.Key == key);

session.ZoneId = (int) zone;
session.ZoneId = zone;

ctx.SaveChanges();
}

public Session GetSession(IPEndPoint endpoint)
{
var task = GetSessionAsync(endpoint);

task.Wait();

return task.Result;
}

[SuppressMessage("ReSharper", "CA2000")]
public async Task<Session> GetSessionAsync(IPEndPoint endpoint)
{
if (endpoint == null)
throw new ArgumentNullException(nameof(endpoint),
ResourceStrings.DatabaseCache_GetSessionAsync_NullEndpointException);

await using var ctx = new UchuContext();

string key;

string key;
var timeout = 1000;

while (!_keys.TryGetValue(endpoint.ToString(), out key))
Expand All @@ -115,23 +121,28 @@ public async Task<Session> GetSessionAsync(IPEndPoint endpoint)
public bool IsKey(string key)
{
using var ctx = new UchuContext();

return ctx.SessionCaches.Any(c => c.Key == key);
}

public void RegisterKey(IPEndPoint endPoint, string key)
{
if (endPoint == null)
throw new ArgumentNullException(nameof(endPoint),
ResourceStrings.DatabaseCache_RegisterKey_NullEndpointException);

_keys.Add(endPoint.ToString(), key);
}

public void DeleteSession(IPEndPoint endpoint)
{
if (endpoint == null)
throw new ArgumentNullException(nameof(endpoint),
ResourceStrings.DatabaseCache_DeleteSession_NullEndpointException);

using var ctx = new UchuContext();

var key = _keys[endpoint.ToString()];

var session = ctx.SessionCaches.First(s => s.Key == key);

ctx.SessionCaches.Remove(session);

ctx.SaveChanges();
Expand All @@ -140,9 +151,7 @@ public void DeleteSession(IPEndPoint endpoint)
private string GenerateKey(int length = 24)
{
var bytes = new byte[length];

_rng.GetBytes(bytes);

return Convert.ToBase64String(bytes).TrimEnd('=');
}

Expand Down
49 changes: 36 additions & 13 deletions Uchu.Core/Cache/RedisSessionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Security.Cryptography;
using System.Threading.Tasks;
using StackExchange.Redis;
using Uchu.Core.Config;
using Uchu.Core.Resources;

namespace Uchu.Core
{
Expand All @@ -22,10 +24,17 @@ public sealed class RedisSessionCache : ISessionCache, IDisposable
/// <param name="config">The Redis cache configuration</param>
public RedisSessionCache(CacheConfig config)
{
var manager = ConnectionMultiplexer.Connect($"{config.Host}:{config.Port}");
_client = manager.GetDatabase();

_rng = new RNGCryptoServiceProvider();
if (config != null)
{
var manager = ConnectionMultiplexer.Connect($"{config.Host}:{config.Port}");
_client = manager.GetDatabase();
_rng = new RNGCryptoServiceProvider();
}
else
{
throw new ArgumentNullException(nameof(config),
ResourceStrings.RedisSessionCache_ConfigNullException);
}
}

/// <summary>
Expand Down Expand Up @@ -67,10 +76,12 @@ public void CreateSession(long userId, string key)
/// <param name="characterId">The character to link to the connection</param>
public void SetCharacter(IPEndPoint endpoint, long characterId)
{
if (endpoint == null)
throw new ArgumentNullException(nameof(endpoint),
ResourceStrings.RedisSessionCache_SetCharacter_EndpointNullException);

var session = GetSession(endpoint);

session.CharacterId = characterId;

_client.StringSet(_keys[endpoint.ToString()], session.ToBytes(), TimeSpan.FromDays(1));
}

Expand All @@ -81,10 +92,13 @@ public void SetCharacter(IPEndPoint endpoint, long characterId)
/// <param name="zone">The zone to link to the connection</param>
public void SetZone(IPEndPoint endpoint, ZoneId zone)
{
if (endpoint == null)
throw new ArgumentNullException(nameof(endpoint),
ResourceStrings.RedisSessionCache_SetZone_EndpointNullException);

var session = GetSession(endpoint);

session.ZoneId = (ushort) zone;

session.ZoneId = zone;

_client.StringSet(_keys[endpoint.ToString()], session.ToBytes(), TimeSpan.FromDays(1));
}

Expand All @@ -102,10 +116,12 @@ public void SetZone(IPEndPoint endpoint, ZoneId zone)
/// <returns>If available, the session that belongs to the endpoint</returns>
public async Task<Session> GetSessionAsync(IPEndPoint endpoint)
{
if (endpoint == null)
throw new ArgumentNullException(nameof(endpoint),
ResourceStrings.RedisSessioncache_GetSessionAsync_EndpointNullException);
string key;

var timeout = 1000;

while (!_keys.TryGetValue(endpoint.ToString(), out key))
{
if (timeout <= 0)
Expand Down Expand Up @@ -136,6 +152,9 @@ public bool IsKey(string key)
/// <param name="key">The key to register for the connection</param>
public void RegisterKey(IPEndPoint endPoint, string key)
{
if (endPoint == null)
throw new ArgumentNullException(nameof(endPoint),
ResourceStrings.RedisSessionCache_RegisterKey_EndpointNullException);
_keys.Add(endPoint.ToString(), key);
}

Expand All @@ -144,7 +163,13 @@ public void RegisterKey(IPEndPoint endPoint, string key)
/// </summary>
/// <param name="endpoint">The user connection to remove from the cache</param>
public void DeleteSession(IPEndPoint endpoint)
=> _client.KeyDelete(endpoint.ToString());
{
if (endpoint == null)
throw new ArgumentNullException(nameof(endpoint),
ResourceStrings.RedisSessionCache_DeleteSession_EndpointNullException);

_client.KeyDelete(endpoint.ToString());
}

/// <summary>
/// Generates a random key
Expand All @@ -154,9 +179,7 @@ public void DeleteSession(IPEndPoint endpoint)
private string GenerateKey(int length = 24)
{
var bytes = new byte[length];

_rng.GetBytes(bytes);

return Convert.ToBase64String(bytes).TrimEnd('=');
}

Expand Down
2 changes: 2 additions & 0 deletions Uchu.Core/Client/Models/UGBehaviorSounds.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;

namespace Uchu.Core.Client
{
[Table("UGBehaviorSounds")]
[SuppressMessage("ReSharper", "CA1720")]
public class UGBehaviorSounds
{
[Key] [Column("efId")]
Expand Down
Loading