Beispiel #1
0
    private static SharedVariable BytesToSharedVariable(FieldSerializationData fieldSerializationData, Dictionary <string, int> fieldIndexMap, byte[] bytes, int dataPosition, IVariableSource variableSource, bool fromField, string namePrefix)
    {
        SharedVariable sharedVariable = null;
        string         text           = (string)LoadField(fieldSerializationData, fieldIndexMap, typeof(string), namePrefix + "Type", null);

        if (string.IsNullOrEmpty(text))
        {
            return(null);
        }
        string name  = (string)LoadField(fieldSerializationData, fieldIndexMap, typeof(string), namePrefix + "Name", null);
        bool   flag  = Convert.ToBoolean(LoadField(fieldSerializationData, fieldIndexMap, typeof(bool), namePrefix + "IsShared", null, null, null));
        bool   flag2 = Convert.ToBoolean(LoadField(fieldSerializationData, fieldIndexMap, typeof(bool), namePrefix + "IsGlobal", null, null, null));

        if (flag && fromField)
        {
            if (!flag2)
            {
                sharedVariable = variableSource.GetVariable(name);
            }
            else
            {
                if (globalVariables == null)
                {
                    globalVariables = GlobalVariables.Instance;
                }
                if (globalVariables != null)
                {
                    sharedVariable = globalVariables.GetVariable(name);
                }
            }
        }
        Type typeWithinAssembly = TaskUtility.GetTypeWithinAssembly(text);

        if (typeWithinAssembly == null)
        {
            return(null);
        }
        bool flag3 = true;

        if (sharedVariable == null || !(flag3 = sharedVariable.GetType().Equals(typeWithinAssembly)))
        {
            sharedVariable             = (TaskUtility.CreateInstance(typeWithinAssembly) as SharedVariable);
            sharedVariable.Name        = name;
            sharedVariable.IsShared    = flag;
            sharedVariable.IsGlobal    = flag2;
            sharedVariable.NetworkSync = Convert.ToBoolean(LoadField(fieldSerializationData, fieldIndexMap, typeof(bool), namePrefix + "NetworkSync", null));
            if (!flag2)
            {
                sharedVariable.PropertyMapping      = (string)LoadField(fieldSerializationData, fieldIndexMap, typeof(string), namePrefix + "PropertyMapping", null);
                sharedVariable.PropertyMappingOwner = (GameObject)LoadField(fieldSerializationData, fieldIndexMap, typeof(GameObject), namePrefix + "PropertyMappingOwner", null);
                sharedVariable.InitializePropertyMapping(variableSource as BehaviorSource);
            }
            if (!flag3)
            {
                sharedVariable.IsShared = true;
            }
            LoadFields(fieldSerializationData, fieldIndexMap, sharedVariable, namePrefix, variableSource);
        }
        return(sharedVariable);
    }
        protected override string ProcessControl(string variableIdentifier, IVariableSource variableSource,
                                                 ContentControl contentControl, List <string> otherParameters)
        {
            try
            {
                var variable = variableSource.GetVariable(variableIdentifier);

                if (variable == null)
                {
                    return(null);
                }

                //If the variable is not of a complex type or if the content control does not support nested controls,
                //(ie, is not a RichText control), then just return the string representation of the variable
                if (contentControl.Type != OpenXmlExtensions.ContentControlType.RichText ||
                    !(variable is Dictionary <string, object> innerData))
                {
                    return(variable.ToString());
                }

                //If the variable is complex (dictionary) type and the control is rich text, we need to do
                //recursive replacement. For that we will add it to the queue
                var innerVariableSource = new VariableSource(innerData);
                Enqueue(new ControlReplacementExecutionData
                {
                    Controls = contentControl.DescendingControls, VariableSource = innerVariableSource
                });

                return(null);
            }
            catch (VariableNotFoundException)
            {
                return(null);
            }
        }
Beispiel #3
0
        internal static bool EvaluateVariable(string varIdentifier, IVariableSource data, out object variableValue)
        {
            bool value;

            try
            {
                variableValue = data.GetVariable(varIdentifier);

                value = EvaluateVariableValue(variableValue);
            }
            catch (VariableNotFoundException)
            {
                value         = false;
                variableValue = null;
            }

            return(value);
        }
