Răsfoiți Sursa

demo已经跑通,有个小问题,recast寻路在线程池模式下存在问题,暂未找到

tanghai 2 ani în urmă
părinte
comite
26a5a1f9c3

+ 2 - 25
Share/Libs/RecastDll/Include/InvokeHelper.h

@@ -16,9 +16,8 @@ extern "C" {
 #endif
 	
 class NavMeshContex;
-RECAST_DLL NavMeshContex* RecastGet(int32_t id);
-RECAST_DLL NavMeshContex* RecastLoad(int32_t id, const char* buffer, int32_t n);
-RECAST_DLL void RecastClear();
+RECAST_DLL NavMeshContex* RecastLoad(const char* buffer, int32_t n);
+RECAST_DLL void RecastClear(NavMeshContex* navMeshContex);
 RECAST_DLL int32_t RecastFind(NavMeshContex* navMeshContex, float* extents, float* startPos, float* endPos, float* straightPath);
 RECAST_DLL int32_t RecastFindNearestPoint(NavMeshContex* navMeshContex, float* extents, float* pos, float* nearestPos);
 RECAST_DLL int32_t RecastFindRandomPoint(NavMeshContex* navMeshContex, float* pos);
@@ -32,26 +31,4 @@ RECAST_DLL int32_t RecastFindRandomPointAroundCircle(NavMeshContex* navMeshConte
 
 int32_t InitNav(const char* buffer, int32_t n, dtNavMesh*& navMesh);
 
-
-class NavMesh
-{
-public:
-	static NavMesh* instance;
-
-	static NavMesh* GetInstace();
-
-	std::map<int32_t, NavMeshContex*> navMeshContexs;
-
-	NavMeshContex* New(int32_t id, const char* buffer, int32_t n);
-
-	NavMeshContex* Get(int32_t id);
-
-	void Clear();
-
-private:
-	NavMesh();
-};
-
-
-
 #endif

+ 5 - 45
Share/Libs/RecastDll/Source/InvokeHelper.cpp

@@ -131,22 +131,7 @@ public:
 	}
 };
 
-NavMesh* NavMesh::instance = nullptr;
-
-NavMesh::NavMesh()
-{
-}
-
-NavMesh* NavMesh::GetInstace()
-{
-	if (NavMesh::instance == nullptr)
-	{
-		NavMesh::instance = new NavMesh();
-	}
-	return NavMesh::instance;
-}
-
-NavMeshContex* NavMesh::New(int32_t id, const char* buffer, int32_t n)
+NavMeshContex* New(const char* buffer, int32_t n)
 {
 	NavMeshContex* navMeshContex = new NavMeshContex();
 	int32_t ret = navMeshContex->Init(buffer, n);
@@ -157,42 +142,17 @@ NavMeshContex* NavMesh::New(int32_t id, const char* buffer, int32_t n)
 		return nullptr;
 	}
 
-	navMeshContexs[id] = navMeshContex;
 	return navMeshContex;
 }
 
-NavMeshContex* NavMesh::Get(int32_t id)
-{
-	const auto it = navMeshContexs.find(id);
-	if (it != navMeshContexs.end())
-	{
-		return it->second;
-	}
-	return nullptr;
-}
-
-void NavMesh::Clear()
-{
-	for (auto kv : navMeshContexs)
-	{
-		delete kv.second;
-	}
-	navMeshContexs.clear();
-}
-
-NavMeshContex* RecastLoad(int32_t id, const char* buffer, int32_t n)
-{
-	return NavMesh::GetInstace()->New(id, buffer, n);
-}
-
-NavMeshContex* RecastGet(int32_t id)
+NavMeshContex* RecastLoad(const char* buffer, int32_t n)
 {
-	return NavMesh::GetInstace()->Get(id);
+	return New(buffer, n);
 }
 
-void RecastClear()
+void RecastClear(NavMeshContex* navMeshContex)
 {
-	NavMesh::GetInstace()->Clear();
+	delete navMeshContex;
 }
 
 int32_t RecastFind(NavMeshContex* navMeshContex, float* extents, float* startPos, float* endPos, float* straightPath)

BIN
Unity/Assets/Plugins/x86_64/RecastDll.dll


+ 8 - 0
Unity/Assets/Scripts/Core/Fiber/Module/Navmesh.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: ebde9d58b893f434eaf40d782d2ef41a
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 9 - 10
Unity/Assets/Scripts/Core/World/Module/Navmesh/NavmeshComponent.cs → Unity/Assets/Scripts/Core/Fiber/Module/Navmesh/NavmeshComponent.cs

