/// <summary>Target path</summary>
 static string GetFullPath(AssetBundleData data, UnityEditor.BuildTarget target)
 {
     return(data.Directory.Replace(AssetBundleManager.PlatformFolder, AssetBundleManager.GetPlatformFolder(target)) + "/" + data.File);
 }
        static void AddAssetBundleToBuildMap(AssetBundleBuild[] buildMap, int position, AssetBundleManageData data, AssetBundleData assetbundle)
        {
            if (assetbundle.GetAllContents().Count <= 0)
            {
                buildMap[position].assetBundleName = "noname";
                buildMap[position].assetNames      = null;
                return;
            }
            // add all path to list
            List <string> files = new List <string>();

            foreach (var path in assetbundle.GetAllContentsPath())
            {
                if (File.Exists(path))
                {
                    files.Add(path);
                }
            }

            Debug.Log(nestString + "Adding Build " + assetbundle.File);

            buildMap[position].assetBundleName = assetbundle.File;
            Debug.Log("There're " + files.ToArray().Length + " file in " + assetbundle.File + " bundle. Created in " + assetbundle.Directory);
            buildMap[position].assetNames = files.ToArray();
        }
        void OnGUI()
        {
            if (this._first)
            {
                this.RefreshAssetBundleList();
                this._first = false;
            }

            // Undo
            switch (Event.current.type)
            {
            case EventType.MouseUp:
            case EventType.MouseDown:
                if (Event.current.button == 0)                  // Left Click
                {
                    Undo.RecordObjects(this._undoObjects, "AssetBundleData");
                }
                break;

            case EventType.DragPerform:
                Undo.RecordObjects(this._undoObjects, "AssetBundleData");
                break;

            case EventType.KeyDown:
                Undo.RecordObjects(this._undoObjects, "AssetBundleData");
                break;
            }

            this._assetBundleData.notUseBundleOnEditor = EditorGUILayout.ToggleLeft(this._notUseBundleContent, this._assetBundleData.notUseBundleOnEditor);

            EditorGUILayout.Separator();
            EditorGUILayout.Separator();

            EditorGUILayout.BeginVertical("Box");
            //EditorGUILayout.BeginHorizontal();
            //EditorGUILayout.PrefixLabel(this._editBundleContent);
            EditorGUI.BeginChangeCheck();
            // assetbundle list
            this._bundleListPos = EditorGUILayout.Popup(this._editBundleContent, this._bundleListPos, this._assetBundlePathList);
            if (EditorGUI.EndChangeCheck())
            {
                this.SelectAssetBundle(this._bundleListPos);
            }
            //EditorGUILayout.EndHorizontal();

            EditorGUILayout.Separator();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.Space();
            if (GUILayout.Button("Add", GUILayout.Width(100)))
            {
                var ret = EditorUtility.SaveFilePanel("Add AssetBundle", Application.dataPath, "NewAssetBundle", "unity3d");
                if (!string.IsNullOrEmpty(ret))
                {
                    var path = AssetBundleUtilityNew.ChangeRelativePath(Application.dataPath, ret);
                    //FileInfo file = new FileInfo(path);
                    var assetbundle = new AssetBundleData(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));
                    this._assetBundleData.assetbundles.Add(assetbundle);
                    this._bundleListPos = this._assetBundleData.assetbundles.Count - 1;
                    this.RefreshAssetBundleList();
                }
            }
            EditorGUI.BeginDisabledGroup(this._assetBundleData.assetbundles.Count <= this._bundleListPos);
            if (GUILayout.Button("Delete", GUILayout.Width(100)))
            {
                // decrement parent no
                foreach (var assetbundle in this._assetBundleData.assetbundles)
                {
                    if (this._bundleListPos == assetbundle.ParentNo)
                    {
                        assetbundle.ParentNo = -1;
                    }
                    if (this._bundleListPos < assetbundle.ParentNo)
                    {
                        --assetbundle.ParentNo;
                    }
                }
                // remove
                this._assetBundleData.assetbundles.RemoveAt(this._bundleListPos);
                this.RefreshAssetBundleList();
            }
            EditorGUI.EndDisabledGroup();
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.EndVertical();

            EditorGUILayout.Separator();
            EditorGUILayout.Separator();

