breakpoint

This commit is contained in:
CortexCore
2023-06-17 16:30:53 +08:00
parent cd02761be7
commit 877ba6e548
88 changed files with 8715 additions and 988 deletions

View File

@@ -0,0 +1,69 @@
namespace Net.Component
{
/// <summary>
/// 网络操作指令 (系统命令使用0-100, 基础网络组件使用100-150) 请从150开始自定义命令
/// </summary>
public class Command : Share.NetCmd
{
/// <summary>
/// 客户端输入操作指令
/// </summary>
public const byte Input = 100;
/// <summary>
/// 玩家运动命令
/// </summary>
public const byte Movement = 101;
/// <summary>
/// 创建玩家命令
/// </summary>
public const byte CreatePlayer = 102;
/// <summary>
/// 玩家攻击命令
/// </summary>
public const byte Attack = 103;
/// <summary>
/// 同步生命值
/// </summary>
public const byte SyncHealth = 104;
/// <summary>
/// 玩家攻击到敌人
/// </summary>
public const byte Damage = 105;
/// <summary>
/// 敌人怪物AI同步指令
/// </summary>
public const byte EnemySync = 106;
/// <summary>
/// 玩家切换状态
/// </summary>
public const byte SwitchState = 107;
/// <summary>
/// 怪物切换状态
/// </summary>
public const byte EnemySwitchState = 108;
/// <summary>
/// Transform同步指令
/// <code><see cref="Net.Share.Operation.identity"/> 作为网络物体标识</code>
/// <code><see cref="Net.Share.Operation.index"/> 作为要实例化registerObjects的物体索引</code>
/// <code><see cref="Net.Share.Operation.index1"/> 用作NetComponentID区分</code>
/// <code><see cref="Net.Share.Operation.index2"/> 作为父子转换组件索引</code>
/// <code><see cref="Net.Share.Operation.cmd1"/> 作为SyncMode(同步模式)</code>
/// <code><see cref="Net.Share.Operation.position"/> 作为位置同步</code>
/// <code><see cref="Net.Share.Operation.rotation"/> 作为旋转同步</code>
/// <code><see cref="Net.Share.Operation.direction"/> 作为缩放同步</code>
/// </summary>
public const byte Transform = 109;
/// <summary>
/// NetworkIdentity组件被销毁指令
/// </summary>
public const byte Destroy = 110;
/// <summary>
/// 当客户端退出游戏, 通知其他客户端删除此客户端所生成的NetworkIdentity物体
/// </summary>
public const byte OnPlayerExit = 114;
/// <summary>
/// 网络组件生成工具同步指令
/// </summary>
public const byte BuildComponent = 115;
}
}

View File

