Exemple #1
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);
                }
        }
Exemple #2
0
        public void FlatJsonRoundTrip()
        {
            var input = new UTinyRegistry();

            var structType = input.CreateType(
                UTinyId.New(),
                "TestStruct",
                UTinyTypeCode.Struct);

            structType.CreateField("IntField", (UTinyType.Reference)UTinyType.Int32);
            structType.CreateField("FloatField", (UTinyType.Reference)UTinyType.Int32);
            structType.CreateField("StringField", (UTinyType.Reference)UTinyType.Int32);

            var module = input.CreateModule(
                UTinyId.New(),
                "TestModule");

            module.AddStructReference((UTinyType.Reference)structType);

            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,
                                                           structType,
                                                           module);

                    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);

                    Assert.IsNotNull(output.FindById <UTinyType>(structType.Id));
                    Assert.IsNotNull(output.FindById <UTinyModule>(module.Id));
                }
        }
Exemple #3
0
        public void FlatJsonProjectWrite()
        {
            var registry = new UTinyRegistry();

            var project = registry.CreateProject(
                UTinyId.New(),
                "TestProject");

            var json = Serialization.FlatJson.BackEnd.Persist(project);

            Debug.Log(json);
        }
Exemple #4
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"]);
        }
Exemple #5
0
        public void RestoreTest_Lists_Containers()
        {
            // Create two new types
            var testStructType    = registry.CreateType(UTinyId.New(), "TestStructType", UTinyTypeCode.Struct);
            var testStructTypeRef = (UTinyType.Reference)testStructType;

            var listField = testStructType.CreateField(UTinyId.New(), "TestListField", (UTinyType.Reference)UTinyType.Float32, true);

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

            // Register for changed events
            caretaker.OnObjectChanged += (originator, memento) =>
            {
                undo[originator.Id] = memento;
            };

            // Update to get the initial state; flush changes
            caretaker.Update();
            // note: UTinyField proxies version storage onto UTinyType, so there should be only 1 memento
            Assert.AreEqual(1, undo.Count);

            {
                // Make some changes to the created type
                testStructType.Name = "OtherTestStructType";
                Assert.AreEqual(testStructType.Name, "OtherTestStructType");
                listField.Name = "RevertMe";
                Assert.AreEqual(listField.Name, "RevertMe");

                // revert changes
                var kvp = undo.First();
                var obj = registry.FindById <UTinyType>(kvp.Key);

                Assert.NotNull(obj);
                Assert.IsTrue(ReferenceEquals(obj, testStructType));

                obj.Restore(kvp.Value);

                obj = testStructTypeRef.Dereference(context.Registry);
                Assert.NotNull(obj);

                // the field was detached from the list and re-created
                Assert.AreEqual(1, obj.Fields.Count);
                var newListField = obj.Fields[0];

                Assert.AreEqual("TestStructType", obj.Name);
                Assert.AreEqual("TestListField", newListField.Name);
                Assert.AreEqual(listField.Id, newListField.Id);
                Assert.AreEqual(listField.DeclaringType.Id, newListField.DeclaringType.Id);
            }
        }
        public void Clear()
        {
            var registry     = new UTinyRegistry();
            var builtInCount = registry.Count;

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

            Assert.AreEqual(builtInCount + 1, registry.Count);

            registry.Clear();
            Assert.AreEqual(builtInCount, registry.Count);
            Assert.IsNull(typeRef.Dereference(registry));
        }
        public void Register()
        {
            var registry     = new UTinyRegistry();
            var builtInCount = registry.Count;

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

            Assert.AreEqual(builtInCount + 1, registry.Count);

            registry.Register(type);
            Assert.AreEqual(builtInCount + 1, registry.Count);

            registry.Unregister(type);
            Assert.AreEqual(builtInCount, registry.Count);
        }
        public void SetUp()
        {
            m_Registry = new UTinyRegistry();

            m_TestStruct = m_Registry.CreateType(UTinyId.New(), "TestStruct", UTinyTypeCode.Struct);
            m_TestStruct.CreateField("Foo", (UTinyType.Reference)UTinyType.String);
            m_TestStruct.CreateField("Bar", (UTinyType.Reference)UTinyType.Int32);

            m_TestStructWithList = m_Registry.CreateType(UTinyId.New(), "TestStructWithList", UTinyTypeCode.Struct);
            m_TestStructWithList.CreateField("Foo", (UTinyType.Reference)UTinyType.String, true);
            m_TestStructWithList.CreateField("Bar", (UTinyType.Reference)UTinyType.Int32, true);

            m_TestComponent = m_Registry.CreateType(UTinyId.New(), "TestComponent", UTinyTypeCode.Component);
            m_TestComponent.CreateField("TestStructField", (UTinyType.Reference)m_TestStruct);
        }
