Exemplo n.º 1
0
        private Tuple <string, string[]> ParseLoc(string loc)
        {
            List <string> locs   = new List <string>();
            string        itemId = null;

            foreach (string token in loc.Split(new[] { ' ' },
                                               StringSplitOptions.RemoveEmptyEntries))
            {
                JsonTextIndexPayload a = _textIndex.Find(token);
                if (a == null)
                {
                    Logger?.LogError("Word ID {WordId} not found", token);
                    return(null);
                }
                if (itemId == null)
                {
                    itemId = a.ItemId;
                }
                else if (a.ItemId != itemId)
                {
                    Logger?.LogError("Fragment spans two items: {Loc}", loc);
                    return(null);
                }
                locs.Add($"{a.Y}.{a.X}");
            }
            return(Tuple.Create(itemId, locs.ToArray()));
        }
Exemplo n.º 2
0
        private Tuple <string, string> ParseFromTo(XElement appElem)
        {
            string from = appElem.Attribute("from").Value.Substring(1);
            string to   = appElem.Attribute("to").Value.Substring(1);

            JsonTextIndexPayload a = _textIndex.Find(from);

            if (a == null)
            {
                Logger?.LogError("Word ID {WordId} not found", from);
                return(null);
            }
            // range
            if (from != to)
            {
                JsonTextIndexPayload b = _textIndex.Find(to);
                if (b == null)
                {
                    Logger?.LogError("Word ID {WordId} not found", to);
                    return(null);
                }
                if (b.ItemId != a.ItemId)
                {
                    Logger?.LogError("Fragment spans two items: {FromLoc} {ToLoc}",
                                     a, b);
                    return(null);
                }
                return(Tuple.Create(a.ItemId, $"{a.Y}.{a.X}-{b.Y}.{b.X}"));
            }
            // point
            return(Tuple.Create(a.ItemId, $"{a.Y}.{a.X}"));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Index data from the specified stream, adding them to this index.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="text">True to include also each tile's text in the
        /// index payload; false to exclude it (default).</param>
        /// <exception cref="ArgumentNullException">stream</exception>
        public void Index(Stream stream, bool text = false)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            using (JsonDocument doc = JsonDocument.Parse(stream, new JsonDocumentOptions
            {
                AllowTrailingCommas = true
            }))
            {
                // item
                foreach (JsonElement itemElem in doc.RootElement.EnumerateArray())
                {
                    // item/id
                    string itemId = itemElem.GetProperty("id").GetString();
                    // item/parts
                    JsonElement partElem =
                        itemElem.GetProperty("parts").EnumerateArray().First();
                    // item/parts[0]/rows
                    int y = 0;
                    foreach (JsonElement rowElem in partElem.GetProperty("rows")
                             .EnumerateArray())
                    {
                        y++;
                        int x = 0;
                        foreach (JsonElement tileElem in
                                 rowElem.GetProperty("tiles").EnumerateArray())
                        {
                            x++;
                            string tileId = tileElem
                                            .GetProperty("data")
                                            .GetProperty("id").GetString();
                            JsonTextIndexPayload payload =
                                new JsonTextIndexPayload(itemId, y, x);
                            if (text)
                            {
                                payload.Text = tileElem.GetProperty("data")
                                               .GetProperty("text").GetString();
                            }
                            _index[tileId] = payload;
                        }
                    }
                }
            }
        }