ArtInfoMgr.cs 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. using Newtonsoft.Json;
  2. using PublicTools.XMLDataBase;
  3. using ShadowStudio.Tool;
  4. using Studio.Scripts;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. #if UNITY_EDITOR
  10. using UnityEditor;
  11. #endif
  12. using UnityEngine;
  13. using XRTool.UI;
  14. using XRTool.Util;
  15. namespace ShadowStudio.Model
  16. {
  17. /// <summary>
  18. /// 资源映射表
  19. /// 读取,获取资源映射表
  20. /// 按照id和类型建立资源映射。通过artid获取对应的artinfo
  21. /// 通过资源类型获取对应的artinfo
  22. /// 创建一个美术资源:
  23. /// </summary>
  24. public class ArtInfoMgr : Singleton<ArtInfoMgr>
  25. {
  26. private string tableName;
  27. private TableInterface table;
  28. private bool isInit = false;
  29. /// <summary>
  30. /// 所有的资源列表,此列表当前服务器和本地各有一份。后期读取服务器列表数据
  31. /// </summary>
  32. private List<ArtInfo> artInfoList;
  33. /// <summary>
  34. /// 资源id对应的资源信息
  35. /// </summary>
  36. private Dictionary<string, ArtInfo> artInfoMap;
  37. private Dictionary<string, ArtInfo> otherArtInfoMap;
  38. /// <summary>
  39. /// 资源类型对应的资源列表
  40. /// </summary>
  41. private Dictionary<ArtType, List<ArtInfo>> artInfoTypeMap;
  42. /// <summary>
  43. /// 组件的集合
  44. /// </summary>
  45. private Dictionary<string, ArtComponent> artComponentMap = new Dictionary<string, ArtComponent>();
  46. /// <summary>
  47. /// 资源的引用
  48. /// </summary>
  49. private Dictionary<string, ArtResConf> resArtMap;
  50. /// <summary>
  51. /// 资源对应的工具类
  52. /// </summary>
  53. private Dictionary<ArtInfo, ArtHandler> artHandlerMap = new Dictionary<ArtInfo, ArtHandler>();
  54. /// <summary>
  55. /// 对应资源类型的容器,例如视频播放器,图片浏览器,模型浏览器
  56. /// </summary>
  57. private Dictionary<ArtType, List<ArtContainer>> artContainerMap = new Dictionary<ArtType, List<ArtContainer>>();
  58. /// <summary>
  59. /// 数据初始化
  60. /// </summary>
  61. public event Action InitComplete;
  62. /// <summary>
  63. /// 对应资源存储的最大的id
  64. /// 本地生成goods的一种辅助方案,此方案即将移除
  65. /// 此方案已移除,请勿使用2020-09-14
  66. /// </summary>
  67. private Dictionary<ArtInfo, int> goodsIdMap = new Dictionary<ArtInfo, int>();
  68. /// <summary>
  69. /// 使用变量缓存服务器下发的物体信息数据
  70. /// </summary>
  71. private GoodsInfo serverCacheData = new GoodsInfo();
  72. /// <summary>
  73. /// 此为上传至服务器的物体的数据缓存,理论上上传多个物体列表信息,实际处理中每次只会上传一条物体的同步信息
  74. /// </summary>
  75. private List<GoodsInfo> goodsDataList = new List<GoodsInfo>();
  76. /// <summary>
  77. /// 创建容器的事件,某个容器被上传的事件
  78. /// </summary>
  79. public event Action<ArtContainer> ContainerCreate;
  80. /// <summary>
  81. /// 删除容器,某个容器被删除的事件
  82. /// </summary>
  83. public event Action<ArtContainer> ContainerDel;
  84. /// <summary>
  85. /// 容器被选中或者容器非选中状态的事件
  86. /// </summary>
  87. public static Action<ArtContainer, bool> ContainerSelect;
  88. /// <summary>
  89. /// 编辑菜单的名称
  90. /// </summary>
  91. private string ModelEditBtnGroup = "ModelEditBtnDlg";
  92. public ArtEditorContainer ModelEditBtn;
  93. /// <summary>
  94. /// 容器缓存
  95. /// 非即时同步容器缓存
  96. /// </summary>
  97. private Dictionary<string, ArtContainer> containerCache = new Dictionary<string, ArtContainer>();
  98. public event Action<string, bool> UserChange;
  99. public void AddCacheContainer(string id, ArtContainer container)
  100. {
  101. if (!ContainerCache.ContainsKey(id))
  102. {
  103. ContainerCache.Add(id, container);
  104. }
  105. }
  106. public void SetContainerSelect(ArtContainer container, bool isSelect)
  107. {
  108. ContainerSelect?.Invoke(container, isSelect);
  109. }
  110. public bool IsSingleMan()
  111. {
  112. if (GameNode.Instance)
  113. {
  114. return GameNode.Instance.UserMap == null || GameNode.Instance.UserMap.Count < 2;
  115. }
  116. return true;
  117. }
  118. public void RemoveArtInfo(List<UnityEngine.Object> resModelList)
  119. {
  120. for (int i = 0; i < resModelList.Count; i++)
  121. {
  122. string artid = "ArtId_" + resModelList[i].name;
  123. table.DeleteData<ArtInfo>(artid);
  124. }
  125. }
  126. /// <summary>
  127. /// 将数据添加至xml
  128. /// </summary>
  129. /// <param name="resModelList"></param>
  130. /// <param name="artType"></param>
  131. /// <param name="references"></param>
  132. public void AddArtInfo(List<UnityEngine.Object> resModelList, ArtType artType, InstaceType references, bool isForceUpdate, string url = "")
  133. {
  134. if (table == null)
  135. {
  136. if (BuildConfigMgr.Instance.IsInit)
  137. {
  138. ReadArtInfo(Path.Combine(ResourcesManager.LocalPath, BuildConfig.Instance.artInfoPath));
  139. }
  140. }
  141. if (table == null)
  142. {
  143. UnityLog.Instance.Log("Read ArtInfo error");
  144. }
  145. if (ArtInfoList == null)
  146. {
  147. ArtInfoList = new List<ArtInfo>();
  148. }
  149. for (int i = 0; i < resModelList.Count; i++)
  150. {
  151. UnityEngine.Object obj = resModelList[i];
  152. string artid = "ArtId_" + obj.name;
  153. if (isForceUpdate || !ArtInfoContains(artid))
  154. {
  155. ArtInfo info = new ArtInfo();
  156. info.ArtId = artid;
  157. info.ArtName = obj.name;
  158. info.ArtType = artType;
  159. info.Distance = 1f;
  160. ///Icon
  161. info.Icon = "ArtIcon/" + artType + "/" + obj.name;
  162. info.InstaceType = references;
  163. info.Size = 1;
  164. if (artType == ArtType.Prefab)
  165. {
  166. info.Distance = (obj as GameObject).transform.localPosition.z;
  167. info.Size = (obj as GameObject).transform.localScale.x;
  168. }
  169. if (references == InstaceType.References)
  170. {
  171. info.Url = url;
  172. }
  173. else if (references == InstaceType.ResourceLoad)
  174. {
  175. #if UNITY_EDITOR
  176. string path = AssetDatabase.GetAssetPath(obj);
  177. int index = path.LastIndexOf("Resources");
  178. path = path.Substring((index += "Resources".Length + 1), path.Length - index - 1);
  179. path = path.Replace(Path.GetExtension(path), "");
  180. info.Url = path;
  181. #endif
  182. }
  183. info.Version = Application.version;
  184. table.InsertData(info);
  185. if (!ArtInfoContains(artid))
  186. {
  187. ArtInfoList.Add(info);
  188. }
  189. }
  190. }
  191. table.Save();
  192. }
  193. public bool ArtInfoContains(string artId)
  194. {
  195. for (int i = 0; i < ArtInfoList.Count; i++)
  196. {
  197. if (ArtInfoList[i].ArtId == artId)
  198. {
  199. return true;
  200. }
  201. }
  202. return false;
  203. }
  204. public bool IsInit { get => isInit; set => isInit = value; }
  205. public List<ArtInfo> ArtInfoList { get => artInfoList; set => artInfoList = value; }
  206. public Dictionary<string, ArtContainer> ContainerCache { get => containerCache; set => containerCache = value; }
  207. public Dictionary<string, ArtInfo> OtherArtInfoMap
  208. {
  209. get
  210. {
  211. if (otherArtInfoMap == null)
  212. {
  213. otherArtInfoMap = new Dictionary<string, ArtInfo>();
  214. }
  215. return otherArtInfoMap;
  216. }
  217. }
  218. /// <summary>
  219. /// 房间内物体的映射
  220. /// 房间id与对应的物体资源映射
  221. /// </summary>
  222. public void LoadModelEditBtn()
  223. {
  224. if (!ModelEditBtn)
  225. {
  226. var tmp = Resources.Load<ArtEditorContainer>(ModelEditBtnGroup);
  227. if (tmp)
  228. {
  229. ModelEditBtn = GameObject.Instantiate(tmp);
  230. GameNode.Instance.SetParent(ObjNode.WorldCanvas, ModelEditBtn.transform, Vector3.zero, Vector3.zero, Vector3.one * 0.001f, false);
  231. ModelEditBtn.gameObject.SetActive(false);
  232. }
  233. }
  234. }
  235. public ArtInfoMgr()
  236. {
  237. var artResList = Resources.LoadAll<ArtResConf>("ArtConf");
  238. resArtMap = new Dictionary<string, ArtResConf>();
  239. if (artResList != null)
  240. {
  241. for (int i = 0; i < artResList.Length; i++)
  242. {
  243. resArtMap.Add(artResList[i].name, artResList[i]);
  244. }
  245. }
  246. var containers = Resources.LoadAll<GameObject>("Container");
  247. if (containers != null)
  248. {
  249. for (int i = 0; i < containers.Length; i++)
  250. {
  251. var container = containers[i].GetComponent<ArtContainer>();
  252. if (container != null)
  253. {
  254. if (!artContainerMap.ContainsKey(container.GetArtType()))
  255. {
  256. List<ArtContainer> list = new List<ArtContainer>();
  257. list.Add(container);
  258. artContainerMap.Add(container.GetArtType(), list);
  259. }
  260. else
  261. {
  262. List<ArtContainer> list = artContainerMap[container.GetArtType()];
  263. list.Add(container);
  264. }
  265. }
  266. }
  267. }
  268. var components = Resources.LoadAll<GameObject>("Container/Component");
  269. if (components != null)
  270. {
  271. for (int i = 0; i < components.Length; i++)
  272. {
  273. var component = containers[i].GetComponent<ArtComponent>();
  274. if (component != null)
  275. {
  276. if (!artComponentMap.ContainsKey(containers[i].name))
  277. {
  278. artComponentMap.Add(containers[i].name, component);
  279. }
  280. }
  281. }
  282. }
  283. //if (BuildConfigMgr.Instance.IsInit)
  284. //{
  285. // ReadArtInfo(Path.Combine(ResourcesManager.LocalPath, BuildConfig.Instance.artInfoPath));
  286. //}
  287. else
  288. {
  289. UnityLog.Instance.LogError("配置文件丢失!");
  290. }
  291. WSHandler.Room.OnBroadcastGood += SyncGoods;
  292. ContainerSelect += OnContainerSelect;
  293. }
  294. public void ListenUser()
  295. {
  296. if (ShowViewMgr.Instance)
  297. {
  298. ShowViewMgr.Instance.OnUserChange -= OnUserChange;
  299. ShowViewMgr.Instance.OnUserChange += OnUserChange;
  300. }
  301. else
  302. {
  303. ShowViewMgr.InitComplte += () =>
  304. {
  305. if (ShowViewMgr.Instance)
  306. {
  307. ShowViewMgr.Instance.OnUserChange -= OnUserChange;
  308. ShowViewMgr.Instance.OnUserChange += OnUserChange;
  309. }
  310. };
  311. }
  312. }
  313. public void ListenPage()
  314. {
  315. if (ShowViewMgr.Instance)
  316. {
  317. ShowViewMgr.Instance.OnRoomPageChange -= OnChangePage;
  318. ShowViewMgr.Instance.OnRoomPageChange += OnChangePage;
  319. }
  320. else
  321. {
  322. ShowViewMgr.InitComplte += () =>
  323. {
  324. if (ShowViewMgr.Instance)
  325. {
  326. ShowViewMgr.Instance.OnRoomPageChange -= OnChangePage;
  327. ShowViewMgr.Instance.OnRoomPageChange += OnChangePage;
  328. }
  329. };
  330. }
  331. }
  332. private void OnContainerSelect(ArtContainer container, bool isSelect)
  333. {
  334. if (container.GetGoodsInfo().art_id == "ArtId_PlayerView")
  335. {
  336. return;
  337. }
  338. if (!ModelEditBtn)
  339. {
  340. LoadModelEditBtn();
  341. }
  342. if (ModelEditBtn)
  343. {
  344. if (ModelEditBtn.gameObject.activeSelf != isSelect)
  345. {
  346. ModelEditBtn.gameObject.SetActive(isSelect);
  347. }
  348. if (isSelect)
  349. {
  350. ModelEditBtn.SetTarget(container, false);
  351. }
  352. }
  353. if (ArtMoreEditor.Instance)
  354. {
  355. if (isSelect)
  356. {
  357. ArtMoreEditor.Instance.SetTarget(container, false);
  358. }
  359. }
  360. }
  361. /// <summary>
  362. /// 读取美术资源
  363. /// </summary>
  364. /// <param name="path"></param>
  365. private void ReadArtInfo(string path)
  366. {
  367. //languageIndex = SystemSettingMgr.Instance.settings.Language;
  368. tableName = typeof(ArtInfo).Name;
  369. #if UNITY_EDITOR || !UNITY_ANDROID
  370. table = XSql.Instance.OpenTable(path, tableName, ".xml", true);
  371. OpenTable();
  372. #else
  373. string fullPath = Path.Combine(path, tableName + ".xml");
  374. GameNode.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List<ArtInfo> packList) =>
  375. {
  376. InitPackage(packList);
  377. }));
  378. #endif
  379. }
  380. public void OpenLocalData()
  381. {
  382. ReadArtInfo(Path.Combine(ResourcesManager.LocalPath, BuildConfig.Instance.artInfoPath));
  383. }
  384. private void OpenTable()
  385. {
  386. if (!table.Open())
  387. {
  388. table.Create(tableName);
  389. UnityLog.Instance.Log("create table" + tableName);
  390. }
  391. var list = table.FindAllData<ArtInfo>();
  392. if (list != null)
  393. {
  394. for (int i = 0; i < list.Count; i++)
  395. {
  396. //list[i].ArtType = CommonMethod.getArtType((int)list[i].ArtType);
  397. }
  398. }
  399. //table.Save();
  400. //languagePackList = table.FindAllData<LanguagePackConf>();
  401. InitPackage(list);
  402. }
  403. /// <summary>
  404. /// 初始化包
  405. /// </summary>
  406. /// <param name="lists"></param>
  407. public void InitPackage(List<ArtInfo> lists)
  408. {
  409. #if UNITY_EDITOR
  410. for (int i = 0; i < lists.Count; i++)
  411. {
  412. UnityLog.Instance.Log("InitPackage:" + JsonConvert.SerializeObject(lists[i]));
  413. }
  414. //if (DataConfMgr<ArtInfo>.Instance.TableHelper == null)
  415. //{
  416. // DataConfMgr<ArtInfo>.Instance.OpenData(Application.persistentDataPath);
  417. //}
  418. //if (!DataConfMgr<ArtInfo>.Instance.TableHelper.isInit)
  419. //{
  420. // DataConfMgr<ArtInfo>.Instance.OpenTable("ArtId");
  421. //}
  422. //for (int i = 0; i < lists.Count; i++)
  423. //{
  424. // DataConfMgr<ArtInfo>.Instance.TableHelper.AddData(lists[i],false);
  425. //}
  426. //DataConfMgr<ArtInfo>.Instance.TableHelper.SaveData();
  427. #endif
  428. ArtInfoList = lists;
  429. if (ArtInfoList == null || ArtInfoList.Count < 1)
  430. {
  431. UnityLog.Instance.LogError("美术资源不存在!");
  432. #if UNITY_EDITOR
  433. //AddDefaultArt(ArtType.Model);
  434. //InitPackage(ArtInfoList);
  435. #endif
  436. return;
  437. }
  438. else
  439. {
  440. if (artInfoMap == null)
  441. {
  442. artInfoMap = new Dictionary<string, ArtInfo>();
  443. }
  444. if (artInfoTypeMap == null)
  445. {
  446. artInfoTypeMap = new Dictionary<ArtType, List<ArtInfo>>();
  447. }
  448. for (int i = 0; i < ArtInfoList.Count; i++)
  449. {
  450. if (!artInfoMap.ContainsKey(ArtInfoList[i].ArtId))
  451. {
  452. artInfoMap.Add(ArtInfoList[i].ArtId, ArtInfoList[i]);
  453. UnityLog.Instance.Log(ArtInfoList[i].Url, 3);
  454. }
  455. if (artInfoTypeMap.ContainsKey(ArtInfoList[i].ArtType))
  456. {
  457. List<ArtInfo> list = artInfoTypeMap[ArtInfoList[i].ArtType];
  458. if (list != null && !list.Contains(ArtInfoList[i]))
  459. {
  460. list.Add(ArtInfoList[i]);
  461. }
  462. }
  463. else
  464. {
  465. List<ArtInfo> list = new List<ArtInfo>();
  466. list.Add(ArtInfoList[i]);
  467. artInfoTypeMap.Add(ArtInfoList[i].ArtType, list);
  468. }
  469. }
  470. }
  471. IsInit = true;
  472. InitComplete?.Invoke();
  473. }
  474. /// <summary>
  475. /// 添加Unity基本资源到文本
  476. /// </summary>
  477. public void AddDefaultArt(ArtType artType)
  478. {
  479. if (ArtInfoList == null)
  480. {
  481. ArtInfoList = new List<ArtInfo>();
  482. }
  483. if (artType == ArtType.Model)
  484. {
  485. foreach (PrimitiveType primitive in Enum.GetValues(typeof(PrimitiveType)))
  486. {
  487. ArtInfo info = new ArtInfo();
  488. info.ArtId = "ArtId_" + (int)primitive;
  489. info.ArtName = primitive.ToString();
  490. info.ArtType = ArtType.Model;
  491. info.Distance = 1f;
  492. ///Icon
  493. info.Icon = "PrimitiveType/" + primitive.ToString();
  494. info.InstaceType = InstaceType.UnityBase;
  495. info.Size = 1;
  496. info.Url = "" + (int)primitive;
  497. info.Version = Application.unityVersion;
  498. table.InsertData(info);
  499. ArtInfoList.Add(info);
  500. }
  501. }
  502. else
  503. {
  504. ArtInfo info = new ArtInfo();
  505. info.ArtId = "ArtId_GameObject";
  506. info.ArtName = "GameObject";
  507. info.ArtType = ArtType.Prefab;
  508. info.Distance = 1f;
  509. ///Icon
  510. info.Icon = "";
  511. info.InstaceType = InstaceType.UnityBase;
  512. info.Size = 1;
  513. info.Url = "New";
  514. info.Version = Application.unityVersion;
  515. table.InsertData(info);
  516. ArtInfoList.Add(info);
  517. }
  518. table.Save();
  519. }
  520. /// <summary>
  521. /// 根据资源id获取对应的资源
  522. /// </summary>
  523. /// <param name="artId"></param>
  524. /// <returns></returns>
  525. public ArtInfo GetArtInfo(string artId)
  526. {
  527. if (IsInit && artInfoMap.ContainsKey(artId))
  528. {
  529. return artInfoMap[artId];
  530. }
  531. return null;
  532. }
  533. /// <summary>
  534. /// 根据资源id获取对应的资源
  535. /// </summary>
  536. /// <param name="artId"></param>
  537. /// <returns></returns>
  538. public List<ArtInfo> GetArtInfoList(ArtType artType)
  539. {
  540. if (IsInit)//
  541. {
  542. if (artInfoTypeMap.ContainsKey(artType))
  543. {
  544. return artInfoTypeMap[artType];
  545. }
  546. List<ArtInfo> tmp = new List<ArtInfo>();
  547. foreach (var item in artInfoTypeMap)
  548. {
  549. ///包含此key
  550. if ((item.Key & artType) == item.Key)
  551. {
  552. tmp.AddRange(item.Value);
  553. }
  554. }
  555. artInfoTypeMap.Add(artType, tmp);
  556. return tmp;
  557. }
  558. return null;
  559. }
  560. ///// <summary>
  561. ///// 根据资源id获取对应的资源
  562. ///// </summary>
  563. ///// <param name="artId"></param>
  564. ///// <returns></returns>
  565. //public List<ArtInfo> GetArtInfoList(ArtType[] artType)
  566. //{
  567. // if (IsInit)//
  568. // {
  569. // List<ArtInfo> ArtInfoList = new List<ArtInfo>();
  570. // for (int i = 0; i < artType.Length; i++)
  571. // {
  572. // if (artInfoTypeMap.ContainsKey(artType[i]))
  573. // {
  574. // ArtInfoList.AddRange(artInfoTypeMap[artType[i]]);
  575. // }
  576. // }
  577. // return ArtInfoList;
  578. // }
  579. // return null;
  580. //}
  581. /// <summary>
  582. /// 根据资源id获取对应的资源
  583. /// </summary>
  584. /// <param name="artId"></param>
  585. /// <returns></returns>
  586. public List<ArtInfo> GetArtInfoListSingle(ArtType artType)
  587. {
  588. if (IsInit)//
  589. {
  590. return artInfoTypeMap[artType];
  591. }
  592. return null;
  593. }
  594. /// <summary>
  595. /// 创建一个资源ArtHandler,注意内存回收
  596. /// </summary>
  597. /// <returns></returns>
  598. public ArtHandler CreateArtHandler(ArtInfo art, string containerName = "")
  599. {
  600. if (string.IsNullOrEmpty(containerName))
  601. {
  602. containerName = art.ContainerName;
  603. }
  604. if (artHandlerMap.ContainsKey(art))
  605. {
  606. return artHandlerMap[art];
  607. }
  608. ArtHandler handler = new ArtHandler();
  609. handler.SetData(art, containerName);
  610. artHandlerMap.Add(art, handler);
  611. return handler;
  612. }
  613. /// <summary>
  614. /// 删除资源帮助类ArtHandler
  615. /// </summary>
  616. /// <param name="art"></param>
  617. public void DestrorArtHandler(ArtInfo art)
  618. {
  619. if (artHandlerMap.ContainsKey(art))
  620. {
  621. var handler = artHandlerMap[art];
  622. handler = null;
  623. artHandlerMap.Remove(art);
  624. }
  625. }
  626. /// <summary>
  627. /// 实例化组件
  628. /// </summary>
  629. /// <param name="componentName"></param>
  630. /// <returns></returns>
  631. public ArtComponent InstanceComponent(string componentName)
  632. {
  633. if (artComponentMap != null && artComponentMap.ContainsKey(componentName))
  634. {
  635. ArtComponent component = artComponentMap[componentName];
  636. var tmp = GameObject.Instantiate(component.GetInstace());
  637. return tmp.GetComponent<ArtComponent>();
  638. }
  639. return null;
  640. }
  641. /// <summary>
  642. /// 实例化出一个容器
  643. /// </summary>
  644. /// <param name="artType"></param>
  645. /// <returns></returns>
  646. public ArtContainer CreateContainer(ArtType artType, string containerName = "")
  647. {
  648. if (artContainerMap != null && artContainerMap.ContainsKey(artType))
  649. {
  650. var containerList = artContainerMap[artType];
  651. if (containerList != null)
  652. {
  653. ArtContainer container = null;
  654. if (containerList.Count == 1)
  655. {
  656. container = containerList[0];
  657. }
  658. else
  659. {
  660. for (int i = 0; i < containerList.Count; i++)
  661. {
  662. if (containerList[i].GetInstace().name == containerName)
  663. {
  664. container = containerList[i];
  665. break;
  666. }
  667. }
  668. }
  669. if (container != null)
  670. {
  671. var tmp = GameObject.Instantiate(container.GetInstace());
  672. return tmp.GetComponent<ArtContainer>();
  673. }
  674. }
  675. }
  676. return null;
  677. }
  678. /// <summary>
  679. /// 获取对应的配置文件
  680. /// </summary>
  681. /// <param name="resName"></param>
  682. /// <returns></returns>
  683. public ArtResConf GetResConf(string resName)
  684. {
  685. if (resArtMap.ContainsKey(resName))
  686. {
  687. return resArtMap[resName];
  688. }
  689. UnityLog.Instance.LogError(resName + " is not at resArtMap");
  690. return null;
  691. }
  692. /// <summary>
  693. /// 物品的自增id设计
  694. /// </summary>
  695. /// <param name="info"></param>
  696. public int GetGoodsId(ArtInfo info)
  697. {
  698. int id = 1;
  699. if (goodsIdMap.ContainsKey(info))
  700. {
  701. id = goodsIdMap[info] + 1;
  702. goodsIdMap[info] = id;
  703. }
  704. else
  705. {
  706. goodsIdMap.Add(info, id);
  707. }
  708. return id;
  709. }
  710. /// <summary>
  711. /// 同步物体:
  712. /// 创建物体时,接收到广播消息时的数据同步都调用此方法
  713. /// </summary>
  714. /// <param name="rid">用户id,物体的创建者</param>
  715. /// <param name="art_id">美术资源id</param>
  716. /// <param name="goods_name">物体名称</param>
  717. /// <param name="goods_extended">物体额外信息</param>
  718. /// <param name="id">物体id</param>
  719. /// <param name="goods_info">物体的姿态信息</param>
  720. public void SyncGoods(int rid, int status, string art_id, string goods_name, string goods_extended, int id, string goods_info)
  721. {
  722. serverCacheData.rid = rid;
  723. serverCacheData.art_id = art_id;
  724. serverCacheData.goods_name = goods_name;
  725. serverCacheData.goods_extended = goods_extended;
  726. serverCacheData.id = id;
  727. serverCacheData.goods_info = goods_info;
  728. serverCacheData.status = status;
  729. SyncGoods(serverCacheData);
  730. }
  731. public void SyncGoods(List<GoodsInfo> goodDataList)
  732. {
  733. if (goodDataList != null)
  734. {
  735. for (int i = 0; i < goodDataList.Count; i++)
  736. {
  737. SyncGoods(goodDataList[i]);
  738. }
  739. }
  740. }
  741. /// <summary>
  742. /// 同步物体数据
  743. /// 服务器同步或者翻页时同步,服务器同步时开始缓存数据
  744. /// 翻页时同步不添加缓存数据
  745. /// </summary>
  746. /// <param name="goodData"></param>
  747. /// <param name="isServer"></param>
  748. public void SyncGoods(GoodsInfo goodData, bool isServer = true)
  749. {
  750. if (GameNode.Instance.GoodsContainerMap == null)
  751. {
  752. GameNode.Instance.GoodsContainerMap = new Dictionary<int, ArtContainer>();
  753. }
  754. ArtContainer container = null;
  755. bool isCreate = false;
  756. if (SynUser(goodData))
  757. {
  758. container = GameNode.Instance.UserMap[goodData.art_id].Container;
  759. }
  760. else if (goodData.id != 0)
  761. {
  762. ///先检查此数据是否在此页面
  763. ///如果数据不在当前页,则只缓存起来
  764. ///如果在当前页,则缓存后实例化进行同步
  765. ///如果不是服务器的数据,仅同步设置,不缓存数据
  766. if (isServer && goodData.scene_id > 0)
  767. {
  768. if (!GameNode.Instance.PageGoods.ContainsKey(goodData.id))
  769. {
  770. GameNode.Instance.PageGoods.Add(goodData.id, goodData);
  771. }
  772. else
  773. {
  774. GameNode.Instance.PageGoods[goodData.id] = goodData;
  775. }
  776. if (!GameNode.Instance.PageGoodsId.ContainsKey(goodData.scene_id))
  777. {
  778. var list = new List<int>();
  779. list.Add(goodData.id);
  780. if (goodData.art_id != "ArtId_GameObject")
  781. {
  782. GameNode.Instance.AddGoodData(goodData);
  783. }
  784. GameNode.Instance.PageGoodsId.Add(goodData.scene_id, list);
  785. }
  786. else
  787. {
  788. var list = GameNode.Instance.PageGoodsId[goodData.scene_id];
  789. if (list == null)
  790. {
  791. list = new List<int>();
  792. }
  793. if (!list.Contains(goodData.id))
  794. {
  795. list.Add(goodData.id);
  796. if (goodData.art_id != "ArtId_GameObject")
  797. {
  798. GameNode.Instance.AddGoodData(goodData);
  799. }
  800. }
  801. }
  802. if (goodData.scene_id != CommonMethod.currentScene.Id)
  803. {
  804. return;
  805. }
  806. }
  807. ///对象已存在,进行同步
  808. if (GameNode.Instance.GoodsContainerMap.ContainsKey(goodData.id))
  809. {
  810. container = GameNode.Instance.GoodsContainerMap[goodData.id];
  811. }
  812. else
  813. {
  814. //Debug.LogError(" 请求物品信息===:" + goodData.art_id);
  815. ArtInfo info = GetArtInfo(goodData.art_id);
  816. if (info == null)
  817. {
  818. if (OtherArtInfoMap.ContainsKey(goodData.art_id))
  819. {
  820. info = OtherArtInfoMap[goodData.art_id];
  821. }
  822. else
  823. {
  824. HomeProxy.Instance.GetArtInfoDetailByArtID(goodData.art_id, (artInfo) =>
  825. {
  826. //Debug.LogWarning("回包信息 goodData.art_id "+ goodData.art_id+" "+artInfo.AID+"QQQ" + artInfo.ArtId + "UUU" + artInfo.Url + "RRR" + artInfo.ArtType);
  827. if (goodData.art_id == artInfo.ArtId)
  828. {
  829. //Debug.LogWarning("确认回包信息");
  830. if (!OtherArtInfoMap.ContainsKey(goodData.art_id))
  831. {
  832. info = artInfo;
  833. if (info != null)
  834. {
  835. //Debug.LogWarning($"添加数据到字典 key:{goodData.art_id} value:{info.ArtId} {info.ArtName} {info.ArtType}");
  836. OtherArtInfoMap.Add(goodData.art_id, info);
  837. SyncGoods(goodData, isServer);
  838. //UnityLog.Instance.LogError("创建完毕 find artinfo:" + goodData.art_id + "info" + info.ArtId);
  839. return;
  840. }
  841. else
  842. {
  843. UnityLog.Instance.LogError("Error find artinfo:" + goodData.art_id);
  844. }
  845. }
  846. }
  847. });
  848. }
  849. }
  850. if (info != null)
  851. {
  852. ///容器缓存
  853. if (ContainerCache.ContainsKey(goodData.art_id))
  854. {
  855. container = ContainerCache[goodData.art_id];
  856. ContainerCache.Remove(goodData.art_id);
  857. }
  858. else
  859. {
  860. ArtHandler handler = CreateArtHandler(info);
  861. //Debug.LogError($"创建 info aid {info.AID } artid {info.ArtId} InstaceType {info.InstaceType} handler {handler.Instancer}");
  862. container = handler.CreateArt();
  863. }
  864. GameNode.Instance.GoodsContainerMap.Add(goodData.id, container);
  865. isCreate = true;
  866. }
  867. else
  868. {
  869. UnityLog.Instance.LogError(goodData.art_id);
  870. }
  871. }
  872. }
  873. try
  874. {
  875. if (container != null && container.GetInstace())
  876. {
  877. container.TransferSyn(goodData, goodData.status == (int)TransferState.Doing);
  878. if (isCreate)
  879. {
  880. ContainerCreate?.Invoke(container);
  881. }
  882. }
  883. }
  884. catch
  885. {
  886. UnityLog.Instance.LogError("SyncGoods Error:" + JsonConvert.SerializeObject(goodData));
  887. ///某个物体可能被删掉了
  888. ///此种情况不在处理
  889. }
  890. }
  891. //Dictionary<string, string> form;
  892. //Dictionary<string, string> header;
  893. ///// <summary>
  894. ///// 读取资源信息
  895. ///// </summary>
  896. ///// <returns></returns>
  897. //public IEnumerator ReqOtherArtInfo(string artId, Action<ArtInfo> onReadComplete)
  898. //{
  899. // if (header == null)
  900. // {
  901. // header = new Dictionary<string, string>();
  902. // header.Add("Content-Type", "application/x-www-form-urlencoded");
  903. // }
  904. // if (form == null)
  905. // {
  906. // form = new Dictionary<string, string>();
  907. // form.Add("action", "");
  908. // }
  909. // yield return 0;
  910. //}
  911. /// <summary>
  912. /// 同步用户
  913. /// </summary>
  914. /// <param name="goodData"></param>
  915. /// <returns></returns>
  916. private bool SynUser(GoodsInfo goodData)
  917. {
  918. if (GameNode.Instance.UserMap == null)
  919. {
  920. GameNode.Instance.UserMap = new Dictionary<string, PlayerContainer>();
  921. }
  922. if (string.IsNullOrEmpty(goodData.art_id))
  923. {
  924. return false;
  925. }
  926. return GameNode.Instance.UserMap.ContainsKey(goodData.art_id);
  927. }
  928. /// <summary>
  929. /// 删除物体
  930. /// 可能由用户主动删除,主动删除传参true.删除相关的数据缓存
  931. /// 翻页时是被动删除。被动删除不移除相关的数据缓存
  932. /// </summary>
  933. /// <param name="id">物体的id</param>
  934. /// <param name="isRealDel">是否真实删除,默认真实删除</param>
  935. public void GoodsDelSync(int id, bool isRealDel = true)
  936. {
  937. if (GameNode.Instance.GoodsContainerMap == null)
  938. {
  939. GameNode.Instance.GoodsContainerMap = new Dictionary<int, ArtContainer>();
  940. }
  941. ArtContainer container = null;
  942. ///对象已存在,进行同步
  943. if (GameNode.Instance.GoodsContainerMap.ContainsKey(id))
  944. {
  945. container = GameNode.Instance.GoodsContainerMap[id];
  946. //if (ModelEditBtn)
  947. //{
  948. // ModelEditBtn.SetTarget(container, true);
  949. //}
  950. if (ArtMoreEditor.Instance)
  951. {
  952. ArtMoreEditor.Instance.SetTarget(container, true);
  953. }
  954. ContainerDel?.Invoke(container);
  955. if (container != null)
  956. {
  957. container.DestroyArt();
  958. }
  959. GameNode.Instance.GoodsContainerMap.Remove(id);
  960. }
  961. ///真实删除
  962. if (isRealDel)
  963. {
  964. ///删除物体的id
  965. ///先查找物体id对应的Goods
  966. if (GameNode.Instance.PageGoods.ContainsKey(id))
  967. {
  968. ///获得Goods
  969. var good = GameNode.Instance.PageGoods[id];
  970. ///查找页对应的物体的id
  971. if (GameNode.Instance.PageGoodsId.ContainsKey(good.scene_id))
  972. {
  973. ///获取页对应的id列表,并
  974. var list = GameNode.Instance.PageGoodsId[good.scene_id];
  975. if (list != null && list.Contains(id))
  976. {
  977. list.Remove(id);
  978. if (good.art_id != "ArtId_GameObject")
  979. {
  980. GameNode.Instance.RemoveGoodData(good);
  981. }
  982. }
  983. }
  984. GameNode.Instance.PageGoods.Remove(id);
  985. ContainerDel?.Invoke(null);
  986. }
  987. }
  988. }
  989. public void SendGoodsTransfer(GoodsInfo info)
  990. {
  991. goodsDataList.Add(info);
  992. WSHandler.Room.BroadcastGood(goodsDataList);
  993. goodsDataList.Clear();
  994. }
  995. public void SendTransfer(GoodsInfo info)
  996. {
  997. goodsDataList.Add(info);
  998. WSHandler.Room.TransmitGood(goodsDataList);
  999. goodsDataList.Clear();
  1000. }
  1001. /// <summary>
  1002. /// 当新用户进入时的事件
  1003. /// </summary>
  1004. /// <param name="userList"></param>
  1005. public void OnUserChange(List<Peer> userList, bool isJoin)
  1006. {
  1007. if (GameNode.Instance.UserMap == null)
  1008. {
  1009. GameNode.Instance.UserMap = new Dictionary<string, PlayerContainer>();
  1010. }
  1011. if (userList != null)
  1012. {
  1013. for (int i = 0; i < userList.Count; i++)
  1014. {
  1015. if (GameNode.Instance.UserMap.ContainsKey(userList[i].PeerId))
  1016. {
  1017. if (isJoin)
  1018. {
  1019. continue;
  1020. }
  1021. else
  1022. {
  1023. var container = GameNode.Instance.UserMap[userList[i].PeerId];
  1024. container.StopSyn();
  1025. container.DestroyArt();
  1026. UserChange?.Invoke(userList[i].PeerId, false);
  1027. GameNode.Instance.UserMap.Remove(userList[i].PeerId);
  1028. }
  1029. }
  1030. else
  1031. {
  1032. if (isJoin)
  1033. {
  1034. ArtInfo info = GetArtInfo(PlayerContainer.userArtId);
  1035. if (info != null)
  1036. {
  1037. //string cName = info.ContainerName;
  1038. //if (string.IsNullOrEmpty(cName))
  1039. //{
  1040. // cName = PlayerContainer.containerName;
  1041. //}
  1042. //ArtHandler handler = CreateArtHandler(info, PlayerContainer.containerName);
  1043. ArtHandler handler = CreateArtHandler(info);
  1044. var container = handler.CreateArt();
  1045. if (container != null)
  1046. {
  1047. if (container.ArtComponent != null && container.ArtComponent.GetInstace())
  1048. {
  1049. if (container.ArtComponent is PlayerContainer)
  1050. {
  1051. (container.ArtComponent as PlayerContainer).SetPeer(userList[i]);
  1052. GameNode.Instance.UserMap.Add(userList[i].PeerId, container.ArtComponent as PlayerContainer);
  1053. UserChange?.Invoke(userList[i].PeerId, true);
  1054. }
  1055. }
  1056. }
  1057. }
  1058. else
  1059. {
  1060. UnityLog.Instance.LogError(PlayerContainer.userArtId + " cant find art");
  1061. }
  1062. }
  1063. else
  1064. {
  1065. continue;
  1066. }
  1067. }
  1068. }
  1069. }
  1070. }
  1071. /// <summary>
  1072. /// 删除场景的物体
  1073. /// </summary>
  1074. /// <param name="id"></param>
  1075. /// <param name="isOnlyScene"></param>
  1076. public void DelGoodsById(int id)
  1077. {
  1078. bool isCanDel = true;
  1079. if (GameNode.Instance.PageGoods.ContainsKey(id))
  1080. {
  1081. var goods = GameNode.Instance.PageGoods[id];
  1082. if (goods.scene_id == CommonMethod.currentScene.Id)
  1083. {
  1084. isCanDel = true;
  1085. }
  1086. }
  1087. if (isCanDel)
  1088. {
  1089. WSHandler.Room.DeleteGood(new int[] { id });
  1090. }
  1091. }
  1092. public void DelGoodsByIds(List<int> ids)
  1093. {
  1094. List<int> list = new List<int>();
  1095. for (int i = 0; i < ids.Count; i++)
  1096. {
  1097. int id = ids[i];
  1098. if (GameNode.Instance.PageGoods.ContainsKey(id))
  1099. {
  1100. var goods = GameNode.Instance.PageGoods[id];
  1101. if (goods.scene_id == CommonMethod.currentScene.Id)
  1102. {
  1103. list.Add(id);
  1104. }
  1105. }
  1106. }
  1107. if (list.Count > 0)
  1108. {
  1109. WSHandler.Room.DeleteGood(list.ToArray());
  1110. }
  1111. }
  1112. /// <summary>
  1113. /// 翻页的数据处理
  1114. /// 翻页时注意,删除旧的数据
  1115. /// 同步新的数据
  1116. /// </summary>
  1117. /// <param name="all"></param>
  1118. /// <param name="current"></param>
  1119. public void OnChangePage(int all, int current)
  1120. {
  1121. if (GameNode.Instance)
  1122. {
  1123. if (GameNode.Instance.DefaultPageIndex != current)
  1124. {
  1125. if (GameNode.Instance.DefaultPageIndex >= 0)
  1126. {
  1127. if (GameNode.Instance.PageGoodsId.ContainsKey(GameNode.Instance.DefaultPageIndex))
  1128. {
  1129. var list = GameNode.Instance.PageGoodsId[GameNode.Instance.DefaultPageIndex];
  1130. for (int i = 0; i < list.Count; i++)
  1131. {
  1132. GoodsDelSync(list[i], false);
  1133. }
  1134. }
  1135. }
  1136. if (ModelEditBtn)
  1137. {
  1138. ModelEditBtn.gameObject.SetActive(false);
  1139. }
  1140. GameNode.Instance.DefaultPageIndex = current;
  1141. if (GameNode.Instance.DefaultPageIndex >= 0)
  1142. {
  1143. if (GameNode.Instance.PageGoodsId.ContainsKey(GameNode.Instance.DefaultPageIndex))
  1144. {
  1145. var list = GameNode.Instance.PageGoodsId[GameNode.Instance.DefaultPageIndex];
  1146. for (int i = 0; i < list.Count; i++)
  1147. {
  1148. if (GameNode.Instance.PageGoods.ContainsKey(list[i]))
  1149. {
  1150. SyncGoods(GameNode.Instance.PageGoods[list[i]], false);
  1151. }
  1152. }
  1153. }
  1154. }
  1155. }
  1156. }
  1157. }
  1158. }
  1159. }