Last active
August 28, 2021 23:00
-
-
Save gamemachine/a54e77051774e902444eb4f40107a701 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ClientServerShared | |
{ | |
public class BinaryAsset | |
{ | |
public static BinaryAssetFolder AssetFolder(string path1, string path2 = null) | |
{ | |
return new BinaryAssetFolder(FolderType.Asset, path1, path2); | |
} | |
public static BinaryAssetFolder PersistentFolder(string path1, string path2 = null) | |
{ | |
return new BinaryAssetFolder(FolderType.Persistent, path1, path2); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
namespace ClientServerShared | |
{ | |
public class BinaryAssetFile | |
{ | |
public readonly string FullPath; | |
private static void ValidateNotDirectory(string path) | |
{ | |
if (Directory.Exists(path)) | |
{ | |
throw new FileNotFoundException("Expecting file or nothing, found directory"); | |
} | |
} | |
public BinaryAssetFile(BinaryAssetFolder directory, string path1, string path2) | |
{ | |
if (string.IsNullOrEmpty(path2)) | |
{ | |
FullPath = Path.Combine(directory.FullPath, string.Format("{0}.bin", path1)); | |
} | |
else | |
{ | |
FullPath = Path.Combine(directory.FullPath, string.Format("{0}_{1}.bin", path1, path2)); | |
} | |
ValidateNotDirectory(FullPath); | |
} | |
public bool Exists() | |
{ | |
return File.Exists(FullPath); | |
} | |
public bool Delete() | |
{ | |
if (Exists()) | |
{ | |
File.Delete(FullPath); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using UnityEngine; | |
namespace ClientServerShared | |
{ | |
public class BinaryAssetFolder | |
{ | |
private const string Root = "YourGame"; | |
public readonly string FullPath; | |
public static string GetRoot(FolderType type) | |
{ | |
if (type == FolderType.Asset) | |
{ | |
return Path.Combine(Application.streamingAssetsPath, Root); | |
} | |
else | |
{ | |
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Root); | |
} | |
} | |
private static void ValidateNotFile(string path) | |
{ | |
if (System.IO.File.Exists(path)) | |
{ | |
throw new FileNotFoundException("Expecting folder or nothing, found file"); | |
} | |
} | |
private static void CreateIfNotExists(string path) | |
{ | |
if (!Directory.Exists(path)) | |
{ | |
Directory.CreateDirectory(path); | |
} | |
} | |
public BinaryAssetFolder(FolderType type, string path1, string path2 = null) | |
{ | |
if (string.IsNullOrEmpty(path2)) | |
{ | |
FullPath = Path.Combine(GetRoot(type), path1); | |
} | |
else | |
{ | |
FullPath = Path.Combine(GetRoot(type), path1, path2); | |
} | |
ValidateNotFile(FullPath); | |
CreateIfNotExists(FullPath); | |
} | |
public string FilePath(string path1, string path2 = null) | |
{ | |
return new BinaryAssetFile(this, path1, path2).FullPath; | |
} | |
public BinaryAssetFile File(string path1, string path2 = null) | |
{ | |
return new BinaryAssetFile(this, path1, path2); | |
} | |
public bool Exists() | |
{ | |
return Directory.Exists(FullPath); | |
} | |
public bool Delete() | |
{ | |
if (Exists()) | |
{ | |
DirectoryInfo di = new DirectoryInfo(FullPath); | |
foreach (FileInfo file in di.GetFiles()) | |
{ | |
file.Delete(); | |
} | |
Directory.Delete(FullPath); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
} | |
public enum FolderType | |
{ | |
Asset, | |
Persistent | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment