public static string WriteReadCode(TlParam p) { switch (p.Type.ToLower()) { case "#": case "int": return($"{CheckForKeywordAndPascalCase(p.Name)} = br.ReadInt32();"); case "long": return($"{CheckForKeywordAndPascalCase(p.Name)} = br.ReadInt64();"); case "string": return($"{CheckForKeywordAndPascalCase(p.Name)} = StringUtil.Deserialize(br);"); case "bool": case "true": return($"{CheckForKeywordAndPascalCase(p.Name)} = BoolUtil.Deserialize(br);"); case "bytes": return($"{CheckForKeywordAndPascalCase(p.Name)} = BytesUtil.Deserialize(br);"); case "double": return($"{CheckForKeywordAndPascalCase(p.Name)} = br.ReadDouble();"); default: if (!IsFlagBase(p.Type)) { if (p.Type.ToLower().Contains("vector")) { return($"{CheckForKeywordAndPascalCase(p.Name)} = ({GetTypeName(p.Type)})ObjectUtils.DeserializeVector<{GetTypeName(p.Type).Replace("TLVector<", "").Replace(">", "")}>(br);"); } else { return($"{CheckForKeywordAndPascalCase(p.Name)} = ({GetTypeName(p.Type)})ObjectUtils.DeserializeObject(br);"); } } else { if (IsTrueFlag(p.Type)) { return($"{CheckForKeywordAndPascalCase(p.Name)} = (Flags & {GetBitMask(p.Type).ToString()}) != 0;"); } else { TlParam p2 = new TlParam() { Name = p.Name, Type = p.Type.Split('?')[1] }; return($"if ((Flags & {GetBitMask(p.Type).ToString()}) != 0)" + Environment.NewLine + WriteReadCode(p2) + Environment.NewLine + "else" + Environment.NewLine + $"{CheckForKeywordAndPascalCase(p.Name)} = null;" + Environment.NewLine); } } } }
public static string WriteWriteCode(TlParam p, bool flag = false) { switch (p.Type.ToLower()) { case "#": case "int": return(flag ? $"bw.Write({CheckForKeywordAndPascalCase(p.Name)}.Value);" : $"bw.Write({CheckForKeywordAndPascalCase(p.Name)});"); case "long": return(flag ? $"bw.Write({CheckForKeywordAndPascalCase(p.Name)}.Value);" : $"bw.Write({CheckForKeywordAndPascalCase(p.Name)});"); case "string": return($"StringUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)},bw);"); case "bool": return(flag ? $"BoolUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)}.Value,bw);" : $"BoolUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)},bw);"); case "true": return($"BoolUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)},bw);"); case "bytes": return($"BytesUtil.Serialize({CheckForKeywordAndPascalCase(p.Name)},bw);"); case "double": return(flag ? $"bw.Write({CheckForKeywordAndPascalCase(p.Name)}.Value);" : $"bw.Write({CheckForKeywordAndPascalCase(p.Name)});"); default: if (!IsFlagBase(p.Type)) { return($"ObjectUtils.SerializeObject({CheckForKeywordAndPascalCase(p.Name)},bw);"); } else { if (IsTrueFlag(p.Type)) { return($""); } else { TlParam p2 = new TlParam() { Name = p.Name, Type = p.Type.Split('?')[1] }; return($"if ((Flags & {GetBitMask(p.Type).ToString()}) != 0)" + Environment.NewLine + WriteWriteCode(p2, true)); } } } }
static void Main(string[] args) { Console.WriteLine("--- TL generator ---"); string absCsTemplate = File.ReadAllText("Tempaltes/ConstructorAbs.cstemplate"); string normalCsTemplate = File.ReadAllText("Tempaltes/Constructor.cstemplate"); string methodCsTemplate = File.ReadAllText("Tempaltes/Method.cstemplate"); //string method = File.ReadAllText("constructor.tt"); string schemaJson = LoadSchema() .GetAwaiter().GetResult(); TlSchema schema = JsonConvert.DeserializeObject <TlSchema>(schemaJson); //FileStream file = File.OpenWrite("Result.cs"); //StreamWriter sw = new StreamWriter(file); foreach (TlConstructor constructor in schema.Constructors) { interfacesList.Add(constructor.Type); classesList.Add(constructor.Predicate); } foreach (TlConstructor constructor in schema.Constructors) { IEnumerable <TlConstructor> list = schema.Constructors.Where(x => x.Type == constructor.Type); if (list.Count() > 1) { string path = (GetNameSpace(constructor.Type).Replace("TeleSharp.TL", "TL\\").Replace(".", "") + "\\" + GetNameofClass(constructor.Type, true) + ".cs").Replace("\\\\", "\\"); FileStream classFile = MakeFile(path); using (StreamWriter writer = new StreamWriter(classFile)) { string nspace = GetNameSpace(constructor.Type).Replace("TeleSharp.TL", "TL\\").Replace(".", "").Replace("\\\\", "\\").Replace("\\", "."); if (nspace.EndsWith(".")) { nspace = nspace.Remove(nspace.Length - 1, 1); } string temp = absCsTemplate.Replace("/* NAMESPACE */", "TeleSharp." + nspace); temp = temp.Replace("/* NAME */", GetNameofClass(constructor.Type, true)); writer.Write(temp); writer.Close(); classFile.Close(); } } else { interfacesList.Remove(list.First().Type); list.First().Type = c_TypeThis; } } foreach (TlConstructor c in schema.Constructors) { string path = (GetNameSpace(c.Predicate).Replace("TeleSharp.TL", "TL\\").Replace(".", "") + "\\" + GetNameofClass(c.Predicate, false) + ".cs").Replace("\\\\", "\\"); FileStream classFile = MakeFile(path); using (StreamWriter writer = new StreamWriter(classFile)) { #region About Class string nspace = GetNameSpace(c.Predicate).Replace("TeleSharp.TL", "TL\\").Replace(".", "").Replace("\\\\", "\\").Replace("\\", "."); if (nspace.EndsWith(".")) { nspace = nspace.Remove(nspace.Length - 1, 1); } string temp = normalCsTemplate.Replace("/* NAMESPACE */", "TeleSharp." + nspace); temp = c.Type == c_TypeThis?temp.Replace("/* PARENT */", "TLObject") : temp.Replace("/* PARENT */", GetNameofClass(c.Type, true)); temp = temp.Replace("/*Constructor*/", c.Id.ToString()); temp = temp.Replace("/* NAME */", GetNameofClass(c.Predicate, false)); #endregion #region Fields string fields = ""; foreach (TlParam tmp in c.Params) { fields += $" public {CheckForFlagBase(tmp.Type, GetTypeName(tmp.Type))} {CheckForKeywordAndPascalCase(tmp.Name)} " + "{get;set;}" + Environment.NewLine; } temp = temp.Replace("/* PARAMS */", fields); #endregion #region ComputeFlagFunc if (!c.Params.Any(x => x.Name == "Flags")) { temp = temp.Replace("/* COMPUTE */", ""); } else { string compute = "Flags = 0;" + Environment.NewLine; foreach (TlParam param in c.Params.Where(x => IsFlagBase(x.Type))) { if (IsTrueFlag(param.Type)) { compute += $"Flags = {CheckForKeywordAndPascalCase(param.Name)} ? (Flags | {GetBitMask(param.Type)}) : (Flags & ~{GetBitMask(param.Type)});" + Environment.NewLine; } else { compute += $"Flags = {CheckForKeywordAndPascalCase(param.Name)} != null ? (Flags | {GetBitMask(param.Type)}) : (Flags & ~{GetBitMask(param.Type)});" + Environment.NewLine; } } temp = temp.Replace("/* COMPUTE */", compute); } #endregion #region SerializeFunc string serialize = ""; if (c.Params.Any(x => x.Name == "Flags")) { serialize += "ComputeFlags();" + Environment.NewLine + "bw.Write(Flags);" + Environment.NewLine; } foreach (TlParam p in c.Params.Where(x => x.Name != "Flags")) { serialize += WriteWriteCode(p) + Environment.NewLine; } temp = temp.Replace("/* SERIALIZE */", serialize); #endregion #region DeSerializeFunc string deserialize = ""; foreach (TlParam p in c.Params) { deserialize += WriteReadCode(p) + Environment.NewLine; } temp = temp.Replace("/* DESERIALIZE */", deserialize); #endregion writer.Write(temp); writer.Close(); classFile.Close(); } } foreach (TlMethod c in schema.Methods) { string path = (GetNameSpace(c.Method).Replace("TeleSharp.TL", "TL\\").Replace(".", "") + "\\" + GetNameofClass(c.Method, false, true) + ".cs").Replace("\\\\", "\\"); FileStream classFile = MakeFile(path); using (StreamWriter writer = new StreamWriter(classFile)) { #region About Class string nspace = GetNameSpace(c.Method).Replace("TeleSharp.TL", "TL\\").Replace(".", "").Replace("\\\\", "\\").Replace("\\", "."); if (nspace.EndsWith(".")) { nspace = nspace.Remove(nspace.Length - 1, 1); } string temp = methodCsTemplate.Replace("/* NAMESPACE */", "TeleSharp." + nspace); temp = temp.Replace("/* PARENT */", "TLMethod"); temp = temp.Replace("/*Constructor*/", c.Id.ToString()); temp = temp.Replace("/* NAME */", GetNameofClass(c.Method, false, true)); #endregion #region Fields string fields = ""; foreach (TlParam tmp in c.Params) { fields += $" public {CheckForFlagBase(tmp.Type, GetTypeName(tmp.Type))} {CheckForKeywordAndPascalCase(tmp.Name)} " + "{get;set;}" + Environment.NewLine; } fields += $" public {CheckForFlagBase(c.Type, GetTypeName(c.Type))} Response" + "{ get; set;}" + Environment.NewLine; temp = temp.Replace("/* PARAMS */", fields); #endregion #region ComputeFlagFunc if (!c.Params.Any(x => x.Name == "Flags")) { temp = temp.Replace("/* COMPUTE */", ""); } else { string compute = "Flags = 0;" + Environment.NewLine; foreach (TlParam param in c.Params.Where(x => IsFlagBase(x.Type))) { if (IsTrueFlag(param.Type)) { compute += $"Flags = {CheckForKeywordAndPascalCase(param.Name)} ? (Flags | {GetBitMask(param.Type)}) : (Flags & ~{GetBitMask(param.Type)});" + Environment.NewLine; } else { compute += $"Flags = {CheckForKeywordAndPascalCase(param.Name)} != null ? (Flags | {GetBitMask(param.Type)}) : (Flags & ~{GetBitMask(param.Type)});" + Environment.NewLine; } } temp = temp.Replace("/* COMPUTE */", compute); } #endregion #region SerializeFunc string serialize = ""; if (c.Params.Any(x => x.Name == "Flags")) { serialize += "ComputeFlags();" + Environment.NewLine + "bw.Write(Flags);" + Environment.NewLine; } foreach (TlParam p in c.Params.Where(x => x.Name != "Flags")) { serialize += WriteWriteCode(p) + Environment.NewLine; } temp = temp.Replace("/* SERIALIZE */", serialize); #endregion #region DeSerializeFunc string deserialize = ""; foreach (TlParam p in c.Params) { deserialize += WriteReadCode(p) + Environment.NewLine; } temp = temp.Replace("/* DESERIALIZE */", deserialize); #endregion #region DeSerializeRespFunc string deserializeResp = ""; TlParam p2 = new TlParam() { Name = "Response", Type = c.Type }; deserializeResp += WriteReadCode(p2) + Environment.NewLine; temp = temp.Replace("/* DESERIALIZEResp */", deserializeResp); #endregion writer.Write(temp); writer.Close(); classFile.Close(); } } }
static void Main(string[] args) { string AbsStyle = File.ReadAllText("ConstructorAbs.tmp"); string NormalStyle = File.ReadAllText("Constructor.tmp"); string MethodStyle = File.ReadAllText("Method.tmp"); //string method = File.ReadAllText("constructor.tt"); string Json = ""; string url; if (!args.Any()) { url = "json"; } else { url = args[0]; } try { Json = File.ReadAllText(url); } catch (FileNotFoundException ex) { throw new Exception("Couldn't find schema JSON file, did you download it first e.g. with `wget https://core.telegram.org/schema/json`?", ex); } FileStream file = File.OpenWrite("Result.cs"); StreamWriter sw = new StreamWriter(file); TlSchema schema = JsonConvert.DeserializeObject <TlSchema>(Json); foreach (var c in schema.Constructors) { interfacesList.Add(c.Type); classesList.Add(c.Predicate); } foreach (var c in schema.Constructors) { var list = schema.Constructors.Where(x => x.Type == c.Type); if (list.Count() > 1) { string path = (GetNameSpace(c.Type).Replace("TeleSharp.TL", "TL\\").Replace(".", "") + "\\" + GetNameofClass(c.Type, true) + ".cs").Replace("\\\\", "\\"); FileStream classFile = MakeFile(path); using (StreamWriter writer = new StreamWriter(classFile)) { string nspace = (GetNameSpace(c.Type).Replace("TeleSharp.TL", "TL\\").Replace(".", "")).Replace("\\\\", "\\").Replace("\\", "."); if (nspace.EndsWith(".")) { nspace = nspace.Remove(nspace.Length - 1, 1); } string temp = AbsStyle.Replace("/* NAMESPACE */", "TeleSharp." + nspace); temp = temp.Replace("/* NAME */", GetNameofClass(c.Type, true)); writer.Write(temp); writer.Close(); classFile.Close(); } } else { interfacesList.Remove(list.First().Type); list.First().Type = "himself"; } } foreach (var c in schema.Constructors) { string path = (GetNameSpace(c.Predicate).Replace("TeleSharp.TL", "TL\\").Replace(".", "") + "\\" + GetNameofClass(c.Predicate, false) + ".cs").Replace("\\\\", "\\"); FileStream classFile = MakeFile(path); using (StreamWriter writer = new StreamWriter(classFile)) { #region About Class string nspace = (GetNameSpace(c.Predicate).Replace("TeleSharp.TL", "TL\\").Replace(".", "")).Replace("\\\\", "\\").Replace("\\", "."); if (nspace.EndsWith(".")) { nspace = nspace.Remove(nspace.Length - 1, 1); } string temp = NormalStyle.Replace("/* NAMESPACE */", "TeleSharp." + nspace); temp = (c.Type == "himself") ? temp.Replace("/* PARENT */", "TLObject") : temp.Replace("/* PARENT */", GetNameofClass(c.Type, true)); temp = temp.Replace("/*Constructor*/", c.Id.ToString()); temp = temp.Replace("/* NAME */", GetNameofClass(c.Predicate, false)); #endregion #region Fields string fields = ""; foreach (var tmp in c.Params) { fields += $" public {CheckForFlagBase(tmp.Type, GetTypeName(tmp.Type))} {CheckForKeywordAndPascalCase(tmp.Name)} " + "{get;set;}" + Environment.NewLine; } temp = temp.Replace("/* PARAMS */", fields); #endregion #region ComputeFlagFunc if (!c.Params.Any(x => x.Name == "Flags")) { temp = temp.Replace("/* COMPUTE */", ""); } else { var compute = "Flags = 0;" + Environment.NewLine; foreach (var param in c.Params.Where(x => IsFlagBase(x.Type))) { if (IsTrueFlag(param.Type)) { compute += $"Flags = {CheckForKeywordAndPascalCase(param.Name)} ? (Flags | {GetBitMask(param.Type)}) : (Flags & ~{GetBitMask(param.Type)});" + Environment.NewLine; } else { compute += $"Flags = {CheckForKeywordAndPascalCase(param.Name)} != null ? (Flags | {GetBitMask(param.Type)}) : (Flags & ~{GetBitMask(param.Type)});" + Environment.NewLine; } } temp = temp.Replace("/* COMPUTE */", compute); } #endregion #region SerializeFunc var serialize = ""; if (c.Params.Any(x => x.Name == "Flags")) { serialize += "ComputeFlags();" + Environment.NewLine + "bw.Write(Flags);" + Environment.NewLine; } foreach (var p in c.Params.Where(x => x.Name != "Flags")) { serialize += WriteWriteCode(p) + Environment.NewLine; } temp = temp.Replace("/* SERIALIZE */", serialize); #endregion #region DeSerializeFunc var deserialize = ""; foreach (var p in c.Params) { deserialize += WriteReadCode(p) + Environment.NewLine; } temp = temp.Replace("/* DESERIALIZE */", deserialize); #endregion writer.Write(temp); writer.Close(); classFile.Close(); } } foreach (var c in schema.Methods) { string path = (GetNameSpace(c.Method).Replace("TeleSharp.TL", "TL\\").Replace(".", "") + "\\" + GetNameofClass(c.Method, false, true) + ".cs").Replace("\\\\", "\\"); FileStream classFile = MakeFile(path); using (StreamWriter writer = new StreamWriter(classFile)) { #region About Class string nspace = (GetNameSpace(c.Method).Replace("TeleSharp.TL", "TL\\").Replace(".", "")).Replace("\\\\", "\\").Replace("\\", "."); if (nspace.EndsWith(".")) { nspace = nspace.Remove(nspace.Length - 1, 1); } string temp = MethodStyle.Replace("/* NAMESPACE */", "TeleSharp." + nspace); temp = temp.Replace("/* PARENT */", "TLMethod"); temp = temp.Replace("/*Constructor*/", c.Id.ToString()); temp = temp.Replace("/* NAME */", GetNameofClass(c.Method, false, true)); #endregion #region Fields string fields = ""; foreach (var tmp in c.Params) { fields += $" public {CheckForFlagBase(tmp.Type, GetTypeName(tmp.Type))} {CheckForKeywordAndPascalCase(tmp.Name)} " + "{get;set;}" + Environment.NewLine; } fields += $" public {CheckForFlagBase(c.Type, GetTypeName(c.Type))} Response" + "{ get; set;}" + Environment.NewLine; temp = temp.Replace("/* PARAMS */", fields); #endregion #region ComputeFlagFunc if (!c.Params.Any(x => x.Name == "Flags")) { temp = temp.Replace("/* COMPUTE */", ""); } else { var compute = "Flags = 0;" + Environment.NewLine; foreach (var param in c.Params.Where(x => IsFlagBase(x.Type))) { if (IsTrueFlag(param.Type)) { compute += $"Flags = {CheckForKeywordAndPascalCase(param.Name)} ? (Flags | {GetBitMask(param.Type)}) : (Flags & ~{GetBitMask(param.Type)});" + Environment.NewLine; } else { compute += $"Flags = {CheckForKeywordAndPascalCase(param.Name)} != null ? (Flags | {GetBitMask(param.Type)}) : (Flags & ~{GetBitMask(param.Type)});" + Environment.NewLine; } } temp = temp.Replace("/* COMPUTE */", compute); } #endregion #region SerializeFunc var serialize = ""; if (c.Params.Any(x => x.Name == "Flags")) { serialize += "ComputeFlags();" + Environment.NewLine + "bw.Write(Flags);" + Environment.NewLine; } foreach (var p in c.Params.Where(x => x.Name != "Flags")) { serialize += WriteWriteCode(p) + Environment.NewLine; } temp = temp.Replace("/* SERIALIZE */", serialize); #endregion #region DeSerializeFunc var deserialize = ""; foreach (var p in c.Params) { deserialize += WriteReadCode(p) + Environment.NewLine; } temp = temp.Replace("/* DESERIALIZE */", deserialize); #endregion #region DeSerializeRespFunc var deserializeResp = ""; TlParam p2 = new TlParam() { Name = "Response", Type = c.Type }; deserializeResp += WriteReadCode(p2) + Environment.NewLine; temp = temp.Replace("/* DESERIALIZEResp */", deserializeResp); #endregion writer.Write(temp); writer.Close(); classFile.Close(); } } }