fix(network): Adds UOG & Fixes ConnectUO packet (#552)
- [X] Adds UOG Extended and Compact (0xF1 0x51 packet) - [X] Fixes ConnectUO bad length
This commit is contained in:
parent
5e3b42bf9c
commit
c50322c0e3
25 changed files with 196 additions and 38 deletions
|
|
@ -193,7 +193,7 @@ namespace Server.Buffers
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe void Append(uint value)
|
||||
{
|
||||
int bufferLength = Utility.CountDigits(value);
|
||||
int bufferLength = value.CountDigits();
|
||||
|
||||
int pos = _length;
|
||||
if ((uint)pos + (uint)bufferLength >= _chars.Length)
|
||||
|
|
|
|||
|
|
@ -478,7 +478,7 @@ namespace Server
|
|||
top = zBottom;
|
||||
}
|
||||
|
||||
avg = Math.Abs(zTop - zBottom) > Math.Abs(zLeft - zRight)
|
||||
avg = (zTop - zBottom).Abs() > (zLeft - zRight).Abs()
|
||||
? FloorAverage(zLeft, zRight)
|
||||
: FloorAverage(zTop, zBottom);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9086,8 +9086,8 @@ namespace Server
|
|||
var rx = (dx - dy) * 44;
|
||||
var ry = (dx + dy) * 44;
|
||||
|
||||
var ax = Math.Abs(rx);
|
||||
var ay = Math.Abs(ry);
|
||||
var ax = rx.Abs();
|
||||
var ay = ry.Abs();
|
||||
|
||||
Direction ret;
|
||||
|
||||
|
|
|
|||
|
|
@ -577,8 +577,8 @@ namespace Server
|
|||
var dx = to.X - from.X;
|
||||
var dy = to.Y - from.Y;
|
||||
|
||||
var adx = Math.Abs(dx);
|
||||
var ady = Math.Abs(dy);
|
||||
var adx = Abs(dx);
|
||||
var ady = Abs(dy);
|
||||
|
||||
if (adx >= ady * 3)
|
||||
{
|
||||
|
|
@ -1395,7 +1395,21 @@ namespace Server
|
|||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CountDigits(uint value)
|
||||
public static int Abs(this int value)
|
||||
{
|
||||
int mask = value >> 31;
|
||||
return (value + mask) ^ mask;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static long Abs(this long value)
|
||||
{
|
||||
long mask = value >> 63;
|
||||
return (value + mask) ^ mask;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CountDigits(this uint value)
|
||||
{
|
||||
int digits = 1;
|
||||
if (value >= 100000)
|
||||
|
|
@ -1428,6 +1442,47 @@ namespace Server
|
|||
return digits;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CountDigits(this int value)
|
||||
{
|
||||
int absValue = Abs(value);
|
||||
|
||||
int digits = 1;
|
||||
if (absValue >= 100000)
|
||||
{
|
||||
absValue /= 100000;
|
||||
digits += 5;
|
||||
}
|
||||
|
||||
if (absValue < 10)
|
||||
{
|
||||
// no-op
|
||||
}
|
||||
else if (absValue < 100)
|
||||
{
|
||||
digits++;
|
||||
}
|
||||
else if (absValue < 1000)
|
||||
{
|
||||
digits += 2;
|
||||
}
|
||||
else if (absValue < 10000)
|
||||
{
|
||||
digits += 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
digits += 4;
|
||||
}
|
||||
|
||||
if (value < 0)
|
||||
{
|
||||
digits += 1; // negative
|
||||
}
|
||||
|
||||
return digits;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint DivRem(uint a, uint b, out uint result)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1072,7 +1072,7 @@ namespace Server.Commands
|
|||
{
|
||||
res = true;
|
||||
}
|
||||
else if (Math.Abs(item.Z - z) < 8)
|
||||
else if ((item.Z - z).Abs() < 8)
|
||||
{
|
||||
m_DeleteQueue.Enqueue(item);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1069,7 +1069,7 @@ namespace Server.Commands
|
|||
{
|
||||
res = true;
|
||||
}
|
||||
else if (Math.Abs(item.Z - z) < 8)
|
||||
else if ((item.Z - z).Abs() < 8)
|
||||
{
|
||||
m_DeleteQueue.Enqueue(item);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -414,7 +414,7 @@ namespace Server.Engines.Harvest
|
|||
|
||||
var z = map.GetAverageZ(x, y);
|
||||
|
||||
if (Math.Abs(z - from.Z) < 10 && map.CanSpawnMobile(x, y, z))
|
||||
if ((z - from.Z).Abs() < 10 && map.CanSpawnMobile(x, y, z))
|
||||
{
|
||||
spawned.OnBeforeSpawn(new Point3D(x, y, z), map);
|
||||
spawned.MoveToWorld(new Point3D(x, y, z), map);
|
||||
|
|
|
|||
|
|
@ -297,7 +297,7 @@ namespace Server.Movement
|
|||
|
||||
if (moveIsOk)
|
||||
{
|
||||
var cmp = Math.Abs(ourZ - m.Z) - Math.Abs(newZ - m.Z);
|
||||
var cmp = (ourZ - m.Z).Abs() - (newZ - m.Z).Abs();
|
||||
|
||||
if (cmp > 0 || cmp == 0 && ourZ > newZ)
|
||||
{
|
||||
|
|
@ -379,7 +379,7 @@ namespace Server.Movement
|
|||
|
||||
if (moveIsOk)
|
||||
{
|
||||
var cmp = Math.Abs(ourZ - m.Z) - Math.Abs(newZ - m.Z);
|
||||
var cmp = (ourZ - m.Z).Abs() - (newZ - m.Z).Abs();
|
||||
|
||||
if (cmp > 0 || cmp == 0 && ourZ > newZ)
|
||||
{
|
||||
|
|
@ -445,7 +445,7 @@ namespace Server.Movement
|
|||
|
||||
if (moveIsOk)
|
||||
{
|
||||
var cmp = Math.Abs(ourZ - m.Z) - Math.Abs(newZ - m.Z);
|
||||
var cmp = (ourZ - m.Z).Abs() - (newZ - m.Z).Abs();
|
||||
|
||||
if (cmp > 0 || cmp == 0 && ourZ > newZ)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -464,7 +464,7 @@ namespace Server.Movement
|
|||
|
||||
if (moveIsOk)
|
||||
{
|
||||
var cmp = Math.Abs(ourZ - m.Z) - Math.Abs(newZ - m.Z);
|
||||
var cmp = (ourZ - m.Z).Abs() - (newZ - m.Z).Abs();
|
||||
|
||||
if (cmp > 0 || cmp == 0 && ourZ > newZ)
|
||||
{
|
||||
|
|
@ -532,7 +532,7 @@ namespace Server.Movement
|
|||
|
||||
if (moveIsOk)
|
||||
{
|
||||
var cmp = Math.Abs(ourZ - m.Z) - Math.Abs(newZ - m.Z);
|
||||
var cmp = (ourZ - m.Z).Abs() - (newZ - m.Z).Abs();
|
||||
|
||||
if (cmp > 0 || cmp == 0 && ourZ > newZ)
|
||||
{
|
||||
|
|
@ -591,7 +591,7 @@ namespace Server.Movement
|
|||
|
||||
if (moveIsOk)
|
||||
{
|
||||
var cmp = Math.Abs(ourZ - m.Z) - Math.Abs(newZ - m.Z);
|
||||
var cmp = (ourZ - m.Z).Abs() - (newZ - m.Z).Abs();
|
||||
|
||||
if (cmp > 0 || cmp == 0 && ourZ > newZ)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ namespace Server
|
|||
}
|
||||
|
||||
public static bool Check(Point3D loc, Point3D goal, int range) =>
|
||||
Utility.InRange(loc, goal, range) && (range > 1 || Math.Abs(loc.Z - goal.Z) < 16);
|
||||
Utility.InRange(loc, goal, range) && (range > 1 || (loc.Z - goal.Z).Abs() < 16);
|
||||
|
||||
public bool Follow(bool run, int range)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ namespace Server.PathAlgorithms.SlowAStar
|
|||
}
|
||||
}
|
||||
|
||||
if (curNode.x == goalNode.x && curNode.y == goalNode.y && Math.Abs(curNode.z - goalNode.z) < 16)
|
||||
if (curNode.x == goalNode.x && curNode.y == goalNode.y && (curNode.z - goalNode.z).Abs() < 16)
|
||||
{
|
||||
if (closedCount == MaxNodes)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -265,7 +265,7 @@ namespace Server.Mobiles
|
|||
{
|
||||
var range = 3;
|
||||
|
||||
if (pm.Alive && Math.Abs(Z - pm.Z) < 16 && InRange(m, range) && !InRange(oldLocation, range))
|
||||
if (pm.Alive && (Z - pm.Z).Abs() < 16 && InRange(m, range) && !InRange(oldLocation, range))
|
||||
{
|
||||
if (pm.ToTItemsTurnedIn >= TreasuresOfTokuno.ItemsPerReward)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -284,7 +284,7 @@ namespace Server.Items
|
|||
|
||||
var rx = from.X - X;
|
||||
var ry = from.Y - Y;
|
||||
var az = Math.Abs(from.Z - Z);
|
||||
var az = (from.Z - Z).Abs();
|
||||
|
||||
return rx >= x && rx < x + w && ry >= y && ry < y + h && az <= 4;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ namespace Server.Items
|
|||
{
|
||||
var p = GetWorldLocation();
|
||||
|
||||
if (Utility.Random(Math.Max(Math.Abs(from.X - p.X), Math.Abs(from.Y - p.Y))) < 1)
|
||||
if (Utility.Random(Math.Max((from.X - p.X).Abs(), (from.Y - p.Y).Abs())) < 1)
|
||||
{
|
||||
Effects.PlaySound(from.Location, from.Map, from.GetHurtSound());
|
||||
from.PublicOverheadMessage(MessageType.Regular, from.SpeechHue, true, "Ouch!");
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ namespace Server.Items
|
|||
percent = v;
|
||||
}
|
||||
|
||||
percent *= 10000 + 10000 / (Math.Abs(high - low) + 1);
|
||||
percent *= 10000 + 10000 / ((high - low).Abs() + 1);
|
||||
|
||||
return low + (high - low) * percent / 1000001;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -291,28 +291,28 @@ namespace Server.Items
|
|||
switch (m_Cannon.CannonDirection)
|
||||
{
|
||||
case CannonDirection.North:
|
||||
if (y < 0 && Math.Abs(x) <= -y / 3)
|
||||
if (y < 0 && x.Abs() <= -y / 3)
|
||||
{
|
||||
allow = true;
|
||||
}
|
||||
|
||||
break;
|
||||
case CannonDirection.East:
|
||||
if (x > 0 && Math.Abs(y) <= x / 3)
|
||||
if (x > 0 && y.Abs() <= x / 3)
|
||||
{
|
||||
allow = true;
|
||||
}
|
||||
|
||||
break;
|
||||
case CannonDirection.South:
|
||||
if (y > 0 && Math.Abs(x) <= y / 3)
|
||||
if (y > 0 && x.Abs() <= y / 3)
|
||||
{
|
||||
allow = true;
|
||||
}
|
||||
|
||||
break;
|
||||
case CannonDirection.West:
|
||||
if (x < 0 && Math.Abs(y) <= -x / 3)
|
||||
if (x < 0 && y.Abs() <= -x / 3)
|
||||
{
|
||||
allow = true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -192,7 +192,7 @@ namespace Server.Misc
|
|||
|
||||
public static void Line2D(Point3D start, Point3D end, Map map, DoEffect_Callback effect)
|
||||
{
|
||||
var steep = Math.Abs(end.Y - start.Y) > Math.Abs(end.X - start.X);
|
||||
var steep = (end.Y - start.Y).Abs() > (end.X - start.X).Abs();
|
||||
|
||||
var x0 = start.X;
|
||||
var x1 = end.X;
|
||||
|
|
@ -212,7 +212,7 @@ namespace Server.Misc
|
|||
}
|
||||
|
||||
var deltax = x1 - x0;
|
||||
var deltay = Math.Abs(y1 - y0);
|
||||
var deltay = (y1 - y0).Abs();
|
||||
var error = deltax / 2;
|
||||
var ystep = y0 < y1 ? 1 : -1;
|
||||
var y = y0;
|
||||
|
|
|
|||
|
|
@ -101,7 +101,7 @@ namespace Server.Mobiles
|
|||
{
|
||||
bc.Karma = (int)(bc.Karma * KarmaBuff);
|
||||
|
||||
if (Math.Abs(bc.Karma) > 32000)
|
||||
if (bc.Karma.Abs() > 32000)
|
||||
{
|
||||
bc.Karma = 32000 * Math.Sign(bc.Karma);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1518,8 +1518,8 @@ namespace Server.Multis
|
|||
var dx = x - X;
|
||||
var dy = y - Y;
|
||||
|
||||
var adx = Math.Abs(dx);
|
||||
var ady = Math.Abs(dy);
|
||||
var adx = dx.Abs();
|
||||
var ady = dy.Abs();
|
||||
|
||||
var dir = Utility.GetDirection(this, new Point2D(x, y));
|
||||
var iDir = (int)dir;
|
||||
|
|
@ -1527,7 +1527,7 @@ namespace Server.Multis
|
|||
// Compute the maximum distance we can travel without going too far away
|
||||
if (iDir % 2 == 0) // North, East, South and West
|
||||
{
|
||||
maxSpeed = Math.Abs(adx - ady);
|
||||
maxSpeed = (adx - ady).Abs();
|
||||
}
|
||||
else // Right, Down, Left and Up
|
||||
{
|
||||
|
|
@ -1732,7 +1732,7 @@ namespace Server.Multis
|
|||
}
|
||||
}
|
||||
|
||||
if (!NewBoatMovement || Math.Abs(xOffset) > 1 || Math.Abs(yOffset) > 1)
|
||||
if (!NewBoatMovement || xOffset.Abs() > 1 || yOffset.Abs() > 1)
|
||||
{
|
||||
Teleport(xOffset, yOffset, 0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -744,7 +744,7 @@ namespace Server.Multis
|
|||
|
||||
foreach (var entity in eable)
|
||||
{
|
||||
if (Math.Abs(location.Z - entity.Z) <= 16)
|
||||
if ((location.Z - entity.Z).Abs() <= 16)
|
||||
{
|
||||
if (entity is PlayerVendor || entity is PlayerBarkeeper || entity is PlayerVendorPlaceholder)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ namespace Server.Network
|
|||
|
||||
var writer = new SpanWriter(stackalloc byte[15]);
|
||||
writer.Write((byte)0xC0); // Packet ID
|
||||
writer.Write(17); // Length
|
||||
writer.Write((ushort)17); // Length
|
||||
writer.Write(ConnectUOProtocolVersion); // Version
|
||||
writer.Write((byte)_serverType);
|
||||
writer.Write((int)(Core.TickCount / 1000));
|
||||
|
|
|
|||
103
Projects/UOContent/Network/UOGateway.cs
Normal file
103
Projects/UOContent/Network/UOGateway.cs
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: UOGateway.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Text;
|
||||
using Server.Misc;
|
||||
using Server.Text;
|
||||
|
||||
namespace Server.Network
|
||||
{
|
||||
public static class UOGateway
|
||||
{
|
||||
public static void Configure()
|
||||
{
|
||||
var enabled = ServerConfiguration.GetOrUpdateSetting("uogateway.enabled", true);
|
||||
|
||||
if (enabled)
|
||||
{
|
||||
FreeshardProtocol.Register(0xFE, false, QueryCompactShardStats);
|
||||
FreeshardProtocol.Register(0xFF, false, QueryExtendedShardStats);
|
||||
}
|
||||
}
|
||||
|
||||
public static void QueryCompactShardStats(NetState ns, CircularBufferReader reader, ref int packetLength)
|
||||
{
|
||||
ns.SendCompactShardStats(
|
||||
(uint)(Core.TickCount / 1000),
|
||||
TcpServer.Instances.Count,
|
||||
World.Items.Count,
|
||||
World.Mobiles.Count,
|
||||
GC.GetTotalMemory(false)
|
||||
);
|
||||
}
|
||||
|
||||
public static void QueryExtendedShardStats(NetState ns, CircularBufferReader reader, ref int packetLength)
|
||||
{
|
||||
const long ticksInHour = 1000 * 60 * 60;
|
||||
ns.SendExtendedShardStats(
|
||||
ServerList.ServerName,
|
||||
(int)(Core.TickCount / ticksInHour),
|
||||
TcpServer.Instances.Count,
|
||||
World.Items.Count,
|
||||
World.Mobiles.Count,
|
||||
(int)(GC.GetTotalMemory(false) / 1024)
|
||||
);
|
||||
}
|
||||
|
||||
public static void SendCompactShardStats(
|
||||
this NetState ns, uint age, int clients, int items, int mobiles, long mem
|
||||
)
|
||||
{
|
||||
if (ns == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var writer = new SpanWriter(stackalloc byte[27]);
|
||||
writer.Write((byte)0x51); // Packet ID
|
||||
writer.Write((ushort)27); // Length
|
||||
writer.Write(clients);
|
||||
writer.Write(items);
|
||||
writer.Write(mobiles);
|
||||
writer.Write(age);
|
||||
writer.Write(mem);
|
||||
|
||||
ns.Send(writer.Span);
|
||||
}
|
||||
|
||||
public static void SendExtendedShardStats(
|
||||
this NetState ns, string name, int age, int clients, int items, int mobiles, int mem
|
||||
)
|
||||
{
|
||||
if (ns == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var str =
|
||||
$"ModernUO, Name={name}, Age={age}, Clients={clients}, Items={items}, Chars={mobiles}, Mem={mem}, Ver=2";
|
||||
|
||||
var length = Encoding.UTF8.GetMaxByteCount(str.Length);
|
||||
|
||||
Span<byte> span = stackalloc byte[length + 1];
|
||||
Encoding.UTF8.GetBytes(str, span);
|
||||
span[^1] = 0; // Terminator
|
||||
|
||||
ns.Send(span);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -288,7 +288,7 @@ namespace Server.SkillHandlers
|
|||
}
|
||||
}
|
||||
|
||||
var info = new DiscordanceInfo(from, targ, Math.Abs(effect), mods);
|
||||
var info = new DiscordanceInfo(from, targ, effect.Abs(), mods);
|
||||
info.m_Timer = Timer.DelayCall(
|
||||
TimeSpan.Zero,
|
||||
TimeSpan.FromSeconds(1.25),
|
||||
|
|
|
|||
|
|
@ -105,7 +105,7 @@ namespace Server.Spells.Fifth
|
|||
|
||||
m_End = Core.Now + duration;
|
||||
|
||||
m_Timer = new InternalTimer(this, TimeSpan.FromSeconds(Math.Abs(val) * 0.2), caster.InLOS(this), canFit);
|
||||
m_Timer = new InternalTimer(this, TimeSpan.FromSeconds(val.Abs() * 0.2), caster.InLOS(this), canFit);
|
||||
m_Timer.Start();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -120,7 +120,7 @@ namespace Server.Spells.Fourth
|
|||
|
||||
m_End = Core.Now + duration;
|
||||
|
||||
m_Timer = new InternalTimer(this, TimeSpan.FromSeconds(Math.Abs(val) * 0.2), caster.InLOS(this), canFit);
|
||||
m_Timer = new InternalTimer(this, TimeSpan.FromSeconds(val.Abs() * 0.2), caster.InLOS(this), canFit);
|
||||
m_Timer.Start();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue