Files
Ragon/Ragon.Client/Sources/Entity/RagonProperty.cs
T

124 lines
2.5 KiB
C#
Raw Normal View History

2023-03-06 10:06:43 +04:00
/*
* 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;
2023-03-23 14:05:03 +04:00
2023-03-06 10:06:43 +04:00
public event Action Changed;
public bool IsDirty => _dirty && _ticks >= _priority;
public bool IsFixed => _fixed;
public int Size => _size;
private bool _fixed;
private string _name;
protected bool _invokeLocal;
2023-03-23 14:05:03 +04:00
2023-03-06 10:06:43 +04:00
private RagonEntity _entity;
private bool _dirty;
private int _size;
private int _ticks;
private int _priority;
2023-03-23 14:05:03 +04:00
2023-03-06 10:06:43 +04:00
protected RagonProperty(int priority, bool invokeLocal)
{
_size = 0;
_priority = priority;
_fixed = false;
_invokeLocal = invokeLocal;
}
2023-03-23 14:05:03 +04:00
2023-03-06 10:06:43 +04:00
public void SetName(string name)
{
_name = name;
}
2023-03-23 14:05:03 +04:00
2023-03-06 10:06:43 +04:00
protected void SetFixedSize(int size)
{
_size = size;
_fixed = true;
}
protected void InvokeChanged()
{
if (!_invokeLocal)
return;
2023-03-23 14:05:03 +04:00
2023-03-06 10:06:43 +04:00
Changed?.Invoke();
}
protected void MarkAsChanged()
{
InvokeChanged();
2023-03-23 14:05:03 +04:00
2023-03-23 19:17:54 +04:00
if (_dirty || _entity == null)
2023-03-23 05:28:47 +04:00
return;
2023-03-06 10:06:43 +04:00
2023-03-23 14:05:03 +04:00
_dirty = true;
2023-03-23 19:17:54 +04:00
_entity.TrackChangedProperty(this);
2023-03-06 10:06:43 +04:00
}
internal void Flush()
{
_dirty = false;
_ticks = 0;
}
internal void AddTick()
{
_ticks++;
}
2023-03-23 14:05:03 +04:00
internal void AssignEntity(RagonEntity ent)
2023-03-06 10:06:43 +04:00
{
2023-03-23 14:05:03 +04:00
_entity = ent;
2023-03-06 10:06:43 +04:00
Changed?.Invoke();
}
internal void Write(RagonBuffer buffer)
{
if (_fixed)
{
Serialize(buffer);
return;
}
var sizeOffset = buffer.WriteOffset;
buffer.Write(0, 16);
var propOffset = buffer.WriteOffset;
Serialize(buffer);
2023-03-23 14:05:03 +04:00
var propSize = (uint)(buffer.WriteOffset - propOffset);
2023-03-06 10:06:43 +04:00
buffer.Write(propSize, 16, sizeOffset);
}
public virtual void Serialize(RagonBuffer buffer)
{
}
public virtual void Deserialize(RagonBuffer buffer)
{
}
}
}