2023-03-06 10:06:43 +04:00
|
|
|
/*
|
2024-05-19 12:26:42 +03:00
|
|
|
* Copyright 2023-2024 Eduard Kargin <kargin.eduard@gmail.com>
|
2023-03-06 10:06:43 +04:00
|
|
|
*
|
|
|
|
|
* 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;
|
|
|
|
|
|
2023-07-01 08:12:40 +03:00
|
|
|
private RagonBuffer _propertyBuffer;
|
2024-01-21 12:20:47 +03:00
|
|
|
private RagonEntity _entity;
|
2023-03-06 10:06:43 +04:00
|
|
|
private bool _dirty;
|
|
|
|
|
private int _size;
|
|
|
|
|
private int _ticks;
|
|
|
|
|
private int _priority;
|
2023-05-07 23:54:07 +03:00
|
|
|
private bool _fixed;
|
|
|
|
|
private string _name;
|
2023-06-27 23:41:30 +03:00
|
|
|
|
2023-05-07 23:54:07 +03:00
|
|
|
protected bool InvokeLocal;
|
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;
|
2023-07-01 08:12:40 +03:00
|
|
|
_propertyBuffer = new RagonBuffer();
|
2024-01-21 12:20:47 +03:00
|
|
|
|
2023-05-07 23:54:07 +03:00
|
|
|
InvokeLocal = invokeLocal;
|
2023-03-06 10:06:43 +04:00
|
|
|
}
|
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()
|
|
|
|
|
{
|
2024-01-21 12:20:47 +03:00
|
|
|
if (_entity.HasAuthority)
|
2023-03-06 10:06:43 +04:00
|
|
|
return;
|
2024-01-21 12:20:47 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
Changed?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void MarkAsChanged()
|
|
|
|
|
{
|
2024-01-21 12:20:47 +03:00
|
|
|
if (InvokeLocal)
|
|
|
|
|
Changed?.Invoke();
|
|
|
|
|
|
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-06-27 23:41:30 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
Changed?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void Write(RagonBuffer buffer)
|
|
|
|
|
{
|
2023-07-01 08:12:40 +03:00
|
|
|
_propertyBuffer.Clear();
|
2024-01-21 12:20:47 +03:00
|
|
|
|
2023-03-06 10:06:43 +04:00
|
|
|
if (_fixed)
|
|
|
|
|
{
|
2023-07-01 08:12:40 +03:00
|
|
|
Serialize(_propertyBuffer);
|
2024-01-21 12:20:47 +03:00
|
|
|
|
2023-11-05 21:53:57 +03:00
|
|
|
buffer.CopyFrom(_propertyBuffer, _size);
|
2023-06-27 23:41:30 +03:00
|
|
|
return;
|
2023-03-06 10:06:43 +04:00
|
|
|
}
|
|
|
|
|
|
2023-07-01 08:12:40 +03:00
|
|
|
Serialize(_propertyBuffer);
|
2023-06-27 23:41:30 +03:00
|
|
|
|
2024-01-21 12:20:47 +03:00
|
|
|
var propertySize = (ushort)_propertyBuffer.WriteOffset;
|
|
|
|
|
buffer.WriteUShort(propertySize);
|
2023-11-05 21:53:57 +03:00
|
|
|
buffer.CopyFrom(_propertyBuffer, propertySize);
|
2023-05-07 23:54:07 +03:00
|
|
|
}
|
2023-03-06 10:06:43 +04:00
|
|
|
|
2023-05-07 23:54:07 +03:00
|
|
|
internal void Read(RagonBuffer buffer)
|
|
|
|
|
{
|
2023-07-01 08:12:40 +03:00
|
|
|
_propertyBuffer.Clear();
|
2024-01-21 12:20:47 +03:00
|
|
|
|
2023-07-01 08:12:40 +03:00
|
|
|
if (_fixed)
|
|
|
|
|
{
|
|
|
|
|
buffer.ToBuffer(_propertyBuffer, _size);
|
2024-01-21 12:20:47 +03:00
|
|
|
|
2023-07-01 08:12:40 +03:00
|
|
|
Deserialize(_propertyBuffer);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-01-21 12:20:47 +03:00
|
|
|
|
2023-07-01 08:12:40 +03:00
|
|
|
var propSize = buffer.ReadUShort();
|
|
|
|
|
buffer.ToBuffer(_propertyBuffer, propSize);
|
|
|
|
|
Deserialize(_propertyBuffer);
|
2023-03-06 10:06:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Serialize(RagonBuffer buffer)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Deserialize(RagonBuffer buffer)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|