Compare commits

..

47 commits

Author SHA1 Message Date
9286c9fe98
ci: test something
Signed-off-by: Timothy Schenk <admin@rainote.dev>
2025-01-16 14:34:17 +01:00
3a24dabdf2
chore: formatting and slnx
Signed-off-by: Timothy Schenk <admin@rainote.dev>
2025-01-16 14:30:40 +01:00
09f8dda990
chore: hexpats + MemCasting benchmarks 2024-09-19 05:09:25 +02:00
863332b806
chore: more information for skills 2024-09-18 17:31:42 +02:00
1f04c5d115
chore: more information for skills 2024-09-18 17:13:38 +02:00
2b85ba672a
chore: Powershell does not set the working dir properly 2024-09-18 16:40:23 +02:00
931d6c39a4
chore: more hexpats 2024-09-18 16:34:58 +02:00
d70041f064
chore: hexpat for baseitemdata 2024-09-18 10:42:21 +02:00
209cc3d8f5
docs: example usage scripts 2024-09-17 06:00:40 +02:00
66ca9c6fa9
fix(auth): otel export logging 2024-09-16 21:36:50 +02:00
50caf0464b
chore: OTEL service name 2024-09-16 21:19:09 +02:00
b0cb649dff
chore: dependency updates 2024-09-16 21:15:50 +02:00
7c1793e9a2
chore: pre-commit update 2024-09-16 21:13:03 +02:00
72026a8f99
fix(auth): exception initial character after login + otel 2024-09-16 21:11:40 +02:00
4e9e5f4730
fix(auth): exception initial character after login + otel 2024-09-16 21:10:52 +02:00
2aedc9dae9
refactor(wonderking): item data adjustments 2024-09-16 19:33:26 +02:00
4042654856
refactor(wonderking): item reader cleanup 2024-09-09 17:37:13 +02:00
a5cd7da487
chore: PacketMediator renaming 2024-04-04 16:57:42 +02:00
4c87d4f429
chore: ignore CA1810 2024-04-04 16:52:25 +02:00
d203c0bbe4
chore: add IDisposableAnalyzer (IDISP007) 2024-04-04 16:52:25 +02:00
f1f22ce1f2
fix: properly dispose ChannelSession 2024-04-04 16:52:24 +02:00
2ec47ae884
fix: seal and adjust async usage 2024-04-04 16:52:24 +02:00
7df83c9877
fix: autodispose argon2id 2024-04-04 16:52:24 +02:00
aff98c4144
chore: add IDisposableAnalyzer (IDISP007) 2024-04-04 16:52:22 +02:00
18345e775e
refactor: required fields 2024-04-04 16:52:18 +02:00
b78c7adf39
chore: minor adjustments to benchmarks 2024-04-04 16:52:17 +02:00
eb0b81cf59
chore: remove develop from basebranches 2024-04-04 16:52:17 +02:00
cc1a0acc30
chore: removal of PacketMediator 2024-04-04 16:52:16 +02:00
7ac4c8c0ef
chore: file header adjustment 2024-04-04 16:52:10 +02:00
6147a76d16
chore: adjustments to build scripts 2024-04-04 16:51:48 +02:00
2e70bf0772
chore: code quality adjustments 2024-04-04 16:51:48 +02:00
ca67b62f52
chore: ignore .run 2024-04-04 16:51:48 +02:00
60247ce42c
refactor: packet differentiation + packet id mapping 2024-04-04 16:51:48 +02:00
4982db6609
chore: otlp adjustments 2024-04-04 16:51:47 +02:00
efd261be77
chore: adjustments 2024-04-04 16:51:47 +02:00
63ddbd707f
chore: minor adjustments 2024-04-04 16:51:47 +02:00
0c2accd72c
feat: Parallel loops for initialization 2024-04-04 16:51:46 +02:00
0d0b27ba49
chore: cleanup dependencies 2024-04-04 16:51:46 +02:00
23aef12502
feat: cancellation token support 2024-04-04 16:51:46 +02:00
b9144a3ff0
chore: cleanup opentelemetry 2024-04-04 16:51:46 +02:00
bae1f812cf
chore: explicit generic Types 2024-04-04 16:51:45 +02:00
1668f0f73d
feat: switching to channels 2024-04-04 16:51:45 +02:00
3615059a96
chore: annotations 2024-04-04 16:51:45 +02:00
e2eb461fb3
chore: make everything proper async 2024-04-04 16:51:45 +02:00
c378ddba07
chore: cleanup of Masstransit library 2024-04-04 16:51:44 +02:00
e305ce49aa
chore: cleanup leftovers 2024-04-04 16:51:44 +02:00
e8b331059d
feat: extraction of generic Packet distribution service 2024-04-04 16:51:44 +02:00
115 changed files with 30445 additions and 1705 deletions

View file

@ -1,4 +1,4 @@
; EditorConfig to support per-solution formatting.
; EditorConfig to support per-solution formatting.
; Use the EditorConfig VS add-in to make this work.
; http://editorconfig.org/
;
@ -23,10 +23,13 @@ indent_style = space
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
file_header_template = Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
[*.(yaml|yml)]
indent_size = 2
[*.cs]
dotnet_diagnostic.MA0004.severity = none
file_header_template = Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
indent_size = 4
dotnet_sort_system_directives_first = true
dotnet_diagnostic.MA0007.severity = none
@ -50,7 +53,7 @@ csharp_style_var_elsewhere = true:suggestion
csharp_style_throw_expression = false:suggestion
# Newline settings
csharp_new_line_before_open_brace = all
csharp_new_line_before_open_brace = none
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true

View file

@ -188,8 +188,8 @@ jobs:
uses: https://github.com/actions/setup-dotnet@v3
with:
dotnet-version: |
7.0
8.0
9.0
dotnet-quality: preview
- name: Install dependencies
run: |
dotnet restore

View file

@ -1,4 +1,4 @@
name: PR Workflow
name: PR Workflow
run-name: ${{ gitea.actor }} PR related workflow
on:
pull_request:
@ -152,8 +152,8 @@ jobs:
uses: https://github.com/actions/setup-dotnet@v3
with:
dotnet-version: |
7.0
8.0
9.0
dotnet-quality: preview
- name: Install dependencies
run: |
dotnet restore

3
.gitignore vendored
View file

@ -4,7 +4,8 @@
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
licenses
.run
BenchmarkDotNet.Artifacts
# User-specific files
*.rsuser
*.suo

View file