Beispiel #4
0
        protected override string ProcessDropdownControl(string variableIdentifier, IVariableSource data,
                                                         SdtContentDropDownList dropdown, List <string> otherParameters)
        {
            //This is the list that we should check to see if the value should be singular or plural
            var list     = data.GetVariable <IList>(variableIdentifier);
            var singular = list.Count <= 1;

            if (dropdown.ChildElements.Count == 0)
            {
                return(null);
            }

            var dropdownChildElement = singular || dropdown.ChildElements.Count == 1
                ? dropdown.ChildElements[0]
                : dropdown.ChildElements[1];

            return(GetListItemValue(dropdownChildElement));
        }
        protected override string ProcessControl(string variableIdentifier, IVariableSource variableSource,
                                                 ContentControl contentControl, List <string> otherParameters)
        {
            try {
                var variable = variableSource.GetVariable(variableIdentifier);

                if (variable == null)
                {
                    return(null);
                }

                var imagePath = variable.ToString();
                var byteArray = File.ReadAllBytes(imagePath);
                return(Convert.ToBase64String(byteArray));
            } catch (VariableNotFoundException) {
                return(null);
            }
        }
        private static SharedVariable DeserializeSharedVariable(Dictionary <string, object> dict, IVariableSource variableSource, bool fromSource, List <UnityEngine.Object> unityObjects)
        {
            if (dict == null)
            {
                return(null);
            }
            SharedVariable sharedVariable = null;
            object         obj;

            if (!fromSource && variableSource != null && dict.TryGetValue("Name", out obj))
            {
                object value;
                dict.TryGetValue("IsGlobal", out value);
                if (!dict.TryGetValue("IsGlobal", out value) || !Convert.ToBoolean(value))
                {
                    sharedVariable = variableSource.GetVariable(obj as string);
                }
                else
                {
                    if (JSONDeserializationDeprecated.globalVariables == null)
                    {
                        JSONDeserializationDeprecated.globalVariables = GlobalVariables.Instance;
                    }
                    if (JSONDeserializationDeprecated.globalVariables != null)
                    {
                        sharedVariable = JSONDeserializationDeprecated.globalVariables.GetVariable(obj as string);
                    }
                }
            }
            Type typeWithinAssembly = TaskUtility.GetTypeWithinAssembly(dict["Type"] as string);

            if (typeWithinAssembly == null)
            {
                return(null);
            }
            bool flag = true;

            if (sharedVariable == null || !(flag = sharedVariable.GetType().Equals(typeWithinAssembly)))
            {
                sharedVariable      = (TaskUtility.CreateInstance(typeWithinAssembly) as SharedVariable);
                sharedVariable.name = (dict["Name"] as string);
                object obj2;
                if (dict.TryGetValue("IsShared", out obj2))
                {
                    sharedVariable.isShared = Convert.ToBoolean(obj2);
                }
                if (dict.TryGetValue("IsGlobal", out obj2))
                {
                    sharedVariable.isGlobal = Convert.ToBoolean(obj2);
                }
                if (dict.TryGetValue("NetworkSync", out obj2))
                {
                    sharedVariable.networkSync = Convert.ToBoolean(obj2);
                }
                if (!sharedVariable.isGlobal && dict.TryGetValue("PropertyMapping", out obj2))
                {
                    sharedVariable.propertyMapping = (obj2 as string);
                    if (dict.TryGetValue("PropertyMappingOwner", out obj2))
                    {
                        sharedVariable.propertyMappingOwner = (JSONDeserializationDeprecated.IndexToUnityObject(Convert.ToInt32(obj2), unityObjects) as GameObject);
                    }
                    sharedVariable.InitializePropertyMapping(variableSource as BehaviorSource);
                }
                if (!flag)
                {
                    sharedVariable.isShared = true;
                }
                JSONDeserializationDeprecated.DeserializeObject(null, sharedVariable, dict, variableSource, unityObjects);
            }
            return(sharedVariable);
        }
