1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144 |
- using Newtonsoft.Json;
- using PublicTools.XMLDataBase;
- using ShadowStudio.Tool;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- using UnityEngine;
- using XRTool.Util;
- namespace ShadowStudio.Model
- {
- /// <summary>
- /// 资源映射表
- /// 读取,获取资源映射表
- /// 按照id和类型建立资源映射。通过artid获取对应的artinfo
- /// 通过资源类型获取对应的artinfo
- /// 创建一个美术资源:
- /// </summary>
- public class ArtInfoMgr : Singleton<ArtInfoMgr>
- {
- private string tableName;
- private TableInterface table;
- private bool isInit = false;
- /// <summary>
- /// 所有的资源列表,此列表当前服务器和本地各有一份。后期读取服务器列表数据
- /// </summary>
- private List<ArtInfo> artInfoList;
- /// <summary>
- /// 资源id对应的资源信息
- /// </summary>
- private Dictionary<string, ArtInfo> artInfoMap;
- private Dictionary<string, ArtInfo> otherArtInfoMap;
- /// <summary>
- /// 资源类型对应的资源列表
- /// </summary>
- private Dictionary<ArtType, List<ArtInfo>> artInfoTypeMap;
- /// <summary>
- /// 组件的集合
- /// </summary>
- private Dictionary<string, ArtComponent> artComponentMap = new Dictionary<string, ArtComponent>();
- /// <summary>
- /// 资源的引用
- /// </summary>
- private Dictionary<string, ArtResConf> resArtMap;
- /// <summary>
- /// 资源对应的工具类
- /// </summary>
- private Dictionary<ArtInfo, ArtHandler> artHandlerMap = new Dictionary<ArtInfo, ArtHandler>();
- /// <summary>
- /// 对应资源类型的容器,例如视频播放器,图片浏览器,模型浏览器
- /// </summary>
- private Dictionary<ArtType, List<ArtContainer>> artContainerMap = new Dictionary<ArtType, List<ArtContainer>>();
- /// <summary>
- /// 数据初始化
- /// </summary>
- public event Action InitComplete;
- /// <summary>
- /// 对应资源存储的最大的id
- /// 本地生成goods的一种辅助方案,此方案即将移除
- /// 此方案已移除,请勿使用2020-09-14
- /// </summary>
- private Dictionary<ArtInfo, int> goodsIdMap = new Dictionary<ArtInfo, int>();
- /// <summary>
- /// 使用变量缓存服务器下发的物体信息数据
- /// </summary>
- private GoodsInfo serverCacheData = new GoodsInfo();
- /// <summary>
- /// 此为上传至服务器的物体的数据缓存,理论上上传多个物体列表信息,实际处理中每次只会上传一条物体的同步信息
- /// </summary>
- private List<GoodsInfo> goodsDataList = new List<GoodsInfo>();
- /// <summary>
- /// 创建容器的事件,某个容器被上传的事件
- /// </summary>
- public event Action<ArtContainer> ContainerCreate;
- /// <summary>
- /// 删除容器,某个容器被删除的事件
- /// </summary>
- public event Action<ArtContainer> ContainerDel;
- /// <summary>
- /// 容器被选中或者容器非选中状态的事件
- /// </summary>
- public static event Action<ArtContainer, bool> ContainerSelect;
- /// <summary>
- /// 编辑菜单的名称
- /// </summary>
- private string ModelEditBtnGroup = "ModelEditBtnDlg";
- //public ArtEditorContainer ModelEditBtn;
- /// <summary>
- /// 容器缓存
- /// 非即时同步容器缓存
- /// </summary>
- private Dictionary<string, ArtContainer> containerCache = new Dictionary<string, ArtContainer>();
- public event Action<string, bool> UserChange;
- public void AddCacheContainer(string id, ArtContainer container)
- {
- if (!ContainerCache.ContainsKey(id))
- {
- ContainerCache.Add(id, container);
- }
- }
- public void SetContainerSelect(ArtContainer container, bool isSelect)
- {
- ContainerSelect?.Invoke(container, isSelect);
- }
- public bool IsSingleMan()
- {
- //if (GameNode.Instance)
- //{
- // return GameNode.Instance.UserMap == null || GameNode.Instance.UserMap.Count < 2;
- //}
- return true;
- }
- public void RemoveArtInfo(List<UnityEngine.Object> resModelList)
- {
- for (int i = 0; i < resModelList.Count; i++)
- {
- string artid = "ArtId_" + resModelList[i].name;
- table.DeleteData<ArtInfo>(artid);
- }
- }
- /// <summary>
- /// 将数据添加至xml
- /// </summary>
- /// <param name="resModelList"></param>
- /// <param name="artType"></param>
- /// <param name="references"></param>
- public void AddArtInfo(List<UnityEngine.Object> resModelList, ArtType artType, InstaceType references, bool isForceUpdate, string url = "")
- {
- if (ArtInfoList == null)
- {
- ArtInfoList = new List<ArtInfo>();
- }
- for (int i = 0; i < resModelList.Count; i++)
- {
- UnityEngine.Object obj = resModelList[i];
- string artid = "ArtId_" + obj.name;
- if (isForceUpdate || !ArtInfoContains(artid))
- {
- ArtInfo info = new ArtInfo();
- info.ArtId = artid;
- info.ArtName = obj.name;
- info.ArtType = artType;
- info.Distance = 1f;
- ///Icon
- info.Icon = "ArtIcon/" + artType + "/" + obj.name;
- info.InstaceType = references;
- info.Size = 1;
- if (artType == ArtType.Prefab)
- {
- info.Distance = (obj as GameObject).transform.localPosition.z;
- info.Size = (obj as GameObject).transform.localScale.x;
- }
- if (references == InstaceType.References)
- {
- info.Url = url;
- }
- else if (references == InstaceType.ResourceLoad)
- {
- #if UNITY_EDITOR
- string path = AssetDatabase.GetAssetPath(obj);
- int index = path.LastIndexOf("Resources");
- path = path.Substring((index += "Resources".Length + 1), path.Length - index - 1);
- path = path.Replace(Path.GetExtension(path), "");
- info.Url = path;
- #endif
- }
- info.Version = Application.version;
- table.InsertData(info);
- if (!ArtInfoContains(artid))
- {
- ArtInfoList.Add(info);
- }
- }
- }
- table.Save();
- }
- public bool ArtInfoContains(string artId)
- {
- for (int i = 0; i < ArtInfoList.Count; i++)
- {
- if (ArtInfoList[i].ArtId == artId)
- {
- return true;
- }
- }
- return false;
- }
- public bool IsInit { get => isInit; set => isInit = value; }
- public List<ArtInfo> ArtInfoList { get => artInfoList; set => artInfoList = value; }
- public Dictionary<string, ArtContainer> ContainerCache { get => containerCache; set => containerCache = value; }
- public Dictionary<string, ArtInfo> OtherArtInfoMap
- {
- get
- {
- if (otherArtInfoMap == null)
- {
- otherArtInfoMap = new Dictionary<string, ArtInfo>();
- }
- return otherArtInfoMap;
- }
- }
- /// <summary>
- /// 房间内物体的映射
- /// 房间id与对应的物体资源映射
- /// </summary>
- public void LoadModelEditBtn()
- {
- //if (!ModelEditBtn)
- //{
- // var tmp = Resources.Load<ArtEditorContainer>(ModelEditBtnGroup);
- // if (tmp)
- // {
- // ModelEditBtn = GameObject.Instantiate(tmp);
- // GameNode.Instance.SetParent(ObjNode.WorldCanvas, ModelEditBtn.transform, Vector3.zero, Vector3.zero, Vector3.one * 0.001f, false);
- // ModelEditBtn.gameObject.SetActive(false);
- // }
- //}
- }
- public ArtInfoMgr()
- {
- var artResList = Resources.LoadAll<ArtResConf>("ArtConf");
- resArtMap = new Dictionary<string, ArtResConf>();
- if (artResList != null)
- {
- for (int i = 0; i < artResList.Length; i++)
- {
- resArtMap.Add(artResList[i].name, artResList[i]);
- }
- }
- var containers = Resources.LoadAll<GameObject>("Container");
- if (containers != null)
- {
- for (int i = 0; i < containers.Length; i++)
- {
- var container = containers[i].GetComponent<ArtContainer>();
- if (container != null)
- {
- if (!artContainerMap.ContainsKey(container.GetArtType()))
- {
- List<ArtContainer> list = new List<ArtContainer>();
- list.Add(container);
- artContainerMap.Add(container.GetArtType(), list);
- }
- else
- {
- List<ArtContainer> list = artContainerMap[container.GetArtType()];
- list.Add(container);
- }
- }
- }
- }
- var components = Resources.LoadAll<GameObject>("Container/Component");
- if (components != null)
- {
- for (int i = 0; i < components.Length; i++)
- {
- var component = containers[i].GetComponent<ArtComponent>();
- if (component != null)
- {
- if (!artComponentMap.ContainsKey(containers[i].name))
- {
- artComponentMap.Add(containers[i].name, component);
- }
- }
- }
- }
- if (BuildConfigMgr.Instance.IsInit)
- {
- //ReadArtInfo(Path.Combine(BuildConfig.Instance.LocalPath, BuildConfig.Instance.artInfoPath));
- }
- else
- {
- UnityLog.LogError("配置文件丢失!");
- }
- //WSHandler.Room.OnBroadcastGood += SyncGoods;
- ContainerSelect += OnContainerSelect;
- }
- public void ListenUser()
- {
- //if (ShowViewMgr.Instance)
- //{
- // ShowViewMgr.Instance.OnUserChange -= OnUserChange;
- // ShowViewMgr.Instance.OnUserChange += OnUserChange;
- //}
- //else
- //{
- // ShowViewMgr.InitComplte += () =>
- // {
- // if (ShowViewMgr.Instance)
- // {
- // ShowViewMgr.Instance.OnUserChange -= OnUserChange;
- // ShowViewMgr.Instance.OnUserChange += OnUserChange;
- // }
- // };
- //}
- }
- public void ListenPage()
- {
- //if (ShowViewMgr.Instance)
- //{
- // ShowViewMgr.Instance.OnRoomPageChange -= OnChangePage;
- // ShowViewMgr.Instance.OnRoomPageChange += OnChangePage;
- //}
- //else
- //{
- // ShowViewMgr.InitComplte += () =>
- // {
- // if (ShowViewMgr.Instance)
- // {
- // ShowViewMgr.Instance.OnRoomPageChange -= OnChangePage;
- // ShowViewMgr.Instance.OnRoomPageChange += OnChangePage;
- // }
- // };
- //}
- }
- private void OnContainerSelect(ArtContainer container, bool isSelect)
- {
- if (container.GetGoodsInfo().art_id == "ArtId_PlayerView")
- {
- return;
- }
- //if (!ModelEditBtn)
- //{
- // LoadModelEditBtn();
- //}
- //if (ModelEditBtn)
- //{
- // if (ModelEditBtn.gameObject.activeSelf != isSelect)
- // {
- // ModelEditBtn.gameObject.SetActive(isSelect);
- // }
- // if (isSelect)
- // {
- // ModelEditBtn.SetTarget(container, false);
- // }
- //}
- //if (ArtMoreEditor.Instance)
- //{
- // if (isSelect)
- // {
- // ArtMoreEditor.Instance.SetTarget(container, false);
- // }
- //}
- }
- /// <summary>
- /// 读取美术资源
- /// </summary>
- /// <param name="path"></param>
- private void ReadArtInfo(string path)
- {
- //languageIndex = SystemSettingMgr.Instance.settings.Language;
- tableName = typeof(ArtInfo).Name;
- #if UNITY_EDITOR || !UNITY_ANDROID
- table = XSql.Instance.OpenTable(path, tableName, ".xml", true);
- OpenTable();
- #else
- string fullPath = Path.Combine(path, tableName + ".xml");
- TimerMgr.Instance.StartCoroutine(XSql.Instance.ReadServerData(fullPath, (List<ArtInfo> packList) =>
- {
- InitPackage(packList);
- }));
- #endif
- }
- public void OpenLocalData()
- {
- //ReadArtInfo(Path.Combine(BuildConfig.Instance.LocalPath, BuildConfig.Instance.artInfoPath));
- }
- private void OpenTable()
- {
- if (!table.Open())
- {
- table.Create(tableName);
- UnityLog.Log("create table" + tableName);
- }
- var list = table.FindAllData<ArtInfo>();
- for (int i = 0; i < list.Count; i++)
- {
- list[i].ArtType = ArtInfo.GetArtType((int)list[i].ArtType);
- }
- //table.Save();
- //languagePackList = table.FindAllData<LanguagePackConf>();
- InitPackage(list);
- }
- /// <summary>
- /// 初始化包
- /// </summary>
- /// <param name="lists"></param>
- public void InitPackage(List<ArtInfo> lists)
- {
- ArtInfoList = lists;
- if (ArtInfoList == null || ArtInfoList.Count < 1)
- {
- UnityLog.LogError("美术资源不存在!");
- #if UNITY_EDITOR
- AddDefaultArt(ArtType.Model);
- InitPackage(ArtInfoList);
- #endif
- return;
- }
- else
- {
- if (artInfoMap == null)
- {
- artInfoMap = new Dictionary<string, ArtInfo>();
- }
- if (artInfoTypeMap == null)
- {
- artInfoTypeMap = new Dictionary<ArtType, List<ArtInfo>>();
- }
- for (int i = 0; i < ArtInfoList.Count; i++)
- {
- if (!artInfoMap.ContainsKey(ArtInfoList[i].ArtId))
- {
- artInfoMap.Add(ArtInfoList[i].ArtId, ArtInfoList[i]);
- UnityLog.Log(ArtInfoList[i].Url, 3);
- }
- if (artInfoTypeMap.ContainsKey(ArtInfoList[i].ArtType))
- {
- List<ArtInfo> list = artInfoTypeMap[ArtInfoList[i].ArtType];
- if (list != null && !list.Contains(ArtInfoList[i]))
- {
- list.Add(ArtInfoList[i]);
- }
- }
- else
- {
- List<ArtInfo> list = new List<ArtInfo>();
- list.Add(ArtInfoList[i]);
- artInfoTypeMap.Add(ArtInfoList[i].ArtType, list);
- }
- }
- }
- IsInit = true;
- InitComplete?.Invoke();
- }
- /// <summary>
- /// 添加Unity基本资源到文本
- /// </summary>
- public void AddDefaultArt(ArtType artType)
- {
- if (ArtInfoList == null)
- {
- ArtInfoList = new List<ArtInfo>();
- }
- if (artType == ArtType.Model)
- {
- foreach (PrimitiveType primitive in Enum.GetValues(typeof(PrimitiveType)))
- {
- ArtInfo info = new ArtInfo();
- info.ArtId = "ArtId_" + (int)primitive;
- info.ArtName = primitive.ToString();
- info.ArtType = ArtType.Model;
- info.Distance = 1f;
- ///Icon
- info.Icon = "PrimitiveType/" + primitive.ToString();
- info.InstaceType = InstaceType.UnityBase;
- info.Size = 1;
- info.Url = "" + (int)primitive;
- info.Version = Application.unityVersion;
- table.InsertData(info);
- ArtInfoList.Add(info);
- }
- }
- else
- {
- ArtInfo info = new ArtInfo();
- info.ArtId = "ArtId_GameObject";
- info.ArtName = "GameObject";
- info.ArtType = ArtType.Prefab;
- info.Distance = 1f;
- ///Icon
- info.Icon = "";
- info.InstaceType = InstaceType.UnityBase;
- info.Size = 1;
- info.Url = "New";
- info.Version = Application.unityVersion;
- table.InsertData(info);
- ArtInfoList.Add(info);
- }
- table.Save();
- }
- /// <summary>
- /// 根据资源id获取对应的资源
- /// </summary>
- /// <param name="artId"></param>
- /// <returns></returns>
- public ArtInfo GetArtInfo(string artId)
- {
- if (IsInit && artInfoMap.ContainsKey(artId))
- {
- return artInfoMap[artId];
- }
- return null;
- }
- /// <summary>
- /// 根据资源id获取对应的资源
- /// </summary>
- /// <param name="artId"></param>
- /// <returns></returns>
- public List<ArtInfo> GetArtInfoList(ArtType artType)
- {
- if (IsInit)//
- {
- if (artInfoTypeMap.ContainsKey(artType))
- {
- return artInfoTypeMap[artType];
- }
- List<ArtInfo> tmp = new List<ArtInfo>();
- foreach (var item in artInfoTypeMap)
- {
- ///包含此key
- if ((item.Key & artType) == item.Key)
- {
- tmp.AddRange(item.Value);
- }
- }
- artInfoTypeMap.Add(artType, tmp);
- return tmp;
- }
- return null;
- }
- ///// <summary>
- ///// 根据资源id获取对应的资源
- ///// </summary>
- ///// <param name="artId"></param>
- ///// <returns></returns>
- //public List<ArtInfo> GetArtInfoList(ArtType[] artType)
- //{
- // if (IsInit)//
- // {
- // List<ArtInfo> ArtInfoList = new List<ArtInfo>();
- // for (int i = 0; i < artType.Length; i++)
- // {
- // if (artInfoTypeMap.ContainsKey(artType[i]))
- // {
- // ArtInfoList.AddRange(artInfoTypeMap[artType[i]]);
- // }
- // }
- // return ArtInfoList;
- // }
- // return null;
- //}
- /// <summary>
- /// 根据资源id获取对应的资源
- /// </summary>
- /// <param name="artId"></param>
- /// <returns></returns>
- public List<ArtInfo> GetArtInfoListSingle(ArtType artType)
- {
- if (IsInit)//
- {
- return artInfoTypeMap[artType];
- }
- return null;
- }
- /// <summary>
- /// 创建一个资源ArtHandler,注意内存回收
- /// </summary>
- /// <returns></returns>
- public ArtHandler CreateArtHandler(ArtInfo art, string containerName = "")
- {
- if (string.IsNullOrEmpty(containerName))
- {
- containerName = art.ContainerName;
- }
- if (artHandlerMap.ContainsKey(art))
- {
- return artHandlerMap[art];
- }
- ArtHandler handler = new ArtHandler();
- handler.SetData(art, containerName);
- artHandlerMap.Add(art, handler);
- return handler;
- }
- /// <summary>
- /// 删除资源帮助类ArtHandler
- /// </summary>
- /// <param name="art"></param>
- public void DestrorArtHandler(ArtInfo art)
- {
- if (artHandlerMap.ContainsKey(art))
- {
- var handler = artHandlerMap[art];
- handler = null;
- artHandlerMap.Remove(art);
- }
- }
- /// <summary>
- /// 实例化组件
- /// </summary>
- /// <param name="componentName"></param>
- /// <returns></returns>
- public ArtComponent InstanceComponent(string componentName)
- {
- if (artComponentMap != null && artComponentMap.ContainsKey(componentName))
- {
- ArtComponent component = artComponentMap[componentName];
- var tmp = GameObject.Instantiate(component.GetInstace());
- return tmp.GetComponent<ArtComponent>();
- }
- return null;
- }
- /// <summary>
- /// 实例化出一个容器
- /// </summary>
- /// <param name="artType"></param>
- /// <returns></returns>
- public ArtContainer CreateContainer(ArtType artType, string containerName = "")
- {
- if (artContainerMap != null && artContainerMap.ContainsKey(artType))
- {
- var containerList = artContainerMap[artType];
- if (containerList != null)
- {
- ArtContainer container = null;
- if (containerList.Count == 1)
- {
- container = containerList[0];
- }
- else
- {
- for (int i = 0; i < containerList.Count; i++)
- {
- if (containerList[i].GetInstace().name == containerName)
- {
- container = containerList[i];
- break;
- }
- }
- }
- if (container != null)
- {
- var tmp = GameObject.Instantiate(container.GetInstace());
- return tmp.GetComponent<ArtContainer>();
- }
- }
- }
- return null;
- }
- /// <summary>
- /// 获取对应的配置文件
- /// </summary>
- /// <param name="resName"></param>
- /// <returns></returns>
- public ArtResConf GetResConf(string resName)
- {
- if (resArtMap.ContainsKey(resName))
- {
- return resArtMap[resName];
- }
- UnityLog.LogError(resName + " is not at resArtMap");
- return null;
- }
- /// <summary>
- /// 物品的自增id设计
- /// </summary>
- /// <param name="info"></param>
- public int GetGoodsId(ArtInfo info)
- {
- int id = 1;
- if (goodsIdMap.ContainsKey(info))
- {
- id = goodsIdMap[info] + 1;
- goodsIdMap[info] = id;
- }
- else
- {
- goodsIdMap.Add(info, id);
- }
- return id;
- }
- /// <summary>
- /// 同步物体:
- /// 创建物体时,接收到广播消息时的数据同步都调用此方法
- /// </summary>
- /// <param name="rid">用户id,物体的创建者</param>
- /// <param name="art_id">美术资源id</param>
- /// <param name="goods_name">物体名称</param>
- /// <param name="goods_extended">物体额外信息</param>
- /// <param name="id">物体id</param>
- /// <param name="goods_info">物体的姿态信息</param>
- public void SyncGoods(int rid, int status, string art_id, string goods_name, string goods_extended, int id, string goods_info)
- {
- serverCacheData.rid = rid;
- serverCacheData.art_id = art_id;
- serverCacheData.goods_name = goods_name;
- serverCacheData.goods_extended = goods_extended;
- serverCacheData.id = id;
- serverCacheData.goods_info = goods_info;
- serverCacheData.status = status;
- SyncGoods(serverCacheData);
- }
- public void SyncGoods(List<GoodsInfo> goodDataList)
- {
- if (goodDataList != null)
- {
- for (int i = 0; i < goodDataList.Count; i++)
- {
- SyncGoods(goodDataList[i]);
- }
- }
- }
- /// <summary>
- /// 同步物体数据
- /// 服务器同步或者翻页时同步,服务器同步时开始缓存数据
- /// 翻页时同步不添加缓存数据
- /// </summary>
- /// <param name="goodData"></param>
- /// <param name="isServer"></param>
- public void SyncGoods(GoodsInfo goodData, bool isServer = true)
- {
- //if (GameNode.Instance.GoodsContainerMap == null)
- //{
- // GameNode.Instance.GoodsContainerMap = new Dictionary<int, ArtContainer>();
- //}
- //ArtContainer container = null;
- //bool isCreate = false;
- //if (SynUser(goodData))
- //{
- // container = GameNode.Instance.UserMap[goodData.art_id].Container;
- //}
- //else if (goodData.id != 0)
- //{
- // ///先检查此数据是否在此页面
- // ///如果数据不在当前页,则只缓存起来
- // ///如果在当前页,则缓存后实例化进行同步
- // ///如果不是服务器的数据,仅同步设置,不缓存数据
- // if (isServer && goodData.scene_id > 0)
- // {
- // if (!GameNode.Instance.PageGoods.ContainsKey(goodData.id))
- // {
- // GameNode.Instance.PageGoods.Add(goodData.id, goodData);
- // }
- // else
- // {
- // GameNode.Instance.PageGoods[goodData.id] = goodData;
- // }
- // if (!GameNode.Instance.PageGoodsId.ContainsKey(goodData.scene_id))
- // {
- // var list = new List<int>();
- // list.Add(goodData.id);
- // GameNode.Instance.PageGoodsId.Add(goodData.scene_id, list);
- // }
- // else
- // {
- // var list = GameNode.Instance.PageGoodsId[goodData.scene_id];
- // if (list == null)
- // {
- // list = new List<int>();
- // }
- // if (!list.Contains(goodData.id))
- // {
- // list.Add(goodData.id);
- // }
- // }
- // if (goodData.scene_id != CommonMethod.currentScene)
- // {
- // return;
- // }
- // }
- // ///对象已存在,进行同步
- // if (GameNode.Instance.GoodsContainerMap.ContainsKey(goodData.id))
- // {
- // container = GameNode.Instance.GoodsContainerMap[goodData.id];
- // }
- // else
- // {
- // ArtInfo info = GetArtInfo(goodData.art_id);
- // if (info == null)
- // {
- // if (OtherArtInfoMap.ContainsKey(goodData.art_id))
- // {
- // info = OtherArtInfoMap[goodData.art_id];
- // }
- // else
- // {
- // NetWorkHeaders.RequestArtInfo(goodData.art_id, (jsonData) =>
- // {
- // if (!OtherArtInfoMap.ContainsKey(goodData.art_id))
- // {
- // info = JsonConvert.DeserializeObject<ArtInfo>(jsonData["data"].ToJson());
- // if (info != null)
- // {
- // info.ArtType = CommonMethod.getArtType(int.Parse(jsonData["data"]["art_type"].ToString()));
- // info.ImmediateSyn = jsonData["data"]["immediate_syn"].ToString() == "0" ? false : true;
- // if (string.IsNullOrEmpty(info.Component))
- // {
- // if (info.ArtType == ArtType.Image)
- // {
- // info.Component = "PictureComponent";
- // }
- // else if (info.ArtType == ArtType.Movies)
- // {
- // info.Component = "VideoComponent";
- // }
- // }
- // OtherArtInfoMap.Add(goodData.art_id, info);
- // SyncGoods(goodData, isServer);
- // UnityLog.Instance.LogError("find artinfo:" + goodData.art_id);
- // return;
- // }
- // else
- // {
- // UnityLog.Instance.LogError("Error find artinfo:" + goodData.art_id);
- // }
- // }
- // });
- // }
- // }
- // if (info != null)
- // {
- // ///容器缓存
- // if (ContainerCache.ContainsKey(goodData.art_id))
- // {
- // container = ContainerCache[goodData.art_id];
- // ContainerCache.Remove(goodData.art_id);
- // }
- // else
- // {
- // ArtHandler handler = CreateArtHandler(info);
- // container = handler.CreateArt();
- // }
- // GameNode.Instance.GoodsContainerMap.Add(goodData.id, container);
- // isCreate = true;
- // }
- // else
- // {
- // UnityLog.Instance.LogError(goodData.art_id);
- // }
- // }
- //}
- //try
- //{
- // if (container != null && container.GetInstace())
- // {
- // container.TransferSyn(goodData, goodData.status == (int)TransferState.Doing);
- // if (isCreate)
- // {
- // ContainerCreate?.Invoke(container);
- // }
- // }
- //}
- //catch
- //{
- // ///某个物体可能被删掉了
- // ///此种情况不在处理
- //}
- }
- //Dictionary<string, string> form;
- //Dictionary<string, string> header;
- ///// <summary>
- ///// 读取资源信息
- ///// </summary>
- ///// <returns></returns>
- //public IEnumerator ReqOtherArtInfo(string artId, Action<ArtInfo> onReadComplete)
- //{
- // if (header == null)
- // {
- // header = new Dictionary<string, string>();
- // header.Add("Content-Type", "application/x-www-form-urlencoded");
- // }
- // if (form == null)
- // {
- // form = new Dictionary<string, string>();
- // form.Add("action", "");
- // }
- // yield return 0;
- //}
- /// <summary>
- /// 同步用户
- /// </summary>
- /// <param name="goodData"></param>
- /// <returns></returns>
- private bool SynUser(GoodsInfo goodData)
- {
- //if (GameNode.Instance.UserMap == null)
- //{
- // GameNode.Instance.UserMap = new Dictionary<string, PlayerContainer>();
- //}
- //if (string.IsNullOrEmpty(goodData.art_id))
- //{
- // return false;
- //}
- //return GameNode.Instance.UserMap.ContainsKey(goodData.art_id);
- return false;
- }
- /// <summary>
- /// 删除物体
- /// 可能由用户主动删除,主动删除传参true.删除相关的数据缓存
- /// 翻页时是被动删除。被动删除不移除相关的数据缓存
- /// </summary>
- /// <param name="id">物体的id</param>
- /// <param name="isRealDel">是否真实删除,默认真实删除</param>
- public void GoodsDelSync(int id, bool isRealDel = true)
- {
- //if (GameNode.Instance.GoodsContainerMap == null)
- //{
- // GameNode.Instance.GoodsContainerMap = new Dictionary<int, ArtContainer>();
- //}
- //ArtContainer container = null;
- /////对象已存在,进行同步
- //if (GameNode.Instance.GoodsContainerMap.ContainsKey(id))
- //{
- // container = GameNode.Instance.GoodsContainerMap[id];
- // //if (ModelEditBtn)
- // //{
- // // ModelEditBtn.SetTarget(container, true);
- // //}
- // if (ArtMoreEditor.Instance)
- // {
- // ArtMoreEditor.Instance.SetTarget(container, true);
- // }
- // ContainerDel?.Invoke(container);
- // if (container != null)
- // {
- // container.DestroyArt();
- // }
- // GameNode.Instance.GoodsContainerMap.Remove(id);
- //}
- /////真实删除
- //if (isRealDel)
- //{
- // ///删除物体的id
- // ///先查找物体id对应的Goods
- // if (GameNode.Instance.PageGoods.ContainsKey(id))
- // {
- // ///获得Goods
- // var good = GameNode.Instance.PageGoods[id];
- // ///查找页对应的物体的id
- // if (GameNode.Instance.PageGoodsId.ContainsKey(good.scene_id))
- // {
- // ///获取页对应的id列表,并
- // var list = GameNode.Instance.PageGoodsId[good.scene_id];
- // if (list != null && list.Contains(id))
- // {
- // list.Remove(id);
- // }
- // }
- // GameNode.Instance.PageGoods.Remove(id);
- // ContainerDel?.Invoke(null);
- // }
- //}
- }
- public void SendGoodsTransfer(GoodsInfo info)
- {
- //goodsDataList.Add(info);
- ////WSHandler.Room.BroadcastGood(goodsDataList);
- //goodsDataList.Clear();
- }
- public void SendTransfer(GoodsInfo info)
- {
- //goodsDataList.Add(info);
- ////WSHandler.Room.TransmitGood(goodsDataList);
- //goodsDataList.Clear();
- }
- /// <summary>
- /// 当新用户进入时的事件
- /// </summary>
- /// <param name="userList"></param>
- //public void OnUserChange(List<Peer> userList, bool isJoin)
- //{
- // if (GameNode.Instance.UserMap == null)
- // {
- // GameNode.Instance.UserMap = new Dictionary<string, PlayerContainer>();
- // }
- // if (userList != null)
- // {
- // for (int i = 0; i < userList.Count; i++)
- // {
- // if (GameNode.Instance.UserMap.ContainsKey(userList[i].PeerId))
- // {
- // if (isJoin)
- // {
- // continue;
- // }
- // else
- // {
- // var container = GameNode.Instance.UserMap[userList[i].PeerId];
- // container.StopSyn();
- // container.DestroyArt();
- // UserChange?.Invoke(userList[i].PeerId, false);
- // GameNode.Instance.UserMap.Remove(userList[i].PeerId);
- // }
- // }
- // else
- // {
- // if (isJoin)
- // {
- // ArtInfo info = GetArtInfo(PlayerContainer.userArtId);
- // if (info != null)
- // {
- // //string cName = info.ContainerName;
- // //if (string.IsNullOrEmpty(cName))
- // //{
- // // cName = PlayerContainer.containerName;
- // //}
- // //ArtHandler handler = CreateArtHandler(info, PlayerContainer.containerName);
- // ArtHandler handler = CreateArtHandler(info);
- // var container = handler.CreateArt();
- // if (container != null)
- // {
- // if (container.ArtComponent != null && container.ArtComponent.GetInstace())
- // {
- // if (container.ArtComponent is PlayerContainer)
- // {
- // (container.ArtComponent as PlayerContainer).SetPeer(userList[i]);
- // GameNode.Instance.UserMap.Add(userList[i].PeerId, container.ArtComponent as PlayerContainer);
- // UserChange?.Invoke(userList[i].PeerId, true);
- // }
- // }
- // }
- // }
- // else
- // {
- // UnityLog.Instance.LogError(PlayerContainer.userArtId + " cant find art");
- // }
- // }
- // else
- // {
- // continue;
- // }
- // }
- // }
- // }
- //}
- /// <summary>
- /// 删除场景的物体
- /// </summary>
- /// <param name="id"></param>
- /// <param name="isOnlyScene"></param>
- public void DelGoodsById(int id)
- {
- //bool isCanDel = true;
- //if (GameNode.Instance.PageGoods.ContainsKey(id))
- //{
- // var goods = GameNode.Instance.PageGoods[id];
- // if (goods.scene_id == CommonMethod.currentScene)
- // {
- // isCanDel = true;
- // }
- //}
- //if (isCanDel)
- //{
- // WSHandler.Room.DeleteGood(new int[] { id });
- //}
- }
- public void DelGoodsByIds(List<int> ids)
- {
- //List<int> list = new List<int>();
- //for (int i = 0; i < ids.Count; i++)
- //{
- // int id = ids[i];
- // if (GameNode.Instance.PageGoods.ContainsKey(id))
- // {
- // var goods = GameNode.Instance.PageGoods[id];
- // if (goods.scene_id == CommonMethod.currentScene)
- // {
- // list.Add(id);
- // }
- // }
- //}
- //if (list.Count > 0)
- //{
- // WSHandler.Room.DeleteGood(list.ToArray());
- //}
- }
- /// <summary>
- /// 翻页的数据处理
- /// 翻页时注意,删除旧的数据
- /// 同步新的数据
- /// </summary>
- /// <param name="all"></param>
- /// <param name="current"></param>
- public void OnChangePage(int all, int current)
- {
- //if (GameNode.Instance)
- //{
- // if (GameNode.Instance.DefaultPageIndex != current)
- // {
- // if (GameNode.Instance.DefaultPageIndex >= 0)
- // {
- // if (GameNode.Instance.PageGoodsId.ContainsKey(GameNode.Instance.DefaultPageIndex))
- // {
- // var list = GameNode.Instance.PageGoodsId[GameNode.Instance.DefaultPageIndex];
- // for (int i = 0; i < list.Count; i++)
- // {
- // GoodsDelSync(list[i], false);
- // }
- // }
- // }
- // if (ModelEditBtn)
- // {
- // ModelEditBtn.gameObject.SetActive(false);
- // }
- // GameNode.Instance.DefaultPageIndex = current;
- // if (GameNode.Instance.DefaultPageIndex >= 0)
- // {
- // if (GameNode.Instance.PageGoodsId.ContainsKey(GameNode.Instance.DefaultPageIndex))
- // {
- // var list = GameNode.Instance.PageGoodsId[GameNode.Instance.DefaultPageIndex];
- // for (int i = 0; i < list.Count; i++)
- // {
- // if (GameNode.Instance.PageGoods.ContainsKey(list[i]))
- // {
- // SyncGoods(GameNode.Instance.PageGoods[list[i]], false);
- // }
- // }
- // }
- // }
- // }
- //}
- }
- }
- }
|