Files
sbox-public/engine/Sandbox.Tools/Assets/GameResourceProcessor.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

53 lines
1.5 KiB
C#

using System;
using System.Text.Json.Nodes;
namespace Editor;
/// <summary>
/// When saving a game resource, we inject information about packages used into it. We do that
/// here in this special callback because GameResource is in engine.dll, so we can't do it in Serialize()
/// </summary>
internal static class GameResourceProcessor
{
public static void Initialize()
{
GameResource.ProcessSerializedObject += ProcessGameResource;
}
internal static IEnumerable<string> GetCloudReferences( JsonObject jso )
{
HashSet<string> cloudPackages = new HashSet<string>( StringComparer.OrdinalIgnoreCase );
Sandbox.Json.WalkJsonTree( jso, ( k, v ) =>
{
if ( !v.TryGetValue<string>( out var path ) )
return v;
// not a path if it has any of these characters in it
if ( path.Contains( ',' ) ) return v;
if ( path.Contains( ':' ) ) return v;
if ( path.Contains( '\"' ) ) return v;
if ( path.Contains( '\'' ) ) return v;
var lastPeriod = path.LastIndexOf( '.' );
if ( lastPeriod < 0 ) return v;
if ( lastPeriod < path.Length - 9 ) return v;
var asset = AssetSystem.FindByPath( path );
if ( asset is null ) return v;
if ( asset.Package is null ) return v;
cloudPackages.Add( asset.Package.GetIdent( false, true ) );
return v;
} );
return cloudPackages.OrderBy( x => x );
}
private static void ProcessGameResource( object target, JsonObject jso )
{
jso["__references"] = JsonValue.Create( GetCloudReferences( jso ) );
}
}