JD2022-TU1/main/phone_apps/JDCompanionApp/Assets/Scripts/ConsoleConnection/JSONParseHelper.cs

317 lines
13 KiB
C#

using System.Collections.Generic;
using LightJSON;
using UnityEngine;
namespace ConsoleConnection
{
public class JSONParseHelper : MonoBehaviour
{
#region Checks
public static bool IsUI(JSON jsonObject)
{
if (!jsonObject.HasField("__class"))
return false;
return jsonObject["__class"].AsString() == "JD_PhoneUiSetupData";
}
public static bool IsPopup(JSON jsonObject)
{
return jsonObject.GetFieldAsBool("isPopup");
}
public static bool IsEnabled(JSON jsonObject)
{
var inputSetup = jsonObject.GetField("inputSetup");
if (inputSetup == null)
return false;
return InputSetupEnabled(inputSetup);
}
public static bool InputSetupEnabled(JSON inputSetupJsonObject)
{
return inputSetupJsonObject.GetFieldAsBool("isEnabled");
}
public static bool ApplyOnPopup(JSON inputSetupJsonObject)
{
return inputSetupJsonObject.GetFieldAsBool("applyOnPopup");
}
public static bool ShowOnScreen(JSON profileJson)
{
return profileJson.GetFieldAsBool("showOnScreen");
}
public static bool IsEnableAccelerometer(string jsonString)
{
JSON jsonObject = JSON.Parse(jsonString);
if (!jsonObject.HasField("__class"))
return false;
return (jsonObject["__class"].AsString() == "JD_EnableAccelValuesSending_ConsoleCommandData");
}
public static bool IsDisableAccelerometer(string jsonString)
{
JSON jsonObject = JSON.Parse(jsonString);
if (!jsonObject.HasField("__class"))
return false;
return (jsonObject["__class"].AsString() == "JD_DisableAccelValuesSending_ConsoleCommandData");
}
#endregion
#region Getters
public static JSON GetInputSetup(JSON jsonObject)
{
return jsonObject.GetField("inputSetup");
}
public static JSON GetPositionSetup(JSON inputSetup)
{
return inputSetup.GetField("carouselPosSetup");
}
public static List<ItemAction> GetActions(JSON item)
{
List<ItemAction> actionList = new List<ItemAction>();
foreach (JSON actionDescriptor in item.GetFieldAsArray("actions"))
actionList.Add(new ItemAction(actionDescriptor));
return actionList;
}
public static JSON GetActionCommand(JSON action)
{
return JSON.Parse(action.GetFieldAsString("command"));
}
public static JSON GetActionCommand(JSON item, int actionIndex)
{
var actions = item.GetField("actions");
if ((actions != null) && actions.IsArray)
{
return GetActionCommand(actions[actionIndex]);
}
return null;
}
public static JSON GetDefaultActionCommand(JSON item)
{
return GetActionCommand(item, 0);
}
public static JSON GetSetupData(JSON jsonObject)
{
return jsonObject.GetField("setupData");
}
public static JSON GetDisabledSetup(JSON jsonObject)
{
return jsonObject.GetField("disabledSetup");
}
public static JSON GetStartButton(JSON lobbySetupJsonObject)
{
return lobbySetupJsonObject.GetField("startButton");
}
// Use this for initialization
public static List<JSON> GetCoaches(JSON lobbySetupJsonObject)
{
var coaches = lobbySetupJsonObject.GetField("coaches");
if ((coaches != null) && coaches.IsArray)
return coaches.Elements;
return null;
}
public static bool HasVisibleActions(JSON mainCarousel)
{
// Workaround for JD2017-7120
//
// The game sometimes sends a carousel with hasVisibleActions set to true,
// but with all action titles set to empty strings. In this case,
// we force the removal of the action bar.
//
var allActionsEmpty = true;
JSON jsonRows = mainCarousel.GetField("rows");
if (jsonRows != null)
{
var rows = jsonRows.Elements;
foreach (var row in rows)
{
var items = row.GetField("items").Elements;
foreach (var item in items)
{
var actions = item.GetField("actions");
if ((actions != null) && actions.IsArray)
{
foreach (JSON action in actions.Elements)
{
allActionsEmpty &= string.IsNullOrEmpty(action.GetFieldAsString("title"));
}
}
}
}
}
if (allActionsEmpty)
return false;
// normal case, use the boolean provided by the game
return mainCarousel.GetFieldAsBool("hasVisibleActions");
}
public static JSON GetChangeItemCommand(int rowIndex, int itemIndex)
{
return new JSON(new Dictionary<string, JSON> {
{"root", new Dictionary<string, JSON> {
{"__class", "ChangeItem_PhoneCommandData"},
{"rowIndex", rowIndex},
{"itemIndex", itemIndex}
}}
});
}
public static JSON GetChangeRowCommand(int rowIndex)
{
return new JSON(new Dictionary<string, JSON> {
{"root", new Dictionary<string, JSON> {
{"__class", "ChangeRow_PhoneCommandData"},
{"rowIndex", rowIndex}
}}
});
}
public static JSON GetChangeActionCommand(int rowIndex, int itemIndex, int actionIndex)
{
return new JSON(new Dictionary<string, JSON> {
{"root", new Dictionary<string, JSON> {
{"__class", "ChangeAction_PhoneCommandData"},
{"rowIndex", rowIndex},
{"itemIndex", itemIndex},
{"actionIndex", actionIndex}
}}
});
}
public static JSON GetValidateActionCommand(int rowIndex, int itemIndex, int actionIndex)
{
return new JSON(new Dictionary<string, JSON> {
{"root", new Dictionary<string, JSON> {
{"__class", "ValidateAction_PhoneCommandData"},
{"rowIndex", rowIndex},
{"itemIndex", itemIndex},
{"actionIndex", actionIndex}
}}
});
}
public static JSON GetSubmitKeyboardCommand(string searchText)
{
return new JSON(new Dictionary<string, JSON> {
{"root", new Dictionary<string, JSON> {
{"__class","JD_SubmitKeyboard_PhoneCommandData"},
{"keyboardOutput", searchText}
}}
});
}
public static JSON GetCancelKeyboardCommand()
{
return new JSON(new Dictionary<string, JSON> {
{"root", new Dictionary<string, JSON> {
{"__class","JD_CancelKeyboard_PhoneCommandData"}
}}
});
}
public static string GetRootClass(JSON jsonObject)
{
var root = jsonObject.GetField("root");
if (root != null)
{
return root.GetFieldAsString("__class");
}
return "";
}
public static int GetPreselectedRow(JSON inputSetup)
{
int row = 0;
JSON posSetup = GetPositionSetup(inputSetup);
if (posSetup != null)
row = (int)posSetup.GetFieldAsNumber("rowIndex");
return row;
}
public static int GetPreselectedItem(JSON inputSetup)
{
int item = 0;
JSON posSetup = GetPositionSetup(inputSetup);
if (posSetup != null)
item = (int)posSetup.GetFieldAsNumber("itemIndex");
return item;
}
public static int GetPreselectedAction(JSON inputSetup)
{
int action = 0;
JSON posSetup = GetPositionSetup(inputSetup);
if (posSetup != null)
action = (int)posSetup.GetFieldAsNumber("actionIndex");
return action;
}
public static JSON GetChangeActionCommand(JSON actionCommand)
{
JSON changeActionCommand = JSON.Clone(actionCommand);
if (changeActionCommand.HasField("root"))
changeActionCommand["root"]["__class"] = "ChangeAction_PhoneCommandData";
return changeActionCommand;
}
#endregion
#region Setters
public static void FillActionsCommand(JSON item, int rowIndex, int itemIndex)
{
var actions = item.GetField("actions");
if ((actions != null) && actions.IsArray)
{
foreach (JSON action in actions.Elements)
{
var command = action.GetField("command");
if ((command != null) && (command.AsString() == ""))
{
JSON commandJson = new JSON(new Dictionary<string, JSON> {
{"root", new Dictionary<string, JSON> {
{"__class", "ValidateAction_PhoneCommandData"},
{"rowIndex", rowIndex},
{"itemIndex", itemIndex},
{"actionIndex", actions.Elements.IndexOf(action)}
}}
});
action.SetField("command", JSON.Stringify(commandJson));
}
}
}
}
#endregion
}
}