Skip to content

Commit a4246a8

Browse files
committed
feat: Re-add obfuscation and make nice pretty print
1 parent 334c7ce commit a4246a8

File tree

6 files changed

+91
-17
lines changed

6 files changed

+91
-17
lines changed

Runtime/Client/LootLockerJson.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static class LootLockerJsonSettings
2323
};
2424
#else
2525
public static readonly JsonOptions Default = new JsonOptions((JsonSerializationOptions.Default | JsonSerializationOptions.EnumAsText) & ~JsonSerializationOptions.SkipGetOnly);
26+
public static readonly JsonOptions Indented = new JsonOptions((JsonSerializationOptions.Default | JsonSerializationOptions.EnumAsText) & ~JsonSerializationOptions.SkipGetOnly);
2627
#endif
2728
}
2829

@@ -91,8 +92,12 @@ public static string PrettifyJsonString(string json)
9192
try
9293
{
9394
var parsedJson = DeserializeObject<object>(json);
94-
var tempSettings = LootLockerJsonSettings.Default;
95-
tempSettings.Formatting = Formatting.Indented;
95+
var tempSettings = new JsonSerializerSettings
96+
{
97+
ContractResolver = LootLockerJsonSettings.Default.ContractResolver,
98+
Converters = LootLockerJsonSettings.Default.Converters,
99+
Formatting = Formatting.Indented
100+
};
96101
return SerializeObject(parsedJson, tempSettings);
97102
}
98103
catch (Exception)
@@ -163,7 +168,9 @@ public static string PrettifyJsonString(string json)
163168
try
164169
{
165170
var parsedJson = DeserializeObject<object>(json);
166-
return Json.SerializeFormatted(parsedJson, LootLockerJsonSettings.Default);
171+
var indentedOptions = LootLockerJsonSettings.Default.Clone();
172+
indentedOptions.FormattingTab = " ";
173+
return Json.SerializeFormatted(parsedJson, indentedOptions);
167174
}
168175
catch (Exception)
169176
{

Runtime/Client/LootLockerResponse.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public class LootLockerResponseFactory
102102
return new T()
103103
{
104104
success = true,
105-
text = LootLockerConfig.current.prettifyJson ? LootLockerJson.PrettifyJsonString(responseBody) : responseBody,
105+
text = responseBody,
106106
statusCode = statusCode,
107107
errorData = null,
108108
requestContext = new LootLockerRequestContext(forPlayerWithUlid, requestTime)
@@ -117,7 +117,7 @@ public class LootLockerResponseFactory
117117
return new T()
118118
{
119119
success = false,
120-
text = LootLockerConfig.current.prettifyJson ? LootLockerJson.PrettifyJsonString(responseBody) : responseBody,
120+
text = responseBody,
121121
statusCode = statusCode,
122122
errorData = null,
123123
requestContext = new LootLockerRequestContext(forPlayerWithUlid, requestTime)
@@ -132,7 +132,7 @@ public class LootLockerResponseFactory
132132
return new T()
133133
{
134134
success = failureResponse.success,
135-
text = LootLockerConfig.current.prettifyJson ? LootLockerJson.PrettifyJsonString(failureResponse.text) : failureResponse.text,
135+
text = failureResponse.text,
136136
statusCode = failureResponse.statusCode,
137137
errorData = failureResponse.errorData,
138138
requestContext = failureResponse.requestContext

Runtime/Editor/LogViewer/LootLockerLogViewerUI.cs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,23 @@ private void ExportLogs()
216216
if (!string.IsNullOrEmpty(http.RequestBody))
217217
{
218218
sb.AppendLine("Request Body:");
219-
sb.AppendLine(http.RequestBody);
219+
sb.AppendLine(
220+
LootLockerConfig.current.obfuscateLogs ?
221+
LootLockerObfuscator.ObfuscateJsonStringForLogging(http.RequestBody) :
222+
http.RequestBody
223+
);
220224
}
221225
sb.AppendLine("Response Headers:");
222226
foreach (var h in http.ResponseHeaders ?? new Dictionary<string, string>())
223227
sb.AppendLine($" {h.Key}: {h.Value}");
224228
if (!string.IsNullOrEmpty(http.Response?.text))
225229
{
226230
sb.AppendLine("Response Body:");
227-
sb.AppendLine(http.Response.text);
231+
sb.AppendLine(
232+
LootLockerConfig.current.obfuscateLogs ?
233+
LootLockerObfuscator.ObfuscateJsonStringForLogging(http.Response.text) :
234+
http.Response.text
235+
);
228236
}
229237
writer.Write(sb.ToString());
230238
}
@@ -401,25 +409,55 @@ private void AddHttpLogEntryToUI(LootLockerLogger.LootLockerHttpLogEntry entry)
401409
var reqHeaders = new TextField { value = $"Request Headers: {FormatHeaders(entry.RequestHeaders)}", isReadOnly = true };
402410
reqHeaders.AddToClassList("log-message-field");
403411
details.Add(reqHeaders);
412+
404413
if (!string.IsNullOrEmpty(entry.RequestBody))
405414
{
406-
var reqBody = new TextField { value = $"Request Body: {entry.RequestBody}", isReadOnly = true };
407-
reqBody.AddToClassList("log-message-field");
408-
details.Add(reqBody);
415+
var requestBodyFoldout = CreateJsonFoldout("Request Body", entry.RequestBody);
416+
details.Add(requestBodyFoldout);
409417
}
418+
410419
var respHeaders = new TextField { value = $"Response Headers: {FormatHeaders(entry.ResponseHeaders)}", isReadOnly = true };
411420
respHeaders.AddToClassList("log-message-field");
412421
details.Add(respHeaders);
422+
413423
if (!string.IsNullOrEmpty(entry.Response?.text))
414424
{
415-
var respBody = new TextField { value = $"Response Body: {entry.Response.text}", isReadOnly = true };
416-
respBody.AddToClassList("log-message-field");
417-
details.Add(respBody);
425+
var responseBodyFoldout = CreateJsonFoldout("Response Body", entry.Response.text);
426+
details.Add(responseBodyFoldout);
418427
}
428+
419429
foldout.Add(details);
420430
logContainer.Add(foldout);
421431
}
422432

433+
private Foldout CreateJsonFoldout(string title, string jsonContent)
434+
{
435+
// Initially show minified JSON (obfuscated if configured)
436+
var obfuscatedJson = LootLockerConfig.current.obfuscateLogs
437+
? LootLockerObfuscator.ObfuscateJsonStringForLogging(jsonContent)
438+
: jsonContent;
439+
var prettifiedJson = LootLockerJson.PrettifyJsonString(obfuscatedJson);
440+
var collapsedTitle = $"{title}: {obfuscatedJson}";
441+
442+
var foldout = new Foldout { text = collapsedTitle, value = false };
443+
444+
// Create a container for the JSON content
445+
var jsonContainer = new VisualElement();
446+
447+
var jsonField = new TextField { value = prettifiedJson, isReadOnly = true, multiline = true };
448+
jsonField.AddToClassList("log-message-field");
449+
jsonField.style.whiteSpace = WhiteSpace.PreWrap;
450+
jsonContainer.Add(jsonField);
451+
452+
foldout.RegisterValueChangedCallback(evt =>
453+
{
454+
foldout.text = evt.newValue ? title : collapsedTitle;
455+
});
456+
457+
foldout.Add(jsonContainer);
458+
return foldout;
459+
}
460+
423461
private string FormatHeaders(Dictionary<string, string> headers)
424462
{
425463
if (headers == null) return "";

Runtime/Editor/ProjectSettings.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ private void DrawGameSettings()
159159
}
160160
EditorGUILayout.Space();
161161

162+
EditorGUI.BeginChangeCheck();
163+
EditorGUILayout.PropertyField(m_CustomSettings.FindProperty("prettifyJson"), new GUIContent("Log JSON Formatted"));
164+
165+
if (EditorGUI.EndChangeCheck())
166+
{
167+
gameSettings.prettifyJson = m_CustomSettings.FindProperty("prettifyJson").boolValue;
168+
}
169+
EditorGUILayout.Space();
170+
162171
EditorGUI.BeginChangeCheck();
163172
EditorGUILayout.PropertyField(m_CustomSettings.FindProperty("allowTokenRefresh"));
164173

Runtime/Game/LootLockerLogger.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,15 @@ public static void LogHttpRequestResponse(LootLockerHttpLogEntry entry)
245245
if (!string.IsNullOrEmpty(entry.RequestBody))
246246
{
247247
sb.AppendLine("Request Body:");
248-
sb.AppendLine(entry.RequestBody);
248+
sb.AppendLine(ObfuscateAndPrettifyJsonIfConfigured(entry.RequestBody));
249249
}
250250
sb.AppendLine("Response Headers:");
251251
foreach (var h in entry.ResponseHeaders ?? new Dictionary<string, string>())
252252
sb.AppendLine($" {h.Key}: {h.Value}");
253253
if (!string.IsNullOrEmpty(entry.Response?.text))
254254
{
255255
sb.AppendLine("Response Body:");
256-
sb.AppendLine(entry.Response.text);
256+
sb.AppendLine(ObfuscateAndPrettifyJsonIfConfigured(entry.Response.text));
257257
}
258258

259259
LogLevel level = entry.Response?.success ?? false ? LogLevel.Verbose : LogLevel.Error;
@@ -302,6 +302,19 @@ private void ReplayHttpLogRecord(ILootLockerHttpLogListener listener)
302302
}
303303
}
304304
}
305+
306+
private static string ObfuscateAndPrettifyJsonIfConfigured(string json)
307+
{
308+
if (LootLockerConfig.current.obfuscateLogs)
309+
{
310+
json = LootLockerObfuscator.ObfuscateJsonStringForLogging(json);
311+
}
312+
if (LootLockerConfig.current.prettifyJson)
313+
{
314+
json = LootLockerJson.PrettifyJsonString(json);
315+
}
316+
return json;
317+
}
305318
}
306319

