Compare commits
105 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d3ae5a4465 | |||
| 5bf1881f81 | |||
| 6886808132 | |||
| b4cba20d82 | |||
| 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 | |||
| 27db256902 | |||
| 8705e93929 | |||
| 08e931d1bd | |||
| fb58dedfaf | |||
| 92062cd708 | |||
| 5fc55eaddc | |||
| 4a8aae11e3 | |||
| cd9304e63a | |||
| 5199b5271b | |||
| c01b748031 | |||
| 0a8d761cc1 | |||
| f38c7e98de | |||
| 0479a21980 | |||
| 1406b17d62 | |||
| 105457ffa0 | |||
| 20662ae24d | |||
| 6c441d9dee | |||
| 907bd2611e | |||
| 91d8516ac9 | |||
| ecdafeab00 | |||
| 88baff9fee | |||
| fdb41649b2 | |||
| aa607a7eb9 | |||
| efebf4ceda | |||
| 17d1b7307d | |||
| fc28f512ba | |||
| 6c4a51534a | |||
| c91551ae08 | |||
| e1a9ad476c | |||
| 24c9aa2043 | |||
| bfd6c1b54b | |||
| f2edc94958 | |||
| b8dfc4cf41 | |||
| bd7713bfcb | |||
| 043523d712 | |||
| 0dc5307b92 | |||
| 7b581b9afe | |||
| 8c5e063ef0 | |||
| 1a5f72a815 | |||
| 828da0d3da | |||
| 951174e491 | |||
| 10b85867af | |||
| 252a46a713 | |||
| 273c167c87 | |||
| 192fb9e8eb | |||
| a8ddc40268 | |||
| 1ae545b353 | |||
| 68cd246641 | |||
| b2058d21ce | |||
| cbda5e9974 | |||
| e84511e1ae | |||
| b793ca3e68 | |||
| 3936e5c95b | |||
| e9418f4b22 | |||
| f34b05e6ff |
@@ -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
|
||||
uses: actions/setup-dotnet@v1
|
||||
with:
|
||||
dotnet-version: 6.0.x
|
||||
dotnet-version: 7.0.x
|
||||
- name: Build
|
||||
shell: bash
|
||||
run: |
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
.DS_Store
|
||||
.idea
|
||||
.vs
|
||||
.vscode
|
||||
obj
|
||||
bin
|
||||
*.user
|
||||
*.dylib
|
||||
*.dll
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 26 KiB |
+202
@@ -0,0 +1,202 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
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.
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
<p align="center">
|
||||
<img src="Images/ragon-logo.png" width="200" >
|
||||
<img src="Images/logo.png">
|
||||
</p>
|
||||
|
||||
## Ragon Server
|
||||
|
||||
Ragon is fully free, small and high perfomance room based game server with plugin based architecture.
|
||||
|
||||
<a href="https://ragon-server.com/docs/category/basics">Documentation</a>
|
||||
<br>
|
||||
<a href="https://ragon-server.com/docs/get-started">Get started</a>
|
||||
<a href="https://www.ragon-server.com/docs/overview">Documentation</a>
|
||||
|
||||
### Features:
|
||||
- Effective
|
||||
- Free
|
||||
- Simple matchmaking
|
||||
- Lobby
|
||||
- Room based architecture
|
||||
- Сustomizable authorization
|
||||
- Сustomizable server-side logic via plugins with flexible API*(2)
|
||||
@@ -28,10 +26,6 @@ Ragon is fully free, small and high perfomance room based game server with plugi
|
||||
### Dependencies
|
||||
* ENet-Sharp [v2.4.8]
|
||||
|
||||
### License
|
||||
MIT
|
||||
|
||||
### Tips
|
||||
\* Limited to 4095 CCU by library ENet-Sharp (1)
|
||||
|
||||
\* Non finally (2)
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ragon.Client\Ragon.Client.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client.Property
|
||||
{
|
||||
[Serializable]
|
||||
public class RagonBool : RagonProperty
|
||||
{
|
||||
public bool Value
|
||||
{
|
||||
get => _value;
|
||||
set
|
||||
{
|
||||
_value = value;
|
||||
|
||||
MarkAsChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private bool _value;
|
||||
|
||||
public RagonBool(
|
||||
bool initialValue,
|
||||
bool invokeLocal = true,
|
||||
int priority = 0
|
||||
) : base(priority, invokeLocal)
|
||||
{
|
||||
_value = initialValue;
|
||||
SetFixedSize(1);
|
||||
}
|
||||
|
||||
public override void Serialize(RagonBuffer buffer)
|
||||
{
|
||||
buffer.WriteBool(_value);
|
||||
}
|
||||
|
||||
public override void Deserialize(RagonBuffer buffer)
|
||||
{
|
||||
_value = buffer.ReadBool();
|
||||
|
||||
InvokeChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
[Serializable]
|
||||
public class RagonFloat : RagonProperty
|
||||
{
|
||||
public float Value
|
||||
{
|
||||
get => _value;
|
||||
set
|
||||
{
|
||||
_value = value;
|
||||
|
||||
if (_value < _min)
|
||||
_value = _min;
|
||||
else if (_value > _max)
|
||||
_value = _max;
|
||||
|
||||
MarkAsChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private float _value;
|
||||
private float _min;
|
||||
private float _max;
|
||||
private int _requiredBits;
|
||||
private float _precision;
|
||||
private uint _mask;
|
||||
|
||||
public RagonFloat(
|
||||
float initialValue,
|
||||
bool invokeLocal = true,
|
||||
int priority = 0
|
||||
) : base(priority, invokeLocal)
|
||||
{
|
||||
_value = initialValue;
|
||||
_min = -1024.0f;
|
||||
_max = 1024.0f;
|
||||
_precision = 0.01f;
|
||||
|
||||
_requiredBits = DeBruijn.Log2((uint)((_max - _min) * (1.0f / _precision) + 0.5f)) + 1;
|
||||
_mask = (uint)((1L << _requiredBits) - 1);
|
||||
|
||||
SetFixedSize(_requiredBits);
|
||||
}
|
||||
|
||||
public RagonFloat(
|
||||
float initialValue,
|
||||
float min = -1024.0f,
|
||||
float max = 1024.0f,
|
||||
float precision = 0.01f,
|
||||
bool invokeLocal = true,
|
||||
int priority = 0
|
||||
) : base(priority, invokeLocal)
|
||||
{
|
||||
_value = initialValue;
|
||||
_min = min;
|
||||
_max = max;
|
||||
_precision = precision;
|
||||
|
||||
_requiredBits = DeBruijn.Log2((uint)((_max - _min) * (1.0f / _precision) + 0.5f)) + 1;
|
||||
_mask = (uint)((1L << _requiredBits) - 1);
|
||||
|
||||
SetFixedSize(_requiredBits);
|
||||
}
|
||||
|
||||
public override void Serialize(RagonBuffer buffer)
|
||||
{
|
||||
var compressedValue = (uint)((_value - _min) * (1f / _precision) + 0.5f) & _mask;
|
||||
buffer.Write(compressedValue, _requiredBits);
|
||||
}
|
||||
|
||||
public override void Deserialize(RagonBuffer buffer)
|
||||
{
|
||||
var compressedValue = buffer.Read(_requiredBits);
|
||||
_value = compressedValue * _precision + _min;
|
||||
|
||||
if (_value < _min)
|
||||
_value = _min;
|
||||
else if (_value > _max)
|
||||
_value = _max;
|
||||
|
||||
InvokeChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client.Property
|
||||
{
|
||||
[Serializable]
|
||||
public class RagonInt : RagonProperty
|
||||
{
|
||||
public int Value
|
||||
{
|
||||
get => _value;
|
||||
set
|
||||
{
|
||||
_value = value;
|
||||
MarkAsChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private int _min;
|
||||
private int _max;
|
||||
private int _requiredBits;
|
||||
private int _value;
|
||||
|
||||
public RagonInt(
|
||||
int initialValue,
|
||||
bool invokeLocal = true,
|
||||
int priority = 0
|
||||
) : base(priority, invokeLocal)
|
||||
{
|
||||
_value = initialValue;
|
||||
_min = -1000;
|
||||
_max = 1000;
|
||||
|
||||
_max = Math.Max(Math.Abs(_min), Math.Abs(_max));
|
||||
_requiredBits = Bits.Compute(_max);
|
||||
|
||||
SetFixedSize(_requiredBits);
|
||||
}
|
||||
|
||||
public RagonInt(
|
||||
int initialValue,
|
||||
int min = -1000,
|
||||
int max = 1000,
|
||||
bool invokeLocal = true,
|
||||
int priority = 0
|
||||
) : base(priority, invokeLocal)
|
||||
{
|
||||
_value = initialValue;
|
||||
_min = min;
|
||||
_max = max;
|
||||
|
||||
_requiredBits = Bits.Compute(_max);
|
||||
|
||||
SetFixedSize(_requiredBits);
|
||||
}
|
||||
|
||||
public override void Serialize(RagonBuffer buffer)
|
||||
{
|
||||
uint compressedValue = (uint)((_value << 1) ^ (_value >> 31));
|
||||
buffer.Write(compressedValue, _requiredBits);
|
||||
}
|
||||
|
||||
public override void Deserialize(RagonBuffer buffer)
|
||||
{
|
||||
var compressedValue = buffer.Read(_requiredBits);
|
||||
_value = (int)((compressedValue >> 1) ^ -(int)(compressedValue & 1));
|
||||
|
||||
InvokeChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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 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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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 System.Text;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client.Property
|
||||
{
|
||||
[Serializable]
|
||||
public class RagonString : RagonProperty
|
||||
{
|
||||
public string Value
|
||||
{
|
||||
get => _value;
|
||||
set
|
||||
{
|
||||
_value = value;
|
||||
|
||||
MarkAsChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private string _value;
|
||||
private readonly UTF8Encoding _utf8Encoding = new UTF8Encoding(false, true);
|
||||
|
||||
public RagonString(
|
||||
string initialValue,
|
||||
bool invokeLocal = true,
|
||||
int priority = 0
|
||||
) : base(priority, invokeLocal)
|
||||
{
|
||||
_value = initialValue;
|
||||
}
|
||||
|
||||
public override void Serialize(RagonBuffer buffer)
|
||||
{
|
||||
var data = _utf8Encoding.GetBytes(_value);
|
||||
var len = (uint) data.Length;
|
||||
|
||||
buffer.Write(len, 16);
|
||||
buffer.WriteBytes(data);
|
||||
}
|
||||
|
||||
public override void Deserialize(RagonBuffer buffer)
|
||||
{
|
||||
var len = (int) buffer.Read(16);
|
||||
var data = buffer.ReadBytes(len);
|
||||
|
||||
_value = _utf8Encoding.GetString(data);
|
||||
|
||||
InvokeChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,330 @@
|
||||
/*
|
||||
* 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 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>10</LangVersion>
|
||||
<RootNamespace>Ragon.Client.Simulation</RootNamespace>
|
||||
<Authors>Eduard Kargin (Edmand46)</Authors>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<DebugType>none</DebugType>
|
||||
<OutputPath></OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<OutputPath></OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ragon.Protocol\Ragon.Protocol.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client.Compressor;
|
||||
|
||||
public class FloatCompressor
|
||||
{
|
||||
public float Min { get; private set; }
|
||||
public float Max { get; private set; }
|
||||
public float Precision { get; private set; }
|
||||
public int RequiredBits { get; private set; }
|
||||
|
||||
private uint _mask;
|
||||
|
||||
public FloatCompressor(float min = -1024.0f, float max = 1024.0f, float precision = 0.01f)
|
||||
{
|
||||
Min = min;
|
||||
Max = max;
|
||||
Precision = precision;
|
||||
RequiredBits = DeBruijn.Log2((uint)((Max - Min) * (1f / Precision) + 0.5f)) + 1;
|
||||
|
||||
_mask = (uint)((1L << RequiredBits) - 1);
|
||||
}
|
||||
|
||||
public uint Compress(float value)
|
||||
{
|
||||
return (uint)((value - Min) * 1f / Precision + 0.5f) & _mask;
|
||||
}
|
||||
|
||||
public float Decompress(uint value)
|
||||
{
|
||||
var result = value * Precision + Min;
|
||||
|
||||
if (result < Min)
|
||||
result = Min;
|
||||
else if (result > Max)
|
||||
result = Max;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client.Compressor;
|
||||
|
||||
public class IntCompressor
|
||||
{
|
||||
public int Min { get; private set; }
|
||||
public int Max { get; private set; }
|
||||
public int RequiredBits { get; private set; }
|
||||
|
||||
public IntCompressor(int min = -1000, int max = 1000)
|
||||
{
|
||||
Min = min;
|
||||
Max = max;
|
||||
RequiredBits = Bits.Compute(Max);
|
||||
}
|
||||
|
||||
public uint Compress(int value)
|
||||
{
|
||||
return (uint)((value << 1) ^ (value >> 31));;
|
||||
}
|
||||
|
||||
public int Decompress(uint value)
|
||||
{
|
||||
return (int)((value >> 1) ^ -(int)(value & 1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
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 RagonClient _client;
|
||||
|
||||
public ushort Id { get; private set; }
|
||||
public ushort Type { get; private set; }
|
||||
|
||||
public RagonAuthority Authority { get; private set; }
|
||||
public RagonPlayer Owner { 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 HasAuthority { get; private set; }
|
||||
|
||||
public event Action<RagonEntity> Attached;
|
||||
public event Action Detached;
|
||||
public event Action<RagonPlayer, RagonPlayer> OwnershipChanged;
|
||||
|
||||
internal bool PropertiesChanged => _propertiesChanged;
|
||||
internal ushort SceneId => _sceneId;
|
||||
|
||||
private ushort _sceneId;
|
||||
private bool _propertiesChanged;
|
||||
|
||||
private RagonPayload _spawnPayload;
|
||||
private RagonPayload _destroyPayload;
|
||||
|
||||
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 RagonEntity(ushort type = 0, ushort sceneId = 0, bool replicated = true)
|
||||
{
|
||||
State = new RagonEntityState(this);
|
||||
Type = type;
|
||||
IsReplicated = replicated;
|
||||
|
||||
_spawnPayload = new RagonPayload(0);
|
||||
_destroyPayload = new RagonPayload(0);
|
||||
_sceneId = sceneId;
|
||||
}
|
||||
|
||||
internal void Attach()
|
||||
{
|
||||
IsAttached = true;
|
||||
|
||||
Attached?.Invoke(this);
|
||||
}
|
||||
|
||||
public void SetReplication(bool enabled)
|
||||
{
|
||||
IsReplicated = enabled;
|
||||
}
|
||||
|
||||
internal void Detach(RagonPayload payload)
|
||||
{
|
||||
_destroyPayload = payload;
|
||||
|
||||
Detached?.Invoke();
|
||||
}
|
||||
|
||||
internal T GetPayload<T>(RagonPayload data) where T : IRagonPayload, new()
|
||||
{
|
||||
var payload = new T();
|
||||
if (data.Size <= 0) return payload;
|
||||
|
||||
var buffer = new RagonBuffer();
|
||||
|
||||
data.Write(buffer);
|
||||
|
||||
payload.Deserialize(buffer);
|
||||
|
||||
return 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;
|
||||
|
||||
Owner = player;
|
||||
}
|
||||
|
||||
public T GetAttachPayload<T>() where T : IRagonPayload, new()
|
||||
{
|
||||
return GetPayload<T>(_spawnPayload);
|
||||
}
|
||||
|
||||
public T GetDetachPayload<T>() where T : IRagonPayload, new()
|
||||
{
|
||||
return GetPayload<T>(_destroyPayload);
|
||||
}
|
||||
|
||||
public void ReplicateEvent<TEvent>(TEvent evnt, RagonPlayer target, RagonReplicationMode replicationMode)
|
||||
where TEvent : IRagonEvent, new()
|
||||
{
|
||||
if (!IsAttached)
|
||||
{
|
||||
RagonLog.Error("Entity not attached");
|
||||
return;
|
||||
}
|
||||
|
||||
var evntId = _client.Event.GetEventCode(evnt);
|
||||
var buffer = _client.Buffer;
|
||||
|
||||
buffer.Clear();
|
||||
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
||||
buffer.WriteUShort(Id);
|
||||
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 ReplicateEvent<TEvent>(
|
||||
TEvent evnt,
|
||||
RagonTarget target = RagonTarget.All,
|
||||
RagonReplicationMode replicationMode = RagonReplicationMode.Server)
|
||||
where TEvent : IRagonEvent, new()
|
||||
{
|
||||
if (!IsAttached)
|
||||
{
|
||||
RagonLog.Error("Entity not attached");
|
||||
return;
|
||||
}
|
||||
|
||||
var eventCode = _client.Event.GetEventCode(evnt);
|
||||
if (target != RagonTarget.ExceptOwner)
|
||||
{
|
||||
if (replicationMode == RagonReplicationMode.Local)
|
||||
{
|
||||
var localListeners = _localListeners[eventCode];
|
||||
foreach (var listener in localListeners)
|
||||
listener.Invoke(_client.Room.Local, evnt);
|
||||
return;
|
||||
}
|
||||
|
||||
if (replicationMode == RagonReplicationMode.LocalAndServer)
|
||||
{
|
||||
var localListeners = _localListeners[eventCode];
|
||||
foreach (var listener in localListeners)
|
||||
listener.Invoke(_client.Room.Local, evnt);
|
||||
}
|
||||
}
|
||||
|
||||
var buffer = _client.Buffer;
|
||||
|
||||
buffer.Clear();
|
||||
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
||||
buffer.WriteUShort(Id);
|
||||
buffer.WriteUShort(eventCode);
|
||||
buffer.WriteByte((byte)replicationMode);
|
||||
buffer.WriteByte((byte)target);
|
||||
|
||||
evnt.Serialize(buffer);
|
||||
|
||||
var sendData = buffer.ToArray();
|
||||
_client.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
internal void Write(RagonBuffer buffer)
|
||||
{
|
||||
buffer.WriteUShort(Id);
|
||||
|
||||
State.WriteState(buffer);
|
||||
|
||||
_propertiesChanged = false;
|
||||
}
|
||||
|
||||
internal void Read(RagonBuffer buffer)
|
||||
{
|
||||
State.ReadState(buffer);
|
||||
}
|
||||
|
||||
internal void Event(ushort eventCode, RagonPlayer caller, RagonBuffer buffer)
|
||||
{
|
||||
if (!IsReplicated) return;
|
||||
|
||||
if (_events.TryGetValue(eventCode, out var evnt))
|
||||
evnt?.Invoke(caller, buffer);
|
||||
else
|
||||
RagonLog.Warn($"Handler event on entity {Id} with eventCode {eventCode} not defined");
|
||||
}
|
||||
|
||||
internal void TrackChangedProperty(RagonProperty property)
|
||||
{
|
||||
_propertiesChanged = true;
|
||||
}
|
||||
|
||||
public void OnOwnershipChanged(RagonPlayer player)
|
||||
{
|
||||
var prevOwner = Owner;
|
||||
|
||||
Owner = player;
|
||||
HasAuthority = player.IsLocal;
|
||||
|
||||
OwnershipChanged?.Invoke(prevOwner, player);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_events.Clear();
|
||||
_listeners.Clear();
|
||||
_localListeners.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public sealed class RagonEntityState
|
||||
{
|
||||
private List<RagonProperty> _properties;
|
||||
private RagonEntity _entity;
|
||||
|
||||
public RagonEntityState(RagonEntity entity)
|
||||
{
|
||||
_entity = entity;
|
||||
_properties = new List<RagonProperty>(6);
|
||||
}
|
||||
|
||||
public void AddProperty(RagonProperty property)
|
||||
{
|
||||
_properties.Add(property);
|
||||
|
||||
property.AssignEntity(_entity);
|
||||
}
|
||||
|
||||
internal void WriteInfo(RagonBuffer buffer)
|
||||
{
|
||||
buffer.WriteUShort((ushort)_properties.Count);
|
||||
foreach (var property in _properties)
|
||||
{
|
||||
buffer.WriteBool(property.IsFixed);
|
||||
buffer.WriteUShort((ushort)property.Size);
|
||||
}
|
||||
}
|
||||
|
||||
internal void ReadState(RagonBuffer buffer)
|
||||
{
|
||||
foreach (var property in _properties)
|
||||
{
|
||||
var changed = buffer.ReadBool();
|
||||
if (changed)
|
||||
property.Read(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
internal void WriteState(RagonBuffer buffer)
|
||||
{
|
||||
foreach (var prop in _properties)
|
||||
{
|
||||
if (prop.IsDirty)
|
||||
{
|
||||
buffer.WriteBool(true);
|
||||
prop.Write(buffer);
|
||||
prop.Flush();
|
||||
}
|
||||
else
|
||||
{
|
||||
prop.AddTick();
|
||||
buffer.WriteBool(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public class RagonPayload
|
||||
{
|
||||
public static RagonPayload Empty = new RagonPayload(0);
|
||||
|
||||
private readonly uint[] _data = new uint[128];
|
||||
private readonly int _size = 0;
|
||||
|
||||
public RagonPayload(int capacity)
|
||||
{
|
||||
_size = capacity;
|
||||
}
|
||||
public int Size => _size;
|
||||
|
||||
public void Read(RagonBuffer buffer)
|
||||
{
|
||||
buffer.ReadArray(_data, _size);
|
||||
}
|
||||
|
||||
public void Write(RagonBuffer buffer)
|
||||
{
|
||||
buffer.WriteArray(_data, _size);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Payload Size: {_size}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
[Serializable]
|
||||
public class RagonProperty
|
||||
{
|
||||
public string Name => _name;
|
||||
public RagonEntity Entity => _entity;
|
||||
|
||||
public event Action Changed;
|
||||
public bool IsDirty => _dirty && _ticks >= _priority;
|
||||
public bool IsFixed => _fixed;
|
||||
public int Size => _size;
|
||||
|
||||
private RagonBuffer _propertyBuffer;
|
||||
private RagonEntity _entity;
|
||||
private bool _dirty;
|
||||
private int _size;
|
||||
private int _ticks;
|
||||
private int _priority;
|
||||
private bool _fixed;
|
||||
private string _name;
|
||||
|
||||
protected bool InvokeLocal;
|
||||
|
||||
protected RagonProperty(int priority, bool invokeLocal)
|
||||
{
|
||||
_size = 0;
|
||||
_priority = priority;
|
||||
_fixed = false;
|
||||
_propertyBuffer = new RagonBuffer();
|
||||
|
||||
InvokeLocal = invokeLocal;
|
||||
}
|
||||
|
||||
public void SetName(string name)
|
||||
{
|
||||
_name = name;
|
||||
}
|
||||
|
||||
protected void SetFixedSize(int size)
|
||||
{
|
||||
_size = size;
|
||||
_fixed = true;
|
||||
}
|
||||
|
||||
protected void InvokeChanged()
|
||||
{
|
||||
if (_entity.HasAuthority)
|
||||
return;
|
||||
|
||||
Changed?.Invoke();
|
||||
}
|
||||
|
||||
protected void MarkAsChanged()
|
||||
{
|
||||
if (InvokeLocal)
|
||||
Changed?.Invoke();
|
||||
|
||||
if (_dirty || _entity == null)
|
||||
return;
|
||||
|
||||
_dirty = true;
|
||||
_entity.TrackChangedProperty(this);
|
||||
}
|
||||
|
||||
internal void Flush()
|
||||
{
|
||||
_dirty = false;
|
||||
_ticks = 0;
|
||||
}
|
||||
|
||||
internal void AddTick()
|
||||
{
|
||||
_ticks++;
|
||||
}
|
||||
|
||||
internal void AssignEntity(RagonEntity ent)
|
||||
{
|
||||
_entity = ent;
|
||||
|
||||
Changed?.Invoke();
|
||||
}
|
||||
|
||||
internal void Write(RagonBuffer buffer)
|
||||
{
|
||||
_propertyBuffer.Clear();
|
||||
|
||||
if (_fixed)
|
||||
{
|
||||
Serialize(_propertyBuffer);
|
||||
|
||||
buffer.CopyFrom(_propertyBuffer, _size);
|
||||
return;
|
||||
}
|
||||
|
||||
Serialize(_propertyBuffer);
|
||||
|
||||
var propertySize = (ushort)_propertyBuffer.WriteOffset;
|
||||
buffer.WriteUShort(propertySize);
|
||||
buffer.CopyFrom(_propertyBuffer, propertySize);
|
||||
}
|
||||
|
||||
internal void Read(RagonBuffer buffer)
|
||||
{
|
||||
_propertyBuffer.Clear();
|
||||
|
||||
if (_fixed)
|
||||
{
|
||||
buffer.ToBuffer(_propertyBuffer, _size);
|
||||
|
||||
Deserialize(_propertyBuffer);
|
||||
return;
|
||||
}
|
||||
|
||||
var propSize = buffer.ReadUShort();
|
||||
buffer.ToBuffer(_propertyBuffer, propSize);
|
||||
Deserialize(_propertyBuffer);
|
||||
}
|
||||
|
||||
public virtual void Serialize(RagonBuffer buffer)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void Deserialize(RagonBuffer buffer)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class AuthorizeFailedHandler: IHandler
|
||||
{
|
||||
private readonly RagonListenerList _listenerList;
|
||||
public AuthorizeFailedHandler(RagonListenerList list)
|
||||
{
|
||||
_listenerList = list;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var message = reader.ReadString();
|
||||
_listenerList.OnAuthorizationFailed(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class AuthorizeSuccessHandler: IHandler
|
||||
{
|
||||
private readonly RagonListenerList _listenerList;
|
||||
private readonly RagonClient _client;
|
||||
|
||||
public AuthorizeSuccessHandler(
|
||||
RagonClient client,
|
||||
RagonListenerList listenerList)
|
||||
{
|
||||
_client = client;
|
||||
_listenerList = listenerList;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var playerId = reader.ReadString();
|
||||
var playerName = reader.ReadString();
|
||||
var playerPayload = reader.ReadString();
|
||||
|
||||
_client.SetStatus(RagonStatus.LOBBY);
|
||||
_listenerList.OnAuthorizationSuccess(playerId, playerName, playerPayload);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class EntityCreateHandler : IHandler
|
||||
{
|
||||
private readonly RagonClient _client;
|
||||
private readonly RagonPlayerCache _playerCache;
|
||||
private readonly RagonEntityCache _entityCache;
|
||||
private readonly IRagonEntityListener _entityListener;
|
||||
|
||||
public EntityCreateHandler(
|
||||
RagonClient client,
|
||||
RagonPlayerCache playerCache,
|
||||
RagonEntityCache entityCache,
|
||||
IRagonEntityListener entityListener
|
||||
)
|
||||
{
|
||||
_client = client;
|
||||
_entityCache = entityCache;
|
||||
_playerCache = playerCache;
|
||||
_entityListener = entityListener;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var attachId = reader.ReadUShort();
|
||||
var entityType = reader.ReadUShort();
|
||||
var entityId = reader.ReadUShort();
|
||||
var ownerId = reader.ReadUShort();
|
||||
var player = _playerCache.GetPlayerByPeer(ownerId);
|
||||
var payload = new RagonPayload(reader.Capacity);
|
||||
payload.Read(reader);
|
||||
|
||||
if (player == null)
|
||||
{
|
||||
RagonLog.Warn($"Owner {ownerId}|{player.Name} not found in players");
|
||||
|
||||
_playerCache.Dump();
|
||||
return;
|
||||
}
|
||||
|
||||
var hasAuthority = _playerCache.Local.Id == player.Id;
|
||||
var entity = _entityCache.TryGetEntity(attachId, entityType, 0, entityId, hasAuthority, out var hasCreated);
|
||||
|
||||
entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload);
|
||||
|
||||
if (hasCreated)
|
||||
_entityListener.OnEntityCreated(entity);
|
||||
|
||||
entity.Attach();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class EntityEventHandler : IHandler
|
||||
{
|
||||
private readonly RagonPlayerCache _playerCache;
|
||||
private readonly RagonEntityCache _entityCache;
|
||||
|
||||
public EntityEventHandler(
|
||||
RagonPlayerCache playerCache,
|
||||
RagonEntityCache entityCache
|
||||
)
|
||||
{
|
||||
_playerCache = playerCache;
|
||||
_entityCache = entityCache;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var eventCode = reader.ReadUShort();
|
||||
var peerId = reader.ReadUShort();
|
||||
var executionMode = (RagonReplicationMode)reader.ReadByte();
|
||||
var entityId = reader.ReadUShort();
|
||||
|
||||
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;
|
||||
|
||||
_entityCache.OnEvent(player, entityId, eventCode, reader);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class EntityOwnershipHandler: IHandler
|
||||
{
|
||||
private readonly RagonListenerList _listenerList;
|
||||
private readonly RagonPlayerCache _playerCache;
|
||||
private readonly RagonEntityCache _entityCache;
|
||||
|
||||
public EntityOwnershipHandler(
|
||||
RagonListenerList listenerList,
|
||||
RagonPlayerCache playerCache,
|
||||
RagonEntityCache entityCache)
|
||||
{
|
||||
_listenerList = listenerList;
|
||||
_playerCache = playerCache;
|
||||
_entityCache = entityCache;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var newOwnerId = reader.ReadUShort();
|
||||
var entities = reader.ReadUShort();
|
||||
|
||||
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++)
|
||||
{
|
||||
var entityId = reader.ReadUShort();
|
||||
_entityCache.OnOwnershipChanged(player, entityId);
|
||||
|
||||
RagonLog.Trace("Entity changed owner: " + entityId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class EntityRemoveHandler: IHandler
|
||||
{
|
||||
private readonly RagonEntityCache _entityCache;
|
||||
|
||||
public EntityRemoveHandler(RagonEntityCache entityCache)
|
||||
{
|
||||
_entityCache = entityCache;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var entityId = reader.ReadUShort();
|
||||
var payload = new RagonPayload(reader.Capacity);
|
||||
payload.Read(reader);
|
||||
|
||||
_entityCache.OnDestroy(entityId, payload);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class StateEntityHandler: IHandler
|
||||
{
|
||||
private readonly RagonEntityCache _entityCache;
|
||||
|
||||
public StateEntityHandler(RagonEntityCache entityCache)
|
||||
{
|
||||
_entityCache = entityCache;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var entitiesCount = reader.ReadUShort();
|
||||
for (var i = 0; i < entitiesCount; i++)
|
||||
{
|
||||
var entityId = reader.ReadUShort();
|
||||
_entityCache.OnState(entityId, reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IHandler
|
||||
{
|
||||
public void Handle(RagonBuffer reader);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class JoinFailedHandler: IHandler
|
||||
{
|
||||
private readonly RagonListenerList _listenerList;
|
||||
|
||||
public JoinFailedHandler(RagonListenerList listenerList)
|
||||
{
|
||||
_listenerList = listenerList;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var message = reader.ReadString();
|
||||
_listenerList.OnFailed(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public struct RoomParameters
|
||||
{
|
||||
public RoomParameters(string roomId, string playerId, string ownerId, ushort min, ushort max)
|
||||
{
|
||||
RoomId = roomId;
|
||||
PlayerId = playerId;
|
||||
OwnerId = ownerId;
|
||||
Min = min;
|
||||
Max = max;
|
||||
}
|
||||
|
||||
public string RoomId { get; private set; }
|
||||
public string PlayerId { get; private set; }
|
||||
public string OwnerId { get; private set; }
|
||||
public ushort Min { get; private set; }
|
||||
public ushort Max { get; private set; }
|
||||
}
|
||||
|
||||
internal class JoinSuccessHandler : IHandler
|
||||
{
|
||||
|
||||
|
||||
private readonly RagonListenerList _listenerList;
|
||||
private readonly RagonPlayerCache _playerCache;
|
||||
private readonly RagonEntityCache _entityCache;
|
||||
private readonly RagonClient _client;
|
||||
|
||||
public JoinSuccessHandler(
|
||||
RagonClient client,
|
||||
RagonListenerList listenerList,
|
||||
RagonPlayerCache playerCache,
|
||||
RagonEntityCache entityCache
|
||||
)
|
||||
{
|
||||
_client = client;
|
||||
_listenerList = listenerList;
|
||||
_entityCache = entityCache;
|
||||
_playerCache = playerCache;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var roomId = reader.ReadString();
|
||||
var localId = reader.ReadString();
|
||||
var ownerId = reader.ReadString();
|
||||
var min = reader.ReadUShort();
|
||||
var max = reader.ReadUShort();
|
||||
var sceneName = reader.ReadString();
|
||||
|
||||
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);
|
||||
|
||||
_playerCache.SetOwnerAndLocal(ownerId, localId);
|
||||
_client.AssignRoom(room);
|
||||
|
||||
_listenerList.OnSceneRequest(sceneName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class LeaveRoomHandler : IHandler
|
||||
{
|
||||
private readonly RagonClient _client;
|
||||
private readonly RagonListenerList _listenerList;
|
||||
private readonly RagonEntityCache _entityCache;
|
||||
|
||||
public LeaveRoomHandler(
|
||||
RagonClient client,
|
||||
RagonListenerList listenerList,
|
||||
RagonEntityCache entityCache)
|
||||
{
|
||||
_client = client;
|
||||
_listenerList = listenerList;
|
||||
_entityCache = entityCache;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
_listenerList.OnLeft();
|
||||
_entityCache.Cleanup();
|
||||
_client.Room.Cleanup();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class SceneLoadHandler: IHandler
|
||||
{
|
||||
private readonly RagonClient _client;
|
||||
private readonly RagonListenerList _listenerList;
|
||||
|
||||
public SceneLoadHandler(
|
||||
RagonClient ragonClient,
|
||||
RagonListenerList listenerList
|
||||
)
|
||||
{
|
||||
_client = ragonClient;
|
||||
_listenerList = listenerList;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var sceneName = reader.ReadString();
|
||||
var room = _client.Room;
|
||||
|
||||
room.Cleanup();
|
||||
room.Update(sceneName);
|
||||
|
||||
_listenerList.OnSceneRequest(sceneName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class OwnershipRoomHandler : IHandler
|
||||
{
|
||||
private readonly RagonListenerList _listenerList;
|
||||
private readonly RagonPlayerCache _playerCache;
|
||||
private readonly RagonEntityCache _entityCache;
|
||||
|
||||
public OwnershipRoomHandler(
|
||||
RagonListenerList listenerList,
|
||||
RagonPlayerCache playerCache,
|
||||
RagonEntityCache entityCache)
|
||||
{
|
||||
_listenerList = listenerList;
|
||||
_playerCache = playerCache;
|
||||
_entityCache = entityCache;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var newOwnerId = reader.ReadUShort();
|
||||
var player = _playerCache.GetPlayerByPeer(newOwnerId);
|
||||
if (player == null)
|
||||
{
|
||||
RagonLog.Warn($"Player with peerId:{newOwnerId} not found in cache");
|
||||
|
||||
_playerCache.Dump();
|
||||
return;
|
||||
}
|
||||
|
||||
_playerCache.OnOwnershipChanged(newOwnerId);
|
||||
_listenerList.OnOwnershipChanged(player);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class PlayerJoinHandler : IHandler
|
||||
{
|
||||
private RagonPlayerCache _playerCache;
|
||||
private RagonListenerList _listenerList;
|
||||
|
||||
public PlayerJoinHandler(
|
||||
RagonPlayerCache playerCache,
|
||||
RagonListenerList listenerList
|
||||
)
|
||||
{
|
||||
_playerCache = playerCache;
|
||||
_listenerList = listenerList;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var playerPeerId = reader.ReadUShort();
|
||||
var playerId = reader.ReadString();
|
||||
var playerName = reader.ReadString();
|
||||
|
||||
_playerCache.AddPlayer(playerPeerId, playerId, playerName);
|
||||
|
||||
var player = _playerCache.GetPlayerById(playerId);
|
||||
if (player != null)
|
||||
_listenerList.OnPlayerJoined(player);
|
||||
else
|
||||
RagonLog.Warn($"Player with Id:{playerId} not found in cache");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class PlayerLeftHandler : IHandler
|
||||
{
|
||||
private RagonPlayerCache _playerCache;
|
||||
private RagonEntityCache _entityCache;
|
||||
private RagonListenerList _listenerList;
|
||||
|
||||
public PlayerLeftHandler(
|
||||
RagonEntityCache entityCache,
|
||||
RagonPlayerCache playerCache,
|
||||
RagonListenerList listenerList
|
||||
)
|
||||
{
|
||||
_entityCache = entityCache;
|
||||
_playerCache = playerCache;
|
||||
_listenerList = listenerList;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
var playerId = reader.ReadString();
|
||||
var player = _playerCache.GetPlayerById(playerId);
|
||||
if (player != null)
|
||||
{
|
||||
_playerCache.RemovePlayer(playerId);
|
||||
_listenerList.OnPlayerLeft(player);
|
||||
|
||||
var entities = reader.ReadUShort();
|
||||
var toDeleteIds = new ushort[entities];
|
||||
for (var i = 0; i < entities; i++)
|
||||
{
|
||||
var entityId = reader.ReadUShort();
|
||||
toDeleteIds[i] = entityId;
|
||||
}
|
||||
|
||||
var emptyPayload = new RagonPayload(0);
|
||||
foreach (var id in toDeleteIds)
|
||||
_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)
|
||||
{
|
||||
player.UserData.Read(reader);
|
||||
|
||||
_listenerList.OnPlayerUserData(player);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
RagonLog.Warn("Received user data for unknown player.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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 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));
|
||||
var 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 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,43 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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 RoomUserDataHandler : IHandler
|
||||
{
|
||||
private readonly RagonClient _client;
|
||||
private readonly RagonListenerList _listenerList;
|
||||
|
||||
public RoomUserDataHandler(RagonClient client, RagonListenerList listenerList)
|
||||
{
|
||||
_client = client;
|
||||
_listenerList = listenerList;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer reader)
|
||||
{
|
||||
_client.Room?.HandleUserData(reader);
|
||||
_listenerList.OnRoomUserData();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* 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 System.Diagnostics;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
internal class SnapshotHandler : IHandler
|
||||
{
|
||||
private readonly IRagonEntityListener _entityListener;
|
||||
private readonly RagonClient _client;
|
||||
private readonly RagonListenerList _listenerList;
|
||||
private readonly RagonEntityCache _entityCache;
|
||||
private readonly RagonPlayerCache _playerCache;
|
||||
|
||||
public SnapshotHandler(
|
||||
RagonClient ragonClient,
|
||||
RagonListenerList listenerList,
|
||||
RagonEntityCache entityCache,
|
||||
RagonPlayerCache playerCache,
|
||||
IRagonEntityListener entityListener
|
||||
)
|
||||
{
|
||||
_client = ragonClient;
|
||||
_entityListener = entityListener;
|
||||
_listenerList = listenerList;
|
||||
_entityCache = entityCache;
|
||||
_playerCache = playerCache;
|
||||
}
|
||||
|
||||
public void Handle(RagonBuffer buffer)
|
||||
{
|
||||
var entities = new List<RagonEntity>();
|
||||
var playersCount = buffer.ReadUShort();
|
||||
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();
|
||||
RagonLog.Trace("Dynamic Entities: " + dynamicEntities);
|
||||
for (var i = 0; i < dynamicEntities; i++)
|
||||
{
|
||||
var entityType = buffer.ReadUShort();
|
||||
var entityId = buffer.ReadUShort();
|
||||
var ownerPeerId = buffer.ReadUShort();
|
||||
var payloadSize = buffer.ReadUShort();
|
||||
|
||||
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
|
||||
if (player == null)
|
||||
{
|
||||
RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
|
||||
|
||||
_playerCache.Dump();
|
||||
return;
|
||||
}
|
||||
|
||||
var hasAuthority = _playerCache.Local.Id == player.Id;
|
||||
var entity = _entityCache.TryGetEntity(0, entityType, 0, entityId, hasAuthority, out _);
|
||||
var payload = RagonPayload.Empty;
|
||||
if (payloadSize > 0)
|
||||
{
|
||||
payload = new RagonPayload(payloadSize);
|
||||
payload.Read(buffer);
|
||||
}
|
||||
|
||||
entity.Prepare(_client, entityId, entityType, hasAuthority, player, payload);
|
||||
|
||||
_entityListener.OnEntityCreated(entity);
|
||||
|
||||
entity.Read(buffer);
|
||||
|
||||
entities.Add(entity);
|
||||
}
|
||||
|
||||
var staticEntities = buffer.ReadUShort();
|
||||
RagonLog.Trace("Scene Entities: " + staticEntities);
|
||||
for (var i = 0; i < staticEntities; i++)
|
||||
{
|
||||
var entityType = buffer.ReadUShort();
|
||||
var entityId = buffer.ReadUShort();
|
||||
var staticId = buffer.ReadUShort();
|
||||
var ownerPeerId = buffer.ReadUShort();
|
||||
var payloadSize = buffer.ReadUShort();
|
||||
|
||||
var player = _playerCache.GetPlayerByPeer(ownerPeerId);
|
||||
if (player == null)
|
||||
{
|
||||
RagonLog.Error($"Player not found with peerId: {ownerPeerId}");
|
||||
|
||||
_playerCache.Dump();
|
||||
return;
|
||||
}
|
||||
|
||||
var hasAuthority = _playerCache.Local.Id == player.Id;
|
||||
var entity = _entityCache.TryGetEntity(0, entityType, staticId, entityId, hasAuthority, out _);
|
||||
|
||||
entity.Prepare(_client, entityId, entityType, hasAuthority, player, RagonPayload.Empty);
|
||||
entity.Read(buffer);
|
||||
|
||||
entities.Add(entity);
|
||||
}
|
||||
|
||||
if (_client.Status == RagonStatus.LOBBY)
|
||||
{
|
||||
_client.SetStatus(RagonStatus.ROOM);
|
||||
_listenerList.OnJoined();
|
||||
}
|
||||
|
||||
foreach (var entity in entities)
|
||||
entity.Attach();
|
||||
|
||||
_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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface INetworkChannel
|
||||
{
|
||||
void Send(byte[] data);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface INetworkConnection: IRagonConnection
|
||||
{
|
||||
public INetworkChannel Reliable { get; }
|
||||
public INetworkChannel Unreliable { get; }
|
||||
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; }
|
||||
public void Prepare();
|
||||
public void Connect(string address, ushort port, uint protocol);
|
||||
public void Disconnect();
|
||||
public void Update();
|
||||
public void Dispose();
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public class NetworkStatistics
|
||||
{
|
||||
private const double Interval = 1.0d;
|
||||
private double _upstreamBandwidth = 0d;
|
||||
private double _downstreamBandwidth = 0d;
|
||||
private double _time = 0d;
|
||||
private ulong _upstreamData = 0;
|
||||
private ulong _downstreamData = 0;
|
||||
private ulong _sent = 0;
|
||||
private ulong _received = 0;
|
||||
private int _ping;
|
||||
|
||||
public int Ping => _ping;
|
||||
public double UpstreamBandwidth => _upstreamBandwidth;
|
||||
public double DownstreamBandwidth => _downstreamBandwidth;
|
||||
|
||||
public void Update(ulong sent, ulong received, int ping, float dt)
|
||||
{
|
||||
_sent = sent;
|
||||
_received = received;
|
||||
_ping = ping;
|
||||
|
||||
_time += dt;
|
||||
if (_time >= Interval)
|
||||
{
|
||||
if (_upstreamData > 0)
|
||||
{
|
||||
_upstreamData = _sent - _upstreamData;
|
||||
_upstreamBandwidth = (_upstreamData / _time) / 1000 ;
|
||||
}
|
||||
|
||||
if (_downstreamData > 0)
|
||||
{
|
||||
_downstreamData = _received - _downstreamData;
|
||||
_downstreamBandwidth = (_downstreamData / _time) / 1000;
|
||||
}
|
||||
|
||||
_upstreamData = _sent;
|
||||
_downstreamData = _received;
|
||||
_time -= Interval;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonConnection
|
||||
{
|
||||
public void Close();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonEntityListener
|
||||
{
|
||||
public void OnEntityCreated(RagonEntity entity);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
public interface IRagonEvent: IRagonSerializable
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
public interface IRagonPayload: IRagonSerializable
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonSceneCollector
|
||||
{
|
||||
public RagonEntity[] Collect();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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 interface IUserData
|
||||
{
|
||||
public byte[] this[string key] { get; set; }
|
||||
bool Dirty { get; }
|
||||
void Read(RagonBuffer buffer);
|
||||
void Write(RagonBuffer buffer);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonAuthorizationListener
|
||||
{
|
||||
void OnAuthorizationSuccess(RagonClient client, string playerId, string playerName);
|
||||
void OnAuthorizationFailed(RagonClient client, string message);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonConnectionListener
|
||||
{
|
||||
void OnConnected(RagonClient client);
|
||||
void OnDisconnected(RagonClient client, RagonDisconnect reason);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonDataListener
|
||||
{
|
||||
public void OnData(RagonPlayer player, byte[] data);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonFailedListener
|
||||
{
|
||||
void OnFailed(RagonClient client, string message);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonJoinListener
|
||||
{
|
||||
void OnJoined(RagonClient client);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonLeftListener
|
||||
{
|
||||
void OnLeft(RagonClient client);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
public interface IRagonListener :
|
||||
IRagonAuthorizationListener,
|
||||
IRagonConnectionListener,
|
||||
IRagonFailedListener,
|
||||
IRagonJoinListener,
|
||||
IRagonLeftListener,
|
||||
IRagonSceneListener,
|
||||
IRagonOwnershipChangedListener,
|
||||
IRagonPlayerJoinListener,
|
||||
IRagonPlayerLeftListener,
|
||||
IRagonRoomListListener,
|
||||
IRagonRoomUserDataListener,
|
||||
IRagonPlayerUserDataListener
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonOwnershipChangedListener
|
||||
{
|
||||
void OnOwnershipChanged(RagonClient client, RagonPlayer player);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonPlayerJoinListener
|
||||
{
|
||||
void OnPlayerJoined(RagonClient client, RagonPlayer player);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonPlayerLeftListener
|
||||
{
|
||||
void OnPlayerLeft(RagonClient client, RagonPlayer player);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonPlayerUserDataListener
|
||||
{
|
||||
void OnPlayerUserDataUpdated(RagonClient client, RagonPlayer player);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonRoomListListener
|
||||
{
|
||||
public void OnRoomListUpdate(IReadOnlyList<RagonRoomInformation> roomsInfos);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonRoomUserDataListener
|
||||
{
|
||||
public void OnUserDataUpdated(RagonClient client);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonSceneListener
|
||||
{
|
||||
void OnSceneLoaded(RagonClient client);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Ragon.Client;
|
||||
|
||||
public interface IRagonSceneRequestListener
|
||||
{
|
||||
void OnRequestScene(RagonClient client, string sceneName);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
public interface IRagonLogger
|
||||
{
|
||||
public void Warn(string message);
|
||||
public void Trace(string message);
|
||||
public void Info(string message);
|
||||
public void Error(string message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
public class RagonConsoleLogger: IRagonLogger
|
||||
{
|
||||
public void Warn(string message)
|
||||
{
|
||||
Console.WriteLine($"[WARN] {message}");
|
||||
}
|
||||
|
||||
public void Trace(string message)
|
||||
{
|
||||
Console.WriteLine($"[TRACE] {message}");
|
||||
}
|
||||
|
||||
public void Info(string message)
|
||||
{
|
||||
Console.WriteLine($"[INFO] {message}");
|
||||
}
|
||||
|
||||
public void Error(string message)
|
||||
{
|
||||
Console.WriteLine($"[ERROR] {message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
public class RagonLog
|
||||
{
|
||||
private static IRagonLogger _ragonLogger;
|
||||
static RagonLog() => _ragonLogger = new RagonConsoleLogger();
|
||||
public static void Set(IRagonLogger logger) => _ragonLogger = logger;
|
||||
public static void Warn(string message) => _ragonLogger.Warn(message);
|
||||
public static void Trace(string message) => _ragonLogger.Trace(message);
|
||||
public static void Info(string message) => _ragonLogger.Info(message);
|
||||
public static void Error(string message) => _ragonLogger.Error(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public enum RagonLogLevel
|
||||
{
|
||||
ALL,
|
||||
CONNECTION,
|
||||
STATE,
|
||||
EVENT,
|
||||
ROOM,
|
||||
}
|
||||
@@ -0,0 +1,307 @@
|
||||
/*
|
||||
* 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 sealed class RagonClient
|
||||
{
|
||||
private readonly INetworkConnection _connection;
|
||||
private readonly NetworkStatistics _stats;
|
||||
private IRagonEntityListener _entityListener;
|
||||
private IRagonSceneCollector _sceneCollector;
|
||||
private IHandler[] _handlers;
|
||||
private RagonBuffer _readBuffer;
|
||||
private RagonBuffer _writeBuffer;
|
||||
private RagonRoom _room;
|
||||
private RagonSession _session;
|
||||
private RagonListenerList _listeners;
|
||||
private RagonPlayerCache _playerCache;
|
||||
private RagonEntityCache _entityCache;
|
||||
private RagonEventCache _eventCache;
|
||||
private RagonStatus _status;
|
||||
|
||||
private double _serverTimestamp;
|
||||
private float _replicationRate = 0;
|
||||
private float _replicationTime = 0;
|
||||
|
||||
public double ServerTimestamp => _serverTimestamp;
|
||||
public IRagonConnection Connection => _connection;
|
||||
public RagonStatus Status => _status;
|
||||
public RagonSession Session => _session;
|
||||
public RagonEventCache Event => _eventCache;
|
||||
public RagonEntityCache Entity => _entityCache;
|
||||
public NetworkStatistics Statistics => _stats;
|
||||
public RagonRoom Room => _room;
|
||||
|
||||
internal RagonBuffer Buffer => _writeBuffer;
|
||||
internal INetworkChannel Reliable => _connection.Reliable;
|
||||
internal INetworkChannel Unreliable => _connection.Unreliable;
|
||||
|
||||
#region PUBLIC
|
||||
|
||||
public RagonClient(INetworkConnection connection, int rate)
|
||||
{
|
||||
_listeners = new RagonListenerList(this);
|
||||
|
||||
_connection = connection;
|
||||
_connection.OnData += OnData;
|
||||
_connection.OnConnected += OnConnected;
|
||||
_connection.OnDisconnected += OnDisconnected;
|
||||
|
||||
_replicationRate = (1000.0f / rate) / 1000.0f;
|
||||
_replicationTime = 0;
|
||||
|
||||
_eventCache = new RagonEventCache();
|
||||
_stats = new NetworkStatistics();
|
||||
_status = RagonStatus.DISCONNECTED;
|
||||
}
|
||||
|
||||
|
||||
public void Configure(IRagonSceneCollector sceneCollector)
|
||||
{
|
||||
_sceneCollector = sceneCollector;
|
||||
}
|
||||
|
||||
public void Configure(IRagonEntityListener listener)
|
||||
{
|
||||
_entityListener = listener;
|
||||
}
|
||||
|
||||
public void Connect(string address, ushort port, string protocol)
|
||||
{
|
||||
if (_sceneCollector == null)
|
||||
{
|
||||
RagonLog.Error("Scene collector is not defined!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_entityListener == null)
|
||||
{
|
||||
RagonLog.Error("Entity Listener is not defined!");
|
||||
return;
|
||||
}
|
||||
|
||||
_writeBuffer = new RagonBuffer();
|
||||
_readBuffer = new RagonBuffer();
|
||||
_playerCache = new RagonPlayerCache();
|
||||
_session = new RagonSession(this, _writeBuffer);
|
||||
_entityCache = new RagonEntityCache(this, _playerCache, _sceneCollector);
|
||||
|
||||
_handlers = new IHandler[byte.MaxValue];
|
||||
_handlers[(byte)RagonOperation.AUTHORIZED_SUCCESS] = new AuthorizeSuccessHandler(this, _listeners);
|
||||
_handlers[(byte)RagonOperation.AUTHORIZED_FAILED] = new AuthorizeFailedHandler(_listeners);
|
||||
_handlers[(byte)RagonOperation.JOIN_SUCCESS] =
|
||||
new JoinSuccessHandler(this, _listeners, _playerCache, _entityCache);
|
||||
_handlers[(byte)RagonOperation.JOIN_FAILED] = new JoinFailedHandler(_listeners);
|
||||
_handlers[(byte)RagonOperation.LEAVE_ROOM] = new LeaveRoomHandler(this, _listeners, _entityCache);
|
||||
_handlers[(byte)RagonOperation.OWNERSHIP_ROOM_CHANGED] =
|
||||
new OwnershipRoomHandler(_listeners, _playerCache, _entityCache);
|
||||
_handlers[(byte)RagonOperation.OWNERSHIP_ENTITY_CHANGED] =
|
||||
new EntityOwnershipHandler(_listeners, _playerCache, _entityCache);
|
||||
_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.REPLICATE_ENTITY_STATE] = new StateEntityHandler(_entityCache);
|
||||
_handlers[(byte)RagonOperation.REPLICATE_ENTITY_EVENT] = new EntityEventHandler(_playerCache, _entityCache);
|
||||
_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);
|
||||
_connection.Connect(address, port, protocolRaw);
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
_status = RagonStatus.DISCONNECTED;
|
||||
_room.Cleanup();
|
||||
_connection.Disconnect();
|
||||
|
||||
OnDisconnected(RagonDisconnect.MANUAL);
|
||||
}
|
||||
|
||||
public void Update(float dt)
|
||||
{
|
||||
if (_status != RagonStatus.DISCONNECTED)
|
||||
{
|
||||
_replicationTime += dt;
|
||||
if (_replicationTime >= _replicationRate)
|
||||
{
|
||||
_replicationTime = 0;
|
||||
_entityCache.WriteState(_writeBuffer);
|
||||
|
||||
SendTimestamp();
|
||||
SendRoomUserData();
|
||||
SendPlayerUserData();
|
||||
}
|
||||
|
||||
_stats.Update(_connection.BytesSent, _connection.BytesReceived, _connection.Ping, dt);
|
||||
}
|
||||
|
||||
_listeners.Update();
|
||||
_connection.Update();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_status != RagonStatus.DISCONNECTED)
|
||||
{
|
||||
_status = RagonStatus.DISCONNECTED;
|
||||
_connection.Disconnect();
|
||||
}
|
||||
|
||||
_connection.Dispose();
|
||||
}
|
||||
|
||||
public void AddListener(IRagonListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonAuthorizationListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonConnectionListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonFailedListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonJoinListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonLeftListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonOwnershipChangedListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonPlayerJoinListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonPlayerLeftListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonSceneListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonSceneRequestListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonDataListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonRoomListListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonPlayerUserDataListener listener) => _listeners.Add(listener);
|
||||
public void AddListener(IRagonRoomUserDataListener listener) => _listeners.Add(listener);
|
||||
public void RemoveListener(IRagonListener listener) => _listeners.Remove(listener);
|
||||
public void RemoveListener(IRagonAuthorizationListener listener) => _listeners.Remove(listener);
|
||||
public void RemoveListener(IRagonConnectionListener listener) => _listeners.Remove(listener);
|
||||
public void RemoveListener(IRagonFailedListener listener) => _listeners.Remove(listener);
|
||||
public void RemoveListener(IRagonJoinListener listener) => _listeners.Remove(listener);
|
||||
public void RemoveListener(IRagonLeftListener listener) => _listeners.Remove(listener);
|
||||
public void RemoveListener(IRagonOwnershipChangedListener 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
|
||||
|
||||
#region INTERNAL
|
||||
|
||||
internal void AssignRoom(RagonRoom room)
|
||||
{
|
||||
_room?.Dispose();
|
||||
_room = room;
|
||||
}
|
||||
|
||||
internal void SetStatus(RagonStatus status)
|
||||
{
|
||||
_status = status;
|
||||
}
|
||||
|
||||
internal void SetTimestamp(double time)
|
||||
{
|
||||
_serverTimestamp = time;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#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()
|
||||
{
|
||||
RagonLog.Trace("Connected");
|
||||
|
||||
_listeners.OnConnected();
|
||||
_status = RagonStatus.CONNECTED;
|
||||
}
|
||||
|
||||
private void OnDisconnected(RagonDisconnect reason)
|
||||
{
|
||||
RagonLog.Trace($"Disconnected: {reason}");
|
||||
|
||||
_listeners.OnDisconnected(reason);
|
||||
_status = RagonStatus.DISCONNECTED;
|
||||
}
|
||||
|
||||
private void OnData(byte[] data)
|
||||
{
|
||||
_readBuffer.Clear();
|
||||
_readBuffer.FromArray(data);
|
||||
|
||||
var operation = _readBuffer.ReadByte();
|
||||
_handlers[operation].Handle(_readBuffer);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public sealed class RagonEntityCache
|
||||
{
|
||||
private readonly List<RagonEntity> _entityList = new();
|
||||
private readonly Dictionary<uint, RagonEntity> _entityMap = new();
|
||||
private readonly Dictionary<uint, RagonEntity> _pendingEntities = new();
|
||||
private readonly Dictionary<uint, RagonEntity> _sceneEntities = new();
|
||||
|
||||
private readonly RagonClient _client;
|
||||
private readonly IRagonSceneCollector _sceneCollector;
|
||||
private readonly RagonPlayerCache _playerCache;
|
||||
|
||||
private int _localEntitiesCounter = 0;
|
||||
|
||||
public RagonEntityCache(
|
||||
RagonClient client,
|
||||
RagonPlayerCache playerCache,
|
||||
IRagonSceneCollector sceneCollector
|
||||
)
|
||||
{
|
||||
_client = client;
|
||||
_sceneCollector = sceneCollector;
|
||||
_playerCache = playerCache;
|
||||
}
|
||||
|
||||
public bool TryGetEntity(ushort id, out RagonEntity entity)
|
||||
{
|
||||
return _entityMap.TryGetValue(id, out entity);
|
||||
}
|
||||
|
||||
public void Create(RagonEntity entity, RagonPayload spawnPayload)
|
||||
{
|
||||
var attachId = (ushort)(_playerCache.Local.PeerId + _localEntitiesCounter++);
|
||||
var buffer = _client.Buffer;
|
||||
|
||||
buffer.Clear();
|
||||
buffer.WriteOperation(RagonOperation.CREATE_ENTITY);
|
||||
buffer.WriteUShort(attachId);
|
||||
buffer.WriteUShort(entity.Type);
|
||||
buffer.WriteByte((byte)entity.Authority);
|
||||
|
||||
entity.State.WriteInfo(buffer);
|
||||
|
||||
spawnPayload?.Write(buffer);
|
||||
|
||||
_pendingEntities.Add(attachId, entity);
|
||||
|
||||
var sendData = buffer.ToArray();
|
||||
_client.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
public void Transfer(RagonEntity entity, RagonPlayer player)
|
||||
{
|
||||
var buffer = _client.Buffer;
|
||||
|
||||
buffer.Clear();
|
||||
buffer.WriteOperation(RagonOperation.TRANSFER_ENTITY_OWNERSHIP);
|
||||
buffer.WriteUShort(entity.Id);
|
||||
buffer.WriteUShort(player.PeerId);
|
||||
|
||||
var sendData = buffer.ToArray();
|
||||
_client.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
public void Destroy(RagonEntity entity, RagonPayload destroyPayload)
|
||||
{
|
||||
if (!entity.IsAttached && !entity.HasAuthority)
|
||||
{
|
||||
RagonLog.Warn("Can't destroy object");
|
||||
return;
|
||||
}
|
||||
|
||||
entity.SetReplication(false);
|
||||
|
||||
var buffer = _client.Buffer;
|
||||
|
||||
buffer.Clear();
|
||||
buffer.WriteOperation(RagonOperation.REMOVE_ENTITY);
|
||||
buffer.WriteUShort(entity.Id);
|
||||
|
||||
destroyPayload?.Write(buffer);
|
||||
|
||||
var sendData = buffer.ToArray();
|
||||
_client.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
internal void WriteState(RagonBuffer buffer)
|
||||
{
|
||||
var changedEntities = 0u;
|
||||
|
||||
buffer.Clear();
|
||||
buffer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
||||
|
||||
var offset = buffer.WriteOffset;
|
||||
buffer.Write(0, 16);
|
||||
|
||||
foreach (var ent in _entityList)
|
||||
{
|
||||
if (!ent.IsAttached ||
|
||||
!ent.IsReplicated ||
|
||||
!ent.PropertiesChanged) continue;
|
||||
|
||||
ent.Write(buffer);
|
||||
|
||||
changedEntities++;
|
||||
}
|
||||
|
||||
if (changedEntities <= 0) return;
|
||||
|
||||
buffer.Write(changedEntities, 16, offset);
|
||||
|
||||
var data = buffer.ToArray();
|
||||
_client.Unreliable.Send(data);
|
||||
}
|
||||
|
||||
internal void WriteScene(RagonBuffer buffer)
|
||||
{
|
||||
_sceneEntities.Clear();
|
||||
|
||||
var entities = _sceneCollector.Collect();
|
||||
buffer.WriteUShort((ushort)entities.Length);
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
buffer.WriteUShort(entity.Type);
|
||||
buffer.WriteByte((byte)entity.Authority);
|
||||
buffer.WriteUShort(entity.SceneId);
|
||||
|
||||
entity.State.WriteInfo(buffer);
|
||||
|
||||
_sceneEntities.Add(entity.SceneId, entity);
|
||||
}
|
||||
}
|
||||
|
||||
internal void CacheScene()
|
||||
{
|
||||
_sceneEntities.Clear();
|
||||
|
||||
var entities = _sceneCollector.Collect();
|
||||
foreach (var entity in entities)
|
||||
_sceneEntities.Add(entity.SceneId, entity);
|
||||
}
|
||||
|
||||
internal void Cleanup()
|
||||
{
|
||||
var payload = new RagonPayload(0);
|
||||
foreach (var ent in _entityList)
|
||||
ent.Detach(payload);
|
||||
|
||||
_entityMap.Clear();
|
||||
_entityList.Clear();
|
||||
}
|
||||
|
||||
internal RagonEntity TryGetEntity(ushort attachId, ushort entityType, ushort sceneId, ushort entityId, bool hasAuthority, out bool hasCreated)
|
||||
{
|
||||
if (sceneId > 0)
|
||||
{
|
||||
if (_sceneEntities.TryGetValue(sceneId, out var sceneEntity))
|
||||
{
|
||||
_entityMap.Add(entityId, sceneEntity);
|
||||
|
||||
if (hasAuthority)
|
||||
_entityList.Add(sceneEntity);
|
||||
|
||||
hasCreated = false;
|
||||
|
||||
return sceneEntity;
|
||||
}
|
||||
}
|
||||
|
||||
if (_pendingEntities.TryGetValue(attachId, out var pendingEntity) && hasAuthority)
|
||||
{
|
||||
_pendingEntities.Remove(attachId);
|
||||
_entityMap.Add(entityId, pendingEntity);
|
||||
_entityList.Add(pendingEntity);
|
||||
|
||||
hasCreated = false;
|
||||
|
||||
return pendingEntity;
|
||||
}
|
||||
|
||||
var entity = new RagonEntity(entityType, sceneId);
|
||||
|
||||
_entityMap.Add(entityId, entity);
|
||||
|
||||
if (hasAuthority)
|
||||
_entityList.Add(entity);
|
||||
|
||||
hasCreated = true;
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
||||
internal void OnDestroy(ushort entityId, RagonPayload payload)
|
||||
{
|
||||
if (_entityMap.TryGetValue(entityId, out var entity))
|
||||
{
|
||||
_entityMap.Remove(entityId);
|
||||
_entityList.Remove(entity);
|
||||
|
||||
entity.Detach(payload);
|
||||
entity.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
internal void OnState(ushort entityId, RagonBuffer buffer)
|
||||
{
|
||||
if (_entityMap.TryGetValue(entityId, out var entity))
|
||||
entity.Read(buffer);
|
||||
else
|
||||
RagonLog.Warn($"Entity {entityId} not found!");
|
||||
}
|
||||
|
||||
internal void OnEvent(RagonPlayer player, ushort entityId, ushort eventCode, RagonBuffer buffer)
|
||||
{
|
||||
if (_entityMap.TryGetValue(entityId, out var entity))
|
||||
entity.Event(eventCode, player, buffer);
|
||||
else
|
||||
RagonLog.Warn($"Entity {entityId} not found!");
|
||||
}
|
||||
|
||||
internal void OnOwnershipChanged(RagonPlayer player, ushort entityId)
|
||||
{
|
||||
if (_entityMap.TryGetValue(entityId, out var entity))
|
||||
{
|
||||
if (player.IsLocal)
|
||||
_entityList.Add(entity);
|
||||
else
|
||||
_entityList.Remove(entity);
|
||||
|
||||
entity.OnOwnershipChanged(player);
|
||||
}
|
||||
else
|
||||
{
|
||||
RagonLog.Warn($"Entity {entityId} not found!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public class RagonEventCache
|
||||
{
|
||||
private readonly Dictionary<Type, ushort> _eventsRegistryByType = new();
|
||||
private readonly HashSet<ushort> _codes = new();
|
||||
private readonly HashSet<Type> _types = new();
|
||||
private ushort _eventIdGenerator = 0;
|
||||
|
||||
public ushort GetEventCode<TEvent>(TEvent _) where TEvent : IRagonEvent
|
||||
{
|
||||
var type = typeof(TEvent);
|
||||
if (!_eventsRegistryByType.TryGetValue(type, out var eventCode))
|
||||
{
|
||||
RagonLog.Error($"Event with type {type} not registered");
|
||||
return 0;
|
||||
}
|
||||
return eventCode;
|
||||
}
|
||||
|
||||
public void Register<T>() where T : IRagonEvent, new()
|
||||
{
|
||||
var type = typeof(T);
|
||||
if (_types.Contains(type))
|
||||
{
|
||||
RagonLog.Trace($"[Ragon] Event already registered: {type.Name}");
|
||||
return;
|
||||
}
|
||||
|
||||
RagonLog.Trace($"[Ragon] Registered Event: {type.Name} - {_eventIdGenerator}");
|
||||
|
||||
_eventsRegistryByType.Add(type, _eventIdGenerator);
|
||||
_codes.Add(_eventIdGenerator);
|
||||
_types.Add(type);
|
||||
|
||||
_eventIdGenerator++;
|
||||
}
|
||||
|
||||
public void Register<T>(ushort evntCode) where T : IRagonEvent, new()
|
||||
{
|
||||
var type = typeof(T);
|
||||
if (_codes.Contains(evntCode) || _types.Contains(type))
|
||||
{
|
||||
RagonLog.Warn($"[Ragon] Event already registered: {type.Name} - {evntCode}");
|
||||
return;
|
||||
}
|
||||
|
||||
RagonLog.Trace($"[Ragon] Registered Event: {type.Name} - {evntCode}");
|
||||
|
||||
_codes.Add(evntCode);
|
||||
_types.Add(type);
|
||||
_eventsRegistryByType.Add(type, evntCode);
|
||||
}
|
||||
|
||||
public T Create<T>() where T : IRagonEvent, new()
|
||||
{
|
||||
return new T();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
/*
|
||||
* 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 RagonListenerList
|
||||
{
|
||||
private readonly RagonClient _client;
|
||||
private readonly List<IRagonAuthorizationListener> _authorizationListeners = new();
|
||||
private readonly List<IRagonConnectionListener> _connectionListeners = new();
|
||||
private readonly List<IRagonFailedListener> _failedListeners = new();
|
||||
private readonly List<IRagonJoinListener> _joinListeners = new();
|
||||
private readonly List<IRagonLeftListener> _leftListeners = new();
|
||||
private readonly List<IRagonSceneListener> _sceneListeners = new();
|
||||
private readonly List<IRagonSceneRequestListener> _sceneRequestListeners = new();
|
||||
private readonly List<IRagonOwnershipChangedListener> _ownershipChangedListeners = new();
|
||||
private readonly List<IRagonPlayerJoinListener> _playerJoinListeners = 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();
|
||||
|
||||
public RagonListenerList(RagonClient client)
|
||||
{
|
||||
_client = client;
|
||||
}
|
||||
|
||||
public void Add(IRagonListener listener)
|
||||
{
|
||||
_authorizationListeners.Add(listener);
|
||||
_connectionListeners.Add(listener);
|
||||
_failedListeners.Add(listener);
|
||||
_joinListeners.Add(listener);
|
||||
_leftListeners.Add(listener);
|
||||
_sceneListeners.Add(listener);
|
||||
_ownershipChangedListeners.Add(listener);
|
||||
_playerJoinListeners.Add(listener);
|
||||
_playerLeftListeners.Add(listener);
|
||||
_roomUserDataListeners.Add(listener);
|
||||
_playerUserDataListeners.Add(listener);
|
||||
}
|
||||
|
||||
public void Remove(IRagonListener listener)
|
||||
{
|
||||
_delayedActions.Add(() =>
|
||||
{
|
||||
_authorizationListeners.Remove(listener);
|
||||
_connectionListeners.Remove(listener);
|
||||
_failedListeners.Remove(listener);
|
||||
_joinListeners.Remove(listener);
|
||||
_leftListeners.Remove(listener);
|
||||
_sceneListeners.Remove(listener);
|
||||
_ownershipChangedListeners.Remove(listener);
|
||||
_playerJoinListeners.Remove(listener);
|
||||
_playerLeftListeners.Remove(listener);
|
||||
_roomUserDataListeners.Remove(listener);
|
||||
_playerUserDataListeners.Remove(listener);
|
||||
});
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
foreach (var action in _delayedActions)
|
||||
action.Invoke();
|
||||
|
||||
_delayedActions.Clear();
|
||||
}
|
||||
|
||||
public void Add(IRagonDataListener dataListener)
|
||||
{
|
||||
_dataListeners.Add(dataListener);
|
||||
}
|
||||
|
||||
public void Add(IRagonAuthorizationListener listener)
|
||||
{
|
||||
_authorizationListeners.Add(listener);
|
||||
}
|
||||
|
||||
public void Add(IRagonSceneRequestListener listener)
|
||||
{
|
||||
_sceneRequestListeners.Add(listener);
|
||||
}
|
||||
|
||||
public void Add(IRagonConnectionListener listener)
|
||||
{
|
||||
_connectionListeners.Add(listener);
|
||||
}
|
||||
|
||||
public void Add(IRagonFailedListener listener)
|
||||
{
|
||||
_failedListeners.Add(listener);
|
||||
}
|
||||
|
||||
public void Add(IRagonJoinListener listener)
|
||||
{
|
||||
_joinListeners.Add(listener);
|
||||
}
|
||||
|
||||
public void Add(IRagonLeftListener listener)
|
||||
{
|
||||
_leftListeners.Add(listener);
|
||||
}
|
||||
|
||||
public void Add(IRagonSceneListener listener)
|
||||
{
|
||||
_sceneListeners.Add(listener);
|
||||
}
|
||||
|
||||
public void Add(IRagonOwnershipChangedListener listener)
|
||||
{
|
||||
_ownershipChangedListeners.Add(listener);
|
||||
}
|
||||
|
||||
public void Add(IRagonPlayerJoinListener listener)
|
||||
{
|
||||
_playerJoinListeners.Add(listener);
|
||||
}
|
||||
|
||||
public void Add(IRagonPlayerLeftListener 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)
|
||||
{
|
||||
_delayedActions.Add(() => _sceneRequestListeners.Remove(listener));
|
||||
}
|
||||
|
||||
public void Remove(IRagonAuthorizationListener listener)
|
||||
{
|
||||
_delayedActions.Add(() => _authorizationListeners.Remove(listener));
|
||||
}
|
||||
|
||||
public void Remove(IRagonConnectionListener listener)
|
||||
{
|
||||
_delayedActions.Add(() => _connectionListeners.Remove(listener));
|
||||
}
|
||||
|
||||
public void Remove(IRagonFailedListener listener)
|
||||
{
|
||||
_delayedActions.Add(() => _failedListeners.Remove(listener));
|
||||
}
|
||||
|
||||
public void Remove(IRagonJoinListener listener)
|
||||
{
|
||||
_delayedActions.Add(() => _joinListeners.Remove(listener));
|
||||
}
|
||||
|
||||
public void Remove(IRagonLeftListener listener)
|
||||
{
|
||||
_delayedActions.Add(() => _leftListeners.Remove(listener));
|
||||
}
|
||||
|
||||
public void Remove(IRagonSceneListener listener)
|
||||
{
|
||||
_delayedActions.Add(() => _sceneListeners.Remove(listener));
|
||||
}
|
||||
|
||||
public void Remove(IRagonOwnershipChangedListener listener)
|
||||
{
|
||||
_delayedActions.Add(() => _ownershipChangedListeners.Remove(listener));
|
||||
}
|
||||
|
||||
public void Remove(IRagonPlayerJoinListener listener)
|
||||
{
|
||||
_delayedActions.Add(() => _playerJoinListeners.Remove(listener));
|
||||
}
|
||||
|
||||
public void Remove(IRagonPlayerLeftListener 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)
|
||||
{
|
||||
foreach (var listener in _authorizationListeners)
|
||||
listener.OnAuthorizationSuccess(_client, playerId, playerName);
|
||||
}
|
||||
|
||||
public void OnAuthorizationFailed(string message)
|
||||
{
|
||||
foreach (var listener in _authorizationListeners)
|
||||
listener.OnAuthorizationFailed(_client, message);
|
||||
}
|
||||
|
||||
public void OnLeft()
|
||||
{
|
||||
foreach (var listener in _leftListeners)
|
||||
listener.OnLeft(_client);
|
||||
}
|
||||
|
||||
public void OnFailed(string message)
|
||||
{
|
||||
foreach (var listener in _failedListeners)
|
||||
listener.OnFailed(_client, message);
|
||||
}
|
||||
|
||||
public void OnOwnershipChanged(RagonPlayer player)
|
||||
{
|
||||
foreach (var listener in _ownershipChangedListeners)
|
||||
listener.OnOwnershipChanged(_client, player);
|
||||
}
|
||||
|
||||
public void OnPlayerLeft(RagonPlayer player)
|
||||
{
|
||||
foreach (var listener in _playerLeftListeners)
|
||||
listener.OnPlayerLeft(_client, player);
|
||||
}
|
||||
|
||||
public void OnPlayerJoined(RagonPlayer player)
|
||||
{
|
||||
foreach (var listener in _playerJoinListeners)
|
||||
listener.OnPlayerJoined(_client, player);
|
||||
}
|
||||
|
||||
public void OnSceneLoaded()
|
||||
{
|
||||
foreach (var listener in _sceneListeners)
|
||||
listener.OnSceneLoaded(_client);
|
||||
}
|
||||
|
||||
public void OnSceneRequest(string sceneName)
|
||||
{
|
||||
foreach (var listener in _sceneRequestListeners)
|
||||
listener.OnRequestScene(_client, sceneName);
|
||||
}
|
||||
|
||||
public void OnJoined()
|
||||
{
|
||||
foreach (var listener in _joinListeners)
|
||||
listener.OnJoined(_client);
|
||||
}
|
||||
|
||||
public void OnConnected()
|
||||
{
|
||||
foreach (var listener in _connectionListeners)
|
||||
listener.OnConnected(_client);
|
||||
}
|
||||
|
||||
public void OnDisconnected(RagonDisconnect disconnect)
|
||||
{
|
||||
foreach (var listener in _connectionListeners)
|
||||
listener.OnDisconnected(_client, disconnect);
|
||||
}
|
||||
|
||||
public void OnData(RagonPlayer player, byte[] data)
|
||||
{
|
||||
foreach (var listener in _dataListeners)
|
||||
listener.OnData(player, data);
|
||||
}
|
||||
|
||||
public void OnRoomList(RagonRoomInformation[] roomInfos)
|
||||
{
|
||||
foreach (var listListener in _roomListListeners)
|
||||
listListener.OnRoomListUpdate(roomInfos);
|
||||
}
|
||||
|
||||
public void OnRoomUserData()
|
||||
{
|
||||
foreach (var userDataListener in _roomUserDataListeners)
|
||||
userDataListener.OnUserDataUpdated(_client);
|
||||
}
|
||||
|
||||
public void OnPlayerUserData(RagonPlayer player)
|
||||
{
|
||||
foreach(var playerUserDataListener in _playerUserDataListeners)
|
||||
playerUserDataListener.OnPlayerUserDataUpdated(_client, player);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
[Serializable]
|
||||
public class RagonPlayer
|
||||
{
|
||||
public string Id { get; private set; }
|
||||
public string Name { get; set; }
|
||||
public ushort PeerId { get; set; }
|
||||
public bool IsRoomOwner { 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)
|
||||
{
|
||||
PeerId = peerId;
|
||||
IsRoomOwner = isRoomOwner;
|
||||
IsLocal = isLocal;
|
||||
Name = name;
|
||||
Id = playerId;
|
||||
UserData = isLocal ? new RagonUserData() : new RagonUserDataReadOnly();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public sealed class RagonPlayerCache
|
||||
{
|
||||
private readonly List<RagonPlayer> _players = new();
|
||||
private readonly Dictionary<string, RagonPlayer> _playersById = new();
|
||||
private readonly Dictionary<ushort, RagonPlayer> _playersByConnection = new();
|
||||
|
||||
public IReadOnlyList<RagonPlayer> Players => _players;
|
||||
public RagonPlayer Owner { get; private set; }
|
||||
public RagonPlayer Local { get; private set; }
|
||||
public bool IsRoomOwner => _ownerId == _localId;
|
||||
|
||||
public RagonPlayer? GetPlayerById(string playerId)
|
||||
{
|
||||
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 _localId;
|
||||
|
||||
public void SetOwnerAndLocal(string ownerId, string localId)
|
||||
{
|
||||
_ownerId = ownerId;
|
||||
_localId = localId;
|
||||
}
|
||||
|
||||
public void AddPlayer(ushort peerId, string playerId, string playerName)
|
||||
{
|
||||
if (_playersById.ContainsKey(playerId))
|
||||
return;
|
||||
|
||||
var isOwner = playerId == _ownerId;
|
||||
var isLocal = playerId == _localId;
|
||||
|
||||
RagonLog.Trace($"Added player {peerId}|{playerId}|{playerName} IsOwner: {isOwner} isLocal: {isLocal}");
|
||||
|
||||
var player = new RagonPlayer(peerId, playerId, playerName, isOwner, isLocal);
|
||||
|
||||
if (player.IsLocal)
|
||||
Local = player;
|
||||
|
||||
if (player.IsRoomOwner)
|
||||
Owner = player;
|
||||
|
||||
_players.Add(player);
|
||||
_playersById.Add(player.Id, player);
|
||||
_playersByConnection.Add(player.PeerId, player);
|
||||
}
|
||||
|
||||
public void RemovePlayer(string playerId)
|
||||
{
|
||||
if (_playersById.TryGetValue(playerId, out var player))
|
||||
{
|
||||
_players.Remove(player);
|
||||
_playersById.Remove(playerId);
|
||||
_playersByConnection.Remove(player.PeerId);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnOwnershipChanged(ushort playerPeerId)
|
||||
{
|
||||
foreach (var player in _players)
|
||||
{
|
||||
if (player.PeerId == playerPeerId)
|
||||
{
|
||||
Owner = player;
|
||||
Owner.IsRoomOwner = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Cleanup()
|
||||
{
|
||||
_players.Clear();
|
||||
_playersByConnection.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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* 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 RagonRoom : 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 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 IReadOnlyList<RagonPlayer> Players => _playerCache.Players;
|
||||
public RagonPlayer Local => _playerCache.Local;
|
||||
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,
|
||||
RagonEntityCache entityCache,
|
||||
RagonPlayerCache playerCache,
|
||||
RoomParameters parameters,
|
||||
RagonScene scene)
|
||||
{
|
||||
_client = client;
|
||||
_parameters = parameters;
|
||||
_entityCache = entityCache;
|
||||
_playerCache = playerCache;
|
||||
_scene = scene;
|
||||
_userData = new RagonUserData();
|
||||
}
|
||||
|
||||
internal void Cleanup()
|
||||
{
|
||||
_entityCache.Cleanup();
|
||||
_playerCache.Cleanup();
|
||||
}
|
||||
|
||||
internal void Update(string 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 on entity {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 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, RagonPayload payload) => _entityCache.Create(entity, payload);
|
||||
public void TransferEntity(RagonEntity entity, RagonPlayer player) => _entityCache.Transfer(entity, player);
|
||||
|
||||
public void DestroyEntity(RagonEntity entityId) => DestroyEntity(entityId, null);
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client;
|
||||
|
||||
public class RagonScene
|
||||
{
|
||||
public string Name { get; private set; }
|
||||
|
||||
private readonly RagonClient _client;
|
||||
private readonly RagonEntityCache _entityCache;
|
||||
private readonly RagonPlayerCache _playerCache;
|
||||
|
||||
public RagonScene(RagonClient client, RagonPlayerCache playerCache, RagonEntityCache entityCache, string sceneName)
|
||||
{
|
||||
Name = sceneName;
|
||||
|
||||
_client = client;
|
||||
_playerCache = playerCache;
|
||||
_entityCache = entityCache;
|
||||
}
|
||||
|
||||
internal void Update(string scene)
|
||||
{
|
||||
Name = scene;
|
||||
}
|
||||
|
||||
internal void Load(string sceneName)
|
||||
{
|
||||
var buffer = _client.Buffer;
|
||||
|
||||
buffer.Clear();
|
||||
buffer.WriteOperation(RagonOperation.LOAD_SCENE);
|
||||
buffer.WriteString(sceneName);
|
||||
|
||||
var sendData = buffer.ToArray();
|
||||
_client.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
internal void SceneLoaded()
|
||||
{
|
||||
var buffer = _client.Buffer;
|
||||
|
||||
buffer.Clear();
|
||||
buffer.WriteOperation(RagonOperation.SCENE_LOADED);
|
||||
|
||||
if (_playerCache.IsRoomOwner)
|
||||
_entityCache.WriteScene(buffer);
|
||||
else
|
||||
_entityCache.CacheScene();
|
||||
|
||||
var sendData = buffer.ToArray();
|
||||
_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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* 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 Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
public class RagonSession
|
||||
{
|
||||
private readonly RagonClient _client;
|
||||
private readonly RagonBuffer _buffer;
|
||||
|
||||
public RagonSession(RagonClient client, RagonBuffer buffer)
|
||||
{
|
||||
_client = client;
|
||||
_buffer = buffer;
|
||||
}
|
||||
|
||||
public void CreateOrJoin(string sceneName, int minPlayers, int maxPlayers)
|
||||
{
|
||||
var parameters = new RagonRoomPayload() {Scene = sceneName, Min = minPlayers, Max = maxPlayers};
|
||||
CreateOrJoin(parameters);
|
||||
}
|
||||
|
||||
public void CreateOrJoin(RagonRoomPayload parameters)
|
||||
{
|
||||
_buffer.Clear();
|
||||
_buffer.WriteOperation(RagonOperation.JOIN_OR_CREATE_ROOM);
|
||||
|
||||
parameters.Serialize(_buffer);
|
||||
|
||||
var sendData = _buffer.ToArray();
|
||||
_client.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
public void Create(string sceneName, int minPlayers, int maxPlayers)
|
||||
{
|
||||
Create(null, new RagonRoomPayload() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
|
||||
}
|
||||
|
||||
public void Create(string roomId, string sceneName, int minPlayers, int maxPlayers)
|
||||
{
|
||||
Create(roomId, new RagonRoomPayload() {Scene = sceneName, Min = minPlayers, Max = maxPlayers});
|
||||
}
|
||||
|
||||
public void Create(string roomId, RagonRoomPayload parameters)
|
||||
{
|
||||
_buffer.Clear();
|
||||
_buffer.WriteOperation(RagonOperation.CREATE_ROOM);
|
||||
|
||||
if (roomId != null)
|
||||
{
|
||||
_buffer.WriteBool(true);
|
||||
_buffer.WriteString(roomId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_buffer.WriteBool(false);
|
||||
}
|
||||
|
||||
parameters.Serialize(_buffer);
|
||||
|
||||
var sendData = _buffer.ToArray();
|
||||
_client.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
public void Leave()
|
||||
{
|
||||
var sendData = new[] {(byte) RagonOperation.LEAVE_ROOM};
|
||||
_client.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
public void Join(string roomId)
|
||||
{
|
||||
_buffer.Clear();
|
||||
_buffer.WriteOperation(RagonOperation.JOIN_ROOM);
|
||||
_buffer.WriteString(roomId);
|
||||
|
||||
var sendData = _buffer.ToArray();
|
||||
_client.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
public void AuthorizeWithKey(string key, string playerName, string payload = "")
|
||||
{
|
||||
_buffer.Clear();
|
||||
_buffer.WriteOperation(RagonOperation.AUTHORIZE);
|
||||
_buffer.WriteString(key);
|
||||
_buffer.WriteString(playerName);
|
||||
_buffer.WriteString(payload);
|
||||
|
||||
var sendData = _buffer.ToArray();
|
||||
_client.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Ragon.Client
|
||||
{
|
||||
public enum RagonStatus
|
||||
{
|
||||
DISCONNECTED,
|
||||
CONNECTED,
|
||||
ROOM,
|
||||
LOBBY,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
_dirty = true;
|
||||
}
|
||||
}
|
||||
public bool Dirty => _dirty;
|
||||
|
||||
private bool _dirty = false;
|
||||
private readonly Dictionary<string, byte[]> _properties = new();
|
||||
|
||||
public RagonUserData()
|
||||
{
|
||||
}
|
||||
|
||||
public void Read(RagonBuffer buffer)
|
||||
{
|
||||
_properties.Clear();
|
||||
|
||||
var len = buffer.ReadUShort();
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var key = buffer.ReadString();
|
||||
var valueSize = buffer.ReadUShort();
|
||||
var value = buffer.ReadBytes(valueSize);
|
||||
|
||||
_properties[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(RagonBuffer buffer)
|
||||
{
|
||||
buffer.WriteUShort((ushort)_properties.Count);
|
||||
foreach (var property in _properties)
|
||||
{
|
||||
buffer.WriteString(property.Key);
|
||||
buffer.WriteUShort((ushort) property.Value.Length);
|
||||
buffer.WriteBytes(property.Value);
|
||||
}
|
||||
|
||||
_dirty = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
buffer.WriteUShort((ushort)_properties.Count);
|
||||
foreach (var property in _properties)
|
||||
{
|
||||
buffer.WriteString(property.Key);
|
||||
buffer.WriteUShort((ushort)property.Value.Length);
|
||||
buffer.WriteBytes(property.Value);
|
||||
}
|
||||
|
||||
_dirty = false;
|
||||
}
|
||||
|
||||
public void Read(RagonBuffer buffer)
|
||||
{
|
||||
_properties.Clear();
|
||||
|
||||
var len = buffer.ReadUShort();
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var key = buffer.ReadString();
|
||||
var valueSize = buffer.ReadUShort();
|
||||
var value = buffer.ReadBytes(valueSize);
|
||||
|
||||
_properties[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Ragon.Client.Compressor;
|
||||
using Ragon.Protocol;
|
||||
|
||||
namespace Ragon.Client.Utils;
|
||||
|
||||
public static class CompressorExtension
|
||||
{
|
||||
public static float Read(this FloatCompressor compressor, RagonBuffer buffer)
|
||||
{
|
||||
return compressor.Decompress(buffer.Read(compressor.RequiredBits));
|
||||
}
|
||||
|
||||
public static void Write(this FloatCompressor compressor, RagonBuffer buffer, float value)
|
||||
{
|
||||
buffer.Write(compressor.Compress(value), compressor.RequiredBits);
|
||||
}
|
||||
|
||||
public static float Read(this IntCompressor compressor, RagonBuffer buffer)
|
||||
{
|
||||
return compressor.Decompress(buffer.Read(compressor.RequiredBits));
|
||||
}
|
||||
|
||||
public static void Write(this IntCompressor compressor, RagonBuffer buffer, int value)
|
||||
{
|
||||
buffer.Write(compressor.Compress(value), compressor.RequiredBits);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Ragon.Core.Time;
|
||||
|
||||
public interface IAction
|
||||
{
|
||||
public void Tick();
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
namespace Ragon.Core.Time;
|
||||
|
||||
public class Loop
|
||||
{
|
||||
private List<IAction> _tasks;
|
||||
|
||||
public Loop()
|
||||
{
|
||||
|
||||
_tasks = new List<IAction>(35);
|
||||
}
|
||||
|
||||
public void Run(IAction task)
|
||||
{
|
||||
_tasks.Add(task);
|
||||
}
|
||||
|
||||
public void Stop(IAction task)
|
||||
{
|
||||
_tasks.Remove(task);
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
foreach (var task in _tasks)
|
||||
task.Tick();
|
||||
}
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
using System.Diagnostics;
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Lobby;
|
||||
using Ragon.Core.Server;
|
||||
using Ragon.Core.Time;
|
||||
using Ragon.Server;
|
||||
using Ragon.Server.ENet;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public class Application : INetworkListener
|
||||
{
|
||||
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private readonly INetworkServer _server;
|
||||
private readonly Thread _dedicatedThread;
|
||||
private readonly Executor _executor;
|
||||
private readonly Configuration _configuration;
|
||||
private readonly HandlerRegistry _handlerRegistry;
|
||||
private readonly ILobby _lobby;
|
||||
private readonly Loop _loop;
|
||||
private readonly Dictionary<ushort, PlayerContext> _contexts;
|
||||
|
||||
public Application(Configuration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_executor = new Executor();
|
||||
_dedicatedThread = new Thread(Execute);
|
||||
_dedicatedThread.IsBackground = true;
|
||||
_contexts = new Dictionary<ushort, PlayerContext>();
|
||||
_handlerRegistry = new HandlerRegistry();
|
||||
_lobby = new LobbyInMemory();
|
||||
_loop = new Loop();
|
||||
|
||||
if (configuration.ServerType == "enet")
|
||||
_server = new ENetServer();
|
||||
|
||||
if (configuration.ServerType == "websocket")
|
||||
_server = new NativeWebSocketServer(_executor);
|
||||
|
||||
Debug.Assert(_server != null, $"Socket type not supported: {configuration.ServerType}. Supported: [enet, websocket]");
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
_executor.Execute();
|
||||
_loop.Tick();
|
||||
_server.Poll();
|
||||
|
||||
Thread.Sleep((int)1000.0f / _configuration.ServerTickRate);
|
||||
}
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
var networkConfiguration = new NetworkConfiguration()
|
||||
{
|
||||
LimitConnections = _configuration.LimitConnections,
|
||||
Protocol = RagonVersion.Parse(_configuration.GameProtocol),
|
||||
Address = "0.0.0.0",
|
||||
Port = _configuration.Port,
|
||||
};
|
||||
|
||||
_server.Start(this, networkConfiguration);
|
||||
_dedicatedThread.Start();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_server.Stop();
|
||||
_dedicatedThread.Interrupt();
|
||||
}
|
||||
|
||||
public void OnConnected(INetworkConnection connection)
|
||||
{
|
||||
var context = new PlayerContext(connection, new LobbyPlayer(connection));
|
||||
context.Lobby = _lobby;
|
||||
context.Loop = _loop;
|
||||
|
||||
_logger.Trace($"Connected {connection.Id}");
|
||||
_contexts.Add(connection.Id, context);
|
||||
}
|
||||
|
||||
public void OnDisconnected(INetworkConnection connection)
|
||||
{
|
||||
_logger.Trace($"Disconnected {connection.Id}");
|
||||
|
||||
if (_contexts.Remove(connection.Id, out var context))
|
||||
{
|
||||
var room = context.Room;
|
||||
if (room != null)
|
||||
{
|
||||
room.RemovePlayer(context.RoomPlayer);
|
||||
|
||||
_lobby.RemoveIfEmpty(room);
|
||||
}
|
||||
|
||||
context.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnTimeout(INetworkConnection connection)
|
||||
{
|
||||
if (_contexts.Remove(connection.Id, out var context))
|
||||
context.Dispose();
|
||||
}
|
||||
|
||||
public void OnData(INetworkConnection connection, byte[] data)
|
||||
{
|
||||
if (_contexts.TryGetValue(connection.Id, out var context))
|
||||
_handlerRegistry.Handle(context, data);
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
[Serializable]
|
||||
public struct Configuration
|
||||
{
|
||||
public string ServerKey;
|
||||
public string ServerType;
|
||||
public ushort ServerTickRate;
|
||||
public string GameProtocol;
|
||||
public ushort Port;
|
||||
public int LimitConnections;
|
||||
public int LimitPlayersPerRoom;
|
||||
public int LimitRooms;
|
||||
|
||||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||
private static readonly string ServerVersion = "1.1.0-rc";
|
||||
|
||||
private static void CopyrightInfo()
|
||||
{
|
||||
Logger.Info($"Server Version: {ServerVersion}");
|
||||
Logger.Info($"Machine Name: {Environment.MachineName}");
|
||||
Logger.Info($"OS: {Environment.OSVersion}");
|
||||
Logger.Info($"Processors: {Environment.ProcessorCount}");
|
||||
Logger.Info($"Runtime Version: {Environment.Version}");
|
||||
Logger.Info("==================================");
|
||||
Logger.Info("| |");
|
||||
Logger.Info("| Ragon |");
|
||||
Logger.Info("| |");
|
||||
Logger.Info("==================================");
|
||||
}
|
||||
|
||||
public static Configuration Load(string filePath)
|
||||
{
|
||||
CopyrightInfo();
|
||||
|
||||
var data = File.ReadAllText(filePath);
|
||||
var configuration = JsonConvert.DeserializeObject<Configuration>(data);
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
@@ -1,189 +0,0 @@
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class Entity
|
||||
{
|
||||
private static ushort _idGenerator = 0;
|
||||
|
||||
public ushort Id { get; private set; }
|
||||
public RoomPlayer Owner { get; private set; }
|
||||
public RagonAuthority Authority { get; private set; }
|
||||
public EntityState State { get; private set; }
|
||||
public byte[] Payload { get; private set; }
|
||||
public ushort StaticId { get; private set; }
|
||||
public ushort Type { get; private set; }
|
||||
|
||||
private List<EntityEvent> _bufferedEvents;
|
||||
|
||||
public Entity(RoomPlayer owner, ushort type, ushort staticId, RagonAuthority eventAuthority)
|
||||
{
|
||||
Owner = owner;
|
||||
StaticId = staticId;
|
||||
Type = type;
|
||||
Id = _idGenerator++;
|
||||
Payload = Array.Empty<byte>();
|
||||
Authority = eventAuthority;
|
||||
State = new EntityState(this);
|
||||
|
||||
_bufferedEvents = new List<EntityEvent>();
|
||||
}
|
||||
|
||||
public void SetPayload(byte[] payload)
|
||||
{
|
||||
Payload = payload;
|
||||
}
|
||||
|
||||
public void SetOwner(RoomPlayer owner)
|
||||
{
|
||||
Owner = owner;
|
||||
}
|
||||
|
||||
public void RestoreBufferedEvents(RoomPlayer roomPlayer, RagonSerializer writer)
|
||||
{
|
||||
foreach (var bufferedEvent in _bufferedEvents)
|
||||
{
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
||||
writer.WriteUShort(bufferedEvent.EventId);
|
||||
writer.WriteUShort(bufferedEvent.Invoker.Connection.Id);
|
||||
writer.WriteByte((byte)RagonReplicationMode.Server);
|
||||
writer.WriteUShort(Id);
|
||||
|
||||
ReadOnlySpan<byte> data = bufferedEvent.EventData.AsSpan();
|
||||
writer.WriteData(ref data);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
roomPlayer.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
}
|
||||
|
||||
public void Create()
|
||||
{
|
||||
var room = Owner.Room;
|
||||
var serializer = room.Writer;
|
||||
|
||||
serializer.Clear();
|
||||
serializer.WriteOperation(RagonOperation.CREATE_ENTITY);
|
||||
serializer.WriteUShort(Type);
|
||||
serializer.WriteUShort(Id);
|
||||
serializer.WriteUShort(Owner.Connection.Id);
|
||||
|
||||
ReadOnlySpan<byte> entityPayload = Payload.AsSpan();
|
||||
serializer.WriteUShort((ushort)entityPayload.Length);
|
||||
serializer.WriteData(ref entityPayload);
|
||||
|
||||
var sendData = serializer.ToArray();
|
||||
foreach (var player in room.ReadyPlayersList)
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
public void Destroy(byte[] payload)
|
||||
{
|
||||
var room = Owner.Room;
|
||||
var serializer = room.Writer;
|
||||
|
||||
serializer.Clear();
|
||||
serializer.WriteOperation(RagonOperation.DESTROY_ENTITY);
|
||||
serializer.WriteInt(Id);
|
||||
serializer.WriteUShort(0);
|
||||
// serializer.WriteData(ref Payload);
|
||||
|
||||
var sendData = serializer.ToArray();
|
||||
foreach (var player in room.ReadyPlayersList)
|
||||
player.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
public void ReplicateEvent(
|
||||
RoomPlayer caller,
|
||||
ushort eventId,
|
||||
ReadOnlySpan<byte> payload,
|
||||
RagonReplicationMode eventMode,
|
||||
RoomPlayer targetPlayer
|
||||
)
|
||||
{
|
||||
var room = Owner.Room;
|
||||
var serializer = room.Writer;
|
||||
|
||||
serializer.Clear();
|
||||
serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
||||
serializer.WriteUShort(eventId);
|
||||
serializer.WriteUShort(caller.Connection.Id);
|
||||
serializer.WriteByte((byte)eventMode);
|
||||
serializer.WriteUShort(Id);
|
||||
serializer.WriteData(ref payload);
|
||||
|
||||
var sendData = serializer.ToArray();
|
||||
targetPlayer.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
public void ReplicateEvent(
|
||||
RoomPlayer caller,
|
||||
ushort eventId,
|
||||
ReadOnlySpan<byte> payload,
|
||||
RagonReplicationMode eventMode,
|
||||
RagonTarget targetMode
|
||||
)
|
||||
{
|
||||
if (Authority == RagonAuthority.OwnerOnly &&
|
||||
Owner.Connection.Id != caller.Connection.Id)
|
||||
{
|
||||
Console.WriteLine($"Player have not enought authority for event with Id {eventId}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (eventMode == RagonReplicationMode.Buffered && targetMode != RagonTarget.Owner)
|
||||
{
|
||||
var bufferedEvent = new EntityEvent(caller, eventId, payload.ToArray(), targetMode);
|
||||
_bufferedEvents.Add(bufferedEvent);
|
||||
}
|
||||
|
||||
var room = Owner.Room;
|
||||
var serializer = room.Writer;
|
||||
|
||||
serializer.Clear();
|
||||
serializer.WriteOperation(RagonOperation.REPLICATE_ENTITY_EVENT);
|
||||
serializer.WriteUShort(eventId);
|
||||
serializer.WriteUShort(caller.Connection.Id);
|
||||
serializer.WriteByte((byte)eventMode);
|
||||
serializer.WriteUShort(Id);
|
||||
serializer.WriteData(ref payload);
|
||||
|
||||
var sendData = serializer.ToArray();
|
||||
|
||||
switch (targetMode)
|
||||
{
|
||||
case RagonTarget.Owner:
|
||||
{
|
||||
Owner.Connection.Reliable.Send(sendData);
|
||||
break;
|
||||
}
|
||||
case RagonTarget.ExceptOwner:
|
||||
{
|
||||
foreach (var roomPlayer in room.ReadyPlayersList)
|
||||
{
|
||||
if (roomPlayer.Connection.Id != Owner.Connection.Id)
|
||||
roomPlayer.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case RagonTarget.ExceptInvoker:
|
||||
{
|
||||
foreach (var roomPlayer in room.ReadyPlayersList)
|
||||
{
|
||||
if (roomPlayer.Connection.Id != caller.Connection.Id)
|
||||
roomPlayer.Connection.Reliable.Send(sendData);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case RagonTarget.All:
|
||||
{
|
||||
foreach (var roomPlayer in room.ReadyPlayersList)
|
||||
roomPlayer.Connection.Reliable.Send(sendData);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class EntityEvent
|
||||
{
|
||||
public RoomPlayer Invoker { get; private set; }
|
||||
public ushort EventId { get; private set; }
|
||||
public byte[] EventData { get; private set; }
|
||||
public RagonTarget Target { set; private get; }
|
||||
|
||||
public EntityEvent(
|
||||
RoomPlayer invoker,
|
||||
ushort eventId,
|
||||
byte[] payload,
|
||||
RagonTarget target
|
||||
)
|
||||
{
|
||||
Invoker = invoker;
|
||||
EventId = eventId;
|
||||
EventData = payload;
|
||||
Target = target;
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class EntityList
|
||||
{
|
||||
private readonly List<Entity> _dynamicEntitiesList = new List<Entity>();
|
||||
private readonly List<Entity> _staticEntitiesList = new List<Entity>();
|
||||
private readonly Dictionary<ushort, Entity> _entitiesMap = new Dictionary<ushort, Entity>();
|
||||
|
||||
public IReadOnlyList<Entity> StaticList => _staticEntitiesList;
|
||||
public IReadOnlyList<Entity> DynamicList => _dynamicEntitiesList;
|
||||
public IReadOnlyDictionary<ushort, Entity> Map => _entitiesMap;
|
||||
|
||||
public void Add(Entity entity)
|
||||
{
|
||||
if (entity.StaticId != 0)
|
||||
_staticEntitiesList.Add(entity);
|
||||
else
|
||||
_dynamicEntitiesList.Add(entity);
|
||||
|
||||
_entitiesMap.Add(entity.Id, entity);
|
||||
}
|
||||
|
||||
public bool Remove(Entity entity)
|
||||
{
|
||||
if (_entitiesMap.Remove(entity.Id, out var existEntity))
|
||||
{
|
||||
_staticEntitiesList.Remove(entity);
|
||||
_dynamicEntitiesList.Remove(entity);
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class EntityState
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private List<EntityStateProperty> _properties;
|
||||
private Entity _entity;
|
||||
|
||||
public EntityState(Entity entity, int capacity = 10)
|
||||
{
|
||||
_entity = entity;
|
||||
_properties = new List<EntityStateProperty>(10);
|
||||
}
|
||||
|
||||
public void AddProperty(EntityStateProperty property)
|
||||
{
|
||||
_properties.Add(property);
|
||||
}
|
||||
|
||||
public void Write(RagonSerializer serializer)
|
||||
{
|
||||
serializer.WriteUShort(_entity.Id);
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < _properties.Count; propertyIndex++)
|
||||
{
|
||||
var property = _properties[propertyIndex];
|
||||
if (property.IsDirty)
|
||||
{
|
||||
serializer.WriteBool(true);
|
||||
var span = serializer.GetWritableData(property.Size);
|
||||
var data = property.Read();
|
||||
data.CopyTo(span);
|
||||
property.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
serializer.WriteBool(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Read(RagonSerializer serializer)
|
||||
{
|
||||
for (var i = 0; i < _properties.Count; i++)
|
||||
{
|
||||
if (serializer.ReadBool())
|
||||
{
|
||||
var property = _properties[i];
|
||||
var size = property.Size;
|
||||
if (!property.IsFixed)
|
||||
size = serializer.ReadUShort();
|
||||
|
||||
if (size > property.Capacity)
|
||||
{
|
||||
Console.WriteLine($"Property {i} payload too large, size: {size}");
|
||||
continue;
|
||||
}
|
||||
|
||||
var propertyPayload = serializer.ReadData(size);
|
||||
property.Write(ref propertyPayload);
|
||||
property.Size = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Snapshot(RagonSerializer serializer)
|
||||
{
|
||||
ReadOnlySpan<byte> payload = _entity.Payload.AsSpan();
|
||||
|
||||
serializer.WriteUShort(_entity.Type);
|
||||
serializer.WriteUShort(_entity.Id);
|
||||
|
||||
if (_entity.StaticId != 0)
|
||||
serializer.WriteUShort(_entity.StaticId);
|
||||
|
||||
serializer.WriteUShort(_entity.Owner.Connection.Id);
|
||||
serializer.WriteUShort((ushort) payload.Length);
|
||||
serializer.WriteData(ref payload);
|
||||
|
||||
for (int propertyIndex = 0; propertyIndex < _properties.Count; propertyIndex++)
|
||||
{
|
||||
var property = _properties[propertyIndex];
|
||||
var hasPayload = property.IsFixed || property.Size > 0 && !property.IsFixed;
|
||||
if (hasPayload)
|
||||
{
|
||||
serializer.WriteBool(true);
|
||||
var span = serializer.GetWritableData(property.Size);
|
||||
var data = property.Read();
|
||||
data.CopyTo(span);
|
||||
}
|
||||
else
|
||||
{
|
||||
serializer.WriteBool(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
using System;
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class EntityStateProperty
|
||||
{
|
||||
public int Size { get; set; }
|
||||
public int Capacity { get; set; }
|
||||
public bool IsDirty { get; private set; }
|
||||
public bool IsFixed { get; private set; }
|
||||
private byte[] _data;
|
||||
|
||||
public EntityStateProperty(int size, bool isFixed)
|
||||
{
|
||||
Capacity = 512;
|
||||
Size = size;
|
||||
IsFixed = isFixed;
|
||||
IsDirty = true;
|
||||
|
||||
_data = new byte[Capacity];
|
||||
}
|
||||
|
||||
public ReadOnlySpan<byte> Read()
|
||||
{
|
||||
var dataSpan = _data.AsSpan();
|
||||
|
||||
return dataSpan.Slice(0, Size);
|
||||
}
|
||||
|
||||
public void Write(ref ReadOnlySpan<byte> src)
|
||||
{
|
||||
src.CopyTo(_data);
|
||||
IsDirty = true;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
IsDirty = false;
|
||||
}
|
||||
}
|
||||
@@ -1,166 +0,0 @@
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Time;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class Room: IAction
|
||||
{
|
||||
public string Id { get; private set; }
|
||||
public RoomInformation Info { get; private set; }
|
||||
public RoomPlayer Owner { get; private set; }
|
||||
public RagonSerializer Writer { get; }
|
||||
public Dictionary<ushort, RoomPlayer> Players { get; private set; }
|
||||
public List<RoomPlayer> WaitPlayersList { get; private set; }
|
||||
public List<RoomPlayer> ReadyPlayersList { get; private set; }
|
||||
public List<RoomPlayer> PlayerList { get; private set; }
|
||||
|
||||
public Dictionary<ushort, Entity> Entities { get; private set; }
|
||||
public List<Entity> DynamicEntitiesList { get; private set; }
|
||||
public List<Entity> StaticEntitiesList { get; private set; }
|
||||
public List<Entity> EntityList { get; private set; }
|
||||
|
||||
private readonly HashSet<Entity> _entitiesDirtySet;
|
||||
|
||||
public Room(string roomId, RoomInformation info)
|
||||
{
|
||||
Id = roomId;
|
||||
Info = info;
|
||||
|
||||
Players = new Dictionary<ushort, RoomPlayer>(info.Max);
|
||||
WaitPlayersList = new List<RoomPlayer>(info.Max);
|
||||
ReadyPlayersList = new List<RoomPlayer>(info.Max);
|
||||
PlayerList = new List<RoomPlayer>(info.Max);
|
||||
|
||||
Entities = new Dictionary<ushort, Entity>();
|
||||
DynamicEntitiesList = new List<Entity>();
|
||||
StaticEntitiesList = new List<Entity>();
|
||||
EntityList = new List<Entity>();
|
||||
|
||||
_entitiesDirtySet = new HashSet<Entity>();
|
||||
Writer = new RagonSerializer(512);
|
||||
}
|
||||
|
||||
public void AttachEntity(RoomPlayer newOwner, Entity entity)
|
||||
{
|
||||
Entities.Add(entity.Id, entity);
|
||||
EntityList.Add(entity);
|
||||
|
||||
if (entity.StaticId == 0)
|
||||
DynamicEntitiesList.Add(entity);
|
||||
else
|
||||
StaticEntitiesList.Add(entity);
|
||||
|
||||
entity.Create();
|
||||
|
||||
newOwner.Entities.Add(entity);
|
||||
}
|
||||
|
||||
public void DetachEntity(RoomPlayer currentOwner, Entity entity, byte[] payload)
|
||||
{
|
||||
Entities.Remove(entity.Id);
|
||||
EntityList.Remove(entity);
|
||||
StaticEntitiesList.Remove(entity);
|
||||
DynamicEntitiesList.Remove(entity);
|
||||
_entitiesDirtySet.Remove(entity);
|
||||
|
||||
entity.Destroy(payload);
|
||||
currentOwner.Entities.Remove(entity);
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
var entities = (ushort) _entitiesDirtySet.Count;
|
||||
if (entities > 0)
|
||||
{
|
||||
Writer.Clear();
|
||||
Writer.WriteOperation(RagonOperation.REPLICATE_ENTITY_STATE);
|
||||
Writer.WriteUShort(entities);
|
||||
|
||||
foreach (var entity in _entitiesDirtySet)
|
||||
entity.State.Write(Writer);
|
||||
|
||||
_entitiesDirtySet.Clear();
|
||||
|
||||
var sendData = Writer.ToArray();
|
||||
foreach (var roomPlayer in ReadyPlayersList)
|
||||
roomPlayer.Connection.Unreliable.Send(sendData);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddPlayer(RoomPlayer player)
|
||||
{
|
||||
if (Players.Count == 0)
|
||||
Owner = player;
|
||||
|
||||
player.Attach(this);
|
||||
|
||||
PlayerList.Add(player);
|
||||
Players.Add(player.Connection.Id, player);
|
||||
}
|
||||
|
||||
public void RemovePlayer(RoomPlayer roomPlayer)
|
||||
{
|
||||
if (Players.Remove(roomPlayer.Connection.Id, out var player))
|
||||
{
|
||||
PlayerList.Remove(player);
|
||||
|
||||
{
|
||||
Writer.Clear();
|
||||
Writer.WriteOperation(RagonOperation.PLAYER_LEAVED);
|
||||
Writer.WriteString(player.Id);
|
||||
|
||||
var entitiesToDelete = player.Entities.DynamicList;
|
||||
Writer.WriteUShort((ushort) entitiesToDelete.Count);
|
||||
foreach (var entity in entitiesToDelete)
|
||||
{
|
||||
Writer.WriteUShort(entity.Id);
|
||||
EntityList.Remove(entity);
|
||||
}
|
||||
|
||||
var sendData = Writer.ToArray();
|
||||
Broadcast(sendData);
|
||||
}
|
||||
|
||||
if (roomPlayer.Connection.Id == Owner.Connection.Id && PlayerList.Count > 0)
|
||||
{
|
||||
var nextOwner = PlayerList[0];
|
||||
|
||||
Owner = nextOwner;
|
||||
|
||||
var entitiesToUpdate = roomPlayer.Entities.StaticList;
|
||||
|
||||
Writer.Clear();
|
||||
Writer.WriteOperation(RagonOperation.OWNERSHIP_CHANGED);
|
||||
Writer.WriteString(Owner.Id);
|
||||
Writer.WriteUShort((ushort) entitiesToUpdate.Count);
|
||||
|
||||
foreach (var entity in entitiesToUpdate)
|
||||
{
|
||||
Writer.WriteUShort(entity.Id);
|
||||
|
||||
entity.SetOwner(nextOwner);
|
||||
nextOwner.Entities.Add(entity);
|
||||
}
|
||||
|
||||
var sendData = Writer.ToArray();
|
||||
Broadcast(sendData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateReadyPlayerList()
|
||||
{
|
||||
ReadyPlayersList = PlayerList.Where(p => p.IsLoaded).ToList();
|
||||
}
|
||||
|
||||
public void Track(Entity entity)
|
||||
{
|
||||
_entitiesDirtySet.Add(entity);
|
||||
}
|
||||
|
||||
public void Broadcast(byte[] data)
|
||||
{
|
||||
foreach (var readyPlayer in ReadyPlayersList)
|
||||
readyPlayer.Connection.Reliable.Send(data);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class RoomInformation
|
||||
{
|
||||
public string Map { get; init; } = "none";
|
||||
public int Min { get; init; }
|
||||
public int Max { get; init; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Map: {Map} Count: {Min}/{Max}";
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
using Ragon.Server;
|
||||
|
||||
namespace Ragon.Core.Game;
|
||||
|
||||
public class RoomPlayer
|
||||
{
|
||||
public INetworkConnection Connection { get; }
|
||||
public string Id { get; }
|
||||
public string Name { get; }
|
||||
public bool IsLoaded { get; private set; }
|
||||
public Room Room { get; private set; }
|
||||
public EntityList Entities { get; private set; }
|
||||
|
||||
public RoomPlayer(INetworkConnection connection, string id, string name)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
Connection = connection;
|
||||
Entities = new EntityList();
|
||||
}
|
||||
|
||||
public void Attach(Room room)
|
||||
{
|
||||
Room = room;
|
||||
}
|
||||
|
||||
public void Detach()
|
||||
{
|
||||
Room = null!;
|
||||
}
|
||||
|
||||
public void SetReady()
|
||||
{
|
||||
IsLoaded = true;
|
||||
}
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Handlers;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public sealed class HandlerRegistry
|
||||
{
|
||||
private IHandler _entityEventHandler;
|
||||
private IHandler _entityCreateHandler;
|
||||
private IHandler _entityDestroyHandler;
|
||||
private IHandler _entityStateHandler;
|
||||
private IHandler _sceneLoadedHandler;
|
||||
|
||||
private IHandler _authorizationHandler;
|
||||
private IHandler _joinOrCreateHandler;
|
||||
private IHandler _createHandler;
|
||||
private IHandler _joinHandler;
|
||||
private IHandler _leaveHandler;
|
||||
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private RagonSerializer _reader;
|
||||
private RagonSerializer _writer;
|
||||
|
||||
public HandlerRegistry()
|
||||
{
|
||||
_reader = new RagonSerializer(2048);
|
||||
_writer = new RagonSerializer(2048);
|
||||
|
||||
_authorizationHandler = new AuthorizationHandler();
|
||||
_joinOrCreateHandler = new JoinOrCreateHandler();
|
||||
_sceneLoadedHandler = new SceneLoadedHandler();
|
||||
_createHandler = new CreateHandler();
|
||||
_joinHandler = new JoinHandler();
|
||||
_leaveHandler = new LeaveHandler();
|
||||
|
||||
_entityEventHandler = new EntityEventHandler();
|
||||
_entityCreateHandler = new EntityCreateHandler();
|
||||
_entityDestroyHandler = new EntityDestroyHandler();
|
||||
_entityStateHandler = new EntityStateHandler();
|
||||
}
|
||||
|
||||
public void Handle(PlayerContext context, byte[] data)
|
||||
{
|
||||
_writer.Clear();
|
||||
_reader.Clear();
|
||||
_reader.FromArray(data);
|
||||
|
||||
var operation = _reader.ReadOperation();
|
||||
switch (operation)
|
||||
{
|
||||
case RagonOperation.REPLICATE_ENTITY_EVENT:
|
||||
{
|
||||
if (context.RoomPlayer != null)
|
||||
_entityEventHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.REPLICATE_ENTITY_STATE:
|
||||
{
|
||||
if (context.RoomPlayer != null)
|
||||
_entityStateHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.CREATE_ENTITY:
|
||||
{
|
||||
if (context.RoomPlayer != null)
|
||||
_entityCreateHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.DESTROY_ENTITY:
|
||||
{
|
||||
if (context.RoomPlayer != null)
|
||||
_entityDestroyHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.SCENE_LOADED:
|
||||
{
|
||||
if (context.RoomPlayer != null)
|
||||
_sceneLoadedHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.JOIN_OR_CREATE_ROOM:
|
||||
{
|
||||
_joinOrCreateHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.CREATE_ROOM:
|
||||
{
|
||||
_createHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.JOIN_ROOM:
|
||||
{
|
||||
_joinHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.LEAVE_ROOM:
|
||||
{
|
||||
_leaveHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
case RagonOperation.AUTHORIZE:
|
||||
{
|
||||
_authorizationHandler.Handle(context, _reader, _writer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
using Ragon.Common;
|
||||
|
||||
namespace Ragon.Core;
|
||||
|
||||
public interface IHandler
|
||||
{
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer);
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Lobby;
|
||||
|
||||
namespace Ragon.Core.Handlers;
|
||||
|
||||
public sealed class AuthorizationHandler: IHandler
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer)
|
||||
{
|
||||
if (context.LobbyPlayer.Status == LobbyPlayerStatus.Authorized)
|
||||
{
|
||||
_logger.Warn("Player already authorized");
|
||||
return;
|
||||
}
|
||||
|
||||
var key = reader.ReadString();
|
||||
var playerName = reader.ReadString();
|
||||
var additionalData = reader.ReadData(reader.Size);
|
||||
|
||||
context.LobbyPlayer.Name = playerName;
|
||||
context.LobbyPlayer.AdditionalData = additionalData.ToArray();
|
||||
context.LobbyPlayer.Status = LobbyPlayerStatus.Authorized;
|
||||
|
||||
var playerId = context.LobbyPlayer.Id;
|
||||
|
||||
writer.Clear();
|
||||
writer.WriteOperation(RagonOperation.AUTHORIZED_SUCCESS);
|
||||
writer.WriteString(playerId);
|
||||
writer.WriteString(playerName);
|
||||
|
||||
var sendData = writer.ToArray();
|
||||
context.Connection.Reliable.Send(sendData);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} authorized");
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
using NLog;
|
||||
using Ragon.Common;
|
||||
using Ragon.Core.Game;
|
||||
|
||||
|
||||
namespace Ragon.Core.Handlers;
|
||||
|
||||
public sealed class EntityCreateHandler: IHandler
|
||||
{
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
public void Handle(PlayerContext context, RagonSerializer reader, RagonSerializer writer)
|
||||
{
|
||||
var entityType = reader.ReadUShort();
|
||||
var eventAuthority = (RagonAuthority) reader.ReadByte();
|
||||
var propertiesCount = reader.ReadUShort();
|
||||
|
||||
var entity = new Entity(context.RoomPlayer, entityType, 0, eventAuthority);
|
||||
for (var i = 0; i < propertiesCount; i++)
|
||||
{
|
||||
var propertyType = reader.ReadBool();
|
||||
var propertySize = reader.ReadUShort();
|
||||
entity.State.AddProperty(new EntityStateProperty(propertySize, propertyType));
|
||||
}
|
||||
|
||||
var entityPayload = reader.ReadData(reader.Size);
|
||||
entity.SetPayload(entityPayload.ToArray());
|
||||
|
||||
context.Room.AttachEntity(context.RoomPlayer, entity);
|
||||
|
||||
_logger.Trace($"Player {context.Connection.Id}|{context.LobbyPlayer.Name} created entity {entity.Id}:{entity.Type}");
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user