/// <summary> /// Parses a SGF collection /// </summary> /// <param name="input">Input</param> /// <returns>SGF collection</returns> private SgfCollection ParseCollection(string input) { if (string.IsNullOrWhiteSpace(input)) { throw new SgfParseException("Input SGF empty"); } int inputPosition = 0; SkipInputWhitespace(input, ref inputPosition); List <SgfGameTree> gameTrees = new List <SgfGameTree>(); while (inputPosition < input.Length) { if (input[inputPosition] != '(') { throw new SgfParseException($"SGF Root does not begin with ( at {inputPosition}"); } SgfGameTree tree = ParseGameTree(input, ref inputPosition); gameTrees.Add(tree); SkipInputWhitespace(input, ref inputPosition); } SgfCollection collection = new SgfCollection(gameTrees); return(collection); }
/// <summary> /// Creates a library item /// </summary> /// <param name="fileInfo">File info</param> /// <param name="sgfCollection">Sgf collection</param> /// <returns></returns> private LibraryItem CreateLibraryItem(FileInfo fileInfo, SgfCollection sgfCollection) { List <LibraryItemGame> games = new List <LibraryItemGame>(sgfCollection.Count()); foreach (var tree in sgfCollection.GameTrees) { SgfGameInfoSearcher searcher = new SgfGameInfoSearcher(tree); var sgfGameInfo = searcher.GetGameInfo(); var comment = sgfGameInfo.GameComment?.Value <string>() ?? ""; if (comment == "") { //try to find a comment in first node var firstNode = tree.Sequence.FirstOrDefault(); if (firstNode != null) { comment = firstNode["C"]?.Value <string>() ?? ""; } } var gameName = sgfGameInfo.GameName?.Value <string>() ?? ""; var blackName = sgfGameInfo.PlayerBlack?.Value <string>() ?? ""; var blackRank = sgfGameInfo.BlackRank?.Value <string>() ?? ""; var whiteName = sgfGameInfo.PlayerWhite?.Value <string>() ?? ""; var whiteRank = sgfGameInfo.WhiteRank?.Value <string>() ?? ""; var date = sgfGameInfo.Date?.Value <string>() ?? ""; var moves = CountPrimaryLineMoves(tree); var libraryItemGame = new LibraryItemGame(gameName, moves, date, blackName, blackRank, whiteName, whiteRank, comment); games.Add(libraryItemGame); } return(new LibraryItem(fileInfo.Name, games.ToArray(), fileInfo.Size, fileInfo.LastModified)); }
/// <summary> /// Creates a library item from file content info. Displays error info. /// </summary> /// <param name="fileInfo">File content info</param> /// <returns>Library item</returns> public async Task <LibraryItem> CreateLibraryItemFromFileContentAsync(FileContentInfo fileInfo) { if (fileInfo == null) { return(null); } var parser = new SgfParser(); SgfCollection collection = null; try { collection = parser.Parse(fileInfo.Contents); } catch (SgfParseException e) { //ignore await _dialogService.ShowAsync(e.Message, Localizer.ErrorParsingSgfFile); } if (collection != null) { var newItem = CreateLibraryItem(fileInfo, collection); return(newItem); } return(null); }
/// <summary> /// Serializes a given SGF collection /// </summary> /// <param name="sgfCollection">SGF collection to be serialized</param> /// <returns>Serialized SGF string</returns> public string Serialize([NotNull] SgfCollection sgfCollection) { if (sgfCollection == null) { throw new ArgumentNullException(nameof(sgfCollection)); } return(SerializeCollection(sgfCollection).Trim()); }
static void Main(string[] args) { SgfParser sgfParser = new SgfParser(); SgfCollection collection = sgfParser.Parse(File.ReadAllText("S:\\DELETEME\\markup.sgf")); SgfToGameTreeConverter gameTreeConverter = new SgfToGameTreeConverter(collection.First()); GameTreeNode rootNode = gameTreeConverter.Convert().GameTree.GameTreeRoot; //SgfParser.Deserialize(File.ReadAllText("C:\\Users\\Martin\\Downloads\\ff4_ex.sgf")); WriteMarkup(rootNode); }
/// <summary> /// Serializes a SGF collection /// </summary> /// <param name="collection">SGF collection</param> /// <returns>Serialized SGF string</returns> private string SerializeCollection([NotNull] SgfCollection collection) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } StringBuilder builder = new StringBuilder(); //append serialized game trees foreach (var gameTree in collection) { builder.Append(SerializeGameTree(gameTree)); } return(builder.ToString()); }
public void ConstructorThrowsWhenNullIsSupplied() { SgfCollection collection = new SgfCollection(null); }