#if DLL
            EditorGUI.BeginDisabledGroup(true);
#endif
            // multi build
            this._assetBundleData.multiPlatform = EditorGUILayout.Toggle("Multi Platform", this._assetBundleData.multiPlatform);
            EditorGUI.BeginDisabledGroup(!this._assetBundleData.multiPlatform);
            // target list
            int targetflg = 0;
            var targets   = (System.Enum.GetValues(typeof(BuildTarget))  as int[]).Distinct().Select(v => (BuildTarget)v).ToArray();
#if !DLL
            for (int i = 0; i < targets.Length; ++i)
            {
                if (this._assetBundleData.targets.Contains(targets[i]))
                {
                    targetflg |= (1 << (int)i);
                }
            }
#endif
            targetflg = EditorGUILayout.MaskField("BuildTarget", targetflg,
                                                  targets.Select(v => ((BuildTarget)v).ToString()).ToArray());
#if !DLL
            this._assetBundleData.targets.Clear();
            for (int i = 0; i < targets.Length; ++i)
            {
                if ((targetflg & (1 << i)) != 0)
                {
                    this._assetBundleData.targets.Add(targets[i]);
                }
            }
#endif
            EditorGUI.EndDisabledGroup();
#if DLL
            EditorGUI.EndDisabledGroup();
#endif
            EditorGUILayout.BeginHorizontal();
            EditorGUI.BeginDisabledGroup(this._assetBundleData.assetbundles.Count <= this._bundleListPos);
            if (GUILayout.Button("Build", GUILayout.Width(150), GUILayout.Height(25)))
            {
                // Create a selected assetbundle
                this._delayAction = () =>
                {
                    this.CreateAssetBundleWithDependency(this._bundleListPos, false);
                    this.Save();
                };
            }
            EditorGUI.EndDisabledGroup();
            if (GUILayout.Button("Build All", GUILayout.Width(150), GUILayout.Height(25)))
            {
                // Create all assetbundles
                this._delayAction = () =>
                {
                    this.CreateAssetBundleAll(false);
                    this.Save();
                };
            }
            EditorGUILayout.EndHorizontal();
            //EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal();
            EditorGUI.BeginDisabledGroup(this._assetBundleData.assetbundles.Count <= this._bundleListPos);
            if (GUILayout.Button("Rebuild", GUILayout.Width(150), GUILayout.Height(25)))
            {
                // Create a selected assetbundle
                this._delayAction = () =>
                {
                    this.CreateAssetBundleWithDependency(this._bundleListPos, true);
                    this.Save();
                };
            }
            EditorGUI.EndDisabledGroup();
            if (GUILayout.Button("Rebuild All", GUILayout.Width(150), GUILayout.Height(25)))
            {
                // Create all assetbundles
                this._delayAction = () =>
                {
                    this.CreateAssetBundleAll(true);
                    this.Save();
                };
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Separator();
            EditorGUILayout.Separator();

            //EditorGUILayout.LabelField("System");
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.Space();
            EditorGUILayout.Space();
            if (GUILayout.Button("Save"))
            {
                // Save
                this.Save();
            }
            if (GUILayout.Button("Exit"))
            {
                // Exit.
                this.Close();
            }
            EditorGUILayout.EndHorizontal();
        }
        static bool CreateAssetBundle(AssetBundleManageData data, AssetBundleData assetbundle, bool rebuild, bool dependency)
        {
            if (assetbundle.GetAllContents().Count <= 0)
            {
                return(false);
            }
            // add all path to list
            List <string> files = new List <string>();

            foreach (var path in assetbundle.GetAllContentsPath())
            {
                if (File.Exists(path))
                {
                    files.Add(path);
                }
            }
            // add special list asset for system
            BundleAssetList list     = null;
            string          listpath = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(data)) + "/list/" + assetbundle.File + "/list.asset";   //Application.temporaryCachePath + "/" + "list.asset";

            if (!assetbundle.ForStreamedScene)
            {
                list        = CreateInstance <BundleAssetList>();
                list.Assets = files.Select(file => {
                    FileInfo info         = new FileInfo(file);
                    BundleAssetInfo asset = new BundleAssetInfo(info.Name);
                    return(asset);
                }).ToArray();

                var directory = System.IO.Path.GetDirectoryName(listpath).Substring("Assets".Length);
                System.IO.Directory.CreateDirectory(Application.dataPath + directory);
                AssetDatabase.CreateAsset(list, listpath);
                //files.Add(listpath);
                AssetBundleUtility.AssetList = list;
                // BundleAssetList.cs(not exist file "BundleAssetList.cs" with DLL)
                //string bundleAssetsListPath = "Assets/" + Path.GetDirectoryName(AssetBundleManager.SettingDataPath) + "/Scripts/BundleAssetList.cs";
                //files.Add(bundleAssetsListPath);
            }
            var targets = new BuildTarget[] { EditorUserBuildSettings.activeBuildTarget };

