Files
sbox-public/engine/Sandbox.Engine/Resources/Compiling/ResourceCompiler.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

78 lines
1.6 KiB
C#

using System.Text.Json.Serialization;
using System.Threading;
namespace Sandbox.Resources;
/// <summary>
/// Takes the "source" of a resource and creates a compiled version. The compiled version
/// can create a number of child resources and store binary data.
/// </summary>
[Expose]
public abstract partial class ResourceCompiler
{
[JsonIgnore]
public ResourceCompileContext Context { get; private set; }
protected ResourceCompiler() { }
internal void SetContext( ResourceCompileContext context )
{
Context = context;
}
internal bool CompileEmbeddedInternal( ref EmbeddedResource json )
{
try
{
return CompileEmbedded( ref json );
}
catch ( System.Exception e )
{
Log.Error( e, "Exception when compiulnig resource" );
return false;
}
}
protected virtual bool CompileEmbedded( ref EmbeddedResource json )
{
return false;
}
internal bool CompileInternal()
{
try
{
var t = Task.Run( Compile );
while ( !t.IsCompleted )
{
Thread.Sleep( 1 );
}
return t.Result;
}
catch ( System.Exception e )
{
Log.Error( e, $"Exception when compiling resource {Context?.AbsolutePath}" );
return false;
}
}
protected abstract Task<bool> Compile();
/// <summary>
/// Mark a ResourceCompiler. This is used to identify the compiler for a specific file extension, or compiler.
/// </summary>
[AttributeUsage( AttributeTargets.Class, AllowMultiple = true )]
public sealed class ResourceIdentityAttribute : System.Attribute
{
public string Name { get; set; }
public ResourceIdentityAttribute( string name )
{
Name = name;
}
}
}