コード例 #1
0
ファイル: Item.cs プロジェクト: aslash/Cpp2Lua
        public string GetDefaultValue()
        {
            string tStr = this.type.ToLower();

            if (tStr == "char" || tStr == "uchar")
            {
                if (this.length == 1)
                {
                    return("0");
                }
                else
                {
                    return("\"\"");
                }
            }

            else if (tStr == "int" || tStr == "uint" || tStr == "ushort" || tStr == "float")
            {
                if (this.length > 1)
                {
                    return("{}");
                }
                else
                {
                    return("0");
                }
            }
            else if (DefineValue.GetStructSize(type) > 0)
            {
                return("nil");
            }

            return("0");
        }
コード例 #2
0
        public void ReadDefineValue(string str)
        {
            while (str.Contains("#define"))
            {
                int startIndex = str.IndexOf("#define");
                str = str.Remove(0, startIndex);
                int lastIndex = str.IndexOf(Environment.NewLine);
                if (lastIndex == -1)
                {
                    lastIndex = str.IndexOf("\n");
                }

                string defineStr = str.Substring(0, lastIndex);
                defineStr = defineStr.Replace("\t", " ");

                string[] arr = defineStr.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                if (arr.Length < 3)
                {
                    str = str.Remove(0, lastIndex);
                    continue;
                }

                string key   = arr[1].Trim();
                string value = arr[2].Trim();

                int val;
                if (int.TryParse(value, out val))
                {
                    DefineValue.PutDefineValue(key, val);
                }
                str = str.Remove(0, lastIndex);
            }
        }
コード例 #3
0
ファイル: Item.cs プロジェクト: aslash/Cpp2Lua
        public int GetSize()
        {
            string tStr = this.type.ToLower();

            if (tStr == "char" || tStr == "uchar")
            {
                return(length);
            }

            else if (tStr == "int" || tStr == "uint" || tStr == "float")
            {
                return(4 * length);
            }

            else if (tStr == "ushort" || tStr == "short")
            {
                return(2 * length);
            }
            else
            {
                int size = DefineValue.GetStructSize(type) * length;
                if (size > 0)
                {
                    return(size);
                }
                else
                {
                    Console.WriteLine("[Error]Unknow Type:" + type);
                }
            }
            return(0);
        }
コード例 #4
0
ファイル: Item.cs プロジェクト: aslash/Cpp2Lua
        public void Construct(string key, string value)
        {
            type = key.Trim();
            if (value.Contains("[") == false)
            {
                name   = value;
                length = 1;
            }
            else
            {
                string[] arr = value.Split(new string[] { "[" }, StringSplitOptions.RemoveEmptyEntries);
                if (arr.Length == 2)
                {
                    name = arr[0];

                    string lenStr = "";
                    if (arr[1].Contains("["))
                    {
                        lenStr = arr[1].Replace("[", "");
                    }

                    if (arr[1].Contains("]"))
                    {
                        lenStr = arr[1].Replace("]", "");
                    }

                    int val = 0;
                    if (int.TryParse(lenStr, out val))
                    {
                        length = val;
                    }
                    else
                    {
                        length = DefineValue.GetDefineValue(lenStr);
                    }
                }
            }

            if (Util.IsCustomizeType(type))
            {
                type = DefineValue.CUSTOM_PREFIX + type;
                DefineValue.AddStructRefCount(type);
            }
        }
