public AssignedBehavior AddBehavior(UnassignedBehavior behavior)
    {
        OnBeforeAnyChange();
        Debug.Assert(!behavior.behaviorUri.IsNullOrEmpty());

        string finalBehaviorUri = behavior.behaviorUri;

        // Alright, some special logic here. If it's a user-library behavior, do not
        // just use the URI directly. Import it, so turn it into embedded, then use
        // it. We want VOOS files to be stand-alone, so we can't have any local user
        // library dependencies.
        if (BehaviorSystem.IsUserLibraryBehaviorUri(behavior.behaviorUri))
        {
            Behaviors.Behavior importedBehavior = GetBehaviorSystem().GetBehaviorData(behavior.behaviorUri);
            Debug.Assert(!importedBehavior.userLibraryFile.IsNullOrEmpty());
            string importedId = GetBehaviorSystem().GenerateUniqueId();
            GetBehaviorSystem().PutBehavior(importedId, importedBehavior);
            string importedUri = BehaviorSystem.IdToEmbeddedBehaviorUri(importedId);
            finalBehaviorUri = importedUri;
        }

        // Create the use in the database
        string useId = actor.GetBehaviorSystem().GenerateUniqueId();
        var    brain = GetBrain();

        brain.AddUse(new BehaviorUse
        {
            id                  = useId,
            behaviorUri         = finalBehaviorUri,
            propertyAssignments = new Behaviors.PropertyAssignment[0]
        });
        GetBehaviorSystem().PutBrain(GetBrainId(), brain);

        return(new AssignedBehavior(useId, this));
    }
Ejemplo n.º 2
0
    public int WriteAllEmbeddedBehaviorsToDirectory(string dir)
    {
        var allUris = from e in this.db.behaviors.GetAll()
                      select BehaviorSystem.IdToEmbeddedBehaviorUri(e.id);

        return(WriteEmbeddedBehaviorsToDirectory(allUris, dir));
    }
Ejemplo n.º 3
0
    public void Setup()
    {
        Util.FindIfNotSet(this, ref behaviorSystem);
        resourceClaimer.Setup();
        contentController.Setup();

        behaviorSystem.onBehaviorDelete += (deleteEvent) =>
        {
            if (gameObject.activeSelf && BehaviorSystem.IdToEmbeddedBehaviorUri(deleteEvent.id) == cardUri)
            {
                Open(null);
            }
        };
    }
Ejemplo n.º 4
0
    public UnassignedBehavior MakeCopy()
    {
        Behaviors.Behavior behavior   = GetBehavior();
        string             behaviorId = behaviorSystem.GenerateUniqueId();

        Debug.Log(behavior.label + ", " + behavior.metadataJson + ", " + behavior.javascript);
        behaviorSystem.PutBehavior(behaviorId, new Behaviors.Behavior
        {
            label        = behavior.label,
            metadataJson = behavior.metadataJson,
            javascript   = behavior.javascript
        });
        return(new UnassignedBehavior(BehaviorSystem.IdToEmbeddedBehaviorUri(behaviorId), behaviorSystem));
    }
    public UnassignedBehavior CreateNewBehavior(string initCode, string metadataJson)
    {
        string behaviorId = GetBehaviorSystem().GenerateUniqueId();

        GetBehaviorSystem().PutBehavior(behaviorId, new Behaviors.Behavior
        {
            label        = "Custom",
            metadataJson = metadataJson,
            javascript   = initCode
        });

        UnassignedBehavior newBehavior = new UnassignedBehavior(BehaviorSystem.IdToEmbeddedBehaviorUri(behaviorId), GetBehaviorSystem());

        // NOTE: We don't need to add it here, per se. The caller should call AddBehavior on us with this instance.
        return(newBehavior);
    }
