Example #1
0
        /// <summary>
        /// Creates and returns a image file from the given serialized data.
        /// </summary>
        public static HashFile GetImageFileFromSerializedData(SerializedHashFileImage serialized)
        {
            var file = GetFileFromSerializedData(serialized.File);

            file.FileType = HashFileType.Image;
            var imageFile = new ImageFile();

            imageFile.ImageContent = serialized.ImageAsset;
            file.Content           = imageFile;
            return(file);
        }
Example #2
0
        private static void FillDirsAndFiles(
            string dir,
            int parentId,
            List <SerializedHashDir> allDirs,
            List <SerializedHashFileText> allTextFiles,
            List <SerializedHashFileImage> allImageFiles)
        {
            var parent = new SerializedHashDir();

            parent.Name        = Path.GetFileName(dir).ToLower();
            parent.DirId       = MathUtil.GetStringHash(dir);
            parent.ParentDirId = parentId;

            var childs        = Directory.GetDirectories(dir);
            var parentDirPath = PathUtil.RemovePathPart(dir, RESOURCE_FOLDER_PATH_IN_PROJECT);
            var files         = Resources.LoadAll <HashFileSO>("FileSystem/" + parentDirPath);

            var filesIds = new List <int>();

            for (int i = 0; i < files.Length; i++)
            {
                var file     = files[i];
                var fileType = file.Type;

                var filePath = AssetDatabase.GetAssetPath(file);

                {
                    // We need this validation because Resources.LoadAll will return files on subdirectory as well
                    var filePathRelativeToResource = PathUtil.RemovePathPart(filePath, RESOURCE_FOLDER_PATH_IN_PROJECT);
                    var fileName = Path.GetFileName(filePathRelativeToResource);

                    var fileDirPath = PathUtil.RemovePathPart(filePathRelativeToResource, fileName);
                    PathUtil.MatchPathFashion(ref fileDirPath, ref parentDirPath);
                    if (fileDirPath != parentDirPath)
                    {
                        continue;
                    }
                }

                if (fileType == HashFileType.Invalid)
                {
                    continue;
                }

                var hashFile = new SerializedHashFile();

                var name = file.name;
                hashFile.Name           = name.ToLower();
                hashFile.FileId         = MathUtil.GetStringHash(filePath);
                hashFile.ParentDirId    = parent.DirId;
                hashFile.UserPermission = file.Permissions;
                hashFile.Status         = file.Status;
                hashFile.Password       = file.Password;
                hashFile.Condition      = file.Condition;

                switch (fileType)
                {
                case HashFileType.Text:
                    var textFile = new SerializedHashFileText();
                    textFile.File = hashFile;

                    textFile.TextAsset = file.Content as TextAsset;
                    if (textFile.TextAsset == null)
                    {
                        Debug.LogError("This file [CLICK ME] is marked as text but it's content is not a text asset!", file);
                    }

                    allTextFiles.Add(textFile);
                    break;

                case HashFileType.Image:
                    var imageFile = new SerializedHashFileImage();
                    imageFile.File = hashFile;

                    imageFile.ImageAsset = file.Content as Texture2D;

                    if (imageFile.ImageAsset == null)
                    {
                        Debug.LogError("This file [CLICK ME] is marked as image but it's content is not a texture asset!", file);
                    }

                    allImageFiles.Add(imageFile);
                    break;

                default:
                    Debug.LogError("Type " + fileType + " not implemented yet!");
                    break;
                }

                filesIds.Add(hashFile.FileId);
            }

            parent.FilesId     = filesIds.ToArray();
            parent.ChildsDirId = new int[childs.Length];

            for (int i = 0; i < childs.Length; i++)
            {
                parent.ChildsDirId[i] = MathUtil.GetStringHash(childs[i]);
            }

            allDirs.Add(parent);

            for (int i = 0; i < childs.Length; i++)
            {
                FillDirsAndFiles(
                    childs[i],
                    parent.DirId,
                    allDirs,
                    allTextFiles,
                    allImageFiles);
            }
        }