/// <summary> /// Checks if the file follows the convention /// </summary> /// <param name="file">The file data</param> /// <returns>The state of the file based on the convention</returns> public static FileConventionState CheckFileConvention(FileData file) { FileConventionState result = FileConventionState.NotValid; //We run through all folders until the root, collecting possible valid file types. If a parent accept, the children should too. List <string> allowedFileTypesList = GetAllTypesOfPath(file.folderAssetsPath); if (config["folderStructure"]["ignore"]["files"].list.Find(x => x.str == file.fullName) != null) { result = FileConventionState.Ignored; } else if (allowedFileTypesList.Count == 0) { result = FileConventionState.NotValid; } else if (allowedFileTypesList.Count > 0) { string tmp = allowedFileTypesList.Find(x => x == file.type); bool isAllowedFileType = (tmp != null) ? true : false; if (!isAllowedFileType) { if (config["folderStructure"]["ignore"]["fileTypes"].list.Find(x => x.str == file.type) != null) { result = FileConventionState.Ignored; } else { result = FileConventionState.NotValid; } } else { JSONObject conventionObject = GetNameConventionObject(file.type); if (!conventionObject.IsNull) { List <JSONObject> typeConventions = conventionObject["conventions"].list; if (typeConventions != null) { result = CheckFileNameConvention(file, typeConventions); } } else { result = FileConventionState.NotValid; } } } else { result = FileConventionState.UnknownFileType; } return(result); }
/// <summary> /// Streamiline the evaluation process for the imported files /// </summary> /// <param name="filePath">the path of the file to be evaluated</param> /// <returns>The state of the given file</returns> public static FileConventionState CheckImportFileConvention(string filePath) { FileData file = new FileData(filePath); FolderConventionState folderState = CheckFolderConvention(file); FileConventionState fileState = FileConventionState.NotValid; switch (folderState) { case FolderConventionState.Valid: fileState = CheckFileConvention(file); break; case FolderConventionState.NotValid: fileState = FileConventionState.NotValid; break; } //Show warnings if any if (folderState == FolderConventionState.NotValid) { NotValidFolderDialog(file.folderAssetsPath); } else if (folderState == FolderConventionState.Valid && fileState == FileConventionState.NotValid) { switch (fileState) { case FileConventionState.WrongFileName: FileNameChangeDialog(file); break; case FileConventionState.NotValid: DialogNoValidFile(file); break; } } return(fileState); }
/// <summary> /// Recursively checks the conventions in folders and files /// </summary> /// <param name="path"></param> public static bool ProcessSubFolders(string path) { bool stopProcess = false; FolderConventionState folderState = CheckFolderConvention(path); switch (folderState) { case FolderConventionState.Ignored: //Do nothing, ignore them :) break; case FolderConventionState.Valid: List <string> allowedFileTypes = GetAllTypesOfPath(path); List <FileData> assetList = GetAllFileDataAtPath(path); if (allowedFileTypes != null && assetList.Count > 0) { string localErrorMessage = string.Empty; foreach (FileData asset in assetList) { FileConventionState conventionState = CheckFileConvention(asset); switch (conventionState) { case FileConventionState.WrongFileName: FileNameChangeDialog(asset); break; case FileConventionState.NotValid: DialogNoValidFile(asset); break; } if (conventionState == FileConventionState.WrongFileName || conventionState == FileConventionState.NotValid) { stopProcess = true; } } } else if (allowedFileTypes == null && assetList.Count > 0) { Dialog("The path \"" + path + "\" has FILES which the TYPE IS NOT in the Convention, what to do?", new List <ButtonData>() { new ButtonData("Add FILE TYPES to this path Convention", delegate() { AddFileTypesToPath(path, assetList); }), new ButtonData("Delete wrong FILES", delegate() { //Delete files that are not in the allowed file types list DeleteDialog(delegate() { foreach (FileData asset in assetList) { AssetDatabase.DeleteAsset(asset.assetsFullPath); } AssetDatabase.Refresh(); RecheckConventionDialog(); }); }), new ButtonData("Do nothing right now", delegate() { //Do nothing - Highlight the parent folder just so the user can check it if wants EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(path)); }) }); stopProcess = true; } //Check sub folders if allowed if (allowedFileTypes == null) //There is no aloowed type in the folder { return(false); } List <string> subFolders = new List <string>(AssetDatabase.GetSubFolders(path)); if (allowedFileTypes.Contains("folder")) { for (int i = 0; i < subFolders.Count; i++) { if (ProcessSubFolders(subFolders[i])) //If returns 1, means we need to stop the processing as the validation process might changed { break; } } } else { if (subFolders.Count > 0) { Dialog("The path \"" + path + "\" does not allow FOLDERS and it contains " + subFolders.Count + " folders, what to do?", new List <ButtonData>() { new ButtonData("Add FOLDERS permission to this path", delegate() { AddFolderTypeToPath(path); }), new ButtonData("Delete child FOLDERS", delegate() { //Delete the list of sub folders DeleteDialog(delegate() { foreach (string folder in subFolders) { AssetDatabase.DeleteAsset(folder); } AssetDatabase.Refresh(); RecheckConventionDialog(); }); }), new ButtonData("Do nothing right now", delegate() { //Do nothing - Highlight the parent folder just so the user can check it if wants EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(path)); }) }); stopProcess = true; } } break; case FolderConventionState.NotValid: NotValidFolderDialog(path); stopProcess = true; break; } return(stopProcess); }
/// <summary> /// Processes all folders under the given path seeking convention breaches. /// </summary> /// <param name="path">The path to process to use results on Overview Window</param> public static void OverviewProcessFolder(string path) { JSONObject pathConfig = config["folderStructure"]["check"]["folders"].list.Find(x => x["path"].str == path); List <JSONObject> allowedFileTypes = (pathConfig != null) ? pathConfig["fileTypesAllowed"].list : null; List <FileData> assetList = GetAllFileDataAtPath(path); FolderConventionState folderState = CheckFolderConvention(path); if (folderState != FolderConventionState.Ignored) { FileData folderData = new FileData(path); if (folderState == FolderConventionState.NotValid) { folderData.folderError.Add(FolderConventionState.NotValid); } List <string> subFolders = new List <string>(AssetDatabase.GetSubFolders(path)); if (subFolders.Count > 0) { folderData.folderError.Add(FolderConventionState.NoFoldersAllowed); for (int i = 0; i < subFolders.Count; i++) { OverviewProcessFolder(subFolders[i]); } } if (allowedFileTypes == null && assetList.Count > 0) { folderData.folderError.Add(FolderConventionState.UnknownFiles); } if (folderState != FolderConventionState.Ignored && folderState != FolderConventionState.Valid) { overviewFolderList.Add(folderData); } //Process Files if (assetList.Count > 0) { foreach (FileData asset in assetList) { FileConventionState conventionState = CheckFileConvention(asset); switch (conventionState) { case FileConventionState.WrongFileName: asset.fileError.Add(FileConventionState.WrongFileName); overviewFileList.Add(asset); break; case FileConventionState.NotValid: asset.fileError.Add(FileConventionState.NotValid); overviewFileList.Add(asset); break; } } } } }