예제 #1
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));
                }
        }
예제 #2
0
        public void AssetSerializationTest()
        {
            // 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");

            // Reference the asset in the module
            m_Module.AddAsset(m_Texture2D);

            Debug.Log(m_Module);

            var registry = new UTinyRegistry();

            using (var json = new MemoryStream())
                using (var command = new MemoryStream())
                {
                    // Write the module to the stream
                    Serialization.FlatJson.BackEnd.Persist(json, m_Module);
                    json.Position = 0;

                    Serialization.FlatJson.FrontEnd.Accept(json, command);
                    command.Position = 0;

                    Serialization.CommandStream.FrontEnd.Accept(command, registry);
                }

            var module = registry.FindById <UTinyModule>(m_Module.Id);

            Debug.Log(module);

            var path = AssetDatabase.GetAssetPath(m_Texture2D);

            AssetDatabase.DeleteAsset(path);
        }
예제 #3
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));
            }
        }
        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);
                }
            }
        }
예제 #5
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);
            }
        }