using System; using UnityEngine; namespace Ingvar.LiveWatch { public class WatchReferenceCreator { private static Type typeAny = typeof(Any); public WatchReference Empty() { return new WatchReference(); } public virtual WatchReference GetOrAdd(string path, bool directChild = true) { var variable = WatchServices.VariableCreator.GetOrAdd(Watch.Watches, path, directChild); var castType = typeof(T); if (castType != typeAny) variable.RuntimeMeta.ValueType ??= castType; return CreateWatchRef(variable); } public virtual WatchReference GetOrAdd(WatchReference parent, string path, bool directChild = true) { var variable = WatchServices.VariableCreator.GetOrAdd(parent.WatchVariable.Childs, path, directChild); if (directChild) variable.Parent = parent.WatchVariable; if (typeof(T) != typeof(Any)) variable.RuntimeMeta.ValueType ??= typeof(T); return CreateWatchRef(variable); } public void SetUpdateCall(WatchReference watch, Action updateCall) { watch.WatchVariable.RuntimeMeta.UpdateCall = updateCall; } public bool IsInvalidType(WatchReference watch) { if (watch.WatchVariable == null) return true; var castType = typeof(T); if (castType == watch.WatchVariable.RuntimeMeta.ValueType || castType == typeAny) return false; return true; } public void MarkAsCollectionValue(WatchReference watch) { watch.WatchVariable.RuntimeMeta.IsCollectionValue = true; } public void MarkAsDictionaryKey(WatchReference watch) { watch.WatchVariable.RuntimeMeta.IsDictionaryKey = true; } public void MarkAsDictionaryValue(WatchReference watch) { watch.WatchVariable.RuntimeMeta.IsDictionaryValue = true; } public bool IsSetUp(WatchReference watch) { return watch.WatchVariable.RuntimeMeta.IsSetUp; } public bool IsDictionaryKey(WatchReference watch) { return watch.WatchVariable.RuntimeMeta.IsDictionaryKey; } public bool IsDictionaryValue(WatchReference watch) { return watch.WatchVariable.RuntimeMeta.IsDictionaryValue; } public bool IsCollectionValue(WatchReference watch) { return watch.WatchVariable.RuntimeMeta.IsCollectionValue; } public WatchReference PushEmpty(WatchReference watch, bool withRoot = true, int maxRecursionDepth = 10) { WatchServices.VariableCreator.PushEmpty(watch.WatchVariable, withRoot, maxRecursionDepth); return watch; } public WatchReference PushDouble(WatchReference watch, double value) { watch.WatchVariable.Values.DoubleList.Add(new WatchValue(value)); AfterPush(watch); return watch; } public WatchReference PushFloat(WatchReference watch, float value) { watch.WatchVariable.Values.FloatList.Add(new WatchValue(value)); AfterPush(watch); return watch; } public WatchReference PushInt(WatchReference watch, int value) { watch.WatchVariable.Values.IntList.Add(new WatchValue(value)); AfterPush(watch); return watch; } public WatchReference PushString(WatchReference watch, string value) { watch.WatchVariable.Values.StringList.Add(new WatchValue(value)); AfterPush(watch); return watch; } public WatchReference PushBool(WatchReference watch, bool value) { watch.WatchVariable.Values.BoolList.Add(new WatchValue(value)); AfterPush(watch); return watch; } public WatchReference PushDecimal(WatchReference watch, decimal value) { watch.WatchVariable.Values.DoubleList.Add(new WatchValue((double)value)); AfterPush(watch); return watch; } public WatchReference PushLong(WatchReference watch, long value) { watch.WatchVariable.Values.DoubleList.Add(new WatchValue(value)); AfterPush(watch); return watch; } public WatchReference PushShort(WatchReference watch, short value) { watch.WatchVariable.Values.IntList.Add(new WatchValue(value)); AfterPush(watch); return watch; } public WatchReference PushByte(WatchReference watch, byte value) { watch.WatchVariable.Values.IntList.Add(new WatchValue(value)); AfterPush(watch); return watch; } public WatchReference PushULong(WatchReference watch, ulong value) { watch.WatchVariable.Values.DoubleList.Add(new WatchValue(value)); AfterPush(watch); return watch; } public WatchReference PushUShort(WatchReference watch, ushort value) { watch.WatchVariable.Values.IntList.Add(new WatchValue(value)); AfterPush(watch); return watch; } public WatchReference PushSByte(WatchReference watch, sbyte value) { watch.WatchVariable.Values.IntList.Add(new WatchValue(value)); AfterPush(watch); return watch; } public WatchReference PushChar(WatchReference watch, char value) { watch.WatchVariable.Values.StringList.Add(new WatchValue(WatchServices.NameBuilder.GetString(value))); AfterPush(watch); return watch; } public WatchReference PushNull(WatchReference watch, int maxRecursionDepth = 10) { watch.WatchVariable.Values.StringList.Add(new WatchValue("NULL")); WatchServices.VariableCreator.PushEmpty(watch.WatchVariable, false, maxRecursionDepth); AfterPush(watch); return watch; } public WatchReference PushNonBasic(WatchReference watch, T value) { watch.WatchVariable.Values.PushEmpty(); AfterPush(watch); return watch; } public WatchReference TrySetupAs(WatchReference watch, WatchValueType type) { if (watch.WatchVariable.RuntimeMeta.IsSetUp) return watch; watch.WatchVariable.Values.Type = type; watch.WatchVariable.RuntimeMeta.ValueType = typeof(T); watch.WatchVariable.RuntimeMeta.IsSetUp = true; var watchStorage = watch.WatchVariable.Parent == null ? Watch.Watches : watch.WatchVariable.Parent.Childs; if (watchStorage.IsSortable) { watch.WatchVariable.RuntimeMeta.IsSortingRequired = true; } return watch; } private void AfterPush(WatchReference watch) { WatchServices.VariableUpdater.UpdateTotalValuesCount(watch.WatchVariable); WatchServices.VariableSortUpdater.TrySortByVariable(watch.WatchVariable); WatchServices.VariableUpdater.AnyPushSinceUpdate = true; } private WatchReference CreateWatchRef(WatchVariable variable) { return new WatchReference(variable); } #region Obsolete public WatchReference PushEmptyRoot(WatchReference watch) { watch.WatchVariable.Values.PushEmpty(); WatchServices.VariableUpdater.UpdateTotalValuesCount(watch.WatchVariable); return watch; } public void SetValuesType(WatchReference watch, WatchValueType valueType) { watch.WatchVariable.Values.Type = valueType; watch.WatchVariable.RuntimeMeta.ValueType = typeof(T); } public void MarkAsSetUp(WatchReference watch) { watch.WatchVariable.RuntimeMeta.IsSetUp = true; } public void PushFloat(WatchReference watch, float value) { watch.WatchVariable.Values.FloatList.Add(new WatchValue(value)); WatchServices.VariableUpdater.UpdateTotalValuesCount(watch.WatchVariable); } public void PushDouble(WatchReference watch, double value) { watch.WatchVariable.Values.DoubleList.Add(new WatchValue(value)); WatchServices.VariableUpdater.UpdateTotalValuesCount(watch.WatchVariable); } public void PushInt(WatchReference watch, int value) { watch.WatchVariable.Values.IntList.Add(new WatchValue(value)); WatchServices.VariableUpdater.UpdateTotalValuesCount(watch.WatchVariable); } public void PushString(WatchReference watch, string value) { watch.WatchVariable.Values.StringList.Add(new WatchValue(value)); WatchServices.VariableUpdater.UpdateTotalValuesCount(watch.WatchVariable); } public void PushBool(WatchReference watch, bool value) { watch.WatchVariable.Values.BoolList.Add(new WatchValue(value)); WatchServices.VariableUpdater.UpdateTotalValuesCount(watch.WatchVariable); } #endregion } }