Beispiel #7
0
        internal static bool EvaluateConditionalVariableWithParameters(string varIdentifier,
                                                                       IVariableSource variableSource,
                                                                       List <string> otherParameters)
        {
            var value = EvaluateVariable(varIdentifier, variableSource, out var variableValue);

            //If no special parameters are found simply return the found value
            if (otherParameters.Count <= 0)
            {
                return(value);
            }

            //Process the extra parameters
            string lastOperator = null;

            foreach (var otherParameter in otherParameters)
            {
                switch (otherParameter)
                {
                case OrTagIdentifier:
                case AndTagIdentifier:
                case EqualTagIdentifier:
                case GreaterTagIdentifier:
                case LessTagIdentifier:
                    lastOperator = otherParameter;
                    break;

                case NotTagIdentifier:
                    value        = !value;
                    lastOperator = null;
                    break;

                default:
                {
                    if (lastOperator != null)
                    {
                        object nextValue;
                        nextValue = variableSource.GetVariable(otherParameter) ?? otherParameter;

                        var nextValueEvaluated = EvaluateVariableValue(nextValue);

                        switch (lastOperator)
                        {
                        case OrTagIdentifier:
                            value = value || nextValueEvaluated;
                            break;

                        case AndTagIdentifier:
                            value = value && nextValueEvaluated;
                            break;

                        case EqualTagIdentifier:
                            value = variableValue?.ToString() == nextValue.ToString();
                            break;

                        case GreaterTagIdentifier:
                            try
                            {
                                value = float.Parse(variableValue?.ToString()) > float.Parse(nextValue.ToString());
                            }
                            catch
                            {
                                try
                                {
                                    value = int.Parse(variableValue?.ToString()) > int.Parse(nextValue.ToString());
                                }
                                catch
                                {
                                    // ignored
                                }
                            }

                            break;

                        case LessTagIdentifier:
                            try
                            {
                                value = float.Parse(variableValue?.ToString()) < float.Parse(nextValue.ToString());
                            }
                            catch
                            {
                                try
                                {
                                    value = int.Parse(variableValue?.ToString()) < int.Parse(nextValue.ToString());
                                }
                                catch
                                {
                                    // ignored
                                }
                            }

                            break;
                        }

                        lastOperator = null;
                    }

                    break;
                }
                }
            }

            return(value);
        }
 private static bool VariableNameValid(IVariableSource variableSource, string variableName)
 {
     return(!variableName.Equals(string.Empty) && (variableSource == null || variableSource.GetVariable(variableName) == null));
 }
Beispiel #9
0
        private static void DoVariablesToICodeSync(IVariableSource variableSource, FsmVariable[] iCodeVariables)
        {
            if (variableSource == null)
            {
                return;
            }

            SharedVariable behaviorDesignerVariable = null;
            FsmVariable    iCodeVariable            = null;

            for (int i = 0; i < iCodeVariables.Length; ++i)
            {
                iCodeVariable = iCodeVariables[i];
                if ((behaviorDesignerVariable = variableSource.GetVariable(iCodeVariable.Name)) != null)
                {
                    // FsmInt
                    if (iCodeVariable is FsmInt)
                    {
                        if (behaviorDesignerVariable is SharedInt)
                        {
                            (iCodeVariable as FsmInt).Value = (int)behaviorDesignerVariable.GetValue();
                        }
                        continue;
                    }

                    // FsmFloat
                    if (iCodeVariable is FsmFloat)
                    {
                        if (behaviorDesignerVariable is SharedFloat)
                        {
                            (iCodeVariable as FsmFloat).Value = (float)behaviorDesignerVariable.GetValue();
                        }
                        continue;
                    }

                    // FsmBool
                    if (iCodeVariable is FsmBool)
                    {
                        if (behaviorDesignerVariable is SharedBool)
                        {
                            (iCodeVariable as FsmBool).Value = (bool)behaviorDesignerVariable.GetValue();
                        }
                        continue;
                    }

                    // FsmString
                    if (iCodeVariable is FsmString)
                    {
                        if (behaviorDesignerVariable is SharedString)
                        {
                            (iCodeVariable as FsmString).Value = (string)behaviorDesignerVariable.GetValue();
                        }
                        continue;
                    }

                    // FsmColor
                    if (iCodeVariable is FsmColor)
                    {
                        if (behaviorDesignerVariable is SharedColor)
                        {
                            (iCodeVariable as FsmColor).Value = (Color)behaviorDesignerVariable.GetValue();
                        }
                        continue;
                    }

                    // FsmVector2
                    if (iCodeVariable is FsmVector2)
                    {
                        if (behaviorDesignerVariable is SharedVector2)
                        {
                            (iCodeVariable as FsmVector2).Value = (Vector2)behaviorDesignerVariable.GetValue();
                        }
                        continue;
                    }

                    // FsmVector3
                    if (iCodeVariable is FsmVector3)
                    {
                        if (behaviorDesignerVariable is SharedVector3)
                        {
                            (iCodeVariable as FsmVector3).Value = (Vector3)behaviorDesignerVariable.GetValue();
                        }
                        continue;
                    }

                    // FsmObject
                    if (iCodeVariable is FsmObject)
                    {
                        if (behaviorDesignerVariable is SharedGameObject)
                        {
                            (iCodeVariable as FsmObject).Value = (GameObject)behaviorDesignerVariable.GetValue();
                        }
                        else if (behaviorDesignerVariable is SharedObject)
                        {
                            (iCodeVariable as FsmObject).Value = (Object)behaviorDesignerVariable.GetValue();
                        }
                        continue;
                    }
                }
            }
        }