@@ -0,0 +1,163 @@
namespace Net.Component
{
using global::System;
using UnityEngine;
using Matrix4x4 = Matrix4x4;
using Quaternion = Quaternion;
using Vector3 = Vector3;
/// <summary>
/// 游戏物体转换实体组建
/// 作者:彼岸流年 QQ:317392507
/// 后期修改:龙兄 QQ:1752062104
/// </summary>
[Serializable]
public class EntityTransform
{
public Matrix4x4 matrix;
public Vector3 position
{
get => matrix.GetPosition();
set => Matrix4Utils.SetPosition(ref matrix, value);
}
public Quaternion rotation
{
get => matrix.GetRotation();
set => Matrix4Utils.Rotate(ref matrix, value);
}
public UnityEngine.Vector3 localScale
{
get => matrix.GetScale();
}
public UnityEngine.Quaternion localRotation
{
get { return rotation; }
set { rotation = value; }
}
public Vector3 eulerAngles
{
get => rotation.eulerAngles;
set => rotation = Quaternion.Euler(value);
}
public Vector3 left
{
get => matrix.left;
set => matrix.left = value;
}
public Vector3 right
{
get => matrix.right;
set => matrix.right = value;
}
public Vector3 up
{
get => matrix.up;
set => matrix.up = value;
}
public Vector3 down
{
get => matrix.down;
set => matrix.down = value;
}
public Vector3 forward
{
get => matrix.forward;
set => matrix.forward = value;
}
public Vector3 back
{
get => matrix.back;
set => matrix.back = value;
}
public EntityTransform()
{
matrix = Matrix4Utils.GetPosition(Vector3.zero);
}
public EntityTransform(Vector3 position, Quaternion rotation)
{
Matrix4Utils.SetPosition(ref matrix, position);
Matrix4Utils.Rotate(ref matrix, rotation);
}
public void Translate(float x, float y, float z)
{
Translate(new Vector3(x, y, z));
}
public void Translate(Vector3 direction)
{
Translate(direction, Space.Self);
}
public void Translate(Vector3 translation, Space relativeTo)
{
if (relativeTo == Space.World)
{
position += translation;
}
else
{
matrix *= Matrix4x4.Translate(translation);
}
}
public void Rotate(Vector3 eulers, Space relativeTo)
{
var rhs = Quaternion.Euler(eulers.x, eulers.y, eulers.z);
if (relativeTo == Space.Self)
{
matrix *= Matrix4x4.Rotate(rhs);
}
else
{
rotation *= Quaternion.Inverse(rotation) * rhs * rotation;
}
}
public void Rotate(Vector3 eulers)
{
Rotate(eulers, Space.Self);
}
public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo)
{
Rotate(new Vector3(xAngle, yAngle, zAngle), relativeTo);
}
public void Rotate(float xAngle, float yAngle, float zAngle)
{
Rotate(new Vector3(xAngle, yAngle, zAngle), Space.Self);
}
public void LookAt(Vector3 worldPosition)
{
LookAt(worldPosition, Vector3.up);
}
public void LookAt(Vector3 worldPosition, Vector3 worldUp)
{
rotation = Quaternion.LookRotation(position, worldPosition, worldUp);
}
}
[Serializable]
public class NTransform : EntityTransform
{
public NTransform() : base() { }
public NTransform(Vector3 position, Quaternion rotation) : base(position, rotation) { }
}
}

View File