コード例 #5
0
ファイル: Item.cs プロジェクト: aslash/Cpp2Lua
        public string GetWriteBufferString()
        {
            StringBuilder sb   = new StringBuilder();
            int           size = DefineValue.GetStructSize(type);

            // 嵌套结构类型
            if (size > 0)
            {
                if (length <= 1)
                {
                    sb.Append("\t").Append("self.").Append(name).Append(":Write();").Append(Environment.NewLine);
                }
                else
                {
                    sb.Append("\tfor i=1,").Append(length).Append(",1 do").Append(Environment.NewLine);
                    sb.Append("\t\tself.").Append(name).Append("[i]").Append(":Write();").Append(Environment.NewLine);
                    sb.Append("\tend").Append(Environment.NewLine);
                }
            }
            else
            {
                string tStr = this.type.ToLower();
                if (tStr == "char")
                {
                    if (length > 1)
                    {
                        sb.Append("\tMessageByteBuffer.").Append("WriteBytes(self.").Append(name).Append(", ").Append(length.ToString()).Append(");").Append(Environment.NewLine);
                    }
                    else
                    {
                        sb.Append("\tMessageByteBuffer.").Append("WriteByte(self.").Append(name).Append(");").Append(Environment.NewLine);
                    }
                }
                else if (tStr == "uchar")
                {
                    if (length > 1)
                    {
                        sb.Append("\tfor i = 1, ").Append(length).Append(", 1 do").Append(Environment.NewLine);
                        sb.Append("\t\tif self.").Append(name).Append("[i] then").Append(Environment.NewLine);
                        sb.Append("\t\t\tMessageByteBuffer.WriteByte(self.").Append(name).Append("[i]);").Append(Environment.NewLine);
                        sb.Append("\t\telse").Append(Environment.NewLine);
                        sb.Append("\t\t\tMessageByteBuffer.WriteByte(0);").Append(Environment.NewLine);
                        sb.Append("\t\tend").Append(Environment.NewLine);
                        sb.Append("\tend").Append(Environment.NewLine);
                    }
                    else
                    {
                        sb.Append("\tMessageByteBuffer.").Append("WriteByte(self.").Append(name).Append(");").Append(Environment.NewLine);
                    }
                }
                else if (tStr == "int")
                {
                    if (length > 1)
                    {
                        sb.Append("\tfor i = 1, #").Append("self.").Append(name).Append(" do").Append(Environment.NewLine);
                        sb.Append("\t\tMessageByteBuffer.").Append("WriteInt(self.").Append(name).Append("[i]);").Append(Environment.NewLine);
                        sb.Append("\tend").Append(Environment.NewLine);
                    }
                    else
                    {
                        sb.Append("\tMessageByteBuffer.").Append("WriteInt(self.").Append(name).Append(");").Append(Environment.NewLine);
                    }
                }
                else if (tStr == "ushort")
                {
                    if (length > 1)
                    {
                        sb.Append("\tfor i = 1, #").Append("self.").Append(name).Append(" do").Append(Environment.NewLine);
                        sb.Append("\t\tMessageByteBuffer.").Append("WriteShort(self.").Append(name).Append("[i]);").Append(Environment.NewLine);
                        sb.Append("\tend").Append(Environment.NewLine);
                    }
                    else
                    {
                        sb.Append("\tMessageByteBuffer.").Append("WriteShort(self.").Append(name).Append(");").Append(Environment.NewLine);
                    }
                }
                else if (tStr == "uint")
                {
                    if (length > 1)
                    {
                        sb.Append("\tfor i = 1, #").Append("self.").Append(name).Append(" do").Append(Environment.NewLine);
                        sb.Append("\t\tMessageByteBuffer.").Append("WriteUInt32(self.").Append(name).Append("[i]);").Append(Environment.NewLine);
                        sb.Append("\tend").Append(Environment.NewLine);
                    }
                    else
                    {
                        sb.Append("\tMessageByteBuffer.").Append("WriteUInt32(self.").Append(name).Append(");").Append(Environment.NewLine);
                    }
                }
                else if (tStr == "float")
                {
                    if (length > 1)
                    {
                        sb.Append("\tfor i = 1, #").Append("self.").Append(name).Append(" do").Append(Environment.NewLine);
                        sb.Append("\t\tMessageByteBuffer.").Append("WriteFloat(self.").Append(name).Append("[i]);").Append(Environment.NewLine);
                        sb.Append("\tend").Append(Environment.NewLine);
                    }
                    else
                    {
                        sb.Append("\tMessageByteBuffer.").Append("WriteFloat(self.").Append(name).Append(");").Append(Environment.NewLine);
                    }
                }
            }

            return(sb.ToString());
        }