Exemple #9
0
        public void StructType()
        {
            var registry = new UTinyRegistry();

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

            type.CreateField(
                "TestField",
                (UTinyType.Reference)UTinyType.Int32);

            Assert.AreEqual(type.Fields.Count, 1);
        }
        public void SourceScope()
        {
            var registry     = new UTinyRegistry();
            var builtInCount = registry.Count;
            var sourceId     = "test";

            using (registry.SourceIdentifierScope(sourceId))
            {
                registry.CreateType(UTinyId.New(), "TestType", UTinyTypeCode.Component);
            }

            Assert.AreEqual(builtInCount + 1, registry.Count);

            registry.UnregisterAllBySource(sourceId);
            Assert.AreEqual(builtInCount, registry.Count);
        }
Exemple #11
0
        public void FlatJsonTypeWrite()
        {
            var registry = new UTinyRegistry();

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

            type.CreateField("IntField", (UTinyType.Reference)UTinyType.Int32);
            type.CreateField("FloatField", (UTinyType.Reference)UTinyType.Int32);
            type.CreateField("StringField", (UTinyType.Reference)UTinyType.Int32);

            var json = Serialization.FlatJson.BackEnd.Persist(type);

            Debug.Log(json);
        }
Exemple #12
0
        public void NestedTypeInitialDefaultValue()
        {
            var registry = new UTinyRegistry();

            // Create a struct with a single int field
            var structType = registry.CreateType(
                UTinyId.New(),
                "TestStruct",
                UTinyTypeCode.Struct);

            structType.CreateField(
                "TestIntField",
                (UTinyType.Reference)UTinyType.Int32);

            // Default the TestStruct.IntField to 7
            structType.DefaultValue = new UTinyObject(registry, (UTinyType.Reference)structType)
            {
                ["TestIntField"] = 7
            };

            // Create a component with a single TestStruct field
            var componentType = registry.CreateType(
                UTinyId.New(),
                "TestComponent",
                UTinyTypeCode.Component);

            componentType.CreateField(
                "TestStructField",
                (UTinyType.Reference)structType);

            // Grab the default value for TestComponent
            var testComponentDefaultValue = componentType.DefaultValue as UTinyObject;

            Assert.IsNotNull(testComponentDefaultValue);

            // Grab the TestComponent.TestStructField FIELD defaultValue
            // NOTE: This is NOT the same as the TestStruct TYPE defaultValue
            var testComponentTestStructFieldDefaultValue = testComponentDefaultValue["TestStructField"] as UTinyObject;

            Assert.IsNotNull(testComponentTestStructFieldDefaultValue);

            Assert.AreNotEqual(testComponentDefaultValue, testComponentTestStructFieldDefaultValue);

            // This value should have been inherited from the type level but CAN be overriden
            Assert.AreEqual(7, testComponentTestStructFieldDefaultValue["TestIntField"]);
        }
Exemple #13
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 ListField()
        {
            var registry = new UTinyRegistry();

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

            type.CreateField("TestField", (UTinyType.Reference)UTinyType.Int32, true);

            var instance = new UTinyObject(registry, (UTinyType.Reference)type)
            {
                ["TestField"] = new UTinyList(registry, (UTinyType.Reference)UTinyType.Int32)
                {
                    1, 2, 3
                }
            };

            Debug.Log(instance);
        }
