コード例 #1
0
        public void TestReadInMemory()
        {
            var          serializer      = new LanguagePackSerializer();
            LanguagePack dictionary      = new LanguagePack(description, languageDictionary);
            LanguagePack splitDictionary = new LanguagePack(description, languageSplitDictionary);

            using (Stream stream = new MemoryStream())
            {
                serializer.Serialize(stream, dictionary);

                stream.Seek(0, SeekOrigin.Begin);
                var pack = serializer.DeserializeSplit(stream);

                CheckIsSame(dictionary, pack);
            }

            using (Stream stream = new MemoryStream())
            {
                serializer.Serialize(stream, splitDictionary);

                stream.Seek(0, SeekOrigin.Begin);
                var pack = serializer.DeserializePack(stream);

                CheckIsSame(splitDictionary, pack);
            }
        }
コード例 #2
0
        /// <summary>
        /// 枚举所有可用的语言文件;文件命名需要符合要求;
        /// </summary>
        public static IEnumerable <LanguagePackInfo> Enumerate(this LanguagePackSerializer packSerializer, Content content, string rootDirectory, SearchOption searchOption)
        {
            if (packSerializer == null)
            {
                throw new ArgumentNullException(nameof(packSerializer));
            }
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            foreach (string entry in content.EnumerateFiles(rootDirectory, LanguagePackFileSearchPattern, searchOption))
            {
                LanguagePackInfo languagePack;
                try
                {
                    using (var stream = content.GetInputStream(entry))
                    {
                        var description = packSerializer.Deserialize(stream);
                        languagePack = new LanguagePackInfo(description, content, entry);
                    }
                }
                catch (Exception ex)
                {
                    UnityEngine.Debug.Log(ex);
                    languagePack = null;
                }

                if (languagePack != null)
                {
                    yield return(languagePack);
                }
            }
        }
コード例 #3
0
        private void ReadWriteTest(Stream stream, LanguagePack pack1)
        {
            var serializer = new LanguagePackSerializer();

            serializer.Serialize(stream, pack1);

            stream.Seek(0, SeekOrigin.Begin);
            ReadTest(stream, pack1);
        }
コード例 #4
0
        private void ReadTest(Stream stream, LanguagePack pack1)
        {
            var serializer = new LanguagePackSerializer();

            var pack2 = serializer.DeserializePack(stream);

            CheckIsSame(pack1.Description, pack2.Description);
            CheckIsSame(pack1.LanguageDictionary, pack2.LanguageDictionary);
        }
コード例 #5
0
        /// <summary>
        /// 搜索模组语言资源,加载合适的语言资源,加载合适的模组语言资源;
        /// </summary>
        void IModificationInitializeHandle.Initialize(IReadOnlyList <Modification> mods, CancellationToken token)
        {
            LocalizationConfigSerializer configSerializer = new LocalizationConfigSerializer();
            LanguagePackSerializer       packSerializer   = new LanguagePackSerializer();

            //读取语言配置
            LocalizationConfig?config = TrySerializeConfig(configSerializer);

            //搜索主语言包
            LanguagePacks = SearchLanguagePack(packSerializer, mods);

            //读取合适的主语言包
            LanguagePack languagePack = null;

            foreach (LanguagePackInfo info in EnumerateLanguagePask(config, LanguagePacks))
            {
                if (TryDeserializePack(packSerializer, info, out languagePack))
                {
                    break;
                }
            }

            if (languagePack == null)
            {
                throw new FileNotFoundException("未找到合适的语言包!");
            }


            //搜索读取语言补充包
            foreach (var mod in mods)
            {
                LanguagePackInfo supplementaryPackInfo = FindSupplementaryPack(packSerializer, mod, languagePack.Description);
                if (supplementaryPackInfo != null)
                {
                    LanguagePack supplementaryPack;
                    if (TryDeserializePack(packSerializer, supplementaryPackInfo, out supplementaryPack))
                    {
                        AddTo(languagePack, supplementaryPack);
                    }
                }
            }


            //若未成功读取语言配置,则输出;
            if (!config.HasValue)
            {
                DeserializeConfig(configSerializer, languagePack.Description);
            }

            Localization.LanguagePack = languagePack;
            Localization.NotifyLanguageChanged();
            UnityDebugHelper.SuccessfulReport(InitializerName, () => GetInfoLog());
        }
コード例 #6
0
        /// <summary>
        /// 搜索所有可用的语言包;
        /// </summary>
        private List <LanguagePackInfo> SearchLanguagePack(LanguagePackSerializer packSerializer, IEnumerable <Modification> mods)
        {
            List <LanguagePackInfo> packInfos = new List <LanguagePackInfo>();

            foreach (var mod in mods)
            {
                var packs = packSerializer.EnumeratePack(mod.BaseContent, SearchOption.TopDirectoryOnly);
                packInfos.AddRange(packs);
            }

            return(packInfos);
        }
コード例 #7
0
        /// <summary>
        /// 获取到该模组合适的语言补充包,若不存在则返回null;
        /// </summary>
        private LanguagePackInfo FindSupplementaryPack(LanguagePackSerializer packSerializer, Modification mod, LanguagePackDescription target)
        {
            var supplementaryPackInfo    = packSerializer.EnumerateSupplementaryPack(mod.BaseContent, SearchOption.TopDirectoryOnly);
            LanguagePackInfo defaultInfo = null;

            foreach (var packInfo in supplementaryPackInfo)
            {
                if (packInfo.Description.Language == target.Language)
                {
                    return(packInfo);
                }
                else if (packInfo.Description.IsDefault)
                {
                    defaultInfo = packInfo;
                }
            }

            return(defaultInfo);
        }
コード例 #8
0
 /// <summary>
 /// 尝试读取到语言包,不返回异常;
 /// </summary>
 private bool TryDeserializePack(LanguagePackSerializer packSerializer, ILanguagePackInfo info, out LanguagePack languagePack)
 {
     using (var stream = info.GetInputStream())
     {
         try
         {
             if (IsReadAsSplitPack)
             {
                 languagePack = packSerializer.DeserializeSplit(stream);
             }
             else
             {
                 languagePack = packSerializer.DeserializePack(stream);
             }
             return(true);
         }
         catch (Exception ex)
         {
             Debug.LogWarning(string.Format("读取语言包[{0}]时失败!{1}", info, ex));
             languagePack = default(LanguagePack);
             return(false);
         }
     }
 }
コード例 #9
0
 /// <summary>
 /// 枚举所有语言补充包;
 /// </summary>
 public static IEnumerable <LanguagePackInfo> EnumerateSupplementaryPack(this LanguagePackSerializer packSerializer, Content content, SearchOption searchOption)
 {
     return(Enumerate(packSerializer, content, SupplementaryPackDirectoryName, searchOption));
 }