/// <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); } } }
/// <summary> /// 枚举所有可用的语言文件;文件命名需要符合要求; /// </summary> public IEnumerable <LanguagePackInfo> EnumeratePack(Content contentConstruct, SearchOption searchOption) { foreach (string entry in contentConstruct.EnumerateFiles(LocalizationDirectoryName, LanguagePackFileSearchPattern, searchOption)) { LanguagePackInfo languagePack; try { using (var stream = contentConstruct.GetInputStream(entry)) { var description = packSerializer.Deserialize(stream); languagePack = new LanguagePackInfo(description, contentConstruct, entry); } } catch (Exception ex) { UnityEngine.Debug.Log(ex); languagePack = null; } if (languagePack != null) { yield return(languagePack); } } }
/// <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()); }
/// <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); }