Ejemplo n.º 1
0
    void IUpdate()
    {
        needRemoveDownloading.Clear();
        foreach (KeyValuePair <string, DownloadInfo> curr in m_DownloadPacks)
        {
            DownloadInfo info     = curr.Value;
            string       packName = curr.Key;
            if (info.www.error != null)
            {
                /*
                 * //加载失败时读取包内资源
                 * Packet_Sealed packet = new Packet_Sealed(packName);
                 * m_DonePacks.Add(packName, packet);
                 *
                 * foreach (IPacketLoadingListener listener in info.listeners)
                 *  listener.OnPacketLoadDone(packName, packet);
                 *
                 * needRemoveDownloading.Add(packName);
                 */
                if (m_DonePacks.ContainsKey(packName))//存在研发目录
                {
                    PacketRouting pk = m_DonePacks[packName];
                    foreach (var OnPacketLoadDone in info.listener_OnPacketLoadDone)
                    {
                        OnPacketLoadDone(packName, pk);
                    }
                }
                else
                {
                    Debug.LogError("包装载失败 " + packName);
                    Debug.Log("包装载失败路径 " + info.www.url);
                    foreach (var OnPacketLoadDone in info.listener_OnPacketLoadDone)
                    {
                        OnPacketLoadDone(packName, null);
                    }
                }
                needRemoveDownloading.Add(packName);
            }
            else if (info.www.isDone)
            {
                //Debug.Log("内部包加载完成" + packName);

                //##############################################解密包数据############################################
                byte[] encryptedData = info.www.bytes;
                //DateTime startTime = DateTime.Now;
                Packet_Bundle bundle;
                if (DefsDate.bundleJudge(encryptedData))
                {
                    bool li = DefsDate.SupDecToStream(encryptedData);
                    //Debug.Log("解密是否成功 = " + li);
                    AssetBundle assetBundle = AssetBundle.LoadFromMemory(encryptedData);
                    bundle = new Packet_Bundle(packName, assetBundle, FileSystem.RES_LOCATION.internalPack);
                }
                else
                {
                    //Debug.Log("解密不了的包或非加密包");
                    bundle = new Packet_Bundle(packName, info.www.assetBundle, FileSystem.RES_LOCATION.internalPack);
                }
                //Debug.Log("加载时间 = " + (DateTime.Now - startTime).ToString());
                //##############################################解密包数据############################################

                PacketRouting pk = AddPacket(packName, bundle);

                foreach (var OnPacketLoadDone in info.listener_OnPacketLoadDone)
                {
                    OnPacketLoadDone(packName, pk);
                }

                needRemoveDownloading.Add(packName);
            }
            else
            {
                foreach (var OnPacketLoadingProgress in info.listener_OnPacketLoadingProgress)
                {
                    OnPacketLoadingProgress(packName, info.www.progress);
                }
            }
        }

        if (needRemoveDownloading.Count > 0)
        {
            int size = 0;
            foreach (string packName in needRemoveDownloading)
            {
                size += m_DownloadPacks[packName].www.bytes.Length;
                m_DownloadPacks[packName].www.Dispose();
                m_DownloadPacks.Remove(packName);
            }

            AutoGC(size);
        }
    }