Beispiel #10
0
        protected override string ProcessControl(string variableIdentifier, IVariableSource variableSource,
                                                 ContentControl original, List <string> otherParameters)
        {
            try
            {
                GetParameters(otherParameters, out var separator, out var lastSeparator);

                var dataItems = variableSource.GetVariable <IList>(variableIdentifier);

                if (dataItems == null || dataItems.Count == 0)
                {
                    //No items found, remove the content
                    original.Remove();
                    return(null);
                }


                //Copy the content control as many times as we have items
                for (var i = 0; i < dataItems.Count; i++)
                {
                    var dataItem = dataItems[i];
                    var clone    = original.Clone();

                    switch (dataItem)
                    {
                    case string _:
                    case int _:
                    case float _:
                    case double _:
                    case decimal _:
                        SetTextAndRemovePlaceholderFormat(clone.SdtElement, dataItem.ToString());
                        break;

                    case Dictionary <string, object> dictItem:
                    {
                        if (dictItem.ContainsKey("index") == false)
                        {
                            dictItem.Add("index", i + 1);
                        }

                        //Since we are creating new elements, we should add them to the queue with both this and the inner data
                        var innerSource = new VariableSource(dictItem);
                        var exInner     = new ControlReplacementExecutionData(clone.DescendingControls, innerSource);
                        var ex          = new ControlReplacementExecutionData(clone.DescendingControls, variableSource);

                        Enqueue(exInner);
                        OnInnerControlReplacementEnqueued(ex);
                        OnInnerControlReplacementEnqueued(exInner);

                        //Support old styled documents
                        var innerRep1 = new InnerRepeatingVariableControlReplacer();
                        var innerRep2 = new InnerRepeatingConditionalRemoveControlReplacer();
                        innerRep1.ReplaceAll(ex.Controls, ex.VariableSource);
                        innerRep2.ReplaceAll(ex.Controls, ex.VariableSource);

                        break;
                    }
                    }

                    var lasttext = clone.SdtElement.Descendants <Text>().LastOrDefault();

                    if (lasttext != null)
                    {
                        if (i < dataItems.Count - 2)
                        {
                            lasttext.InsertAfterSelf(new Text(separator)
                            {
                                Space = SpaceProcessingModeValues.Preserve
                            });
                        }
                        else if (i < dataItems.Count - 1)
                        {
                            lasttext.InsertAfterSelf(new Text(lastSeparator)
                            {
                                Space = SpaceProcessingModeValues.Preserve
                            });
                        }
                    }
                }

                //Delete the original
                original.Remove();

                return(null);
            }
            catch (VariableNotFoundException)
            {
                return(null);
            }
        }