Files
Ragon/Ragon.Server/Sources/Entity/RagonProperty.cs
T
2023-03-06 10:06:43 +04:00

71 lines
1.7 KiB
C#

/*
* 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.ComponentModel;
using Ragon.Protocol;
namespace Ragon.Server;
public class RagonProperty : RagonPayload
{
public int Size { get; set; }
public bool IsDirty { get; private set; }
public bool IsFixed { get; private set; }
private uint[] _data;
public RagonProperty(int size, bool isFixed)
{
Size = size;
IsFixed = isFixed;
IsDirty = false;
_data = new uint[128];
}
public void Read(RagonBuffer buffer)
{
var readOnlySpan = _data.AsSpan();
if (IsFixed)
{
buffer.ReadSpan(ref readOnlySpan, Size);
}
else
{
Size = (int) buffer.Read();
buffer.ReadSpan(ref readOnlySpan, Size);
}
IsDirty = true;
}
public void Write(RagonBuffer buffer)
{
ReadOnlySpan<uint> readOnlySpan = _data.AsSpan();
buffer.WriteSpan(ref readOnlySpan, Size);
}
public void Clear()
{
IsDirty = false;
}
public void Dump()
{
Console.WriteLine( $"[{Size.ToString("00")}] {string.Join("", _data.Take(8).Reverse().Select(b => Convert.ToString(b, 2).PadLeft(32, '0')))}");
}
}