307320
public interface LootLockerLogListener

Runtime/Game/Resources/LootLockerConfig.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,14 @@ static void ListInstalledPackagesRequestProgress()
173173
}
174174
#endif
175175

176-
public static bool CreateNewSettings(string apiKey, string gameVersion, string domainKey, LootLockerLogger.LogLevel logLevel = LootLockerLogger.LogLevel.Info, bool logInBuilds = false, bool errorsAsWarnings = false, bool allowTokenRefresh = false)
176+
public static bool CreateNewSettings(string apiKey, string gameVersion, string domainKey, LootLockerLogger.LogLevel logLevel = LootLockerLogger.LogLevel.Info, bool logInBuilds = false, bool errorsAsWarnings = false, bool allowTokenRefresh = false, bool prettifyJson = false)
177177
{
178178
_current = Get();
179179

180180
_current.apiKey = apiKey;
181181
_current.game_version = gameVersion;
182182
_current.logLevel = logLevel;
183+
_current.prettifyJson = prettifyJson;
183184
_current.logInBuilds = logInBuilds;
184185
_current.logErrorsAsWarnings = errorsAsWarnings;
185186
_current.allowTokenRefresh = allowTokenRefresh;
@@ -199,6 +200,10 @@ public static bool ClearSettings()
199200
_current.apiKey = null;
200201
_current.game_version = null;
201202
_current.logLevel = LootLockerLogger.LogLevel.Info;
203+
_current.prettifyJson = false;
204+
_current.logInBuilds = false;
205+
_current.logErrorsAsWarnings = false;
206+
_current.obfuscateLogs = true;
202207
_current.allowTokenRefresh = true;
203208
_current.domainKey = null;
204209
#if UNITY_EDITOR
@@ -274,7 +279,9 @@ public static bool IsTargetingProductionEnvironment()
274279
[HideInInspector] public string baseUrl = UrlProtocol + GetUrlCore();
275280
[HideInInspector] public float clientSideRequestTimeOut = 180f;
276281
public LootLockerLogger.LogLevel logLevel = LootLockerLogger.LogLevel.Info;
282+
// Write JSON in a pretty and indented format when logging
277283
public bool prettifyJson = true;
284+
[HideInInspector] public bool obfuscateLogs = true;
278285
public bool logErrorsAsWarnings = false;
279286
public bool logInBuilds = false;
280287
public bool allowTokenRefresh = true;

0 commit comments

Comments
 (0)