@ -5,15 +5,20 @@ repos:
- id: dotnet-format
name: dotnet-format
language: system
entry: dotnet format --include
entry: dotnet format --include Continuity.slnx
types_or: [c#, vb]
- repo: https://github.com/Mateusz-Grzelinski/actionlint-py
rev: v1.6.26.11
rev: v1.7.6.22
hooks:
- id: actionlint
additional_dependencies: [pyflakes>=3.0.1, shellcheck-py>=0.9.0.5]
- repo: https://github.com/hadolint/hadolint
rev: v2.12.0
rev: v2.13.1-beta
hooks:
- id: hadolint-docker
args: [--ignore, SC2086]
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.14.0
hooks:
- id: pretty-format-yaml
args: [--autofix, --indent, '2']

View file

@ -1,16 +0,0 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Server/Dockerfile" type="docker-deploy" factoryName="dockerfile" server-name="Docker">
<deployment type="dockerfile">
<settings>
<option name="imageTag" value="continuity" />
<option name="buildCliOptions" value="--platform linux/amd64" />
<option name="buildKitEnabled" value="true" />
<option name="buildOnly" value="true" />
<option name="contextFolderPath" value="." />
<option name="sourceFilePath" value="Server/Dockerfile" />
</settings>
</deployment>
<EXTENSION ID="com.jetbrains.rider.docker.debug" isFastModeEnabled="true" isSslEnabled="false" />
<method v="2" />
</configuration>
</component>

View file

@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="build images" type="PowerShellRunType" factoryName="PowerShell" scriptUrl="C:\Users\Timot\Projects\Continuity\build-image.ps1" workingDirectory="C:\Users\Timot\Projects\Continuity" executablePath="C:\Users\Timot\scoop\apps\pwsh\current\pwsh.exe">
<configuration default="false" name="build images" type="PowerShellRunType" factoryName="PowerShell" scriptUrl="$PROJECT_DIR$\build-image.ps1" workingDirectory="$PROJECT_DIR$">
<envs />
<method v="2" />
</configuration>

View file

@ -4,12 +4,14 @@
<settings>
<option name="buildKitEnabledForCompose" value="true" />
<option name="envFilePath" value="" />
<option name="removeImagesOnComposeDown" value="LOCAL" />
<option name="sourceFilePath" value="docker-compose.yml" />
<option name="upDetach" value="false" />
<option name="upForceRecreate" value="true" />
<option name="upRemoveOrphans" value="true" />
</settings>
</deployment>
<EXTENSION ID="com.jetbrains.rider.docker.debug" isFastModeEnabled="true" isSslEnabled="false" />
<EXTENSION ID="com.jetbrains.rider.docker.debug" isFastModeEnabled="false" isSslEnabled="false" />
<method v="2">
<option name="Build" default="false" projectName="Wonderking" projectPath="$PROJECT_DIR$/Wonderking/Wonderking.csproj" />
<option name="Build" default="false" projectName="Server" projectPath="$PROJECT_DIR$/Server/Server.csproj" />

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Security.Cryptography;
using BenchmarkDotNet.Attributes;
@ -11,69 +11,59 @@ namespace Benchmarks;
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[Config(typeof(GenericConfig))]
public class Argon2Benchmarks
{
public class Argon2Benchmarks {
private byte[] _additionalData = null!;
[Params(2, 4)] public int _iterations;
[Params(16)] public int _length;
[Params(16, 32)] public int _memory;
[Params(1, 2)] public int _parallelism;
private byte[] _salt = null!;
private byte[] _additionalData = null!;
[Params(16)] public int _length;
private byte[] _password = null!;
private byte[] _salt = null!;
[GlobalSetup]
public void Setup()
{
public void Setup() {
_salt = RandomNumberGenerator.GetBytes(16);
_additionalData = RandomNumberGenerator.GetBytes(16);
_password = RandomNumberGenerator.GetBytes(16);
}
[Benchmark]
public byte[] Argon2i()
{
return new Argon2i(_password)
{
public byte[] Argon2i() {
return new Argon2i(_password) {
Iterations = _iterations,
MemorySize = 1024 * _memory,
DegreeOfParallelism = _parallelism,
AssociatedData = _additionalData,
Salt = _salt,
Salt = _salt
}.GetBytes(_length);
}
[Benchmark]
public byte[] Argon2d()
{
return new Argon2d(_password)
{
public byte[] Argon2d() {
return new Argon2d(_password) {
Iterations = _iterations,
MemorySize = 1024 * _memory,
DegreeOfParallelism = _parallelism,
AssociatedData = _additionalData,
Salt = _salt,
Salt = _salt
}.GetBytes(_length);
}
[Benchmark]
public byte[] Argon2id()
{
return new Argon2id(_password)
{
public byte[] Argon2id() {
return new Argon2id(_password) {
Iterations = _iterations,
MemorySize = 1024 * _memory,
DegreeOfParallelism = _parallelism,
AssociatedData = _additionalData,
Salt = _salt,
Salt = _salt
}.GetBytes(_length);
}
[Benchmark]
public byte[] IsopohArgon2DD()
{
var config = new Argon2Config
{
public byte[] IsopohArgon2DD() {
var config = new Argon2Config {
Type = Argon2Type.DataDependentAddressing,
Version = Argon2Version.Nineteen,
MemoryCost = 1024 * _memory,
@ -82,17 +72,15 @@ public class Argon2Benchmarks
HashLength = _length,
Salt = _salt,
AssociatedData = _additionalData,
Password = _password,
Password = _password
};
var argon2 = new Argon2(config);
return argon2.Hash().Buffer;
}
[Benchmark]
public byte[] IsopohArgon2DI()
{
var config = new Argon2Config
{
public byte[] IsopohArgon2DI() {
var config = new Argon2Config {
Type = Argon2Type.DataIndependentAddressing,
Version = Argon2Version.Nineteen,
MemoryCost = 1024 * _memory,
@ -101,17 +89,15 @@ public class Argon2Benchmarks
HashLength = _length,
Salt = _salt,
AssociatedData = _additionalData,
Password = _password,
Password = _password
};
var argon2 = new Argon2(config);
return argon2.Hash().Buffer;
}
[Benchmark]
public byte[] IsopohArgon2Hybrid()
{
var config = new Argon2Config
{
public byte[] IsopohArgon2Hybrid() {
var config = new Argon2Config {
Type = Argon2Type.HybridAddressing,
Version = Argon2Version.Nineteen,
MemoryCost = 1024 * _memory,
@ -120,7 +106,7 @@ public class Argon2Benchmarks
HashLength = _length,
Salt = _salt,
AssociatedData = _additionalData,
Password = _password,
Password = _password
};
var argon2 = new Argon2(config);
return argon2.Hash().Buffer;

View file

@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
@ -13,15 +13,15 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="DotNext.Unsafe" Version="0.14.0"/>
<PackageReference Include="Isopoh.Cryptography.Argon2" Version="2.0.0"/>
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.0" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.146">
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.1" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.163">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.9.28">
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.11.20">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Buffers.Binary;
using System.Security.Cryptography;
@ -9,29 +9,25 @@ namespace Benchmarks;
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[Config(typeof(GenericConfig))]
public class BinaryConversionBenchmarks
{
public class BinaryConversionBenchmarks {
private byte[] _data = null!;
private int _offset;
private int _writeBuffer;
[GlobalSetup]
public void Setup()
{
public void Setup() {
_data = RandomNumberGenerator.GetBytes(4000);
_offset = RandomNumberGenerator.GetInt32(0, 3500);
_writeBuffer = RandomNumberGenerator.GetInt32(int.MinValue, int.MaxValue);
}
[Benchmark]
public short BitConverterParseTest()
{
public short BitConverterParseTest() {
return BitConverter.ToInt16(_data, _offset);
}
[Benchmark]
public short BinaryReader()
{
public short BinaryReader() {
using var ms = new MemoryStream(_data);
using var reader = new BinaryReader(ms);
reader.BaseStream.Position = _offset;
@ -39,28 +35,24 @@ public class BinaryConversionBenchmarks
}
[Benchmark]
public short BinaryPrimitivesRead()
{
public short BinaryPrimitivesRead() {
return BinaryPrimitives.ReadInt16LittleEndian(
new ArraySegment<byte>(_data, _offset, sizeof(short)));
}
[Benchmark]
public void BinaryPrimitivesWrite()
{
public void BinaryPrimitivesWrite() {
BinaryPrimitives.WriteInt32LittleEndian(_data.AsSpan(_offset, 4),
_writeBuffer);
}
[Benchmark]
public void BitConverterCopy()
{
public void BitConverterCopy() {
BitConverter.GetBytes(_writeBuffer).CopyTo(_data, _offset);
}
[Benchmark]
public void BitConverterAssignment()
{
public void BitConverterAssignment() {
var bytes = BitConverter.GetBytes(_writeBuffer);
_data[_offset] = bytes[0];
_data[_offset + 1] = bytes[1];

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Collections.Concurrent;
using System.Collections.Immutable;
@ -9,17 +9,15 @@ namespace Benchmarks;
[Config(typeof(GenericConfig))]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
public class DataCacheBenchmark
{
private ConcurrentDictionary<int, int> _concurrentDictionary;
private Dictionary<int, int> _dictionary;
private HashSet<int> _hashSet;
private ImmutableHashSet<int> _immutableHashSet;
public class DataCacheBenchmark {
private ConcurrentDictionary<int, int> _concurrentDictionary = null!;
private Dictionary<int, int> _dictionary = null!;
private HashSet<int> _hashSet = null!;
private ImmutableHashSet<int> _immutableHashSet = null!;
[Params(1000, 100000, 1000000)] public int N;
[GlobalSetup]
public void Setup()
{
public void Setup() {
_hashSet = new HashSet<int>();
_dictionary = new Dictionary<int, int>();
_concurrentDictionary = new ConcurrentDictionary<int, int>();
@ -32,8 +30,7 @@ public class DataCacheBenchmark
_hashSet.EnsureCapacity(N);
_dictionary.EnsureCapacity(N);
for (var i = 0; i < N; i++)
{
for (var i = 0; i < N; i++) {
_immutableHashSet = _immutableHashSet.Add(i);
_hashSet.Add(i);
_dictionary.Add(i, i);
@ -42,45 +39,38 @@ public class DataCacheBenchmark
}
[Benchmark]
public void HashSetAdd()
{
public void HashSetAdd() {
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _hashSet.Add(i));
}
[Benchmark]
public void DictionaryAdd()
{
public void DictionaryAdd() {
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _dictionary.Add(N + i, i));
}
[Benchmark]
public void ConcurrentDictionaryAddOrUpdate()
{
public void ConcurrentDictionaryAddOrUpdate() {
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i =>
_concurrentDictionary.AddOrUpdate(N + i, i, (key, oldValue) => oldValue + i));
_concurrentDictionary.AddOrUpdate(N + i, i, (_, oldValue) => oldValue + i));
}
[Benchmark]
public void ImmutableHashSetLookup()
{
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _immutableHashSet.Contains(i));
public void ImmutableHashSetLookup() {
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => { _ = _immutableHashSet.Contains(i); });
}
[Benchmark]
public void HashSetLookup()
{
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _hashSet.Contains(i));
public void HashSetLookup() {
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => { _ = _hashSet.Contains(i); });
}
[Benchmark]
public void DictionaryLookup()
{
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _dictionary.ContainsKey(i));
public void DictionaryLookup() {
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => { _ = _dictionary.ContainsKey(i); });
}
[Benchmark]
public void ConcurrentDictionaryLookup()
{
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _concurrentDictionary.ContainsKey(i));
public void ConcurrentDictionaryLookup() {
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => { _ = _concurrentDictionary.ContainsKey(i); });
}
}

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using BenchmarkDotNet.Analysers;
using BenchmarkDotNet.Columns;
@ -11,10 +11,8 @@ using BenchmarkDotNet.Jobs;
namespace Benchmarks;
public class GenericConfig : ManualConfig
{
public GenericConfig()
{
public class GenericConfig : ManualConfig {
public GenericConfig() {
AddJob(Job.Default
.WithRuntime(CoreRuntime.Core80))
.AddDiagnoser(ThreadingDiagnoser.Default, MemoryDiagnoser.Default,

163
Benchmarks/MemCasting.cs Normal file
View file

@ -0,0 +1,163 @@
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
namespace Benchmarks;
[Config(typeof(GenericConfig))]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
public class MemCasting {
private byte[] _data = null!;
private byte[] _arrayData = null!;
[Params(1000, 100000, 1000000)] public int N;
[GlobalSetup]
public void Setup() {
_data = new byte[64];
_arrayData = new byte[N * 64];
Random.Shared.NextBytes(_data);
Random.Shared.NextBytes(_arrayData);
}
private static ElementalStats ReadElementalStatsMemoryMarshal(ref Span<byte> data) {
return MemoryMarshal.Cast<byte, ElementalStats>(data.Slice(0, 64))[0];
}
private static ElementalStats ReadElementalStatsMemoryMarshal2(ref Span<byte> data) {
return MemoryMarshal.Read<ElementalStats>(data.Slice(0, 64));
}
private static ElementalStats ReadElementalStatsNew(ref Span<byte> data) {
return new ElementalStats {
MinimumFireDamage = BitConverter.ToInt32(data.Slice(0, 4)), // 140 -> 144
MinimumWaterDamage = BitConverter.ToInt32(data.Slice(4, 4)), // 144 -> 148
MinimumDarkDamage = BitConverter.ToInt32(data.Slice(8, 4)), // 148 -> 152
MinimumHolyDamage = BitConverter.ToInt32(data.Slice(12, 4)), // 152 -> 156
MaximumFireDamage = BitConverter.ToInt32(data.Slice(16, 4)), // 156 -> 160
MaximumWaterDamage = BitConverter.ToInt32(data.Slice(20, 4)), // 160 -> 164
MaximumDarkDamage = BitConverter.ToInt32(data.Slice(24, 4)), // 164 -> 168
MaximumHolyDamage = BitConverter.ToInt32(data.Slice(28, 4)), // 168 -> 172
ElementFire = BitConverter.ToUInt32(data.Slice(32, 4)), // 172 -> 176
ElementWater = BitConverter.ToUInt32(data.Slice(36, 4)), // 176 -> 180
ElementDark = BitConverter.ToUInt32(data.Slice(40, 4)), // 180 -> 184
ElementHoly = BitConverter.ToUInt32(data.Slice(44, 4)), // 184 -> 188
FireResistance = BitConverter.ToInt32(data.Slice(48, 4)), // 188 -> 192
WaterResistance = BitConverter.ToInt32(data.Slice(52, 4)), // 192 -> 196
DarkResistance = BitConverter.ToInt32(data.Slice(56, 4)), // 196 -> 200
HolyResistance = BitConverter.ToInt32(data.Slice(60, 4)) // 200 -> 204
};
}
[Benchmark]
public Span<ElementalStats> MemoryMarshalCastArray() {
var data = _arrayData.AsSpan();
var elements = MemoryMarshal.Cast<byte, ElementalStats>(data);
return elements;
}
[Benchmark]
public Span<ElementalStats> MemoryMarshalReadArray() {
Span<ElementalStats> statsArr = stackalloc ElementalStats[N + 1];
var data = _data.AsSpan();
for (int i = 0; i <= N; i++) {
statsArr[i] = ReadElementalStatsMemoryMarshal2(ref data);
}
return statsArr.ToArray();
}
[Benchmark]
public Span<ElementalStats> ManualSpanSlicingArray() {
Span<ElementalStats> statsArr = stackalloc ElementalStats[N + 1];
var data = _data.AsSpan();
for (int i = 0; i <= N; i++) {
statsArr[i] = ReadElementalStatsNew(ref data);
}
return statsArr.ToArray();
}
[Benchmark]
public ElementalStats MemoryMarshalCastSingle() {
var data = _data.AsSpan();
return ReadElementalStatsMemoryMarshal(ref data);
}
[Benchmark]
public ElementalStats MemoryMarshalReadSingle() {
var data = _data.AsSpan();
return ReadElementalStatsMemoryMarshal2(ref data);
}
[Benchmark]
public ElementalStats ManualSpanSlicingSingle() {
var data = _data.AsSpan();
return ReadElementalStatsNew(ref data);
}
[StructLayout(LayoutKind.Explicit, Size = 64)]
public struct ElementalStats {
[FieldOffset(0)]
[MarshalAs(UnmanagedType.I4)]
public int MinimumFireDamage;
[FieldOffset(4)]
[MarshalAs(UnmanagedType.I4)]
public int MinimumWaterDamage;
[FieldOffset(8)]
[MarshalAs(UnmanagedType.I4)]
public int MinimumDarkDamage;
[FieldOffset(12)]
[MarshalAs(UnmanagedType.I4)]
public int MinimumHolyDamage;
[FieldOffset(16)]
[MarshalAs(UnmanagedType.I4)]
public int MaximumFireDamage;
[FieldOffset(20)]
[MarshalAs(UnmanagedType.I4)]
public int MaximumWaterDamage;
[FieldOffset(24)]
[MarshalAs(UnmanagedType.I4)]
public int MaximumDarkDamage;
[FieldOffset(28)]
[MarshalAs(UnmanagedType.I4)]
public int MaximumHolyDamage;
[FieldOffset(32)]
[MarshalAs(UnmanagedType.U4)]
public uint ElementFire;
[FieldOffset(36)]
[MarshalAs(UnmanagedType.U4)]
public uint ElementWater;
[FieldOffset(40)]
[MarshalAs(UnmanagedType.U4)]
public uint ElementDark;
[FieldOffset(44)]
[MarshalAs(UnmanagedType.U4)]
public uint ElementHoly;
[FieldOffset(48)]
[MarshalAs(UnmanagedType.I4)]
public int FireResistance;
[FieldOffset(52)]
[MarshalAs(UnmanagedType.I4)]
public int WaterResistance;
[FieldOffset(56)]
[MarshalAs(UnmanagedType.I4)]
public int DarkResistance;
[FieldOffset(60)]
[MarshalAs(UnmanagedType.I4)]
public int HolyResistance;
}
}

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Reflection;
using BenchmarkDotNet.Running;

View file

@ -1,46 +1,37 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Net.Sockets;
using System.Reflection;
using Continuity.AuthServer.Packets;
using MassTransit.Mediator;
using Microsoft.Extensions.Logging;
using NetCoreServer;
using RaiNote.PacketMediator;
using Wonderking.Packets;
namespace Continuity.AuthServer;
public class AuthSession : TcpSession
{
public class AuthSession : TcpSession {
private readonly PacketDistributorService<OperationCode, AuthSession> _distributorService;
private readonly ILogger<AuthSession> _logger;
private readonly IMediator _mediator;
public AuthSession(TcpServer
server, IMediator mediator, ILogger<AuthSession> logger) : base(server)
{
server, IMediator mediator, ILogger<AuthSession> logger,
PacketDistributorService<OperationCode, AuthSession> distributorService) : base(server) {
_mediator = mediator;
_logger = logger;
_distributorService = distributorService;
}
public Guid AccountId { get; set; }
public override long Send(byte[] buffer)
{
public override long Send(byte[] buffer) {
_logger.LogInformation("Data being sent is: {Data}", BitConverter.ToString(buffer));
return base.Send(buffer);
}
public Task SendAsync(IPacket packet)
{
var type = packet.GetType();
_logger.LogInformation("Packet of type {Type} is being serialized", type.Name);
var packetIdAttribute = type.GetCustomAttribute<PacketIdAttribute>();
if (packetIdAttribute == null)
{
return Task.CompletedTask;
}
var opcode = packetIdAttribute.Code;
public Task SendAsync(IOutgoingPacket packet) {
var opcode = _distributorService.GetOperationCodeByPacketType(packet);
Span<byte> packetData = packet.Serialize();
var length = (ushort)(packetData.Length + 8);
@ -51,13 +42,11 @@ public class AuthSession : TcpSession
var bytesOfLength = BitConverter.GetBytes(length);
var bytesOfOpcode = BitConverter.GetBytes((ushort)opcode);
for (var i = 0; i < bytesOfLength.Length || i < 2; i++)
{
for (var i = 0; i < bytesOfLength.Length || i < 2; i++) {
buffer[i] = bytesOfLength[i];
}
for (var i = 0; i < bytesOfOpcode.Length || i < 2; i++)
{
for (var i = 0; i < bytesOfOpcode.Length || i < 2; i++) {
buffer[2 + i] = bytesOfOpcode[i];
}
@ -68,8 +57,7 @@ public class AuthSession : TcpSession
return Task.CompletedTask;
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
protected override void OnReceived(byte[] buffer, long offset, long size) {
_logger.LogDebug("Length: {Size} & offset: {Offset}", size, offset);
Span<byte> decryptedBuffer = stackalloc byte[(int)size];
@ -98,18 +86,15 @@ public class AuthSession : TcpSession
base.OnReceived(decryptedBuffer.ToArray(), offset, decryptedBuffer.Length);
}
private static Span<byte> Decrypt(Span<byte> buffer)
{
for (var i = 0; i < buffer.Length; ++i)
{
private static Span<byte> Decrypt(Span<byte> buffer) {
for (var i = 0; i < buffer.Length; ++i) {
buffer[i] = (byte)(buffer[i] ^ i ^ (3 * (0xFE - i)));
}
return buffer;
}
protected override void OnError(SocketError error)
{
protected override void OnError(SocketError error) {
_logger.LogWarning("An error has occured: {Error}", error);
}
}

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Security.Cryptography;
using System.Text;
@ -8,8 +8,7 @@ using NetCoreServer;
namespace Continuity.AuthServer;
public class ChannelSession : TcpSession
{
public class ChannelSession : TcpSession {
private static readonly byte[] _key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 }
.Reverse().ToArray();
@ -23,12 +22,12 @@ public class ChannelSession : TcpSession
private readonly ICryptoTransform _encryptor;
private readonly ILogger<ChannelSession> _logger;
private readonly IMediator _mediator;
private bool _disposed;
public ChannelSession(TcpServer server, IMediator mediator, ILogger<ChannelSession> logger) : base(server)
{
public ChannelSession(TcpServer server, IMediator mediator, ILogger<ChannelSession> logger) : base(server) {
_mediator = mediator;
_logger = logger;
var aes = Aes.Create();
using var aes = Aes.Create();
aes.Key = _key;
aes.IV = _iv;
aes.Padding = PaddingMode.None;
@ -40,33 +39,44 @@ public class ChannelSession : TcpSession
_encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
try
{
protected override void OnReceived(byte[] buffer, long offset, long size) {
try {
using (var ms = new MemoryStream(Decrypt(buffer)))
using (var cs = new CryptoStream(ms, _decryptor, CryptoStreamMode.Read))
{
using (var cs = new CryptoStream(ms, _decryptor, CryptoStreamMode.Read)) {
var amountOfReadBytes = cs.Read(buffer);
if (amountOfReadBytes != buffer.Length)
{
if (amountOfReadBytes != buffer.Length) {
_logger.LogError("Amount of read bytes is not equal to buffer length");
}
}
base.OnReceived(buffer, offset, size);
}
catch (CryptographicException ex)
{
catch (CryptographicException ex) {
_logger.LogError("An error has occured while decrypting: {ErrorMessage}", ex.Message);
_logger.LogError("Default buffer message: {Message}", Encoding.ASCII.GetString(buffer));
}
}
private static byte[] Decrypt(byte[] buffer)
{
for (var i = 0; i < buffer.Length; ++i)
{
protected override void Dispose(bool disposingManagedResources) {
if (_disposed) {
return;
}
_disposed = true;
if (disposingManagedResources) {
_decryptor.Dispose();
_encryptor.Dispose();
}
base.Dispose(disposingManagedResources);
}
protected virtual void ThrowIfDisposed() {
ObjectDisposedException.ThrowIf(_disposed, GetType());
}
private static byte[] Decrypt(byte[] buffer) {
for (var i = 0; i < buffer.Length; ++i) {
buffer[i] = (byte)(buffer[i] ^ i ^ (3 * (0xFE - i)));
}

View file

@ -1,23 +1,35 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Diagnostics;
using Continuity.AuthServer.Packets;
using Continuity.AuthServer.Services;
using JetBrains.Annotations;
using MassTransit;
using RaiNote.PacketMediator;
using Wonderking.Packets;
namespace Continuity.AuthServer.Consumers;
public class PacketConsumer : IConsumer<RawPacket>
{
private readonly PacketDistributorService _distributorService;
[UsedImplicitly]
public sealed class PacketConsumer : IConsumer<RawPacket>, IDisposable {
private readonly ActivitySource _activitySource;
private readonly PacketDistributorService<OperationCode, AuthSession> _distributorService;
public PacketConsumer(PacketDistributorService distributorService)
{
public PacketConsumer(PacketDistributorService<OperationCode, AuthSession> distributorService) {
_distributorService = distributorService;
_activitySource = new ActivitySource(nameof(PacketConsumer));
}
public Task Consume(ConsumeContext<RawPacket> context)
{
_distributorService.AddPacket(context.Message);
return Task.CompletedTask;
public async Task Consume(ConsumeContext<RawPacket> context) {
using var activity = _activitySource?.StartActivity("PacketConsumption");
activity?.SetTag("PacketId", context.Message.OperationCode.ToString());
activity?.SetTag("SessionId", context.Message.Session.Id.ToString());
activity?.SetTag("PacketSize", context.Message.MessageBody.Length);
await _distributorService.AddPacketAsync(context.Message.MessageBody, context.Message.OperationCode,
context.Message.Session);
}
public void Dispose() {
_activitySource.Dispose();
}
}

View file

@ -18,7 +18,7 @@
<ProduceReferenceAssembly>True</ProduceReferenceAssembly>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<AnalysisLevel>latest-recommended</AnalysisLevel>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0|AnyCPU'">
@ -40,66 +40,68 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="DotNext" Version="5.13.0" />
<PackageReference Include="DotNext.IO" Version="5.13.0" />
<PackageReference Include="DotNext.Metaprogramming" Version="5.13.0" />
<PackageReference Include="DotNext.Threading" Version="5.13.0" />
<PackageReference Include="DotNext.Unsafe" Version="5.13.0" />
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0"/>
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.0"/>
<PackageReference Include="MassTransit" Version="8.1.3" />
<PackageReference Include="MassTransit.Analyzers" Version="8.1.3">
<PackageReference Include="DotNext" Version="5.17.2" />
<PackageReference Include="DotNext.IO" Version="5.17.2" />
<PackageReference Include="DotNext.Metaprogramming" Version="5.17.2" />
<PackageReference Include="DotNext.Threading" Version="5.17.2" />
<PackageReference Include="DotNext.Unsafe" Version="5.17.2" />
<PackageReference Include="IDisposableAnalyzers" Version="4.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.146">
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.1" />
<PackageReference Include="MassTransit" Version="8.3.4" />
<PackageReference Include="MassTransit.Analyzers" Version="8.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<PackageReference Include="Meziantou.Analyzer" Version="2.0.186">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0"/>
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.9.28">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="9.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="9.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.12.19">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NetCoreServer" Version="8.0.7" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="8.0.2" />
<PackageReference Include="Nullable.Extended.Analyzer" Version="1.15.6169">
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.2" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="9.0.2" />
<PackageReference Include="Nullable.Extended.Analyzer" Version="1.15.6495">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="OpenTelemetry" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.10"/>
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.7.1" />
<PackageReference Include="OpenTelemetry" Version="1.11.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.11.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.11.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.11.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta.12" />
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="1.0.0-alpha.6" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.PersistentStorage.FileSystem" Version="1.0.0" />
<PackageReference Include="OpenTelemetry.ResourceDetectors.Container" Version="1.0.0-beta.6"/>
<PackageReference Include="OpenTelemetry.ResourceDetectors.Container" Version="1.0.0-beta.7" />
<PackageReference Include="RaiNote.PacketMediator" Version="0.0.7" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0"/>
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
</ItemGroup>
<ItemGroup>

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@ -8,10 +8,8 @@ namespace Continuity.AuthServer.DB.Documents;
[Index(nameof(Username), IsUnique = true)]
[Index(nameof(Id), IsUnique = true)]
public class Account
{
public Account(string username, byte[] password, string email, byte permissionLevel, byte[] salt)
{
public class Account {
public Account(string username, byte[] password, string email, byte permissionLevel, byte[] salt) {
Username = username;
Password = password;
Email = email;

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@ -10,8 +10,7 @@ namespace Continuity.AuthServer.DB.Documents;
[Index(nameof(Name), IsUnique = true)]
[Index(nameof(Id), IsUnique = true)]
public class Character
{
public class Character {
[DeleteBehavior(DeleteBehavior.Cascade)]
[Required]
public virtual Account Account { get; set; }

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@ -8,8 +8,7 @@ namespace Continuity.AuthServer.DB.Documents;
[Index(nameof(Name), IsUnique = true)]
[Index(nameof(Id), IsUnique = true)]
public class Guild
{
public class Guild {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@ -7,8 +7,7 @@ using Microsoft.EntityFrameworkCore;
namespace Continuity.AuthServer.DB.Documents;
[Index(nameof(Id), IsUnique = true)]
public class GuildMember
{
public class GuildMember {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }

View file

@ -1,9 +1,8 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
namespace Continuity.AuthServer.DB.Documents;
public enum GuildRank : byte
{
public enum GuildRank : byte {
Initiate = 0,
Member = 1,
Veteran = 2,

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@ -6,8 +6,7 @@ using Microsoft.EntityFrameworkCore;
namespace Continuity.AuthServer.DB.Documents;
public class InventoryItem
{
public class InventoryItem {
[DeleteBehavior(DeleteBehavior.Restrict)]
[Required]
public virtual Character Character { get; set; }

View file

@ -1,9 +1,8 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
namespace Continuity.AuthServer.DB.Documents;
public enum InventoryTab : byte
{
public enum InventoryTab : byte {
WornEquipment = 0,
WornCashEquipment = 1,
Equipment = 2,

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
#nullable disable
@ -6,13 +6,11 @@ namespace Server.DB.Migrations;
using Microsoft.EntityFrameworkCore.Migrations;
/// <inheritdoc />
public partial class Initial : Migration
{
public partial class Initial : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder) => migrationBuilder.CreateTable(
name: "Accounts",
columns: table => new
{
columns: table => new {
Id = table.Column<Guid>(type: "uuid", nullable: false),
Username = table.Column<string>(type: "varchar(20)", nullable: false),
Password = table.Column<byte[]>(type: "bytea", nullable: false),

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
#nullable disable
@ -6,11 +6,9 @@ namespace Server.DB.Migrations;
using Microsoft.EntityFrameworkCore.Migrations;
/// <inheritdoc />
public partial class CharacterDataDraft : Migration
{
public partial class CharacterDataDraft : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder) {
migrationBuilder.AlterColumn<string>(
name: "Username",
table: "Accounts",
@ -45,8 +43,7 @@ public partial class CharacterDataDraft : Migration
migrationBuilder.CreateTable(
name: "Characters",
columns: table => new
{
columns: table => new {
Id = table.Column<Guid>(type: "uuid", nullable: false),
ServerId = table.Column<byte>(type: "smallint", nullable: false),
AccountId = table.Column<Guid>(type: "uuid", nullable: false),
@ -59,8 +56,7 @@ public partial class CharacterDataDraft : Migration
Experience = table.Column<long>(type: "bigint", nullable: false),
Level = table.Column<byte>(type: "smallint", nullable: false)
},
constraints: table =>
{
constraints: table => {
table.PrimaryKey("PK_Characters", x => x.Id);
table.ForeignKey(
name: "FK_Characters_Accounts_AccountId",
@ -77,8 +73,7 @@ public partial class CharacterDataDraft : Migration
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Down(MigrationBuilder migrationBuilder) {
migrationBuilder.DropTable(
name: "Characters");

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore.Migrations;
@ -7,15 +7,12 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Server.DB.Migrations;
/// <inheritdoc />
public partial class AddInventoryToCharacter : Migration
{
public partial class AddInventoryToCharacter : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder) {
migrationBuilder.CreateTable(
name: "InventoryItem",
columns: table => new
{
columns: table => new {
Id = table.Column<Guid>(type: "uuid", nullable: false),
CharacterId = table.Column<Guid>(type: "uuid", nullable: false),
ItemId = table.Column<short>(type: "smallint", nullable: false),
@ -31,8 +28,7 @@ public partial class AddInventoryToCharacter : Migration
Option2 = table.Column<short>(type: "smallint", nullable: false),
Option3 = table.Column<short>(type: "smallint", nullable: false)
},
constraints: table =>
{
constraints: table => {
table.PrimaryKey("PK_InventoryItem", x => x.Id);
table.ForeignKey(
name: "FK_InventoryItem_Characters_CharacterId",
@ -49,8 +45,7 @@ public partial class AddInventoryToCharacter : Migration
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Down(MigrationBuilder migrationBuilder) {
migrationBuilder.DropTable(
name: "InventoryItem");
}

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore.Migrations;
@ -7,11 +7,9 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Server.DB.Migrations;
/// <inheritdoc />
public partial class AdditionalCharacterData : Migration
{
public partial class AdditionalCharacterData : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder) {
migrationBuilder.AlterColumn<int>(
name: "ItemId",
table: "InventoryItem",
@ -96,8 +94,7 @@ public partial class AdditionalCharacterData : Migration
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Down(MigrationBuilder migrationBuilder) {
migrationBuilder.DropColumn(
name: "BaseStats_Dexterity",
table: "Characters");

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore.Migrations;
@ -7,11 +7,9 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Server.DB.Migrations;
/// <inheritdoc />
public partial class AddGuildData : Migration
{
public partial class AddGuildData : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder) {
migrationBuilder.AddColumn<Guid>(
name: "GuildId",
table: "Characters",
@ -21,28 +19,24 @@ public partial class AddGuildData : Migration
migrationBuilder.CreateTable(
name: "Guild",
columns: table => new
{
columns: table => new {
Id = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "text", nullable: true),
Notice = table.Column<string>(type: "text", nullable: true)
},
constraints: table =>
{
constraints: table => {
table.PrimaryKey("PK_Guild", x => x.Id);
});
migrationBuilder.CreateTable(
name: "GuildMember",
columns: table => new
{
columns: table => new {
Id = table.Column<Guid>(type: "uuid", nullable: false),
CharacterId = table.Column<Guid>(type: "uuid", nullable: false),
GuildId = table.Column<Guid>(type: "uuid", nullable: false),
Rank = table.Column<byte>(type: "smallint", nullable: false)
},
constraints: table =>
{
constraints: table => {
table.PrimaryKey("PK_GuildMember", x => x.Id);
table.ForeignKey(
name: "FK_GuildMember_Characters_CharacterId",
@ -95,8 +89,7 @@ public partial class AddGuildData : Migration
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Down(MigrationBuilder migrationBuilder) {
migrationBuilder.DropForeignKey(
name: "FK_Characters_Guild_GuildId",
table: "Characters");

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore.Migrations;
@ -7,11 +7,9 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Server.DB.Migrations;
/// <inheritdoc />
public partial class GuildIsNotRequired : Migration
{
public partial class GuildIsNotRequired : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder) {
migrationBuilder.RenameColumn(
name: "ItemType",
table: "InventoryItem",
@ -19,8 +17,7 @@ public partial class GuildIsNotRequired : Migration
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Down(MigrationBuilder migrationBuilder) {
migrationBuilder.RenameColumn(
name: "InventoryTab",
table: "InventoryItem",

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore.Migrations;
@ -7,11 +7,9 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Server.DB.Migrations;
/// <inheritdoc />
public partial class SwitchToDataAnnotations : Migration
{
public partial class SwitchToDataAnnotations : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder) {
migrationBuilder.DropForeignKey(
name: "FK_Characters_Accounts_AccountId",
table: "Characters");
@ -113,8 +111,7 @@ public partial class SwitchToDataAnnotations : Migration
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Down(MigrationBuilder migrationBuilder) {
migrationBuilder.DropForeignKey(
name: "FK_Characters_Accounts_AccountId",
table: "Characters");

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore.Migrations;
@ -7,11 +7,9 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Server.DB.Migrations;
/// <inheritdoc />
public partial class DBPoolingAndLazyLoadingSupport : Migration
{
public partial class DBPoolingAndLazyLoadingSupport : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder) {
migrationBuilder.AlterColumn<string>(
name: "Name",
table: "Characters",
@ -36,8 +34,7 @@ public partial class DBPoolingAndLazyLoadingSupport : Migration
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Down(MigrationBuilder migrationBuilder) {
migrationBuilder.DropIndex(
name: "IX_Characters_Id",
table: "Characters");

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore.Migrations;
@ -7,11 +7,9 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Server.DB.Migrations;
/// <inheritdoc />
public partial class FixInventoryItemAsDbset : Migration
{
public partial class FixInventoryItemAsDbset : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder) {
migrationBuilder.DropForeignKey(
name: "FK_Characters_Guild_GuildId",
table: "Characters");
@ -86,8 +84,7 @@ public partial class FixInventoryItemAsDbset : Migration
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Down(MigrationBuilder migrationBuilder) {
migrationBuilder.DropForeignKey(
name: "FK_Characters_Guilds_GuildId",
table: "Characters");

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore.Migrations;
@ -7,11 +7,9 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Server.DB.Migrations;
/// <inheritdoc />
public partial class MissingCascadeDeletionOnCharacter : Migration
{
public partial class MissingCascadeDeletionOnCharacter : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder) {
migrationBuilder.DropForeignKey(
name: "FK_InventoryItems_Characters_CharacterId",
table: "InventoryItems");
@ -26,8 +24,7 @@ public partial class MissingCascadeDeletionOnCharacter : Migration
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Down(MigrationBuilder migrationBuilder) {
migrationBuilder.DropForeignKey(
name: "FK_InventoryItems_Characters_CharacterId",
table: "InventoryItems");

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore.Migrations;
@ -7,11 +7,9 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Server.DB.Migrations;
/// <inheritdoc />
public partial class IndexingAndVariousOtherAnnotations : Migration
{
public partial class IndexingAndVariousOtherAnnotations : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder) {
migrationBuilder.DropForeignKey(
name: "FK_Characters_Accounts_AccountId",
table: "Characters");
@ -112,8 +110,7 @@ public partial class IndexingAndVariousOtherAnnotations : Migration
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Down(MigrationBuilder migrationBuilder) {
migrationBuilder.DropForeignKey(
name: "FK_Characters_Accounts_AccountId",
table: "Characters");

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore.Migrations;
@ -7,11 +7,9 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Server.DB.Migrations;
/// <inheritdoc />
public partial class VariousDeletionBehaviours : Migration
{
public partial class VariousDeletionBehaviours : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder) {
migrationBuilder.DropForeignKey(
name: "FK_Characters_Accounts_AccountId",
table: "Characters");
@ -45,8 +43,7 @@ public partial class VariousDeletionBehaviours : Migration
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Down(MigrationBuilder migrationBuilder) {
migrationBuilder.DropForeignKey(
name: "FK_Accounts_GuildMember_GuildMemberId",
table: "Accounts");

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore.Migrations;
@ -7,11 +7,9 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Server.DB.Migrations;
/// <inheritdoc />
public partial class FixEntityRelationships : Migration
{
public partial class FixEntityRelationships : Migration {
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder) {
migrationBuilder.DropForeignKey(
name: "FK_Accounts_GuildMember_GuildMemberId",
table: "Accounts");
@ -88,8 +86,7 @@ public partial class FixEntityRelationships : Migration
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Down(MigrationBuilder migrationBuilder) {
migrationBuilder.DropIndex(
name: "IX_GuildMember_CharacterId",
table: "GuildMember");

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Continuity.AuthServer.DB.Documents;
using JetBrains.Annotations;
@ -6,14 +6,21 @@ using Microsoft.EntityFrameworkCore;
namespace Continuity.AuthServer.DB;
public class WonderkingContext : DbContext
{
public WonderkingContext([NotNull] DbContextOptions options) : base(options)
{
public class WonderkingContext : DbContext {
public WonderkingContext([NotNull] DbContextOptions options) : base(options) {
}
public DbSet<Account> Accounts { get; set; }
public DbSet<Character> Characters { get; set; }
public DbSet<InventoryItem> InventoryItems { get; set; }
public DbSet<Guild> Guilds { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Account>(entity => {
entity.HasIndex(e => e.Username).IsUnique();
entity.HasIndex(e => e.Id).IsUnique();
entity.Property(e => e.Id).HasColumnType("uuid");
});
}
}

View file

@ -1,12 +1,11 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Continuity.AuthServer.PacketHandlers;
using Microsoft.Extensions.Logging;
namespace Continuity.AuthServer.LoggerMessages;
internal static partial class LoginHandlerLoggerMessages
{
internal static partial class LoginHandlerLoggerMessages {
[LoggerMessage(EventId = 0, Level = LogLevel.Information,
Message = "Login data: Username {Username} & Password {Password}")]
public static partial void LoginData(this ILogger<LoginHandler> logger, string username, string password);

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
@ -6,8 +6,7 @@ using Wonderking.Packets;
namespace Continuity.AuthServer.LoggerMessages;
internal static partial class PacketLoggerMessages
{
internal static partial class PacketLoggerMessages {
[LoggerMessage(EventId = 0, Level = LogLevel.Information,
Message = "Packet creation function created for {PacketID}")]
public static partial void PacketCreationFunctionCreated(this ILogger logger, OperationCode packetId);

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Continuity.AuthServer.DB.Documents;
using Wonderking.Game.Data.Character;
@ -6,8 +6,7 @@ using Wonderking.Packets.Outgoing.Data;
namespace Continuity.AuthServer.PacketHandlers;
public partial class ChannelSelectionHandler
{
public partial class ChannelSelectionHandler {
private sealed record InventoryItemProjection(ushort ItemId, byte Slot, InventoryTab InventoryTab);
private sealed record CharacterDataProjection(

View file

@ -1,67 +1,56 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Continuity.AuthServer.DB;
using Continuity.AuthServer.DB.Documents;
using DotNext.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using NetCoreServer;
using RaiNote.PacketMediator;
using Wonderking.Packets.Incoming;
using Wonderking.Packets.Outgoing;
using Wonderking.Packets.Outgoing.Data;
namespace Continuity.AuthServer.PacketHandlers;
public partial class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPacket>
{
public partial class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPacket, AuthSession> {
private readonly WonderkingContext _wonderkingContext;
public ChannelSelectionHandler(WonderkingContext wonderkingContext)
{
public ChannelSelectionHandler(WonderkingContext wonderkingContext) {
_wonderkingContext = wonderkingContext;
}
public async Task HandleAsync(ChannelSelectionPacket packet, TcpSession session)
{
if (session is not AuthSession authSession)
{
return;
}
public async Task HandleAsync(ChannelSelectionPacket packet, AuthSession session,
CancellationToken cancellationToken) {
ChannelSelectionResponsePacket responsePacket;
var guildNameResponsePacket = new CharacterSelectionSetGuildNamePacket { GuildNames = Array.Empty<string>() };
var accountExists =
await _wonderkingContext.Accounts.AsNoTracking().AnyAsync(a => a.Id == authSession.AccountId);
await _wonderkingContext.Accounts.AsNoTracking()
.AnyAsync(a => a.Id == session.AccountId, cancellationToken);
var amountOfCharacter = await _wonderkingContext.Characters.AsNoTracking().Include(c => c.Account)
.Where(c => c.Account.Id == authSession.AccountId).Take(3)
.CountAsync();
.Where(c => c.Account.Id == session.AccountId).Take(3)
.CountAsync(cancellationToken);
if (!accountExists)
{
if (!accountExists) {
return;
}
if (amountOfCharacter > 0)
{
responsePacket = new ChannelSelectionResponsePacket
{
if (amountOfCharacter > 0) {
responsePacket = new ChannelSelectionResponsePacket {
ChannelIsFullFlag = 0,
Endpoint = "127.0.0.1",
Port = 2000,
Characters = await GetCharacterDataAsync(authSession.AccountId).ToArrayAsync()
Characters = await GetCharacterDataAsync(session.AccountId).ToArrayAsync(token: cancellationToken)
};
guildNameResponsePacket.GuildNames =
await _wonderkingContext.Characters.AsNoTracking().Include(c => c.Account).Include(c => c.GuildMember)
.ThenInclude(gm => gm.Guild)
.Where(c => c.Account.Id == authSession.AccountId && c.GuildMember.Guild != null)
.Select(c => c.GuildMember.Guild.Name).Take(3).ToArrayAsync();
.Where(c => c.Account.Id == session.AccountId && c.GuildMember.Guild != null)
.Select(c => c.GuildMember.Guild.Name).Take(3).ToArrayAsync(cancellationToken);
}
else
{
responsePacket = new ChannelSelectionResponsePacket
{
else {
responsePacket = new ChannelSelectionResponsePacket {
ChannelIsFullFlag = 0,
Endpoint = "127.0.0.1",
Port = 2000,
@ -69,23 +58,19 @@ public partial class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPa
};
}
await authSession.SendAsync(responsePacket);
await session.SendAsync(responsePacket);
if (guildNameResponsePacket.GuildNames.Length > 0 &&
guildNameResponsePacket.GuildNames.Select(n => n != string.Empty).Any())
{
await authSession.SendAsync(guildNameResponsePacket);
guildNameResponsePacket.GuildNames.Select(n => n != string.Empty).Any()) {
await session.SendAsync(guildNameResponsePacket);
}
}
private static ushort[] GetItemIDsByInventoryTab(IEnumerable<InventoryItemProjection> items)
{
private static ushort[] GetItemIDsByInventoryTab(IEnumerable<InventoryItemProjection> items) {
var ids = new ushort[20];
ids.AsSpan().Clear();
foreach (var item in items)
{
if (item.Slot > 20)
{
foreach (var item in items) {
if (item.Slot > 20) {
continue;
}
@ -95,8 +80,7 @@ public partial class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPa
return ids;
}
private async IAsyncEnumerable<CharacterData> GetCharacterDataAsync(Guid accountId)
{
private async IAsyncEnumerable<CharacterData> GetCharacterDataAsync(Guid accountId) {
var characterDataProjections = _wonderkingContext.Characters.AsNoTracking().AsSplitQuery()
.Include(c => c.InventoryItems).Include(c => c.Account)
.Where(c => c.Account.Id == accountId)
@ -113,10 +97,8 @@ public partial class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPa
new InventoryItemProjection(i.ItemId, i.Slot, i.InventoryTab))
)).Take(3);
await foreach (var c in characterDataProjections)
{
yield return new CharacterData
{
await foreach (var c in characterDataProjections) {
yield return new CharacterData {
Name = c.Name,
Job = c.JobData,
Gender = c.Gender,

View file

@ -1,10 +1,10 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Continuity.AuthServer.DB;
using Continuity.AuthServer.DB.Documents;
using Continuity.AuthServer.Services;
using Microsoft.EntityFrameworkCore;
using NetCoreServer;
using RaiNote.PacketMediator;
using Wonderking.Game.Data.Character;
using Wonderking.Game.Mapping;
using Wonderking.Packets.Incoming;
@ -13,31 +13,24 @@ using Wonderking.Packets.Outgoing.Data;
namespace Continuity.AuthServer.PacketHandlers;
public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
{
public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket, AuthSession> {
private readonly CharacterStatsMappingConfiguration _characterStatsMapping;
private readonly ItemObjectPoolService _itemObjectPoolService;
private readonly WonderkingContext _wonderkingContext;
public CharacterCreationHandler(WonderkingContext wonderkingContext, ItemObjectPoolService itemObjectPoolService,
CharacterStatsMappingConfiguration characterStatsMappingConfiguration)
{
CharacterStatsMappingConfiguration characterStatsMappingConfiguration) {
_wonderkingContext = wonderkingContext;
_itemObjectPoolService = itemObjectPoolService;
_characterStatsMapping = characterStatsMappingConfiguration;
}
public async Task HandleAsync(CharacterCreationPacket packet, TcpSession session)
{
if (session is not AuthSession authSession)
{
return;
}
public async Task HandleAsync(CharacterCreationPacket packet, AuthSession session,
CancellationToken cancellationToken) {
var account =
await _wonderkingContext.Accounts.Include(a => a.Characters).FirstOrDefaultAsync(a => a.Id == session.AccountId, cancellationToken);
var account = await _wonderkingContext.Accounts.FirstOrDefaultAsync(a => a.Id == authSession.AccountId);
if (account is null)
{
if (account is null) {
return;
}
@ -46,11 +39,10 @@ public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
var toBeAddedCharacter = CreateDefaultCharacter(packet, account, items, firstJobConfig);
account.Characters.Add(toBeAddedCharacter);
await _wonderkingContext.SaveChangesAsync();
await _wonderkingContext.SaveChangesAsync(cancellationToken);
var character =
new CharacterData
{
new CharacterData {
Name = toBeAddedCharacter.Name,
Job = toBeAddedCharacter.JobData,
Gender = toBeAddedCharacter.Gender,
@ -68,16 +60,14 @@ public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
.Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).AsEnumerable())
};
await authSession.SendAsync(new CharacterCreationResponsePacket
{
await session.SendAsync(new CharacterCreationResponsePacket {
Character = character,
Slot = packet.Slot,
isDuplicate = false
});
}
private InventoryItem[] CreateDefaultItems(CharacterCreationPacket packet, JobSpecificMapping firstJobConfig)
{
private InventoryItem[] CreateDefaultItems(CharacterCreationPacket packet, JobSpecificMapping firstJobConfig) {
var mappedDefaultItems = _characterStatsMapping.DefaultCharacterMapping.Items
.Select(i => _itemObjectPoolService.GetBaseInventoryItem(i.Id, i.Quantity)).ToArray();
@ -94,10 +84,8 @@ public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
}
private static Character CreateDefaultCharacter(CharacterCreationPacket packet, Account account,
InventoryItem[] items, JobSpecificMapping firstJobConfig)
{
return new Character()
{
InventoryItem[] items, JobSpecificMapping firstJobConfig) {
return new Character {
Account = account,
MapId = 300,
Name = packet.Name,
@ -115,10 +103,8 @@ public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
};
}
private JobSpecificMapping SelectFirstJobConfig(byte firstJob)
{
return firstJob switch
{
private JobSpecificMapping SelectFirstJobConfig(byte firstJob) {
return firstJob switch {
1 => _characterStatsMapping.Swordsman,
2 => _characterStatsMapping.Mage,
3 => _characterStatsMapping.Thief,
@ -127,8 +113,7 @@ public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
};
}
private InventoryItem[] CreateChosenItems(CharacterCreationPacket packet)
{
private InventoryItem[] CreateChosenItems(CharacterCreationPacket packet) {
return new[]
{
_itemObjectPoolService.GetBaseInventoryItem((ushort)((packet.FirstJob - 1) * 6 +
@ -144,27 +129,22 @@ public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
};
}
private static int CalculateCurrentHealth(ushort level, JobSpecificMapping firstJobConfig)
{
private static int CalculateCurrentHealth(ushort level, JobSpecificMapping firstJobConfig) {
return (int)((level - 1) * firstJobConfig.DynamicStats.HealthPerLevel +
firstJobConfig.BaseStats.Vitality * firstJobConfig.DynamicStats.HealthPerVitality);
}
private static int CalculateCurrentMana(ushort level, JobSpecificMapping firstJobConfig)
{
private static int CalculateCurrentMana(ushort level, JobSpecificMapping firstJobConfig) {
return (int)((level - 1) * firstJobConfig.DynamicStats.ManaPerLevel +
firstJobConfig.BaseStats.Wisdom * firstJobConfig.DynamicStats.ManaPerWisdom);
}
private static ushort[] GetItemIDsByInventoryTab(IEnumerable<Tuple<ushort, byte>> items)
{
private static ushort[] GetItemIDsByInventoryTab(IEnumerable<Tuple<ushort, byte>> items) {
Span<ushort> ids = stackalloc ushort[20];
ids.Clear();
foreach (var item in items)
{
if (item.Item2 > 20)
{
foreach (var item in items) {
if (item.Item2 > 20) {
continue;
}

View file

@ -1,43 +1,34 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Continuity.AuthServer.DB;
using Microsoft.EntityFrameworkCore;
using NetCoreServer;
using RaiNote.PacketMediator;
using Wonderking.Packets.Incoming;
using Wonderking.Packets.Outgoing;
namespace Continuity.AuthServer.PacketHandlers;
public class CharacterDeletionHandler : IPacketHandler<CharacterDeletePacket>
{
public class CharacterDeletionHandler : IPacketHandler<CharacterDeletePacket, AuthSession> {
private readonly WonderkingContext _wonderkingContext;
public CharacterDeletionHandler(WonderkingContext wonderkingContext)
{
public CharacterDeletionHandler(WonderkingContext wonderkingContext) {
_wonderkingContext = wonderkingContext;
}
public async Task HandleAsync(CharacterDeletePacket packet, TcpSession session)
{
if (session is not AuthSession authSession)
{
session.Disconnect();
return;
}
public async Task HandleAsync(CharacterDeletePacket packet, AuthSession session,
CancellationToken cancellationToken) {
var character = await _wonderkingContext.Characters.FirstOrDefaultAsync(x => x.Name == packet.Name &&
x.Account.Id == authSession.AccountId);
x.Account.Id == session.AccountId, cancellationToken);
var response = new CharacterDeleteResponsePacket { HasToBeZero = 0 };
if (character == null)
{
await authSession.SendAsync(response);
if (character == null) {
await session.SendAsync(response);
return;
}
_wonderkingContext.Characters.Remove(character);
await _wonderkingContext.SaveChangesAsync();
await _wonderkingContext.SaveChangesAsync(cancellationToken);
await authSession.SendAsync(response);
await session.SendAsync(response);
}
}

View file

@ -1,12 +1,11 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Runtime.InteropServices;
namespace Continuity.AuthServer.PacketHandlers;
[StructLayout(LayoutKind.Auto)]
public struct CharacterMappingItemEntry
{
public struct CharacterMappingItemEntry {
public required ushort Id { get; set; }
public required ushort Quantity { get; set; }
}

View file

@ -1,29 +1,27 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Continuity.AuthServer.DB;
using Microsoft.EntityFrameworkCore;
using NetCoreServer;
using RaiNote.PacketMediator;
using Wonderking.Packets.Incoming;
using Wonderking.Packets.Outgoing;
namespace Continuity.AuthServer.PacketHandlers;
public class CharacterNameCheckHandler : IPacketHandler<CharacterNameCheckPacket>
{
public class CharacterNameCheckHandler : IPacketHandler<CharacterNameCheckPacket, AuthSession> {
private readonly WonderkingContext _wonderkingContext;
public CharacterNameCheckHandler(WonderkingContext wonderkingContext)
{
public CharacterNameCheckHandler(WonderkingContext wonderkingContext) {
_wonderkingContext = wonderkingContext;
}
public async Task HandleAsync(CharacterNameCheckPacket packet, TcpSession session)
{
var isTaken = await _wonderkingContext.Characters.AnyAsync(c => c.Name == packet.Name);
public async Task HandleAsync(CharacterNameCheckPacket packet, AuthSession session,
CancellationToken cancellationToken) {
var isTaken =
await _wonderkingContext.Characters.AnyAsync(c => c.Name == packet.Name,
cancellationToken);
var responsePacket = new CharacterNameCheckPacketResponse { IsTaken = isTaken };
if (session is AuthSession authSession)
{
await authSession.SendAsync(responsePacket);
}
await session.SendAsync(responsePacket);
}
}

View file

@ -1,37 +0,0 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
using System.Diagnostics;
using JetBrains.Annotations;
using NetCoreServer;
using Wonderking.Packets;
namespace Continuity.AuthServer.PacketHandlers;
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
public interface IPacketHandler<in T> : IPacketHandler where T : IPacket
{
[UsedImplicitly]
public Task HandleAsync(T packet, TcpSession session);
async Task<bool> IPacketHandler.TryHandleAsync(IPacket packet, TcpSession session)
{
if (packet is not T tPacket)
{
return false;
}
using (var activity = new ActivitySource(nameof(Server)).StartActivity("HandleAsync"))
{
activity?.SetTag("Handler", this.ToString());
activity?.SetTag("PacketId", packet.ToString());
await HandleAsync(tPacket, session);
}
return true;
}
}
public interface IPacketHandler
{
Task<bool> TryHandleAsync(IPacket packet, TcpSession session);
}

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Diagnostics;
using System.Security.Cryptography;
@ -10,48 +10,44 @@ using Konscious.Security.Cryptography;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using NetCoreServer;
using RaiNote.PacketMediator;
using Wonderking.Packets.Incoming;
using Wonderking.Packets.Outgoing;
using Wonderking.Packets.Outgoing.Data;
namespace Continuity.AuthServer.PacketHandlers;
public class LoginHandler : IPacketHandler<LoginInfoPacket>
{
public class LoginHandler : IPacketHandler<LoginInfoPacket, AuthSession> {
private static readonly ActivitySource _activitySource = new(nameof(Server));
private readonly IConfiguration _configuration;
private readonly ILogger<LoginHandler> _logger;
private readonly WonderkingContext _wonderkingContext;
private static readonly ActivitySource _activitySource = new ActivitySource(nameof(Server));
public LoginHandler(ILogger<LoginHandler> logger, WonderkingContext wonderkingContext, IConfiguration configuration)
{
public LoginHandler(ILogger<LoginHandler> logger, WonderkingContext wonderkingContext, IConfiguration configuration) {
_logger = logger;
_wonderkingContext = wonderkingContext;
_configuration = configuration;
}
public async Task HandleAsync(LoginInfoPacket packet, TcpSession session)
{
public async Task HandleAsync(LoginInfoPacket packet, AuthSession session, CancellationToken cancellationToken) {
LoginResponseReason loginResponseReason;
_logger.LoginData(packet.Username, packet.Password);
var account = await _wonderkingContext.Accounts.FirstOrDefaultAsync(a => a.Username == packet.Username);
var account =
await _wonderkingContext.Accounts.FirstOrDefaultAsync(a => a.Username == packet.Username,
cancellationToken);
if (account == null)
{
if (_configuration.GetSection("Testing").GetValue<bool>("CreateAccountOnLogin"))
{
if (account == null) {
if (_configuration.GetSection("Testing").GetValue<bool>("CreateAccountOnLogin")) {
loginResponseReason = await CreateAccountOnLoginAsync(packet.Username, packet.Password);
account = await _wonderkingContext.Accounts.FirstOrDefaultAsync(a => a.Username == packet.Username);
account = await _wonderkingContext.Accounts.FirstOrDefaultAsync(a => a.Username == packet.Username,
cancellationToken);
}
else
{
else {
_logger.RequestedAccountDoesNotExist(packet.Username);
loginResponseReason = LoginResponseReason.AccountDoesNotExit;
}
}
else
{
else {
var salt = account.Salt;
var tempPasswordBytes = await GetPasswordHashAsync(packet.Password, salt, account.Id);
loginResponseReason = tempPasswordBytes.SequenceEqual(account.Password)
@ -61,50 +57,41 @@ public class LoginHandler : IPacketHandler<LoginInfoPacket>
var channelData = new ServerChannelData[1];
channelData[0] = new ServerChannelData { ChannelId = 0, LoadPercentage = 0, ServerId = 0 };
var loginResponsePacket = new LoginResponsePacket
{
var loginResponsePacket = new LoginResponsePacket {
ResponseReason = loginResponseReason,
ChannelData = channelData.ToArray(),
UnknownFlag = 1,
IsGameMaster = true
};
var authSession = session as AuthSession;
if (account != null && authSession != null)
{
authSession.AccountId = account.Id;
if (account != null) {
session.AccountId = account.Id;
}
_logger.LogInformation("LoginResponsePacket: {@LoginResponsePacket}", loginResponsePacket);
_ = authSession?.SendAsync(loginResponsePacket);
_ = session.SendAsync(loginResponsePacket);
}
private static async Task<byte[]> GetPasswordHashAsync(string password, byte[] salt, Guid userId)
{
using var activity = _activitySource.StartActivity("GetPasswordHashAsync");
private static async Task<byte[]> GetPasswordHashAsync(string password, byte[] salt, Guid userId) {
using var activity = _activitySource.StartActivity();
// https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Chea1t_Sheet.html#argon2id
// "Use Argon2id with a minimum configuration of 19 MiB of memory, an iteration count of 2, and 1 degree of parallelism."
var argon2Id = new Argon2id(Encoding.ASCII.GetBytes(password))
{
MemorySize = 1024 * 19,
Iterations = 2,
DegreeOfParallelism = 1,
Salt = salt,
AssociatedData = userId.ToByteArray()
};
using var argon2Id = new Argon2id(Encoding.ASCII.GetBytes(password));
argon2Id.MemorySize = 1024 * 19;
argon2Id.Iterations = 2;
argon2Id.DegreeOfParallelism = 1;
argon2Id.Salt = salt;
argon2Id.AssociatedData = userId.ToByteArray();
return await argon2Id.GetBytesAsync(16);
}
private async Task<LoginResponseReason> CreateAccountOnLoginAsync(string username, string password)
{
using var activity = _activitySource.StartActivity("CreateAccountOnLoginAsync");
private async Task<LoginResponseReason> CreateAccountOnLoginAsync(string username, string password) {
using var activity = _activitySource.StartActivity();
LoginResponseReason loginResponseReason;
var transaction =
await _wonderkingContext.Database.BeginTransactionAsync();
await using (transaction)
{
try
{
await using (transaction) {
try {
var salt = RandomNumberGenerator.GetBytes(16);
var finalAccount = await _wonderkingContext.Accounts.AddAsync(new Account(username,
Array.Empty<byte>(), "", 0, salt));
@ -117,8 +104,7 @@ public class LoginHandler : IPacketHandler<LoginInfoPacket>
await transaction.CommitAsync();
}
catch (Exception)
{
catch (Exception) {
await transaction.RollbackAsync(); // Rollback the transaction on error
throw;
}

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using MassTransit;
using Wonderking.Packets;
@ -6,11 +6,9 @@ using Wonderking.Packets;
namespace Continuity.AuthServer.Packets;
[MessageUrn("packets")]
public class RawPacket
{
public class RawPacket {
public RawPacket(OperationCode operationCode, Span<byte> messageBody, uint aliveTime, byte unknownValue2,
byte unknownValue, Guid sessionId, AuthSession session)
{
byte unknownValue, Guid sessionId, AuthSession session) {
MessageBody = messageBody.ToArray();
UnknownValue2 = unknownValue2;
UnknownValue = unknownValue;

View file

@ -1,9 +1,11 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Net;
using System.Reflection;
using System.Text.Json;
using Continuity.AuthServer;
using Continuity.AuthServer.DB;
using Continuity.AuthServer.PacketHandlers;
using Continuity.AuthServer.Services;
using MassTransit;
using Microsoft.EntityFrameworkCore;
@ -17,7 +19,9 @@ using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using RaiNote.PacketMediator;
using Wonderking.Game.Mapping;
using Wonderking.Packets;
var builder = Host.CreateApplicationBuilder();
#if DEBUG
@ -31,70 +35,43 @@ builder.Configuration.AddJsonFile("settings.json", true, true)
.AddEnvironmentVariables().Build();
builder.Services.AddLogging();
var loggerFactory = LoggerFactory.Create(loggingBuilder =>
{
var loggerFactory = LoggerFactory.Create(loggingBuilder => {
loggingBuilder.AddFile("logs/Continuity.AuthServer-{Date}.log", LogLevel.Trace);
loggingBuilder.AddFile("logs/Continuity.AuthServer-{Date}.json.log", LogLevel.Trace, isJson: true);
loggingBuilder.AddConsole();
});
var configuration = builder.Configuration;
if (configuration.GetValue<bool>("Tracing:Enabled"))
{
Action<ResourceBuilder> resourceBuilderAction = r => r
.AddService("Continuity", serviceInstanceId: Environment.MachineName);
builder.Services.AddOpenTelemetry()
.ConfigureResource(resourceBuilderAction)
.WithTracing(tracing =>
{
tracing.AddSource(nameof(Server));
//tracing.AddSource("MassTransit");
tracing.AddEntityFrameworkCoreInstrumentation(options => options.SetDbStatementForText = true);
tracing.AddNpgsql();
})
.WithMetrics(metrics =>
{
metrics.AddRuntimeInstrumentation();
metrics.AddProcessInstrumentation();
});
builder.Logging.AddOpenTelemetry(logging =>
{
var resourceBuilder = ResourceBuilder.CreateDefault();
resourceBuilderAction(resourceBuilder);
logging.SetResourceBuilder(resourceBuilder);
loggingBuilder.AddOpenTelemetry(logging => {
logging.AddOtlpExporter();
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});
builder.Services.Configure<OpenTelemetryLoggerOptions>(logging =>
{
logging.AddOtlpExporter(options =>
{
options.Endpoint = new Uri(configuration["OTLP:Logging:Endpoint"] ?? string.Empty);
});
});
builder.Services.ConfigureOpenTelemetryMeterProvider(metrics =>
{
metrics.AddOtlpExporter(options =>
options.Endpoint = new Uri(configuration["OTLP:Metrics:Endpoint"] ?? string.Empty));
});
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing =>
{
tracing.AddZipkinExporter(options =>
options.Endpoint = new Uri(configuration["Zipkin:Endpoint"] ?? string.Empty));
tracing.AddOtlpExporter(options => options.Endpoint = new Uri(configuration["OTLP:Tracing:Endpoint"] ?? string.Empty));
});
var configuration = builder.Configuration;
builder.Services.AddOpenTelemetry()
.WithMetrics(metrics => {
metrics.AddRuntimeInstrumentation().AddProcessInstrumentation().AddMeter("Microsoft.AspNetCore.Hosting", nameof(Continuity.AuthServer))
.ConfigureResource(resourceBuilder => resourceBuilder.AddService("Continuity", serviceNamespace: "Wonderking", serviceVersion: "0.0.1"));
})
.WithTracing(tracing => {
if (builder.Environment.IsDevelopment()) {
tracing.SetSampler<AlwaysOnSampler>();
}
tracing.AddEntityFrameworkCoreInstrumentation(options => options.SetDbStatementForText = true).AddNpgsql();
});
builder.Services.AddMetrics();
builder.Services.Configure<OpenTelemetryLoggerOptions>(logging => logging.AddOtlpExporter());
builder.Services.ConfigureOpenTelemetryMeterProvider(meter => meter.AddOtlpExporter());
builder.Services.ConfigureOpenTelemetryTracerProvider(tracer => tracer.AddOtlpExporter());
builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
builder.Services.AddDbContextPool<WonderkingContext>(o =>
{
builder.Services.AddDbContextPool<WonderkingContext>(o => {
o.UseNpgsql(
$"Host={configuration["DB:Host"]};Username={configuration["DB:Username"]};Password={configuration["DB:Password"]};Database={configuration["DB:Database"]};Port={configuration["DB:Port"]}")
.EnableSensitiveDataLogging().UseLazyLoadingProxies().UseLoggerFactory(loggerFactory);
.EnableSensitiveDataLogging().UseLoggerFactory(loggerFactory);
});
builder.Services.AddSingleton<CharacterStatsMappingConfiguration>(
@ -102,24 +79,33 @@ builder.Services.AddSingleton<CharacterStatsMappingConfiguration>(
File.ReadAllText("config/character-stats.mapping.json")) ?? throw new InvalidOperationException());
builder.Services.AddSingleton<ILoggerFactory>(loggerFactory);
builder.Services.AddSingleton<PacketDistributorService>();
builder.Services.AddSingleton(provider =>
new PacketDistributorService<OperationCode, AuthSession>(
provider.GetRequiredService<IServiceProvider>(),
new List<Assembly> { Assembly.GetAssembly(typeof(OperationCode)) }.AsReadOnly(),
new List<Assembly> { Assembly.GetAssembly(typeof(LoginHandler)) }.AsReadOnly()
));
builder.Services.AddSingleton<ItemObjectPoolService>();
builder.Services.AddHostedService(provider =>
provider.GetService<ItemObjectPoolService>() ?? throw new InvalidOperationException());
builder.Services.AddHostedService(provider =>
provider.GetService<PacketDistributorService>() ?? throw new InvalidOperationException());
builder.Services.AddMassTransit(x =>
{
x.UsingInMemory((context, configurator) => configurator.ConfigureEndpoints(context));
x.AddMediator(cfg => cfg.AddConsumers(Assembly.GetExecutingAssembly()));
});
builder.Services.AddHostedService(provider => new WonderkingAuthServer(IPAddress.Any, 10001,
provider.GetService<ILogger<WonderkingAuthServer>>() ?? throw new InvalidOperationException(),
provider.GetService<IServiceProvider>() ?? throw new InvalidOperationException()));
builder.Services.AddHostedService(provider =>
provider.GetService<ItemObjectPoolService>() ?? throw new InvalidOperationException());
builder.Services.AddHostedService(provider =>
provider.GetService<PacketDistributorService<OperationCode, AuthSession>>() ??
throw new InvalidOperationException());
builder.Services.AddMassTransit(x => {
x.UsingInMemory((context, configurator) => configurator.ConfigureEndpoints(context));
x.AddMediator(cfg => cfg.AddConsumers(Assembly.GetExecutingAssembly()));
});
using var host = builder.Build();
using (var scope = host.Services.CreateScope())
{
await using (var scope = host.Services.CreateAsyncScope()) {
var db = scope.ServiceProvider.GetRequiredService<WonderkingContext>();
await db.Database.MigrateAsync();
}

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Collections.Concurrent;
using Continuity.AuthServer.DB.Documents;
@ -10,14 +10,12 @@ using Wonderking.Game.Reader;
namespace Continuity.AuthServer.Services;
public class ItemObjectPoolService : IHostedService
{
public class ItemObjectPoolService : IHostedService {
private readonly ConcurrentDictionary<uint, ItemObject> _itemObjectPool;
private readonly ItemReader _itemReader;
private readonly ILogger<ItemObjectPoolService> _logger;
public ItemObjectPoolService(IConfiguration configuration, ILogger<ItemObjectPoolService> logger)
{
public ItemObjectPoolService(IConfiguration configuration, ILogger<ItemObjectPoolService> logger) {
_logger = logger;
_itemReader = new ItemReader(configuration.GetSection("Game").GetSection("Data").GetValue<string>("Path") ??
string.Empty);
@ -25,58 +23,48 @@ public class ItemObjectPoolService : IHostedService
_itemObjectPool = new ConcurrentDictionary<uint, ItemObject>();
}
public Task StartAsync(CancellationToken cancellationToken)
{
public Task StartAsync(CancellationToken cancellationToken) {
var amountOfEntries = _itemReader.GetAmountOfEntries();
Parallel.For(0, (int)amountOfEntries, i =>
{
Parallel.For(0, (int)amountOfEntries, i => {
var itemObject = _itemReader.GetEntry((uint)i);
var result = _itemObjectPool.TryAdd(itemObject.ItemID, itemObject);
if (!result)
{
if (!result) {
throw new KeyNotFoundException($"Failed to add item {itemObject.ItemID} to the item object pool");
}
_logger.LogTrace("Item with {ID} has been added", itemObject.ItemID);
_logger.LogTrace("Item with id:{ID} and name: '{Name}' has been added", itemObject.ItemID, itemObject.Name);
});
_logger.LogInformation("A total of {AmountOfEntries} items have been added to the item object pool",
_itemObjectPool.Count);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
public Task StopAsync(CancellationToken cancellationToken) {
return Task.CompletedTask;
}
public ItemObject GetItem(ushort itemId)
{
public ItemObject GetItem(ushort itemId) {
_ = _itemObjectPool.TryGetValue(itemId, out var itemObject);
return itemObject;
}
public bool ContainsItem(ushort itemId)
{
public bool ContainsItem(ushort itemId) {
return _itemObjectPool.ContainsKey(itemId);
}
public IQueryable<ItemObject> QueryItems()
{
public IQueryable<ItemObject> QueryItems() {
return _itemObjectPool.AsReadOnly().Values.AsQueryable();
}
public InventoryItem GetBaseInventoryItem(ushort itemId, ushort count = 1, bool isWorn = false)
{
public InventoryItem GetBaseInventoryItem(ushort itemId, ushort count = 1, bool isWorn = false) {
var item = GetItem(itemId);
return new InventoryItem
{
return new InventoryItem {
ItemId = itemId,
Count = count,
Slot = (byte)item.SlotNo1,
InventoryTab =
item.ItemType switch
{
item.ItemType switch {
1 => InventoryTab.WornCashEquipment,
2 => isWorn ? InventoryTab.WornEquipment : InventoryTab.Equipment,
3 => InventoryTab.Etc,

View file

@ -1,187 +0,0 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
using System.Collections.Concurrent;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Reflection;
using Continuity.AuthServer.LoggerMessages;
using Continuity.AuthServer.PacketHandlers;
using Continuity.AuthServer.Packets;
using DotNext.Collections.Generic;
using DotNext.Linq.Expressions;
using DotNext.Metaprogramming;
using MassTransit.Internals;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic.CompilerServices;
using Newtonsoft.Json;
using Wonderking.Packets;
namespace Continuity.AuthServer.Services;
using static CodeGenerator;
using static ExpressionBuilder;
public class PacketDistributorService : IHostedService, IDisposable
{
private readonly ConcurrentQueue<RawPacket> _concurrentQueue;
private readonly ILogger<PacketDistributorService> _logger;
private readonly IServiceProvider _serviceProvider;
private ImmutableDictionary<OperationCode,
Func<byte[], IPacket>> _deserializationMap;
private ConcurrentDictionary<OperationCode, IPacketHandler> _packetHandlersInstantiation;
private readonly ActivitySource _activitySource;
public PacketDistributorService(ILogger<PacketDistributorService> logger, IServiceProvider serviceProvider)
{
_concurrentQueue = new ConcurrentQueue<RawPacket>();
_logger = logger;
_serviceProvider = serviceProvider;
_activitySource = new ActivitySource(nameof(Server));
}
public Task StartAsync(CancellationToken cancellationToken)
{
var tempDeserializationMap =
new Dictionary<OperationCode, Func<byte[], IPacket>>();
var wonderkingAssembly = Assembly.GetAssembly(typeof(IPacket));
var packetsTypes = GetPacketsWithId(wonderkingAssembly);
var packetHandlers = GetAllPacketHandlersWithId(Assembly.GetExecutingAssembly());
_packetHandlersInstantiation = new ConcurrentDictionary<OperationCode, IPacketHandler>();
packetHandlers.ForEach(x =>
{
var packetHandler =
ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider,
x.Value);
_packetHandlersInstantiation.TryAdd(x.Key, packetHandler as IPacketHandler);
});
foreach (var packetsType in packetsTypes)
{
var lambda = Lambda<Func<byte[], IPacket>>(fun =>
{
var argPacketData = fun[0];
var newPacket = packetsType.Value.New();
var packetVariable = DeclareVariable(packetsType.Value, "packet");
Assign(packetVariable, newPacket);
Call(packetVariable, nameof(IPacket.Deserialize), argPacketData);
Return(packetVariable);
}).Compile();
_logger.PacketCreationFunctionCreated(packetsType.Key);
tempDeserializationMap.Add(packetsType.Key, lambda);
}
_deserializationMap = tempDeserializationMap.ToImmutableDictionary();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
private Dictionary<OperationCode, Type> GetPacketsWithId(Assembly executingAssembly)
{
// ! : We are filtering if types that don't have an instance of the required Attribute
var packetsWithId = executingAssembly.GetTypes().AsParallel()
.Where(type => type.HasInterface(typeof(IPacket)) && type is { IsInterface: false, IsAbstract: false })
.Where(type => type.Namespace?.Contains("Incoming") ?? false)
.Select(type => new { Type = type, Attribute = type.GetCustomAttribute<PacketIdAttribute>() })
.Where(item => item.Attribute is not null)
.ToDictionary(item => item.Attribute!.Code, item => item.Type);
if (packetsWithId is not { Count: 0 })
{
packetsWithId.AsParallel()
.ForAll(packet => _logger.PacketWithIdAdded(packet.Key, packet.Value.FullName));
return packetsWithId;
}
_logger.NoPacketsFound();
throw new IncompleteInitialization();
}
private Dictionary<OperationCode, Type> GetAllPacketHandlersWithId(Assembly assembly)
{
// ! : We are filtering if types that don't have an instance of the required Attribute
var packetHandlersWithId = assembly.GetTypes().AsParallel()
.Where(t =>
t is { IsClass: true, IsAbstract: false } && Array.Exists(t
.GetInterfaces(), i =>
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<>)))
.Select(type => new
{
Type = type,
PacketId = type
.GetInterfaces().First(t1 =>
t1 is { IsGenericType: true } && t1.GetGenericTypeDefinition() == typeof(IPacketHandler<>))
.GetGenericArguments()[0].GetCustomAttribute<PacketIdAttribute>()?.Code
})
.Where(x => x.PacketId is not null)
.ToDictionary(
x => x.PacketId!.Value, x => x.Type
);
if (packetHandlersWithId is not { Count: 0 })
{
packetHandlersWithId.AsParallel().ForAll(packetHandler =>
_logger.PacketHandlerWithIdAdded(packetHandler.Key, packetHandler.Value.FullName));
return packetHandlersWithId;
}
_logger.NoPacketHandlersFound();
throw new IncompleteInitialization();
}
public void AddPacket(RawPacket rawPacket)
{
_concurrentQueue.Enqueue(rawPacket);
DequeueRawPacket();
_logger.PacketReceived(rawPacket.OperationCode);
}
private void DequeueRawPacket()
{
if (_concurrentQueue.TryDequeue(out var item))
{
ThreadPool.QueueUserWorkItem(InvokePacketHandler, item, preferLocal: false);
}
}
private void InvokePacketHandler(RawPacket item)
{
IPacket packet;
_logger.PacketDequeued(item.Session.Id, item.OperationCode);
if (!_deserializationMap.TryGetValue(item.OperationCode, out var value))
{
_logger.PacketTypeNotFound(item.OperationCode);
return;
}
using (var packetParsingActivity = _activitySource.StartActivity("PacketParsing"))
{
packetParsingActivity?.SetTag("PacketId", item.OperationCode);
packet = value(item.MessageBody);
}
using (var packetHandlerActivity = _activitySource.StartActivity("PacketHandler"))
{
packetHandlerActivity?.SetTag("PacketId", item.OperationCode);
_ = _packetHandlersInstantiation[item.OperationCode].TryHandleAsync(packet, item.Session);
}
_logger.PacketData(JsonConvert.SerializeObject(packet));
_logger.PacketFinished(item.Session.Id, item.OperationCode);
}
public void Dispose()
{
GC.SuppressFinalize(this);
_activitySource.Dispose();
}
}

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Net;
using System.Net.Sockets;
@ -9,74 +9,62 @@ using NetCoreServer;
namespace Continuity.AuthServer.Services;
public class WonderkingAuthServer : TcpServer, IHostedService
{
public class WonderkingAuthServer : TcpServer, IHostedService {
private readonly ILogger<WonderkingAuthServer> _logger;
private readonly IServiceProvider _serviceProvider;
public WonderkingAuthServer(IPAddress address, int port, ILogger<WonderkingAuthServer> logger,
IServiceProvider serviceProvider) : base(address, port)
{
IServiceProvider serviceProvider) : base(address, port) {
_logger = logger;
_serviceProvider = serviceProvider;
}
public Task StartAsync(CancellationToken cancellationToken)
{
public Task StartAsync(CancellationToken cancellationToken) {
Start();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
public Task StopAsync(CancellationToken cancellationToken) {
DisconnectAll();
Stop();
return Task.CompletedTask;
}
protected override TcpSession CreateSession()
{
protected override TcpSession CreateSession() {
return ActivatorUtilities.CreateInstance<AuthSession>(_serviceProvider, this);
}
protected override void OnStarting()
{
protected override void OnStarting() {
_logger.LogInformation("Starting");
base.OnStarting();
}
protected override void OnStarted()
{
protected override void OnStarted() {
_logger.LogInformation("Started");
base.OnStarted();
}
protected override void OnStopping()
{
protected override void OnStopping() {
_logger.LogInformation("Stopping");
base.OnStopping();
}
protected override void OnStopped()
{
protected override void OnStopped() {
_logger.LogInformation("Stopped");
base.OnStopped();
}
protected override void OnConnected(TcpSession session)
{
protected override void OnConnected(TcpSession session) {
_logger.LogInformation("Client connected {Session}", session.Id);
base.OnConnected(session);
}
protected override void OnDisconnected(TcpSession session)
{
protected override void OnDisconnected(TcpSession session) {
_logger.LogInformation("Client disconnected {Session}", session.Id);
base.OnDisconnected(session);
}
protected override void OnError(SocketError error)
{
protected override void OnError(SocketError error) {
_logger.LogError("An error has occured {Error}", error);
}
}

View file

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="IDisposableAnalyzers" Version="4.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Wonderking\Wonderking.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,21 @@
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Runtime.InteropServices;
using System.Text.Json;
Span<byte> buffer = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
Console.WriteLine(buffer.Length == Marshal.SizeOf<TestStruct>());
var tstruct = MemoryMarshal.Cast<byte, TestStruct>(buffer)[0];
Console.WriteLine(JsonSerializer.Serialize(tstruct));
Console.WriteLine(tstruct.b);
[StructLayout(LayoutKind.Explicit, Size = 20)]
public struct TestStruct {
[FieldOffset(0)]
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I4, SizeConst = 4)]
public int[] arr;
[FieldOffset(16)]
[MarshalAs(UnmanagedType.I4)]
public int b;
}

View file

@ -1,28 +0,0 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Continuity.AuthServer", "Continuity.AuthServer\Continuity.AuthServer.csproj", "{7EDA8B31-3E03-4CA3-87D1-CFEB05C277D6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmarks", "Benchmarks\Benchmarks.csproj", "{7D560FA1-A61C-4B67-8300-835CA5814621}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wonderking", "Wonderking\Wonderking.csproj", "{6B53A10B-C397-4347-BB00-A12272D0528E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7EDA8B31-3E03-4CA3-87D1-CFEB05C277D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7EDA8B31-3E03-4CA3-87D1-CFEB05C277D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EDA8B31-3E03-4CA3-87D1-CFEB05C277D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EDA8B31-3E03-4CA3-87D1-CFEB05C277D6}.Release|Any CPU.Build.0 = Release|Any CPU
{7D560FA1-A61C-4B67-8300-835CA5814621}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D560FA1-A61C-4B67-8300-835CA5814621}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D560FA1-A61C-4B67-8300-835CA5814621}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D560FA1-A61C-4B67-8300-835CA5814621}.Release|Any CPU.Build.0 = Release|Any CPU
{6B53A10B-C397-4347-BB00-A12272D0528E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B53A10B-C397-4347-BB00-A12272D0528E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B53A10B-C397-4347-BB00-A12272D0528E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B53A10B-C397-4347-BB00-A12272D0528E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

6
Continuity.slnx Normal file
View file

@ -0,0 +1,6 @@
<Solution>
<Project Path="Benchmarks/Benchmarks.csproj" />
<Project Path="Continuity.AuthServer/Continuity.AuthServer.csproj" />
<Project Path="Continuity.WorldServer/Continuity.WorldServer.csproj" />
<Project Path="Wonderking/Wonderking.csproj" />
</Solution>

View file

@ -4,3 +4,11 @@ This is a continuation and rewrite of the original Project Infinity for Wonderki
## Important notes for developers
* Avoid using statements with TcpSession, AuthSession or any type that inherits Session outside the session itself.
# Scripts example usage
**split_dat_file.ps1**
``.\scripts\split_dat_file.ps1 .\wk-data\baseitemdata_clean.dat 932 9``
**dexor_binary_file.ps1**
``.\scripts\dexor_binary_file.ps1 .\wk-data\baseitemdata.dat .\wk-data\baseitemdata_clean.dat 0xc5``

View file

@ -1,9 +1,8 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
namespace Wonderking.Game.Data.Character;
public enum Gender : byte
{
public enum Gender : byte {
None = 0,
Male = 1,
Female = 2

View file

@ -1,9 +1,8 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
namespace Wonderking.Game.Data.Character;
public enum PvPLevel : byte
{
public enum PvPLevel : byte {
None = 0,
Dualer = 1,
Challenger = 2,

View file

@ -1,12 +1,11 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Runtime.InteropServices;
namespace Wonderking.Game.Data.Item;
[StructLayout(LayoutKind.Sequential)]
public struct ContainedItem
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ContainedItem {
public short ID { get; internal set; }
public float ObtainChance { get; internal set; }
}

View file

@ -1,12 +1,11 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Runtime.InteropServices;
namespace Wonderking.Game.Data.Item;
[StructLayout(LayoutKind.Sequential)]
public struct CraftMaterial
{
public struct CraftMaterial {
public uint ID;
public uint Amount;
}

View file

@ -1,12 +1,11 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Runtime.InteropServices;
namespace Wonderking.Game.Data.Item;
[StructLayout(LayoutKind.Explicit, Size = 64)]
public struct ElementalStats
{
public struct ElementalStats {
[FieldOffset(0)]
[MarshalAs(UnmanagedType.I4)]
public int MinimumFireDamage;

View file

@ -1,9 +1,13 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Runtime.InteropServices;
namespace Wonderking.Game.Data.Item;
public struct ItemOptions
{
public uint[] OptionIDs { get; internal set; }
public bool OptionAvailable { get; internal set; }
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ItemOptions {
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.U4)]
public uint[] OptionIDs;
public bool OptionAvailable;
}

View file

@ -1,12 +1,11 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Runtime.InteropServices;
namespace Wonderking.Game.Data.Item;
[StructLayout(LayoutKind.Explicit, Size = 24)]
public struct Stats
{
public struct Stats {
[FieldOffset(0)]
[MarshalAs(UnmanagedType.I4)]
public int Strength;

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text.Json.Serialization;
using Wonderking.Game.Data.Item;
@ -7,8 +7,7 @@ using Wonderking.Utils;
namespace Wonderking.Game.Data;
[GameDataMetadata(932, "baseitemdata.dat", 197)]
public struct ItemObject
{
public struct ItemObject {
public uint ItemID { get; set; }
public bool Disabled { get; set; }
public uint ItemType { get; set; }
@ -128,10 +127,10 @@ public struct ItemObject
[JsonConverter(typeof(ByteArrayConverter))]
public byte[] R16C { get; set; }
public int InventoryX { get; set; }
public int InventoryY { get; set; }
public int InventoryWidth { get; set; }
public int InventoryHeight { get; set; }
public int SheetX { get; set; }
public int SheetY { get; set; }
public int SheetWidth { get; set; }
public int SheetHeight { get; set; }
public int SheetID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
@ -169,4 +168,7 @@ public struct ItemObject
[JsonConverter(typeof(ByteArrayConverter))]
public byte[] Unknown21_2 { get; set; }
public int RearWearItemIndex { get; set; }
public int FrontWearItemIndex { get; set; }
}

View file

@ -1,18 +1,16 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Reflection;
namespace Wonderking.Game;
public abstract class DataReader<T>
{
public abstract class DataReader<T> {
private readonly string _datFileName;
private readonly byte _xorKey;
protected readonly ushort SizeOfEntry;
protected DataReader(string path)
{
protected DataReader(string path) {
Path = path;
_xorKey = GetXorKey();
SizeOfEntry = GetSizeOfEntry();
@ -27,31 +25,26 @@ public abstract class DataReader<T>
public abstract uint GetAmountOfEntries();
public abstract T GetEntry(uint entryId);
private static ushort GetSizeOfEntry()
{
private static ushort GetSizeOfEntry() {
return typeof(T).GetCustomAttribute<GameDataMetadataAttribute>()?.DataEntrySize ??
throw new NotSupportedException("DataEntrySize is null");
}
private static string GetDatFileName()
{
private static string GetDatFileName() {
return typeof(T).GetCustomAttribute<GameDataMetadataAttribute>()?.DatFileName ??
throw new NotSupportedException("DatFileName is null");
}
private static byte GetXorKey()
{
private static byte GetXorKey() {
return typeof(T).GetCustomAttribute<GameDataMetadataAttribute>()?.XorKey ??
throw new NotSupportedException("XorKey is null");
}
private Span<byte> GetDatFileContent(string path)
{
private ReadOnlySpan<byte> GetDatFileContent(string path) {
var fileData = File.ReadAllBytes(path + _datFileName);
var data = new byte[fileData.Length];
for (var i = 0; i < fileData.Length; i++)
{
for (var i = 0; i < fileData.Length; i++) {
data[i] = (byte)(fileData[i] ^ _xorKey);
}

View file

@ -1,12 +1,11 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using JetBrains.Annotations;
namespace Wonderking.Game;
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class)]
public class GameDataMetadataAttribute(ushort dataEntrySize, string datFileName, byte xorKey) : Attribute
{
public class GameDataMetadataAttribute(ushort dataEntrySize, string datFileName, byte xorKey) : Attribute {
[UsedImplicitly] public byte XorKey { get; init; } = xorKey;
[UsedImplicitly] public ushort DataEntrySize { get; init; } = dataEntrySize;

View file

@ -1,11 +1,10 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text.Json.Serialization;
namespace Wonderking.Game.Mapping;
public class CharacterStatsMappingConfiguration
{
public class CharacterStatsMappingConfiguration {
[JsonPropertyName("default")] public required DefaultCharacterMapping DefaultCharacterMapping { get; set; }
[JsonPropertyName("1")] public required JobSpecificMapping Swordsman { get; set; }

View file

@ -1,10 +1,9 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text.Json.Serialization;
namespace Wonderking.Game.Mapping;
public class DefaultCharacterMapping
{
public class DefaultCharacterMapping {
[JsonPropertyName("items")] public required ICollection<Item> Items { get; set; }
}

View file

@ -1,11 +1,10 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text.Json.Serialization;
namespace Wonderking.Game.Mapping;
public class DynamicStats
{
public class DynamicStats {
[JsonPropertyName("healthPerLevel")] public int HealthPerLevel { get; set; }
[JsonPropertyName("manaPerLevel")] public int ManaPerLevel { get; set; }

View file

@ -1,11 +1,10 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text.Json.Serialization;
namespace Wonderking.Game.Mapping;
public class Item
{
public class Item {
[JsonPropertyName("id")] public ushort Id { get; set; }
[JsonPropertyName("quantity")] public ushort Quantity { get; set; }

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text.Json.Serialization;
using JetBrains.Annotations;
@ -7,8 +7,7 @@ using Wonderking.Packets.Outgoing.Data;
namespace Wonderking.Game.Mapping;
[UsedImplicitly]
public class JobSpecificMapping
{
public class JobSpecificMapping {
[JsonPropertyName("items")] public required ICollection<Item> Items { get; set; }
[JsonPropertyName("baseStats")] public required BaseStats BaseStats { get; set; }

View file

@ -1,75 +1,61 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Runtime.InteropServices;
namespace Wonderking.Game.Reader;
public static class BinaryReader<T> where T : new()
{
public static class BinaryReader<T> where T : new() {
public static readonly Func<BinaryReader, T> Read;
#pragma warning disable MA0051, CA1810
static BinaryReader()
#pragma warning restore MA0051, CA1810
{
var type = typeof(T);
if (type == typeof(bool))
{
if (type == typeof(bool)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, bool>)(p => p.ReadBoolean());
}
else if (type == typeof(char))
{
else if (type == typeof(char)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, char>)(p => p.ReadChar());
}
else if (type == typeof(string))
{
else if (type == typeof(string)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, string>)(p => p.ReadString());
}
else if (type == typeof(sbyte))
{
else if (type == typeof(sbyte)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, sbyte>)(p => p.ReadSByte());
}
else if (type == typeof(short))
{
else if (type == typeof(short)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, short>)(p => p.ReadInt16());
}
else if (type == typeof(int))
{
else if (type == typeof(int)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, int>)(p => p.ReadInt32());
}
else if (type == typeof(long))
{
else if (type == typeof(long)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, long>)(p => p.ReadInt64());
}
else if (type == typeof(byte))
{
else if (type == typeof(byte)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, byte>)(p => p.ReadByte());
}
else if (type == typeof(ushort))
{
else if (type == typeof(ushort)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, ushort>)(p => p.ReadUInt16());
}
else if (type == typeof(uint))
{
else if (type == typeof(uint)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, uint>)(p => p.ReadUInt32());
}
else if (type == typeof(ulong))
{
else if (type == typeof(ulong)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, ulong>)(p => p.ReadUInt64());
}
else if (type == typeof(float))
{
else if (type == typeof(float)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, float>)(p => p.ReadSingle());
}
else if (type == typeof(double))
{
else if (type == typeof(double)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, double>)(p => p.ReadDouble());
}
else if (type == typeof(decimal))
{
else if (type == typeof(decimal)) {
Read = (Func<BinaryReader, T>)(Delegate)(Func<BinaryReader, decimal>)(p => p.ReadDecimal());
}
else
{
else {
Read = (Func<BinaryReader, T>)(p =>
(T)(object)p.ReadBytes(Marshal.SizeOf(new T())));
}

View file

@ -1,38 +1,25 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
/* Nicht gemergte Änderung aus Projekt "Wonderking(net7.0)"
Vor:
using System.Runtime.InteropServices;
using System.Text;
Nach:
using System.Text;
*/
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text;
namespace Wonderking.Game.Reader;
public static class GenericReaderExtensions
{
public static string ReadString(this BinaryReader reader, int length)
{
var ret = Encoding.ASCII.GetString(reader.ReadBytes(length)).Replace("\0", "");
public static class GenericReaderExtensions {
public static string ReadString(this BinaryReader reader, int length) {
var ret = Encoding.ASCII.GetString(reader.ReadBytes(length)).Replace("\0", "", StringComparison.Ordinal);
return ret;
}
public static T[] ReadArray<T>(this BinaryReader pReader, int pLength) where T : new()
{
public static T[] ReadArray<T>(this BinaryReader pReader, int pLength) where T : new() {
var array = new T[pLength];
for (var index = 0; index < pLength; ++index)
{
for (var index = 0; index < pLength; ++index) {
array[index] = pReader.Read<T>();
}
return array;
}
public static T Read<T>(this BinaryReader br) where T : new()
{
public static T Read<T>(this BinaryReader br) where T : new() {
return BinaryReader<T>.Read(br);
}
}

View file

@ -1,16 +1,16 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using Wonderking.Game.Data;
using Wonderking.Game.Data.Item;
namespace Wonderking.Game.Reader;
public class ItemReader(string path) : DataReader<ItemObject>(path)
{
public override uint GetAmountOfEntries()
{
public class ItemReader(string path) : DataReader<ItemObject>(path) {
public override uint GetAmountOfEntries() {
return (uint)((DatFileContent.Length - 9) / SizeOfEntry);
}
@ -46,34 +46,8 @@ public class ItemReader(string path) : DataReader<ItemObject>(path)
item.Unknown8 = data.Slice(92, 4).ToArray(); // 92 -> 96
item.R2C = data.Slice(96, 16).ToArray(); // 96 -> 112
item.Unknown9 = data.Slice(112, 4).ToArray(); // 112 -> 116
item.Stats = new Stats
{
Strength = BitConverter.ToInt32(data.Slice(116, 4)), // 116 -> 120
Dexterity = BitConverter.ToInt32(data.Slice(120, 4)), // 120 -> 124
Intelligence = BitConverter.ToInt32(data.Slice(124, 4)), // 124 -> 128
Vitality = BitConverter.ToInt32(data.Slice(128, 4)), // 128 -> 132
Luck = BitConverter.ToInt32(data.Slice(132, 4)), // 132 -> 136
Wisdom = BitConverter.ToInt32(data.Slice(136, 4)) // 136 -> 140
}; // 116 -> 140
item.ElementalStats = new ElementalStats
{
MinimumFireDamage = BitConverter.ToInt32(data.Slice(140, 4)), // 140 -> 144
MinimumWaterDamage = BitConverter.ToInt32(data.Slice(144, 4)), // 144 -> 148
MinimumDarkDamage = BitConverter.ToInt32(data.Slice(148, 4)), // 148 -> 152
MinimumHolyDamage = BitConverter.ToInt32(data.Slice(152, 4)), // 152 -> 156
MaximumFireDamage = BitConverter.ToInt32(data.Slice(156, 4)), // 156 -> 160
MaximumWaterDamage = BitConverter.ToInt32(data.Slice(160, 4)), // 160 -> 164
MaximumDarkDamage = BitConverter.ToInt32(data.Slice(164, 4)), // 164 -> 168
MaximumHolyDamage = BitConverter.ToInt32(data.Slice(168, 4)), // 168 -> 172
ElementFire = BitConverter.ToUInt32(data.Slice(172, 4)), // 172 -> 176
ElementWater = BitConverter.ToUInt32(data.Slice(176, 4)), // 176 -> 180
ElementDark = BitConverter.ToUInt32(data.Slice(180, 4)), // 180 -> 184
ElementHoly = BitConverter.ToUInt32(data.Slice(184, 4)), // 184 -> 188
FireResistance = BitConverter.ToInt32(data.Slice(188, 4)), // 188 -> 192
WaterResistance = BitConverter.ToInt32(data.Slice(192, 4)), // 192 -> 196
DarkResistance = BitConverter.ToInt32(data.Slice(196, 4)), // 196 -> 200
HolyResistance = BitConverter.ToInt32(data.Slice(200, 4)) // 200 -> 204
}; // 140 -> 204
item.Stats = ReadStats(ref data); // 116 -> 140
item.ElementalStats = ReadElementalStats(ref data); // 140 -> 204
item.R7C = data.Slice(204, 4).ToArray(); // 204 -> 208
item.R8C = data.Slice(208, 8).ToArray(); // 208 -> 216
item.Speed = BinaryPrimitives.ReadSingleLittleEndian(data.Slice(216, 4)); // 216 -> 220
@ -101,71 +75,80 @@ public class ItemReader(string path) : DataReader<ItemObject>(path)
item.MinimalAttackDamage = BitConverter.ToInt32(data.Slice(308, 4)); // 308 -> 312
item.MaximalAttackDamage = BitConverter.ToInt32(data.Slice(312, 4)); // 312 -> 316
item.PhysicalDamage = BitConverter.ToInt32(data.Slice(316, 4)); // 316 -> 320
item.CraftMaterial = new CraftMaterial[]
{
new()
{
ID = BitConverter.ToUInt32(data.Slice(320, 4)), // 320 -> 324
Amount = BitConverter.ToUInt32(data.Slice(336, 4)) // 336 -> 340
},
new()
{
ID = BitConverter.ToUInt32(data.Slice(324, 4)), // 324 -> 328
Amount = BitConverter.ToUInt32(data.Slice(340, 4)) // 340 -> 344
},
new()
{
ID = BitConverter.ToUInt32(data.Slice(328, 4)), // 328 -> 332
Amount = BitConverter.ToUInt32(data.Slice(344, 4)) // 344 -> 348
},
new()
{
ID = BitConverter.ToUInt32(data.Slice(332, 4)), // 332 -> 336
Amount = BitConverter.ToUInt32(data.Slice(348, 4)) // 348 -> 352
}
}; // 320 -> 352
item.CraftMaterial = ReadCraftMaterial(ref data); // 320 -> 352
item.CraftResultAmount = BitConverter.ToUInt32(data.Slice(352, 4)); // 352 -> 356
item.R14C = data.Slice(356, 4).ToArray(); // 356 -> 360
item.CraftResultItem = BitConverter.ToUInt32(data.Slice(360, 4)); // 360 -> 364
item.R15C = data.Slice(364, 4).ToArray(); // 364 -> 368
item.R16C = data.Slice(368, 20).ToArray(); // 368 -> 388
item.InventoryX = BitConverter.ToInt32(data.Slice(388, 4)); // 388 -> 392
item.InventoryY = BitConverter.ToInt32(data.Slice(392, 4)); // 392 -> 396
item.InventoryWidth = BitConverter.ToInt32(data.Slice(396, 4)); // 396 -> 400
item.InventoryHeight = BitConverter.ToInt32(data.Slice(400, 4)); // 400 -> 404
item.R16C = data.Slice(368, 12).ToArray(); // 368 -> 380
item.FrontWearItemIndex = BitConverter.ToInt32(data.Slice(380, 4));
item.RearWearItemIndex = BitConverter.ToInt32(data.Slice(380, 4));
item.SheetX = BitConverter.ToInt32(data.Slice(388, 4)); // 388 -> 392
item.SheetY = BitConverter.ToInt32(data.Slice(392, 4)); // 392 -> 396
item.SheetWidth = BitConverter.ToInt32(data.Slice(396, 4)); // 396 -> 400
item.SheetHeight = BitConverter.ToInt32(data.Slice(400, 4)); // 400 -> 404
item.SheetID = BitConverter.ToInt32(data.Slice(404, 4)); // 404 -> 408
item.Name = Encoding.ASCII.GetString(data.Slice(408, 20)); // 408 -> 428
item.Description = Encoding.ASCII.GetString(data.Slice(428, 85)); // 428 -> 513
item.Unknown1 = data.Slice(513, 175).ToArray(); // 513 -> 688
item.IsEnchantable = BitConverter.ToBoolean(data.Slice(688, 4)); // 688 -> 672
item.Unknown1_2 = data.Slice(692, 104).ToArray(); // 692 -> 796
item.SetItems = new[]
{
BitConverter.ToUInt32(data.Slice(796, 4)), // 796 -> 800
BitConverter.ToUInt32(data.Slice(800, 4)), // 800 -> 804
BitConverter.ToUInt32(data.Slice(804, 4)), // 804 -> 808
BitConverter.ToUInt32(data.Slice(808, 4)), // 808 -> 812
BitConverter.ToUInt32(data.Slice(812, 4)) // 812 -> 816
}; // 796 -> 816
item.SetItems = ReadSetItems(ref data); // 796 -> 816
item.SetID = BitConverter.ToUInt32(data.Slice(816, 4)); // 816 -> 820
item.Options = new ItemOptions
{
OptionIDs = new[]
{
BitConverter.ToUInt32(data.Slice(824, 4)), // 824 -> 828
BitConverter.ToUInt32(data.Slice(828, 4)), // 828 -> 832
BitConverter.ToUInt32(data.Slice(832, 4)), // 832 -> 836
BitConverter.ToUInt32(data.Slice(836, 4)) // 836 -> 840
},
OptionAvailable = BitConverter.ToBoolean(data.Slice(820, 4)) // 820 -> 824
}; // 820 -> 840
item.Options = ReadItemOptions(ref data); // 820 -> 840
item.Unknown19 = data.Slice(840, 23).ToArray(); // 840 -> 863
item.PetID = data[863]; // 863 -> 864
item.Unknown20 = data.Slice(864, 20).ToArray(); // 864 -> 884
item.HitBoxScaling = data[884]; // 884 -> 885
item.Unknown20_2 = data.Slice(885, 13).ToArray(); // 885 -> 898
item.ContainedItems = new[]
{
item.ContainedItems = ReadContainedItems(ref data); // 898 -> 928
item.MinimumLevelRequirement = data[928]; // 928 -> 929
item.Unknown21_2 = data.Slice(929, 3).ToArray(); // 929 -> 932
return item;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Stats ReadStats(ref Span<byte> data) {
return new Stats {
Strength = BitConverter.ToInt32(data.Slice(116, 4)), // 116 -> 120
Dexterity = BitConverter.ToInt32(data.Slice(120, 4)), // 120 -> 124
Intelligence = BitConverter.ToInt32(data.Slice(124, 4)), // 124 -> 128
Vitality = BitConverter.ToInt32(data.Slice(128, 4)), // 128 -> 132
Luck = BitConverter.ToInt32(data.Slice(132, 4)), // 132 -> 136
Wisdom = BitConverter.ToInt32(data.Slice(136, 4)) // 136 -> 140
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint[] ReadSetItems(ref Span<byte> data) {
return
[
BitConverter.ToUInt32(data.Slice(796, 4)), // 796 -> 800
BitConverter.ToUInt32(data.Slice(800, 4)), // 800 -> 804
BitConverter.ToUInt32(data.Slice(804, 4)), // 804 -> 808
BitConverter.ToUInt32(data.Slice(808, 4)), // 808 -> 812
BitConverter.ToUInt32(data.Slice(812, 4)) // 812 -> 816
];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ItemOptions ReadItemOptions(ref Span<byte> data) {
return new ItemOptions {
OptionAvailable = BitConverter.ToBoolean(data.Slice(820, 4)), // 820 -> 824
OptionIDs =
[
BitConverter.ToUInt32(data.Slice(824, 4)), // 824 -> 828
BitConverter.ToUInt32(data.Slice(828, 4)), // 828 -> 832
BitConverter.ToUInt32(data.Slice(832, 4)), // 832 -> 836
BitConverter.ToUInt32(data.Slice(836, 4)) // 836 -> 840
]
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ContainedItem[] ReadContainedItems(ref Span<byte> data) {
return
[
new ContainedItem
{
ID = BitConverter.ToInt16(data.Slice(898, 2)), // 898 -> 900
@ -191,9 +174,38 @@ public class ItemReader(string path) : DataReader<ItemObject>(path)
ID = BitConverter.ToInt16(data.Slice(906, 2)), // 906 -> 908
ObtainChance = BitConverter.ToSingle(data.Slice(924, 4)) // 924 -> 928
}
};
item.MinimumLevelRequirement = data[928]; // 928 -> 929
item.Unknown21_2 = data.Slice(929, 3).ToArray(); // 929 -> 932
return item;
];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static CraftMaterial[] ReadCraftMaterial(ref Span<byte> data) {
return
[
new CraftMaterial
{
ID = BitConverter.ToUInt32(data.Slice(320, 4)), // 320 -> 324
Amount = BitConverter.ToUInt32(data.Slice(336, 4)) // 336 -> 340
},
new CraftMaterial
{
ID = BitConverter.ToUInt32(data.Slice(324, 4)), // 324 -> 328
Amount = BitConverter.ToUInt32(data.Slice(340, 4)) // 340 -> 344
},
new CraftMaterial
{
ID = BitConverter.ToUInt32(data.Slice(328, 4)), // 328 -> 332
Amount = BitConverter.ToUInt32(data.Slice(344, 4)) // 344 -> 348
},
new CraftMaterial
{
ID = BitConverter.ToUInt32(data.Slice(332, 4)), // 332 -> 336
Amount = BitConverter.ToUInt32(data.Slice(348, 4)) // 348 -> 352
}
];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ElementalStats ReadElementalStats(ref Span<byte> data) {
return MemoryMarshal.Cast<byte, ElementalStats>(data.Slice(140, 64))[0];
}
}

View file

@ -1,15 +1,12 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Wonderking.Game.Data.Item;
namespace Wonderking.Game.Reader;
public static class ItemReaderExtensions
{
public static Stats ReadStats(this BinaryReader reader)
{
return new Stats
{
public static class ItemReaderExtensions {
public static Stats ReadStats(this BinaryReader reader) {
return new Stats {
Strength = reader.ReadInt32(), //125
Dexterity = reader.ReadInt32(), //129
Intelligence = reader.ReadInt32(), //133
@ -19,10 +16,8 @@ public static class ItemReaderExtensions
};
}
public static ElementalStats ReadElementalStats(this BinaryReader reader)
{
return new ElementalStats
{
public static ElementalStats ReadElementalStats(this BinaryReader reader) {
return new ElementalStats {
MinimumFireDamage = reader.ReadInt32(), //149
MinimumWaterDamage = reader.ReadInt32(), //153
MinimumDarkDamage = reader.ReadInt32(), //157
@ -42,52 +37,44 @@ public static class ItemReaderExtensions
};
}
public static ContainedItem[] ReadContainedItems(this BinaryReader reader)
{
public static ContainedItem[] ReadContainedItems(this BinaryReader reader) {
var list = new ContainedItem[5];
//893
for (var i = 0; i < 5; i++)
{
for (var i = 0; i < 5; i++) {
list[i].ID = reader.ReadInt16();
}
//903
for (var i = 0; i < 5; i++)
{
for (var i = 0; i < 5; i++) {
list[i].ObtainChance = reader.ReadSingle();
}
return list;
}
public static CraftMaterial[] ReadCraftMaterial(this BinaryReader reader)
{
public static CraftMaterial[] ReadCraftMaterial(this BinaryReader reader) {
var mats = new CraftMaterial[4];
//329
for (var i = 0; i < 4; ++i)
{
for (var i = 0; i < 4; ++i) {
mats[i].ID = reader.ReadUInt32();
}
//345
for (var i = 0; i < 4; ++i)
{
for (var i = 0; i < 4; ++i) {
mats[i].ID = reader.ReadUInt32();
}
return mats;
}
public static ItemOptions ReadItemOptions(this BinaryReader reader)
{
public static ItemOptions ReadItemOptions(this BinaryReader reader) {
var options = new ItemOptions();
options.OptionAvailable = reader.ReadInt32() == 1; //819
var optionIDs = new List<uint>(4);
//823
for (var i = 0; i < 3; i++)
{
for (var i = 0; i < 3; i++) {
optionIDs.Add(reader.ReadUInt32());
}

View file

@ -1,12 +0,0 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
using JetBrains.Annotations;
namespace Wonderking.Packets;
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
public interface IPacket
{
public void Deserialize(byte[] data);
public byte[] Serialize();
}

View file

@ -1,21 +1,16 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using RaiNote.PacketMediator;
namespace Wonderking.Packets.Incoming;
[PacketId(OperationCode.ChannelSelection)]
public class ChannelSelectionPacket : IPacket
{
[WonderkingPacketId(OperationCode.ChannelSelection)]
public class ChannelSelectionPacket : IIncomingPacket {
public required ushort ServerId { get; set; }
public required ushort ChannelId { get; set; }
public void Deserialize(byte[] data)
{
public void Deserialize(byte[] data) {
ServerId = BitConverter.ToUInt16(data, 0);
ChannelId = BitConverter.ToUInt16(data, 2);
}
public byte[] Serialize()
{
throw new NotSupportedException();
}
}

View file

@ -1,13 +1,13 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text;
using RaiNote.PacketMediator;
using Wonderking.Game.Data.Character;
namespace Wonderking.Packets.Incoming;
[PacketId(OperationCode.CharacterCreation)]
public class CharacterCreationPacket : IPacket
{
[WonderkingPacketId(OperationCode.CharacterCreation)]
public class CharacterCreationPacket : IIncomingPacket {
public required byte Slot { get; set; }
public required byte Unknown { get; set; }
public required ushort Id { get; set; }
@ -20,8 +20,7 @@ public class CharacterCreationPacket : IPacket
public required byte Shirt { get; set; }
public required byte Pants { get; set; }
public void Deserialize(byte[] data)
{
public void Deserialize(byte[] data) {
Slot = data[0];
Unknown = data[1];
Id = BitConverter.ToUInt16(data, 2);
@ -33,9 +32,4 @@ public class CharacterCreationPacket : IPacket
Shirt = data[28];
Pants = data[29];
}
public byte[] Serialize()
{
throw new NotSupportedException();
}
}

View file

@ -1,26 +1,20 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text;
using RaiNote.PacketMediator;
namespace Wonderking.Packets.Incoming;
[PacketId(OperationCode.CharacterDeletion)]
public class CharacterDeletePacket : IPacket
{
public byte Slot { get; set; }
public string Name { get; set; }
public uint Unknown { get; set; }
[WonderkingPacketId(OperationCode.CharacterDeletion)]
public class CharacterDeletePacket : IIncomingPacket {
public required byte Slot { get; set; }
public required string Name { get; set; }
public required uint Unknown { get; set; }
public void Deserialize(byte[] data)
{
public void Deserialize(byte[] data) {
Span<byte> span = data;
Slot = span[0];
Name = Encoding.ASCII.GetString(span.Slice(1, 20)).TrimEnd('\0').TrimEnd('\n').TrimEnd('\0');
Unknown = BitConverter.ToUInt32(span.Slice(21, 4));
}
public byte[] Serialize()
{
throw new NotSupportedException();
}
}

View file

@ -1,21 +1,15 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text;
using RaiNote.PacketMediator;
namespace Wonderking.Packets.Incoming;
[PacketId(OperationCode.CharacterNameCheck)]
public class CharacterNameCheckPacket : IPacket
{
[WonderkingPacketId(OperationCode.CharacterNameCheck)]
public class CharacterNameCheckPacket : IIncomingPacket {
public required string Name { get; set; }
public void Deserialize(byte[] data)
{
public void Deserialize(byte[] data) {
Name = Encoding.ASCII.GetString(data, 0, 20).TrimEnd('\0').TrimEnd('\n').TrimEnd('\0');
}
public byte[] Serialize()
{
throw new NotSupportedException();
}
}

View file

@ -1,38 +1,19 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text;
using RaiNote.PacketMediator;
namespace Wonderking.Packets.Incoming;
[PacketId(OperationCode.LoginInfo)]
public class LoginInfoPacket : IPacket
{
[WonderkingPacketId(OperationCode.LoginInfo)]
public class LoginInfoPacket : IIncomingPacket {
public required string Username { get; set; }
public required string Password { get; set; }
public void Deserialize(byte[] data)
{
public void Deserialize(byte[] data) {
Username = Encoding.ASCII.GetString(data, 0, 20).TrimEnd('\0').TrimEnd('\n').TrimEnd('\0');
// Remove unnecessary Symbols
Password = Encoding.ASCII.GetString(data, 20, 31).TrimEnd('\0').TrimEnd('\n').TrimEnd('\0');
}
public byte[] Serialize()
{
Span<byte> dataSpan = stackalloc byte[20 + 31];
var usernameBytes = Encoding.ASCII.GetBytes(Username);
var passwordBytes = Encoding.ASCII.GetBytes(Password);
for (var i = 0; i < 20 || i < Username.Length; i++)
{
dataSpan[i] = usernameBytes[i];
}
for (var i = 0; i < 31 || i < Password.Length; i++)
{
dataSpan[20 + i] = passwordBytes[i];
}
return dataSpan.ToArray();
}
}

View file

@ -1,9 +1,8 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
namespace Wonderking.Packets;
public enum OperationCode : ushort
{
public enum OperationCode : ushort {
LoginInfo = 11,
LoginResponse = 12,
ChannelSelection = 13,

View file

@ -1,26 +1,20 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Buffers.Binary;
using System.Text;
using RaiNote.PacketMediator;
using Wonderking.Packets.Outgoing.Data;
namespace Wonderking.Packets.Outgoing;
[PacketId(OperationCode.ChannelSelectionResponse)]
public class ChannelSelectionResponsePacket : IPacket
{
[WonderkingPacketId(OperationCode.ChannelSelectionResponse)]
public class ChannelSelectionResponsePacket : IOutgoingPacket {
public required byte ChannelIsFullFlag { get; set; }
public required string Endpoint { get; set; }
public required ushort Port { get; set; }
public required CharacterData[] Characters { get; set; }
public void Deserialize(byte[] data)
{
throw new NotSupportedException();
}
public byte[] Serialize()
{
public byte[] Serialize() {
Span<byte> data = stackalloc byte[1 + 16 + 2 + 1 + 132 * Characters.Length];
data.Clear();
data[0] = ChannelIsFullFlag;
@ -29,8 +23,7 @@ public class ChannelSelectionResponsePacket : IPacket
data[19] = (byte)Characters.Length;
// Character Data
for (var i = 0; i < Characters.Length; i++)
{
for (var i = 0; i < Characters.Length; i++) {
var offset = 20 + i * 132;
var character = Characters[i];
// Character Data
@ -58,8 +51,7 @@ public class ChannelSelectionResponsePacket : IPacket
BinaryPrimitives.WriteInt32LittleEndian(data.Slice(offset + 44, 4), character.Health);
BinaryPrimitives.WriteInt32LittleEndian(data.Slice(offset + 48, 4), character.Mana);
for (var j = 0; j < 20; j++)
{
for (var j = 0; j < 20; j++) {
// Equipped Items
BinaryPrimitives.WriteUInt16LittleEndian(data.Slice(offset + 52 + j * 2, 2),
character.EquippedItems.Length > j ? character.EquippedItems[j] : (ushort)0);

View file

@ -1,25 +1,19 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Buffers.Binary;
using System.Text;
using RaiNote.PacketMediator;
using Wonderking.Packets.Outgoing.Data;
namespace Wonderking.Packets.Outgoing;
[PacketId(OperationCode.CharacterCreationResponse)]
public class CharacterCreationResponsePacket : IPacket
{
[WonderkingPacketId(OperationCode.CharacterCreationResponse)]
public class CharacterCreationResponsePacket : IOutgoingPacket {
public required CharacterData Character { get; set; }
public required int Slot { get; set; }
public required bool isDuplicate { get; set; }
public void Deserialize(byte[] data)
{
throw new NotSupportedException();
}
public byte[] Serialize()
{
public byte[] Serialize() {
Span<byte> data = stackalloc byte[1 + 132];
data[0] = isDuplicate ? (byte)1 : (byte)0;
@ -48,8 +42,7 @@ public class CharacterCreationResponsePacket : IPacket
BinaryPrimitives.WriteInt32LittleEndian(data.Slice(45, 4), Character.Health);
BinaryPrimitives.WriteInt32LittleEndian(data.Slice(49, 4), Character.Mana);
for (var i = 0; i < 20; i++)
{
for (var i = 0; i < 20; i++) {
// Equipped Items
BinaryPrimitives.WriteUInt16LittleEndian(data.Slice(53 + i * 2, 2),
Character.EquippedItems.Length > i ? Character.EquippedItems[i] : (ushort)0);

View file

@ -1,21 +1,14 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using NotSupportedException = System.NotSupportedException;
using RaiNote.PacketMediator;
namespace Wonderking.Packets.Outgoing;
[PacketId(OperationCode.CharacterDeletionResponse)]
public class CharacterDeleteResponsePacket : IPacket
{
[WonderkingPacketId(OperationCode.CharacterDeletionResponse)]
public class CharacterDeleteResponsePacket : IOutgoingPacket {
public required byte HasToBeZero { get; set; }
public void Deserialize(byte[] data)
{
throw new NotSupportedException();
}
public byte[] Serialize()
{
public byte[] Serialize() {
Span<byte> data = stackalloc byte[1];
data[0] = HasToBeZero;
return data.ToArray();

View file

@ -1,19 +1,14 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using RaiNote.PacketMediator;
namespace Wonderking.Packets.Outgoing;
[PacketId(OperationCode.CharacterNameCheckResponse)]
public class CharacterNameCheckPacketResponse : IPacket
{
[WonderkingPacketId(OperationCode.CharacterNameCheckResponse)]
public class CharacterNameCheckPacketResponse : IOutgoingPacket {
public required bool IsTaken { get; set; }
public void Deserialize(byte[] data)
{
throw new NotSupportedException();
}
public byte[] Serialize()
{
public byte[] Serialize() {
Span<byte> data = stackalloc byte[1];
data[0] = IsTaken ? (byte)1 : (byte)0;
return data.ToArray();

View file

@ -1,26 +1,19 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text;
using RaiNote.PacketMediator;
namespace Wonderking.Packets.Outgoing;
[PacketId(OperationCode.CharacterSelectionSetGuildName)]
public class CharacterSelectionSetGuildNamePacket : IPacket
{
[WonderkingPacketId(OperationCode.CharacterSelectionSetGuildName)]
public class CharacterSelectionSetGuildNamePacket : IOutgoingPacket {
public required string[] GuildNames { get; set; }
public void Deserialize(byte[] data)
{
throw new NotSupportedException();
}
public byte[] Serialize()
{
public byte[] Serialize() {
Span<byte> data = stackalloc byte[1 + (1 + 16 + 1) * GuildNames.Length];
data.Clear();
data[0] = (byte)GuildNames.Length;
for (var i = 0; i < GuildNames.Length; i++)
{
for (var i = 0; i < GuildNames.Length; i++) {
data[1 + i * (1 + 16 + 1)] = (byte)i;
Encoding.ASCII.GetBytes(GuildNames[i], data.Slice(2 + i * (1 + 16 + 1), 16));
// Null terminator

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text.Json.Serialization;
using JetBrains.Annotations;
@ -8,8 +8,7 @@ namespace Wonderking.Packets.Outgoing.Data;
[UsedImplicitly]
[Owned]
public class BaseStats
{
public class BaseStats {
[JsonPropertyName("strength")] public required short Strength { get; set; }
[JsonPropertyName("dexterity")] public required short Dexterity { get; set; }
[JsonPropertyName("intelligence")] public required short Intelligence { get; set; }

View file

@ -1,11 +1,10 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using Wonderking.Game.Data.Character;
namespace Wonderking.Packets.Outgoing.Data;
public struct CharacterData
{
public struct CharacterData {
public required string Name { get; set; }
public required JobData Job { get; set; }
public required Gender Gender { get; set; }

View file

@ -1,4 +1,4 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
@ -7,8 +7,7 @@ namespace Wonderking.Packets.Outgoing.Data;
[UsedImplicitly]
[Owned]
public class JobData
{
public class JobData {
public required byte FirstJob { get; set; }
public required byte SecondJob { get; set; }
public required byte ThirdJob { get; set; }

View file

@ -1,9 +1,8 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
namespace Wonderking.Packets.Outgoing.Data;
public enum LoginResponseReason : byte
{
public enum LoginResponseReason : byte {
Ok,
AccountDoesNotExit,
WrongPassword,

View file

@ -1,12 +1,11 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Runtime.InteropServices;
namespace Wonderking.Packets.Outgoing.Data;
[StructLayout(LayoutKind.Auto)]
public struct ServerChannelData
{
public struct ServerChannelData {
public ushort ServerId { get; set; }
public ushort ChannelId { get; set; }
public byte LoadPercentage { get; set; }

View file

@ -1,36 +1,20 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Buffers.Binary;
using RaiNote.PacketMediator;
using Wonderking.Packets.Outgoing.Data;
namespace Wonderking.Packets.Outgoing;
[PacketId(OperationCode.LoginResponse)]
public class LoginResponsePacket : IPacket
{
[WonderkingPacketId(OperationCode.LoginResponse)]
public class LoginResponsePacket : IOutgoingPacket {
public required LoginResponseReason ResponseReason { get; set; }
public required byte UnknownFlag { get; set; } = 1;
public required bool IsGameMaster { get; set; }
public required ServerChannelData[] ChannelData { get; set; }
public void Deserialize(byte[] data)
{
ResponseReason = (LoginResponseReason)data[0];
UnknownFlag = data[1];
IsGameMaster = BitConverter.ToBoolean(data, 2);
var channelAmount = BitConverter.ToUInt16(data, 3);
const int sizeOfServerChannelData = 5;
ChannelData = Enumerable.Repeat(0, channelAmount).Select(i => new ServerChannelData
{
ServerId = BitConverter.ToUInt16(data, 5 + 0 + i * sizeOfServerChannelData),
ChannelId = BitConverter.ToUInt16(data, 5 + 2 + i * sizeOfServerChannelData),
LoadPercentage = data[5 + 4 + i * sizeOfServerChannelData]
}).ToArray();
}
public byte[] Serialize()
{
public byte[] Serialize() {
const int sizeOfServerChannelData = 5;
Span<byte> dataSpan = stackalloc byte[5 + ChannelData.Length * sizeOfServerChannelData];
dataSpan.Clear();
@ -39,8 +23,7 @@ public class LoginResponsePacket : IPacket
dataSpan[2] = BitConverter.GetBytes(IsGameMaster)[0];
BinaryPrimitives.WriteUInt16LittleEndian(dataSpan.Slice(3, 2), (ushort)ChannelData.Length);
for (var i = 0; i < ChannelData.Length; i++)
{
for (var i = 0; i < ChannelData.Length; i++) {
var bytesOfServerId = BitConverter.GetBytes(ChannelData[i].ServerId);
var bytesOfChannelId = BitConverter.GetBytes(ChannelData[i].ChannelId);
dataSpan[5 + 0 + i * sizeOfServerChannelData] = bytesOfServerId[0];

View file

@ -1,14 +0,0 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
namespace Wonderking.Packets;
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class PacketIdAttribute : Attribute
{
public PacketIdAttribute(OperationCode code)
{
Code = code;
}
public OperationCode Code { get; }
}

View file

@ -0,0 +1,11 @@
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using RaiNote.PacketMediator;
namespace Wonderking.Packets;
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class WonderkingPacketIdAttribute : PacketIdAttribute<OperationCode> {
public WonderkingPacketIdAttribute(OperationCode code) : base(code) {
}
}

View file

@ -1,20 +1,17 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Wonderking.Utils;
public class ByteArrayConverter : JsonConverter<byte[]>
{
public class ByteArrayConverter : JsonConverter<byte[]> {
public override byte[] Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
JsonSerializerOptions options) {
var hexData = reader.GetString();
if (hexData != null)
{
if (hexData != null) {
return hexData.Split('-').Select(b => Convert.ToByte(b, 16)).ToArray();
}
@ -24,14 +21,11 @@ public class ByteArrayConverter : JsonConverter<byte[]>
public override void Write(
Utf8JsonWriter writer,
byte[]? value,
JsonSerializerOptions options)
{
if (value == null)
{
JsonSerializerOptions options) {
if (value == null) {
writer.WriteNullValue();
}
else
{
else {
var hexData = BitConverter.ToString(value).Replace("-", string.Empty);
writer.WriteStringValue(hexData);
}

Some files were not shown because too many files have changed in this diff Show more