/// <summary> /// Exit a parse tree produced by <see cref="LuaParser.field" />. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public override void ExitField(LuaParser.FieldContext context) { switch (_currentScope) { case MMSavedVariableScope.EsoItem: if (CurrentItem != null) { LuaTableField itemField = GetField(context); if (itemField != null) { CurrentItem.Set(itemField); } } break; case MMSavedVariableScope.EsoGuildStoreSale: if (CurrentSale != null) { LuaTableField saleField = GetField(context); if (saleField != null) { CurrentSale.Set(saleField); } } break; } }
/// <summary> /// Loads the value from a given Lua table field into it's corresponding property, if one exists. /// </summary> /// <param name="field">The Lua table field containing the property and value to set.</param> public void Set(LuaTableField field) { if (field == null) { return; } switch (field.Name) { case "buyer": Buyer = field.Value; break; case "guild": GuildName = field.Value; break; case "seller": Seller = field.Value; break; case "itemLink": ItemLink = field.Value; break; case "price": int price; if (int.TryParse(field.Value, out price)) { Price = price; } break; case "wasKiosk": bool wasKiosk; if (bool.TryParse(field.Value, out wasKiosk)) { WasKiosk = wasKiosk; } break; case "quant": int quantity; if (int.TryParse(field.Value, out quantity)) { Quantity = quantity; } break; case "timestamp": int timestamp; if (int.TryParse(field.Value, out timestamp)) { SaleTimestamp = timestamp; } break; } }
/// <summary> /// Loads the value from a given Lua table field into it's corresponding property, if one exists. /// </summary> /// <param name="field">The Lua table field containing the property and value to set.</param> internal void Set(LuaTableField field) { if (field == null) { return; } switch (field.Name) { case "itemIcon": ItemIcon = field.Value; break; } }
/// <summary> /// Gets a name/value pair representing a given Lua table field and whether the value is a nested table or not. /// </summary> /// <param name="context">The parse tree node for the field.</param> /// <param name="valueChild"> /// (optional) The parse tree node for the value, if known; if not known, it is assumed to be the /// fifth child of the field node. /// </param> /// <returns></returns> protected LuaTableField GetField(LuaParser.FieldContext context, LuaParser.ExpContext valueChild = null) { // Fields are expected to have 5 children: <left bracket> <name> <right bracket> <equal> <value> // When called from the table constructor enter method, the <value> child is not yet populated, so we pass it explicitly. int expectedChildCount = valueChild == null ? 5 : 4; // Validate field count if (context.ChildCount < expectedChildCount) { return(null); } // Value child not specified, so get it from the fifth child. if (valueChild == null) { valueChild = context.GetChild(4) as LuaParser.ExpContext; } // Value isn't an expression? Malformed Lua table. if (valueChild == null) { return(null); } // Second child is the <name> node. var keyChild = context.GetChild(1) as LuaParser.ExpContext; // Name wasn't an expression? Malformed Lua table. if (keyChild == null) { return(null); } // Create key/value instance and return var field = new LuaTableField { Name = GetString(keyChild), IsTable = valueChild.Start.Type == LuaParser.T__48 // left curly brace token indicates a nested table }; if (!field.IsTable) { field.Value = GetString(valueChild); } return(field); }