コード例 #6
0
ファイル: Item.cs プロジェクト: aslash/Cpp2Lua
        public string GetReadBufferString()
        {
            StringBuilder sb        = new StringBuilder();
            string        tStr      = this.type.ToLower();
            bool          structArr = false;

            if (tStr == "char")
            {
                if (length > 1)
                {
                    sb.Append("\tself.").Append(name).Append("=").Append("buffer:ReadString(").Append(length.ToString()).Append(")");
                }
                else
                {
                    sb.Append("\tself.").Append(name).Append("=").Append("buffer:ReadByte()");
                }
            }
            else if (tStr == "uchar")
            {
                if (length > 1)
                {
                    sb.Append("\tself.").Append(name).Append(" = {};").Append(Environment.NewLine);
                    sb.Append("\tfor i = 1, ").Append(length.ToString()).Append(", 1 do").Append(Environment.NewLine);
                    sb.Append("\t\tself.").Append(name).Append("[i]=").Append("buffer:ReadByte();").Append(Environment.NewLine);
                    sb.Append("\tend");
                }
                else
                {
                    sb.Append("\tself.").Append(name).Append("=").Append("buffer:ReadByte()");
                }
            }
            else if (tStr == "int")
            {
                if (length > 1)
                {
                    sb.Append("\tfor i = 1,").Append(length.ToString()).Append(", 1 do").Append(Environment.NewLine);
                    sb.Append("\t\tself.").Append(name).Append("[i]=").Append("buffer:ReadInt();").Append(Environment.NewLine);
                    sb.Append("\tend");
                }
                else
                {
                    sb.Append("\tself.").Append(name).Append("=").Append("buffer:ReadInt()");
                }
            }
            else if (tStr == "ushort")
            {
                if (length > 1)
                {
                    sb.Append("\tfor i = 1,").Append(length.ToString()).Append(", 1 do").Append(Environment.NewLine);
                    sb.Append("\t\tself.").Append(name).Append("[i]=").Append("buffer:ReadShort();").Append(Environment.NewLine);
                    sb.Append("\tend");
                }
                else
                {
                    sb.Append("\tself.").Append(name).Append("=").Append("buffer:ReadShort()");
                }
            }
            else if (tStr == "uint")
            {
                if (length > 1)
                {
                    sb.Append("\tfor i = 1,").Append(length.ToString()).Append(", 1 do").Append(Environment.NewLine);
                    sb.Append("\t\tself.").Append(name).Append("[i]=").Append("buffer:ReadUInt32();").Append(Environment.NewLine);
                    sb.Append("\tend");
                }
                else
                {
                    sb.Append("\tself.").Append(name).Append("=").Append("buffer:ReadUInt32()");
                }
            }
            else if (tStr == "float")
            {
                if (length > 1)
                {
                    sb.Append("\tfor i = 1,").Append(length.ToString()).Append(", 1 do").Append(Environment.NewLine);
                    sb.Append("\t\tself.").Append(name).Append("[i]=").Append("buffer:ReadFloat()").Append(Environment.NewLine);
                    sb.Append("\tend");
                }
                else
                {
                    sb.Append("\tself.").Append(name).Append("=").Append("Buffer:ReadFloat()");
                }
            }
            else
            {
                if (length <= 1)
                {
                    int size = DefineValue.GetStructSize(type);
                    if (size > 0)
                    {
                        sb.Append("\tself.").Append(name).Append("=").Append(type).Append(".Create();").Append(Environment.NewLine);
                        sb.Append("\tself.").Append(name).Append(":ReadFromBuffer(buffer)");
                    }
                    else
                    {
                        Console.WriteLine("[Error]Unknow Type:" + type);
                    }
                }
                else
                {
                    structArr = true;
                    sb.Append("\tself.").Append(name).Append(" = {};").Append(Environment.NewLine);
                    sb.Append("\tfor i=1,").Append(length + "").Append(",1 do").Append(Environment.NewLine);
                    sb.Append("\t\tself.").Append(name).Append("[i]=").Append(type).Append(".Create();").Append(Environment.NewLine);
                    sb.Append("\t\tself.").Append(name).Append("[i]:ReadFromBuffer(buffer);").Append(Environment.NewLine);
                    sb.Append("\tend").Append(Environment.NewLine);
                }
            }
            if (!structArr)
            {
                sb.Append(";").Append(Environment.NewLine);
            }
            return(sb.ToString());
        }