Ejemplo n.º 2
0
    /// <summary>
    /// 装载包
    /// </summary>
    /// <param name="pkType"></param>
    /// <param name="packName"></param>
    /// <param name="OnPacketLoadDone">包装载完成通知 包名,包路由对象</param>
    /// <param name="OnPacketLoadingProgress">包装载进度通知 包名,进度</param>
    public void LoadPacket(
        PackType pkType, string packName,
        Action <string, PacketRouting> OnPacketLoadDone,
        Action <string, float> OnPacketLoadingProgress,
        bool FoceLoadInternalPack = false//强制装内部包
        )
    {
        //已经装载完成,直接返回装载结果
        if (m_DonePacks.ContainsKey(packName))
        {
            OnPacketLoadDone(packName, m_DonePacks[packName]);
            return;
        }

        //当前处于下载中
        if (m_DownloadPacks.ContainsKey(packName))
        {
            DownloadInfo info = m_DownloadPacks[packName];
            if (OnPacketLoadDone != null && !info.listener_OnPacketLoadDone.Contains(OnPacketLoadDone))
            {
                info.listener_OnPacketLoadDone.Add(OnPacketLoadDone);
            }

            if (OnPacketLoadingProgress != null && !info.listener_OnPacketLoadingProgress.Contains(OnPacketLoadingProgress))
            {
                info.listener_OnPacketLoadingProgress.Add(OnPacketLoadingProgress);
            }
            return;
        }

        if (WorkInResourcesEditor)//当前处于资源编辑器中
        {
            Packet_ResourcesEditor bundle = new Packet_ResourcesEditor(packName);
            PacketRouting          pk     = AddPacket(packName, bundle);
            OnPacketLoadDone(packName, pk);
            return;
        }

        FileSystem.RES_LOCATION srcType;
        string devPackDir;
        string url = FileSystem.RrelativePath2Absolute_Packet(pkType == PackType.Res ? "pack_res" : "pack_script", packName + packetSuffix, false, out srcType, out devPackDir);

        //存在研发调试资源
        if (!string.IsNullOrEmpty(devPackDir))
        {
            Packet_Dir bundle = new Packet_Dir(devPackDir);
            AddPacket(packName, bundle);
        }

        switch (srcType)
        {
        case FileSystem.RES_LOCATION.fileSystem:    //文件系统
        {
            Packet_Dir bundle = new Packet_Dir(url);

            PacketRouting pk = AddPacket(packName, bundle);
            OnPacketLoadDone(packName, pk);
        }
        break;

        case FileSystem.RES_LOCATION.externalPack:    //外部包
        {
            //##############################################解密包数据############################################
            //DateTime startTime = DateTime.Now;
            long          FileSize;
            Packet_Bundle bundle;
            {
                byte[] encryptedData;

                using (FileStream kk = new FileStream(url, FileMode.Open))
                {
                    FileSize      = kk.Length;
                    encryptedData = new byte[FileSize];
                    kk.Read(encryptedData, 0, (int)kk.Length);
                }

                if (DefsDate.bundleJudge(encryptedData))
                {
                    bool li = DefsDate.SupDecToStream(encryptedData);
                    //Debug.Log("解密是否成功 = " + li);
                }
                //else
                //{
                //    Debug.Log("解密不了的包或非加密包");
                //}
                AssetBundle assetBundle = AssetBundle.LoadFromMemory(encryptedData);
                bundle = new Packet_Bundle(packName, assetBundle, FileSystem.RES_LOCATION.internalPack);
            }

            AutoGC(FileSize);

            //Debug.Log("加载时间 = " + (DateTime.Now - startTime).ToString());
            //##############################################解密包数据############################################

            //AssetBundle assetBundle = AssetBundle.LoadFromFile(url);

            //Packet_Bundle bundle = new Packet_Bundle(packName,assetBundle, FileSystem.RES_LOCATION.externalPack);
            PacketRouting pk = AddPacket(packName, bundle);
            if (FoceLoadInternalPack)
            {
                url = FileSystem.RrelativePath2Absolute_Packet(pkType == PackType.Res ? "pack_res" : "pack_script", packName + packetSuffix, false, out srcType, out devPackDir, true);
                LoadInternalPack(url, packName, OnPacketLoadDone, OnPacketLoadingProgress);
            }
            else
            {
                OnPacketLoadDone(packName, pk);
            }
        }
        break;

        case FileSystem.RES_LOCATION.internalPack:    //内部包
        {
            LoadInternalPack(url, packName, OnPacketLoadDone, OnPacketLoadingProgress);
        }
        break;
        }
    }