using System.Text.Json.Nodes; namespace Sandbox.Resources; public abstract class ResourceCompileContext { /// /// The absolute path to the resource on disk /// public abstract string AbsolutePath { get; } /// /// The path relative to the assets folder /// public abstract string RelativePath { get; } /// /// The resource version can be important /// public abstract int ResourceVersion { get; set; } internal abstract int WriteBlock( string blockName, IntPtr data, int count ); /// /// Add a reference. This means that the resource we're compiling depends on this resource. /// public abstract void AddRuntimeReference( string path ); /// /// Add a reference that is needed to compile this resource, but isn't actually needed once compiled. /// public abstract void AddCompileReference( string path ); /// /// Get the streaming data to write to /// public DataStream StreamingData { get; internal set; } /// /// Get the data to write to /// public DataStream Data { get; internal set; } /// /// Create a child resource /// public abstract Child CreateChild( string absolutePath ); /// /// Load the json and scan it for paths or any embedded resources /// public abstract string ScanJson( string json ); /// /// Read the source, either from in memory, or from disk /// public abstract byte[] ReadSource(); /// /// Read the source, either from in memory, or from disk /// public string ReadSourceAsString() { var data = ReadSource(); return System.Text.Encoding.UTF8.GetString( data ); } /// /// Read the source, either from in memory, or from disk /// public JsonObject ReadSourceAsJson() { try { var jsonString = ReadSourceAsString(); if ( string.IsNullOrWhiteSpace( jsonString ) ) return null; return Json.ParseToJsonObject( jsonString ); } catch { return null; } } public abstract class Child { public abstract bool Compile(); public abstract void SetInputData( string data ); } public abstract class DataStream { internal readonly ResourceCompileContext source; public abstract void Write( byte[] bytes ); /// /// Write a string with a null terminator /// public void Write( string strValue ) { Write( System.Text.Encoding.UTF8.GetBytes( strValue ) ); Write( new byte[] { 0 } ); } //public abstract void SetAlignment( int v ); } }