提交 5d56826f 编写于 作者: T tanghai

整理代码

上级 f7c3e927
......@@ -52,5 +52,7 @@
<s:String x:Key="/Default/Housekeeping/Layout/DialogWindows/RefactoringWizardWindow/Location/@EntryValue">122,-178</s:String>
<s:Int64 x:Key="/Default/Environment/SearchAndNavigation/DefaultOccurrencesGroupingIndex/@EntryValue">0</s:Int64>
<s:String x:Key="/Default/Profiling/Configurations/=1/@EntryIndexedValue">&lt;data&gt;&lt;HostParameters type="LocalHostParameters" /&gt;&lt;Argument type="StandaloneArgument"&gt;&lt;Arguments IsNull="False"&gt;&lt;/Arguments&gt;&lt;FileName IsNull="False"&gt;&lt;/FileName&gt;&lt;WorkingDirectory IsNull="False"&gt;&lt;/WorkingDirectory&gt;&lt;Scope&gt;&lt;ProcessFilters /&gt;&lt;/Scope&gt;&lt;/Argument&gt;&lt;Info type="TimelineInfo" /&gt;&lt;HostOptions type="HostOptions"&gt;&lt;HostTempPath IsNull="False"&gt;&lt;/HostTempPath&gt;&lt;/HostOptions&gt;&lt;/data&gt;</s:String>
<s:Boolean x:Key="/Default/UnloadedProject/UnloadedProjects/=1c2827bf_002Daa19_002D402a_002Dbfd1_002D1c92e6b8fd84_0023FileServer/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UnloadedProject/UnloadedProjects/=cc55624f_002Dd5cb_002D4705_002Da879_002D9fcaec1fed71_0023Proto2CS/@EntryIndexedValue">True</s:Boolean>
</wpf:ResourceDictionary>
\ No newline at end of file
using System;
using ETModel;
namespace ETHotfix
{
[MessageHandler(AppType.Gate)]
public class G2G_LockReleaseRequestHandler : AMRpcHandler<G2G_LockReleaseRequest, G2G_LockReleaseResponse>
{
protected override void Run(Session session, G2G_LockReleaseRequest message, Action<G2G_LockReleaseResponse> reply)
{
G2G_LockReleaseResponse g2GLockReleaseResponse = new G2G_LockReleaseResponse();
try
{
Unit unit = Game.Scene.GetComponent<UnitComponent>().Get(message.Id);
if (unit == null)
{
g2GLockReleaseResponse.Error = ErrorCode.ERR_NotFoundUnit;
reply(g2GLockReleaseResponse);
return;
}
unit.GetComponent<MasterComponent>().Release(NetworkHelper.ToIPEndPoint(message.Address));
reply(g2GLockReleaseResponse);
}
catch (Exception e)
{
ReplyError(g2GLockReleaseResponse, e, reply);
}
}
}
}
\ No newline at end of file
using System;
using ETModel;
namespace ETHotfix
{
[MessageHandler(AppType.Gate)]
public class G2G_LockRequestHandler : AMRpcHandler<G2G_LockRequest, G2G_LockResponse>
{
protected override void Run(Session session, G2G_LockRequest message, Action<G2G_LockResponse> reply)
{
RunAsync(session, message, reply).NoAwait();
}
protected async ETVoid RunAsync(Session session, G2G_LockRequest message, Action<G2G_LockResponse> reply)
{
G2G_LockResponse response = new G2G_LockResponse();
try
{
Unit unit = Game.Scene.GetComponent<UnitComponent>().Get(message.Id);
if (unit == null)
{
response.Error = ErrorCode.ERR_NotFoundUnit;
reply(response);
return;
}
await unit.GetComponent<MasterComponent>().Lock(NetworkHelper.ToIPEndPoint(message.Address));
reply(response);
}
catch (Exception e)
{
ReplyError(response, e, reply);
}
}
}
}
\ No newline at end of file
using System;
using System.Net;
using ETModel;
namespace ETHotfix
{
[ObjectSystem]
public class LockComponentAwakeSystem : AwakeSystem<LockComponent, IPEndPoint>
{
public override void Awake(LockComponent self, IPEndPoint a)
{
self.Awake(a);
}
}
/// <summary>
/// 分布式锁组件,Unit对象可能在不同进程上有镜像,访问该对象的时候需要对他加锁
/// </summary>
public static class LockComponentEx
{
public static void Awake(this LockComponent self, IPEndPoint addr)
{
self.address = addr;
}
public static async ETTask Lock(this LockComponent self)
{
++self.lockCount;
if (self.status == LockStatus.Locked)
{
return;
}
if (self.status == LockStatus.LockRequesting)
{
await self.WaitLock();
return;
}
self.status = LockStatus.LockRequesting;
// 真身直接本地请求锁,镜像需要调用Rpc获取锁
MasterComponent masterComponent = self.Entity.GetComponent<MasterComponent>();
if (masterComponent != null)
{
await masterComponent.Lock(self.address);
}
else
{
self.RequestLock().NoAwait();
await self.WaitLock();
}
}
private static ETTask WaitLock(this LockComponent self)
{
if (self.status == LockStatus.Locked)
{
return ETTask.FromResult(true);
}
ETTaskCompletionSource tcs = new ETTaskCompletionSource();
self.queue.Enqueue(tcs);
return tcs.Task;
}
private static async ETVoid RequestLock(this LockComponent self)
{
try
{
Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.address);
string serverAddress = StartConfigComponent.Instance.StartConfig.ServerIP;
G2G_LockRequest request = new G2G_LockRequest { Id = self.Entity.Id, Address = serverAddress };
await session.Call(request);
self.status = LockStatus.Locked;
foreach (ETTaskCompletionSource taskCompletionSource in self.queue)
{
taskCompletionSource.SetResult();
}
self.queue.Clear();
}
catch (Exception e)
{
Log.Error($"获取锁失败: {self.address} {self.Entity.Id} {e}");
}
}
public static async ETTask Release(this LockComponent self)
{
--self.lockCount;
if (self.lockCount != 0)
{
return;
}
self.status = LockStatus.LockedNot;
Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.address);
G2G_LockReleaseRequest request = new G2G_LockReleaseRequest();
await session.Call(request);
}
}
}
\ No newline at end of file
using System.Net;
using ETModel;
namespace ETHotfix
{
public static class MasterComponentEx
{
public static void AddGhost(this MasterComponent self, IPEndPoint address)
{
self.ghostsAddress.Add(address);
}
public static void RemoveGhost(this MasterComponent self, IPEndPoint address)
{
self.ghostsAddress.Remove(address);
}
public static ETTask Lock(this MasterComponent self, IPEndPoint address)
{
if (self.lockedAddress == null)
{
self.lockedAddress = address;
return ETTask.FromResult(true);
}
ETTaskCompletionSource tcs = new ETTaskCompletionSource();
LockInfo lockInfo = new LockInfo(address, tcs);
self.queue.Enqueue(lockInfo);
return tcs.Task;
}
public static void Release(this MasterComponent self, IPEndPoint address)
{
if (!self.lockedAddress.Equals(address))
{
Log.Error($"解锁地址与锁地址不匹配! {self.lockedAddress} {address}");
return;
}
if (self.queue.Count == 0)
{
self.lockedAddress = null;
return;
}
LockInfo lockInfo = self.queue.Dequeue();
self.lockedAddress = lockInfo.Address;
lockInfo.Tcs.SetResult();
}
}
}
\ No newline at end of file
......@@ -39,8 +39,7 @@ namespace ETHotfix
PathfindingComponent pathfindingComponent = Game.Scene.GetComponent<PathfindingComponent>();
self.ABPath = ComponentFactory.Create<ETModel.ABPath, Vector3, Vector3>(unit.Position,
new Vector3(target.x, target.y, target.z));
self.ABPath = ComponentFactory.Create<ABPathWrap, Vector3, Vector3>(unit.Position, new Vector3(target.x, target.y, target.z));
pathfindingComponent.Search(self.ABPath);
Log.Debug($"find result: {self.ABPath.Result.ListToString()}");
......
using System.Collections.Generic;
using System.Net;
namespace ETModel
{
public enum LockStatus
{
LockedNot,
LockRequesting,
Locked,
}
/// <summary>
/// 分布式锁组件,Unit对象可能在不同进程上有镜像,访问该对象的时候需要对他加锁
/// </summary>
public class LockComponent: Component
{
public LockStatus status = LockStatus.LockedNot;
public IPEndPoint address;
public int lockCount;
public readonly Queue<ETTaskCompletionSource> queue = new Queue<ETTaskCompletionSource>();
}
}
\ No newline at end of file
using System.Collections.Generic;
using System.Net;
namespace ETModel
{
public class LockInfo
{
public IPEndPoint Address;
public ETTaskCompletionSource Tcs;
public LockInfo(IPEndPoint address, ETTaskCompletionSource tcs)
{
this.Address = address;
this.Tcs = tcs;
}
}
public class MasterComponent : Component
{
/// 镜像的地址
public readonly List<IPEndPoint> ghostsAddress = new List<IPEndPoint>();
/// 当前获取锁的进程地址
public IPEndPoint lockedAddress;
/// 请求锁的队列
public readonly Queue<LockInfo> queue = new Queue<LockInfo>();
}
}
\ No newline at end of file
......@@ -8,13 +8,13 @@ namespace ETModel
{
public Vector3 Target;
private ABPath abPath;
private ABPathWrap abPath;
public List<Vector3> Path;
public CancellationTokenSource CancellationTokenSource;
public ABPath ABPath
public ABPathWrap ABPath
{
get
{
......
......@@ -4,21 +4,21 @@ using PF;
namespace ETModel
{
[ObjectSystem]
public class ABPathAwakeSystem : AwakeSystem<ABPath, Vector3, Vector3>
public class ABPathAwakeSystem : AwakeSystem<ABPathWrap, Vector3, Vector3>
{
public override void Awake(ABPath self, Vector3 start, Vector3 end)
public override void Awake(ABPathWrap self, Vector3 start, Vector3 end)
{
self.Awake(start, end);
}
}
public class ABPath: Component
public class ABPathWrap: Component
{
public Path Path { get; private set; }
public ABPath Path { get; private set; }
public void Awake(Vector3 start, Vector3 end)
{
this.Path = PF.ABPath.Construct(start, end);
this.Path = ABPath.Construct(start, end);
this.Path.Claim(this);
}
......
......@@ -27,7 +27,7 @@ namespace ETModel
public AStarConfig AStarConfig;
public bool Search(ABPath path)
public bool Search(ABPathWrap path)
{
this.PathProcessor.queue.Push(path.Path);
while (this.PathProcessor.CalculatePaths().MoveNext())
......@@ -43,7 +43,7 @@ namespace ETModel
return false;
}
PathModifyHelper.StartEndModify((PF.ABPath)path.Path);
PathModifyHelper.StartEndModify(path.Path);
PathModifyHelper.FunnelModify(path.Path);
return true;
......
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
namespace ETModel
{
/// <summary>
/// ET组件可视化
/// </summary>
public class ECSView : MonoBehaviour
{
#region Static Parts
/// <summary>
/// 组件与其对应可视化对象map
/// </summary>
private static DoubleMap<object, ECSView> _dic = new DoubleMap<object, ECSView>();
private static Transform root;
/// <summary>
/// 可视化对象根节点
/// </summary>
private static Transform Root
{
get
{
if (root == null)
{
root = new GameObject("ETViewRoot").transform;
DontDestroyOnLoad(root);
}
return root;
}
}
private static Transform pool;
/// <summary>
/// 组件放入Pool的可视化根节点
/// </summary>
private static Transform Pool
{
get
{
if (pool == null)
{
pool = new GameObject("Pool").transform;
pool.parent = Root;
}
return pool;
}
}
/// <summary>
/// 创建组件的可视化节点
/// </summary>
/// <param name="self"></param>
public static void CreateView(object self)
{
if (!Define.IsEditorMode)
return;
if (_dic.ContainsKey(self))
return;
ECSView view = new GameObject(self.GetType().ToString()).AddComponent<ECSView>();
view.Component = self;
_dic.Add(self, view);
SetParent(self);
}
/// <summary>
/// 销毁组件的可视化节点
/// </summary>
/// <param name="self"></param>
public static void DestroyView(object self)
{
if (!Define.IsEditorMode)
return;
if (_dic.ContainsKey(self))
{
ECSView view = _dic.GetValueByKey(self);
if (view != null)
DestroyImmediate(view.gameObject);
_dic.RemoveByKey(self);
}
}
/// <summary>
/// 根据组件获取可视化节点
/// </summary>
/// <param name="self"></param>
/// <returns></returns>
public static ECSView GetView(object self)
{
if (!Define.IsEditorMode)
return null;
if (_dic.ContainsKey(self))
return _dic.GetValueByKey(self);
return null;
}
/// <summary>
/// 根据可视化节点获取其组件
/// </summary>
/// <param name="self"></param>
/// <returns></returns>
public static object GetComponent(ECSView self)
{
if (!Define.IsEditorMode)
return null;
if (_dic.ContainsValue(self))
return _dic.GetKeyByValue(self);
return null;
}
/// <summary>
/// 放入Pool操作,修改可视化节点到Pool节点下
/// </summary>
/// <param name="self"></param>
public static void ReturnPool(object self)
{
if (!Define.IsEditorMode)
return;
if (self == null)
return;
ECSView selfView = GetView(self);
if (selfView == null)
{
_dic.RemoveByKey(self);
return;
}
selfView.transform.parent = Pool;
}
/// <summary>
/// 设置可视化父对象
/// </summary>
/// <param name="self"></param>
/// <param name="parent"></param>
public static void SetParent(object self, object parent = null)
{
if (!Define.IsEditorMode)
return;
if (self == null)
return;
ECSView selfView = GetView(self);
if (selfView == null)
{
_dic.RemoveByKey(self);
return;
}
ECSView parentView = GetView(parent);
if (parentView != null)
selfView.transform.parent = parentView.transform;
else
selfView.transform.parent = Root;
}
#endregion
/// <summary>
/// 该可视化节点对应的组件,对其属性显示到Inspector视图内
/// </summary>
public object Component;
}
#if UNITY_EDITOR
[InitializeOnLoad]
public class MyHierarchyEditor
{
static MyHierarchyEditor()
{
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyWindowItemOnGUI;
}
private static void OnHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
{
GameObject obj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (obj == null)
return;
if (obj.GetComponent<ECSView>() != null)
{
GUIStyle style = new GUIStyle(){
padding ={ left =EditorStyles.label.padding.left-1, top = EditorStyles.label.padding.top },
normal ={ textColor =Color.red }
};
GUI.Box(selectionRect, GUIContent.none);
GUI.Label(selectionRect, obj.name, style);
}
}
}
#endif
}
fileFormatVersion: 2
guid: f5a40ca22ac5ad54687060716411206f
timeCreated: 1536830842
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
namespace ETModel
{
public abstract class IInstanceMethod
{
public abstract void Run();
public abstract void Run(object a);
public abstract void Run(object a, object b);
public abstract void Run(object a, object b, object c);
}
public abstract class IStaticMethod
{
public abstract void Run();
......
using System;
using System.Reflection;
namespace ETModel
{
public class MonoInstanceMethod : IInstanceMethod
{
private readonly object obj;
private readonly MethodInfo methodInfo;
private readonly object[] param;
public MonoInstanceMethod(Type type, string methodName)
{
this.obj = Activator.CreateInstance(type);
this.methodInfo = type.GetMethod(methodName);
this.param = new object[this.methodInfo.GetParameters().Length];
}
public override void Run()
{
this.methodInfo.Invoke(this.obj, param);
}
public override void Run(object a)
{
this.param[0] = a;
this.methodInfo.Invoke(this.obj, param);
}
public override void Run(object a, object b)
{
this.param[0] = a;
this.param[1] = b;
this.methodInfo.Invoke(this.obj, param);
}
public override void Run(object a, object b, object c)
{
this.param[0] = a;
this.param[1] = b;
this.param[2] = c;
this.methodInfo.Invoke(this.obj, param);
}
}
public class MonoStaticMethod : IStaticMethod
{
private readonly MethodInfo methodInfo;
private readonly object[] param;
public MonoStaticMethod(Type type, string methodName)
{
this.methodInfo = type.GetMethod(methodName);
this.param = new object[this.methodInfo.GetParameters().Length];
}
public override void Run()
{
this.methodInfo.Invoke(null, param);
}
public override void Run(object a)
{
this.param[0] = a;
this.methodInfo.Invoke(null, param);
}
public override void Run(object a, object b)
{
this.param[0] = a;
this.param[1] = b;
this.methodInfo.Invoke(null, param);
}
public override void Run(object a, object b, object c)
{
this.param[0] = a;
this.param[1] = b;
this.param[2] = c;
this.methodInfo.Invoke(null, param);
}
}
}
fileFormatVersion: 2
guid: d2c01f9c277a82b4ca6ba13a14a8214c
timeCreated: 1495179020
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -468,11 +468,9 @@
<Compile Include="Assets\Model\Module\UI\UIType.cs" />
<Compile Include="Assets\Model\Other\AppType.cs" />
<Compile Include="Assets\Model\Other\Define.cs" />
<Compile Include="Assets\Model\Other\ECSView.cs" />
<Compile Include="Assets\Model\Other\GizmosDebug.cs" />
<Compile Include="Assets\Model\Other\IInstanceMethod.cs" />
<Compile Include="Assets\Model\Other\ILMethod.cs" />
<Compile Include="Assets\Model\Other\MonoMethod.cs" />
<Compile Include="Assets\Model\Other\MotionType.cs" />
<Compile Include="Assets\Model\Other\ReferenceCollector.cs" />
<Compile Include="Assets\Model\UI\UILoading\Component\UILoadingComponent.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册