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() ))); }
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))); }
/// <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); } }
public static Try <Unit> clear(PathStr path) => F.doTry(() => File.Delete(path));
public static Try <Unit> store(PathStr path, byte[] data) => F.doTry(() => File.WriteAllBytes(path, data));
public static Option <Try <byte[]> > read(PathStr path) => File.Exists(path) ? F.doTry(() => File.ReadAllBytes(path)).some() : F.none <Try <byte[]> >();
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)))); }
PersistentDataCache(PathStr root) { this.root = root; }
FilesystemDataCache(PathStr root) { this.root = root; }