Ejemplo n.º 1
0
 private void ReadChildOutputData(SymbolChild symbolChild, Guid outputId, JToken json)
 {
     if (json["Type"] != null)
     {
         symbolChild.Outputs[outputId].OutputData.ReadFromJson(json);
     }
 }
Ejemplo n.º 2
0
        private SymbolChild ReadSymbolChild(Model model, JToken symbolChildJson)
        {
            var childId  = Guid.Parse(symbolChildJson["Id"].Value <string>());
            var symbolId = Guid.Parse(symbolChildJson["SymbolId"].Value <string>());

            if (!SymbolRegistry.Entries.TryGetValue(symbolId, out var symbol))
            {
                // if the used symbol hasn't been loaded so far ensure it's loaded now
                symbol = model.ReadSymbolWithId(symbolId);
            }

            if (symbol == null)
            {
                Log.Warning($"Failed to load symbol {symbolId}.");
                return(null);
            }

            var symbolChild = new SymbolChild(symbol, childId, null);

            var nameToken = symbolChildJson["Name"];

            if (nameToken != null)
            {
                symbolChild.Name = nameToken.Value <string>();
            }

            foreach (var inputValue in (JArray)symbolChildJson["InputValues"])
            {
                ReadChildInputValue(symbolChild, inputValue);
            }

            foreach (var outputJson in (JArray)symbolChildJson["Outputs"])
            {
                var outputId       = Guid.Parse(outputJson["Id"].Value <string>());
                var outputDataJson = outputJson["OutputData"];
                if (outputDataJson != null)
                {
                    ReadChildOutputData(symbolChild, outputId, outputDataJson);
                }

                var dirtyFlagJson = outputJson["DirtyFlagTrigger"];
                if (dirtyFlagJson != null)
                {
                    symbolChild.Outputs[outputId].DirtyFlagTrigger = (DirtyFlagTrigger)Enum.Parse(typeof(DirtyFlagTrigger), dirtyFlagJson.Value <string>());
                }

                var isDisabledJson = outputJson["IsDisabled"];
                if (isDisabledJson != null)
                {
                    symbolChild.Outputs[outputId].IsDisabled = isDisabledJson.Value <bool>();
                }
            }

            return(symbolChild);
        }
Ejemplo n.º 3
0
        private void ReadChildInputValue(SymbolChild symbolChild, JToken inputJson)
        {
            var id        = Guid.Parse(inputJson["Id"].Value <string>());
            var jsonValue = inputJson["Value"];

            try
            {
                symbolChild.InputValues[id].Value.SetValueFromJson(jsonValue);
                symbolChild.InputValues[id].IsDefault = false;
            }
            catch
            {
                Log.Error("Failed to read input value");
            }
        }
Ejemplo n.º 4
0
        public Symbol ReadSymbol(Model model, JToken o, bool allowNonOperatorInstanceType = false)
        {
            var id = Guid.Parse(o["Id"].Value <string>());

            if (SymbolRegistry.Entries.ContainsKey(id))
            {
                return(null); // symbol already in registry - nothing to do
            }
            var name           = o["Name"].Value <string>();
            var @namespace     = o["Namespace"]?.Value <string>() ?? "";
            var symbolChildren = new List <SymbolChild>();

            var missingSymbolChildIds = new HashSet <Guid>();
            var missingSymbolsIds     = new HashSet <Guid>();

            foreach (var childJson in ((JArray)o["Children"]))
            {
                SymbolChild symbolChild = ReadSymbolChild(model, childJson);
                if (symbolChild == null)
                {
                    var childId  = Guid.Parse((childJson["Id"] ?? "").Value <string>() ?? string.Empty);
                    var symbolId = Guid.Parse((childJson["SymbolId"] ?? "").Value <string>() ?? string.Empty);
                    Log.Warning($"Skipping child of undefined type {symbolId} in {name}");

                    if (childId != Guid.Empty)
                    {
                        missingSymbolChildIds.Add(childId);
                    }

                    if (symbolId != Guid.Empty)
                    {
                        missingSymbolsIds.Add(symbolId);
                    }
                }
                else
                {
                    symbolChildren.Add(symbolChild);
                }
            }

            var connections = new List <Symbol.Connection>();

            foreach (var c in ((JArray)o["Connections"]))
            {
                Symbol.Connection connection = ReadConnection(c);
                if (connection == null)
                {
                    Log.Warning($"Skipping invalid connection in {name}");
                }
                else if (missingSymbolChildIds.Contains(connection.TargetParentOrChildId) ||
                         missingSymbolChildIds.Contains(connection.SourceParentOrChildId)
                         )
                {
                    Log.Warning("Skipping connection to child of undefined type");
                }
                else
                {
                    connections.Add(connection);
                }
            }

            var orderedInputIds = (from jsonInput in (JArray)o["Inputs"]
                                   let idAndValue = ReadSymbolInputDefaults(jsonInput)
                                                    select idAndValue.Item1).ToArray();

            var inputDefaultValues = (from jsonInput in (JArray)o["Inputs"]
                                      let idAndValue = ReadSymbolInputDefaults(jsonInput)
                                                       select idAndValue).ToDictionary(entry => entry.Item1, entry => entry.Item2);
            var animatorData = (JArray)o["Animator"];

            string namespaceId      = id.ToString().ToLower().Replace('-', '_');
            string instanceTypeName = "T3.Operators.Types.Id_" + namespaceId + "." + name +
                                      ", Operators, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null";
            Type instanceType = Type.GetType(instanceTypeName);

            if (instanceType == null)
            {
                if (allowNonOperatorInstanceType)
                {
                    instanceType = typeof(object);
                }
                else
                {
                    throw new Exception($"The type for '{instanceTypeName}' could not be found in Operator assembly.");
                }
            }

            var symbol = new Symbol(instanceType, id, orderedInputIds, symbolChildren)
            {
                Name      = name,
                Namespace = @namespace,
            };

            symbol.Connections.AddRange(connections);

            if (animatorData != null)
            {
                symbol.Animator.Read(animatorData);
            }

            foreach (var input in symbol.InputDefinitions)
            {
                // if no entry is present just the value default is used, happens for new inputs
                if (inputDefaultValues.TryGetValue(input.Id, out var jsonDefaultValue))
                {
                    input.DefaultValue.SetValueFromJson(jsonDefaultValue);
                }
            }

            var jAudioClipArray = (JArray)o[nameof(symbol.AudioClips)];

            if (jAudioClipArray != null)
            {
                foreach (var c in jAudioClipArray)
                {
                    AudioClip clip = AudioClip.FromJson(c);
                    symbol.AudioClips.Add(clip);
                }
            }

            return(symbol);
        }