Skip to content

Commit

Permalink
Initial 508 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JayArrowz committed Jun 15, 2021
1 parent 12f629d commit 92e7b38
Show file tree
Hide file tree
Showing 36 changed files with 1,069 additions and 49 deletions.
25 changes: 21 additions & 4 deletions NetScape.Abstractions/Extensions/ByteBufferExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace NetScape.Abstractions.Extensions
{
public static class ByteBufferExtensions
{
static Encoding Iso_8859 { get; } = Encoding.GetEncoding("ISO-8859-1");

public static string ReadString(this IByteBuffer buffer)
{
return ReadString(buffer, 10);
Expand All @@ -23,6 +25,21 @@ public static string ReadString(this IByteBuffer buffer, int terminator)
return strBldr.ToString();
}

public static void WriteSmart(this IByteBuffer buffer, int value)
{
if (value < 128)
buffer.WriteByte(value);
else
buffer.WriteShort(32768 + value);
}

public static void Write8859String(this IByteBuffer buffer, string str)
{
byte[] bytes = Iso_8859.GetBytes(str);
buffer.WriteBytes(bytes);
buffer.WriteByte(0);
}

public static IByteBuffer CompressGzip(this IByteBuffer buffer)
{
var data = buffer.GetBytes();
Expand Down Expand Up @@ -79,8 +96,8 @@ public static void DecodeXtea(this IByteBuffer buffer, uint[] keys, int start, i
k1 -= (l1 >> 5 ^ l1 << 4) + l1 ^ keys[sum & 3] + sum;
}
buffer.SetReaderIndex(buffer.ReaderIndex - 8);
buffer.WriteInt((int) k1);
buffer.WriteInt((int) l1);
buffer.WriteInt((int)k1);
buffer.WriteInt((int)l1);
}
buffer.SetReaderIndex(l);
}
Expand All @@ -105,8 +122,8 @@ public static void EncodeXtea(this IByteBuffer buffer, uint[] keys, int start, i
}

buffer.SetWriterIndex(buffer.WriterIndex - 8);
buffer.WriteInt((int) l);
buffer.WriteInt((int) i1);
buffer.WriteInt((int)l);
buffer.WriteInt((int)i1);
}
buffer.SetWriterIndex(o);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Autofac;
using NetScape.Abstractions.Cache;

