public static void PushMessage(this ILuaState self, IMessage nm, PB_Message pb) { self.PushValue(1); for (int i = 0; i < pb.Count; ++i) { var field = pb[i]; if (field.repeated) { self.GetField(-1, field.name); if (!self.IsTable(-1)) { self.Pop(1); self.PushString(field.name); self.NewTable(); } int count = nm.readU32(); for (int n = 0; n < count; ++n) { self.PushField(nm, field); } self.SetTable(-3); } else { self.PushField(nm, field); } } }
public static PB_Message Parse(string protoDef) { protoDef = protoDef.Trim(); if (!protoDef.StartsWith("message", System.StringComparison.OrdinalIgnoreCase)) { return(Error(protoDef, "Invalid proto head")); } int indexL = protoDef.IndexOf('{'); int indexR = protoDef.IndexOf('}'); // Message ID string strId = protoDef.Substring(LEN_MSG, indexL - LEN_MSG - 1).Trim(); int id = 0; if (!int.TryParse(strId, out id)) { return(Error(strId, "Invalid Message ID")); } PB_Message message = new PB_Message(); // Message Fields string strFields = protoDef.Substring(indexL + 1, indexR - indexL - 2).Trim(); string[] fields = strFields.Split(SPLIT_FIELD, System.StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < fields.Length; ++i) { string[] segs = fields[i].Split(SPLIT_SEG, System.StringSplitOptions.RemoveEmptyEntries); bool repeated = segs[0].EndsWith("[]"); string strType = repeated ? segs[0].Substring(0, segs[0].Length - 2) : segs[0]; string name = segs[1]; string strIndex = segs[2]; PB_Type type; try { type = (PB_Type)PB_Type.Parse(typeof(PB_Type), strType, true); } catch (System.Exception e) { return(Error(fields[i], e.Message)); } int index = 0; if (!int.TryParse(strIndex, out index)) { return(Error(fields[i], "Invalid index.")); } message.Add(new PB_Field(type, name, index, repeated)); } message.Sort(); return(message); }