using System; using System.Linq; using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core; namespace KairoEngine.Core.GameActions { [System.Serializable] public class GameActionContext : ISerializationCallbackReceiver { [ListDrawerSettings(HideAddButton = true, HideRemoveButton = false, DraggableItems = false, Expanded = false, ShowPaging = false, ShowItemCount = true)] [ShowInInspector] public List variables = new List(); public bool HasVariable(string variableName) { for (int i = 0; i < variables.Count; i++) { if(variables[i].name == variableName) return true; } return false; } public GameActionContextVariable GetVariable(string title) { foreach (var variable in variables) { if(variable.name == title) return variable; } return null; } public void AddVariable(GameActionContextVariable variable) { for (int i = 0; i < variables.Count; i++) { if(variables[i].name == variable.name) { variables[i] = variable; return; } } variables.Add(variable); } #region Serialization [SerializeField, HideInInspector] private List serializedVariables = new List(); [SerializeField, HideInInspector] public GameActionObjectSerializer objectSerializer = new GameActionObjectSerializer(); private void SerializeActions() { serializedVariables = new List(); objectSerializer.ClearGameObjects(); foreach (var variable in variables) { variable.className = variable.GetType().AssemblyQualifiedName; variable.typeName = variable.GetTypeName(); variable.OnBeforeSerialize(objectSerializer); string data = JsonUtility.ToJson(variable); serializedVariables.Add(data); } } private void DeserializeActions() { if(serializedVariables == null) return; foreach (var data in serializedVariables) { GameActionContextVariable variable = JsonUtility.FromJson(data); variable = GameActionContextVariable.InvokeStringMethod(variable.className, variable.typeName, data); variable.OnBeforeDeserialize(objectSerializer); AddVariable(variable); } objectSerializer.ClearGameObjects(); } public void OnBeforeSerialize() => SerializeActions(); public void OnAfterDeserialize() => DeserializeActions(); #endregion #region NewVariable [OnInspectorInit("GetCompatibleVariablenames"), OnValueChanged("AddNewVariable")] [ValueDropdown("possibleVariables", IsUniqueList = false)] [LabelText("Add New Variable")] [NonSerialized] public GameActionContextVariable newVariable; [NonSerialized] private IEnumerable possibleVariables = new ValueDropdownList(); private void GetCompatibleVariablenames() { possibleVariables = ReflectiveEnumerator.GetEnumerableOfType() .Where(x => x.GetTypeName() != "GameActionContextVariable") .Where(x => x.GetTypeName() != "GameActionContextVariableBase") .Select(x => new ValueDropdownItem(x.GetVariableName(), x)); } private void AddNewVariable() { if(newVariable != null) { newVariable.className = newVariable.GetType().AssemblyQualifiedName; variables.Add(newVariable); newVariable = null; } } #endregion #region Utlilities public GameActionContext Duplicate() { GameActionContext newContext = new GameActionContext(); var newSerializedVariables = new List(); newContext.objectSerializer = new GameActionObjectSerializer(); foreach (var variable in variables) { variable.className = variable.GetType().AssemblyQualifiedName; variable.typeName = variable.GetTypeName(); variable.OnBeforeSerialize(newContext.objectSerializer); string data = JsonUtility.ToJson(variable); newSerializedVariables.Add(data); } foreach (var data in newSerializedVariables) { GameActionContextVariable variable = JsonUtility.FromJson(data); variable = GameActionContextVariable.InvokeStringMethod(variable.className, variable.typeName, data); variable.OnBeforeDeserialize(newContext.objectSerializer); newContext.AddVariable(variable); } newContext.objectSerializer.ClearGameObjects(); return newContext; } #endregion } }