#if !DLL
            if (assetbundle.PlatformFolder && data.multiPlatform && 0 < data.targets.Count)
            {
                targets = data.targets.ToArray();
            }
#endif

            bool          ret          = false;
            List <string> platformList = new List <string>();
            foreach (BuildTarget target in targets)
            {
                var path = assetbundle.Path.Replace(AssetBundleManager.PlatformFolder, AssetBundleManager.GetPlatformFolder(Application.platform));
#if !DLL
                if (assetbundle.PlatformFolder)
                {
                    var platformFolder = AssetBundleManager.GetPlatformFolder(target);
                    if (platformList.Contains(platformFolder))
                    {
                        continue;
                    }
                    platformList.Add(platformFolder);
                    path = GetFullPath(assetbundle, target);
                }
#endif
                var dir       = Path.GetDirectoryName(path);
                var assetName = Path.GetFileName(path);
                if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);                    //AssetDatabase.CreateFolder(Path.GetDirectoryName(dir), Path.GetFileName(dir));
                }
                if (!string.IsNullOrEmpty(dir))
                {
                    dir = dir + "/";
                }

                if (!assetbundle.ForStreamedScene)
                {
                    if (!assetbundle.Separated)
                    {
                        Debug.Log(nestString + "Build " + assetbundle.File);

                        /*if( AssetBundleUtilityNew.CreateBundle(files.ToArray(), dir, assetName, (BuildAssetBundleOptions)assetbundle.Options, target, rebuild | assetbundle.Changed) )
                         * {
                         *      ret = true;
                         * }*/
                        //Hoavq change to new build system
                        AssetBundleBuild[] buildMap = new AssetBundleBuild[1];

                        /*buildMap[0].assetBundleName = assetName;
                         * Debug.Log("There're " + files.ToArray().Length + " file in " + assetName + " bundle. Created in " + dir);
                         * buildMap[0].assetNames = files.ToArray();*/

                        AddAssetBundleToBuildMap(buildMap, 0, data, assetbundle);
                        BuildPipeline.BuildAssetBundles(dir, buildMap, (BuildAssetBundleOptions)assetbundle.Options, target);
                    }
                }
            }
            assetbundle.Changed = false;
            return(ret);
        }
        static bool CreateAssetBundle(AssetBundleManageData data, AssetBundleData assetbundle, bool rebuild, bool dependency)
        {
            if (assetbundle.GetAllContents().Count <= 0)
            {
                return(false);
            }
            // add all path to list
            List <string> files = new List <string>();

            foreach (var path in assetbundle.GetAllContentsPath())
            {
                if (File.Exists(path))
                {
                    files.Add(path);
                }
            }
            // add special list asset for system
            BundleAssetList list     = null;
            string          listpath = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(data)) + "/list/" + assetbundle.File + "/list.asset";   //Application.temporaryCachePath + "/" + "list.asset";

            if (!assetbundle.ForStreamedScene)
            {
                list        = CreateInstance <BundleAssetList>();
                list.Assets = files.Select(file => {
                    FileInfo info         = new FileInfo(file);
                    BundleAssetInfo asset = new BundleAssetInfo(info.Name);
                    return(asset);
                }).ToArray();

                var directory = System.IO.Path.GetDirectoryName(listpath).Substring("Assets".Length);
                System.IO.Directory.CreateDirectory(Application.dataPath + directory);
                AssetDatabase.CreateAsset(list, listpath);
                //files.Add(listpath);
                AssetBundleUtility.AssetList = list;
                // BundleAssetList.cs(not exist file "BundleAssetList.cs" with DLL)
                //string bundleAssetsListPath = "Assets/" + Path.GetDirectoryName(AssetBundleManager.SettingDataPath) + "/Scripts/BundleAssetList.cs";
                //files.Add(bundleAssetsListPath);
            }
            var targets = new BuildTarget[] { EditorUserBuildSettings.activeBuildTarget };

