Files
sbox-public/engine/Sandbox.Engine/Resources/Compiling/ResourceCompileContext.cs
Lorenz Junglas e58dfcf938 Raw File Dependencies for Resources (#3988)
### Problem
NavMesh baking needed to store raw binary data in external files (`.navdata`) that should be tracked and packaged, but aren't native compiled resources.

### Solution
Reused the existing `RegisterAdditionalRelatedFile_Game` native infrastructure to track raw files similar to how `RegisterAdditionalRelatedFile_Content` is used to track content sources (.fbx, .tga...). `RegisterAdditionalRelatedFile_Game` was not used anywhere in the codebase, so it's safe to repurpose. This avoids changing the asset file format.

### Changes

**C# Asset System:**
- Split `GetAdditionalRelatedFiles()` into:
  - `GetAdditionalContentFiles()` - content-side files (.fbx, .lxo, .rect, etc.) filtered by `ASSET_LOCATION_CONTENT`
  - `GetAdditionalGameFiles()` - game-side data files (.navdata, etc.) filtered by `ASSET_LOCATION_GAME`

**Resource Compilation:**
- Added `FileReference` struct for JSON serialization with `{ "$reference_type: "filereference", "path": "..." }` pattern
- Added `AddGameFileReference()` to `ResourceCompileContext` which calls `RegisterAdditionalRelatedFile_Game()`

**Packaging:**
- `PackageManifest` now additionally collects game/rawfiles files for publishing (content files are still only included when publishing source)
2026-02-04 09:24:33 +01:00

116 lines
2.9 KiB
C#

using System.Text.Json.Nodes;
namespace Sandbox.Resources;
public abstract class ResourceCompileContext
{
/// <summary>
/// The absolute path to the resource on disk
/// </summary>
public abstract string AbsolutePath { get; }
/// <summary>
/// The path relative to the assets folder
/// </summary>
public abstract string RelativePath { get; }
/// <summary>
/// The resource version can be important
/// </summary>
public abstract int ResourceVersion { get; set; }
internal abstract int WriteBlock( string blockName, IntPtr data, int count );
/// <summary>
/// Add a reference. This means that the resource we're compiling depends on this resource.
/// </summary>
public abstract void AddRuntimeReference( string path );
/// <summary>
/// Add a reference that is needed to compile this resource, but isn't actually needed once compiled.
/// </summary>
public abstract void AddCompileReference( string path );
/// <summary>
/// Add a game file reference. This file will be included in packages but is not a native resource.
/// Use this for arbitrary data files that are loaded by managed code (e.g. navdata files).
/// </summary>
public abstract void AddGameFileReference( string path );
/// <summary>
/// Get the streaming data to write to
/// </summary>
public DataStream StreamingData { get; internal set; }
/// <summary>
/// Get the data to write to
/// </summary>
public DataStream Data { get; internal set; }
/// <summary>
/// Create a child resource
/// </summary>
public abstract Child CreateChild( string absolutePath );
/// <summary>
/// Load the json and scan it for paths or any embedded resources
/// </summary>
public abstract string ScanJson( string json );
/// <summary>
/// Read the source, either from in memory, or from disk
/// </summary>
public abstract byte[] ReadSource();
/// <summary>
/// Read the source, either from in memory, or from disk
/// </summary>
public string ReadSourceAsString()
{
var data = ReadSource();
return System.Text.Encoding.UTF8.GetString( data );
}
/// <summary>
/// Read the source, either from in memory, or from disk
/// </summary>
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 );
/// <summary>
/// Write a string with a null terminator
/// </summary>
public void Write( string strValue )
{
Write( System.Text.Encoding.UTF8.GetBytes( strValue ) );
Write( new byte[] { 0 } );
}
//public abstract void SetAlignment( int v );
}
}