Exemple #15
0
        public void DetectComponentChanges()
        {
            // Create a type and an entity
            var componentType = registry.CreateType(UTinyId.New(), "TestComponentType", UTinyTypeCode.Component);
            var testField     = componentType.CreateField("TestField", (UTinyType.Reference)UTinyType.Int32);
            var entity        = registry.CreateEntity(UTinyId.New(), "TestEntity");
            var component     = entity.AddComponent((UTinyType.Reference)componentType);

            component.Refresh();

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

            // Register for changed events
            caretaker.OnObjectChanged += (originator, memento) =>
            {
                Debug.Log(memento);
            };

            {
                (componentType.DefaultValue as UTinyObject)["TestField"] = 5;
                componentType.Refresh();

                // Invoke update
                // This will detect any changes that were made between now and the last Update.
                Debug.Log("-------------------- UPDATE --------------------");
                caretaker.Update();

                component["TestField"] = 10;

                // Invoke update
                // This will detect any changes that were made between now and the last Update.
                Debug.Log("-------------------- UPDATE --------------------");
                caretaker.Update();

                testField.FieldType = (UTinyType.Reference)UTinyType.String;
                component.Refresh();

                // Invoke update
                // This will detect any changes that were made between now and the last Update.
                Debug.Log("-------------------- UPDATE --------------------");
                caretaker.Update();
            }
        }
Exemple #16
0
        public void ResetObjectToDefaultValues()
        {
            var registry = new UTinyRegistry();

            // Create a type
            var type = registry.CreateType(
                UTinyId.New(),
                "TestStructType",
                UTinyTypeCode.Struct);

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

            type.CreateField(
                "TestFloatField",
                (UTinyType.Reference)UTinyType.Float32);

            // Default the TestStruct.IntField to 7 and FloatField to 0.5f
            type.DefaultValue = new UTinyObject(registry, (UTinyType.Reference)type)
            {
                ["TestIntField"]   = 7,
                ["TestFloatField"] = 0.5f
            };

            var @object = new UTinyObject(registry, (UTinyType.Reference)type);

            Assert.AreEqual(7, @object["TestIntField"]);
            Assert.AreEqual(0.5f, @object["TestFloatField"]);

            @object["TestIntField"]   = 1;
            @object["TestFloatField"] = 7.9f;

            Assert.AreEqual(1, @object["TestIntField"]);
            Assert.AreEqual(7.9f, @object["TestFloatField"]);

            @object.Reset();

            Assert.AreEqual(7, @object["TestIntField"]);
            Assert.AreEqual(0.5f, @object["TestFloatField"]);
        }
Exemple #17
0
        public void StreamingRoundTrip()
        {
            var input = new UTinyRegistry();

            var type = input.CreateType(
                UTinyId.New(),
                "TestStruct",
                UTinyTypeCode.Struct);

            type.CreateField("IntField", (UTinyType.Reference)UTinyType.Int32);
            type.CreateField("FloatField", (UTinyType.Reference)UTinyType.Int32);
            type.CreateField("StringField", (UTinyType.Reference)UTinyType.Int32);

            var module = input.CreateModule(
                UTinyId.New(),
                "TestModule");

            module.AddStructReference((UTinyType.Reference)type);

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

                command.Position = 0;

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

                // Process the command
                // commands -> mem
                FrontEnd.Accept(command, output);

                Assert.IsNotNull(output.FindById <UTinyType>(type.Id));
                Assert.IsNotNull(output.FindById <UTinyModule>(module.Id));
            }
        }
Exemple #18
0
        public void NameChangeTest()
        {
            var registry = new UTinyRegistry();

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

            var module = registry.CreateModule(
                UTinyId.New(),
                "TestModule"
                );

            module.AddStructReference((UTinyType.Reference)type);
            module.Refresh();

            type.Name = "NewStruct";

            Debug.Log(module.ToString());
        }
