Lua API bindings for player clothing and appearance. Exposes outfit, skin tone and body height setters/getters on the player object and a `clothing` library for listing and validating .clothing resources.
using System;
using System.Collections.Generic;
using System.Linq;
using RoleplayFramework.Lua;
namespace RoleplayFramework.Scripting;
/// <summary>
/// Player clothing and skin (citizen model only).
///
/// Player methods (setters: server):
/// ply:SetOutfit({ "models/citizen_clothes/hat/fedora/fedora.clothing", { "…/hoodie.clothing", 0.4 }, ... } | nil)
/// nil = the player's own Steam avatar outfit; {} = nothing but underwear. A tint (0-1) is optional per item.
/// ply:GetOutfit() → list of paths, or nil when wearing their own avatar
/// ply:AddClothing(path [, tint]) ply:RemoveClothing(path) ply:ClearClothing() ply:ResetClothing()
/// ply:SetSkinTone(0-1 | nil) ply:GetSkinTone() ply:SetBodyHeight(0-1 | nil) ply:GetBodyHeight()
/// Library (both realms):
/// clothing.GetAll([category]) → { { path, title, subtitle, category, tintable }, ... }
/// clothing.GetCategories() clothing.IsValid(path) clothing.GetTitle(path)
/// Items are .clothing paths (204 ship with s&box under models/citizen_clothes/) or mounted package idents.
/// </summary>
public static class LuaClothing
{
public const int MaxItems = 32;
/// <summary>Path or package ident → a .clothing path that exists, or a Lua error.</summary>
public static string Resolve( string raw, string fname )
{
var path = LuaAppearance.ResolvePath( raw, ".clothing", fname );
if ( ResourceLibrary.Get<Clothing>( path ) is null ) throw new LuaRuntimeException( $"{fname}: clothing not found: {path}" );
return path;
}
static List<(string Path, float? Tint)> Parse( string outfit ) =>
(outfit ?? "").Split( ';', StringSplitOptions.RemoveEmptyEntries ).Where( p => p != "-" ).Select( p =>
{
var at = p.IndexOf( '@' );
float? tint = at > 0 && float.TryParse( p.Substring( at + 1 ), System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var t ) ? t : null;
return (at > 0 ? p.Substring( 0, at ) : p, tint);
} ).ToList();
static string Serialize( List<(string Path, float? Tint)> items ) =>
items.Count == 0 ? "-" : string.Join( ";", items.Select( i => i.Tint is { } t ? $"{i.Path}@{t.ToString( "0.###", System.Globalization.CultureInfo.InvariantCulture )}" : i.Path ) );
public static void AddPlayerMethods( LuaTable methods, Func<LuaValue[], string, GameObject> resolve, bool server )
{
RpAppearance W( LuaValue[] a, string name )
{
if ( !server ) throw new LuaRuntimeException( $"{name} can only be called on the server" );
return RpAppearance.Ensure( resolve( a, name ) );
}
RpAppearance R( LuaValue[] a, string name ) => resolve( a, name ).Components.Get<RpAppearance>();
LuaState.Register( methods, "SetOutfit", ( s, a ) =>
{
var look = W( a, "SetOutfit" );
var arg = LuaState.Arg( a, 1 );
if ( arg.IsNil ) { look.Outfit = ""; return null; }
if ( !arg.IsTable ) throw LuaState.ArgError( 1, "SetOutfit", "table of clothing paths (or nil for the player's own avatar) expected" );
var items = new List<(string, float?)>();
foreach ( var (_, v) in arg.AsTable.Pairs() )
{
if ( items.Count >= MaxItems ) throw new LuaRuntimeException( $"SetOutfit: at most {MaxItems} items" );
if ( v.IsString ) items.Add( (Resolve( v.AsString, "SetOutfit" ), null) );
else if ( v.IsTable && v.AsTable.Get( 1 ).IsString )
{
float? tint = LuaState.ToNumber( v.AsTable.Get( 2 ), out var t ) ? (float)Math.Clamp( t, 0, 1 ) : null;
items.Add( (Resolve( v.AsTable.Get( 1 ).AsString, "SetOutfit" ), tint) );
}
else throw LuaState.ArgError( 1, "SetOutfit", "items must be paths or { path, tint }" );
}
look.Outfit = Serialize( items );
return null;
} );
LuaState.Register( methods, "GetOutfit", ( s, a ) =>
{
var outfit = R( a, "GetOutfit" )?.Outfit ?? "";
if ( outfit == "" ) return new[] { LuaValue.Nil };
var t = new LuaTable();
foreach ( var (path, _) in Parse( outfit ) ) t.Append( path );
return new LuaValue[] { t };
} );
LuaState.Register( methods, "AddClothing", ( s, a ) =>
{
var look = W( a, "AddClothing" );
var path = Resolve( LuaState.CheckString( a, 1, "AddClothing" ), "AddClothing" );
var items = Parse( look.Outfit ).Where( i => i.Path != path ).ToList();
if ( items.Count >= MaxItems ) throw new LuaRuntimeException( $"AddClothing: at most {MaxItems} items" );
float? tint = LuaState.ToNumber( LuaState.Arg( a, 2 ), out var tt ) ? (float)Math.Clamp( tt, 0, 1 ) : null;
items.Add( (path, tint) );
look.Outfit = Serialize( items );
return null;
} );
LuaState.Register( methods, "RemoveClothing", ( s, a ) =>
{
var look = W( a, "RemoveClothing" );
var path = LuaState.CheckString( a, 1, "RemoveClothing" ).Trim().ToLowerInvariant();
look.Outfit = Serialize( Parse( look.Outfit ).Where( i => i.Path != path ).ToList() );
return null;
} );
LuaState.Register( methods, "ClearClothing", ( s, a ) => { W( a, "ClearClothing" ).Outfit = "-"; return null; } );
LuaState.Register( methods, "ResetClothing", ( s, a ) =>
{
var look = W( a, "ResetClothing" );
look.Outfit = ""; look.SkinTone = -1; look.BodyHeight = -1;
return null;
} );
LuaState.Register( methods, "SetSkinTone", ( s, a ) =>
{
var look = W( a, "SetSkinTone" );
look.SkinTone = LuaState.Arg( a, 1 ).IsNil ? -1 : (float)Math.Clamp( LuaState.CheckNumber( a, 1, "SetSkinTone" ), 0, 1 );
return null;
} );
LuaState.Register( methods, "GetSkinTone", ( s, a ) =>
new[] { R( a, "GetSkinTone" ) is { SkinTone: >= 0 } l ? LuaValue.FromNumber( l.SkinTone ) : LuaValue.Nil } );
LuaState.Register( methods, "SetBodyHeight", ( s, a ) =>
{
var look = W( a, "SetBodyHeight" );
look.BodyHeight = LuaState.Arg( a, 1 ).IsNil ? -1 : (float)Math.Clamp( LuaState.CheckNumber( a, 1, "SetBodyHeight" ), 0, 1 );
return null;
} );
LuaState.Register( methods, "GetBodyHeight", ( s, a ) =>
new[] { R( a, "GetBodyHeight" ) is { BodyHeight: >= 0 } l ? LuaValue.FromNumber( l.BodyHeight ) : LuaValue.Nil } );
}
public static void RegisterLibrary( LuaRealm realm )
{
var t = realm.State.GetOrCreateLibrary( "clothing" );
LuaTable Info( Clothing c )
{
var e = new LuaTable();
e.Set( "path", c.ResourcePath );
e.Set( "title", c.Title ?? "" );
e.Set( "category", c.Category.ToString() );
e.Set( "subtitle", c.Subtitle ?? "" );
e.Set( "tintable", c.AllowTintSelect );
return e;
}
LuaState.Register( t, "GetAll", ( s, a ) =>
{
var cat = LuaState.Arg( a, 0 ).IsString ? LuaState.Arg( a, 0 ).AsString : null;
var list = new LuaTable();
foreach ( var c in ResourceLibrary.GetAll<Clothing>().OrderBy( c => c.ResourcePath ) )
if ( cat is null || string.Equals( c.Category.ToString(), cat, StringComparison.OrdinalIgnoreCase ) ) list.Append( Info( c ) );
return new LuaValue[] { list };
} );
LuaState.Register( t, "GetCategories", ( s, a ) =>
{
var list = new LuaTable();
foreach ( var c in ResourceLibrary.GetAll<Clothing>().Select( c => c.Category.ToString() ).Distinct().OrderBy( x => x ) ) list.Append( c );
return new LuaValue[] { list };
} );
LuaState.Register( t, "IsValid", ( s, a ) =>
{
try { Resolve( LuaState.CheckString( a, 0, "clothing.IsValid" ), "clothing.IsValid" ); return new LuaValue[] { true }; }
catch ( LuaRuntimeException ) { return new LuaValue[] { false }; }
} );
LuaState.Register( t, "GetTitle", ( s, a ) =>
{
var c = ResourceLibrary.Get<Clothing>( LuaState.CheckString( a, 0, "clothing.GetTitle" ).Trim().ToLowerInvariant() );
return new[] { c is null ? LuaValue.Nil : LuaValue.FromString( c.Title ) };
} );
}
}