namespace NetScape.Abstractions.Interfaces.Cache
{
public interface IReferenceTableCache : IDisposable
public interface IReferenceTableCache : IDisposable, IStartable
{
ConcurrentDictionary<CacheIndex, ReferenceTableFile> CachedReferenceTables { get; set; }
void FlushCachedReferenceTables();
Expand Down
1 change: 1 addition & 0 deletions NetScape.Abstractions/NetScape.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Autofac" Version="6.1.0" />
<PackageReference Include="Dawn.Guard" Version="1.12.0" />
<PackageReference Include="DotNetty.Codecs" Version="0.6.0" />
<PackageReference Include="DotNetty.Handlers" Version="0.6.0" />
<PackageReference Include="DotNetty.Transport" Version="0.6.0" />
<PackageReference Include="Google.Protobuf" Version="3.15.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="5.0.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using DotNetty.Transport.Channels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DotNetty.Transport.Channels;

namespace NetScape.Modules.Server.IO.EventLoop
namespace NetScape.Modules.Server.EventLoop
{
/// <summary>
/// The <see cref="BaseLoopGroupFactory"/> class keeps references of event loop groups that are created
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using NetScape.Abstractions.Interfaces.IO.EventLoop;
using DotNetty.Transport.Channels;
using System.Threading.Tasks;
using DotNetty.Transport.Channels;
using Microsoft.Extensions.Configuration;
using NetScape.Abstractions.Interfaces.IO.EventLoop;

namespace NetScape.Modules.Server.IO.EventLoop
namespace NetScape.Modules.Server.EventLoop
{
public class GameServerEventLoopGroupFactory : BaseLoopGroupFactory, IEventLoopGroupFactory
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using NetScape.Abstractions.Interfaces.IO;
using NetScape.Abstractions.Interfaces.IO.EventLoop;
using NetScape.Abstractions.IO;
using DotNetty.Transport.Bootstrapping;
using DotNetty.Transport.Bootstrapping;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Channels.Sockets;
using NetScape.Abstractions.Interfaces.IO;
using NetScape.Abstractions.Interfaces.IO.EventLoop;
using Serilog;
using System.Net;
using System.Threading.Tasks;

namespace NetScape.Modules.Server.IO
namespace NetScape.Modules.Server
{
public sealed class GameServer : IGameServer
{
Expand Down
21 changes: 21 additions & 0 deletions NetScape.GameServer/GameServerChannelHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using DotNetty.Buffers;
using DotNetty.Transport.Channels;
using Serilog;
using System;

namespace NetScape.Modules.Server
{
public class GameServerChannelHandler : SimpleChannelInboundHandler<IByteBuffer>
{
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
{
Log.Logger.Error(exception, nameof(GameServerChannelHandler));
}

protected override void ChannelRead0(IChannelHandlerContext ctx, IByteBuffer msg)
{
msg.Retain();
ctx.FireChannelRead(msg);
}
}
}
9 changes: 4 additions & 5 deletions NetScape.GameServer/GameServerModule.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using NetScape.Abstractions.Interfaces.IO;
using Autofac;
using NetScape.Abstractions.Interfaces.IO;
using NetScape.Abstractions.Interfaces.IO.EventLoop;
using NetScape.Abstractions.IO;
using NetScape.Modules.Server.IO;
using NetScape.Modules.Server.IO.EventLoop;
using Autofac;
using NetScape.Abstractions.Model.Game;
using NetScape.Abstractions.Interfaces.Messages;
using NetScape.Modules.Server.EventLoop;

namespace NetScape.Modules.Server
{
Expand All @@ -23,6 +21,7 @@ protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<GameServer>().As<IGameServer>();
builder.RegisterType<ServerChannelInitializer>();
builder.RegisterType<GameServerChannelHandler>().AsSelf();
builder.RegisterType<GameServerEventLoopGroupFactory>()
.As<IEventLoopGroupFactory>();
builder.RegisterInstance(_gameServerParams).As<IGameServerParameters>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using DotNetty.Transport.Channels;
using DotNetty.Handlers.Logging;
using DotNetty.Transport.Channels;
using NetScape.Abstractions.Interfaces.Login;

namespace NetScape.Abstractions.IO
namespace NetScape.Modules.Server
{
public class ServerChannelInitializer : ChannelInitializer<IChannel>
{
Expand All @@ -15,6 +16,7 @@ public ServerChannelInitializer(ILoginProvider loginProvider)
protected override void InitChannel(IChannel channel)
{
var pipeline = channel.Pipeline;
pipeline.AddLast(new GameServerChannelHandler());
pipeline.AddLast(_loginProvider.Provide());
}
}
Expand Down
2 changes: 1 addition & 1 deletion NetScape.Modules.Cache/CacheModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CacheModule : Module
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<FileStore>().As<IFileStore>().As<IStartable>().SingleInstance();
builder.RegisterType<RuneTek5Cache>().As<IReferenceTableCache>().SingleInstance();
builder.RegisterType<RuneTek5Cache>().As<IReferenceTableCache>().As<IStartable>().SingleInstance();
}
}
}
18 changes: 16 additions & 2 deletions NetScape.Modules.Cache/RuneTek5/FileStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class FileStore : IFileStore, IStartable
private const int IndexPointerSize = 6; // filesize + firstSectorPosition

private readonly Dictionary<CacheIndex, Stream> _indexStreams = new Dictionary<CacheIndex, Stream>();
private Dictionary<CacheIndex, Dictionary<int, byte[]>> CachedStorage { get; } = new();

private Stream _dataStream;

Expand All @@ -43,7 +44,6 @@ public FileStore(IFileSystem fileSystem)

public bool ReadOnly { get; private set; }
public string CacheDirectory { get; private set; }
public IReferenceTableCache ReferenceTableCache { get; set; }

/// <summary>
/// The loaded/existing indexes.
Expand All @@ -67,12 +67,26 @@ private IEnumerable<Sector> ReadSectors(CacheIndex index, int fileId)
/// <returns></returns>
public byte[] ReadFileData(CacheIndex index, int fileId)
{
if(CachedStorage.ContainsKey(index))
{
if(CachedStorage[index].ContainsKey(fileId))
{
return CachedStorage[index][fileId];
}
} else
{
CachedStorage.Add(index, new());
}

int filesize;
return this.ReadSectors(index, fileId, out filesize).Aggregate(new List<byte>(), (bytes, sector) =>
var fileData = this.ReadSectors(index, fileId, out filesize).Aggregate(new List<byte>(), (bytes, sector) =>
{
bytes.AddRange(sector.Payload);
return bytes;
}).Take(filesize).ToArray();
CachedStorage[index].Add(fileId, fileData);
return fileData;

}

private IEnumerable<Sector> ReadSectors(CacheIndex index, int fileId, out int filesize)
Expand Down
22 changes: 22 additions & 0 deletions NetScape.Modules.Cache/RuneTek5/RuneTek5Cache.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NetScape.Abstractions.Cache;
using NetScape.Abstractions.Interfaces.Cache;
using Serilog;
using System;
using System.Collections.Generic;
using System.Text;
Expand Down Expand Up @@ -62,5 +63,26 @@ public override void Dispose()
this._fileStore = null;
}
}

public void Start()
{
var indexes = GetIndexes();
foreach(var index in indexes)
{
var dataMap = new Dictionary<int, byte[]>();
var fileIds = this.GetFileIds(index);

foreach(var id in fileIds)
{
try
{
_fileStore.ReadFileData(index, id);
} catch(Exception e)
{
Log.Logger.Error("Could not cache Index: {0} Id: {1} Error: {2}", index, id, e);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Autofac;
using NetScape.Abstractions.Interfaces.Login;
using NetScape.Modules.FiveZeroEight.LoginProtocol.Handlers;

namespace NetScape.Modules.FiveZeroEight.LoginProtocol
{
public class FiveZeroEightLoginModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<LoginProvider>().As<ILoginProvider>().SingleInstance();
builder.RegisterType<HandshakeDecoder>();
builder.RegisterType<LoginEncoder>();
builder.RegisterType<JS5Decoder>().AsSelf();
builder.RegisterType<JS5Encoder>().AsSelf();
builder.RegisterType<WorldListDecoder>().AsSelf();
builder.RegisterType<WorldListEncoder>().AsSelf();
builder.RegisterType<LoginDecoder>();
builder.RegisterType<LoginProcessor>().As<ILoginProcessor<FiveZeroEightLoginRequest, FiveZeroEightLoginResponse>>()
.As<IStartable>().SingleInstance();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using NetScape.Abstractions.Model.Login;
using System;
using System.Threading.Tasks;

namespace NetScape.Modules.FiveZeroEight.LoginProtocol
{
public record FiveZeroEightLoginRequest : LoginRequest<FiveZeroEightLoginResponse>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using NetScape.Abstractions.Model.Login;

namespace NetScape.Modules.FiveZeroEight.LoginProtocol
{
public class FiveZeroEightLoginResponse : LoginResponse<FiveZeroEightLoginStatus>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace NetScape.Modules.FiveZeroEight.LoginProtocol
{
public enum FiveZeroEightLoginStatus
{
StatusExchangeData = 0,
StatusDelay = 1,
StatusOk = 2,
StatusInvalidCredentials = 3,
StatusAccountDisabled = 4,
StatusAccountOnline = 5,
StatusGameUpdated = 6,
StatusServerFull = 7,
StatusLoginServerOffline = 8,
StatusTooManyConnections = 9,
StatusBadSessionId = 10,
StatusLoginServerRejectedSession = 11,
StatusMembersAccountRequired = 12,
StatusCouldNotComplete = 13,
StatusUpdating = 14,
StatusReconnectionOk = 15,
StatusTooManyLogins = 16,
StatusInMembersArea = 17,
StatusInvalidLoginServer = 20,
StatusProfileTransfer = 21,
TypeStandard = 16,
TypeReconnection = 18
}
}
Loading

0 comments on commit 92e7b38

Please sign in to comment.