s&box Package Code Search

Search C# source code, UI razor templates, shaders, and configs across s&box packages.

Showing code results for query: * (3 total matches found)
badandbest.sprays / Code/Spray.Placement.cs
Game library
using Sandbox;
using Sandbox.Utility;
using System.Collections.Generic;
namespace badandbest.Sprays;

public static partial class Spray
{
	public static GameObject LocalSpray;

	/// <summary>
	/// Places an image on a surface.
	/// </summary>
	public static void Place()
	{
		const float RANGE = 128;// Range in GMOD.

		var ray = Game.ActiveScene.Camera.Transform.World.ForwardRay;
		var trace = Game.SceneTrace.Ray( ray, RANGE );

		Place( trace );
	}

	/// <summary>
	/// Places an image on a surface.
	/// </summary>
	/// <param name="trace">The trace to use.</param>
	public static void Place( SceneTrace trace )
	{
		// We only want to hit static bodies. ( maps, etc )
		if ( trace.Run() is not { Body.BodyType: PhysicsBodyType.Static } tr )
			return;

		var config = new CloneConfig
		{
			Name = $"Spray - {Steam.PersonaName}",
			Transform = new Transform( tr.HitPosition, Rotation.LookAt( tr.Normal ) ),
			PrefabVariables = new Dictionary<string, object>
			{
				{ "Image", Cookie.Get( "spray.url", "materials/decals/default.png" ) },
				{ "Placer", Steam.PersonaName },
			}
		};

		LocalSpray?.Destroy();
		LocalSpray = GameObject.Clone( "prefabs/spray.prefab", config );

		LocalSpray.NetworkSpawn(); // NetworkSpawn breaks the prefab
		LocalSpray.SetPrefabSource( "prefabs/spray.prefab" );
	}
}

[Title( "Spray Renderer" ), Icon( "imagesearch_roller" )]
internal class SprayRenderer : Renderer
{
	[Property] internal DecalRenderer _decal { get; set; }
	[Property] internal TextRenderer _text { get; set; }

	[Property, ImageAssetPath]
	public string Image { get; set; }

	[Button( "Remove Spray", Icon = "clear" )]
	public void Remove() => GameObject.Destroy();
	
	public virtual void UpdateObject()
	{
		_decal.Enabled = !Spray.DisableRendering;
		_text.Enabled = Spray.EnableDebug;
	}

	protected override async void OnAwake()
	{
		UpdateObject();

		var texture = await Texture.LoadAsync( FileSystem.Mounted, Image );

		_decal.Material = Material.Load( "materials/spray.vmat" ).CreateCopy();
		_decal.Material.Set( "g_tColor", texture );
	}
}
badandbest.sprays / Code/Spray.ConVars.cs
Game library
using Sandbox;
namespace badandbest.Sprays;

public static partial class Spray
{
	[ConVar( "spraydebug", Help = "Renders who placed a spray." ), Change( nameof( OnDirty ) )]
	internal static bool EnableDebug { get; set; }

	[ConVar( "spraydisable", Help = "Disables player sprays. Good for streamers.", Saved = true ), Change( nameof( OnDirty ) )]
	internal static bool DisableRendering { get; set; }

	private static void OnDirty( object oldValue, object newValue )
	{
		foreach ( var spray in Game.ActiveScene.GetAllComponents<SprayRenderer>() )
		{
			spray.UpdateObject();
		}
	}
}
badandbest.sprays / Code/Spray.Downloading.cs
Game library
using Sandbox;
using System;
using System.IO;
using System.Linq;
using System.Net.Http;

namespace badandbest.Sprays;

/// <summary>
/// A library to allow the placement of sprays in the world
/// </summary>
public static partial class Spray
{
	private static bool IsImage( this HttpContent content ) => content.Headers.GetValues( "Content-Type" ).Any( type => type.Contains( "image" ) );

