public static void ReloadObject(IRegistry registry, string guid)
        {
            var path = AssetDatabase.GUIDToAssetPath(guid);

            registry.UnregisterAllBySource(guid);
            LoadJson(path, registry, guid);

            foreach (var obj in registry.FindAllBySource(guid).OfType <IPersistentObject>())
            {
                obj.PersistenceId = guid;
            }
        }
        public static void LoadModule(string modulePath, IRegistry registry)
        {
            registry.Clear();
            LoadAllModules(registry);

            var guid = AssetDatabase.AssetPathToGUID(modulePath);

            registry.UnregisterAllBySource(guid);
            LoadJson(modulePath, registry, UTinyRegistry.DefaultSourceIdentifier);

            foreach (var module in registry.FindAllBySource(UTinyRegistry.DefaultSourceIdentifier).OfType <UTinyModule>())
            {
                module.PersistenceId = AssetDatabase.AssetPathToGUID(modulePath);
            }
        }
        public static void LoadProject(string projectPath, IRegistry registry)
        {
            registry.Clear();
            LoadAllModules(registry);

            // the project itself has no identifier
            registry.UnregisterAllBySource(UTinyRegistry.DefaultSourceIdentifier);
            LoadJson(projectPath, registry, UTinyRegistry.DefaultSourceIdentifier);

            foreach (var project in registry.FindAllBySource(UTinyRegistry.DefaultSourceIdentifier).OfType <UTinyProject>())
            {
                project.PersistenceId = AssetDatabase.AssetPathToGUID(projectPath);
                UTinyUpdater.UpdateProject(project);
            }
        }
        public static void LoadAllModules(IRegistry registry)
        {
            var guids = FindAllAssetsGuidsOfType(ModuleFileImporterExtension);

            foreach (var guid in guids)
            {
                var path = AssetDatabase.GUIDToAssetPath(guid);
                LoadJson(path, registry, guid);

                foreach (var module in registry.FindAllBySource(guid).OfType <UTinyModule>())
                {
                    module.PersistenceId = guid;
                }
            }
        }
Exemple #5
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);
        }