private void AddTo(LanguagePack target, LanguagePack source) { foreach (var item in source.LanguageDictionary) { target.LanguageDictionary.Add(item.Key, item.Key); } }
private void TestReadWriteInMemory(LanguagePack pack1) { using (Stream stream = new MemoryStream()) { ReadWriteTest(stream, pack1); } }
public void SplitDictionaryTest() { LanguagePack pack = new LanguagePack(description, languageSplitDictionary); TestReadWriteInMemory(pack); TestReadWriteInFile(pack, "SplitDictionary.zip"); }
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); } }
private void ReadWriteTest(Stream stream, LanguagePack pack1) { var serializer = new LanguagePackSerializer(); serializer.Serialize(stream, pack1); stream.Seek(0, SeekOrigin.Begin); ReadTest(stream, pack1); }
private void TestReadWriteInFile(LanguagePack pack1, string fileName) { string filePath = Path.Combine(rootDirectory, fileName); using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite)) { ReadWriteTest(stream, pack1); } }
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); }
/// <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> /// 获取到规范的文件名; /// </summary> public string GetFileName(LanguagePack pack) { if (string.IsNullOrWhiteSpace(pack.Description.Name)) { return(packFilePrefix + pack.Description.Name + packFileExtension); } else if (string.IsNullOrWhiteSpace(pack.Description.Language)) { return(packFilePrefix + pack.Description.Language + packFileExtension); } else { return(packFilePrefix + DateTime.Now.Ticks + packFileExtension); } }
/// <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); } } }
private void CheckIsSame(LanguagePack pack1, LanguagePack pack2) { CheckIsSame(pack1.Description, pack1.Description); CheckIsSame(pack1.LanguageDictionary, pack1.LanguageDictionary); }
/// <summary> /// 序列化语言包内容; /// </summary> public void Serialize(Stream stream, LanguagePack pack) { Serialize(stream, pack.Description, pack.LanguageDictionary); }