@@ -10,32 +10,31 @@ namespace ET
             public string Name { get; set; }
         }
 
-        private readonly Dictionary<string, long> navmeshs = new();
+        private readonly Dictionary<string, byte[]> navmeshs = new();
         
         public void Awake()
         {
         }
         
-        public long Get(string name)
+        public byte[] Get(string name)
         {
             lock (this)
             {
-                long ptr;
-                if (this.navmeshs.TryGetValue(name, out ptr))
+                if (this.navmeshs.TryGetValue(name, out byte[] bytes))
                 {
-                    return ptr;
+                    return bytes;
                 }
 
-                byte[] buffer = EventSystem.Instance.Invoke<RecastFileLoader, byte[]>(new RecastFileLoader() {Name = name});
+                byte[] buffer =
+                        EventSystem.Instance.Invoke<NavmeshComponent.RecastFileLoader, byte[]>(
+                            new NavmeshComponent.RecastFileLoader() { Name = name });
                 if (buffer.Length == 0)
                 {
                     throw new Exception($"no nav data: {name}");
                 }
 
-                ptr = Recast.RecastLoadLong(name.GetHashCode(), buffer, buffer.Length);
-                this.navmeshs[name] = ptr;
-
-                return ptr;
+                this.navmeshs[name] = buffer;
+                return buffer;
             }
         }
     }

+ 1 - 1
Unity/Assets/Scripts/Core/World/Module/Navmesh/NavmeshComponent.cs.meta → Unity/Assets/Scripts/Core/Fiber/Module/Navmesh/NavmeshComponent.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: f5bfbd4a24f65d34c9fa77337601c17d
+guid: bae9eeb5e7ebfeb4fa7173090cdafaa2
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 1 - 1
Unity/Assets/Scripts/Core/World/Module/Scheduler/ThreadScheduler.cs

