Skip to content

Instantly share code, notes, and snippets.

@exts
Created October 5, 2018 05:18
Show Gist options
  • Save exts/641cad4b22d9c24c809ec0852d415fec to your computer and use it in GitHub Desktop.
Save exts/641cad4b22d9c24c809ec0852d415fec to your computer and use it in GitHub Desktop.
Godot 3.1 C# Pck File Exposer
public class PckFileExposer : IDisposable
{
private byte[] _data;
private string _filename;
private string _tmpDataFilename;
private bool _disposed;
public PckFileExposer(string filename)
{
_filename = filename;
var file = new File();
var fileErrorCode = file.Open(filename, (int) File.ModeFlags.ReadWrite);
if(fileErrorCode != Error.Ok)
{
throw new FileLoadException($"There was a problem loading ({filename}) with error code ({fileErrorCode.ToString()})");
}
// store data
_data = file.GetBuffer(file.GetLen());
// close file
file.Close();
CreateTempDataFile();
}
public void DeleteTempDataFile()
{
var filename = GetDataPath();
if(System.IO.File.Exists(filename))
{
System.IO.File.Delete(filename);
}
}
public string GetDataPath()
{
return $"{GetTempDataDir()}/{_tmpDataFilename}";
}
private void CreateTempDataFile()
{
// NOTE: need to implement your own temp name code here
_tmpDataFilename = Md5.Hash(_filename + Time.NowTimestamp().ToString());
// attempt to create dir if not exist
var dir = GetTempDataDir();
if(!System.IO.Directory.Exists(dir))
{
System.IO.Directory.CreateDirectory(dir);
}
// attempt to create file
System.IO.File.WriteAllBytes(GetDataPath(), _data);
}
private string GetTempDataDir()
{
return $"{OS.GetUserDataDir()}/Tmp";
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public virtual void Dispose(bool disposing)
{
if(_disposed) return;
if(disposing)
{
DeleteTempDataFile();
}
_disposed = true;
}
~PckFileExposer() => Dispose(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment