示例#1
0
        public void FieldTest()
        {
            var registry = new UTinyRegistry();

            var @enum = registry.CreateType(
                UTinyId.New(),
                "TestEnum",
                UTinyTypeCode.Enum
                );

            @enum.BaseType = (UTinyType.Reference)UTinyType.Int32;
            @enum.CreateField("A", (UTinyType.Reference)UTinyType.Int32);

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

            type.CreateField(
                "TextureReference",
                (UTinyType.Reference)UTinyType.Texture2DEntity);

            type.CreateField(
                "EntityReference",
                (UTinyType.Reference)UTinyType.EntityReference);

            type.CreateField(
                "EnumReference",
                (UTinyType.Reference)@enum);

            type.Refresh();

            Debug.Log(type);
        }
示例#2
0
        public void EnumDefaultValue()
        {
            var registry = new UTinyRegistry();

            var enumType = registry.CreateType(UTinyId.New(), "TestEnum", UTinyTypeCode.Enum);

            enumType.BaseType = (UTinyType.Reference)UTinyType.Int32;
            enumType.CreateField("A", (UTinyType.Reference)UTinyType.Int32);
            enumType.CreateField("B", (UTinyType.Reference)UTinyType.Int32);
            enumType.CreateField("C", (UTinyType.Reference)UTinyType.Int32);
            enumType.DefaultValue = new UTinyObject(registry, (UTinyType.Reference)enumType)
            {
                ["A"] = 1,
                ["B"] = 2,
                ["C"] = 3,
            };

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

            structType.CreateField("EnumField", (UTinyType.Reference)enumType);
            structType.DefaultValue = new UTinyObject(registry, (UTinyType.Reference)structType)
            {
                ["EnumField"] = new UTinyEnum.Reference(enumType, "B")
            };

            var instance = new UTinyObject(registry, (UTinyType.Reference)structType);

            Assert.AreEqual(2, ((UTinyEnum.Reference)instance["EnumField"]).Value);
            Assert.AreEqual("B", ((UTinyEnum.Reference)instance["EnumField"]).Name);
        }
        public void NestedSourceScope()
        {
            var       registry = new UTinyRegistry();
            var       builtInCount = registry.Count;
            var       sourceId = "outer";
            var       nestedSourceId = "inner";
            UTinyType testType, testType2;

            using (registry.SourceIdentifierScope(sourceId))
            {
                testType = registry.CreateType(UTinyId.New(), "TestType", UTinyTypeCode.Component);
                using (registry.SourceIdentifierScope(nestedSourceId))
                {
                    testType2 = registry.CreateType(UTinyId.New(), "TestType2", UTinyTypeCode.Component);
                }
            }

            var testTypeRef  = (UTinyType.Reference)testType;
            var testType2Ref = (UTinyType.Reference)testType2;

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

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

            Assert.IsNull(testTypeRef.Dereference(registry));
            Assert.IsNotNull(testType2Ref.Dereference(registry));

            registry.UnregisterAllBySource(nestedSourceId);
            Assert.AreEqual(builtInCount, registry.Count);
            Assert.IsNull(testTypeRef.Dereference(registry));
            Assert.IsNull(testType2Ref.Dereference(registry));
        }
示例#4
0
        public void TypeInitialDefaultValue()
        {
            var registry = new UTinyRegistry();

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

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

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

            var defaultValue = type.DefaultValue as UTinyObject;

            // Assert that we have some default value object that has been created for us
            Assert.IsNotNull(defaultValue);

            // Test the existance and value of the fields
            Assert.AreEqual(0, defaultValue["TestIntField"]);
            Assert.AreEqual(0f, defaultValue["TestFloatField"]);
        }
        public void ListFieldPrimitiveAssignment()
        {
            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
                }
            };

            instance["TestField"] = new UTinyList(registry, (UTinyType.Reference)UTinyType.Int32)
            {
                3,
                6,
                7
            };

            Debug.Log(instance);
        }
        public void ObjectListVersionChange()
        {
            var registry = new UTinyRegistry();

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

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

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

            var version = list.Version;

            (list[0] as UTinyObject)["TestField"] = 7;

            Assert.AreNotEqual(version, list.Version);

            Debug.Log(list);
        }
示例#7
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"]);
        }
示例#8
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);
                }
        }
示例#9
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));
                }
        }
        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 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);
        }
示例#13
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);
        }
示例#14
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);
        }
示例#15
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"]);
        }
示例#16
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));
            }
        }
示例#17
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());
        }
示例#18
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"]);
            }
        }
示例#19
0
        public void DetectTypeChanges()
        {
            // Create two new types
            var testStructType = registry.CreateType(UTinyId.New(), "TestStructType", UTinyTypeCode.Struct);

            registry.CreateType(UTinyId.New(), "TestComponentType", UTinyTypeCode.Struct);

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

            {
                // 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
                testStructType.CreateField("TestIntField", (UTinyType.Reference)UTinyType.Int32);
                testStructType.CreateField("TestStringField", (UTinyType.Reference)UTinyType.String);
                testStructType.Name = "OtherTestStructType";

                var count = 0;

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

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

                // We should be notified that one object was changed
                Assert.AreEqual(1, count);
            }
        }
        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");
                    }
                }
        }