Exemplo n.º 1
0
 private void OnResLoad(Resource res)
 {
     if (res.isSucc)
     {
         Queue <GameObject> queueGO = null;
         if (!m_dicGO.TryGetValue(res, out queueGO))
         {
             queueGO = new Queue <GameObject>();
             m_dicGO.Add(res, queueGO);
             res.Retain();
         }
         List <GameObjectPoolHandler> lstCallback = null;
         if (m_dicCallback.TryGetValue(res.path, out lstCallback))
         {
             while (lstCallback.Count > 0)
             {
                 GameObject go = null;
                 if (queueGO.Count > 0)
                 {
                     go = queueGO.Dequeue();
                 }
                 else
                 {
                     go = GetGameObject(res);
                 }
                 var callback = lstCallback[0];
                 lstCallback.RemoveAt(0);
                 go.SetActive(true);
                 callback.Invoke(go);
             }
         }
     }
     else
     {
         CLog.LogError("加载unit资源" + res.path + "失败");
     }
 }
Exemplo n.º 2
0
 public static string Serialize <T>(T obj)
 {
     if (obj == null)
     {
         CLog.LogError("Serialize obj can not null!");
         return(null);
     }
     try
     {
         IFormatter   formatter = new BinaryFormatter();
         MemoryStream ms        = new MemoryStream();
         formatter.Serialize(ms, obj);
         ms.Position = 0;
         byte[] buffer = new byte[ms.Length];
         ms.Read(buffer, 0, buffer.Length);
         ms.Flush();
         ms.Close();
         return(Convert.ToBase64String(buffer));
     }
     catch (Exception ex)
     {
         throw new Exception("Serialize fail,reason:" + ex.Message);
     }
 }
