Exemple #1
0
 public static string WriteWriteCode(Param p, bool flag = false)
 {
     switch (p.type.ToLower())
     {
         case "#":
         case "int":
             return flag ? $"bw.Write({CheckForKeyword(p.name)}.Value);" : $"bw.Write({CheckForKeyword(p.name)});";
         case "long":
             return flag ? $"bw.Write({CheckForKeyword(p.name)}.Value);" : $"bw.Write({CheckForKeyword(p.name)});";
         case "string":
             return $"StringUtil.Serialize({CheckForKeyword(p.name)},bw);";
         case "bool":
             return flag ? $"BoolUtil.Serialize({CheckForKeyword(p.name)}.Value,bw);" : $"BoolUtil.Serialize({CheckForKeyword(p.name)},bw);";
         case "true":
             return $"BoolUtil.Serialize({CheckForKeyword(p.name)},bw);";
         case "bytes":
             return $"BytesUtil.Serialize({CheckForKeyword(p.name)},bw);";
         case "double":
             return flag ? $"bw.Write({CheckForKeyword(p.name)}.Value);" : $"bw.Write({CheckForKeyword(p.name)});";
         default:
             if (!IsFlagBase(p.type))
                 return $"ObjectUtils.SerializeObject({CheckForKeyword(p.name)},bw);";
             else
             {
                 if (IsTrueFlag(p.type))
                     return $"";
                 else
                 {
                     Param p2 = new Param() { name = p.name, type = p.type.Split('?')[1] };
                     return $"if ((flags & {GetBitMask(p.type).ToString()}) != 0)" + Environment.NewLine +
                         WriteWriteCode(p2, true);
                 }
             }
     }
 }
Exemple #2
0
        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.Count() == 0) url = "tl-schema.json"; else url = args[0];

            Json = File.ReadAllText(url);
            FileStream file = File.OpenWrite("Result.cs");
            StreamWriter sw = new StreamWriter(file);
            Schema schema = JsonConvert.DeserializeObject<Schema>(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))} {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;
                    }
                    temp = temp.Replace("/* DESERIALIZE */", deserialize);
                    #endregion
                    writer.Write(temp);
                    writer.Close();
                    classFile.Close();
                }
            }
            foreach (var c in schema.methods)
            {
                if (c.method.Contains("updateUsername"))
                {

                }
                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();
                }
            }
        }
Exemple #3
0
 public static string WriteReadCode(Param p)
 {
     switch (p.type.ToLower())
     {
         case "#":
         case "int":
             return $"{CheckForKeyword(p.name)} = br.ReadInt32();";
         case "long":
             return $"{CheckForKeyword(p.name)} = br.ReadInt64();";
         case "string":
             return $"{CheckForKeyword(p.name)} = StringUtil.Deserialize(br);";
         case "bool":
         case "true":
             return $"{CheckForKeyword(p.name)} = BoolUtil.Deserialize(br);";
         case "bytes":
             return $"{CheckForKeyword(p.name)} = BytesUtil.Deserialize(br);";
         case "double":
             return $"{CheckForKeyword(p.name)} = br.ReadDouble();";
         default:
             if (!IsFlagBase(p.type))
             {
                 if (p.type.ToLower().Contains("vector"))
                 {
                     return $"{CheckForKeyword(p.name)} = ({GetTypeName(p.type)})ObjectUtils.DeserializeVector<{GetTypeName(p.type).Replace("TLVector<","").Replace(">","")}>(br);";
                 }
                 else return $"{CheckForKeyword(p.name)} = ({GetTypeName(p.type)})ObjectUtils.DeserializeObject(br);";
             }
             else
             {
                 if (IsTrueFlag(p.type))
                     return $"{CheckForKeyword(p.name)} = (flags & {GetBitMask(p.type).ToString()}) != 0;";
                 else
                 {
                     Param p2 = new Param() { 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 +
                         $"{CheckForKeyword(p.name)} = null;" + Environment.NewLine;
                 }
             }
     }
 }