Exemple #19
0
        public void SetUp()
        {
            m_Registry = new UTinyRegistry();

            m_EnumType = m_Registry.CreateType(
                UTinyId.New(),
                "TestEnum",
                UTinyTypeCode.Enum);

            m_EnumType.BaseType = (UTinyType.Reference)UTinyType.Int32;

            m_EnumType.CreateField("A", (UTinyType.Reference)UTinyType.Int32);
            m_EnumType.CreateField("B", (UTinyType.Reference)UTinyType.Int32);
            m_EnumType.CreateField("C", (UTinyType.Reference)UTinyType.Int32);

            m_EnumType.DefaultValue = new UTinyObject(m_Registry, (UTinyType.Reference)m_EnumType)
            {
                // @NOTE We are intentionally starting at 1 to detect 0 case as errors
                ["A"] = 1,
                ["B"] = 2,
                ["C"] = 3
            };

            // Create a component with a single int field
            m_ComponentType = m_Registry.CreateType(
                UTinyId.New(),
                "TestComponent",
                UTinyTypeCode.Component);

            m_ComponentType.CreateField(
                "TestEnumField",
                (UTinyType.Reference)m_EnumType);

            m_Entity = m_Registry.CreateEntity(UTinyId.New(), "TestEntity");
            var component = m_Entity.AddComponent((UTinyType.Reference)m_ComponentType);

            component.Refresh();
        }
Exemple #20
0
        public void FlatJsonProjectRoundTrip()
        {
            var registry = new UTinyRegistry();

            var project = registry.CreateProject(
                UTinyId.New(),
                "TestProject");

            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, project);

                    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);
                }
        }
Exemple #21
0
        public void DetectEntityChanges()
        {
            // Create a type and an entity
            var componentType = registry.CreateType(UTinyId.New(), "TestStructType", UTinyTypeCode.Component);
            var entity        = registry.CreateEntity(UTinyId.New(), "TestEntity");

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

            {
                // Snapshot the initial version
                var entityVersion = entity.Version;

                // Make some changes to the data model
                // NOTE: We can make as many changes as we want with no callbacks being invoked. It is simply a version increment
                entity.AddComponent((UTinyType.Reference)componentType);
                entity.Name = "NewEntityName";

                var count = 0;

                // Register for changed events
                caretaker.OnObjectChanged += (originator, memento) =>
                {
                    count++;
                    Assert.AreEqual(originator, entity);
                };

                // Invoke update
                // This will detect any changes that were made between now and the last Update.
                caretaker.Update();

                Assert.AreNotEqual(entityVersion, entity.Version);

                // We should be notified that one object was changed
                Assert.AreEqual(1, count);
            }
        }
        public void SetUp()
        {
            m_Registry = new UTinyRegistry();

            // Create a component with a single Texture2D field
            m_ComponentType = m_Registry.CreateType(
                UTinyId.New(),
                "TestComponent",
                UTinyTypeCode.Component);

            m_ComponentType.CreateField(
                "TestTexture2DField",
                (UTinyType.Reference)UTinyType.Texture2DEntity);

            m_Entity = m_Registry.CreateEntity(UTinyId.New(), "TestEntity");
            var component = m_Entity.AddComponent((UTinyType.Reference)m_ComponentType);

            component.Refresh();

            // Create some asset on disc
            File.WriteAllBytes(Application.dataPath + "/TestTexture.png", new Texture2D(32, 32).EncodeToPNG());
            AssetDatabase.ImportAsset("Assets/TestTexture.png");
            m_Texture2D = AssetDatabase.LoadAssetAtPath <Texture2D>("Assets/TestTexture.png");
        }
        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 SetUp()
        {
            m_Registry = new UTinyRegistry();

            // Create a component with a single int field
            m_ComponentType = m_Registry.CreateType(
                UTinyId.New(),
                "TestComponent",
                UTinyTypeCode.Component);

            m_ComponentType.CreateField(
                "TestIntField",
                (UTinyType.Reference)UTinyType.Int32);

            // Default the TestStruct.IntField to 7
            m_ComponentType.DefaultValue = new UTinyObject(m_Registry, (UTinyType.Reference)m_ComponentType)
            {
                ["TestIntField"] = KTestFieldDefaultValue
            };

            // Create an entity with our test component
            m_DefaultEntity = m_Registry.CreateEntity(UTinyId.New(), "DefaultEntity");

            {
                var c = m_DefaultEntity.AddComponent((UTinyType.Reference)m_ComponentType);
                c.Refresh();
            }

            // Create another entity with our test component
            m_OverridenEntity = m_Registry.CreateEntity(UTinyId.New(), "OverridenEntity");
            {
                var c = m_OverridenEntity.AddComponent((UTinyType.Reference)m_ComponentType);
                c.Refresh();
                c["TestIntField"] = KTestFieldOverrideValue;
            }
        }