Exemplo n.º 3
0
        public void Init(string assetBundleEntryXmlContent)
        {
            try
            {
                XmlDocument document = new XmlDocument();
                document.LoadXml(assetBundleEntryXmlContent);

                foreach (XmlElement element in document.FirstChild.NextSibling.ChildNodes)
                {
                    if (element.Name == "AssetMappings")
                    {
                        mainAssetBundlePath = element.GetAttribute("manifest");
                        foreach (XmlElement mapping in element.ChildNodes)
                        {
                            assetPathToAssetBundleNames.Add(mapping.GetAttribute("assetName"), mapping.GetAttribute("bundleName"));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                CLog.LogError(e.Message + "," + e.StackTrace);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 派发事件,当参数为一个或没有时调用
        /// </summary>
        /// <param name="eventId"></param>
        /// <param name="args"></param>
        public void Dispatch(int key, object args = null)
        {
            if (_listeners == null || !_listeners.ContainsKey(key))
            {
                return;
            }
            if (_isDispatching)
            {
                DynamicDispatcherEntry dispatchEntry = _poolDispatch.GetObject();
                dispatchEntry.Key  = key;
                dispatchEntry.Args = args;
                _dispatchPendings.Enqueue(dispatchEntry);
                return;
            }

            _isDispatching = true;

            List <List <DynamicDispatcherObjEntry> > allEntries = _listeners[key];
            int priorityCount = allEntries.Count;
            int count;
            List <DynamicDispatcherObjEntry> entries;

            for (int i = priorityCount - 1; i > -1; --i)
            {
                entries = allEntries[i];
                count   = entries.Count;
                //先添加的事件需要先执行
                _lstRemoveId.Clear();
                for (int j = 0; j < count; ++j)
                {
                    IDynamicDispatcherObj dispatcherObj = entries[j].DispatcherObj;
                    try
                    {
                        object p = args;
                        if (args != null && args is DynamicDispatcherParam)
                        {
                            p = ((DynamicDispatcherParam)args).Arg;
                        }
                        if (!dispatcherObj.OnDispatcher(p))
                        {
                            //移除
                            _lstRemoveId.Add(j);
                        }
                    }
                    catch (Exception e)
                    {
                        CLog.LogError(e.ToString());
                    }
                }
                //移除
                for (int j = _lstRemoveId.Count - 1; j > -1; j--)
                {
                    entries.RemoveAt(_lstRemoveId[j]);
                }
            }
            _isDispatching = false;
            if (_isClear)
            {
                Clear();
            }

            //deal with pendings
            count = _pendings.Count;
            for (int i = 0; i < count; ++i)
            {
                if (_pendings[i].Status == DynamicDispatcherObjEntry.EntryStatus.TOINSERT)
                {
                    _pendings[i].Status = DynamicDispatcherObjEntry.EntryStatus.ACTIVE;
                    allEntries          = _listeners[_pendings[i].Key];
                    entries             = allEntries[(int)(_pendings[i].Priority)];
                    entries.Add(_pendings[i]);
                }
                else if (_pendings[i].Status == DynamicDispatcherObjEntry.EntryStatus.TODELETE)
                {
                    SafeRemoveDispatcherObj(_pendings[i].Key, _pendings[i].DispatcherObj);
                    _pool.SaveObject(_pendings[i]);
                }
            }
            _pendings.Clear();

            if (_dispatchPendings.Count > 0)
            {
                DynamicDispatcherEntry dispatchEntry = _dispatchPendings.Dequeue();
                object p = dispatchEntry.Args;
                if (dispatchEntry.Args != null && dispatchEntry.Args is DynamicDispatcherParam)
                {
                    p = ((DynamicDispatcherParam)dispatchEntry.Args).Arg;
                }
                Dispatch(dispatchEntry.Key, p);
                _poolDispatch.SaveObject(dispatchEntry);
            }
        }
Exemplo n.º 5
0
 //系统断开连接
 private void OnDisConnect()
 {
     //网络自动断开,可以处理断线重连
     CLog.Log("系统断开连接", CLogColor.Red);
 }
Exemplo n.º 6
0
 private void Run()
 {
     while (!m_bStop)
     {
         try
         {
             int opcodeCount = m_cSocket.Receive(m_cBuffer, 0, 2, SocketFlags.None);
             if (CheckReceiveZero(opcodeCount))
             {
                 break;
             }
             short       opcode  = BitConverter.ToInt16(m_cBuffer, 0);
             NetRecvData netData = new NetRecvData();
             //如果是帧包
             if (opcode < 256)
             {
                 netData.recvOpcode = 0;
                 netData.len        = opcode;
                 int frameIndexCount = m_cSocket.Receive(m_cBuffer, 0, 4, SocketFlags.None);
                 if (CheckReceiveZero(frameIndexCount))
                 {
                     break;
                 }
                 netData.data = BitConverter.ToInt32(m_cBuffer, 0);
                 // CLog.Log(string.Format("收到帧包frameCount={0},frameIndex={1},buff={2}",netData.len,netData.data,BitConverter.ToString(m_cBuffer,0,4)));
             }
             else
             {
                 netData.recvOpcode = opcode;
                 //读取长度
                 int lenCount = m_cSocket.Receive(m_cBuffer, 0, 2, SocketFlags.None);
                 if (CheckReceiveZero(lenCount))
                 {
                     break;
                 }
                 netData.len = BitConverter.ToInt16(m_cBuffer, 0);
                 int dataLen = m_cSocket.Receive(m_cBuffer, 0, (int)netData.len, SocketFlags.None);
                 if (CheckReceiveZero(dataLen))
                 {
                     break;
                 }
                 //反序列化
                 try {
                     Type type = m_dicOpcodeToType[netData.recvOpcode];
                     if (ProtoBuf.Serializer.NonGeneric.CanSerialize(type))
                     {
                         m_cStream.Position = 0;
                         m_cStream.Write(m_cBuffer, 0, netData.len);
                         m_cStream.Position = 0;
                         netData.data       = ProtoBuf.Serializer.NonGeneric.Deserialize(type, m_cStream);
                     }
                 }
                 catch (Exception ex)
                 {
                     CLog.LogError("SocketReceiver Deserialize fail!opcode=" + netData.recvOpcode);
                     continue;
                 }
             }
             lock (m_queueData)
             {
                 m_queueData.Enqueue(netData);
             }
         }
         catch (Exception e)
         {
             //忽略WSACancelBlockingCall异常
             if (!e.Message.Contains("WSACancelBlockingCall"))
             {
                 CLog.LogError("SocketReceiver exception:" + e.Message + "\n" + e.StackTrace);
             }
             m_bLostConnect = true;
             break;
         }
     }
 }
Exemplo n.º 7
0
 private void AsyncCallback(IAsyncResult ar)
 {
     CLog.Log("Tcp AsyncCallback");
 }
Exemplo n.º 8
0
        public void LoadResCfgs(string resDir, Action OnFinish = null)
        {
            m_finishAction = OnFinish;
            m_dicCfgInfo   = new Dictionary <string, ResCfgInfo>();
            resDir         = resDir.Replace('\\', '/');
            if (!resDir.EndsWith("/"))
            {
                resDir += "/";
            }
            if (resDir.StartsWith("/"))
            {
                resDir = resDir.Substring(1);
            }
            Assembly assembly = Assembly.GetAssembly(typeof(ResCfgSys));
            var      types    = assembly.GetTypes();

            foreach (var type in types)
            {
                object[] arr = type.GetCustomAttributes(typeof(ResCfgAttribute), false);
                if (arr.Length == 0)
                {
                    continue;
                }
                var          properties   = type.GetProperties();
                PropertyInfo propertyInfo = null;
                for (int i = 0; i < properties.Length; i++)
                {
                    object[] arrMember = properties[i].GetCustomAttributes(typeof(ResCfgKeyAttribute), false);
                    if (arrMember.Length > 0)
                    {
                        propertyInfo = properties[i];
                        break;
                    }
                }
                if (propertyInfo == null)
                {
                    CLog.LogError("配置名称:" + type.Name + "找不到唯一key的定义ResCfgAttribute");
                    continue;
                }
                string path = resDir + type.Name + ".xml";
                //考虑下需要过滤不在此目录的文件
                ResCfgInfo cfgInfo = new ResCfgInfo();
                cfgInfo.xmlPath     = resDir + type.Name + ".xml";
                cfgInfo.type        = type;
                cfgInfo.keyProperty = propertyInfo;
                m_dicCfgInfo.Add(cfgInfo.xmlPath, cfgInfo);
                //CLog.LogColorArgs(CLogColor.Red,type.Name,propertyInfo != null ? propertyInfo.Name : "null");
            }
            List <string> names = new List <string>();

            foreach (var item in m_dicCfgInfo)
            {
                names.Add(item.Value.xmlPath);
            }
            //如果是播放模式
            if (Application.isPlaying)
            {
                //下载对应的资源
                m_resLoader = new MultiResourceLoader(ResourceSys.Instance);
                m_resLoader.LoadList(names, OnComplete, OnProgress, ResourceType.Text);
            }
            else
            {
                for (int i = 0; i < names.Count; i++)
                {
                    var textAsset = UnityEditor.AssetDatabase.LoadAssetAtPath <TextAsset>(names[i]);
                    if (textAsset != null)
                    {
                        ParseData(names[i], textAsset.text);
                    }
                }
                if (null != m_finishAction)
                {
                    Action action = m_finishAction;
                    m_finishAction = null;
                    action();
                }
            }
        }