Exemplo n.º 1
0
        public void RestoreTest_UTinyEntity()
        {
            var compType    = registry.CreateType(UTinyId.New(), "TestComponent", UTinyTypeCode.Component);
            var compTypeRef = (UTinyType.Reference)compType;

            var testStructType = registry.CreateType(UTinyId.New(), "TestStruct", UTinyTypeCode.Struct);

            testStructType.CreateField(UTinyId.New(), "IntField", (UTinyType.Reference)UTinyType.Int32);

            compType.CreateField(UTinyId.New(), "TestStructField", (UTinyType.Reference)testStructType);

            var undo = new Dictionary <UTinyId, IMemento>();

            caretaker.OnObjectChanged += (originator, memento) =>
            {
                undo[originator.Id] = memento;
            };

            var entity    = registry.CreateEntity(UTinyId.New(), "TestEntity");
            var entityRef = (UTinyEntity.Reference)entity;

            var testCompInstance = entity.AddComponent(compTypeRef);

            testCompInstance.Refresh();

            var obj = new UTinyObject(registry, (UTinyType.Reference)testStructType)
            {
                ["IntField"] = 0
            };

            testCompInstance["TestStructField"] = obj;
            var item = (UTinyObject)testCompInstance["TestStructField"];

            // Update to get the initial state; flush changes
            caretaker.Update();

            item["IntField"] = 123;
            Assert.AreEqual(123, item["IntField"]);

            // UNDO
            entity.Restore(undo[entity.Id]);

            entity = entityRef.Dereference(context.Registry);
            Assert.NotNull(entity);
            testCompInstance = entity.GetComponent(compTypeRef);
            Assert.NotNull(testCompInstance);
            item = (UTinyObject)testCompInstance["TestStructField"];
            Assert.NotNull(item);

            // make sure IntField was restored
            Assert.AreEqual(0, item["IntField"]);
        }
Exemplo n.º 2
0
        public void FlatJsonEntityRoundTrip()
        {
            var registry = new UTinyRegistry();

            var type = registry.CreateType(
                UTinyId.New(),
                "TestType",
                UTinyTypeCode.Component
                );

            type.CreateField("TestIntField", (UTinyType.Reference)UTinyType.Int32);
            type.CreateField("TestStringField", (UTinyType.Reference)UTinyType.String);

            var entity = registry.CreateEntity(
                UTinyId.New(),
                "TestEntity");

            var component = entity.AddComponent((UTinyType.Reference)type);

            component.Refresh();

            component["TestIntField"]    = 10;
            component["TestStringField"] = "Test";

            using (var json = new MemoryStream())
                using (var command = new MemoryStream())
                {
                    // Write the data model to a stream as json
                    // mem -> json
                    Serialization.FlatJson.BackEnd.Persist(json,
                                                           type,
                                                           entity);

                    json.Position = 0;

                    var reader = new StreamReader(json);
                    {
                        Debug.Log(reader.ReadToEnd());
                    }

                    json.Position = 0;

                    // Read the data model
                    // json -> commands
                    Serialization.FlatJson.FrontEnd.Accept(json, command);

                    command.Position = 0;

                    // Create a registry to hold accepted objects
                    var output = new UTinyRegistry();

                    // Process the command
                    // commands -> mem
                    Serialization.CommandStream.FrontEnd.Accept(command, output);
                }
        }
