Example #1
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));
            }
        }
Example #2
0
        public void CommandStreamEntityPerformance()
        {
            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 command = new MemoryStream())
            {
                // Write the data model to a stream as json
                // mem -> command

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

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

                    BackEnd.Persist(command, (IEnumerable <UTinyEntity>)entities);

                    watch.Stop();
                    Debug.Log($"CommandStream.BackEnd.Persist 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();

                    FrontEnd.Accept(command, output);

                    watch.Stop();
                    Debug.Log($"CommandStream.FrontEnd.Accept Entities=[{kCount}] {watch.ElapsedMilliseconds}ms");
                }
            }
        }
Example #3
0
        /// <summary>
        /// @TODO This method has too many conditionals and checks... it should be managed at a higher level
        /// </summary>
        /// <param name="registry"></param>
        /// <param name="persistenceId"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static bool Accept(IRegistry registry, out string persistenceId)
        {
            Assert.IsTrue(Exists());

            registry.Clear();
            UTinyPersistence.LoadAllModules(registry);
            registry.UnregisterAllBySource(UTinyRegistry.DefaultSourceIdentifier);

            persistenceId = null;

            using (var command = new MemoryStream())
                using (var stream = File.OpenRead(GetTempLocation().FullName))
                    using (var reader = new BinaryReader(stream))
                        using (registry.SourceIdentifierScope(UTinyRegistry.DefaultSourceIdentifier))
                        {
                            var version = reader.ReadInt32();

                            Assert.IsTrue(version > 0);

                            var type = (SaveType)reader.ReadByte();

                            switch (type)
                            {
                            case SaveType.PersistentUnchanged:
                                persistenceId = reader.ReadString();
                                return(false);

                            case SaveType.PersistentChanged:
                                persistenceId = reader.ReadString();

                                var hash = reader.ReadString();
                                if (!string.IsNullOrEmpty(hash) && !string.Equals(hash, ComputeHash(persistenceId)))
                                {
                                    // Ask the user if they want to keep their changes or reload from disc
                                    if (EditorUtility.DisplayDialog($"{UTinyConstants.ApplicationName} assets changed", $"{UTinyConstants.ApplicationName} assets have changed on disk, would you like to reload the current project?", "Yes", "No"))
                                    {
                                        return(false);
                                    }
                                }
                                break;

                            case SaveType.Temporary:
                                break;

                            default:
                                throw new ArgumentOutOfRangeException();
                            }

                            // This is to handle module editing.
                            // We want to unregister it from its current source and re-register it with the persistenceId as the scope
                            if (!string.IsNullOrEmpty(persistenceId))
                            {
                                registry.UnregisterAllBySource(persistenceId);
                            }

                            FrontEnd.Accept(stream, command);

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

            foreach (var project in registry.FindAllBySource(UTinyRegistry.DefaultSourceIdentifier).OfType <UTinyProject>())
            {
                project.PersistenceId = persistenceId;
            }

            return(true);
        }
 public void Restore(UTinyRegistryObjectBase obj)
 {
     Data.Position = 0;
     FrontEnd.Accept(Data, obj.Registry);
 }