@@ -0,0 +1,238 @@
using Net.Helper;
using Net.Share;
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace Net.Common
{
/// <summary>
/// 属性观察接口
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IPropertyObserver<T>
{
/// <summary>
/// 属性值
/// </summary>
T Value { get; set; }
/// <summary>
/// 当属性被修改事件
/// </summary>
Action<T> OnValueChanged { get; set; }
/// <summary>
/// 获取属性值
/// </summary>
/// <returns></returns>
T GetValue();
/// <summary>
/// 设置属性值
/// </summary>
/// <param name="value">新的属性值</param>
/// <param name="isNotify">是否通知事件</param>
void SetValue(T value, bool isNotify = true);
}
/// <summary>
/// 属性观察类
/// </summary>
/// <typeparam name="T"></typeparam>
public class PropertyObserver<T> : IPropertyObserver<T>
{
protected T value;
public T Value { get => GetValue(); set => SetValue(value); }
public Action<T> OnValueChanged { get; set; }
public PropertyObserver() { }
public PropertyObserver(T value) : this(value, null) { }
public PropertyObserver(T value, Action<T> onValueChanged)
{
this.value = value;
OnValueChanged = onValueChanged;
}
public virtual T GetValue()
{
return value;
}
public virtual void SetValue(T value, bool isNotify = true)
{
if (Equals(this.value, value))
return;
this.value = value;
if (isNotify) OnValueChanged?.Invoke(value);
}
public override string ToString()
{
return $"{Value}";
}
public static implicit operator PropertyObserver<T>(T value)
{
return new PropertyObserver<T>(value, null);
}
public static implicit operator T(PropertyObserver<T> value)
{
return value.Value;
}
public override bool Equals(object obj)
{
if (obj is PropertyObserver<T> value)
return Equals(value);
return false;
}
public bool Equals(PropertyObserver<T> obj)
{
return Value.Equals(obj.Value);
}
}
/// <summary>
/// 模糊属性观察类, 此类只支持byte, sbyte, short, ushort, char, int, uint, float, long, ulong, double
/// </summary>
/// <typeparam name="T"></typeparam>
public class ObscuredPropertyObserver<T> : IPropertyObserver<T>
{
private string name;
private long valueAtk;
private long valueAtkKey;
private byte crcValue;
public T Value { get => GetValue(); set => SetValue(value); }
public Action<T> OnValueChanged { get; set; }
public ObscuredPropertyObserver(T value) : this(null, value) { }
public ObscuredPropertyObserver(string name, T value) : this(name, value, null) { }
public ObscuredPropertyObserver(string name, T value, Action<T> onValueChanged)
{
this.name = name;
valueAtkKey = RandomHelper.Range(0, int.MaxValue);
SetValue(value);
OnValueChanged = onValueChanged;
}
public unsafe T GetValue()
{
var value = valueAtk ^ valueAtkKey;
var ptr = (byte*)&value;
var crcIndex = (byte)(valueAtk % 247);
crcValue = Net.Helper.CRCHelper.CRC8(ptr, 0, 8, crcIndex);
if (this.crcValue != crcValue)
{
AntiCheatHelper.OnDetected?.Invoke(name, value, value);
return default;
}
var value1 = Unsafe.As<long, T>(ref value);
return value1;
}
public unsafe void SetValue(T value, bool isNotify = true)
{
var value1 = Unsafe.As<T, long>(ref value);
valueAtk = value1 ^ valueAtkKey;
var ptr = (byte*)&value1;
var crcIndex = (byte)(valueAtk % 247);
crcValue = Net.Helper.CRCHelper.CRC8(ptr, 0, 8, crcIndex);
if (isNotify) OnValueChanged?.Invoke(value);
}
public override string ToString()
{
return $"{Value}";
}
public static implicit operator ObscuredPropertyObserver<T>(T value)
{
return new ObscuredPropertyObserver<T>(string.Empty, value, null);
}
public static implicit operator T(ObscuredPropertyObserver<T> value)
{
return value.Value;
}
public override bool Equals(object obj)
{
if (obj is ObscuredPropertyObserver<T> value)
return Equals(value);
return false;
}
public bool Equals(ObscuredPropertyObserver<T> obj)
{
return Value.Equals(obj.Value);
}
}
/// <summary>
/// 属性观察自动类, 可模糊,不模糊
/// </summary>
/// <typeparam name="T"></typeparam>
public class PropertyObserverAuto<T> : IPropertyObserver<T>
{
private readonly bool available;
private IPropertyObserver<T> binding;
public T Value { get => GetValue(); set => SetValue(value); }
public Action<T> OnValueChanged { get => binding.OnValueChanged; set => binding.OnValueChanged = value; }
public PropertyObserverAuto() { }
/// <summary>
/// 属性观察自动类构造
/// </summary>
/// <param name="name">当属性被发现修改时提示名称</param>
/// <param name="available">使用模糊属性?</param>
/// <param name="onValueChanged">当属性被修改事件</param>
public PropertyObserverAuto(string name, bool available, Action<T> onValueChanged) : this(name, available, default, onValueChanged)
{
}
public PropertyObserverAuto(string name, bool available, T value, Action<T> onValueChanged)
{
this.available = available;
if (!AntiCheatHelper.IsActive | !available)
binding = new PropertyObserver<T>(value, onValueChanged);
else
binding = new ObscuredPropertyObserver<T>(name, value, onValueChanged);
}
public T GetValue()
{
return binding.GetValue();
}
public void SetValue(T value, bool isNotify = true)
{
binding.SetValue(value, isNotify);
}
public override string ToString()
{
return $"{Value}";
}
public static implicit operator PropertyObserverAuto<T>(T value)
{
return new PropertyObserverAuto<T>(string.Empty, true, value, null);
}
public static implicit operator T(PropertyObserverAuto<T> value)
{
return value.Value;
}
public override bool Equals(object obj)
{
if (obj is PropertyObserverAuto<T> value)
return Equals(value);
return false;
}
public bool Equals(PropertyObserverAuto<T> obj)
{
return Value.Equals(obj.Value);
}
}
}

View File

@@ -0,0 +1,39 @@
using Net.Share;
namespace Net.Component
{
/// <summary>
/// 房间数据信息
/// </summary>
public class RoomData
{
/// <summary>
/// 房间名称
/// </summary>
public string name;
/// <summary>
/// 房间可以组队人数
/// </summary>
public int num;
/// <summary>
/// 当前加入房间人数
/// </summary>
public int currNum;
/// <summary>
/// 房间的状态
/// </summary>
public NetState state;
/// <summary>
/// 竞技模式 1:个人 2:团队
/// </summary>
public int mode;
}
public class JoinData
{
public string name;//玩家名称
public bool ready;//是否准备
public bool teamTag;//true:为红队 false:为绿队
public string iconName;
}
}