	[ConCmd( "spray", Help = "URL of image. Must be in quotes." )]
	internal static async void SetImage( string imageUrl )
	{
		try
		{
			var uri = new Uri( imageUrl );
			var response = await Http.RequestAsync( uri.AbsoluteUri );
			
			if ( !response.Content.IsImage() )
			{
				throw new FileNotFoundException("Not an image type: Sites like Tenor require you to Right click > Copy image address");
			}
		}
		catch ( Exception e )
		{
			Log.Warning( e );
			imageUrl = "materials/fallback.vtex";
		}
		finally
		{
			Cookie.Set( "spray.url", imageUrl );
		}
	}
}
Debug: View Raw JSON Response
{
    "TotalCount": 3,
    "Files": [
        {
            "Ident": "badandbest.sprays",
            "Path": "Code/Spray.Placement.cs",
            "FileName": "Spray.Placement.cs",
            "PackageType": "library",
            "CodeKind": "Game",
            "AssetVersionId": 61422,
            "IsPrivate": false,
            "Code": "using Sandbox;\r\nusing Sandbox.Utility;\r\nusing System.Collections.Generic;\r\nnamespace badandbest.Sprays;\r\n\r\npublic static partial class Spray\r\n{\r\n\tpublic static GameObject LocalSpray;\r\n\r\n\t/// <summary>\r\n\t/// Places an image on a surface.\r\n\t/// </summary>\r\n\tpublic static void Place()\r\n\t{\r\n\t\tconst float RANGE = 128;// Range in GMOD.\r\n\r\n\t\tvar ray = Game.ActiveScene.Camera.Transform.World.ForwardRay;\r\n\t\tvar trace = Game.SceneTrace.Ray( ray, RANGE );\r\n\r\n\t\tPlace( trace );\r\n\t}\r\n\r\n\t/// <summary>\r\n\t/// Places an image on a surface.\r\n\t/// </summary>\r\n\t/// <param name=\"trace\">The trace to use.</param>\r\n\tpublic static void Place( SceneTrace trace )\r\n\t{\r\n\t\t// We only want to hit static bodies. ( maps, etc )\r\n\t\tif ( trace.Run() is not { Body.BodyType: PhysicsBodyType.Static } tr )\r\n\t\t\treturn;\r\n\r\n\t\tvar config = new CloneConfig\r\n\t\t{\r\n\t\t\tName = $\"Spray - {Steam.PersonaName}\",\r\n\t\t\tTransform = new Transform( tr.HitPosition, Rotation.LookAt( tr.Normal ) ),\r\n\t\t\tPrefabVariables = new Dictionary<string, object>\r\n\t\t\t{\r\n\t\t\t\t{ \"Image\", Cookie.Get( \"spray.url\", \"materials/decals/default.png\" ) },\r\n\t\t\t\t{ \"Placer\", Steam.PersonaName },\r\n\t\t\t}\r\n\t\t};\r\n\r\n\t\tLocalSpray?.Destroy();\r\n\t\tLocalSpray = GameObject.Clone( \"prefabs/spray.prefab\", config );\r\n\r\n\t\tLocalSpray.NetworkSpawn(); // NetworkSpawn breaks the prefab\r\n\t\tLocalSpray.SetPrefabSource( \"prefabs/spray.prefab\" );\r\n\t}\r\n}\r\n\r\n[Title( \"Spray Renderer\" ), Icon( \"imagesearch_roller\" )]\r\ninternal class SprayRenderer : Renderer\r\n{\r\n\t[Property] internal DecalRenderer _decal { get; set; }\r\n\t[Property] internal TextRenderer _text { get; set; }\r\n\r\n\t[Property, ImageAssetPath]\r\n\tpublic string Image { get; set; }\r\n\r\n\t[Button( \"Remove Spray\", Icon = \"clear\" )]\r\n\tpublic void Remove() => GameObject.Destroy();\r\n\t\r\n\tpublic virtual void UpdateObject()\r\n\t{\r\n\t\t_decal.Enabled = !Spray.DisableRendering;\r\n\t\t_text.Enabled = Spray.EnableDebug;\r\n\t}\r\n\r\n\tprotected override async void OnAwake()\r\n\t{\r\n\t\tUpdateObject();\r\n\r\n\t\tvar texture = await Texture.LoadAsync( FileSystem.Mounted, Image );\r\n\r\n\t\t_decal.Material = Material.Load( \"materials/spray.vmat\" ).CreateCopy();\r\n\t\t_decal.Material.Set( \"g_tColor\", texture );\r\n\t}\r\n}\r\n"
        },
        {
            "Ident": "badandbest.sprays",
            "Path": "Code/Spray.ConVars.cs",
            "FileName": "Spray.ConVars.cs",
            "PackageType": "library",
            "CodeKind": "Game",
            "AssetVersionId": 61422,
            "IsPrivate": false,
            "Code": "using Sandbox;\r\nnamespace badandbest.Sprays;\r\n\r\npublic static partial class Spray\r\n{\r\n\t[ConVar( \"spraydebug\", Help = \"Renders who placed a spray.\" ), Change( nameof( OnDirty ) )]\r\n\tinternal static bool EnableDebug { get; set; }\r\n\r\n\t[ConVar( \"spraydisable\", Help = \"Disables player sprays. Good for streamers.\", Saved = true ), Change( nameof( OnDirty ) )]\r\n\tinternal static bool DisableRendering { get; set; }\r\n\r\n\tprivate static void OnDirty( object oldValue, object newValue )\r\n\t{\r\n\t\tforeach ( var spray in Game.ActiveScene.GetAllComponents<SprayRenderer>() )\r\n\t\t{\r\n\t\t\tspray.UpdateObject();\r\n\t\t}\r\n\t}\r\n}\r\n"
        },
        {
            "Ident": "badandbest.sprays",
            "Path": "Code/Spray.Downloading.cs",
            "FileName": "Spray.Downloading.cs",
            "PackageType": "library",
            "CodeKind": "Game",
            "AssetVersionId": 61422,
            "IsPrivate": false,
            "Code": "using Sandbox;\r\nusing System;\r\nusing System.IO;\r\nusing System.Linq;\r\nusing System.Net.Http;\r\n\r\nnamespace badandbest.Sprays;\r\n\r\n/// <summary>\r\n/// A library to allow the placement of sprays in the world\r\n/// </summary>\r\npublic static partial class Spray\r\n{\r\n\tprivate static bool IsImage( this HttpContent content ) => content.Headers.GetValues( \"Content-Type\" ).Any( type => type.Contains( \"image\" ) );\r\n\r\n\t[ConCmd( \"spray\", Help = \"URL of image. Must be in quotes.\" )]\r\n\tinternal static async void SetImage( string imageUrl )\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\tvar uri = new Uri( imageUrl );\r\n\t\t\tvar response = await Http.RequestAsync( uri.AbsoluteUri );\r\n\t\t\t\r\n\t\t\tif ( !response.Content.IsImage() )\r\n\t\t\t{\r\n\t\t\t\tthrow new FileNotFoundException(\"Not an image type: Sites like Tenor require you to Right click > Copy image address\");\r\n\t\t\t}\r\n\t\t}\r\n\t\tcatch ( Exception e )\r\n\t\t{\r\n\t\t\tLog.Warning( e );\r\n\t\t\timageUrl = \"materials/fallback.vtex\";\r\n\t\t}\r\n\t\tfinally\r\n\t\t{\r\n\t\t\tCookie.Set( \"spray.url\", imageUrl );\r\n\t\t}\r\n\t}\r\n}\r\n"
        }
    ]
}