示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="data"></param>
        /// <param name="conversationNodeFactory"></param>
        /// <param name="domainNodeFactory"></param>
        /// <param name="initialContent">Represents the current contents of the file. Reference is not held. A copy is made.</param>
        /// <param name="projectFile"></param>
        /// <param name="serializer"></param>
        /// <param name="conversationSerializer"></param>
        /// <param name="conversationSerializerDeserializerFactory"></param>
        /// <param name="domainSerializer"></param>
        /// <param name="pluginsConfig"></param>
        /// <param name="audioCustomization"></param>
        public Project(ILocalizationContext context, TData data, INodeFactory conversationNodeFactory, INodeFactory domainNodeFactory, MemoryStream initialData, FileInfo projectFile, ISerializer <TData> serializer, ISerializer <TConversationData> conversationSerializer, ConversationSerializerDeserializerFactory conversationSerializerDeserializerFactory, ISerializer <TDomainData> domainSerializer, PluginsConfig pluginsConfig, Func <IAudioProviderCustomization> audioCustomization, UpToDateFile.BackEnd backend)
        {
            AssertUniqueFileIds(data);
            AssertUniqueLocalizationSetNames(data);
            AssertUniqueFilePaths(data);

            m_upToDateFileBackend = backend;
            Action <Stream> saveTo = stream =>
            {
                Write(GetFilePath, Conversations.Select(x => (x.Id)), LocalizationFiles.Select(x => (x.Id)),
                      DomainFiles.Select(x => (x.Id)), AudioFiles.Select(x => (x.Id)), Localizer.LocalizationSets, stream, m_serializer);
            };

            m_file = new SaveableFileNotUndoable(initialData, projectFile, saveTo, backend);
            ConversationNodeFactory         = conversationNodeFactory;
            DomainNodeFactory               = domainNodeFactory;
            m_serializer                    = serializer;
            ConversationSerializer          = conversationSerializer;
            m_conversationSerializerFactory = conversationSerializerDeserializerFactory;
            m_domainSerializer              = domainSerializer;

            m_filePaths = data.Conversations.Concat(data.Localizations).Concat(data.Domains).Concat(data.Audios).ToDictionary(f => f.Id, f => f.Path);

            IEnumerable <Id <FileInProject> > conversationIds = data.Conversations.Select(f => f.Id);
            IEnumerable <Id <FileInProject> > localizerIds    = data.Localizations.Select(f => f.Id);
            IEnumerable <Id <FileInProject> > domainIds       = data.Domains.Select(f => f.Id);
            IEnumerable <Id <FileInProject> > audioIds        = data.Audios.Select(f => f.Id);

            m_audioProvider = new AudioProvider(new FileInfo(projectFile.FullName), GetFilePath, s => CheckFolder(s, Origin), this, audioCustomization());
            using (m_audioProvider.SuppressUpdates())
            {
                {
                    m_audioProvider.AudioFiles.Load(audioIds);
                }

                {
                    //Dictionary<IDynamicEnumParameter, DynamicEnumParameter.Source> domainEnumSource = new Dictionary<IDynamicEnumParameter, DynamicEnumParameter.Source>();
                    Func <IDynamicEnumParameter, object, DynamicEnumParameter.Source> getDomainEnumSource = (k, o) =>
                    {
                        return(null); //Nothing should need a source (but the system will ask anyway)
                                      //if (!domainEnumSource.ContainsKey(k))
                                      //domainEnumSource[k] = new DynamicEnumParameter.Source();
                                      //return domainEnumSource[k];
                    };

                    m_domainDataSource = new DomainDomain(pluginsConfig);
                    Func <IEnumerable <Tuple <Id <FileInProject>, DocumentPath> >, IEnumerable <IDomainFile> > loader = paths =>
                    {
                        var result = DomainFile.Load(paths, m_domainDataSource, DomainSerializerDeserializer.Make, DomainNodeFactory, () => DomainUsage, getDomainEnumSource, backend).Evaluate();
                        result.ForAll(a => a.ConversationDomainModified += ConversationDatasourceModified);
                        return(result);
                    };
                    Func <DirectoryInfo, DomainFile> makeEmpty = path => DomainFile.CreateEmpty(path, m_domainDataSource, m_domainSerializer, pathOk, DomainNodeFactory, () => DomainUsage, getDomainEnumSource, backend, Origin);
                    m_domainFiles = new ProjectElementList <IDomainFile>(GetFilePath, s => CheckFolder(s, Origin), loader, makeEmpty);
                    m_domainFiles.Load(domainIds);
                }
                m_conversationDataSource = new ConversationDataSource(m_domainFiles.Select(df => df.Data));

                if (m_conversationDataSource.DomainErrors.Any())
                {
                    MessageBox.Show(string.Join("\n", m_conversationDataSource.DomainErrors.Select(error => error.Message)));
                }

                {
                    m_localizer = new LocalizationEngine(GetFilePath, data.LocalizationSets, context, UsedLocalizations, ShouldContract, pathOk, s => CheckFolder(s, Origin), backend, Origin);
                    m_localizer.Localizers.Load(localizerIds);
                    context.CurrentLocalization.Value    = m_localizer.LocalizationSets.FirstOrDefault() ?? Project.TData.LocalizerSetData.Empty;
                    m_localizer.LocalizationSetsChanged += () => { m_file.Change(); };
                }

                {
                    GenerateAudio audio = (c) =>
                    {
                        return(m_audioProvider.Generate(new AudioGenerationParameters(c.File.File, this.File.File)));
                    };

                    //This can be called from multiple threads simultaneously and in arbitrary orders by design of
                    //ConversationDataSource and the underlying ConstantTypeSet and DynamicEnumParameter.Source
                    Func <IDynamicEnumParameter, object, DynamicEnumParameter.Source> getSource = (localEnum, newSourceID) =>
                    {
                        return(m_conversationDataSource.GetSource(localEnum.TypeId, newSourceID));
                    };

                    Func <IEnumerable <Tuple <Id <FileInProject>, DocumentPath> >, IEnumerable <IConversationFile> > loadConversations = files =>
                    {
                        ISerializerDeserializer <XmlGraphData <NodeUIData, ConversationEditorData> > conversationSerializerDeserializer = m_conversationSerializerFactory(m_conversationDataSource);
                        //                                                 _/          _/                           _/                         X        _/             X            _/
                        //return files.Select(file => ConversationFile.Load(file, ConversationNodeFactory, conversationSerializerDeserializer, audio, getSource, m_audioProvider, backend));
                        //TODO: AUDIO: This is ok as long as we're not using audio parameters at all
                        return(ParallelEnumerable.Select(files.AsParallel(), file => ConversationFile.Load(file.Item1, file.Item2, ConversationNodeFactory, conversationSerializerDeserializer, audio, getSource, m_audioProvider, backend)));
                    };
                    Func <DirectoryInfo, ConversationFile> makeEmpty = path => ConversationFile.CreateEmpty(path, this, ConversationNodeFactory, audio, getSource, m_audioProvider, backend, Origin);
                    m_conversations = new ProjectElementList <IConversationFile>(GetFilePath, s => CheckFolder(s, Origin), loadConversations, makeEmpty);
                    m_conversations.Load(conversationIds);
                }

                RefreshCallbacks(m_conversations, m_actionsOnRefreshCallbacksConversations);
                RefreshCallbacks(m_audioProvider.AudioFiles, m_actionsOnRefreshCallbacksAudios);
                RefreshCallbacks(m_localizer.Localizers, m_actionsOnRefreshCallbacksLocalizations);
                RefreshCallbacks(m_domainFiles, m_actionsOnRefreshCallbacksDomains);

                m_conversations.GotChanged            += GotChanged;
                m_audioProvider.AudioFiles.GotChanged += GotChanged;
                m_localizer.Localizers.GotChanged     += GotChanged;
                m_domainFiles.GotChanged += GotChanged;

                m_conversations.GotChanged            += () => RefreshCallbacks(m_conversations, m_actionsOnRefreshCallbacksConversations);
                m_audioProvider.AudioFiles.GotChanged += () => RefreshCallbacks(m_audioProvider.AudioFiles, m_actionsOnRefreshCallbacksAudios);
                m_localizer.Localizers.GotChanged     += () => RefreshCallbacks(m_localizer.Localizers, m_actionsOnRefreshCallbacksLocalizations);
                m_domainFiles.GotChanged += () => RefreshCallbacks(m_domainFiles, m_actionsOnRefreshCallbacksDomains);


                m_domainFiles.GotChanged += ConversationDatasourceModified;
                Action <IDomainFile> MaybeConversationDatasourceModified = file => { if (!file.File.Changed())
                                                                                     {
                                                                                         ConversationDatasourceModified();
                                                                                     }
                };
                m_domainFiles.Added    += argument => { argument.File.SaveStateChanged += () => MaybeConversationDatasourceModified(argument); };
                m_domainFiles.Reloaded += (_, argument) => { argument.File.SaveStateChanged += () => MaybeConversationDatasourceModified(argument); };
                m_domainFiles.ForAll(d => d.File.SaveStateChanged += () => MaybeConversationDatasourceModified(d));
                //m_domainFiles.Added += argument => { argument.File.SaveStateChanged += () => { if (!argument.File.Changed) ReloadDatasource(); }; };
                //m_domainFiles.Reloaded += (from, to) => { to.File.SaveStateChanged += () => { if (!to.File.Changed) ReloadDatasource(); }; };
                //m_domainFiles.ForAll(d => d.File.SaveStateChanged += () => { if (!d.File.Changed) ReloadDatasource(); });

                m_domainFiles.Added                += file => m_filePaths[file.Id] = DocumentPath.FromAbsolutePath(file.File.File.FullName, Origin);
                m_conversations.Added              += file => m_filePaths[file.Id] = DocumentPath.FromAbsolutePath(file.File.File.FullName, Origin);
                m_audioProvider.AudioFiles.Added   += file => m_filePaths[file.Id] = DocumentPath.FromAbsolutePath(file.File.File.FullName, Origin);
                m_localizer.Localizers.Added       += file => m_filePaths[file.Id] = DocumentPath.FromAbsolutePath(file.File.File.FullName, Origin);
                m_domainFiles.Removed              += file => m_filePaths.Remove(file.Id);
                m_conversations.Removed            += file => m_filePaths.Remove(file.Id);
                m_audioProvider.AudioFiles.Removed += file => m_filePaths.Remove(file.Id);
                m_localizer.Localizers.Removed     += file => m_filePaths.Remove(file.Id);
                //Files being moved is setup in RefreshCallbacks()

                DomainUsage = new DomainUsage(this);

                m_audioProvider.UpdateUsage();
            }
        }