#if !DLL
            if (assetbundle.PlatformFolder && data.multiPlatform && 0 < data.targets.Count)
            {
                targets = data.targets.ToArray();
            }
#endif

            bool          ret          = false;
            List <string> platformList = new List <string>();
            foreach (BuildTarget target in targets)
            {
                var path = assetbundle.Path.Replace(AssetBundleManager.PlatformFolder, AssetBundleManager.GetPlatformFolder(Application.platform));
#if !DLL
                if (assetbundle.PlatformFolder)
                {
                    var platformFolder = AssetBundleManager.GetPlatformFolder(target);
                    if (platformList.Contains(platformFolder))
                    {
                        continue;
                    }
                    platformList.Add(platformFolder);
                    path = GetFullPath(assetbundle, target);
                }
#endif
                var dir = Path.GetDirectoryName(path);
                if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);                    //AssetDatabase.CreateFolder(Path.GetDirectoryName(dir), Path.GetFileName(dir));
                }
                if (!string.IsNullOrEmpty(dir))
                {
                    dir = dir + "/";
                }

                if (!assetbundle.ForStreamedScene)
                {
                    if (!assetbundle.Separated)
                    {
                        Debug.Log(nestString + "Build " + assetbundle.File);
                        if (AssetBundleUtility.CreateBundle(files.ToArray(), path, (BuildAssetBundleOptions)assetbundle.Options, target, rebuild | assetbundle.Changed))
                        {
                            ret = true;
                        }
                    }
                    else
                    {
                        var infos = new BundleAssetInfo[] { new BundleAssetInfo("") };
                        list.Assets = infos;
                        AssetBundleUtility.AssetList = list;
                        foreach (var file in files)
                        {
                            infos[0].Name = file;
                            string name = Path.GetFileNameWithoutExtension(file) + ".unity3d";
                            Debug.Log(nestString + "Build " + name);
                            if (AssetBundleUtility.CreateBundle(file, dir + name, (BuildAssetBundleOptions)assetbundle.Options, target, rebuild | assetbundle.Changed))
                            {
                                ret = true;
                            }
                            BuildPipeline.PopAssetDependencies();
                            BuildPipeline.PushAssetDependencies();
                        }
                    }
                }
                else
                {
                    if (!assetbundle.Separated)
                    {
                        Debug.Log(nestString + "Build " + assetbundle.File);
                        if (AssetBundleUtility.CreateSceneBundle(files.ToArray(), path, (BuildAssetBundleOptions)assetbundle.Options, target, rebuild | assetbundle.Changed))
                        {
                            ret = true;
                        }
                    }
                    else
                    {
                        foreach (var file in files)
                        {
                            string name = Path.GetFileNameWithoutExtension(file) + ".unity3d";
                            Debug.Log(nestString + "Build " + name);
                            if (AssetBundleUtility.CreateSceneBundle(file, dir + name, (BuildAssetBundleOptions)assetbundle.Options, target, rebuild | assetbundle.Changed))
                            {
                                ret = true;
                            }
                            BuildPipeline.PopAssetDependencies();
                            BuildPipeline.PushAssetDependencies();
                        }
                    }
                }
            }
            assetbundle.Changed = false;
            //AssetDatabase.DeleteAsset(listpath);	Child assetbundle require list.asset of parent assetbundle.
            //AssetDatabase.Refresh();
            return(ret);
        }