44using References ::Newtonsoft . Json ;
55using References ::Newtonsoft . Json . Converters ;
66using References ::Newtonsoft . Json . Linq ;
7+ using References ::ProtoBuf ;
78using System ;
89using System . Collections . Generic ;
10+ using System . Globalization ;
11+ using System . IO ;
12+ using System . Text ;
913using UnityEngine ;
1014using UnityEngine . UI ;
1115
1216namespace Oxide . Game . Rust . Cui
1317{
18+ public sealed class JsonArrayPool < T > : IArrayPool < T >
19+ {
20+ public static readonly JsonArrayPool < T > Shared = new JsonArrayPool < T > ( ) ;
21+ public T [ ] Rent ( int minimumLength ) => System . Buffers . ArrayPool < T > . Shared . Rent ( minimumLength ) ;
22+ public void Return ( T [ ] array ) => System . Buffers . ArrayPool < T > . Shared . Return ( array ) ;
23+ }
24+
1425 public static class CuiHelper
1526 {
16- public static string ToJson ( List < CuiElement > elements , bool format = false )
27+ private static readonly StringBuilder sb = new StringBuilder ( 64 * 1024 ) ;
28+ private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
1729 {
18- return JsonConvert . SerializeObject ( elements , format ? Formatting . Indented : Formatting . None , new JsonSerializerSettings
19- {
20- DefaultValueHandling = DefaultValueHandling . Ignore
21- } ) . Replace ( "\\ n" , "\n " ) ;
30+ DefaultValueHandling = DefaultValueHandling . Ignore ,
31+ NullValueHandling = NullValueHandling . Ignore ,
32+ DateParseHandling = DateParseHandling . None ,
33+ FloatFormatHandling = FloatFormatHandling . Symbol ,
34+ StringEscapeHandling = StringEscapeHandling . Default
35+ } ;
36+
37+ private static readonly JsonSerializer _serializer = JsonSerializer . Create ( Settings ) ;
38+ private static readonly StringWriter sw = new StringWriter ( sb , CultureInfo . InvariantCulture ) ;
39+ private static readonly JsonTextWriter jw = new JsonTextWriter ( sw )
40+ {
41+ Formatting = Formatting . None ,
42+ ArrayPool = JsonArrayPool < char > . Shared ,
43+ CloseOutput = false
44+ } ;
45+ private static readonly JsonTextWriter jwFormated = new JsonTextWriter ( sw )
46+ {
47+ Formatting = Formatting . Indented ,
48+ ArrayPool = JsonArrayPool < char > . Shared ,
49+ CloseOutput = false
50+ } ;
51+
52+ public static string ToJson ( IReadOnlyList < CuiElement > elements , bool format = false )
53+ {
54+ sb . Clear ( ) ;
55+ var writer = format ? jwFormated : jw ;
56+ _serializer . Serialize ( writer , elements ) ;
57+ var json = sb . ToString ( ) . Replace ( "\\ n" , "\n " ) ;
58+ return json ;
2259 }
2360
2461 public static List < CuiElement > FromJson ( string json ) => JsonConvert . DeserializeObject < List < CuiElement > > ( json ) ;
2562
26- public static string GetGuid ( ) => Guid . NewGuid ( ) . ToString ( ) . Replace ( "-" , string . Empty ) ;
63+ public static string GetGuid ( ) => Guid . NewGuid ( ) . ToString ( "N" ) ;
64+
65+ public static bool AddUi ( BasePlayer player , List < CuiElement > elements )
66+ {
67+ if ( player ? . net == null )
68+ return false ;
69+
70+ var json = ToJson ( elements ) ;
2771
28- public static bool AddUi ( BasePlayer player , List < CuiElement > elements ) => AddUi ( player , ToJson ( elements ) ) ;
72+ if ( Interface . CallHook ( "CanUseUI" , player , json ) != null )
73+ return false ;
74+
75+ CommunityEntity . ServerInstance . ClientRPC ( RpcTarget . Player ( "AddUI" , player . net . connection ) , json ) ;
76+ return true ;
77+ }
2978
3079 public static bool AddUi ( BasePlayer player , string json )
3180 {
@@ -52,7 +101,12 @@ public static bool DestroyUi(BasePlayer player, string elem)
52101
53102 public static void SetColor ( this ICuiColor elem , Color color )
54103 {
55- elem . Color = $ "{ color . r } { color . g } { color . b } { color . a } ";
104+ sb . Clear ( ) ;
105+ sb . Append ( color . r ) . Append ( ' ' )
106+ . Append ( color . g ) . Append ( ' ' )
107+ . Append ( color . b ) . Append ( ' ' )
108+ . Append ( color . a ) ;
109+ elem . Color = sb . ToString ( ) ;
56110 }
57111
58112 public static Color GetColor ( this ICuiColor elem ) => ColorEx . Parse ( elem . Color ) ;
@@ -299,7 +353,7 @@ public class CuiRawImageComponent : ICuiComponent, ICuiColor
299353
300354 [ JsonProperty ( "png" ) ]
301355 public string Png { get ; set ; }
302-
356+
303357 [ JsonProperty ( "steamid" ) ]
304358 public string SteamId { get ; set ; }
305359
0 commit comments