Utility to read a weapon prefab text by path. It first tries to get a compiled PrefabFile from the ResourceLibrary and returns a JSON object string with a RootObject property containing the serialized prefab; if that fails it falls back to reading the file text via the mounted filesystem. Returns null on invalid path or if both attempts fail.
using Sandbox;
using System;
using System.Text.Json;
namespace NZombies;
/// <summary>
/// A weapon prefab's JSON, for the code that reads numbers out of a gun without spawning it:
/// `WeaponClassStats`, `ChimeraPool` and `PublishCheck`.
///
/// ⛔ FROM THE COMPILED PREFAB, NOT THE `.prefab` TEXT (2026-10-03). The text only reached a
/// published build through the `prefabs/weapons/*.prefab` Resources glob, which shipped every gun
/// a second time: 1,070 files, against a file list that has to stay under 8 MiB (INSTRUCTIONS.md).
/// `PrefabFile.RootObject` is the JSON the prefab was read from, in the editor and in a published
/// game alike; `WallBuyManager` reads it the same way.
///
/// Returns the document in the file's own shape, `{"RootObject": …}`, so a reader that parsed the
/// file parses this unchanged. Falls back to the text through `Mounted`, which the editor still has.
/// Null when neither is there.
/// </summary>
public static class PrefabText
{
public static string Read( string path )
{
if ( string.IsNullOrWhiteSpace( path ) ) return null;
try
{
if ( ResourceLibrary.Get<PrefabFile>( path )?.RootObject is { } root )
return "{\"RootObject\":" + JsonSerializer.Serialize( root ) + "}";
}
catch ( Exception ) { }
try { return FileSystem.Mounted.ReadAllText( path ); }
catch ( Exception ) { return null; }
}
}