using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using LLM.Editor.Analysis; using LLM.Editor.Helper; using UnityEditor; using UnityEditorInternal; using UnityEngine; namespace AssetBank.Editor { public class AssetBankScanner : EditorWindow { private List folderToIgnore = new List() { "AssetBank", "Library" }; private List fileExtensionToIgnore = new List() { ".meta", ".dylib", ".bytes", ".dll" }; private List typesToIgnore = new List() { typeof(DefaultAsset).ToString() }; private int currentTab = 0; private string[] tabs = new[] { "General" , "Settings"}; private readonly string folderstoignore = "AssetBankScanner_FolderToIgnore"; private readonly string extensiontoignore = "AssetBankScanner_FileExtensionToIgnore"; private readonly string typesToignore = "AssetBankScanner_TypesToIgnore"; private readonly string CACHE_FOLDER = "AssetBank"; private string cachePath; private List prefLists = new(); private StringBuilder projectReport = new StringBuilder(); [MenuItem("Window/Asset Database Scanner")] public static void ShowWindow() { var window = GetWindow(); window.titleContent = new GUIContent("Asset Database Scanner"); window.Show(); } private void OnEnable() { cachePath = Path.Join("Library", CACHE_FOLDER); LoadSettings(); SetupLists(); } private void SetupLists() { prefLists.Add(CreateReorderableList("Folders to ignore", folderToIgnore)); prefLists.Add(CreateReorderableList("Files to ignore", fileExtensionToIgnore)); prefLists.Add(CreateReorderableList("Types to ignore", typesToIgnore)); } private void LoadSettings() { if (!EditorPrefs.HasKey(folderstoignore)) { EditorPrefs.SetString(folderstoignore, String.Join(";",folderToIgnore)); } if (!EditorPrefs.HasKey(extensiontoignore)) { EditorPrefs.SetString(extensiontoignore, String.Join(";", fileExtensionToIgnore)); } if (!EditorPrefs.HasKey(typesToignore)) { EditorPrefs.SetString(typesToignore, String.Join(";",typesToIgnore)); } folderToIgnore = EditorPrefs.GetString(folderstoignore, "").Split(';') .Where(x => !string.IsNullOrWhiteSpace(x)).ToList(); fileExtensionToIgnore = EditorPrefs.GetString(extensiontoignore, "").Split(';') .Where(x => !string.IsNullOrWhiteSpace(x)).ToList(); typesToIgnore = EditorPrefs.GetString(typesToignore, "").Split(';').Where(x => !string.IsNullOrWhiteSpace(x)).ToList(); } private void OnGUI() { EditorGUILayout.LabelField("Asset Bank Scanner", EditorStyles.boldLabel); GUILayout.Space(10); currentTab = GUILayout.Toolbar(currentTab, tabs); GUILayout.Space(10); switch (currentTab) { case 0: DrawScanner(); break; case 1: DrawSettings(); break; } } private void DrawSettings() { GUILayout.Label("Settings", EditorStyles.boldLabel); foreach (var prefList in prefLists) { prefList.DoLayoutList(); EditorGUILayout.Space(); } if (GUILayout.Button("Clear cache")) { Directory.Delete(cachePath, true); } return; # region "Clear options" if (GUILayout.Button("Clear preferences", EditorStyles.miniButton)) { prefLists.Clear(); EditorPrefs.DeleteKey(folderstoignore); EditorPrefs.DeleteKey(extensiontoignore); EditorPrefs.DeleteKey(typesToignore); } #endregion } private ReorderableList CreateReorderableList(string title, List targetList) { var list = new ReorderableList(targetList, typeof(string), true, true, true, true) { drawHeaderCallback = rect => { EditorGUI.LabelField(rect, title); }, drawElementCallback = (rect, index, isActive, isFocused) => { targetList[index] = EditorGUI.TextField( new Rect(rect.x, rect.y + 2, rect.width, EditorGUIUtility.singleLineHeight), targetList[index] ); }, onAddCallback = l => { targetList.Add(""); }, onRemoveCallback = l => { targetList.RemoveAt(l.index); } }; return list; } private void DrawScanner() { if (GUILayout.Button("Scan project", GUILayout.Height(25))) { ScanProject(); } } private void ScanProject() { CreateMeta(); SerializeProjectSettings(); } private void SerializeProjectSettings() { // ProjectSettingsProvider _provider = new ProjectSettingsProvider(); // var q = _provider.GetContext(null, "QualitySettings"); // } private void CreateMeta() { float index = 0; var assetPaths = AssetDatabase.GetAllAssetPaths(); int assetCount = assetPaths.Length; foreach (var assetPath in assetPaths) { var dataPath = Path.Join(cachePath, assetPath) + ".json"; var metaPath = Path.Join(cachePath, assetPath) + ".meta" + ".json"; var guid = AssetDatabase.AssetPathToGUID(assetPath); var progress = index / assetCount; index++; if (string.IsNullOrEmpty(guid)) { Debug.LogWarning("Asset is invalid: " + assetPath); continue; } var extension = Path.GetExtension(assetPath); if (fileExtensionToIgnore.Contains(extension)) { Debug.LogWarning("Asset file is not supported: " + assetPath); continue; } FileInfo info = new FileInfo(metaPath); if (info.Directory != null) { if (folderToIgnore.Contains(info.Directory.FullName)) { Debug.Log("Ignoring directory: " + assetPath); continue; } info.Directory.Create(); } AssetMetaData assetMetaData = new AssetMetaData { path = assetPath, guid = guid, dependencies = AssetDatabase.GetDependencies(assetPath) }; if (EditorUtility.DisplayCancelableProgressBar("Updating", "Updating meta for file" + assetPath, progress)) { return; } Thread.Sleep(1); var metaData = assetMetaData.ToJson(); File.WriteAllText(metaPath, metaData); SerializeFile(assetPath, dataPath); } EditorUtility.ClearProgressBar(); } private void SerializeFile(string path, string dataPath) { Type type = AssetDatabase.GetMainAssetTypeAtPath(path); // Looking at supporting types only if (type != typeof(GameObject) && type != typeof(Texture) && type != typeof(Material) && type != typeof(Shader) && type != typeof(Font) && type != typeof(PlayerSettings) && type != typeof(Animation) && type != typeof(Material) && type != typeof(PrefabAssetType) && type != typeof(TextAsset) && type != typeof(AudioClip) && type != typeof(SceneAsset) && type != typeof(MonoScript) && type != typeof(ScriptableObject)) { return; } AssetDatabase.LoadAssetAtPath(path, type); AssetData assetData = new AssetData(); // TODO: Serialization for asset types var jsonifiedAsset = assetData.ToJson(); File.WriteAllText(dataPath, jsonifiedAsset); } } }