Skip to content

Commit a0790bf

Browse files
Tryhard999ads102003
authored andcommitted
Update CuiHelper serialization (#561)
1 parent bfe3314 commit a0790bf

1 file changed

Lines changed: 61 additions & 10 deletions

File tree

src/RustCui.cs

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,78 @@
1-
extern alias References;
1+
extern alias References;
22

33
using Oxide.Core;
44
using References::Newtonsoft.Json;
55
using References::Newtonsoft.Json.Converters;
66
using References::Newtonsoft.Json.Linq;
77
using System;
88
using System.Collections.Generic;
9+
using System.Globalization;
10+
using System.IO;
11+
using System.Text;
12+
using Facepunch;
913
using UnityEngine;
1014
using UnityEngine.UI;
1115

1216
namespace 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+
private readonly ArrayPool<T> _pool = new ArrayPool<T>(50);
22+
public T[] Rent(int minimumLength) => _pool.Rent(minimumLength);
23+
public void Return(T[] array) => _pool.Return(array);
24+
}
25+
1426
public static class CuiHelper
1527
{
16-
public static string ToJson(List<CuiElement> elements, bool format = false)
28+
private static readonly StringBuilder sb = new StringBuilder(64 * 1024);
29+
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
1730
{
18-
return JsonConvert.SerializeObject(elements, format ? Formatting.Indented : Formatting.None, new JsonSerializerSettings
19-
{
20-
DefaultValueHandling = DefaultValueHandling.Ignore
21-
}).Replace("\\n", "\n");
31+
DefaultValueHandling = DefaultValueHandling.Ignore,
32+
NullValueHandling = NullValueHandling.Ignore,
33+
DateParseHandling = DateParseHandling.None,
34+
FloatFormatHandling = FloatFormatHandling.Symbol,
35+
StringEscapeHandling = StringEscapeHandling.Default
36+
};
37+
38+
private static readonly JsonSerializer _serializer = JsonSerializer.Create(Settings);
39+
private static readonly StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture);
40+
private static readonly JsonTextWriter jw = new JsonTextWriter(sw)
41+
{
42+
Formatting = Formatting.None,
43+
ArrayPool = JsonArrayPool<char>.Shared,
44+
CloseOutput = false
45+
};
46+
private static readonly JsonTextWriter jwFormated = new JsonTextWriter(sw)
47+
{
48+
Formatting = Formatting.Indented,
49+
ArrayPool = JsonArrayPool<char>.Shared,
50+
CloseOutput = false
51+
};
52+
53+
public static string ToJson(IReadOnlyList<CuiElement> elements, bool format = false)
54+
{
55+
sb.Clear();
56+
var writer = format ? jwFormated : jw;
57+
_serializer.Serialize(writer, elements);
58+
var json = sb.ToString().Replace("\\n", "\n");
59+
return json;
2260
}
2361

2462
public static List<CuiElement> FromJson(string json) => JsonConvert.DeserializeObject<List<CuiElement>>(json);
2563

26-
public static string GetGuid() => Guid.NewGuid().ToString().Replace("-", string.Empty);
64+
public static string GetGuid() => Guid.NewGuid().ToString("N");
2765

28-
public static bool AddUi(BasePlayer player, List<CuiElement> elements) => AddUi(player, ToJson(elements));
66+
public static bool AddUi(BasePlayer player, List<CuiElement> elements)
67+
{
68+
if (player?.net == null)
69+
{
70+
return false;
71+
}
2972

73+
return AddUi(player, ToJson(elements));
74+
}
75+
3076
public static bool AddUi(BasePlayer player, string json)
3177
{
3278
if (player?.net != null && Interface.CallHook("CanUseUI", player, json) == null)
@@ -52,7 +98,12 @@ public static bool DestroyUi(BasePlayer player, string elem)
5298

5399
public static void SetColor(this ICuiColor elem, Color color)
54100
{
55-
elem.Color = $"{color.r} {color.g} {color.b} {color.a}";
101+
sb.Clear();
102+
sb.Append(color.r).Append(' ')
103+
.Append(color.g).Append(' ')
104+
.Append(color.b).Append(' ')
105+
.Append(color.a);
106+
elem.Color = sb.ToString();
56107
}
57108

58109
public static Color GetColor(this ICuiColor elem) => ColorEx.Parse(elem.Color);
@@ -299,7 +350,7 @@ public class CuiRawImageComponent : ICuiComponent, ICuiColor
299350

300351
[JsonProperty("png")]
301352
public string Png { get; set; }
302-
353+
303354
[JsonProperty("steamid")]
304355
public string SteamId { get; set; }
305356

0 commit comments

Comments
 (0)