Exemple #25
0
        public void PerformanceTest()
        {
            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 Entities=[{kCount}] {watch.ElapsedMilliseconds}ms");
            }

            caretaker.OnObjectChanged += (originiator, memento) =>
            {
                // Force the callback
            };

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

                caretaker.Update();

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

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

                caretaker.Update();

                watch.Stop();
                Debug.Log($"Caretaker.Update Entities=[{kCount}] {watch.ElapsedMilliseconds}ms");
            }
        }
Exemple #26
0
        public void SetUp()
        {
            m_Registry = new UTinyRegistry();

            // Create a component with an int array field
            m_IntArrayComponentType = m_Registry.CreateType(
                UTinyId.New(),
                "TestComponent",
                UTinyTypeCode.Component);

            m_IntArrayComponentType.CreateField(
                "TestIntArrayField",
                (UTinyType.Reference)UTinyType.Int32,
                true);

            m_IntArrayEntity = m_Registry.CreateEntity(UTinyId.New(), "TestEntity");
            var component = m_IntArrayEntity.AddComponent((UTinyType.Reference)m_IntArrayComponentType);

            component.Refresh();

            component["TestIntArrayField"] = new UTinyList(m_Registry, (UTinyType.Reference)UTinyType.Int32)
            {
                3, 6, 9
            };

            m_StructType = m_Registry.CreateType(
                UTinyId.New(),
                "TestStruct",
                UTinyTypeCode.Struct);

            m_StructType.CreateField(
                "TestIntField",
                (UTinyType.Reference)UTinyType.Int32);

            m_StructArrayComponentType = m_Registry.CreateType(
                UTinyId.New(),
                "TestComponent2",
                UTinyTypeCode.Component);

            m_StructArrayComponentType.CreateField(
                "TestStructArrayField",
                (UTinyType.Reference)m_StructType,
                true);

            m_StructArrayEntity = m_Registry.CreateEntity(UTinyId.New(), "TestEntity2");
            var component2 = m_StructArrayEntity.AddComponent((UTinyType.Reference)m_StructArrayComponentType);

            component2.Refresh();
            component2["TestStructArrayField"] = new UTinyList(m_Registry, (UTinyType.Reference)m_StructType)
            {
                new UTinyObject(m_Registry, (UTinyType.Reference)m_StructType)
                {
                    ["TestIntField"] = 3
                },
                new UTinyObject(m_Registry, (UTinyType.Reference)m_StructType)
                {
                    ["TestIntField"] = 6
                },
                new UTinyObject(m_Registry, (UTinyType.Reference)m_StructType)
                {
                    ["TestIntField"] = 9
                }
            };
        }
        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");
                    }
                }
        }