コード例 #7
0
ファイル: Struct.cs プロジェクト: aslash/Cpp2Lua
        public string GetStructDefineString()
        {
            StringBuilder sb = new StringBuilder();

            fileName = fileName.Substring(fileName.LastIndexOf("\\"), fileName.Length - fileName.LastIndexOf("\\"));
            fileName = fileName.Substring(1, fileName.Length - 1);

            // LoginReq = {username="", passwd=""}
            sb.Append("----------Define for ").Append(name).Append("----(").Append(fileName).Append(")----------").Append(Environment.NewLine);
            sb.Append(GetLuaClassDefine());

            sb.Append(Environment.NewLine);

            // LoginReq.__index = LoginReq
            sb.Append(name).Append(".__index = ").Append(name).Append(Environment.NewLine).Append(Environment.NewLine);

            //--------------------------Req有用部分------------------------------//
            //function LoginReq:New(u, p)
            sb.Append("----------给服务器发送的响应使用这个构造----------------").Append(Environment.NewLine);
            sb.Append("function ").Append(name).Append(":New(cmd");
            string ctorParamStr = GetLuaCtorParam();

            if (ctorParamStr == null || ctorParamStr.Length > 0)
            {
                sb.Append(",").Append(ctorParamStr);
            }
            sb.Append(")").Append(Environment.NewLine);

            // local self = {};
            sb.Append("\tlocal self = {};").Append(Environment.NewLine);

            // setmetatable(self, LoginReq);
            sb.Append("\tsetmetatable(self, ").Append(name).Append(");").Append(Environment.NewLine).Append(Environment.NewLine);
            sb.Append("\tself.cmd = cmd;").Append(Environment.NewLine);
            // self.username = u;
            // self.passwd = p;
            sb.Append("\tself.size=").Append(GetTotalSizeString()).Append(";").Append(Environment.NewLine);
            sb.Append(GetLuaCtorCode());

            // return self;    --返回自身
            // end
            sb.Append(Environment.NewLine).Append("\treturn self;").Append(Environment.NewLine).Append("end").Append(Environment.NewLine).Append(Environment.NewLine).Append(Environment.NewLine);

            sb.Append("----------编码----------------").Append(Environment.NewLine);
            // function LoginReq:WriteToBuffer()
            sb.Append("function ").Append(name).Append(":WriteToBuffer()").Append(Environment.NewLine);

            // MessageByteBuffer.CreateWithPackHead(tonumber(MessageType.MSG_TYPE_C_LOGINCHECKUSER), 64 + 8);
            sb.Append("\tMessageByteBuffer.CreateWithPackHead(self.cmd, ").Append(GetTotalSizeString()).Append(" + 8, GetServerTarget(self.cmd));").Append(Environment.NewLine).Append(Environment.NewLine);

            sb.Append(GetWriteBufferString());
            sb.Append("end").Append(Environment.NewLine).Append(Environment.NewLine);

            /*-----------------对于被引用的结构体,定义一个不处理头部的写buffer函数 ----------------*/
            if (DefineValue.GetStructRefCount(this.name) > 0)
            {
                sb.Append("function ").Append(name).Append(":Write()").Append(Environment.NewLine);
                sb.Append(GetWriteBufferString());
                sb.Append("end").Append(Environment.NewLine).Append(Environment.NewLine);
            }
            //----------------------------Rsp有用部分----------------------------------//

            //function LoginRsp:Create()
            sb.Append("----------服务器来的包用这个构造--------------").Append(Environment.NewLine);
            sb.Append("function ").Append(name).Append(":Create()").Append(Environment.NewLine);

            // local self = {};
            sb.Append("\tlocal self = {};").Append(Environment.NewLine);

            // setmetatable(self, LoginRsp);
            sb.Append("\tsetmetatable(self, ").Append(name).Append(");").Append(Environment.NewLine).Append(Environment.NewLine);
            sb.Append("\tself.cmd = 0;").Append(Environment.NewLine);
            // self.username = "";
            // self.passwd = 0;
            sb.Append("\tself.size=").Append(GetTotalSizeString()).Append(";").Append(Environment.NewLine);
            sb.Append(GetLuaEmptyCtorCode());

            // return self;    --返回自身
            // end
            sb.Append(Environment.NewLine).Append("\treturn self").Append(Environment.NewLine).Append("end").Append(Environment.NewLine).Append(Environment.NewLine).Append(Environment.NewLine);

            // function LoginRsp:ReadFromBuffer()
            sb.Append("----------解码----------").Append(Environment.NewLine);
            sb.Append("function ").Append(name).Append(":ReadFromBuffer(buffer)").Append(Environment.NewLine).Append(Environment.NewLine);


            //sb.Append("--没有用到的clientId和verifyNum").Append(Environment.NewLine);
            //sb.Append("\tbuffer:ReadInt();").Append(Environment.NewLine);
            //sb.Append("\tbuffer:ReadInt();").Append(Environment.NewLine);
            // sb.Append(Environment.NewLine);
            sb.Append(GetReadBufferString());
            sb.Append("end").Append(Environment.NewLine).Append(Environment.NewLine);
            return(sb.ToString());
        }
