public static void ParseObject(Models.Schema schema) { string AbsStyle = File.ReadAllText("ConstructorAbs.tmp"); string NormalStyle = File.ReadAllText("Constructor.tmp"); string MethodStyle = File.ReadAllText("Method.tmp"); string ToStyle = File.ReadAllText("ToMethod.tmp"); foreach (Constructor c in schema.constructors) { interfacesList.Add(c.type); classesList.Add(c.predicate); } //interfacesList = interfacesList.Distinct().ToList(); // Creating TLAbs*.cs files foreach (var c in schema.constructors) { 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)); temp = temp.Replace("/*TYPES*/", string.Join(",", schema.constructors.Where(x => x.type == c.type).Select(x => GetNameofClass(x.predicate)).ToArray())); temp = temp.Replace("/*Tos*/", string.Join("\n", schema.constructors.Where(x => x.type == c.type).Select(x => ToStyle.Replace("/*Type*/", GetNameofClass(x.predicate))))); writer.Write(temp); writer.Close(); classFile.Close(); } } // Creating TL.s files 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("/* 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))} {CheckForKeyword(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 = {CheckForKeyword(param.name)} ? (flags | {GetBitMask(param.type)}) : (flags & ~{GetBitMask(param.type)});" + Environment.NewLine; } else { compute += $"flags = {CheckForKeyword(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; } deserialize += $"Type = {GetNameofClass(c.type, true)}Types.{GetNameofClass(c.predicate)};"; temp = temp.Replace("/* DESERIALIZE */", deserialize); #endregion writer.Write(temp); writer.Close(); classFile.Close(); } } // creating TLRequest*.cs files 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))} {CheckForKeyword(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 = {CheckForKeyword(param.name)} ? (flags | {GetBitMask(param.type)}) : (flags & ~{GetBitMask(param.type)});" + Environment.NewLine; } else { compute += $"flags = {CheckForKeyword(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 = ""; Param p2 = new Param() { name = "Response", type = c.type }; deserializeResp += WriteReadCode(p2) + Environment.NewLine; temp = temp.Replace("/* DESERIALIZEResp */", deserializeResp); #endregion writer.Write(temp); writer.Close(); classFile.Close(); } } }
public static void ParseTL(string FileAddress) { string source = File.ReadAllText(FileAddress); IEnumerable <string> sourceLines = source.Split('\n'); //remove comments and empty lines sourceLines = sourceLines.Where(x => !x.StartsWith("//") && !string.IsNullOrEmpty(x) && !string.IsNullOrWhiteSpace(x)); //remove semicolons & Carriage Returns sourceLines = sourceLines.Select(x => x.Replace(";", "").Replace("\r", "")).ToList(); byte section = 1; // 1 types; 2 functions Models.Schema schema = new Models.Schema(); foreach (string line in sourceLines) { switch (line) { case "---types---": section = 1; continue; case "---functions---": section = 2; continue; } List <string> lineParts = line.Split(' ').ToList(); string constHeader = lineParts[0]; string constNameAssembly = constHeader.Split('#')[0]; string constAssembly = constNameAssembly.Contains(".") ? constNameAssembly.Split('.')[0] : ""; string constName = constNameAssembly.Contains(".") ? constNameAssembly.Split('.')[1] : constNameAssembly; string constId = constHeader.Split('#')[1]; if (constNameAssembly.Contains("vector")) { continue; } int equalIndex = lineParts.ToList().IndexOf("="); if (section == 1) // types (contsructors) { Models.Constructor @const = new Models.Constructor { id = int.Parse(constId, System.Globalization.NumberStyles.HexNumber), predicate = constNameAssembly, type = lineParts[equalIndex + 1] }; if (equalIndex > 1) { for (int i = 1; i < equalIndex; i++) { string constParam = lineParts[i]; if (constParam != "{X:Type}") { @const.Params.Add(new Models.Param { name = constParam.Split(':')[0], type = constParam.Split(':')[1] }); } } } schema.constructors.Add(@const); } else if (section == 2) // functions { Models.Method @const = new Models.Method { id = int.Parse(constId, System.Globalization.NumberStyles.HexNumber), method = constNameAssembly, type = lineParts[equalIndex + 1] }; if (equalIndex > 1) { for (int i = 1; i < equalIndex; i++) { string constParam = lineParts[i]; if (constParam != "{X:Type}") { @const.Params.Add(new Models.Param { name = constParam.Split(':')[0], type = constParam.Split(':')[1] }); } } } schema.methods.Add(@const); } } File.WriteAllText("schema.json", JsonConvert.SerializeObject(schema)); ParseObject(schema); }
public static void ParseJson(string JSonString) { Models.Schema obj = JsonConvert.DeserializeObject <Models.Schema>(JSonString); ParseObject(obj); }