Exemplo n.º 3
0
        public void FlatJsonSceneWrite()
        {
            var registry = new UTinyRegistry();

            var entityGroup = registry.CreateEntityGroup(
                UTinyId.New(),
                "TestEntityGroup");

            var entity = registry.CreateEntity(
                UTinyId.New(),
                "TestEntity");

            entityGroup.AddEntityReference((UTinyEntity.Reference)entity);

            var json = Serialization.FlatJson.BackEnd.Persist(entityGroup, entity);

            Debug.Log(json);
        }
        public void SimpleBinaryRoundTrip()
        {
            var registry = new UTinyRegistry();
            var entity   = registry.CreateEntity(UTinyId.New(), "Entity");
            var entities = new IPropertyContainer[] { entity };

            using (var memory = new MemoryStream())
            {
                Serialization.Binary.BackEnd.Persist(memory, entities);
                memory.Position = 0;

                using (var commands = new MemoryStream())
                {
                    Serialization.Binary.FrontEnd.Accept(memory, commands);
                    commands.Position = 0;

                    var output = new UTinyRegistry();
                    Serialization.CommandStream.FrontEnd.Accept(commands, output);

                    var readEntity = output.FindById <UTinyEntity>(entity.Id);
                    Assert.NotNull(readEntity);
                }
            }
        }
        public void BinaryEntityPerformance()
        {
            var registry = new UTinyRegistry();

            var vector3Type = registry.CreateType(
                UTinyId.New(),
                "Vector3",
                UTinyTypeCode.Struct);

            vector3Type.CreateField("X", (UTinyType.Reference)UTinyType.Float32);
            vector3Type.CreateField("Y", (UTinyType.Reference)UTinyType.Float32);
            vector3Type.CreateField("Z", (UTinyType.Reference)UTinyType.Float32);

            var transformType = registry.CreateType(
                UTinyId.New(),
                "Transform",
                UTinyTypeCode.Component);

            transformType.CreateField("Position", (UTinyType.Reference)vector3Type);
            transformType.CreateField("Scale", (UTinyType.Reference)vector3Type);

            const int kCount   = 1000;
            var       entities = new UTinyEntity[kCount];
            var       transformTypeReference = (UTinyType.Reference)transformType;

            {
                var watch = System.Diagnostics.Stopwatch.StartNew();

                for (var i = 0; i < kCount; i++)
                {
                    entities[i] = registry.CreateEntity(UTinyId.New(), "Entity_" + i);
                    var transform = entities[i].AddComponent(transformTypeReference);

                    // if (i < kCount)
                    {
                        transform.Refresh(null, true);

                        var position = transform["Position"] as UTinyObject;
                        position["X"] = i * 2f;
                    }
                }

                watch.Stop();
                Debug.Log($"Create Objects Entities=[{kCount}] {watch.ElapsedMilliseconds}ms");
            }

            using (var binary = new MemoryStream())
                using (var command = new MemoryStream())
                {
                    // Write the data model to a stream as json
                    // mem -> command

                    {
                        var watch = System.Diagnostics.Stopwatch.StartNew();

                        Serialization.Binary.BackEnd.Persist(binary, (IEnumerable <UTinyEntity>)entities);

                        watch.Stop();
                        Debug.Log($"Binary.BackEnd.Persist Entities=[{kCount}] {watch.ElapsedMilliseconds}ms Len=[{binary.Position}]");
                    }

                    binary.Position = 0;

                    // Push the types to the command stream before the entities
                    Serialization.CommandStream.BackEnd.Persist(command, vector3Type, transformType);

                    {
                        var watch = System.Diagnostics.Stopwatch.StartNew();

                        Serialization.Binary.FrontEnd.Accept(binary, command);

                        watch.Stop();
                        Debug.Log($"Binary.FrontEnd.Accept Entities=[{kCount}] {watch.ElapsedMilliseconds}ms Len=[{command.Position}]");
                    }

                    command.Position = 0;

                    // Create a registry to hold accepted objects
                    var output = new UTinyRegistry();

                    // Process the command
                    // commands -> mem
                    {
                        var watch = System.Diagnostics.Stopwatch.StartNew();

                        Serialization.CommandStream.FrontEnd.Accept(command, output);

                        watch.Stop();
                        Debug.Log($"CommandStream.FrontEnd.Accept Entities=[{kCount}] {watch.ElapsedMilliseconds}ms");
                    }
                }
        }