Exemple #28
0
        public void NestedTypePropagateDefaultValueChange()
        {
            var registry = new UTinyRegistry();

            // Create a struct with 2 fields
            var testStructType = registry.CreateType(
                UTinyId.New(),
                "TestStructType",
                UTinyTypeCode.Struct);

            testStructType.CreateField(
                "TestIntField",
                (UTinyType.Reference)UTinyType.Int32);

            testStructType.CreateField(
                "TestFloatField",
                (UTinyType.Reference)UTinyType.Float32);

            // Default the TestStruct.IntField to 7 and FloatField to 0.5f
            testStructType.DefaultValue = new UTinyObject(registry, (UTinyType.Reference)testStructType)
            {
                ["TestIntField"]   = 7,
                ["TestFloatField"] = 0.5f
            };

            // Create a component with a single TestStruct field
            var testComponentType = registry.CreateType(
                UTinyId.New(),
                "TestComponentType",
                UTinyTypeCode.Component);

            testComponentType.CreateField(
                "TestStructField",
                (UTinyType.Reference)testStructType);

            // Sanity check
            // NOTE: This is covered in other tests
            {
                var testComponentTypeDefaultValue = testComponentType.DefaultValue as UTinyObject;
                Assert.IsNotNull(testComponentTypeDefaultValue);

                var testComponentTypeTestStructFieldDefaultValue = testComponentTypeDefaultValue["TestStructField"] as UTinyObject;
                Assert.IsNotNull(testComponentTypeTestStructFieldDefaultValue);

                // This value should have been inherited from the type level but CAN be overridden
                Assert.AreEqual(7, testComponentTypeTestStructFieldDefaultValue["TestIntField"]);
                Assert.AreEqual(0.5, testComponentTypeTestStructFieldDefaultValue["TestFloatField"]);
            }

            {
                var testComponentTypeDefaultValue = (UTinyObject)testComponentType.DefaultValue;
                Assert.IsNotNull(testComponentTypeDefaultValue);

                var testComponentTypeTestStructFieldDefaultValue = testComponentTypeDefaultValue["TestStructField"] as UTinyObject;
                Assert.IsNotNull(testComponentTypeTestStructFieldDefaultValue);

                // Override the default value of the TestComponent.TestStructField.FloatField to 2.5f
                testComponentTypeTestStructFieldDefaultValue["TestFloatField"] = 2.5f;
            }

            {
                var testStructTypeDefaultValue = (UTinyObject)testStructType.DefaultValue;
                Assert.IsNotNull(testStructTypeDefaultValue);

                // Update the default value of TestStruct.IntField to 10
                testStructTypeDefaultValue["TestIntField"] = 10;
            }

            {
                var testComponentTypeDefaultValue = (UTinyObject)testComponentType.DefaultValue;
                Assert.IsNotNull(testComponentTypeDefaultValue);

                var testComponentTypeTestStructFieldDefaultValue = testComponentTypeDefaultValue["TestStructField"] as UTinyObject;
                Assert.IsNotNull(testComponentTypeTestStructFieldDefaultValue);

                // The IntField should have been correctly updated while the float field should remain overridden
                Assert.AreEqual(10, testComponentTypeTestStructFieldDefaultValue["TestIntField"]);
                Assert.AreEqual(2.5f, testComponentTypeTestStructFieldDefaultValue["TestFloatField"]);
            }
        }
Exemple #29
0
 public void SetUp()
 {
     m_Registry = new UTinyRegistry();
     m_Module   = m_Registry.CreateModule(UTinyId.New(), "TestModule");
 }
