Пример #1
0
        public static void SaveDeclarations(VariableDeclarations declarations)
        {
            WarnAndNullifyUnityObjectReferences(declarations);

            try
            {
                var data = declarations.Serialize();

                if (data.objectReferences.Length != 0)
                {
                    // Hopefully, WarnAndNullify will have prevented this exception,
                    // but in case an object reference was nested as a member of the
                    // serialized objects, it wouldn't have caught it, and thus we need
                    // to abort the save process and inform the user.
                    throw new InvalidOperationException("Cannot use Unity object variable references in saved variables.");
                }

                PlayerPrefs.SetString(playerPrefsKey, data.json);
                PlayerPrefs.Save();
            }
            catch (Exception ex)
            {
                Debug.LogWarning($"Failed to save variables to player prefs: \n{ex}");
            }
        }
        public static IEnumerable <string> GetPredefinedVariableNames(VariableKind kind, GraphReference reference)
        {
            VariableDeclarations source = null;

            switch (kind)
            {
            case VariableKind.Flow:
                break;

            case VariableKind.Graph:
            {
                source = Variables.Graph(reference);
                break;
            }

            case VariableKind.Object:
            {
                if (reference.gameObject != null)
                {
                    source = Variables.Object(reference.gameObject);
                }
                break;
            }

            case VariableKind.Scene:
            {
                if (reference.scene != null && Variables.ExistInScene(reference.scene))
                {
                    source = Variables.Scene(reference.scene);
                }
                break;
            }

            case VariableKind.Application:
            {
                source = Variables.Application;
                break;
            }

            case VariableKind.Saved:
            {
                source = Variables.Saved;
                break;
            }

            default:
                throw new UnexpectedEnumValueException <VariableKind>(kind);
            }

            if (source == null)
            {
                return(Enumerable.Empty <string>());
            }

            return(source.Select(d => d.name).Where(name => !StringUtility.IsNullOrWhiteSpace(name)).OrderBy(name => name));
        }
Пример #3
0
        private static void WarnAndNullifyUnityObjectReferences(VariableDeclarations declarations)
        {
            Ensure.That(nameof(declarations)).IsNotNull(declarations);

            foreach (var declaration in declarations)
            {
                if (declaration.value is UnityObject)
                {
                    Debug.LogWarning($"Saved variable '{declaration.name}' refers to a Unity object. This is not supported. Its value will be null.");
                    declarations[declaration.name] = null;
                }
            }
        }
Пример #4
0
 private static void FetchSavedDeclarations()
 {
     if (PlayerPrefs.HasKey(playerPrefsKey))
     {
         try
         {
             saved = (VariableDeclarations) new SerializationData(PlayerPrefs.GetString(playerPrefsKey)).Deserialize();
         }
         catch (Exception ex)
         {
             Debug.LogWarning($"Failed to fetch saved variables from player prefs: \n{ex}");
             saved = new VariableDeclarations();
         }
     }
     else
     {
         saved = new VariableDeclarations();
     }
 }
Пример #5
0
        public FlowGraph()
        {
            units = new GraphElementCollection <IUnit>(this);
            controlConnections = new GraphConnectionCollection <ControlConnection, ControlOutput, ControlInput>(this);
            valueConnections   = new GraphConnectionCollection <ValueConnection, ValueOutput, ValueInput>(this);
            invalidConnections = new GraphConnectionCollection <InvalidConnection, IUnitOutputPort, IUnitInputPort>(this);
            groups             = new GraphElementCollection <GraphGroup>(this);

            elements.Include(units);
            elements.Include(controlConnections);
            elements.Include(valueConnections);
            elements.Include(invalidConnections);
            elements.Include(groups);

            controlInputDefinitions  = new UnitPortDefinitionCollection <ControlInputDefinition>();
            controlOutputDefinitions = new UnitPortDefinitionCollection <ControlOutputDefinition>();
            valueInputDefinitions    = new UnitPortDefinitionCollection <ValueInputDefinition>();
            valueOutputDefinitions   = new UnitPortDefinitionCollection <ValueOutputDefinition>();

            variables = new VariableDeclarations();
        }
Пример #6
0
        private static void MergeInitialAndSavedDeclarations()
        {
            merged = initial.CloneViaFakeSerialization();

            WarnAndNullifyUnityObjectReferences(merged);

            foreach (var name in saved.Select(vd => vd.name))
            {
                if (!merged.IsDefined(name))
                {
                    merged[name] = saved[name];
                }
                else if (merged[name] == null)
                {
                    if (saved[name] == null || saved[name].GetType().IsNullable())
                    {
                        merged[name] = saved[name];
                    }
                    else
                    {
                        Debug.LogWarning($"Cannot convert saved player pref '{name}' to null.\n");
                    }
                }
                else
                {
                    if (saved[name].IsConvertibleTo(merged[name].GetType(), true))
                    {
                        merged[name] = saved[name];
                    }
                    else
                    {
                        Debug.LogWarning($"Cannot convert saved player pref '{name}' to expected type ({merged[name].GetType()}).\nReverting to initial value.");
                    }
                }
            }
        }
 private static void DestroyRuntimeDeclarations()
 {
     runtime = null;
 }
 private static void CreateRuntimeDeclarations()
 {
     runtime = asset.declarations.CloneViaFakeSerialization();
 }
Пример #9
0
 private static void DestroyMergedDeclarations()
 {
     merged = null;
 }