Compare commits
76 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5937e026eb | |||
| 7c3a2dab89 | |||
| 5dd6aad913 | |||
| 9da397dd8c | |||
| 6bb84daf03 | |||
| f1c5c99417 | |||
| e52e940fda | |||
| e78e8048ff | |||
| 5136f08dab | |||
| f7719e1bca | |||
| 211b24fe2b | |||
| bdf7d4f94a | |||
| 3bec19c2b2 | |||
| 0f2d316523 | |||
| 7cf1353869 | |||
| 6199af3d1e | |||
| e9dc45265a | |||
| b84538b238 | |||
| 7a2196ff50 | |||
| 5634a182e6 | |||
| 0ede864f40 | |||
| 646744c9a1 | |||
| a9e6a3e853 | |||
| c74ce0f00f | |||
| d3ae5a4465 | |||
| 5bf1881f81 | |||
| 6886808132 | |||
| b4cba20d82 | |||
| b39bd8bd0a | |||
| 12b80c546c | |||
| accd442388 | |||
| d115c98b79 | |||
| 5c20fbafc1 | |||
| bd0c6df5ea | |||
| c4811ef052 | |||
| d82964526c | |||
| acedaef270 | |||
| 0c4bdb83c9 | |||
| 5a79502848 | |||
| b8fe2bb13f | |||
| 89d130a193 | |||
| 0cd912a1aa | |||
| c64cc61c78 | |||
| 2dcb047014 | |||
| 33f8bba2ed | |||
| 5aa159ed2f | |||
| 892558ab16 | |||
| f55634877a | |||
| 8b3a29a750 | |||
| 6a50bffe1e | |||
| 9144ad58ef | |||
| ee9f3fbe3a | |||
| 893c73512a | |||
| e7dd693147 | |||
| fef1007c8c | |||
| e33c442a18 | |||
| 09b185a3ad | |||
| 7d154ea4d4 | |||
| d2577e5d1f | |||
| e25f42f9ff | |||
| 28cc41c3ad | |||
| fc483c0854 | |||
| bcc45f7db8 | |||
| 689a240e5b | |||
| 85b75766a9 | |||
| b90ed974e5 | |||
| 3da57e086e | |||
| 745d196a8b | |||
| 860051777e | |||
| 6422db783a | |||
| 5d812d7acc | |||
| c214b6ca7f | |||
| 64842886d7 | |||
| c892c2b67a | |||
| e1a3ea45e2 | |||
| 8788cb0fcf |
@@ -0,0 +1,25 @@
|
|||||||
|
**/.dockerignore
|
||||||
|
**/.env
|
||||||
|
**/.git
|
||||||
|
**/.gitignore
|
||||||
|
**/.project
|
||||||
|
**/.settings
|
||||||
|
**/.toolstarget
|
||||||
|
**/.vs
|
||||||
|
**/.vscode
|
||||||
|
**/.idea
|
||||||
|
**/*.*proj.user
|
||||||
|
**/*.dbmdl
|
||||||
|
**/*.jfm
|
||||||
|
**/azds.yaml
|
||||||
|
**/bin
|
||||||
|
**/charts
|
||||||
|
**/docker-compose*
|
||||||
|
**/Dockerfile*
|
||||||
|
**/node_modules
|
||||||
|
**/npm-debug.log
|
||||||
|
**/obj
|
||||||
|
**/secrets.dev.yaml
|
||||||
|
**/values.dev.yaml
|
||||||
|
LICENSE
|
||||||
|
README.md
|
||||||
@@ -49,7 +49,7 @@ jobs:
|
|||||||
- name: Setup dotnet
|
- name: Setup dotnet
|
||||||
uses: actions/setup-dotnet@v1
|
uses: actions/setup-dotnet@v1
|
||||||
with:
|
with:
|
||||||
dotnet-version: 7.0.x
|
dotnet-version: 9.0.x
|
||||||
- name: Build
|
- name: Build
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
.idea
|
.idea
|
||||||
.vs
|
.vs
|
||||||
|
.vscode
|
||||||
obj
|
obj
|
||||||
bin
|
bin
|
||||||
*.user
|
*.user
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 26 KiB |
@@ -1,5 +1,5 @@
|
|||||||
<p align="center">
|
<p align="center">
|
||||||
<img src="Images/ragon-logo.png" width="200" >
|
<img src="Images/logo.png">
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
## Ragon Server
|
## Ragon Server
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Client.Simulation
|
namespace Ragon.Client.Property
|
||||||
{
|
{
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class RagonBool : RagonProperty
|
public class RagonBool : RagonProperty
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Client.Simulation
|
namespace Ragon.Client.Property
|
||||||
{
|
{
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class RagonInt : RagonProperty
|
public class RagonInt : RagonProperty
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System.Numerics;
|
||||||
|
using Ragon.Client.Compressor;
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client.Property;
|
||||||
|
|
||||||
|
public class RagonQuaternion : RagonProperty
|
||||||
|
{
|
||||||
|
private Quaternion _value;
|
||||||
|
|
||||||
|
public Quaternion Value
|
||||||
|
{
|
||||||
|
get => _value;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_value = value;
|
||||||
|
|
||||||
|
MarkAsChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly FloatCompressor _compressor;
|
||||||
|
|
||||||
|
public RagonQuaternion(bool invokeLocal = false, int priority = 0) : base(priority, invokeLocal)
|
||||||
|
{
|
||||||
|
_compressor = new FloatCompressor(-1.0f, 1f, 0.01f);
|
||||||
|
|
||||||
|
SetFixedSize(_compressor.RequiredBits * 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Serialize(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
var compressedX = _compressor.Compress(_value.X);
|
||||||
|
var compressedY = _compressor.Compress(_value.Y);
|
||||||
|
var compressedZ = _compressor.Compress(_value.Z);
|
||||||
|
var compressedW = _compressor.Compress(_value.W);
|
||||||
|
|
||||||
|
buffer.Write(compressedX, _compressor.RequiredBits);
|
||||||
|
buffer.Write(compressedY, _compressor.RequiredBits);
|
||||||
|
buffer.Write(compressedZ, _compressor.RequiredBits);
|
||||||
|
buffer.Write(compressedW, _compressor.RequiredBits);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Deserialize(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
var compressedX = buffer.Read(_compressor.RequiredBits);
|
||||||
|
var compressedY = buffer.Read(_compressor.RequiredBits);
|
||||||
|
var compressedZ = buffer.Read(_compressor.RequiredBits);
|
||||||
|
var compressedW = buffer.Read(_compressor.RequiredBits);
|
||||||
|
|
||||||
|
var x = _compressor.Decompress(compressedX);
|
||||||
|
var y = _compressor.Decompress(compressedY);
|
||||||
|
var z = _compressor.Decompress(compressedZ);
|
||||||
|
var w = _compressor.Decompress(compressedW);
|
||||||
|
|
||||||
|
_value = new Quaternion(x, y, z, w);
|
||||||
|
|
||||||
|
InvokeChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,7 +18,7 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Client.Simulation
|
namespace Ragon.Client.Property
|
||||||
{
|
{
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class RagonString : RagonProperty
|
public class RagonString : RagonProperty
|
||||||
|
|||||||
@@ -0,0 +1,330 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Numerics;
|
||||||
|
using Ragon.Client.Compressor;
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client.Property
|
||||||
|
{
|
||||||
|
[Serializable]
|
||||||
|
public enum RagonAxis
|
||||||
|
{
|
||||||
|
XYZ,
|
||||||
|
XY,
|
||||||
|
YZ,
|
||||||
|
XZ,
|
||||||
|
X,
|
||||||
|
Y,
|
||||||
|
Z
|
||||||
|
}
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class RagonVector3 : RagonProperty
|
||||||
|
{
|
||||||
|
private Vector3 _value;
|
||||||
|
|
||||||
|
public Vector3 Value
|
||||||
|
{
|
||||||
|
get => _value;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_value = value;
|
||||||
|
|
||||||
|
MarkAsChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private RagonAxis _axis;
|
||||||
|
private FloatCompressor _compressorX;
|
||||||
|
private FloatCompressor _compressorY;
|
||||||
|
private FloatCompressor _compressorZ;
|
||||||
|
|
||||||
|
|
||||||
|
public RagonVector3(
|
||||||
|
RagonAxis axis = RagonAxis.XYZ,
|
||||||
|
bool invokeLocal = true,
|
||||||
|
int priority = 0
|
||||||
|
) : base(priority, invokeLocal)
|
||||||
|
{
|
||||||
|
_axis = axis;
|
||||||
|
|
||||||
|
var defaultCompressor = new FloatCompressor(-1024.0f, 1024f, 0.01f);
|
||||||
|
|
||||||
|
_compressorX = defaultCompressor;
|
||||||
|
_compressorY = defaultCompressor;
|
||||||
|
_compressorZ = defaultCompressor;
|
||||||
|
|
||||||
|
switch (_axis)
|
||||||
|
{
|
||||||
|
case RagonAxis.XYZ:
|
||||||
|
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits + _compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.XY:
|
||||||
|
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.XZ:
|
||||||
|
SetFixedSize(_compressorX.RequiredBits + _compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.YZ:
|
||||||
|
SetFixedSize(_compressorY.RequiredBits + _compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.X:
|
||||||
|
SetFixedSize(_compressorX.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.Y:
|
||||||
|
SetFixedSize(_compressorY.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.Z:
|
||||||
|
SetFixedSize(_compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public RagonVector3(
|
||||||
|
Vector3 initialValue,
|
||||||
|
RagonAxis axis = RagonAxis.XYZ,
|
||||||
|
float min = -1024.0f,
|
||||||
|
float max = 1024.0f,
|
||||||
|
float precision = 0.1f,
|
||||||
|
bool invokeLocal = true,
|
||||||
|
int priority = 0
|
||||||
|
) : base(priority, invokeLocal)
|
||||||
|
{
|
||||||
|
_value = initialValue;
|
||||||
|
_axis = axis;
|
||||||
|
|
||||||
|
var defaultCompressor = new FloatCompressor(min, max, precision);
|
||||||
|
|
||||||
|
_compressorX = defaultCompressor;
|
||||||
|
_compressorY = defaultCompressor;
|
||||||
|
_compressorZ = defaultCompressor;
|
||||||
|
|
||||||
|
switch (_axis)
|
||||||
|
{
|
||||||
|
case RagonAxis.XYZ:
|
||||||
|
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits + _compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.XY:
|
||||||
|
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.XZ:
|
||||||
|
SetFixedSize(_compressorX.RequiredBits + _compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.YZ:
|
||||||
|
SetFixedSize(_compressorY.RequiredBits + _compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.X:
|
||||||
|
SetFixedSize(_compressorX.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.Y:
|
||||||
|
SetFixedSize(_compressorY.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.Z:
|
||||||
|
SetFixedSize(_compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public RagonVector3(
|
||||||
|
RagonAxis axis = RagonAxis.XYZ,
|
||||||
|
FloatCompressor compressorX = null,
|
||||||
|
FloatCompressor compressorY = null,
|
||||||
|
FloatCompressor compressorZ = null,
|
||||||
|
bool invokeLocal = true,
|
||||||
|
int priority = 0
|
||||||
|
) : base(priority, invokeLocal)
|
||||||
|
{
|
||||||
|
_axis = axis;
|
||||||
|
|
||||||
|
var defaultCompressor = new FloatCompressor(-1024.0f, 1024f, 0.01f);
|
||||||
|
|
||||||
|
_compressorX = defaultCompressor;
|
||||||
|
_compressorY = defaultCompressor;
|
||||||
|
_compressorZ = defaultCompressor;
|
||||||
|
|
||||||
|
if (compressorX != null)
|
||||||
|
_compressorX = compressorX;
|
||||||
|
|
||||||
|
if (compressorY != null)
|
||||||
|
_compressorY = compressorY;
|
||||||
|
|
||||||
|
if (compressorZ != null)
|
||||||
|
_compressorZ = compressorZ;
|
||||||
|
|
||||||
|
switch (_axis)
|
||||||
|
{
|
||||||
|
case RagonAxis.XYZ:
|
||||||
|
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits + _compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.XY:
|
||||||
|
SetFixedSize(_compressorX.RequiredBits + _compressorY.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.XZ:
|
||||||
|
SetFixedSize(_compressorX.RequiredBits + _compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.YZ:
|
||||||
|
SetFixedSize(_compressorY.RequiredBits + _compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.X:
|
||||||
|
SetFixedSize(_compressorX.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.Y:
|
||||||
|
SetFixedSize(_compressorY.RequiredBits);
|
||||||
|
break;
|
||||||
|
case RagonAxis.Z:
|
||||||
|
SetFixedSize(_compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Serialize(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
switch (_axis)
|
||||||
|
{
|
||||||
|
case RagonAxis.XYZ:
|
||||||
|
{
|
||||||
|
var compressedX = _compressorX.Compress(_value.X);
|
||||||
|
var compressedY = _compressorY.Compress(_value.Y);
|
||||||
|
var compressedZ = _compressorZ.Compress(_value.Z);
|
||||||
|
|
||||||
|
buffer.Write(compressedX, _compressorX.RequiredBits);
|
||||||
|
buffer.Write(compressedY, _compressorY.RequiredBits);
|
||||||
|
buffer.Write(compressedZ, _compressorZ.RequiredBits);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case RagonAxis.XY:
|
||||||
|
{
|
||||||
|
var compressedX = _compressorX.Compress(_value.X);
|
||||||
|
var compressedY = _compressorY.Compress(_value.Y);
|
||||||
|
|
||||||
|
buffer.Write(compressedX, _compressorX.RequiredBits);
|
||||||
|
buffer.Write(compressedY, _compressorY.RequiredBits);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case RagonAxis.XZ:
|
||||||
|
{
|
||||||
|
var compressedX = _compressorX.Compress(_value.X);
|
||||||
|
var compressedZ = _compressorZ.Compress(_value.Z);
|
||||||
|
|
||||||
|
buffer.Write(compressedX, _compressorX.RequiredBits);
|
||||||
|
buffer.Write(compressedZ, _compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case RagonAxis.YZ:
|
||||||
|
{
|
||||||
|
var compressedY = _compressorY.Compress(_value.Y);
|
||||||
|
var compressedZ = _compressorZ.Compress(_value.Z);
|
||||||
|
|
||||||
|
buffer.Write(compressedY, _compressorY.RequiredBits);
|
||||||
|
buffer.Write(compressedZ, _compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RagonAxis.X:
|
||||||
|
{
|
||||||
|
var compressedX = _compressorX.Compress(_value.X);
|
||||||
|
|
||||||
|
buffer.Write(compressedX, _compressorX.RequiredBits);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RagonAxis.Y:
|
||||||
|
{
|
||||||
|
var compressedY = _compressorY.Compress(_value.Y);
|
||||||
|
|
||||||
|
buffer.Write(compressedY, _compressorY.RequiredBits);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RagonAxis.Z:
|
||||||
|
{
|
||||||
|
var compressedZ = _compressorZ.Compress(_value.Z);
|
||||||
|
|
||||||
|
buffer.Write(compressedZ, _compressorZ.RequiredBits);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Deserialize(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
switch (_axis)
|
||||||
|
{
|
||||||
|
case RagonAxis.XYZ:
|
||||||
|
{
|
||||||
|
var compressedX = buffer.Read(_compressorX.RequiredBits);
|
||||||
|
var compressedY = buffer.Read(_compressorY.RequiredBits);
|
||||||
|
var compressedZ = buffer.Read(_compressorZ.RequiredBits);
|
||||||
|
|
||||||
|
_value.X = _compressorX.Decompress(compressedX);
|
||||||
|
_value.Y = _compressorY.Decompress(compressedY);
|
||||||
|
_value.Z = _compressorZ.Decompress(compressedZ);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RagonAxis.XY:
|
||||||
|
{
|
||||||
|
var compressedX = buffer.Read(_compressorX.RequiredBits);
|
||||||
|
var compressedY = buffer.Read(_compressorY.RequiredBits);
|
||||||
|
|
||||||
|
_value.X = _compressorX.Decompress(compressedX);
|
||||||
|
_value.Z = _compressorY.Decompress(compressedY);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RagonAxis.XZ:
|
||||||
|
{
|
||||||
|
var compressedX = buffer.Read(_compressorX.RequiredBits);
|
||||||
|
var compressedZ = buffer.Read(_compressorZ.RequiredBits);
|
||||||
|
|
||||||
|
_value.X = _compressorX.Decompress(compressedX);
|
||||||
|
_value.Z = _compressorZ.Decompress(compressedZ);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RagonAxis.YZ:
|
||||||
|
{
|
||||||
|
var compressedY = buffer.Read(_compressorY.RequiredBits);
|
||||||
|
var compressedZ = buffer.Read(_compressorZ.RequiredBits);
|
||||||
|
|
||||||
|
_value.Y = _compressorY.Decompress(compressedY);
|
||||||
|
_value.Z = _compressorZ.Decompress(compressedZ);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RagonAxis.X:
|
||||||
|
{
|
||||||
|
var compressedX = buffer.Read(_compressorX.RequiredBits);
|
||||||
|
|
||||||
|
_value.X = _compressorX.Decompress(compressedX);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RagonAxis.Y:
|
||||||
|
{
|
||||||
|
var compressedY = buffer.Read(_compressorY.RequiredBits);
|
||||||
|
|
||||||
|
_value.Y = _compressorY.Decompress(compressedY);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case RagonAxis.Z:
|
||||||
|
{
|
||||||
|
var compressedZ = buffer.Read(_compressorZ.RequiredBits);
|
||||||
|
|
||||||
|
_value.Z = _compressorZ.Decompress(compressedZ);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
InvokeChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<OutputType>Exe</OutputType>
|
|
||||||
<TargetFramework>net7.0</TargetFramework>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\Ragon.Client.Property\Ragon.Client.Property.csproj" />
|
|
||||||
<ProjectReference Include="..\Ragon.Client\Ragon.Client.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="ENet-CSharp" Version="2.4.8" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<None Update="libenet.dylib">
|
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
|
||||||
</None>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,116 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
using Ragon.Protocol;
|
|
||||||
|
|
||||||
namespace Ragon.Client.Simulation;
|
|
||||||
|
|
||||||
public class Game : IRagonListener, IRagonSceneRequestListener
|
|
||||||
{
|
|
||||||
private RagonFloat _health;
|
|
||||||
private RagonInt _points;
|
|
||||||
private RagonString _name;
|
|
||||||
private RagonEntity _entity;
|
|
||||||
private RagonClient _client;
|
|
||||||
|
|
||||||
public Game(RagonClient client)
|
|
||||||
{
|
|
||||||
_client = client;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnConnected(RagonClient client)
|
|
||||||
{
|
|
||||||
RagonLog.Trace("Connected");
|
|
||||||
_client.Session.AuthorizeWithKey("defaultkey", "Player Eduard");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnAuthorizationSuccess(RagonClient client, string playerId, string playerName)
|
|
||||||
{
|
|
||||||
RagonLog.Trace("Authorized");
|
|
||||||
client.Session.CreateOrJoin("Example", 1, 20);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnAuthorizationFailed(RagonClient client, string message)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Authorization failed: {message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnJoined(RagonClient client)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnFailed(RagonClient client, string message)
|
|
||||||
{
|
|
||||||
RagonLog.Trace("Failed to join");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnLeft(RagonClient client)
|
|
||||||
{
|
|
||||||
RagonLog.Trace("Left");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnDisconnected(RagonClient client, RagonDisconnect ragonDisconnect)
|
|
||||||
{
|
|
||||||
RagonLog.Trace("Disconnected");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnPlayerJoined(RagonClient client, RagonPlayer player)
|
|
||||||
{
|
|
||||||
RagonLog.Trace("Player joined");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnPlayerLeft(RagonClient client, RagonPlayer player)
|
|
||||||
{
|
|
||||||
RagonLog.Trace("Player left");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnOwnershipChanged(RagonClient client, RagonPlayer player)
|
|
||||||
{
|
|
||||||
RagonLog.Trace("Owner ship changed");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnSceneLoaded(RagonClient client)
|
|
||||||
{
|
|
||||||
RagonLog.Trace("Joined");
|
|
||||||
|
|
||||||
_health = new RagonFloat(100.0f, false, 0);
|
|
||||||
_health.Changed += () => Console.WriteLine($"[Ragon Property] Health: {_health.Value}");
|
|
||||||
|
|
||||||
_points = new RagonInt(0, -1000, 1000, false, 0);
|
|
||||||
_points.Changed += () => Console.WriteLine($"[Ragon Property] Points: {_points.Value}");
|
|
||||||
|
|
||||||
_name = new RagonString("Edmand 000", false);
|
|
||||||
_name.Changed += () => Console.WriteLine($"[Ragon Property] Name: {_name.Value}");
|
|
||||||
|
|
||||||
_entity = new RagonEntity(12, 0);
|
|
||||||
_entity.State.AddProperty(_health);
|
|
||||||
_entity.State.AddProperty(_points);
|
|
||||||
_entity.State.AddProperty(_name);
|
|
||||||
|
|
||||||
client.Room.CreateEntity(_entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
private float _timer = 0;
|
|
||||||
|
|
||||||
public void Update()
|
|
||||||
{
|
|
||||||
if (_client.Status != RagonStatus.ROOM)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_timer += 1 / 60.0f;
|
|
||||||
if (_timer > 1)
|
|
||||||
{
|
|
||||||
_health.Value += 20.0f;
|
|
||||||
_points.Value += 10;
|
|
||||||
_name.Value = $"Edmand 00{_client.Room.Local.PeerId}";
|
|
||||||
Console.WriteLine($"{_health.Value} {_points.Value} {_name.Value}");
|
|
||||||
_timer = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnRequestScene(RagonClient client, string sceneName)
|
|
||||||
{
|
|
||||||
client.Room.SceneLoaded();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,131 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
using ENet;
|
|
||||||
using Ragon.Protocol;
|
|
||||||
using Event = ENet.Event;
|
|
||||||
using EventType = ENet.EventType;
|
|
||||||
|
|
||||||
namespace Ragon.Client
|
|
||||||
{
|
|
||||||
public class RagonENetConnection : INetworkConnection
|
|
||||||
{
|
|
||||||
public ushort Id { get; }
|
|
||||||
|
|
||||||
public NetworkStatistics Statistics { get; private set; }
|
|
||||||
public INetworkChannel Reliable { get; private set; }
|
|
||||||
public INetworkChannel Unreliable { get; private set; }
|
|
||||||
|
|
||||||
public Action<byte[]> OnData { get; set; }
|
|
||||||
public Action OnConnected { get; set; }
|
|
||||||
public Action<RagonDisconnect> OnDisconnected { get; set; }
|
|
||||||
public ulong BytesSent { get; }
|
|
||||||
public ulong BytesReceived { get; }
|
|
||||||
public int Ping { get; }
|
|
||||||
|
|
||||||
private static bool _libraryLoaded = false;
|
|
||||||
private Host _host;
|
|
||||||
private Peer _peer;
|
|
||||||
private Event _netEvent;
|
|
||||||
|
|
||||||
public RagonENetConnection()
|
|
||||||
{
|
|
||||||
_host = new Host();
|
|
||||||
_host.Create();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void Prepare()
|
|
||||||
{
|
|
||||||
if (!_libraryLoaded)
|
|
||||||
{
|
|
||||||
Library.Initialize();
|
|
||||||
_libraryLoaded = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Disconnect()
|
|
||||||
{
|
|
||||||
if (_peer.IsSet)
|
|
||||||
_peer.DisconnectNow(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Connect(string server, ushort port, uint protocol)
|
|
||||||
{
|
|
||||||
Address address = new Address();
|
|
||||||
address.SetHost(server);
|
|
||||||
address.Port = port;
|
|
||||||
|
|
||||||
_peer = _host.Connect(address, 2, protocol);
|
|
||||||
_peer.Timeout(32, 5000, 5000);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Update()
|
|
||||||
{
|
|
||||||
bool polled = false;
|
|
||||||
while (!polled)
|
|
||||||
{
|
|
||||||
if (_host.CheckEvents(out _netEvent) <= 0)
|
|
||||||
{
|
|
||||||
if (_host.Service(0, out _netEvent) <= 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
polled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (_netEvent.Type)
|
|
||||||
{
|
|
||||||
case EventType.None:
|
|
||||||
break;
|
|
||||||
case EventType.Connect:
|
|
||||||
Statistics = new NetworkStatistics();
|
|
||||||
Reliable = new ENetReliableChannel(_netEvent.Peer, 0);
|
|
||||||
Unreliable = new ENetUnreliableChannel(_netEvent.Peer, 1);
|
|
||||||
|
|
||||||
OnConnected?.Invoke();
|
|
||||||
break;
|
|
||||||
case EventType.Disconnect:
|
|
||||||
OnDisconnected?.Invoke(RagonDisconnect.SERVER);
|
|
||||||
break;
|
|
||||||
case EventType.Timeout:
|
|
||||||
OnDisconnected?.Invoke(RagonDisconnect.TIMEOUT);
|
|
||||||
break;
|
|
||||||
case EventType.Receive:
|
|
||||||
var data = new byte[_netEvent.Packet.Length];
|
|
||||||
|
|
||||||
_netEvent.Packet.CopyTo(data);
|
|
||||||
_netEvent.Packet.Dispose();
|
|
||||||
|
|
||||||
OnData?.Invoke(data);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
if (_host.IsSet)
|
|
||||||
{
|
|
||||||
_host?.Flush();
|
|
||||||
_host?.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_libraryLoaded)
|
|
||||||
Library.Deinitialize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
using ENet;
|
|
||||||
|
|
||||||
namespace Ragon.Client;
|
|
||||||
|
|
||||||
public sealed class ENetUnreliableChannel : INetworkChannel
|
|
||||||
{
|
|
||||||
private Peer _peer;
|
|
||||||
private byte _channelId;
|
|
||||||
|
|
||||||
public ENetUnreliableChannel(Peer peer, int channelId)
|
|
||||||
{
|
|
||||||
_peer = peer;
|
|
||||||
_channelId = (byte) channelId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Send(byte[] data)
|
|
||||||
{
|
|
||||||
var newPacket = new Packet();
|
|
||||||
newPacket.Create(data, data.Length, PacketFlags.None);
|
|
||||||
|
|
||||||
_peer.Send(_channelId, ref newPacket);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,132 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
using ENet;
|
|
||||||
using Ragon.Protocol;
|
|
||||||
using Event = ENet.Event;
|
|
||||||
using EventType = ENet.EventType;
|
|
||||||
|
|
||||||
namespace Ragon.Client
|
|
||||||
{
|
|
||||||
public class RagonNullConnection : INetworkConnection
|
|
||||||
{
|
|
||||||
public ushort Id { get; }
|
|
||||||
|
|
||||||
public NetworkStatistics Statistics { get; private set; }
|
|
||||||
public INetworkChannel Reliable { get; private set; }
|
|
||||||
public INetworkChannel Unreliable { get; private set; }
|
|
||||||
|
|
||||||
public Action<byte[]> OnData { get; set; }
|
|
||||||
public Action OnConnected { get; set; }
|
|
||||||
public Action<RagonDisconnect> OnDisconnected { get; set; }
|
|
||||||
public ulong BytesSent { get; }
|
|
||||||
public ulong BytesReceived { get; }
|
|
||||||
public int Ping { get; }
|
|
||||||
|
|
||||||
private static bool _libraryLoaded = false;
|
|
||||||
private Host _host;
|
|
||||||
private Peer _peer;
|
|
||||||
private Event _netEvent;
|
|
||||||
|
|
||||||
public RagonNullConnection()
|
|
||||||
{
|
|
||||||
_host = new Host();
|
|
||||||
_host.Create();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void Prepare()
|
|
||||||
{
|
|
||||||
if (!_libraryLoaded)
|
|
||||||
{
|
|
||||||
Library.Initialize();
|
|
||||||
_libraryLoaded = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Disconnect()
|
|
||||||
{
|
|
||||||
if (_peer.IsSet)
|
|
||||||
_peer.DisconnectNow(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Connect(string server, ushort port, uint protocol)
|
|
||||||
{
|
|
||||||
Address address = new Address();
|
|
||||||
address.SetHost(server);
|
|
||||||
address.Port = port;
|
|
||||||
|
|
||||||
_peer = _host.Connect(address, 2, protocol);
|
|
||||||
_peer.Timeout(32, 5000, 5000);
|
|
||||||
|
|
||||||
Statistics = new NetworkStatistics();
|
|
||||||
Reliable = new NullReliableChannel(_netEvent.Peer, 0);
|
|
||||||
Unreliable = new NullUnreliableChannel(_netEvent.Peer, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Update()
|
|
||||||
{
|
|
||||||
bool polled = false;
|
|
||||||
while (!polled)
|
|
||||||
{
|
|
||||||
if (_host.CheckEvents(out _netEvent) <= 0)
|
|
||||||
{
|
|
||||||
if (_host.Service(0, out _netEvent) <= 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
polled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (_netEvent.Type)
|
|
||||||
{
|
|
||||||
case EventType.None:
|
|
||||||
break;
|
|
||||||
case EventType.Connect:
|
|
||||||
|
|
||||||
OnConnected?.Invoke();
|
|
||||||
break;
|
|
||||||
case EventType.Disconnect:
|
|
||||||
OnDisconnected?.Invoke(RagonDisconnect.SERVER);
|
|
||||||
break;
|
|
||||||
case EventType.Timeout:
|
|
||||||
OnDisconnected?.Invoke(RagonDisconnect.TIMEOUT);
|
|
||||||
break;
|
|
||||||
case EventType.Receive:
|
|
||||||
var data = new byte[_netEvent.Packet.Length];
|
|
||||||
|
|
||||||
_netEvent.Packet.CopyTo(data);
|
|
||||||
_netEvent.Packet.Dispose();
|
|
||||||
|
|
||||||
OnData?.Invoke(data);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
if (_host.IsSet)
|
|
||||||
{
|
|
||||||
_host?.Flush();
|
|
||||||
_host?.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_libraryLoaded)
|
|
||||||
Library.Deinitialize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
namespace Ragon.Client.Simulation;
|
|
||||||
|
|
||||||
public class EntityListener : IRagonEntityListener
|
|
||||||
{
|
|
||||||
public void OnEntityCreated(RagonEntity entity)
|
|
||||||
{
|
|
||||||
var health = new RagonFloat(100.0f, false, 0);
|
|
||||||
health.Value = 50;
|
|
||||||
health.Changed += () => Console.WriteLine($"[Ragon Property] Another Health: {health.Value}");
|
|
||||||
|
|
||||||
var points = new RagonInt(0, -1000, 1000, false, 0);
|
|
||||||
points.Changed += () => Console.WriteLine($"[Ragon Property] Anther Points: {points.Value}");
|
|
||||||
|
|
||||||
var name = new RagonString("Eduard", false);
|
|
||||||
name.Changed += () => Console.WriteLine($"[Ragon Property] Another Name: {name.Value}");
|
|
||||||
|
|
||||||
entity.State.AddProperty(health);
|
|
||||||
entity.State.AddProperty(points);
|
|
||||||
entity.State.AddProperty(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Simulation
|
|
||||||
{
|
|
||||||
public void Start()
|
|
||||||
{
|
|
||||||
// INetworkConnection protocol = debug ? new RagonNullConnection() : new RagonENetConnection();
|
|
||||||
// var network = new RagonClient(protocol, new EntityListener(), 30);
|
|
||||||
// var game = new Game(network);
|
|
||||||
// network.AddListener(game);
|
|
||||||
// network.Connect("127.0.0.1", 5001, "1.0.0");
|
|
||||||
// var dt = 1000 / 60.0f;
|
|
||||||
// while (true)
|
|
||||||
// {
|
|
||||||
// game.Update();
|
|
||||||
// network.Update(dt);
|
|
||||||
// Thread.Sleep((int) dt);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// network.Disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,18 +5,18 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<LangVersion>10</LangVersion>
|
<LangVersion>10</LangVersion>
|
||||||
<RootNamespace>Ragon.Client.Simulation</RootNamespace>
|
<RootNamespace>Ragon.Client.Simulation</RootNamespace>
|
||||||
<Authors>Eduard Kargin (Edmand46)</Authors>
|
<Authors>Eduard Kargin</Authors>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
<DebugType>none</DebugType>
|
<DebugType>none</DebugType>
|
||||||
<OutputPath>/Users/edmand46/RagonProjects/ragon-oss-examples/Assets/Ragon/Plugins/</OutputPath>
|
<OutputPath></OutputPath>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||||
<OutputPath>/Users/edmand46/RagonProjects/ragon-oss-examples/Assets/Ragon/Plugins/</OutputPath>
|
<OutputPath></OutputPath>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,20 +19,48 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client
|
namespace Ragon.Client
|
||||||
{
|
{
|
||||||
public sealed class RagonEntity
|
public sealed class RagonEntity : IDisposable
|
||||||
{
|
{
|
||||||
|
private class EventSubscription : IDisposable
|
||||||
|
{
|
||||||
|
private List<Action<RagonPlayer, IRagonEvent>> _callbacks;
|
||||||
|
private List<Action<RagonPlayer, IRagonEvent>> _localCallbacks;
|
||||||
|
private Action<RagonPlayer, IRagonEvent> _callback;
|
||||||
|
|
||||||
|
public EventSubscription(
|
||||||
|
List<Action<RagonPlayer, IRagonEvent>> callbacks,
|
||||||
|
List<Action<RagonPlayer, IRagonEvent>> localCallbacks,
|
||||||
|
Action<RagonPlayer, IRagonEvent> callback)
|
||||||
|
{
|
||||||
|
_callbacks = callbacks;
|
||||||
|
_localCallbacks = localCallbacks;
|
||||||
|
_callback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_callbacks?.Remove(_callback);
|
||||||
|
_localCallbacks?.Remove(_callback);
|
||||||
|
|
||||||
|
_callbacks = null!;
|
||||||
|
_localCallbacks = null!;
|
||||||
|
_callback = null!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
|
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
|
||||||
|
|
||||||
private RagonClient _client;
|
private RagonClient _client;
|
||||||
|
|
||||||
public ushort Id { get; private set; }
|
public ushort Id { get; private set; }
|
||||||
public ushort Type { get; private set; }
|
public ushort Type { get; private set; }
|
||||||
public bool Replication { get; private set; }
|
|
||||||
|
|
||||||
public RagonAuthority Authority { get; private set; }
|
public RagonAuthority Authority { get; private set; }
|
||||||
public RagonPlayer Owner { get; private set; }
|
public RagonPlayer Owner { get; private set; }
|
||||||
public RagonEntityState State { get; private set; }
|
public RagonEntityState State { get; private set; }
|
||||||
|
|
||||||
|
public bool IsStatic => SceneId > 0;
|
||||||
|
public bool IsReplicated { get; private set; }
|
||||||
public bool IsAttached { get; private set; }
|
public bool IsAttached { get; private set; }
|
||||||
public bool HasAuthority { get; private set; }
|
public bool HasAuthority { get; private set; }
|
||||||
|
|
||||||
@@ -43,39 +71,39 @@ namespace Ragon.Client
|
|||||||
internal bool PropertiesChanged => _propertiesChanged;
|
internal bool PropertiesChanged => _propertiesChanged;
|
||||||
internal ushort SceneId => _sceneId;
|
internal ushort SceneId => _sceneId;
|
||||||
|
|
||||||
private ushort _sceneId;
|
private readonly ushort _sceneId;
|
||||||
private bool _propertiesChanged;
|
private bool _propertiesChanged;
|
||||||
|
|
||||||
private RagonPayload _spawnPayload;
|
private RagonPayload _spawnPayload;
|
||||||
private RagonPayload _destroyPayload;
|
private RagonPayload _destroyPayload;
|
||||||
|
|
||||||
private readonly Dictionary<int, OnEventDelegate> _events = new Dictionary<int, OnEventDelegate>();
|
private readonly Dictionary<int, OnEventDelegate> _events = new();
|
||||||
private readonly Dictionary<int, Action<RagonPlayer, IRagonEvent>> _localEvents = new Dictionary<int, Action<RagonPlayer, IRagonEvent>>();
|
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners = new();
|
||||||
|
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners = new();
|
||||||
|
|
||||||
public RagonEntity(ushort type = 0, ushort sceneId = 0)
|
public RagonEntity(ushort type = 0, ushort sceneId = 0, bool replicated = true)
|
||||||
{
|
{
|
||||||
State = new RagonEntityState(this);
|
State = new RagonEntityState(this);
|
||||||
Type = type;
|
Type = type;
|
||||||
|
IsReplicated = replicated;
|
||||||
|
|
||||||
_spawnPayload = new RagonPayload(0);
|
_spawnPayload = new RagonPayload(0);
|
||||||
_destroyPayload = new RagonPayload(0);
|
_destroyPayload = new RagonPayload(0);
|
||||||
_sceneId = sceneId;
|
_sceneId = sceneId;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Attach(RagonClient client, ushort entityId, ushort entityType, bool hasAuthority, RagonPlayer owner)
|
internal void Attach()
|
||||||
{
|
{
|
||||||
Type = entityType;
|
|
||||||
Id = entityId;
|
|
||||||
Owner = owner;
|
|
||||||
IsAttached = true;
|
IsAttached = true;
|
||||||
Replication = true;
|
|
||||||
HasAuthority = hasAuthority;
|
|
||||||
|
|
||||||
_client = client;
|
|
||||||
|
|
||||||
Attached?.Invoke(this);
|
Attached?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SetReplication(bool enabled)
|
||||||
|
{
|
||||||
|
IsReplicated = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
internal void Detach(RagonPayload payload)
|
internal void Detach(RagonPayload payload)
|
||||||
{
|
{
|
||||||
_destroyPayload = payload;
|
_destroyPayload = payload;
|
||||||
@@ -97,9 +125,23 @@ namespace Ragon.Client
|
|||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AttachPayload(RagonPayload payload)
|
public void Prepare(
|
||||||
|
RagonClient client,
|
||||||
|
ushort entityId,
|
||||||
|
ushort entityType,
|
||||||
|
bool hasAuthority,
|
||||||
|
RagonPlayer player,
|
||||||
|
RagonPayload payload
|
||||||
|
)
|
||||||
{
|
{
|
||||||
|
Type = entityType;
|
||||||
|
Id = entityId;
|
||||||
|
HasAuthority = hasAuthority;
|
||||||
|
|
||||||
|
_client = client;
|
||||||
_spawnPayload = payload;
|
_spawnPayload = payload;
|
||||||
|
|
||||||
|
Owner = player;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T GetAttachPayload<T>() where T : IRagonPayload, new()
|
public T GetAttachPayload<T>() where T : IRagonPayload, new()
|
||||||
@@ -155,12 +197,17 @@ namespace Ragon.Client
|
|||||||
{
|
{
|
||||||
if (replicationMode == RagonReplicationMode.Local)
|
if (replicationMode == RagonReplicationMode.Local)
|
||||||
{
|
{
|
||||||
_localEvents[eventCode].Invoke(_client.Room.Local, evnt);
|
var localListeners = _localListeners[eventCode];
|
||||||
|
foreach (var listener in localListeners)
|
||||||
|
listener.Invoke(_client.Room.Local, evnt);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (replicationMode == RagonReplicationMode.LocalAndServer)
|
if (replicationMode == RagonReplicationMode.LocalAndServer)
|
||||||
{
|
{
|
||||||
_localEvents[eventCode].Invoke(_client.Room.Local, evnt);
|
var localListeners = _localListeners[eventCode];
|
||||||
|
foreach (var listener in localListeners)
|
||||||
|
listener.Invoke(_client.Room.Local, evnt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,27 +226,41 @@ namespace Ragon.Client
|
|||||||
_client.Reliable.Send(sendData);
|
_client.Reliable.Send(sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
|
public IDisposable OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
|
||||||
{
|
{
|
||||||
var t = new TEvent();
|
var t = new TEvent();
|
||||||
var eventCode = _client.Event.GetEventCode(t);
|
var eventCode = _client.Event.GetEventCode(t);
|
||||||
|
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
|
||||||
|
|
||||||
if (_events.ContainsKey(eventCode))
|
if (!_listeners.TryGetValue(eventCode, out var callbacks))
|
||||||
{
|
{
|
||||||
_events.Remove(eventCode);
|
callbacks = new List<Action<RagonPlayer, IRagonEvent>>();
|
||||||
_localEvents.Remove(eventCode);
|
_listeners.Add(eventCode, callbacks);
|
||||||
|
|
||||||
RagonLog.Warn($"Event already {eventCode} subscribed, removed old one!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_localEvents.Add(eventCode, (player, eventData) => { callback.Invoke(player, (TEvent)eventData); });
|
if (!_localListeners.TryGetValue(eventCode, out var localCallbacks))
|
||||||
|
{
|
||||||
|
localCallbacks = new List<Action<RagonPlayer, IRagonEvent>>();
|
||||||
|
_localListeners.Add(eventCode, localCallbacks);
|
||||||
|
}
|
||||||
|
|
||||||
|
callbacks.Add(action);
|
||||||
|
localCallbacks.Add(action);
|
||||||
|
|
||||||
|
if (!_events.ContainsKey(eventCode))
|
||||||
|
{
|
||||||
_events.Add(eventCode, (player, serializer) =>
|
_events.Add(eventCode, (player, serializer) =>
|
||||||
{
|
{
|
||||||
t.Deserialize(serializer);
|
t.Deserialize(serializer);
|
||||||
callback.Invoke(player, t);
|
|
||||||
|
foreach (var callbackListener in callbacks)
|
||||||
|
callbackListener.Invoke(player, t);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return new EventSubscription(callbacks, localCallbacks, action);
|
||||||
|
}
|
||||||
|
|
||||||
internal void Write(RagonBuffer buffer)
|
internal void Write(RagonBuffer buffer)
|
||||||
{
|
{
|
||||||
buffer.WriteUShort(Id);
|
buffer.WriteUShort(Id);
|
||||||
@@ -209,7 +270,6 @@ namespace Ragon.Client
|
|||||||
_propertiesChanged = false;
|
_propertiesChanged = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
internal void Read(RagonBuffer buffer)
|
internal void Read(RagonBuffer buffer)
|
||||||
{
|
{
|
||||||
State.ReadState(buffer);
|
State.ReadState(buffer);
|
||||||
@@ -217,6 +277,8 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
|
internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
|
||||||
{
|
{
|
||||||
|
if (!IsReplicated) return;
|
||||||
|
|
||||||
if (_events.TryGetValue(eventCode, out var evnt))
|
if (_events.TryGetValue(eventCode, out var evnt))
|
||||||
evnt?.Invoke(caller, buffer);
|
evnt?.Invoke(caller, buffer);
|
||||||
else
|
else
|
||||||
@@ -237,5 +299,12 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
OwnershipChanged?.Invoke(prevOwner, player);
|
OwnershipChanged?.Invoke(prevOwner, player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_events.Clear();
|
||||||
|
_listeners.Clear();
|
||||||
|
_localListeners.Clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -21,6 +21,8 @@ namespace Ragon.Client;
|
|||||||
|
|
||||||
public class RagonPayload
|
public class RagonPayload
|
||||||
{
|
{
|
||||||
|
public static RagonPayload Empty = new RagonPayload(0);
|
||||||
|
|
||||||
private readonly uint[] _data = new uint[128];
|
private readonly uint[] _data = new uint[128];
|
||||||
private readonly int _size = 0;
|
private readonly int _size = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -63,7 +63,7 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
protected void InvokeChanged()
|
protected void InvokeChanged()
|
||||||
{
|
{
|
||||||
if (!InvokeLocal)
|
if (_entity.HasAuthority)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Changed?.Invoke();
|
Changed?.Invoke();
|
||||||
@@ -71,7 +71,8 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
protected void MarkAsChanged()
|
protected void MarkAsChanged()
|
||||||
{
|
{
|
||||||
InvokeChanged();
|
if (InvokeLocal)
|
||||||
|
Changed?.Invoke();
|
||||||
|
|
||||||
if (_dirty || _entity == null)
|
if (_dirty || _entity == null)
|
||||||
return;
|
return;
|
||||||
@@ -106,15 +107,15 @@ namespace Ragon.Client
|
|||||||
{
|
{
|
||||||
Serialize(_propertyBuffer);
|
Serialize(_propertyBuffer);
|
||||||
|
|
||||||
buffer.FromBuffer(_propertyBuffer, _size);
|
buffer.CopyFrom(_propertyBuffer, _size);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serialize(_propertyBuffer);
|
Serialize(_propertyBuffer);
|
||||||
|
|
||||||
var propertySize = (ushort)_propertyBuffer.WriteOffset;
|
var propertySize = (ushort)_propertyBuffer.WriteOffset;
|
||||||
buffer.WriteUShort(propertySize);;
|
buffer.WriteUShort(propertySize);
|
||||||
buffer.FromBuffer(_propertyBuffer, propertySize);
|
buffer.CopyFrom(_propertyBuffer, propertySize);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Read(RagonBuffer buffer)
|
internal void Read(RagonBuffer buffer)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,7 +19,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class AuthorizeFailedHandler: Handler
|
internal class AuthorizeFailedHandler: IHandler
|
||||||
{
|
{
|
||||||
private readonly RagonListenerList _listenerList;
|
private readonly RagonListenerList _listenerList;
|
||||||
public AuthorizeFailedHandler(RagonListenerList list)
|
public AuthorizeFailedHandler(RagonListenerList list)
|
||||||
@@ -27,9 +27,9 @@ internal class AuthorizeFailedHandler: Handler
|
|||||||
_listenerList = list;
|
_listenerList = list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
var message = buffer.ReadString();
|
var message = reader.ReadString();
|
||||||
_listenerList.OnAuthorizationFailed(message);
|
_listenerList.OnAuthorizationFailed(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,7 +19,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class AuthorizeSuccessHandler: Handler
|
internal class AuthorizeSuccessHandler: IHandler
|
||||||
{
|
{
|
||||||
private readonly RagonListenerList _listenerList;
|
private readonly RagonListenerList _listenerList;
|
||||||
private readonly RagonClient _client;
|
private readonly RagonClient _client;
|
||||||
@@ -32,11 +32,11 @@ internal class AuthorizeSuccessHandler: Handler
|
|||||||
_listenerList = listenerList;
|
_listenerList = listenerList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
var playerId = buffer.ReadString();
|
var playerId = reader.ReadString();
|
||||||
var playerName = buffer.ReadString();
|
var playerName = reader.ReadString();
|
||||||
var playerPayload = buffer.ReadString();
|
var playerPayload = reader.ReadString();
|
||||||
|
|
||||||
_client.SetStatus(RagonStatus.LOBBY);
|
_client.SetStatus(RagonStatus.LOBBY);
|
||||||
_listenerList.OnAuthorizationSuccess(playerId, playerName, playerPayload);
|
_listenerList.OnAuthorizationSuccess(playerId, playerName, playerPayload);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,12 +18,13 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class EntityCreateHandler : Handler
|
internal class EntityCreateHandler : IHandler
|
||||||
{
|
{
|
||||||
private readonly RagonClient _client;
|
private readonly RagonClient _client;
|
||||||
private readonly RagonPlayerCache _playerCache;
|
private readonly RagonPlayerCache _playerCache;
|
||||||
private readonly RagonEntityCache _entityCache;
|
private readonly RagonEntityCache _entityCache;
|
||||||
private readonly IRagonEntityListener _entityListener;
|
private readonly IRagonEntityListener _entityListener;
|
||||||
|
|
||||||
public EntityCreateHandler(
|
public EntityCreateHandler(
|
||||||
RagonClient client,
|
RagonClient client,
|
||||||
RagonPlayerCache playerCache,
|
RagonPlayerCache playerCache,
|
||||||
@@ -37,30 +38,32 @@ internal class EntityCreateHandler : Handler
|
|||||||
_entityListener = entityListener;
|
_entityListener = entityListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
var attachId = buffer.ReadUShort();
|
var attachId = reader.ReadUShort();
|
||||||
var entityType = buffer.ReadUShort();
|
var entityType = reader.ReadUShort();
|
||||||
var entityId = buffer.ReadUShort();
|
var entityId = reader.ReadUShort();
|
||||||
var ownerId = buffer.ReadUShort();
|
var ownerId = reader.ReadUShort();
|
||||||
var player = _playerCache.GetPlayerByPeer(ownerId);
|
var player = _playerCache.GetPlayerByPeer(ownerId);
|
||||||
var payload = new RagonPayload(buffer.Capacity);
|
var payload = new RagonPayload(reader.Capacity);
|
||||||
payload.Read(buffer);
|
payload.Read(reader);
|
||||||
|
|
||||||
if (player == null)
|
if (player == null)
|
||||||
{
|
{
|
||||||
RagonLog.Warn($"Owner {ownerId}|{player.Name} not found in players");
|
RagonLog.Warn($"Owner {ownerId}|{player.Name} not found in players");
|
||||||
|
|
||||||
|
_playerCache.Dump();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var hasAuthority = _playerCache.Local.Id == player.Id;
|
var hasAuthority = _playerCache.Local.Id == player.Id;
|
||||||
var entity = _entityCache.TryGetEntity(attachId, entityType, 0, entityId, hasAuthority, out var hasCreated);
|
var entity = _entityCache.TryGetEntity(attachId, entityType, 0, entityId, hasAuthority, out var hasCreated);
|
||||||
|
|
||||||
entity.AttachPayload(payload);
|
entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload);
|
||||||
|
|
||||||
if (hasCreated)
|
if (hasCreated)
|
||||||
_entityListener.OnEntityCreated(entity);
|
_entityListener.OnEntityCreated(entity);
|
||||||
|
|
||||||
entity.Attach(_client, entityId, entityType, hasAuthority, player);
|
entity.Attach();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,40 +18,39 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class EntityEventHandler : Handler
|
internal class EntityEventHandler : IHandler
|
||||||
{
|
{
|
||||||
private readonly RagonClient _client;
|
|
||||||
private readonly RagonPlayerCache _playerCache;
|
private readonly RagonPlayerCache _playerCache;
|
||||||
private readonly RagonEntityCache _entityCache;
|
private readonly RagonEntityCache _entityCache;
|
||||||
|
|
||||||
public EntityEventHandler(
|
public EntityEventHandler(
|
||||||
RagonClient client,
|
|
||||||
RagonPlayerCache playerCache,
|
RagonPlayerCache playerCache,
|
||||||
RagonEntityCache entityCache
|
RagonEntityCache entityCache
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
_client = client;
|
|
||||||
_playerCache = playerCache;
|
_playerCache = playerCache;
|
||||||
_entityCache = entityCache;
|
_entityCache = entityCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
var eventCode = buffer.ReadUShort();
|
var eventCode = reader.ReadUShort();
|
||||||
var peerId = buffer.ReadUShort();
|
var peerId = reader.ReadUShort();
|
||||||
var executionMode = (RagonReplicationMode)buffer.ReadByte();
|
var executionMode = (RagonReplicationMode)reader.ReadByte();
|
||||||
var entityId = buffer.ReadUShort();
|
var entityId = reader.ReadUShort();
|
||||||
|
|
||||||
var player = _playerCache.GetPlayerByPeer(peerId);
|
var player = _playerCache.GetPlayerByPeer(peerId);
|
||||||
if (player == null)
|
if (player == null)
|
||||||
{
|
{
|
||||||
RagonLog.Warn($"Player not found for event {eventCode}");
|
RagonLog.Error($"Player with peerId:{peerId} not found as owner of event with code:{eventCode}");
|
||||||
|
|
||||||
|
_playerCache.Dump();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer)
|
if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_entityCache.OnEvent(player, entityId, eventCode, buffer);
|
_entityCache.OnEvent(player, entityId, eventCode, reader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,7 +19,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class EntityOwnershipHandler: Handler
|
internal class EntityOwnershipHandler: IHandler
|
||||||
{
|
{
|
||||||
private readonly RagonListenerList _listenerList;
|
private readonly RagonListenerList _listenerList;
|
||||||
private readonly RagonPlayerCache _playerCache;
|
private readonly RagonPlayerCache _playerCache;
|
||||||
@@ -35,15 +35,23 @@ internal class EntityOwnershipHandler: Handler
|
|||||||
_entityCache = entityCache;
|
_entityCache = entityCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
var newOwnerId = buffer.ReadUShort();
|
var newOwnerId = reader.ReadUShort();
|
||||||
var entities = buffer.ReadUShort();
|
var entities = reader.ReadUShort();
|
||||||
|
|
||||||
var player = _playerCache.GetPlayerByPeer(newOwnerId);
|
var player = _playerCache.GetPlayerByPeer(newOwnerId);
|
||||||
|
if (player == null)
|
||||||
|
{
|
||||||
|
RagonLog.Error($"Player with Id:{newOwnerId} not found in cache");
|
||||||
|
|
||||||
|
_playerCache.Dump();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (var i = 0; i < entities; i++)
|
for (var i = 0; i < entities; i++)
|
||||||
{
|
{
|
||||||
var entityId = buffer.ReadUShort();
|
var entityId = reader.ReadUShort();
|
||||||
_entityCache.OnOwnershipChanged(player, entityId);
|
_entityCache.OnOwnershipChanged(player, entityId);
|
||||||
|
|
||||||
RagonLog.Trace("Entity changed owner: " + entityId);
|
RagonLog.Trace("Entity changed owner: " + entityId);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,7 +19,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class EntityRemoveHandler: Handler
|
internal class EntityRemoveHandler: IHandler
|
||||||
{
|
{
|
||||||
private readonly RagonEntityCache _entityCache;
|
private readonly RagonEntityCache _entityCache;
|
||||||
|
|
||||||
@@ -28,11 +28,11 @@ internal class EntityRemoveHandler: Handler
|
|||||||
_entityCache = entityCache;
|
_entityCache = entityCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
var entityId = buffer.ReadUShort();
|
var entityId = reader.ReadUShort();
|
||||||
var payload = new RagonPayload(buffer.Capacity);
|
var payload = new RagonPayload(reader.Capacity);
|
||||||
payload.Read(buffer);
|
payload.Read(reader);
|
||||||
|
|
||||||
_entityCache.OnDestroy(entityId, payload);
|
_entityCache.OnDestroy(entityId, payload);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,7 +18,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class StateEntityHandler: Handler
|
internal class StateEntityHandler: IHandler
|
||||||
{
|
{
|
||||||
private readonly RagonEntityCache _entityCache;
|
private readonly RagonEntityCache _entityCache;
|
||||||
|
|
||||||
@@ -27,13 +27,13 @@ internal class StateEntityHandler: Handler
|
|||||||
_entityCache = entityCache;
|
_entityCache = entityCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
var entitiesCount = buffer.ReadUShort();
|
var entitiesCount = reader.ReadUShort();
|
||||||
for (var i = 0; i < entitiesCount; i++)
|
for (var i = 0; i < entitiesCount; i++)
|
||||||
{
|
{
|
||||||
var entityId = buffer.ReadUShort();
|
var entityId = reader.ReadUShort();
|
||||||
_entityCache.OnState(entityId, buffer);
|
_entityCache.OnState(entityId, reader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,7 +19,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
public interface Handler
|
public interface IHandler
|
||||||
{
|
{
|
||||||
public void Handle(RagonBuffer buffer);
|
public void Handle(RagonBuffer reader);
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,7 +19,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class JoinFailedHandler: Handler
|
internal class JoinFailedHandler: IHandler
|
||||||
{
|
{
|
||||||
private readonly RagonListenerList _listenerList;
|
private readonly RagonListenerList _listenerList;
|
||||||
|
|
||||||
@@ -28,9 +28,9 @@ internal class JoinFailedHandler: Handler
|
|||||||
_listenerList = listenerList;
|
_listenerList = listenerList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
var message = buffer.ReadString();
|
var message = reader.ReadString();
|
||||||
_listenerList.OnFailed(message);
|
_listenerList.OnFailed(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,9 +19,9 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
public struct RagonRoomInformation
|
public struct RoomParameters
|
||||||
{
|
{
|
||||||
public RagonRoomInformation(string roomId, string playerId, string ownerId, ushort min, ushort max)
|
public RoomParameters(string roomId, string playerId, string ownerId, ushort min, ushort max)
|
||||||
{
|
{
|
||||||
RoomId = roomId;
|
RoomId = roomId;
|
||||||
PlayerId = playerId;
|
PlayerId = playerId;
|
||||||
@@ -37,7 +37,7 @@ public struct RagonRoomInformation
|
|||||||
public ushort Max { get; private set; }
|
public ushort Max { get; private set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class JoinSuccessHandler : Handler
|
internal class JoinSuccessHandler : IHandler
|
||||||
{
|
{
|
||||||
private readonly RagonListenerList _listenerList;
|
private readonly RagonListenerList _listenerList;
|
||||||
private readonly RagonPlayerCache _playerCache;
|
private readonly RagonPlayerCache _playerCache;
|
||||||
@@ -46,7 +46,6 @@ internal class JoinSuccessHandler : Handler
|
|||||||
|
|
||||||
public JoinSuccessHandler(
|
public JoinSuccessHandler(
|
||||||
RagonClient client,
|
RagonClient client,
|
||||||
RagonBuffer buffer,
|
|
||||||
RagonListenerList listenerList,
|
RagonListenerList listenerList,
|
||||||
RagonPlayerCache playerCache,
|
RagonPlayerCache playerCache,
|
||||||
RagonEntityCache entityCache
|
RagonEntityCache entityCache
|
||||||
@@ -58,22 +57,42 @@ internal class JoinSuccessHandler : Handler
|
|||||||
_playerCache = playerCache;
|
_playerCache = playerCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
var roomId = buffer.ReadString();
|
var roomId = reader.ReadString();
|
||||||
var localId = buffer.ReadString();
|
var min = reader.ReadUShort();
|
||||||
var ownerId = buffer.ReadString();
|
var max = reader.ReadUShort();
|
||||||
var min = buffer.ReadUShort();
|
var sceneName = reader.ReadString();
|
||||||
var max = buffer.ReadUShort();
|
var localId = reader.ReadString();
|
||||||
var sceneName = buffer.ReadString();
|
var ownerId = reader.ReadString();
|
||||||
|
|
||||||
var scene = new RagonScene(_client, _playerCache, _entityCache, sceneName);
|
|
||||||
var roomInfo = new RagonRoomInformation(roomId, localId, ownerId, min, max);
|
|
||||||
var room = new RagonRoom(_client, _entityCache, _playerCache, roomInfo, scene);
|
|
||||||
|
|
||||||
_playerCache.SetOwnerAndLocal(ownerId, localId);
|
_playerCache.SetOwnerAndLocal(ownerId, localId);
|
||||||
_client.AssignRoom(room);
|
|
||||||
|
|
||||||
|
var scene = new RagonScene(_client, _playerCache, _entityCache, sceneName);
|
||||||
|
var roomInfo = new RoomParameters(roomId, localId, ownerId, min, max);
|
||||||
|
var room = new RagonRoom(_client, _entityCache, _playerCache, roomInfo, scene);
|
||||||
|
|
||||||
|
room.UserData.Read(reader);
|
||||||
|
|
||||||
|
var playersCount = reader.ReadUShort();
|
||||||
|
RagonLog.Trace("Players: " + playersCount);
|
||||||
|
for (var i = 0; i < playersCount; i++)
|
||||||
|
{
|
||||||
|
var playerPeerId = reader.ReadUShort();
|
||||||
|
var playerId = reader.ReadString();
|
||||||
|
var playerName = reader.ReadString();
|
||||||
|
|
||||||
|
var player = _playerCache.AddPlayer(playerPeerId, playerId, playerName);
|
||||||
|
|
||||||
|
player.UserData.Read(reader);
|
||||||
|
|
||||||
|
RagonLog.Trace($"Player {playerPeerId} - {playerId} - {playerName}");
|
||||||
|
}
|
||||||
|
|
||||||
|
_client.AssignRoom(room);
|
||||||
|
_client.SetStatus(RagonStatus.ROOM);
|
||||||
|
|
||||||
|
_listenerList.OnJoined();
|
||||||
_listenerList.OnSceneRequest(sceneName);
|
_listenerList.OnSceneRequest(sceneName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,7 +19,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class LeaveRoomHandler : Handler
|
internal class LeaveRoomHandler : IHandler
|
||||||
{
|
{
|
||||||
private readonly RagonClient _client;
|
private readonly RagonClient _client;
|
||||||
private readonly RagonListenerList _listenerList;
|
private readonly RagonListenerList _listenerList;
|
||||||
@@ -35,7 +35,7 @@ internal class LeaveRoomHandler : Handler
|
|||||||
_entityCache = entityCache;
|
_entityCache = entityCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
_listenerList.OnLeft();
|
_listenerList.OnLeft();
|
||||||
_entityCache.Cleanup();
|
_entityCache.Cleanup();
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,7 +18,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class SceneLoadHandler: Handler
|
internal class SceneLoadHandler: IHandler
|
||||||
{
|
{
|
||||||
private readonly RagonClient _client;
|
private readonly RagonClient _client;
|
||||||
private readonly RagonListenerList _listenerList;
|
private readonly RagonListenerList _listenerList;
|
||||||
@@ -32,9 +32,9 @@ internal class SceneLoadHandler: Handler
|
|||||||
_listenerList = listenerList;
|
_listenerList = listenerList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
var sceneName = buffer.ReadString();
|
var sceneName = reader.ReadString();
|
||||||
var room = _client.Room;
|
var room = _client.Room;
|
||||||
|
|
||||||
room.Cleanup();
|
room.Cleanup();
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,7 +19,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class OwnershipRoomHandler: Handler
|
internal class OwnershipRoomHandler : IHandler
|
||||||
{
|
{
|
||||||
private readonly RagonListenerList _listenerList;
|
private readonly RagonListenerList _listenerList;
|
||||||
private readonly RagonPlayerCache _playerCache;
|
private readonly RagonPlayerCache _playerCache;
|
||||||
@@ -35,10 +35,17 @@ internal class OwnershipRoomHandler: Handler
|
|||||||
_entityCache = entityCache;
|
_entityCache = entityCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
var newOwnerId = buffer.ReadUShort();
|
var newOwnerId = reader.ReadUShort();
|
||||||
var player = _playerCache.GetPlayerByPeer(newOwnerId);
|
var player = _playerCache.GetPlayerByPeer(newOwnerId);
|
||||||
|
if (player == null)
|
||||||
|
{
|
||||||
|
RagonLog.Warn($"Player with peerId:{newOwnerId} not found in cache");
|
||||||
|
|
||||||
|
_playerCache.Dump();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_playerCache.OnOwnershipChanged(newOwnerId);
|
_playerCache.OnOwnershipChanged(newOwnerId);
|
||||||
_listenerList.OnOwnershipChanged(player);
|
_listenerList.OnOwnershipChanged(player);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,7 +19,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class PlayerJoinHandler : Handler
|
internal class PlayerJoinHandler : IHandler
|
||||||
{
|
{
|
||||||
private RagonPlayerCache _playerCache;
|
private RagonPlayerCache _playerCache;
|
||||||
private RagonListenerList _listenerList;
|
private RagonListenerList _listenerList;
|
||||||
@@ -33,11 +33,11 @@ internal class PlayerJoinHandler : Handler
|
|||||||
_listenerList = listenerList;
|
_listenerList = listenerList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
var playerPeerId = buffer.ReadUShort();
|
var playerPeerId = reader.ReadUShort();
|
||||||
var playerId = buffer.ReadString();
|
var playerId = reader.ReadString();
|
||||||
var playerName = buffer.ReadString();
|
var playerName = reader.ReadString();
|
||||||
|
|
||||||
_playerCache.AddPlayer(playerPeerId, playerId, playerName);
|
_playerCache.AddPlayer(playerPeerId, playerId, playerName);
|
||||||
|
|
||||||
@@ -45,6 +45,6 @@ internal class PlayerJoinHandler : Handler
|
|||||||
if (player != null)
|
if (player != null)
|
||||||
_listenerList.OnPlayerJoined(player);
|
_listenerList.OnPlayerJoined(player);
|
||||||
else
|
else
|
||||||
RagonLog.Trace($"[Joined] {playerId}");
|
RagonLog.Warn($"Player with Id:{playerId} not found in cache");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,7 +19,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class PlayerLeftHandler : Handler
|
internal class PlayerLeftHandler : IHandler
|
||||||
{
|
{
|
||||||
private RagonPlayerCache _playerCache;
|
private RagonPlayerCache _playerCache;
|
||||||
private RagonEntityCache _entityCache;
|
private RagonEntityCache _entityCache;
|
||||||
@@ -36,20 +36,20 @@ internal class PlayerLeftHandler : Handler
|
|||||||
_listenerList = listenerList;
|
_listenerList = listenerList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
var playerId = buffer.ReadString();
|
var playerId = reader.ReadString();
|
||||||
var player = _playerCache.GetPlayerById(playerId);
|
var player = _playerCache.GetPlayerById(playerId);
|
||||||
if (player != null)
|
if (player != null)
|
||||||
{
|
{
|
||||||
_playerCache.RemovePlayer(playerId);
|
_playerCache.RemovePlayer(playerId);
|
||||||
_listenerList.OnPlayerLeft(player);
|
_listenerList.OnPlayerLeft(player);
|
||||||
|
|
||||||
var entities = buffer.ReadUShort();
|
var entities = reader.ReadUShort();
|
||||||
var toDeleteIds = new ushort[entities];
|
var toDeleteIds = new ushort[entities];
|
||||||
for (var i = 0; i < entities; i++)
|
for (var i = 0; i < entities; i++)
|
||||||
{
|
{
|
||||||
var entityId = buffer.ReadUShort();
|
var entityId = reader.ReadUShort();
|
||||||
toDeleteIds[i] = entityId;
|
toDeleteIds[i] = entityId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,5 +57,9 @@ internal class PlayerLeftHandler : Handler
|
|||||||
foreach (var id in toDeleteIds)
|
foreach (var id in toDeleteIds)
|
||||||
_entityCache.OnDestroy(id, emptyPayload);
|
_entityCache.OnDestroy(id, emptyPayload);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
RagonLog.Warn($"Player with Id:{playerId} not found in cache");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client
|
||||||
|
{
|
||||||
|
|
||||||
|
internal class PlayerUserDataHandler: IHandler
|
||||||
|
{
|
||||||
|
private RagonPlayerCache _playerCache;
|
||||||
|
private RagonListenerList _listenerList;
|
||||||
|
|
||||||
|
public PlayerUserDataHandler(
|
||||||
|
RagonPlayerCache playerCache,
|
||||||
|
RagonListenerList listenerList
|
||||||
|
)
|
||||||
|
{
|
||||||
|
_playerCache = playerCache;
|
||||||
|
_listenerList = listenerList;
|
||||||
|
}
|
||||||
|
public void Handle(RagonBuffer reader)
|
||||||
|
{
|
||||||
|
var playerPeerId = reader.ReadUShort();
|
||||||
|
var player = _playerCache.GetPlayerByPeer(playerPeerId);
|
||||||
|
|
||||||
|
if (player != null)
|
||||||
|
{
|
||||||
|
var changes = player.UserData.Read(reader);
|
||||||
|
|
||||||
|
_listenerList.OnPlayerUserData(player, changes);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
RagonLog.Warn("Received user data for unknown player.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
|
internal class RoomDataHandler: IHandler
|
||||||
|
{
|
||||||
|
private readonly RagonListenerList _listeners;
|
||||||
|
private readonly RagonPlayerCache _playerCache;
|
||||||
|
|
||||||
|
public RoomDataHandler(
|
||||||
|
RagonPlayerCache playerCache,
|
||||||
|
RagonListenerList listeners)
|
||||||
|
{
|
||||||
|
_playerCache = playerCache;
|
||||||
|
_listeners = listeners;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Handle(RagonBuffer reader)
|
||||||
|
{
|
||||||
|
var rawData = reader.RawData;
|
||||||
|
var peerId = (ushort)(rawData[1] + (rawData[2] << 8));
|
||||||
|
|
||||||
|
RagonPlayer player = null;
|
||||||
|
if (peerId != 10000)
|
||||||
|
{
|
||||||
|
player = _playerCache.GetPlayerByPeer(peerId);
|
||||||
|
if (player == null)
|
||||||
|
{
|
||||||
|
RagonLog.Error($"Player with peerId:{peerId} not found");
|
||||||
|
|
||||||
|
_playerCache.Dump();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var headerSize = 3;
|
||||||
|
var payload = new byte[rawData.Length - headerSize];
|
||||||
|
|
||||||
|
Array.Copy(rawData, headerSize, payload, 0, payload.Length);
|
||||||
|
|
||||||
|
_listeners.OnData(player, payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
|
public class RoomEventHandler : IHandler
|
||||||
|
{
|
||||||
|
private readonly RagonClient _client;
|
||||||
|
private readonly RagonPlayerCache _playerCache;
|
||||||
|
|
||||||
|
public RoomEventHandler(
|
||||||
|
RagonClient client,
|
||||||
|
RagonPlayerCache playerCache
|
||||||
|
)
|
||||||
|
{
|
||||||
|
_client = client;
|
||||||
|
_playerCache = playerCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Handle(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
var eventCode = buffer.ReadUShort();
|
||||||
|
var peerId = buffer.ReadUShort();
|
||||||
|
var executionMode = (RagonReplicationMode)buffer.ReadByte();
|
||||||
|
|
||||||
|
var player = _playerCache.GetPlayerByPeer(peerId);
|
||||||
|
if (player == null)
|
||||||
|
{
|
||||||
|
RagonLog.Error($"Player with peerId:{peerId} not found as owner of event with code:{eventCode}");
|
||||||
|
|
||||||
|
_playerCache.Dump();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_client.Room.HandleEvent(eventCode, player, buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
|
internal class RoomListHandler: IHandler
|
||||||
|
{
|
||||||
|
private RagonListenerList _listenerList;
|
||||||
|
private RagonSession _session;
|
||||||
|
|
||||||
|
public RoomListHandler(RagonSession session, RagonListenerList list)
|
||||||
|
{
|
||||||
|
_session = session;
|
||||||
|
_listenerList = list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Handle(RagonBuffer reader)
|
||||||
|
{
|
||||||
|
var roomCount = reader.ReadUShort();
|
||||||
|
var roomList = new RagonRoomInformation[roomCount];
|
||||||
|
|
||||||
|
for (int i = 0; i < roomCount; i++)
|
||||||
|
{
|
||||||
|
var id = reader.ReadString();
|
||||||
|
var scene = reader.ReadString();
|
||||||
|
var maxPlayers = reader.ReadUShort();
|
||||||
|
var minPlayers = reader.ReadUShort();
|
||||||
|
var players = reader.ReadUShort();
|
||||||
|
|
||||||
|
var roomInfo = new RagonRoomInformation()
|
||||||
|
{
|
||||||
|
Id = id,
|
||||||
|
Scene = scene,
|
||||||
|
PlayerCount = players,
|
||||||
|
PlayerMax = maxPlayers,
|
||||||
|
PlayerMin = minPlayers,
|
||||||
|
Properties = new Dictionary<string, byte[]>()
|
||||||
|
};
|
||||||
|
|
||||||
|
roomList[i] = roomInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
_listenerList.OnRoomList(roomList);
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
-14
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -14,25 +14,25 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
using ENet;
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client
|
||||||
|
|
||||||
public sealed class NullUnreliableChannel : INetworkChannel
|
|
||||||
{
|
{
|
||||||
private Peer _peer;
|
internal class RoomUserDataHandler : IHandler
|
||||||
private byte _channelId;
|
|
||||||
|
|
||||||
public NullUnreliableChannel(Peer peer, int channelId)
|
|
||||||
{
|
{
|
||||||
_peer = peer;
|
private readonly RagonClient _client;
|
||||||
_channelId = (byte) channelId;
|
private readonly RagonListenerList _listenerList;
|
||||||
|
|
||||||
|
public RoomUserDataHandler(RagonClient client, RagonListenerList listenerList)
|
||||||
|
{
|
||||||
|
_client = client;
|
||||||
|
_listenerList = listenerList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Send(byte[] data)
|
public void Handle(RagonBuffer reader)
|
||||||
{
|
{
|
||||||
|
var changes = _client.Room?.UserData.Read(reader);
|
||||||
|
_listenerList.OnRoomUserData(changes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -20,7 +20,7 @@ using Ragon.Protocol;
|
|||||||
|
|
||||||
namespace Ragon.Client;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
internal class SnapshotHandler : Handler
|
internal class SnapshotHandler : IHandler
|
||||||
{
|
{
|
||||||
private readonly IRagonEntityListener _entityListener;
|
private readonly IRagonEntityListener _entityListener;
|
||||||
private readonly RagonClient _client;
|
private readonly RagonClient _client;
|
||||||
@@ -45,19 +45,7 @@ internal class SnapshotHandler : Handler
|
|||||||
|
|
||||||
public void Handle(RagonBuffer buffer)
|
public void Handle(RagonBuffer buffer)
|
||||||
{
|
{
|
||||||
var playersCount = buffer.ReadUShort();
|
var entities = new List<RagonEntity>();
|
||||||
RagonLog.Trace("Players: " + playersCount);
|
|
||||||
for (var i = 0; i < playersCount; i++)
|
|
||||||
{
|
|
||||||
var playerPeerId = buffer.ReadUShort();
|
|
||||||
var playerId = buffer.ReadString();
|
|
||||||
var playerName = buffer.ReadString();
|
|
||||||
|
|
||||||
_playerCache.AddPlayer(playerPeerId, playerId, playerName);
|
|
||||||
|
|
||||||
RagonLog.Trace($"Player {playerPeerId} - {playerId} - {playerName}");
|
|
||||||
}
|
|
||||||
|
|
||||||
var dynamicEntities = buffer.ReadUShort();
|
var dynamicEntities = buffer.ReadUShort();
|
||||||
RagonLog.Trace("Dynamic Entities: " + dynamicEntities);
|
RagonLog.Trace("Dynamic Entities: " + dynamicEntities);
|
||||||
for (var i = 0; i < dynamicEntities; i++)
|
for (var i = 0; i < dynamicEntities; i++)
|
||||||
@@ -70,25 +58,28 @@ internal class SnapshotHandler : Handler
|
|||||||
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
|
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
|
||||||
if (player == null)
|
if (player == null)
|
||||||
{
|
{
|
||||||
RagonLog.Error($"Player not found with peerId: ${ownerPeerId}");
|
RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
|
||||||
|
|
||||||
|
_playerCache.Dump();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var hasAuthority = _playerCache.Local.Id == player.Id;
|
var hasAuthority = _playerCache.Local.Id == player.Id;
|
||||||
var entity = _entityCache.TryGetEntity(0, entityType, 0, entityId, hasAuthority, out _);
|
var entity = _entityCache.TryGetEntity(0, entityType, 0, entityId, hasAuthority, out _);
|
||||||
|
var payload = RagonPayload.Empty;
|
||||||
if (payloadSize > 0)
|
if (payloadSize > 0)
|
||||||
{
|
{
|
||||||
var payload = new RagonPayload(payloadSize);
|
payload = new RagonPayload(payloadSize);
|
||||||
payload.Read(buffer);
|
payload.Read(buffer);
|
||||||
|
|
||||||
entity.AttachPayload(payload);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload);
|
||||||
|
|
||||||
_entityListener.OnEntityCreated(entity);
|
_entityListener.OnEntityCreated(entity);
|
||||||
|
|
||||||
entity.Read(buffer);
|
entity.Read(buffer);
|
||||||
entity.Attach(_client, entityId, entityType, hasAuthority, player);
|
|
||||||
|
entities.Add(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
var staticEntities = buffer.ReadUShort();
|
var staticEntities = buffer.ReadUShort();
|
||||||
@@ -104,22 +95,23 @@ internal class SnapshotHandler : Handler
|
|||||||
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
|
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
|
||||||
if (player == null)
|
if (player == null)
|
||||||
{
|
{
|
||||||
RagonLog.Error($"Player not found with peerId: ${ownerPeerId}");
|
RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
|
||||||
|
|
||||||
|
_playerCache.Dump();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var hasAuthority = _playerCache.Local.Id == player.Id;
|
var hasAuthority = _playerCache.Local.Id == player.Id;
|
||||||
var entity = _entityCache.TryGetEntity(0, entityType, staticId, entityId, hasAuthority, out _);
|
var entity = _entityCache.TryGetEntity(0, entityType, staticId, entityId, hasAuthority, out _);
|
||||||
|
|
||||||
|
entity.Prepare(_client, entityId, entityType, hasAuthority, player, RagonPayload.Empty);
|
||||||
entity.Read(buffer);
|
entity.Read(buffer);
|
||||||
entity.Attach(_client, entityId, entityType, hasAuthority, player);
|
|
||||||
|
entities.Add(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_client.Status == RagonStatus.LOBBY)
|
foreach (var entity in entities)
|
||||||
{
|
entity.Attach();
|
||||||
_client.SetStatus(RagonStatus.ROOM);
|
|
||||||
_listenerList.OnJoined();
|
|
||||||
}
|
|
||||||
|
|
||||||
_listenerList.OnSceneLoaded();
|
_listenerList.OnSceneLoaded();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
|
public class TimestampHandler: IHandler
|
||||||
|
{
|
||||||
|
private readonly RagonClient _client;
|
||||||
|
public TimestampHandler(RagonClient client)
|
||||||
|
{
|
||||||
|
_client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Handle(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
var timestamp0 = buffer.Read(32);
|
||||||
|
var timestamp1 = buffer.Read(32);
|
||||||
|
var value = new DoubleToUInt { Int0 = timestamp0, Int1 = timestamp1 };
|
||||||
|
|
||||||
|
_client.SetTimestamp(value.Double);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,5 +18,5 @@ namespace Ragon.Client;
|
|||||||
|
|
||||||
public interface IRagonConnection
|
public interface IRagonConnection
|
||||||
{
|
{
|
||||||
|
public void Close();
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,9 +16,12 @@
|
|||||||
|
|
||||||
using Ragon.Protocol;
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Server.Handler;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
public interface IRagonOperation
|
public interface IUserData
|
||||||
{
|
{
|
||||||
public void Handle(RagonContext context, RagonBuffer reader, RagonBuffer writer);
|
public byte[] this[string key] { get; set; }
|
||||||
|
bool Dirty { get; }
|
||||||
|
IReadOnlyList<string> Read(RagonBuffer buffer);
|
||||||
|
void Write(RagonBuffer buffer);
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+4
-6
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -14,11 +14,9 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
namespace Ragon.Server.Plugin.Web;
|
public interface IRagonDataListener
|
||||||
|
|
||||||
[Serializable]
|
|
||||||
public class RoomRemovedRequest
|
|
||||||
{
|
{
|
||||||
public RoomDto Room { get; set; }
|
public void OnData(RagonClient client, RagonPlayer player, byte[] data);
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -25,7 +25,10 @@ namespace Ragon.Client
|
|||||||
IRagonSceneListener,
|
IRagonSceneListener,
|
||||||
IRagonOwnershipChangedListener,
|
IRagonOwnershipChangedListener,
|
||||||
IRagonPlayerJoinListener,
|
IRagonPlayerJoinListener,
|
||||||
IRagonPlayerLeftListener
|
IRagonPlayerLeftListener,
|
||||||
|
IRagonRoomListListener,
|
||||||
|
IRagonRoomUserDataListener,
|
||||||
|
IRagonPlayerUserDataListener
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+4
-6
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -14,11 +14,9 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Ragon.Server.Plugin.Web;
|
namespace Ragon.Client;
|
||||||
|
|
||||||
[Serializable]
|
public interface IRagonPlayerUserDataListener
|
||||||
public class RoomCreatedRequest
|
|
||||||
{
|
{
|
||||||
public RoomDto Room { get; set; }
|
void OnPlayerUserDataUpdated(RagonClient client, RagonPlayer player, IReadOnlyList<string> changes);
|
||||||
public PlayerDto Player { get; set; }
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
|
public interface IRagonRoomListListener
|
||||||
|
{
|
||||||
|
public void OnRoomListUpdate(RagonClient client, IReadOnlyList<RagonRoomInformation> roomsInfos);
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
|
public interface IRagonRoomUserDataListener
|
||||||
|
{
|
||||||
|
public void OnUserDataUpdated(RagonClient client, IReadOnlyList<string> changes);
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -24,20 +24,22 @@ namespace Ragon.Client
|
|||||||
private readonly NetworkStatistics _stats;
|
private readonly NetworkStatistics _stats;
|
||||||
private IRagonEntityListener _entityListener;
|
private IRagonEntityListener _entityListener;
|
||||||
private IRagonSceneCollector _sceneCollector;
|
private IRagonSceneCollector _sceneCollector;
|
||||||
private Handler[] _handlers;
|
private IHandler[] _handlers;
|
||||||
private RagonBuffer _readBuffer;
|
private RagonBuffer _readBuffer;
|
||||||
private RagonBuffer _writeBuffer;
|
private RagonBuffer _writeBuffer;
|
||||||
private RagonRoom _room;
|
private RagonRoom _room;
|
||||||
private RagonSession _session;
|
private RagonSession _session;
|
||||||
private RagonListenerList listeners;
|
private RagonListenerList _listeners;
|
||||||
private RagonPlayerCache _playerCache;
|
private RagonPlayerCache _playerCache;
|
||||||
private RagonEntityCache _entityCache;
|
private RagonEntityCache _entityCache;
|
||||||
private RagonEventCache _eventCache;
|
private RagonEventCache _eventCache;
|
||||||
private RagonStatus _status;
|
private RagonStatus _status;
|
||||||
|
|
||||||
|
private double _serverTimestamp;
|
||||||
private float _replicationRate = 0;
|
private float _replicationRate = 0;
|
||||||
private float _replicationTime = 0;
|
private float _replicationTime = 0;
|
||||||
|
|
||||||
|
public double ServerTimestamp => _serverTimestamp;
|
||||||
public IRagonConnection Connection => _connection;
|
public IRagonConnection Connection => _connection;
|
||||||
public RagonStatus Status => _status;
|
public RagonStatus Status => _status;
|
||||||
public RagonSession Session => _session;
|
public RagonSession Session => _session;
|
||||||
@@ -54,13 +56,13 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
public RagonClient(INetworkConnection connection, int rate)
|
public RagonClient(INetworkConnection connection, int rate)
|
||||||
{
|
{
|
||||||
|
_listeners = new RagonListenerList(this);
|
||||||
|
|
||||||
_connection = connection;
|
_connection = connection;
|
||||||
_connection.OnData += OnData;
|
_connection.OnData += OnData;
|
||||||
_connection.OnConnected += OnConnected;
|
_connection.OnConnected += OnConnected;
|
||||||
_connection.OnDisconnected += OnDisconnected;
|
_connection.OnDisconnected += OnDisconnected;
|
||||||
|
|
||||||
listeners = new RagonListenerList(this);
|
|
||||||
|
|
||||||
_replicationRate = (1000.0f / rate) / 1000.0f;
|
_replicationRate = (1000.0f / rate) / 1000.0f;
|
||||||
_replicationTime = 0;
|
_replicationTime = 0;
|
||||||
|
|
||||||
@@ -96,27 +98,37 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
_writeBuffer = new RagonBuffer();
|
_writeBuffer = new RagonBuffer();
|
||||||
_readBuffer = new RagonBuffer();
|
_readBuffer = new RagonBuffer();
|
||||||
_session = new RagonSession(this, _readBuffer);
|
|
||||||
|
|
||||||
_playerCache = new RagonPlayerCache();
|
_playerCache = new RagonPlayerCache();
|
||||||
|
_session = new RagonSession(this, _writeBuffer);
|
||||||
_entityCache = new RagonEntityCache(this, _playerCache, _sceneCollector);
|
_entityCache = new RagonEntityCache(this, _playerCache, _sceneCollector);
|
||||||
|
|
||||||
_handlers = new Handler[byte.MaxValue];
|
_handlers = new IHandler[byte.MaxValue];
|
||||||
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, listeners);
|
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, _listeners);
|
||||||
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(listeners);
|
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(_listeners);
|
||||||
_handlers[(byte)RagonOperation.JOIN_SUCCESS] = new JoinSuccessHandler(this, _readBuffer, listeners, _playerCache, _entityCache);
|
_handlers[(byte)RagonOperation.JOIN_SUCCESS] =
|
||||||
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(listeners);
|
new JoinSuccessHandler(this, _listeners, _playerCache, _entityCache);
|
||||||
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, listeners, _entityCache);
|
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listeners);
|
||||||
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] = new OwnershipRoomHandler(listeners, _playerCache, _entityCache);
|
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listeners, _entityCache);
|
||||||
_handlers[(byte)RagonOperation.OWNERSHIP_ENTITY_CHANGED] = new EntityOwnershipHandler(listeners, _playerCache, _entityCache);
|
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] =
|
||||||
_handlers[(byte)RagonOperation.PLAYER_JOINED] = new PlayerJoinHandler(_playerCache, listeners);
|
new OwnershipRoomHandler(_listeners, _playerCache, _entityCache);
|
||||||
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_entityCache, _playerCache, listeners);
|
_handlers[(byte)RagonOperation.OWNERSHIP_ENTITY_CHANGED] =
|
||||||
_handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, listeners);
|
new EntityOwnershipHandler(_listeners, _playerCache, _entityCache);
|
||||||
_handlers[(byte)RagonOperation.CREATE_ENTITY] = new EntityCreateHandler(this, _playerCache, _entityCache, _entityListener);
|
_handlers[(byte)RagonOperation.PLAYER_JOINED] = new PlayerJoinHandler(_playerCache, _listeners);
|
||||||
|
_handlers[(byte)RagonOperation.PLAYER_LEAVED] = new PlayerLeftHandler(_entityCache, _playerCache, _listeners);
|
||||||
|
_handlers[(byte)RagonOperation.LOAD_SCENE] = new SceneLoadHandler(this, _listeners);
|
||||||
|
_handlers[(byte)RagonOperation.CREATE_ENTITY] =
|
||||||
|
new EntityCreateHandler(this, _playerCache, _entityCache, _entityListener);
|
||||||
_handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityRemoveHandler(_entityCache);
|
_handlers[(byte)RagonOperation.REMOVE_ENTITY] = new EntityRemoveHandler(_entityCache);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_STATE] = new StateEntityHandler(_entityCache);
|
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_STATE] = new StateEntityHandler(_entityCache);
|
||||||
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventHandler(this, _playerCache, _entityCache);
|
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventHandler(_playerCache, _entityCache);
|
||||||
_handlers[(byte)RagonOperation.SNAPSHOT] = new SnapshotHandler(this, listeners, _entityCache, _playerCache, _entityListener);
|
_handlers[(byte)RagonOperation.REPLICATE_ROOM_EVENT] = new RoomEventHandler(this, _playerCache);
|
||||||
|
_handlers[(byte)RagonOperation.SNAPSHOT] =
|
||||||
|
new SnapshotHandler(this, _listeners, _entityCache, _playerCache, _entityListener);
|
||||||
|
_handlers[(byte)RagonOperation.TIMESTAMP_SYNCHRONIZATION] = new TimestampHandler(this);
|
||||||
|
_handlers[(byte)RagonOperation.REPLICATE_RAW_DATA] = new RoomDataHandler(_playerCache, _listeners);
|
||||||
|
_handlers[(byte)RagonOperation.ROOM_LIST_UPDATED] = new RoomListHandler(_session, _listeners);
|
||||||
|
_handlers[(byte)RagonOperation.ROOM_DATA_UPDATED] = new RoomUserDataHandler(this, _listeners);
|
||||||
|
_handlers[(byte)RagonOperation.PLAYER_DATA_UPDATED] = new PlayerUserDataHandler(_playerCache, _listeners);
|
||||||
|
|
||||||
var protocolRaw = RagonVersion.Parse(protocol);
|
var protocolRaw = RagonVersion.Parse(protocol);
|
||||||
_connection.Connect(address, port, protocolRaw);
|
_connection.Connect(address, port, protocolRaw);
|
||||||
@@ -138,47 +150,62 @@ namespace Ragon.Client
|
|||||||
_replicationTime += dt;
|
_replicationTime += dt;
|
||||||
if (_replicationTime >= _replicationRate)
|
if (_replicationTime >= _replicationRate)
|
||||||
{
|
{
|
||||||
_entityCache.WriteState(_readBuffer);
|
|
||||||
_replicationTime = 0;
|
_replicationTime = 0;
|
||||||
|
_entityCache.WriteState(_writeBuffer);
|
||||||
|
|
||||||
|
SendTimestamp();
|
||||||
|
SendRoomUserData();
|
||||||
|
SendPlayerUserData();
|
||||||
}
|
}
|
||||||
|
|
||||||
_stats.Update(_connection.BytesSent, _connection.BytesReceived, _connection.Ping, dt);
|
_stats.Update(_connection.BytesSent, _connection.BytesReceived, _connection.Ping, dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
listeners.Update();
|
_listeners.Update();
|
||||||
_connection.Update();
|
_connection.Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
{
|
||||||
|
if (_status != RagonStatus.DISCONNECTED)
|
||||||
{
|
{
|
||||||
_status = RagonStatus.DISCONNECTED;
|
_status = RagonStatus.DISCONNECTED;
|
||||||
_connection.Disconnect();
|
_connection.Disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
_connection.Dispose();
|
_connection.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddListener(IRagonListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonAuthorizationListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonAuthorizationListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonConnectionListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonConnectionListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonFailedListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonFailedListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonJoinListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonJoinListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonLeftListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonLeftListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonOwnershipChangedListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonOwnershipChangedListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonPlayerJoinListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonPlayerJoinListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonPlayerLeftListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonPlayerLeftListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonSceneListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonSceneListener listener) => _listeners.Add(listener);
|
||||||
public void AddListener(IRagonSceneRequestListener listener) => listeners.Add(listener);
|
public void AddListener(IRagonSceneRequestListener listener) => _listeners.Add(listener);
|
||||||
|
public void AddListener(IRagonDataListener listener) => _listeners.Add(listener);
|
||||||
public void RemoveListener(IRagonListener listener) => listeners.Remove(listener);
|
public void AddListener(IRagonRoomListListener listener) => _listeners.Add(listener);
|
||||||
public void RemoveListener(IRagonAuthorizationListener listener) => listeners.Remove(listener);
|
public void AddListener(IRagonPlayerUserDataListener listener) => _listeners.Add(listener);
|
||||||
public void RemoveListener(IRagonConnectionListener listener) => listeners.Remove(listener);
|
public void AddListener(IRagonRoomUserDataListener listener) => _listeners.Add(listener);
|
||||||
public void RemoveListener(IRagonFailedListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonJoinListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonAuthorizationListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonLeftListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonConnectionListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonOwnershipChangedListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonFailedListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonPlayerJoinListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonJoinListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonPlayerLeftListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonLeftListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonSceneListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonOwnershipChangedListener listener) => _listeners.Remove(listener);
|
||||||
public void RemoveListener(IRagonSceneRequestListener listener) => listeners.Remove(listener);
|
public void RemoveListener(IRagonPlayerJoinListener listener) => _listeners.Remove(listener);
|
||||||
|
public void RemoveListener(IRagonPlayerLeftListener listener) => _listeners.Remove(listener);
|
||||||
|
public void RemoveListener(IRagonSceneListener listener) => _listeners.Remove(listener);
|
||||||
|
public void RemoveListener(IRagonSceneRequestListener listener) => _listeners.Remove(listener);
|
||||||
|
public void RemoveListener(IRagonDataListener listener) => _listeners.Remove(listener);
|
||||||
|
public void RemoveListener(IRagonRoomListListener listener) => _listeners.Remove(listener);
|
||||||
|
public void RemoveListener(IRagonRoomUserDataListener listener) => _listeners.Remove(listener);
|
||||||
|
public void RemoveListener(IRagonPlayerUserDataListener listener) => _listeners.Remove(listener);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -186,6 +213,7 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
internal void AssignRoom(RagonRoom room)
|
internal void AssignRoom(RagonRoom room)
|
||||||
{
|
{
|
||||||
|
_room?.Dispose();
|
||||||
_room = room;
|
_room = room;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,15 +222,66 @@ namespace Ragon.Client
|
|||||||
_status = status;
|
_status = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void SetTimestamp(double time)
|
||||||
|
{
|
||||||
|
_serverTimestamp = time;
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region PRIVATE
|
#region PRIVATE
|
||||||
|
|
||||||
|
private void SendTimestamp()
|
||||||
|
{
|
||||||
|
var timestamp = RagonTime.CurrentTimestamp();
|
||||||
|
var value = new DoubleToUInt()
|
||||||
|
{
|
||||||
|
Double = timestamp,
|
||||||
|
};
|
||||||
|
|
||||||
|
_writeBuffer.Clear();
|
||||||
|
_writeBuffer.WriteOperation(RagonOperation.TIMESTAMP_SYNCHRONIZATION);
|
||||||
|
_writeBuffer.Write(value.Int0, 32);
|
||||||
|
_writeBuffer.Write(value.Int1, 32);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SendRoomUserData()
|
||||||
|
{
|
||||||
|
if (_room == null) return;
|
||||||
|
|
||||||
|
var props = _room.UserData;
|
||||||
|
if (!props.Dirty) return;
|
||||||
|
|
||||||
|
_writeBuffer.Clear();
|
||||||
|
_writeBuffer.WriteOperation(RagonOperation.ROOM_DATA_UPDATED);
|
||||||
|
|
||||||
|
props.Write(_writeBuffer);
|
||||||
|
|
||||||
|
var sendData = _writeBuffer.ToArray();
|
||||||
|
_connection.Reliable.Send(sendData);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SendPlayerUserData()
|
||||||
|
{
|
||||||
|
if (_playerCache.Local == null) return;
|
||||||
|
|
||||||
|
var props = _playerCache.Local.UserData;
|
||||||
|
if (!props.Dirty) return;
|
||||||
|
|
||||||
|
_writeBuffer.Clear();
|
||||||
|
_writeBuffer.WriteOperation(RagonOperation.PLAYER_DATA_UPDATED);
|
||||||
|
|
||||||
|
props.Write(_writeBuffer);
|
||||||
|
|
||||||
|
var sendData = _writeBuffer.ToArray();
|
||||||
|
_connection.Reliable.Send(sendData);
|
||||||
|
}
|
||||||
|
|
||||||
private void OnConnected()
|
private void OnConnected()
|
||||||
{
|
{
|
||||||
RagonLog.Trace("Connected");
|
RagonLog.Trace("Connected");
|
||||||
|
|
||||||
listeners.OnConnected();
|
_listeners.OnConnected();
|
||||||
_status = RagonStatus.CONNECTED;
|
_status = RagonStatus.CONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,11 +289,11 @@ namespace Ragon.Client
|
|||||||
{
|
{
|
||||||
RagonLog.Trace($"Disconnected: {reason}");
|
RagonLog.Trace($"Disconnected: {reason}");
|
||||||
|
|
||||||
listeners.OnDisconnected(reason);
|
_listeners.OnDisconnected(reason);
|
||||||
_status = RagonStatus.DISCONNECTED;
|
_status = RagonStatus.DISCONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnData(byte[] data)
|
private void OnData(byte[] data)
|
||||||
{
|
{
|
||||||
_readBuffer.Clear();
|
_readBuffer.Clear();
|
||||||
_readBuffer.FromArray(data);
|
_readBuffer.FromArray(data);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -83,12 +83,14 @@ public sealed class RagonEntityCache
|
|||||||
|
|
||||||
public void Destroy(RagonEntity entity, RagonPayload destroyPayload)
|
public void Destroy(RagonEntity entity, RagonPayload destroyPayload)
|
||||||
{
|
{
|
||||||
if (!entity.IsAttached)
|
if (!entity.IsAttached && !entity.HasAuthority)
|
||||||
{
|
{
|
||||||
RagonLog.Warn("Can't destroy object, he is not created");
|
RagonLog.Warn("Can't destroy object");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
entity.SetReplication(false);
|
||||||
|
|
||||||
var buffer = _client.Buffer;
|
var buffer = _client.Buffer;
|
||||||
|
|
||||||
buffer.Clear();
|
buffer.Clear();
|
||||||
@@ -114,7 +116,7 @@ public sealed class RagonEntityCache
|
|||||||
foreach (var ent in _entityList)
|
foreach (var ent in _entityList)
|
||||||
{
|
{
|
||||||
if (!ent.IsAttached ||
|
if (!ent.IsAttached ||
|
||||||
!ent.Replication ||
|
!ent.IsReplicated ||
|
||||||
!ent.PropertiesChanged) continue;
|
!ent.PropertiesChanged) continue;
|
||||||
|
|
||||||
ent.Write(buffer);
|
ent.Write(buffer);
|
||||||
@@ -184,12 +186,10 @@ public sealed class RagonEntityCache
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_pendingEntities.TryGetValue(attachId, out var pendingEntity))
|
if (_pendingEntities.TryGetValue(attachId, out var pendingEntity) && hasAuthority)
|
||||||
{
|
{
|
||||||
_pendingEntities.Remove(attachId);
|
_pendingEntities.Remove(attachId);
|
||||||
_entityMap.Add(entityId, pendingEntity);
|
_entityMap.Add(entityId, pendingEntity);
|
||||||
|
|
||||||
if (hasAuthority)
|
|
||||||
_entityList.Add(pendingEntity);
|
_entityList.Add(pendingEntity);
|
||||||
|
|
||||||
hasCreated = false;
|
hasCreated = false;
|
||||||
@@ -197,7 +197,6 @@ public sealed class RagonEntityCache
|
|||||||
return pendingEntity;
|
return pendingEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var entity = new RagonEntity(entityType, sceneId);
|
var entity = new RagonEntity(entityType, sceneId);
|
||||||
|
|
||||||
_entityMap.Add(entityId, entity);
|
_entityMap.Add(entityId, entity);
|
||||||
@@ -219,6 +218,7 @@ public sealed class RagonEntityCache
|
|||||||
_entityList.Remove(entity);
|
_entityList.Remove(entity);
|
||||||
|
|
||||||
entity.Detach(payload);
|
entity.Detach(payload);
|
||||||
|
entity.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -31,6 +31,10 @@ namespace Ragon.Client
|
|||||||
private readonly List<IRagonOwnershipChangedListener> _ownershipChangedListeners = new();
|
private readonly List<IRagonOwnershipChangedListener> _ownershipChangedListeners = new();
|
||||||
private readonly List<IRagonPlayerJoinListener> _playerJoinListeners = new();
|
private readonly List<IRagonPlayerJoinListener> _playerJoinListeners = new();
|
||||||
private readonly List<IRagonPlayerLeftListener> _playerLeftListeners = new();
|
private readonly List<IRagonPlayerLeftListener> _playerLeftListeners = new();
|
||||||
|
private readonly List<IRagonDataListener> _dataListeners = new();
|
||||||
|
private readonly List<IRagonRoomListListener> _roomListListeners = new();
|
||||||
|
private readonly List<IRagonRoomUserDataListener> _roomUserDataListeners = new();
|
||||||
|
private readonly List<IRagonPlayerUserDataListener> _playerUserDataListeners = new();
|
||||||
private readonly List<Action> _delayedActions = new();
|
private readonly List<Action> _delayedActions = new();
|
||||||
|
|
||||||
public RagonListenerList(RagonClient client)
|
public RagonListenerList(RagonClient client)
|
||||||
@@ -49,6 +53,8 @@ namespace Ragon.Client
|
|||||||
_ownershipChangedListeners.Add(listener);
|
_ownershipChangedListeners.Add(listener);
|
||||||
_playerJoinListeners.Add(listener);
|
_playerJoinListeners.Add(listener);
|
||||||
_playerLeftListeners.Add(listener);
|
_playerLeftListeners.Add(listener);
|
||||||
|
_roomUserDataListeners.Add(listener);
|
||||||
|
_playerUserDataListeners.Add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Remove(IRagonListener listener)
|
public void Remove(IRagonListener listener)
|
||||||
@@ -64,6 +70,8 @@ namespace Ragon.Client
|
|||||||
_ownershipChangedListeners.Remove(listener);
|
_ownershipChangedListeners.Remove(listener);
|
||||||
_playerJoinListeners.Remove(listener);
|
_playerJoinListeners.Remove(listener);
|
||||||
_playerLeftListeners.Remove(listener);
|
_playerLeftListeners.Remove(listener);
|
||||||
|
_roomUserDataListeners.Remove(listener);
|
||||||
|
_playerUserDataListeners.Remove(listener);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,6 +83,10 @@ namespace Ragon.Client
|
|||||||
_delayedActions.Clear();
|
_delayedActions.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Add(IRagonDataListener dataListener)
|
||||||
|
{
|
||||||
|
_dataListeners.Add(dataListener);
|
||||||
|
}
|
||||||
|
|
||||||
public void Add(IRagonAuthorizationListener listener)
|
public void Add(IRagonAuthorizationListener listener)
|
||||||
{
|
{
|
||||||
@@ -126,6 +138,26 @@ namespace Ragon.Client
|
|||||||
_playerLeftListeners.Add(listener);
|
_playerLeftListeners.Add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Add(IRagonRoomListListener listener)
|
||||||
|
{
|
||||||
|
_roomListListeners.Add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(IRagonRoomUserDataListener listener)
|
||||||
|
{
|
||||||
|
_roomUserDataListeners.Add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(IRagonPlayerUserDataListener listener)
|
||||||
|
{
|
||||||
|
_playerUserDataListeners.Add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(IRagonDataListener listener)
|
||||||
|
{
|
||||||
|
_delayedActions.Add(() => _dataListeners.Remove(listener));
|
||||||
|
}
|
||||||
|
|
||||||
public void Remove(IRagonSceneRequestListener listener)
|
public void Remove(IRagonSceneRequestListener listener)
|
||||||
{
|
{
|
||||||
_delayedActions.Add(() => _sceneRequestListeners.Remove(listener));
|
_delayedActions.Add(() => _sceneRequestListeners.Remove(listener));
|
||||||
@@ -138,7 +170,6 @@ namespace Ragon.Client
|
|||||||
|
|
||||||
public void Remove(IRagonConnectionListener listener)
|
public void Remove(IRagonConnectionListener listener)
|
||||||
{
|
{
|
||||||
|
|
||||||
_delayedActions.Add(() => _connectionListeners.Remove(listener));
|
_delayedActions.Add(() => _connectionListeners.Remove(listener));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,6 +208,21 @@ namespace Ragon.Client
|
|||||||
_delayedActions.Add(() => _playerLeftListeners.Remove(listener));
|
_delayedActions.Add(() => _playerLeftListeners.Remove(listener));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Remove(IRagonRoomListListener listener)
|
||||||
|
{
|
||||||
|
_delayedActions.Add(() => _roomListListeners.Remove(listener));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(IRagonRoomUserDataListener listener)
|
||||||
|
{
|
||||||
|
_delayedActions.Add(() => _roomUserDataListeners.Remove(listener));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(IRagonPlayerUserDataListener listener)
|
||||||
|
{
|
||||||
|
_delayedActions.Add(() => _playerUserDataListeners.Remove(listener));
|
||||||
|
}
|
||||||
|
|
||||||
public void OnAuthorizationSuccess(string playerId, string playerName, string payload)
|
public void OnAuthorizationSuccess(string playerId, string playerName, string payload)
|
||||||
{
|
{
|
||||||
foreach (var listener in _authorizationListeners)
|
foreach (var listener in _authorizationListeners)
|
||||||
@@ -248,5 +294,29 @@ namespace Ragon.Client
|
|||||||
foreach (var listener in _connectionListeners)
|
foreach (var listener in _connectionListeners)
|
||||||
listener.OnDisconnected(_client, disconnect);
|
listener.OnDisconnected(_client, disconnect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OnData(RagonPlayer player, byte[] data)
|
||||||
|
{
|
||||||
|
foreach (var listener in _dataListeners)
|
||||||
|
listener.OnData(_client, player, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnRoomList(RagonRoomInformation[] roomInfos)
|
||||||
|
{
|
||||||
|
foreach (var listListener in _roomListListeners)
|
||||||
|
listListener.OnRoomListUpdate(_client, roomInfos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnRoomUserData(IReadOnlyList<string> changes)
|
||||||
|
{
|
||||||
|
foreach (var userDataListener in _roomUserDataListeners)
|
||||||
|
userDataListener.OnUserDataUpdated(_client, changes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnPlayerUserData(RagonPlayer player, IReadOnlyList<string> changes)
|
||||||
|
{
|
||||||
|
foreach(var playerUserDataListener in _playerUserDataListeners)
|
||||||
|
playerUserDataListener.OnPlayerUserDataUpdated(_client, player, changes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -24,6 +24,7 @@ namespace Ragon.Client
|
|||||||
public ushort PeerId { get; set; }
|
public ushort PeerId { get; set; }
|
||||||
public bool IsRoomOwner { get; set; }
|
public bool IsRoomOwner { get; set; }
|
||||||
public bool IsLocal { get; set; }
|
public bool IsLocal { get; set; }
|
||||||
|
public IUserData UserData { get; private set; }
|
||||||
|
|
||||||
public RagonPlayer(ushort peerId, string playerId, string name, bool isRoomOwner, bool isLocal)
|
public RagonPlayer(ushort peerId, string playerId, string name, bool isRoomOwner, bool isLocal)
|
||||||
{
|
{
|
||||||
@@ -32,6 +33,7 @@ namespace Ragon.Client
|
|||||||
IsLocal = isLocal;
|
IsLocal = isLocal;
|
||||||
Name = name;
|
Name = name;
|
||||||
Id = playerId;
|
Id = playerId;
|
||||||
|
UserData = isLocal ? new RagonUserData() : new RagonUserDataReadOnly();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -27,8 +27,21 @@ public sealed class RagonPlayerCache
|
|||||||
public RagonPlayer Local { get; private set; }
|
public RagonPlayer Local { get; private set; }
|
||||||
public bool IsRoomOwner => _ownerId == _localId;
|
public bool IsRoomOwner => _ownerId == _localId;
|
||||||
|
|
||||||
public RagonPlayer? GetPlayerById(string playerId) => _playersById[playerId];
|
public RagonPlayer? GetPlayerById(string playerId)
|
||||||
public RagonPlayer? GetPlayerByPeer(ushort peerId) => _playersByConnection[peerId];
|
{
|
||||||
|
if (_playersById.TryGetValue(playerId, out var player))
|
||||||
|
return player;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RagonPlayer? GetPlayerByPeer(ushort peerId)
|
||||||
|
{
|
||||||
|
if (_playersByConnection.TryGetValue(peerId, out var player))
|
||||||
|
return player;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private string _ownerId;
|
private string _ownerId;
|
||||||
private string _localId;
|
private string _localId;
|
||||||
@@ -39,10 +52,10 @@ public sealed class RagonPlayerCache
|
|||||||
_localId = localId;
|
_localId = localId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddPlayer(ushort peerId, string playerId, string playerName)
|
public RagonPlayer AddPlayer(ushort peerId, string playerId, string playerName)
|
||||||
{
|
{
|
||||||
if (_playersById.ContainsKey(playerId))
|
if (_playersById.ContainsKey(playerId))
|
||||||
return;
|
return null;
|
||||||
|
|
||||||
var isOwner = playerId == _ownerId;
|
var isOwner = playerId == _ownerId;
|
||||||
var isLocal = playerId == _localId;
|
var isLocal = playerId == _localId;
|
||||||
@@ -60,6 +73,8 @@ public sealed class RagonPlayerCache
|
|||||||
_players.Add(player);
|
_players.Add(player);
|
||||||
_playersById.Add(player.Id, player);
|
_playersById.Add(player.Id, player);
|
||||||
_playersByConnection.Add(player.PeerId, player);
|
_playersByConnection.Add(player.PeerId, player);
|
||||||
|
|
||||||
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemovePlayer(string playerId)
|
public void RemovePlayer(string playerId)
|
||||||
@@ -91,4 +106,14 @@ public sealed class RagonPlayerCache
|
|||||||
_playersByConnection.Clear();
|
_playersByConnection.Clear();
|
||||||
_playersById.Clear();
|
_playersById.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dump()
|
||||||
|
{
|
||||||
|
RagonLog.Trace("Players: ");
|
||||||
|
RagonLog.Trace("[Connection] [ID] [Name]");
|
||||||
|
foreach (var player in _players)
|
||||||
|
{
|
||||||
|
RagonLog.Trace($"[{player.PeerId}] {player.Id} {player.Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -14,36 +14,78 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
namespace Ragon.Client
|
namespace Ragon.Client
|
||||||
{
|
{
|
||||||
public class RagonRoom
|
public class RagonRoom : IDisposable
|
||||||
{
|
{
|
||||||
private RagonClient _client;
|
private class EventSubscription : IDisposable
|
||||||
private RagonScene _scene;
|
{
|
||||||
private RagonEntityCache _entityCache;
|
private List<Action<RagonPlayer, IRagonEvent>> _callbacks;
|
||||||
private RagonPlayerCache _playerCache;
|
private List<Action<RagonPlayer, IRagonEvent>> _localCallbacks;
|
||||||
private RagonRoomInformation _information;
|
private Action<RagonPlayer, IRagonEvent> _callback;
|
||||||
|
|
||||||
public string Id => _information.RoomId;
|
public EventSubscription(
|
||||||
public int MinPlayers => _information.Min;
|
List<Action<RagonPlayer, IRagonEvent>> callbacks,
|
||||||
public int MaxPlayers => _information.Max;
|
List<Action<RagonPlayer, IRagonEvent>> localCallbacks,
|
||||||
|
Action<RagonPlayer, IRagonEvent> callback)
|
||||||
|
{
|
||||||
|
_callbacks = callbacks;
|
||||||
|
_localCallbacks = localCallbacks;
|
||||||
|
_callback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_callbacks?.Remove(_callback);
|
||||||
|
_localCallbacks?.Remove(_callback);
|
||||||
|
|
||||||
|
_callbacks = null!;
|
||||||
|
_localCallbacks = null!;
|
||||||
|
_callback = null!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer);
|
||||||
|
|
||||||
|
private readonly RagonClient _client;
|
||||||
|
private readonly RagonScene _scene;
|
||||||
|
private readonly RagonEntityCache _entityCache;
|
||||||
|
private readonly RagonPlayerCache _playerCache;
|
||||||
|
private readonly RoomParameters _parameters;
|
||||||
|
private readonly RagonUserData _userData;
|
||||||
|
|
||||||
|
public string Id => _parameters.RoomId;
|
||||||
|
public int MinPlayers => _parameters.Min;
|
||||||
|
public int MaxPlayers => _parameters.Max;
|
||||||
public string Scene => _scene.Name;
|
public string Scene => _scene.Name;
|
||||||
|
|
||||||
public IReadOnlyList<RagonPlayer> Players => _playerCache.Players;
|
public IReadOnlyList<RagonPlayer> Players => _playerCache.Players;
|
||||||
public RagonPlayer Local => _playerCache.Local;
|
public RagonPlayer Local => _playerCache.Local;
|
||||||
public RagonPlayer Owner => _playerCache.Owner;
|
public RagonPlayer Owner => _playerCache.Owner;
|
||||||
|
public RagonUserData UserData => _userData;
|
||||||
|
|
||||||
|
private readonly Dictionary<int, OnEventDelegate> _events = new Dictionary<int, OnEventDelegate>();
|
||||||
|
|
||||||
|
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _localListeners =
|
||||||
|
new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
|
||||||
|
|
||||||
|
private readonly Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>> _listeners =
|
||||||
|
new Dictionary<int, List<Action<RagonPlayer, IRagonEvent>>>();
|
||||||
|
|
||||||
public RagonRoom(RagonClient client,
|
public RagonRoom(RagonClient client,
|
||||||
RagonEntityCache entityCache,
|
RagonEntityCache entityCache,
|
||||||
RagonPlayerCache playerCache,
|
RagonPlayerCache playerCache,
|
||||||
RagonRoomInformation information,
|
RoomParameters parameters,
|
||||||
RagonScene scene)
|
RagonScene scene)
|
||||||
{
|
{
|
||||||
_client = client;
|
_client = client;
|
||||||
_information = information;
|
_parameters = parameters;
|
||||||
_entityCache = entityCache;
|
_entityCache = entityCache;
|
||||||
_playerCache = playerCache;
|
_playerCache = playerCache;
|
||||||
_scene = scene;
|
_scene = scene;
|
||||||
|
_userData = new RagonUserData();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Cleanup()
|
internal void Cleanup()
|
||||||
@@ -57,14 +99,79 @@ namespace Ragon.Client
|
|||||||
_scene.Update(sceneName);
|
_scene.Update(sceneName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void HandleEvent(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
if (_events.TryGetValue(eventCode, out var evnt))
|
||||||
|
evnt?.Invoke(caller, buffer);
|
||||||
|
else
|
||||||
|
RagonLog.Warn($"Handler event {Id} with eventCode {eventCode} not defined");
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void HandleUserData(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
_userData.Read(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IDisposable OnEvent<TEvent>(Action<RagonPlayer, TEvent> callback) where TEvent : IRagonEvent, new()
|
||||||
|
{
|
||||||
|
var t = new TEvent();
|
||||||
|
var eventCode = _client.Event.GetEventCode(t);
|
||||||
|
var action = (RagonPlayer player, IRagonEvent eventData) => callback.Invoke(player, (TEvent)eventData);
|
||||||
|
|
||||||
|
if (!_listeners.TryGetValue(eventCode, out var callbacks))
|
||||||
|
{
|
||||||
|
callbacks = new List<Action<RagonPlayer, IRagonEvent>>();
|
||||||
|
_listeners.Add(eventCode, callbacks);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_localListeners.TryGetValue(eventCode, out var localCallbacks))
|
||||||
|
{
|
||||||
|
localCallbacks = new List<Action<RagonPlayer, IRagonEvent>>();
|
||||||
|
_localListeners.Add(eventCode, localCallbacks);
|
||||||
|
}
|
||||||
|
|
||||||
|
callbacks.Add(action);
|
||||||
|
localCallbacks.Add(action);
|
||||||
|
|
||||||
|
if (!_events.ContainsKey(eventCode))
|
||||||
|
{
|
||||||
|
_events.Add(eventCode, (player, serializer) =>
|
||||||
|
{
|
||||||
|
t.Deserialize(serializer);
|
||||||
|
|
||||||
|
foreach (var callbackListener in callbacks)
|
||||||
|
callbackListener.Invoke(player, t);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return new EventSubscription(callbacks, localCallbacks, action);
|
||||||
|
}
|
||||||
|
|
||||||
public void LoadScene(string sceneName) => _scene.Load(sceneName);
|
public void LoadScene(string sceneName) => _scene.Load(sceneName);
|
||||||
public void SceneLoaded() => _scene.SceneLoaded();
|
public void SceneLoaded() => _scene.SceneLoaded();
|
||||||
|
|
||||||
|
public void ReplicateEvent<TEvent>(TEvent evnt, RagonTarget target, RagonReplicationMode mode)
|
||||||
|
where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode);
|
||||||
|
|
||||||
|
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode mode)
|
||||||
|
where TEvent : IRagonEvent, new() => _scene.ReplicateEvent(evnt, target, mode);
|
||||||
|
|
||||||
|
public void ReplicateData(byte[] data, bool reliable = false) => _scene.ReplicateData(data, reliable);
|
||||||
|
|
||||||
public void CreateEntity(RagonEntity entity) => CreateEntity(entity, null);
|
public void CreateEntity(RagonEntity entity) => CreateEntity(entity, null);
|
||||||
public void CreateEntity(RagonEntity entity, RagonPayload payload) => _entityCache.Create(entity, payload);
|
public void CreateEntity(RagonEntity entity, RagonPayload payload) => _entityCache.Create(entity, payload);
|
||||||
public void TransferEntity(RagonEntity entity, RagonPlayer player) => _entityCache.Transfer(entity, player);
|
public void TransferEntity(RagonEntity entity, RagonPlayer player) => _entityCache.Transfer(entity, player);
|
||||||
|
|
||||||
public void DestroyEntity(RagonEntity entityId) => DestroyEntity(entityId, null);
|
public void DestroyEntity(RagonEntity entityId) => DestroyEntity(entityId, null);
|
||||||
public void DestroyEntity(RagonEntity entityId, RagonPayload payload) => _entityCache.Destroy(entityId, payload);
|
public void DestroyEntity(RagonEntity entityId, RagonPayload payload) => _entityCache.Destroy(entityId, payload);
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Cleanup();
|
||||||
|
|
||||||
|
_events.Clear();
|
||||||
|
_listeners.Clear();
|
||||||
|
_localListeners.Clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
namespace Ragon.Client
|
||||||
|
{
|
||||||
|
public struct RagonRoomInformation
|
||||||
|
{
|
||||||
|
public string Id;
|
||||||
|
public string Scene;
|
||||||
|
public int PlayerMax;
|
||||||
|
public int PlayerMin;
|
||||||
|
public int PlayerCount;
|
||||||
|
public Dictionary<string, byte[]> Properties;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -67,4 +67,53 @@ public class RagonScene
|
|||||||
var sendData = buffer.ToArray();
|
var sendData = buffer.ToArray();
|
||||||
_client.Reliable.Send(sendData);
|
_client.Reliable.Send(sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void ReplicateEvent<TEvent>(TEvent evnt, RagonTarget target, RagonReplicationMode replicationMode)
|
||||||
|
where TEvent : IRagonEvent, new()
|
||||||
|
{
|
||||||
|
var evntId = _client.Event.GetEventCode(evnt);
|
||||||
|
var buffer = _client.Buffer;
|
||||||
|
|
||||||
|
buffer.Clear();
|
||||||
|
buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
|
||||||
|
buffer.WriteUShort(evntId);
|
||||||
|
buffer.WriteByte((byte)replicationMode);
|
||||||
|
buffer.WriteByte((byte)target);
|
||||||
|
|
||||||
|
evnt.Serialize(buffer);
|
||||||
|
|
||||||
|
var sendData = buffer.ToArray();
|
||||||
|
_client.Reliable.Send(sendData);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode)
|
||||||
|
where TEvent : IRagonEvent, new()
|
||||||
|
{
|
||||||
|
var evntId = _client.Event.GetEventCode(evnt);
|
||||||
|
var buffer = _client.Buffer;
|
||||||
|
|
||||||
|
buffer.Clear();
|
||||||
|
buffer.WriteOperation(RagonOperation.REPLICATE_ROOM_EVENT);
|
||||||
|
buffer.WriteUShort(evntId);
|
||||||
|
buffer.WriteByte((byte)replicationMode);
|
||||||
|
buffer.WriteByte((byte)RagonTarget.Player);
|
||||||
|
buffer.WriteUShort(target.PeerId);
|
||||||
|
|
||||||
|
evnt.Serialize(buffer);
|
||||||
|
|
||||||
|
var sendData = buffer.ToArray();
|
||||||
|
_client.Reliable.Send(sendData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ReplicateData(byte[] data, bool reliable)
|
||||||
|
{
|
||||||
|
var sendData = new byte[data.Length + 1];
|
||||||
|
sendData[0] = (byte) RagonOperation.REPLICATE_RAW_DATA;
|
||||||
|
Array.Copy(data, 0, sendData, 1, data.Length);
|
||||||
|
|
||||||
|
if (reliable)
|
||||||
|
_client.Reliable.Send(sendData);
|
||||||
|
else
|
||||||
|
_client.Unreliable.Send(sendData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -51,9 +51,9 @@ namespace Ragon.Client
|
|||||||
Create(null, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
|
Create(null, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Create(string roomId, string sceneNa, int minPlayers, int maxPlayers)
|
public void Create(string roomId, string sceneName, int minPlayers, int maxPlayers)
|
||||||
{
|
{
|
||||||
Create(roomId, new RagonRoomParameters() {Scene = sceneNa, Min = minPlayers, Max = maxPlayers});
|
Create(roomId, new RagonRoomParameters() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Create(string roomId, RagonRoomParameters parameters)
|
public void Create(string roomId, RagonRoomParameters parameters)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client
|
||||||
|
{
|
||||||
|
public class RagonUserData : IUserData
|
||||||
|
{
|
||||||
|
public byte[] this[string key]
|
||||||
|
{
|
||||||
|
get => _properties[key];
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_properties[key] = value;
|
||||||
|
|
||||||
|
if (!_changesCache.ContainsKey(key))
|
||||||
|
{
|
||||||
|
_localChanges.Add(key);
|
||||||
|
_changesCache.Add(key, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(string key)
|
||||||
|
{
|
||||||
|
if (_properties.Remove(key))
|
||||||
|
{
|
||||||
|
if (!_changesCache.ContainsKey(key))
|
||||||
|
{
|
||||||
|
_localChanges.Add(key);
|
||||||
|
_changesCache.Add(key, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Dirty => _localChanges.Count > 0;
|
||||||
|
|
||||||
|
private readonly List<string> _localChanges = new();
|
||||||
|
private readonly Dictionary<string, bool> _changesCache = new();
|
||||||
|
private readonly Dictionary<string, byte[]> _properties = new();
|
||||||
|
|
||||||
|
public RagonUserData()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public IReadOnlyList<string> Read(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
var len = buffer.ReadUShort();
|
||||||
|
var changes = new List<string>(len);
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
var key = buffer.ReadString();
|
||||||
|
var valueSize = buffer.ReadUShort();
|
||||||
|
if (valueSize > 0)
|
||||||
|
{
|
||||||
|
var value = buffer.ReadBytes(valueSize);
|
||||||
|
_properties[key] = value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_properties.Remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
changes.Add(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return changes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
buffer.WriteUShort((ushort)_localChanges.Count);
|
||||||
|
foreach (var propertyChanged in _localChanges)
|
||||||
|
{
|
||||||
|
buffer.WriteString(propertyChanged);
|
||||||
|
if (_properties.TryGetValue(propertyChanged, out var property))
|
||||||
|
{
|
||||||
|
buffer.WriteUShort((ushort)property.Length);
|
||||||
|
buffer.WriteBytes(property);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
buffer.WriteUShort(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_changesCache.Clear();
|
||||||
|
_localChanges.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Ragon.Protocol;
|
||||||
|
|
||||||
|
namespace Ragon.Client;
|
||||||
|
|
||||||
|
public class RagonUserDataReadOnly : IUserData
|
||||||
|
{
|
||||||
|
public byte[] this[string key]
|
||||||
|
{
|
||||||
|
get => _properties[key];
|
||||||
|
set { }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Dirty => _dirty;
|
||||||
|
|
||||||
|
private bool _dirty = false;
|
||||||
|
private readonly Dictionary<string, byte[]> _properties = new();
|
||||||
|
|
||||||
|
public RagonUserDataReadOnly()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public IReadOnlyList<string> Read(RagonBuffer buffer)
|
||||||
|
{
|
||||||
|
var len = buffer.ReadUShort();
|
||||||
|
var changes = new List<string>(len);
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
var key = buffer.ReadString();
|
||||||
|
var valueSize = buffer.ReadUShort();
|
||||||
|
if (valueSize > 0)
|
||||||
|
{
|
||||||
|
var value = buffer.ReadBytes(valueSize);
|
||||||
|
_properties[key] = value;
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_properties.Remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
changes.Add(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return changes;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,10 +6,11 @@
|
|||||||
<LangVersion>8</LangVersion>
|
<LangVersion>8</LangVersion>
|
||||||
<RootNamespace>Ragon.Common</RootNamespace>
|
<RootNamespace>Ragon.Common</RootNamespace>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Version>1.2.4-rc</Version>
|
|
||||||
<Title>Ragon.Protocol</Title>
|
<Title>Ragon.Protocol</Title>
|
||||||
<Copyright>Eduard Kargin</Copyright>
|
<Copyright>Eduard Kargin</Copyright>
|
||||||
<PackageProjectUrl>https://ragon-server.com</PackageProjectUrl>
|
<Version>1.4.0</Version>
|
||||||
|
<Authors>Eduard Kargin</Authors>
|
||||||
|
<PackageProjectUrl>https://ragon.io</PackageProjectUrl>
|
||||||
<RepositoryUrl>https://github.com/edmand46/Ragon</RepositoryUrl>
|
<RepositoryUrl>https://github.com/edmand46/Ragon</RepositoryUrl>
|
||||||
<RepositoryType>Source</RepositoryType>
|
<RepositoryType>Source</RepositoryType>
|
||||||
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -65,19 +65,24 @@ namespace Ragon.Protocol
|
|||||||
{
|
{
|
||||||
public class RagonBuffer
|
public class RagonBuffer
|
||||||
{
|
{
|
||||||
|
private const int MaxBufferSize = 1024 * 1024; // 1MB max buffer size
|
||||||
|
|
||||||
private int _read;
|
private int _read;
|
||||||
private int _write;
|
private int _write;
|
||||||
private uint[] _buckets;
|
private uint[] _buckets;
|
||||||
|
private byte[] _rawData;
|
||||||
private readonly UTF8Encoding _utf8Encoding = new UTF8Encoding(false, true);
|
private readonly UTF8Encoding _utf8Encoding = new UTF8Encoding(false, true);
|
||||||
|
|
||||||
|
public byte[] RawData => _rawData;
|
||||||
public int ReadOffset => _read;
|
public int ReadOffset => _read;
|
||||||
public int WriteOffset => _write;
|
public int WriteOffset => _write;
|
||||||
public int Length => ((_write - 1) >> 3) + 1;
|
public int Length => ((_write - 1) >> 3) + 1;
|
||||||
public int Capacity => _write - _read;
|
public int Capacity => _write - _read - 1;
|
||||||
|
|
||||||
public RagonBuffer(int capacity = 128)
|
public RagonBuffer(int capacity = 128)
|
||||||
{
|
{
|
||||||
_buckets = new uint[capacity];
|
_buckets = new uint[capacity];
|
||||||
|
_rawData = Array.Empty<byte>();
|
||||||
_read = 0;
|
_read = 0;
|
||||||
_write = 0;
|
_write = 0;
|
||||||
}
|
}
|
||||||
@@ -118,6 +123,21 @@ namespace Ragon.Protocol
|
|||||||
return (RagonOperation)Read(8);
|
return (RagonOperation)Read(8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteFloat(float value)
|
||||||
|
{
|
||||||
|
var bytes = BitConverter.GetBytes(value);
|
||||||
|
WriteBytes(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public float ReadFloat()
|
||||||
|
{
|
||||||
|
var bytes = ReadBytes(4);
|
||||||
|
var value = BitConverter.ToSingle(bytes, 0);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteFloat(float value, float min, float max, float precision)
|
public void WriteFloat(float value, float min, float max, float precision)
|
||||||
{
|
{
|
||||||
@@ -144,6 +164,23 @@ namespace Ragon.Protocol
|
|||||||
return adjusted;
|
return adjusted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteInt(int value)
|
||||||
|
{
|
||||||
|
var bytes = BitConverter.GetBytes(value);
|
||||||
|
|
||||||
|
WriteBytes(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int ReadInt()
|
||||||
|
{
|
||||||
|
var bytes = ReadBytes(4);
|
||||||
|
var value = BitConverter.ToInt32(bytes, 0);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void WriteInt(int value, int min, int max)
|
public void WriteInt(int value, int min, int max)
|
||||||
{
|
{
|
||||||
@@ -233,9 +270,6 @@ namespace Ragon.Protocol
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void Write(uint value, int numBits = 16)
|
public void Write(uint value, int numBits = 16)
|
||||||
{
|
{
|
||||||
Debug.Assert(!(numBits < 0));
|
|
||||||
Debug.Assert(!(numBits > 32));
|
|
||||||
|
|
||||||
var currentBucketIndex = _write >> 5;
|
var currentBucketIndex = _write >> 5;
|
||||||
var used = _write & 0x0000001F;
|
var used = _write & 0x0000001F;
|
||||||
var mask = (1UL << used) - 1;
|
var mask = (1UL << used) - 1;
|
||||||
@@ -251,6 +285,7 @@ namespace Ragon.Protocol
|
|||||||
_write += numBits;
|
_write += numBits;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public uint Read(int numBits = 16)
|
public uint Read(int numBits = 16)
|
||||||
{
|
{
|
||||||
@@ -281,10 +316,10 @@ namespace Ragon.Protocol
|
|||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
[Obsolete("Do not use this method, will be removed")]
|
[Obsolete("Do not use this method, will be removed")]
|
||||||
public byte[] ReadBytes(int lenght)
|
public byte[] ReadBytes(int len)
|
||||||
{
|
{
|
||||||
var data = new byte[lenght];
|
var data = new byte[len];
|
||||||
for (int i = 0; i < lenght; i++)
|
for (int i = 0; i < len; i++)
|
||||||
data[i] = (byte)Read(8);
|
data[i] = (byte)Read(8);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
@@ -298,6 +333,9 @@ namespace Ragon.Protocol
|
|||||||
var limit = (size + 32 - 1) / 32;
|
var limit = (size + 32 - 1) / 32;
|
||||||
var capacity = size;
|
var capacity = size;
|
||||||
|
|
||||||
|
if (index + limit >= _buckets.Length)
|
||||||
|
Resize(size);
|
||||||
|
|
||||||
for (int i = 0; i < limit; i++)
|
for (int i = 0; i < limit; i++)
|
||||||
{
|
{
|
||||||
var dataSize = capacity > 32 ? 32 : capacity;
|
var dataSize = capacity > 32 ? 32 : capacity;
|
||||||
@@ -354,7 +392,7 @@ namespace Ragon.Protocol
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void FromBuffer(RagonBuffer buffer, int size)
|
public void CopyFrom(RagonBuffer buffer, int size)
|
||||||
{
|
{
|
||||||
WriteArray(buffer._buckets, size);
|
WriteArray(buffer._buckets, size);
|
||||||
}
|
}
|
||||||
@@ -368,6 +406,12 @@ namespace Ragon.Protocol
|
|||||||
public void FromArray(byte[] data)
|
public void FromArray(byte[] data)
|
||||||
{
|
{
|
||||||
var length = data.Length;
|
var length = data.Length;
|
||||||
|
|
||||||
|
if (length > MaxBufferSize)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Input data exceeds maximum buffer size: {length} bytes > {MaxBufferSize} bytes");
|
||||||
|
}
|
||||||
|
|
||||||
var bucketsCount = length / 4 + 1;
|
var bucketsCount = length / 4 + 1;
|
||||||
|
|
||||||
if (_buckets.Length < bucketsCount)
|
if (_buckets.Length < bucketsCount)
|
||||||
@@ -397,10 +441,13 @@ namespace Ragon.Protocol
|
|||||||
|
|
||||||
_write = ((length - 1) * 8) + positionInByte;
|
_write = ((length - 1) * 8) + positionInByte;
|
||||||
_read = 0;
|
_read = 0;
|
||||||
|
_rawData = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] ToArray()
|
public byte[] ToArray()
|
||||||
{
|
{
|
||||||
|
Write(1, 1);
|
||||||
|
|
||||||
var data = new byte[Length];
|
var data = new byte[Length];
|
||||||
int bucketsCount = (_write >> 5) + 1;
|
int bucketsCount = (_write >> 5) + 1;
|
||||||
int length = data.Length;
|
int length = data.Length;
|
||||||
@@ -426,9 +473,42 @@ namespace Ragon.Protocol
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ToArray(byte[] outData)
|
||||||
|
{
|
||||||
|
Write(1, 1);
|
||||||
|
|
||||||
|
var bucketsCount = (_write >> 5) + 1;
|
||||||
|
var length = Length;
|
||||||
|
|
||||||
|
for (int i = 0; i < bucketsCount; i++)
|
||||||
|
{
|
||||||
|
var dataIdx = i * 4;
|
||||||
|
var bucket = _buckets[i];
|
||||||
|
|
||||||
|
if (dataIdx < length)
|
||||||
|
outData[dataIdx] = (byte)bucket;
|
||||||
|
|
||||||
|
if (dataIdx + 1 < length)
|
||||||
|
outData[dataIdx + 1] = (byte)(bucket >> 8);
|
||||||
|
|
||||||
|
if (dataIdx + 2 < length)
|
||||||
|
outData[dataIdx + 2] = (byte)(bucket >> 16);
|
||||||
|
|
||||||
|
if (dataIdx + 3 < length)
|
||||||
|
outData[dataIdx + 3] = (byte)(bucket >> 24);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void Resize(int capacity)
|
private void Resize(int capacity)
|
||||||
{
|
{
|
||||||
var buckets = new uint[_buckets.Length * 2 + capacity];
|
var newSize = _buckets.Length * 2 + capacity;
|
||||||
|
|
||||||
|
if (newSize * 4 > MaxBufferSize)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Buffer size limit exceeded: {newSize * 4} bytes > {MaxBufferSize} bytes");
|
||||||
|
}
|
||||||
|
|
||||||
|
var buckets = new uint[newSize];
|
||||||
Array.Copy(_buckets, buckets, _buckets.Length);
|
Array.Copy(_buckets, buckets, _buckets.Length);
|
||||||
_buckets = buckets;
|
_buckets = buckets;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,27 +19,33 @@ namespace Ragon.Protocol
|
|||||||
{
|
{
|
||||||
public enum RagonOperation: byte
|
public enum RagonOperation: byte
|
||||||
{
|
{
|
||||||
AUTHORIZE,
|
AUTHORIZE = 1,
|
||||||
AUTHORIZED_SUCCESS,
|
AUTHORIZED_SUCCESS = 2,
|
||||||
AUTHORIZED_FAILED,
|
AUTHORIZED_FAILED = 3,
|
||||||
JOIN_OR_CREATE_ROOM,
|
JOIN_OR_CREATE_ROOM = 4,
|
||||||
CREATE_ROOM,
|
CREATE_ROOM = 5,
|
||||||
JOIN_ROOM,
|
JOIN_ROOM = 6,
|
||||||
LEAVE_ROOM,
|
LEAVE_ROOM = 7,
|
||||||
OWNERSHIP_ENTITY_CHANGED,
|
OWNERSHIP_ENTITY_CHANGED = 8,
|
||||||
OWNERSHIP_ROOM_CHANGED,
|
OWNERSHIP_ROOM_CHANGED= 9,
|
||||||
JOIN_SUCCESS,
|
JOIN_SUCCESS = 10,
|
||||||
JOIN_FAILED,
|
JOIN_FAILED = 11,
|
||||||
LOAD_SCENE,
|
LOAD_SCENE = 12,
|
||||||
SCENE_LOADED,
|
SCENE_LOADED = 13,
|
||||||
PLAYER_JOINED,
|
PLAYER_JOINED = 14,
|
||||||
PLAYER_LEAVED,
|
PLAYER_LEAVED = 15,
|
||||||
CREATE_ENTITY,
|
CREATE_ENTITY = 16,
|
||||||
REMOVE_ENTITY,
|
REMOVE_ENTITY = 17,
|
||||||
SNAPSHOT,
|
SNAPSHOT = 18,
|
||||||
REPLICATE_ENTITY_STATE,
|
REPLICATE_ENTITY_STATE = 19,
|
||||||
REPLICATE_ENTITY_EVENT,
|
REPLICATE_ENTITY_EVENT = 20,
|
||||||
TRANSFER_ROOM_OWNERSHIP,
|
REPLICATE_RAW_DATA = 21,
|
||||||
TRANSFER_ENTITY_OWNERSHIP,
|
REPLICATE_ROOM_EVENT = 22,
|
||||||
|
TRANSFER_ROOM_OWNERSHIP = 23,
|
||||||
|
TRANSFER_ENTITY_OWNERSHIP = 24,
|
||||||
|
TIMESTAMP_SYNCHRONIZATION = 25,
|
||||||
|
ROOM_LIST_UPDATED = 26,
|
||||||
|
PLAYER_DATA_UPDATED = 27,
|
||||||
|
ROOM_DATA_UPDATED = 28,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -0,0 +1,309 @@
|
|||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Ragon.Protocol
|
||||||
|
{
|
||||||
|
[StructLayout(LayoutKind.Explicit)]
|
||||||
|
internal struct ValueConverter
|
||||||
|
{
|
||||||
|
[FieldOffset(0)] public int Int;
|
||||||
|
[FieldOffset(0)] public float Float;
|
||||||
|
[FieldOffset(0)] public long Long;
|
||||||
|
[FieldOffset(0)] public byte Byte0;
|
||||||
|
[FieldOffset(1)] public byte Byte1;
|
||||||
|
[FieldOffset(2)] public byte Byte2;
|
||||||
|
[FieldOffset(3)] public byte Byte3;
|
||||||
|
[FieldOffset(4)] public byte Byte4;
|
||||||
|
[FieldOffset(5)] public byte Byte5;
|
||||||
|
[FieldOffset(6)] public byte Byte6;
|
||||||
|
[FieldOffset(7)] public byte Byte7;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RagonStream
|
||||||
|
{
|
||||||
|
private byte[] _data;
|
||||||
|
private int _offset;
|
||||||
|
private int _size;
|
||||||
|
|
||||||
|
public int Lenght => _offset;
|
||||||
|
public int Size => _size - _offset;
|
||||||
|
|
||||||
|
public RagonStream(int capacity = 256)
|
||||||
|
{
|
||||||
|
_data = new byte[capacity];
|
||||||
|
_offset = 0;
|
||||||
|
_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void Reset()
|
||||||
|
{
|
||||||
|
_size = _offset;
|
||||||
|
_offset = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void AddOffset(int offset)
|
||||||
|
{
|
||||||
|
_offset += offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteByte(byte value)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(1);
|
||||||
|
_data[_offset] = value;
|
||||||
|
_offset += 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public byte ReadByte()
|
||||||
|
{
|
||||||
|
var value = _data[_offset];
|
||||||
|
_offset += 1;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteBool(bool value)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(1);
|
||||||
|
_data[_offset] = value ? (byte)1 : (byte)0;
|
||||||
|
_offset += 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool ReadBool()
|
||||||
|
{
|
||||||
|
var value = _data[_offset];
|
||||||
|
_offset += 1;
|
||||||
|
return value == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteInt(int value)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(4);
|
||||||
|
var converter = new ValueConverter() { Int = value };
|
||||||
|
_data[_offset] = converter.Byte0;
|
||||||
|
_data[_offset + 1] = converter.Byte1;
|
||||||
|
_data[_offset + 2] = converter.Byte2;
|
||||||
|
_data[_offset + 3] = converter.Byte3;
|
||||||
|
_offset += 4;
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteInt(int value, int offset)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(4);
|
||||||
|
var converter = new ValueConverter() { Int = value };
|
||||||
|
_data[offset] = converter.Byte0;
|
||||||
|
_data[offset + 1] = converter.Byte1;
|
||||||
|
_data[offset + 2] = converter.Byte2;
|
||||||
|
_data[offset + 3] = converter.Byte3;
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int ReadInt()
|
||||||
|
{
|
||||||
|
var converter = new ValueConverter
|
||||||
|
{ Byte0 = _data[_offset], Byte1 = _data[_offset + 1], Byte2 = _data[_offset + 2], Byte3 = _data[_offset + 3] };
|
||||||
|
_offset += 4;
|
||||||
|
return converter.Int;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteLong(long value)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(8);
|
||||||
|
WriteLong(value, _offset);
|
||||||
|
_offset += 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteLong(long value, int offset)
|
||||||
|
{
|
||||||
|
var converter = new ValueConverter() { Long = value };
|
||||||
|
_data[offset] = converter.Byte0;
|
||||||
|
_data[offset + 1] = converter.Byte1;
|
||||||
|
_data[offset + 2] = converter.Byte2;
|
||||||
|
_data[offset + 3] = converter.Byte3;
|
||||||
|
_data[offset + 4] = converter.Byte4;
|
||||||
|
_data[offset + 5] = converter.Byte5;
|
||||||
|
_data[offset + 6] = converter.Byte6;
|
||||||
|
_data[offset + 7] = converter.Byte7;
|
||||||
|
return 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public long ReadLong()
|
||||||
|
{
|
||||||
|
var converter = new ValueConverter
|
||||||
|
{
|
||||||
|
Byte0 = _data[_offset],
|
||||||
|
Byte1 = _data[_offset + 1],
|
||||||
|
Byte2 = _data[_offset + 2],
|
||||||
|
Byte3 = _data[_offset + 3],
|
||||||
|
Byte4 = _data[_offset + 4],
|
||||||
|
Byte5 = _data[_offset + 5],
|
||||||
|
Byte6 = _data[_offset + 6],
|
||||||
|
Byte7 = _data[_offset + 7],
|
||||||
|
};
|
||||||
|
_offset += 8;
|
||||||
|
return converter.Long;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteFloat(float value)
|
||||||
|
{
|
||||||
|
var converter = new ValueConverter() { Float = value };
|
||||||
|
WriteInt(converter.Int);
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public float ReadFloat()
|
||||||
|
{
|
||||||
|
var rawValue = ReadInt();
|
||||||
|
var converter = new ValueConverter() { Int = rawValue };
|
||||||
|
var value = converter.Float;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public int WriteString(string value)
|
||||||
|
{
|
||||||
|
var rawData = Encoding.UTF8.GetBytes(value);
|
||||||
|
var len = rawData.Length;
|
||||||
|
ResizeIfNeed(2 + len);
|
||||||
|
WriteUShort((ushort)len);
|
||||||
|
|
||||||
|
Buffer.BlockCopy(rawData, 0, _data, _offset, len);
|
||||||
|
|
||||||
|
_offset += len;
|
||||||
|
|
||||||
|
return len + 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public string ReadString()
|
||||||
|
{
|
||||||
|
var len = ReadUShort();
|
||||||
|
var rawData = new byte[len];
|
||||||
|
|
||||||
|
Buffer.BlockCopy(_data, _offset, rawData, 0, len);
|
||||||
|
|
||||||
|
var str = Encoding.UTF8.GetString(rawData);
|
||||||
|
_offset += len;
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public byte[] ReadBinary(int len)
|
||||||
|
{
|
||||||
|
if (len >= _data.Length)
|
||||||
|
return Array.Empty<byte>();
|
||||||
|
|
||||||
|
var payload = new byte[len];
|
||||||
|
Buffer.BlockCopy(_data, _offset, payload, 0, len);
|
||||||
|
|
||||||
|
_offset += len;
|
||||||
|
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteBinary(byte[] payload)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(payload.Length);
|
||||||
|
|
||||||
|
Array.Copy(payload, 0, _data, _offset, payload.Length);
|
||||||
|
|
||||||
|
_offset += payload.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteOperation(RagonOperation operation)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(1);
|
||||||
|
|
||||||
|
_data[_offset] = (byte)operation;
|
||||||
|
_offset += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public RagonOperation ReadOperation()
|
||||||
|
{
|
||||||
|
var op = (RagonOperation)_data[_offset];
|
||||||
|
_offset += 1;
|
||||||
|
return op;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteUShort(ushort value)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(2);
|
||||||
|
|
||||||
|
_data[_offset] = (byte)(value & 0x00FF);
|
||||||
|
_data[_offset + 1] = (byte)((value & 0xFF00) >> 8);
|
||||||
|
_offset += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WriteUShort(ushort value, int offset)
|
||||||
|
{
|
||||||
|
ResizeIfNeed(2);
|
||||||
|
_data[offset] = (byte)(value & 0x00FF);
|
||||||
|
_data[offset + 1] = (byte)((value & 0xFF00) >> 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public ushort ReadUShort()
|
||||||
|
{
|
||||||
|
var value = (ushort)(_data[_offset] + (_data[_offset + 1] << 8));
|
||||||
|
_offset += 2;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
_offset = 0;
|
||||||
|
_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void FromArray(byte[] data)
|
||||||
|
{
|
||||||
|
Clear();
|
||||||
|
ResizeIfNeed(data.Length);
|
||||||
|
Buffer.BlockCopy(data, 0, _data, 0, data.Length);
|
||||||
|
_size = data.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public byte[] ToArray()
|
||||||
|
{
|
||||||
|
var bytes = new byte[_offset];
|
||||||
|
Buffer.BlockCopy(_data, 0, bytes, 0, _offset);
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ResizeIfNeed(int lenght)
|
||||||
|
{
|
||||||
|
if (_offset + lenght < _data.Length)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var newData = new byte[_data.Length * 4 + lenght];
|
||||||
|
Buffer.BlockCopy(_data, 0, newData, 0, _data.Length);
|
||||||
|
_data = newData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,9 +16,34 @@
|
|||||||
|
|
||||||
|
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace Ragon.Protocol
|
namespace Ragon.Protocol
|
||||||
{
|
{
|
||||||
|
[StructLayout(LayoutKind.Explicit)]
|
||||||
|
public struct DoubleToUInt
|
||||||
|
{
|
||||||
|
[FieldOffset(0)]
|
||||||
|
public double Double;
|
||||||
|
|
||||||
|
[FieldOffset(0)]
|
||||||
|
public uint Int0;
|
||||||
|
|
||||||
|
[FieldOffset(4)]
|
||||||
|
public uint Int1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class RagonTime {
|
||||||
|
public static double CurrentTimestamp()
|
||||||
|
{
|
||||||
|
var currentTime = System.DateTime.UtcNow.ToUniversalTime().Subtract(
|
||||||
|
new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc)
|
||||||
|
).TotalMilliseconds;
|
||||||
|
|
||||||
|
return currentTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class DeBruijn
|
public static class DeBruijn
|
||||||
{
|
{
|
||||||
private static readonly int[] _lookup = new int[32]
|
private static readonly int[] _lookup = new int[32]
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build-env
|
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build-env
|
||||||
|
|
||||||
WORKDIR /App
|
WORKDIR /App
|
||||||
|
|
||||||
@@ -7,7 +7,7 @@ COPY . ./
|
|||||||
RUN dotnet restore
|
RUN dotnet restore
|
||||||
RUN dotnet publish -c Release -o out
|
RUN dotnet publish -c Release -o out
|
||||||
|
|
||||||
FROM mcr.microsoft.com/dotnet/runtime:7.0
|
FROM mcr.microsoft.com/dotnet/runtime:9.0
|
||||||
|
|
||||||
WORKDIR /App
|
WORKDIR /App
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2023 Eduard Kargin <kargin.eduard@gmail.com>
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<RootNamespace>Ragon.Relay</RootNamespace>
|
<RootNamespace>Ragon.Relay</RootNamespace>
|
||||||
<TargetFramework>net7.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||||
@@ -23,9 +23,14 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Ragon.Server.ENet\Ragon.Server.ENet.csproj" />
|
<ProjectReference Include="..\Ragon.Server.ENetServer\Ragon.Server.ENetServer.csproj" />
|
||||||
<ProjectReference Include="..\Ragon.Server.WebSocketServer\Ragon.Server.WebSocketServer.csproj" />
|
<ProjectReference Include="..\Ragon.Server.WebSocketServer\Ragon.Server.WebSocketServer.csproj" />
|
||||||
<ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" />
|
<ProjectReference Include="..\Ragon.Server\Ragon.Server.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
|
<PackageReference Include="NLog" Version="5.3.2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user