@@ -6,7 +6,7 @@ using System.Threading;
 namespace ET
 {
     // 一个Process一个固定的线程
-    public class ThreadScheduler: Singleton<ThreadScheduler>, IScheduler
+    public class ThreadScheduler: Singleton<ThreadScheduler>, ISingletonAwake, IScheduler
     {
         private readonly ConcurrentDictionary<int, Thread> dictionary = new();
 

+ 0 - 2
Unity/Assets/Scripts/Hotfix/Client/Module/Message/NetClientComponentSystem.cs

@@ -37,8 +37,6 @@ namespace ET.Client
 
             session.LastRecvTime = self.Fiber().TimeInfo.ClientNow();
             
-            Log.Debug(message.ToJson());
-            
             EventSystem.Instance.Publish(self.Scene(), new NetClientComponentOnRead() {Session = session, Message = message});
         }
 

+ 6 - 16
Unity/Assets/Scripts/Hotfix/Server/Demo/EntryEvent2_InitServer.cs

@@ -10,45 +10,35 @@ namespace ET.Server
         {
             await ETTask.CompletedTask;
             
-            // 发送普通actor消息
-            //fiber.AddComponent<ActorSenderComponent>();
-            //// 发送location actor消息
-            //fiber.AddComponent<ActorLocationSenderComponent>();
-            //// 访问location server的组件
-            //fiber.AddComponent<LocationProxyComponent>();
-            //fiber.AddComponent<RobotCaseComponent>();
-            //fiber.AddComponent<NavmeshComponent>();
-
             switch (Options.Instance.AppType)
             {
                 case AppType.Server:
                 {
-                    //ThreadPoolScheduler threadPoolScheduler = World.Instance.AddSingleton<ThreadPoolScheduler, int>(10);
+                    World.Instance.AddSingleton<ThreadPoolScheduler, int>(Environment.ProcessorCount);
+                    World.Instance.AddSingleton<ThreadScheduler>();
                     
                     // 创建进程通信纤程
                     int fiberId = FiberManager.Instance.Create(ConstFiberId.NetInner, 0, SceneType.NetInner, SceneType.NetInner.ToString());
-                    MainThreadScheduler.Instance.Add(fiberId);
+                    ThreadScheduler.Instance.Add(fiberId);
                     
                     // 根据配置创建纤程
                     var processScenes = StartSceneConfigCategory.Instance.GetByProcess(root.Fiber().Process);
                     foreach (StartSceneConfig startConfig in processScenes)
                     {
                         fiberId = FiberManager.Instance.Create(startConfig.Id, startConfig.Zone, startConfig.Type, startConfig.Name);
-                        MainThreadScheduler.Instance.Add(fiberId);
+                        ThreadScheduler.Instance.Add(fiberId);
                     }
 
                     break;
                 }
                 case AppType.Watcher:
                 {
-                    //StartMachineConfig startMachineConfig = WatcherHelper.GetThisMachineConfig();
-                    //WatcherComponent watcherComponent = fiber.AddComponent<WatcherComponent>();
-                    //watcherComponent.Start(Options.Instance.CreateScenes);
-                    //fiber.AddComponent<NetInnerComponent, IPEndPoint>(NetworkHelper.ToIPEndPoint($"{startMachineConfig.InnerIP}:{startMachineConfig.WatcherPort}"));
                     break;
                 }
                 case AppType.GameTool:
+                {
                     break;
+                }
             }
 
             if (Options.Instance.Console == 1)

+ 2 - 0
Unity/Assets/Scripts/Hotfix/Server/Demo/Map/Move/C2M_PathfindingResultHandler.cs

@@ -6,6 +6,8 @@ namespace ET.Server
 	{
 		protected override async ETTask Run(Unit unit, C2M_PathfindingResult message)
 		{
+			Log.Debug($"11111111111111111111111111: {message}");
+			
 			unit.FindPathMoveToAsync(message.Position).Coroutine();
 			await ETTask.CompletedTask;
 		}

+ 14 - 11
Unity/Assets/Scripts/Hotfix/Share/Module/Recast/PathfindingComponentSystem.cs

@@ -11,9 +11,9 @@ namespace ET
         private static void Awake(this PathfindingComponent self, string name)
         {
             self.Name = name;
-            self.NavMesh = NavmeshComponent.Instance.Get(name);
-
-            if (self.NavMesh == 0)
+            byte[] buffer = NavmeshComponent.Instance.Get(name);
+            self.navMesh = Recast.RecastLoad(buffer, buffer.Length);
+            if (self.navMesh == IntPtr.Zero)
             {
                 throw new Exception($"nav load fail: {name}");
             }
@@ -23,12 +23,15 @@ namespace ET
         private static void Destroy(this PathfindingComponent self)
         {
             self.Name = string.Empty;
-            self.NavMesh = 0;
+            
+            Recast.RecastClear(self.navMesh);
+            
+            self.navMesh = IntPtr.Zero;
         }
         
         public static void Find(this PathfindingComponent self, float3 start, float3 target, List<float3> result)
         {
-            if (self.NavMesh == 0)
+            if (self.navMesh == IntPtr.Zero)
             {
                 Log.Debug("寻路| Find 失败 pathfinding ptr is zero");
                 throw new Exception($"pathfinding ptr is zero: {self.Scene().Name}");
@@ -42,7 +45,7 @@ namespace ET
             self.EndPos[1] = target.y;
             self.EndPos[2] = target.z;
             //Log.Debug($"start find path: {self.GetParent<Unit>().Id}");
-            int n = Recast.RecastFind(self.NavMesh, PathfindingComponent.extents, self.StartPos, self.EndPos, self.Result);
+            int n = Recast.RecastFind(self.navMesh, PathfindingComponent.extents, self.StartPos, self.EndPos, self.Result);
             for (int i = 0; i < n; ++i)
             {
                 int index = i * 3;
@@ -63,7 +66,7 @@ namespace ET
         
         public static float3 FindRandomPointWithRaduis(this PathfindingComponent self, float3 pos, float raduis)
         {
-            if (self.NavMesh == 0)
+            if (self.navMesh == IntPtr.Zero)
             {
                 throw new Exception($"pathfinding ptr is zero: {self.Scene().Name}");
             }
@@ -95,7 +98,7 @@ namespace ET
         /// <exception cref="Exception"></exception>
         public static float3 FindRandomPointWithRectangle(this PathfindingComponent self, float3 pos, int width, int height)
         {
-            if (self.NavMesh == 0)
+            if (self.navMesh == IntPtr.Zero)
             {
                 throw new Exception($"pathfinding ptr is zero: {self.Scene().Name}");
             }
@@ -115,7 +118,7 @@ namespace ET
         
         public static float3 FindRandomPointWithRaduis(this PathfindingComponent self, float3 pos, float minRadius, float maxRadius)
         {
-            if (self.NavMesh == 0)
+            if (self.navMesh == IntPtr.Zero)
             {
                 throw new Exception($"pathfinding ptr is zero: {self.Scene().Name}");
             }
@@ -138,7 +141,7 @@ namespace ET
 
         public static float3 RecastFindNearestPoint(this PathfindingComponent self, float3 pos)
         {
-            if (self.NavMesh == 0)
+            if (self.navMesh == IntPtr.Zero)
             {
                 throw new Exception($"pathfinding ptr is zero: {self.Scene().Name}");
             }
@@ -147,7 +150,7 @@ namespace ET
             self.StartPos[1] = pos.y;
             self.StartPos[2] = pos.z;
 
-            int ret = Recast.RecastFindNearestPoint(self.NavMesh, PathfindingComponent.extents, self.StartPos, self.EndPos);
+            int ret = Recast.RecastFindNearestPoint(self.navMesh, PathfindingComponent.extents, self.StartPos, self.EndPos);
             if (ret == 0)
             {
                 throw new Exception($"RecastFindNearestPoint fail, 可能是位置配置有问题: sceneName:{self.Scene().Name} {pos} {self.Name} {self.GetParent<Unit>().Id} {self.GetParent<Unit>().Config.Id} {self.EndPos.ArrayToString()}");

+ 1 - 1
Unity/Assets/Scripts/Model/Share/Module/Recast/PathfindingComponent.cs

@@ -16,7 +16,7 @@ namespace ET
         
         public string Name;
         
-        public long NavMesh;
+        public IntPtr navMesh;
 
         public float[] StartPos = new float[3];
 

+ 6 - 37
Unity/Assets/Scripts/ThirdParty/Recast/Recast.cs

@@ -13,54 +13,23 @@ namespace ET
         public const int MAX_POLYS = 256;
         
         [DllImport(RecastDLL, CallingConvention=CallingConvention.Cdecl)]
-        private static extern IntPtr RecastLoad(int id, byte[] buffer, int n);
-
-        public static long RecastLoadLong(int id, byte[] buffer, int n)
-        {
-            return RecastLoad(id, buffer, n).ToInt64();
-        }
-        
-        [DllImport(RecastDLL, CallingConvention=CallingConvention.Cdecl)]
-        private static extern IntPtr RecastGet(int id);
-
-        public static long RecastGetLong(int id)
-        {
-            return RecastGet(id).ToInt32();
-        }
+        public static extern IntPtr RecastLoad(byte[] buffer, int n);
         
         [DllImport(RecastDLL, CallingConvention=CallingConvention.Cdecl)]
-        private static extern void RecastClear();
+        public static extern void RecastClear(IntPtr navPtr);
         
         [DllImport(RecastDLL, CallingConvention=CallingConvention.Cdecl)]
-        private static extern int RecastFind(IntPtr navPtr, float[] extents, float[] startPos, float[] endPos, float[] straightPath);
+        public static extern int RecastFind(IntPtr navPtr, float[] extents, float[] startPos, float[] endPos, float[] straightPath);
         
-        public static int RecastFind(long navPtr, float[] extents, float[] startPos, float[] endPos, float[] straightPath)
-        {
-            return RecastFind(new IntPtr(navPtr), extents, startPos, endPos, straightPath);
-        }
         
         [DllImport(RecastDLL, CallingConvention=CallingConvention.Cdecl)]
-        private static extern int RecastFindNearestPoint(IntPtr navPtr, float[] extents, float[] pos, float[] nearestPos);
+        public static extern int RecastFindNearestPoint(IntPtr navPtr, float[] extents, float[] pos, float[] nearestPos);
 
-        public static int RecastFindNearestPoint(long navPtr, float[] extents, float[] pos, float[] nearestPos)
-        {
-            return RecastFindNearestPoint(new IntPtr(navPtr), extents, pos, nearestPos);
-        }
         
         [DllImport(RecastDLL, CallingConvention=CallingConvention.Cdecl)]
-        private static extern int RecastFindRandomPointAroundCircle(IntPtr navPtr, float[] extents, float[] centerPos, float radius, float[] randomPos);
-        
-        public static int RecastFindRandomPointAroundCircle(long navPtr, float[] extents, float[] centerPos, float radius, float[] randomPos)
-        {
-            return RecastFindRandomPointAroundCircle(new IntPtr(navPtr), extents, centerPos, radius, randomPos);
-        }
+        public static extern int RecastFindRandomPointAroundCircle(IntPtr navPtr, float[] extents, float[] centerPos, float radius, float[] randomPos);
         
         [DllImport(RecastDLL, CallingConvention=CallingConvention.Cdecl)]
-        private static extern int RecastFindRandomPoint(IntPtr navPtr, float[] randomPos);
-
-        public static int RecastFindRandomPoint(long navPtr, float[] randomPos)
-        {
-            return RecastFindRandomPoint(new IntPtr(navPtr), randomPos);
-        }
+        public static extern int RecastFindRandomPoint(IntPtr navPtr, float[] randomPos);
     }
 }