Ejemplo n.º 1
0
    public void ExportBrain(string brainId, Behaviors.Database destination)
    {
        // Export the brain itself.
        Brain brain = GetBrain(brainId).DeepClone();

        destination.brains.Set(brainId, brain);

        // Export the needed embedded behaviors. Don't need to export builtins -
        // they're always available.

        HashSet <string> exportedEmbeddedBehaviorIds = new HashSet <string>();

        foreach (var use in brain.behaviorUses)
        {
            if (BehaviorSystem.IsEmbeddedBehaviorUri(use.behaviorUri))
            {
                exportedEmbeddedBehaviorIds.Add(GetIdOfBehaviorUri(use.behaviorUri));
            }
        }

        foreach (var behaviorId in exportedEmbeddedBehaviorIds)
        {
            destination.behaviors.Set(behaviorId, db.GetBehavior(behaviorId));
        }
    }
Ejemplo n.º 2
0
    public void TestBehaviorDbGarbageCollectDeleteUnusedBehaviors()
    {
        var db = new Behaviors.Database();

        db.behaviors.Set("A", new Behaviors.Behavior {
            javascript = "test();"
        });
        db.behaviors.Set("B", new Behaviors.Behavior {
            javascript = "test();"
        });
        db.behaviors.Set("C", new Behaviors.Behavior {
            javascript = "test();"
        });
        db.behaviors.Set("D", new Behaviors.Behavior {
            javascript = "test();"
        });

        db.brains.Set("b0", new Brain
        {
            behaviorUses = new BehaviorUse[] {
                new BehaviorUse {
                    id = "u00", behaviorUri = "embedded:B"
                },
                new BehaviorUse {
                    id = "u01", behaviorUri = "embedded:C"
                }
            }
        });

        db.brains.Set("b1", new Brain
        {
            behaviorUses = new BehaviorUse[] {
                new BehaviorUse {
                    id = "u10", behaviorUri = "embedded:C"
                },
                new BehaviorUse {
                    id = "u11", behaviorUri = "embedded:D"
                }
            }
        });

        var usedBrainIds = new HashSet <string> {
            "b0"
        };

        db.GarbageCollect(true, usedBrainIds);

        Assert.IsTrue(db.brains.Exists("b0"), "Used brain not deleted");

        Assert.IsFalse(db.brains.Exists("b1"), "Unused brain is deleted");

        // Only used behaviors left..
        Assert.IsFalse(db.behaviors.Exists("A"));
        Assert.IsTrue(db.behaviors.Exists("B"));
        Assert.IsTrue(db.behaviors.Exists("C"));
        Assert.IsFalse(db.behaviors.Exists("D"));
    }
Ejemplo n.º 3
0
    SavedActorPrefab ToPrefab(VoosActor root, Texture2D thumbnail)
    {
        List <VoosActor.PersistedState> savedActors = new List <VoosActor.PersistedState>();

        Behaviors.Database brainDatabase = new Behaviors.Database();
        List <Sojo.Saved>  sojos         = new List <Sojo.Saved>();

        void ExportActorRecursive(VoosActor actor)
        {
            savedActors.Add(actor.Save());
            actor.GetBehaviorSystem().ExportBrain(actor.GetBrainName(), brainDatabase);

            foreach (string sojoId in GetUsedSojoIds(actor))
            {
                Sojo sojo = sojoSystem.GetSojoById(sojoId);
                if (sojo == null)
                {
                    Util.LogError($"Could not find sojo of id {sojoId} for actor {actor.name}");
                    continue;
                }
                sojos.Add(sojo.Save());
            }

            foreach (VoosActor child in actor.GetChildActors())
            {
                ExportActorRecursive(child);
            }
        }

        ExportActorRecursive(root);

        return(new SavedActorPrefab
        {
            version = SavedActorPrefab.CurrentVersion,
            label = root.GetDisplayName(),
            thumbnailJZB64 = System.Convert.ToBase64String(Util.TextureToZippedJpeg(thumbnail)),
            brainDatabase = brainDatabase.Save(),
            sojos = sojos.ToArray(),
            actors = savedActors.ToArray()
        });
    }