Exemple #30
0
        public void RestoreTest_FieldTypeChanged()
        {
            //////////////////////////////////////////////////////////////
            // Setup for this specific test.
            //////////////////////////////////////////////////////////////
            var initialFieldType    = registry.FindByName <UTinyType>("Int32");
            var expectedInitialType = typeof(int);
            var changedFieldType    = registry.FindByName <UTinyType>("EntityReference");
            var expectedChangedType = typeof(UTinyEntity.Reference);

            IMemento state = null;

            // Register for changed events
            caretaker.OnObjectChanged += (originator, memento) =>
            {
                state = memento;
            };

            //////////////////////////////////////////////////////////////
            // 1. Create a component with a single field of type int.
            //////////////////////////////////////////////////////////////
            var componentType = registry.CreateType(UTinyId.New(), "Component", UTinyTypeCode.Component);
            var field         = componentType.CreateField(UTinyId.New(), "Field", (UTinyType.Reference)initialFieldType);

            componentType.Refresh();

            // Check default value
            {
                var defaultValue = componentType.DefaultValue as UTinyObject;
                Assert.IsNotNull(defaultValue);
                Assert.IsTrue(defaultValue.IsDefaultValue);
                Assert.IsTrue(field.FieldType.Equals((UTinyType.Reference)initialFieldType));
                Assert.AreEqual(expectedInitialType, defaultValue["Field"].GetType());
                Assert.AreEqual(defaultValue["Field"], 0);
            }

            Debug.Log($"Initial State: {componentType.Id}: {(componentType.DefaultValue as UTinyObject)["Field"]}");

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

            Assert.NotNull(initialState);

            //////////////////////////////////////////////////////////////
            // 2- Change the field type to be of EntityReference.
            //////////////////////////////////////////////////////////////
            field.FieldType = (UTinyType.Reference)changedFieldType;
            componentType.Refresh();

            // Check default value
            {
                var defaultValue = componentType.DefaultValue as UTinyObject;
                Assert.IsNotNull(defaultValue);
                Assert.IsTrue(defaultValue.IsDefaultValue);
                Assert.IsTrue(field.FieldType.Equals((UTinyType.Reference)changedFieldType));
                Assert.AreEqual(expectedChangedType, defaultValue["Field"].GetType());
                Assert.AreEqual(defaultValue["Field"], UTinyEntity.Reference.None);
            }

            Debug.Log($"Changed State: {componentType.Id}: {(componentType.DefaultValue as UTinyObject)["Field"]}");

            // Update to get the changed state; flush changes
            caretaker.Update();
            IMemento changedState = state;

            Assert.NotNull(changedState);
            Assert.AreNotEqual(initialState, changedState);
            Assert.IsTrue(initialState.Version < changedState.Version);

            //////////////////////////////////////////////////////////////
            // 3 - Restore it back to its initial field type (Undo).
            //////////////////////////////////////////////////////////////
            Debug.Log("Undo");
            Debug.Log($"Before: {componentType.Id}: {(componentType.DefaultValue as UTinyObject)["Field"]}");
            componentType.Restore(initialState);
            // Note: Restoring is not in-place, so we need to re-set the references
            componentType = registry.FindById <UTinyType>(componentType.Id);
            componentType.Refresh();
            field = componentType.Fields[0];
            Debug.Log($"After: {componentType.Id}: {(componentType.DefaultValue as UTinyObject)["Field"]}");
            // Check default value
            {
                var defaultValue = componentType.DefaultValue as UTinyObject;
                Assert.IsNotNull(defaultValue);
                Assert.IsTrue(defaultValue.IsDefaultValue);
                Assert.IsTrue(field.FieldType.Equals((UTinyType.Reference)initialFieldType));
                Assert.AreEqual(expectedInitialType, defaultValue["Field"].GetType());
                Assert.AreEqual(defaultValue["Field"], 0);
            }

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

            //////////////////////////////////////////////////////////////
            // 4- Restore the field type change (Redo).
            //////////////////////////////////////////////////////////////
            Debug.Log("Redo");
            Debug.Log($"Before: {componentType.Id}: {(componentType.DefaultValue as UTinyObject)["Field"]}");
            componentType.Restore(changedState);
            // Note: Restoring is not in-place, so we need to re-set the references
            componentType = registry.FindById <UTinyType>(componentType.Id);
            componentType.Refresh();
            field = componentType.Fields[0];
            Debug.Log($"After: {componentType.Id}: {(componentType.DefaultValue as UTinyObject)["Field"]}");
            // Check default value
            {
                var defaultValue = componentType.DefaultValue as UTinyObject;
                Assert.IsNotNull(defaultValue);
                Assert.IsTrue(defaultValue.IsDefaultValue);
                Assert.IsTrue(field.FieldType.Equals((UTinyType.Reference)changedFieldType));
                Assert.AreEqual(expectedChangedType, defaultValue["Field"].GetType());
                Assert.AreEqual(defaultValue["Field"], UTinyEntity.Reference.None);
            }
        }