Beispiel #1
0
 public static ICachedBlob <A> a <A>(
     ISerializedRW <A> rw, PathStr path, A defaultValue,
     ILog log = null, Log.Level onDeserializeFailureLogLevel = Log.Level.ERROR
     )
 {
     log = log ?? Log.d;
     return(new FileCachedBlob(path).bimap(BiMapper.a(
                                               (byte[] bytes) => {
         var deserializedOpt = rw.deserialize(bytes, 0);
         if (deserializedOpt.isNone)
         {
             if (log.willLog(onDeserializeFailureLogLevel))
             {
                 log.log(
                     onDeserializeFailureLogLevel,
                     $"Can't deserialize {path}, deleting and returning default value."
                     );
             }
             clear(path).getOrLog($"Couldn't clear file: '{path}'", log: log);
             return defaultValue;
         }
         return deserializedOpt.__unsafeGetValue.value;
     },
                                               a => rw.serialize(a).toArray()
                                               )));
 }
Beispiel #2
0
        public static PathStr toAbsoluteRelativeToProjectDir(this PathStr path)
        {
            var full = path.path.Contains(':') || path.path.StartsWithFast("/")
        ? path
        : Application.dataPath + "/../" + path;

            return(new PathStr(Path.GetFullPath(full)));
        }
Beispiel #3
0
        /// <summary>
        /// Because Unity method sucks balls with uninformative error messages.
        ///
        /// Source: https://msdn.microsoft.com/en-us/library/bb762914(v=vs.110).aspx
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        /// <param name="overwrite">Allow overwriting existing files.</param>
        public static void copyDirectory(PathStr source, PathStr destination, bool overwrite)
        {
            // Get the subdirectories for the specified directory.
            var dir = new DirectoryInfo(source);

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                          $"Source directory does not exist or could not be found: {source}"
                          );
            }

            var dirs = dir.GetDirectories();

            // If the destination directory doesn't exist, create it.
            if (!Directory.Exists(destination))
            {
                Directory.CreateDirectory(destination);
            }

            // Get the files in the directory and copy them to the new location.
            var files = dir.GetFiles();

            foreach (var file in files)
            {
                var temppath = destination / file.Name;
                file.CopyTo(temppath, overwrite: overwrite);
            }

            // Copying subdirectories, copy them and their contents to new location.
            foreach (var subdir in dirs)
            {
                var temppath = destination / subdir.Name;
                copyDirectory(PathStr.a(subdir.FullName), temppath, overwrite);
            }
        }
Beispiel #4
0
 public static Try <Unit> clear(PathStr path) => F.doTry(() => File.Delete(path));
Beispiel #5
0
 public static Try <Unit> store(PathStr path, byte[] data) =>
 F.doTry(() => File.WriteAllBytes(path, data));
Beispiel #6
0
 public static Option <Try <byte[]> > read(PathStr path) =>
 File.Exists(path)
 ? F.doTry(() => File.ReadAllBytes(path)).some()
 : F.none <Try <byte[]> >();
Beispiel #7
0
 public FileCachedBlob(PathStr path)
 {
     this.path = path;
 }
 public static Try <Unit> storeString(PathStr path, string data, Encoding encoding = null)
 {
     encoding = encoding ?? defaultEncoding;
     return(store(path, encoding.GetBytes(data)));
 }
 public static Option <Try <string> > readString(PathStr path, Encoding encoding = null)
 {
     encoding = encoding ?? defaultEncoding;
     return(read(path).map(t => t.map(bytes => encoding.GetString(bytes))));
 }
Beispiel #10
0
 PersistentDataCache(PathStr root)
 {
     this.root = root;
 }
Beispiel #11
0
 FilesystemDataCache(PathStr root)
 {
     this.root = root;
 }