Exemplo n.º 1
0
    public static void CretatePartPack(bool isMain)
    {
        string copyPath = FEPath.GetDirectoryName(SceneManager.STREAMINGPATH.Replace("file://", ""));
        string newPath  = copyPath.Replace("StreamingAssets", "_TempStreamingAssets");

        FSaveHandle firstData = FSaveHandle.Create(copyPath + "/" + ResConfig.STREAMINGASSETSCONFIG, FFilePath.FP_Abs);

        if (!firstData.IsLoad)
        {
            Debug.LogError("没找到新配置文件:" + copyPath + "/" + ResConfig.STREAMINGASSETSCONFIG + "操作失败");
            return;
        }

        BundleManager.StreamConfig firstStreamConfi = new BundleManager.StreamConfig();
        firstData.FromObject(firstStreamConfi);

        MyEdior.DeletFile(newPath);
        MyEdior.CopyDirectory(copyPath, newPath);
        AssetDatabase.Refresh();

        CreateConfig(newPath, firstStreamConfi, 0);

        string playFile = "";
        string mainPack = copyPath.Replace("Assets/StreamingAssets", "FPartPack/MainPack");

        if (isMain)
        {
            string copyPack = copyPath.Replace("Assets/StreamingAssets", "FTempCopyPack/Pack_" + DateTime.Now.ToString("MM_dd--HH_mm"));
            MyEdior.DeletFile(mainPack);

            FEPath.CreateDirectory(mainPack);

            EditorUtility.DisplayCancelableProgressBar("备份资源", "正在处理 ", 0.5f);
            File.Copy(newPath + "/" + ResConfig.STREAMINGASSETSCONFIG, mainPack + "/" + ResConfig.STREAMINGASSETSCONFIG, true);
            MyEdior.CopyDirectory(copyPath, copyPack + "/StreamingAssets");
            File.Copy(newPath + "/" + ResConfig.STREAMINGASSETSCONFIG, copyPack + "/" + ResConfig.STREAMINGASSETSCONFIG, true);
            FZipTool.ZipDirectory(copyPack, FEPath.GetDirectoryName(copyPack) + "/");
            MyEdior.DeletFile(copyPack);
            EditorUtility.ClearProgressBar();
            playFile = mainPack;
        }
        else
        {
            //加载新配置
            string      newstreamingPath = newPath + "/" + ResConfig.STREAMINGASSETSCONFIG;
            FSaveHandle sd = FSaveHandle.Create(newstreamingPath, FFilePath.FP_Abs);
            if (sd == null)
            {
                Debug.LogError("没找到新配置文件:" + ResConfig.STREAMINGASSETSCONFIG + "对比文件失败");
            }
            else
            {
                BundleManager.StreamConfig NewStreamConif = new BundleManager.StreamConfig();
                sd.FromObject(NewStreamConif);
                //加载旧配置
                string lastStremingPath = copyPath.Replace("Assets/StreamingAssets", "FPartPack/MainPack") + "/" + ResConfig.STREAMINGASSETSCONFIG;
                sd = FSaveHandle.Create(lastStremingPath, FFilePath.FP_Abs);

                if (sd == null)
                {
                    Debug.LogError(lastStremingPath + "文件夹下,没有找到旧的配置文件" + ResConfig.STREAMINGASSETSCONFIG + "对比文件失败");
                }
                else
                {
                    //曾经变化文件
                    string changeFile = mainPack + "/" + ResConfig.FCACHEFILE;
                    BundleManager.LegacyPackFile cfiles = new BundleManager.LegacyPackFile();
                    FSaveHandle cSave = FSaveHandle.Create(changeFile, FFilePath.FP_Abs);
                    if (cSave != null)
                    {
                        cSave.FromObject(cfiles);
                    }


                    BundleManager.StreamConfig LastStreamConif = new BundleManager.StreamConfig();
                    sd.FromObject(LastStreamConif);

                    if (NewStreamConif.versionId <= LastStreamConif.versionId)
                    {
                        Debug.LogError(ResConfig.STREAMINGASSETSCONFIG + ":新的配置文件versionId比旧配置文件要低,对比文件失败");
                    }
                    else
                    {
                        //精简文件
                        for (int i = 0; i < NewStreamConif.fileDatas.Count; i++)
                        {
                            var    newData        = NewStreamConif.fileDatas[i];
                            string tempBundleName = newData.fileName;
                            string newMd5         = NewStreamConif.GetMd5(tempBundleName);
                            string lastMd5        = LastStreamConif.GetMd5(tempBundleName);
                            if (cfiles.Files.ContainsKey(newData.fileName))//&& newData.path.Contains("fxlua"))
                            {
                                ScriptsTime.Debug("以前版本有变动的文件:" + newData.path + "==" + tempBundleName);
                            }
                            else if ((newMd5 != lastMd5 || lastMd5 == "")) //&& newData.path.Contains("fxlua"))
                            {
                                //文件有更新
                                ScriptsTime.Debug("产生新的文件:" + newData.path + "==" + tempBundleName);
                                cfiles.Files[newData.fileName] = 0;
                                string exten = FEPath.GetExtension(newData.fileName);
                                if (exten == ResConfig.ASSETBUNDLESUFFIXES)
                                {
                                    cfiles.Bundles[newData.fileName.Replace(ResConfig.FDESASSETBUNDLE + "/", "")] = 1;
                                }
                            }
                            else
                            {
                                string spePath = newPath + "/" + newData.fileName;
                                File.Delete(spePath);
                            }
                        }


                        string cachefile = newPath + "/" + ResConfig.FDESASSETBUNDLE + "/" + ResConfig.FCACHEFILE;
                        cSave = FSaveHandle.Create(cachefile, FFilePath.FP_Abs, FOpenType.OT_Write);
                        cSave.PushObject(cfiles);
                        cSave.Save();
                        File.Copy(cachefile, changeFile, true);


                        //重新生成压缩文件
                        int versioin = firstStreamConfi.versionId;
                        CreateConfig(newPath, firstStreamConfi, 1);
                        string partversionFile = "";
                        if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
                        {
                            partversionFile = copyPath.Replace("Assets/StreamingAssets", "FPartPack/" + versioin + "/ios1");
                        }
                        else
                        {
                            partversionFile = copyPath.Replace("Assets/StreamingAssets", "FPartPack/" + versioin + "/az1");
                        }
                        MyEdior.DeletFile(partversionFile);
                        MyEdior.CopyDirectory(newPath, partversionFile);
                        var    t    = FSaveHandle.Create(partversionFile + "/version", FFilePath.FP_Abs, FOpenType.OT_Write | FOpenType.OT_Txt);
                        string md5  = FCommonFunction.GetMD5HashFromFile(partversionFile + "/FAssetbundle.zip");
                        int    type = EditorUserBuildSettings.activeBuildTarget != BuildTarget.iOS ? 0:1;
                        t.SetContext("{\"version\":" + versioin.ToString() + ",\"md5\":\"" + md5 + ",\"type\":\"" + type.ToString() + "\"}");
                        t.Save();
                        File.Delete(partversionFile + "/FAssetbundle.zip.meta");
                        File.Delete(partversionFile + "/FStreamingAssets");
                        File.Delete(partversionFile + "/FStreamingAssets.meta");
                        playFile = partversionFile;
                    }
                }
            }
        }
        MyEdior.DeletFile(newPath);
        AssetDatabase.Refresh();
        if (playFile != "")
        {
            System.Diagnostics.Process.Start(playFile);
            Debug.Log(isMain ? "主包生成完成" : "分包生成完成");
        }
    }