Ejemplo n.º 6
0
    private void OnBehaviorPut(BehaviorSystem.PutEvent putEvent)
    {
        if (!IsOpen())
        {
            return;
        }

        if (putEvent.isNewBehavior)
        {
            GameObject containerObj = CreateCardAndContainer();
            PopulateContainerWithUri(containerObj, BehaviorSystem.IdToEmbeddedBehaviorUri(putEvent.id));
            StartCoroutine(ScrollToAndFlash(containerObj));
            CardContainer container = containerObj.GetComponentInChildren <CardContainer>();
            containerObj.SetActive(DoesCardMatchSearch(container.GetCard(), cardLibraryUI.inputField.text));
            if (selection != null)
            {
                StartSelectionForContainerObj(containerObj);
            }
        }
        else
        {
            foreach (GameObject containerObj in cardContainerObjects)
            {
                CardContainer container = containerObj.GetComponentInChildren <CardContainer>();
                if (container.GetCard().GetModel().GetId() == putEvent.id)
                {
                    PopulateContainerWithUri(containerObj, BehaviorSystem.IdToEmbeddedBehaviorUri(putEvent.id));
                    break;
                }
            }
        }

        if (cardDetail.IsOpen() && cardDetail.IsModelValid() && cardDetail.GetModel().GetId() == putEvent.id)
        {
            cardDetail.Populate(
                new BehaviorCards.UnassignedCard(new UnassignedBehavior(
                                                     BehaviorSystem.IdToEmbeddedBehaviorUri(putEvent.id), behaviorSystem)));
        }
    }
Ejemplo n.º 7
0
    // Returns the brain ID of the imported brain in the active system. Ideally
    // wouldn't have this 'system' dependency..I think we need to move the
    // RPC/replication logic all into this class. Makes sense that the database
    // is responsible for keeping itself sync'd.
    public string ImportBrain(Database.Jsonable exported, string expectedBrainId)
    {
        exported.AssertValid();

        BeginBatchEdit("ImportBrain");
        try
        {
            exported.PerformUpgrades(new HashSet <string> {
                expectedBrainId
            });
            exported.AssertValid();

            Debug.Assert(exported.brainIds.Length == 1);
            Debug.Assert(exported.brains.Length == 1);
            Debug.Assert(exported.brainIds[0] == expectedBrainId);

            // Import all embedded behaviors, but give them new IDs
            Dictionary <string, string> exported2importedBehaviorId = new Dictionary <string, string>();
            for (int i = 0; i < exported.behaviorIds.Length; i++)
            {
                string   expId    = exported.behaviorIds[i];
                Behavior behavior = exported.behaviors[i];
                string   impId    = GenerateUniqueId();
                exported2importedBehaviorId[expId] = impId;
                PutBehavior(impId, behavior);
            }

            Brain brain = exported.brains[0];

            // Convert all embedded behavior URIs to the imported ones.
            for (int i = 0; i < brain.behaviorUses.Length; i++)
            {
                var use = brain.behaviorUses[i];

                if (IsEmbeddedBehaviorUri(use.behaviorUri))
                {
                    if (!exported2importedBehaviorId.ContainsKey(use.behaviorUri))
                    {
                        // If the behavior URI doesn't exist, it was not exported. So just
                        // assume this is meant to refer to some behavior that already
                        // exists in the scene. Intended for scene actor library.
                        // Check that it does exist.
                        var existing = GetBehaviorData(use.behaviorUri);
                        Debug.Assert(existing.javascript != null);
                        continue;
                    }

                    // Freshly imported as a new behavior - update the URI.
                    string expBehaviorId = BehaviorSystem.GetIdOfBehaviorUri(use.behaviorUri);
                    string impBehaviorId = exported2importedBehaviorId[expBehaviorId];
                    use.behaviorUri       = BehaviorSystem.IdToEmbeddedBehaviorUri(impBehaviorId);
                    brain.behaviorUses[i] = use;
                }
            }

            // Ok, ready to go in! We want to import into a different brain ID,
            // though.
            string importedBrainId = GenerateUniqueId();
            PutBrain(importedBrainId, brain);
            return(importedBrainId);
        }
        catch (System.Exception e)
        {
            Util.LogError($"ImportBrain exception: {e}");
            throw e;
        }
        finally
        {
            EndBatchEdit("ImportBrain");
        }
    }