using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

/// <summary>
/// 缓存bundle
/// </summary>
public class BuildRulesBundleDatas : ScriptableObject
{
    public RuleBundle[] ruleBundles = new RuleBundle[0];

    private static BuildRulesBundleDatas current;
    public static BuildRulesBundleDatas Load()
    {
        if(current == null)
        {
            string tempPath = "Assets/TempRuleDatas.asset";
            current = AssetDatabase.LoadAssetAtPath<BuildRulesBundleDatas>(tempPath);
            if(current == null)
            {
                current = BuildRulesBundleDatas.CreateInstance<BuildRulesBundleDatas>();
                AssetDatabase.CreateAsset(current, tempPath);
            }
            
        }
        return current;
    }

    public void Save()
    {

        string assetPath = AssetDatabase.GetAssetPath(this);
        File.SetAttributes(assetPath, FileAttributes.Normal);
        EditorUtility.SetDirty(this);
        AssetDatabase.SaveAssets();
    }
    public AssetBundleBuild[] GetBuilds()
    {
        var builds = new List<AssetBundleBuild>();
        foreach (var bundle in ruleBundles)
        {
            builds.Add(new AssetBundleBuild
            {
                assetNames = bundle.assets,
                assetBundleName = bundle.name
            });
        }

        return builds.ToArray();
    }
}
