Skip to content

Instantly share code, notes, and snippets.

@gamemachine
Last active August 28, 2021 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamemachine/a54e77051774e902444eb4f40107a701 to your computer and use it in GitHub Desktop.
Save gamemachine/a54e77051774e902444eb4f40107a701 to your computer and use it in GitHub Desktop.
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);
}
}
}
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;
}
}
}
}
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