コード例 #8
0
ファイル: Struct.cs プロジェクト: aslash/Cpp2Lua
        public void Construct(string s, string file = "", bool incl = false)
        {
            fileName = file;
            included = incl;

            // 构造名字
            s = s.TrimStart().TrimEnd();

            if (s[0] == '{')
            {
                name = s.Substring(s.IndexOf("}") + 1, s.Length - s.IndexOf("}") - 1);
                name = name.Replace(";", "");
                name = name.TrimStart().TrimEnd();
            }
            else
            {
                name = s.Substring(0, s.IndexOf("{"));
                name = name.TrimStart().TrimEnd();
            }

            name = DefineValue.CUSTOM_PREFIX + name;

            // 构造成员列表
            string str = s.Substring(s.IndexOf("{") + 1, s.IndexOf("}") - s.IndexOf("{") - 1);

            string[] strList = str.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < strList.Length; i++)
            {
                //指针不处理
                if (str.Contains("*") || str.Contains("vector<") || str.Contains("list<") || str.Contains("map<"))
                {
                    this.unexported = true;
                    DefineValue.PutUnexportedType(name);
                    continue;
                }

                string   member = Util.TrimTable(strList[i]);
                string[] memArr;
                if (member.Contains("\t"))
                {
                    memArr = member.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
                }
                else
                {
                    memArr = member.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                }

                string key = "", value = "";
                if (memArr.Length == 2)
                {
                    key   = memArr[0];
                    value = memArr[1];
                }
                else
                {
                    for (int j = 0; j < memArr.Length; j++)
                    {
                        if (memArr[j].Contains("\t") == false && memArr[j].Contains(" ") == false)
                        {
                            key = memArr[j];
                            break;
                        }
                    }

                    for (int j = memArr.Length - 1; j >= 0; j--)
                    {
                        if (memArr[j].Contains("\t") == false && memArr[j].Contains(" ") == false)
                        {
                            value = memArr[j];
                            break;
                        }
                    }
                }

                Item item = new Item();
                if (key != "" && value != "")
                {
                    item.Construct(key, value);
                    if (DefineValue.GetUnexportedType(item.type))
                    {
                        this.unexported = true;
                    }
                    memberList.Add(item);
                }
            }

            DefineValue.PutStructSize(name, int.Parse(this.GetTotalSizeString()));
        }