/// <summary> /// 创建新实例。 /// Initialize a new instance. /// </summary> /// <param name="translationStream"> /// 翻译流。 /// The stream of the translation. /// </param> /// <param name="leaveOpen"> /// 是否在读取后保存流开启。 /// Whether to keep the stream open after reading. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="translationStream"/> 为 <c>null</c> 。 /// <paramref name="translationStream"/> is <c>null</c>. /// </exception> /// <exception cref="CannotReadTranslationException"> /// 读取翻译失败。 /// Cannot read the translation. /// </exception> public Zhouyi(Stream translationStream, bool leaveOpen = false) { if (translationStream is null) { throw new ArgumentNullException(nameof(translationStream)); } TranslationFile?translation; try { using (var stream = new StreamReader(translationStream, null, true, -1, leaveOpen)) translation = JsonSerializer.Deserialize <TranslationFile>( stream.ReadToEnd(), this.jsonSerializerOptions); } catch (JsonException e) { throw new CannotReadTranslationException($"Invalid translation.", e); } if (translation is null || !translation.Check()) { throw new CannotReadTranslationException($"Invalid translation."); } this.patternsAndNumbers = new PatternsAndNumbers( translation.Patterns, translation.Numbers); this.trigramsAndHexagrams = new TrigramsAndHexagrams( translation.Hexagrams, translation.Trigrams); }
internal ZhouyiHexagram(PatternsAndNumbers patterns, int index, string name, string text, string?applyNinesOrApplySixes, ZhouyiTrigram lowerTrigram, ZhouyiTrigram upperTrigram, string[] lines) { this.Patterns = patterns; this.Text = text; this.Index = index; this.Name = name; var trigram = lowerTrigram.GetPainting(); this.FirstLine = new Line(this, 1, trigram[0], lines[0]); this.SecondLine = new Line(this, 2, trigram[1], lines[1]); this.ThirdLine = new Line(this, 3, trigram[2], lines[2]); trigram = upperTrigram.GetPainting(); this.FourthLine = new Line(this, 4, trigram[0], lines[3]); this.FifthLine = new Line(this, 5, trigram[1], lines[4]); this.SixthLine = new Line(this, 6, trigram[2], lines[5]); this.LowerTrigram = lowerTrigram; this.UpperTrigram = upperTrigram; if (applyNinesOrApplySixes is not null) { this.ApplyNinesOrApplySixes = new Line(this, 0, trigram[0], applyNinesOrApplySixes); } }
/// <summary> /// 创建一个不具有翻译的新实例。 /// 所有翻译将使用空字符串代替。 /// Initialize a new instance without translations. /// Empty strings will be used instead of the translations. /// </summary> public Zhouyi() { TranslationFile translation = TranslationFile.Empty; Debug.Assert(translation.Check()); this.patternsAndNumbers = new PatternsAndNumbers( translation.Patterns, translation.Numbers); this.trigramsAndHexagrams = new TrigramsAndHexagrams( translation.Hexagrams, translation.Trigrams); }
/// <summary> /// 创建新实例。 /// Initialize a new instance. /// </summary> /// <param name="translationFilePath"> /// 翻译文件路径。 /// Path of the translation file. /// </param> /// <exception cref="CannotReadTranslationException"> /// 读取翻译失败。 /// Cannot read the translation. /// </exception> /// <exception cref="ArgumentNullException"> /// <paramref name="translationFilePath"/> 为 <c>null</c> 。 /// <paramref name="translationFilePath"/> is <c>null</c>. /// </exception> public Zhouyi(string translationFilePath) { if (translationFilePath is null) { throw new ArgumentNullException(nameof(translationFilePath)); } TranslationFile?translation; try { using (var stream = new StreamReader(translationFilePath)) translation = JsonSerializer.Deserialize <TranslationFile>( stream.ReadToEnd(), this.jsonSerializerOptions); } catch (IOException e) { throw new CannotReadTranslationException($"Cannot read translation file: {translationFilePath}", e); } catch (JsonException e) { throw new CannotReadTranslationException($"Invalid translation file: {translationFilePath}", e); } catch (ArgumentException e) { throw new CannotReadTranslationException($"Cannot read translation file: {translationFilePath}", e); } if (translation is null || !translation.Check()) { throw new CannotReadTranslationException($"Invalid translation file: {translationFilePath}"); } this.patternsAndNumbers = new PatternsAndNumbers( translation.Patterns, translation.Numbers); this.trigramsAndHexagrams = new TrigramsAndHexagrams( translation.Hexagrams, translation.Trigrams); }