๐Ÿ™‡โ€โ™€๏ธPacketGenerator2

  • PacketGenerator1์˜ ํ™œ๋™
    • PDL(PacketDefinitionList) ์ƒ์„ฑ
    • XmlReader๋ฅผ ํ†ตํ•ด PDL์„ ํŒŒ์‹ฑ
    • packet๊ณผ packet์˜ memeber๋“ค์„ ์ˆœ์ฐจ์ ์œผ๋กœ ํŒŒ์‹ฑ
    • ์ž๋™ํ™” ํˆด์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•˜์—ฌ PacketFormat class ์ƒ์„ฑ
    • public static string์œผ๋กœ ์—ฌ๋Ÿฌ ๋ฌธ์ž์—ด(@โ€โ€;)์„ Session์˜ Read, Write ๋“ฑ ํด๋ž˜์Šค ๋ณต์‚ฌ
    • ๊ณ ์ •์ ์ธ ๋ถ€๋ถ„์€ ๊ทธ๋Œ€๋กœ ๊ฐ€๋ณ€์ ์ธ ๋ถ€๋ถ„์€ {0} {1} โ€ฆ๋กœ ์น˜ํ™˜
  • PacketGenerator2์—์„œ ํ•  ๊ฒƒ๋“ค
    • genPakcets๋ผ๋Š” string์„ ๋งŒ๋“ค์–ด์„œ ์ด ํ•จ์ณ์ง„ gemPackets.csํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ๋‹ด์„ ๊ฒƒ
    • PacketFormat์—์„œ ๋งŒ๋“  {0} {1} .. ์„ ์ฑ„์›Œ์ฃผ๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋ฐ€์–ด๋„ฃ์–ด์ฃผ๊ณ  ํ•ฉ์ฒด
    • List๋ถ€๋ถ„๋„ ๋งŒ๋“ค์–ด์ค˜์„œ memberListFormat, readListFormat, writeListFormat ๋งŒ๋“ค์–ด ์ฃผ๊ธฐ

๐Ÿช์ฐจ๊ทผ์ฐจ๊ทผ

  • PacketGenerator Program
    • File.WriteAllText("GenPackets.cs",genPackets);๋กœ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด ์คŒ
      • ์ฒซ ์ธ์ž๋Š” ํŒŒ์ผ ์ด๋ฆ„, ๋‹ค์Œ ์ธ์ž๋Š” ์ž๋™์™„์„ฑ ํ•œ ๊ฒƒ๋“ค์„ ๋„ฃ์–ด์คŒ
    • static string genPackets; - ์ž๋™์™„์„ฑํ•œ๊ฒƒ์„ ์—ฌ๊ธฐ์— ๋ฐ€์–ด ๋„ฃ์–ด ์ค„๊ฑฐ์ž„
      • ParsePacket์„ ํ•˜๋Š” ๋™์•ˆ์— genPackets์— ๋„ฃ์„ ๊ฒƒ์„ ์™„์„ฑํ•ด์•ผ ๋จ
      • ParsePacket์† ParseMembers๊ฐ€ ๋๋‚˜๋ฉด genPackets += string.Format(PacketFormat.packetFormat, "", "", "", "")๋กœ ๋‚ด์šฉ์„ ๋ฐ€์–ด ๋„ฃ์Œ
        • ์ด๋•Œ โ€œโ€ ์€ ๊ฐ€๋ณ€์ธ์ž๋กœ {0} {1} ๋กœ ๋ฐ”๊พผ ๋ถ€๋ถ„๋“ค
        • ๊ฐ๊ฐ ํŒจํ‚ท ์ด๋ฆ„, ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋“ค, ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ Read, ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ Write ์ด๊ณ  ํŒจํ‚ท์ด๋ฆ„์€ packetName์œผ๋กœ ๋Œ€์‘๋จ
        • ๋‚˜๋จธ์ง€๋Š” ParseMember๋ฅผ void์—์„œ Tuple<string, string, string>์œผ๋กœ ๋ฐ”๊พธ์–ด ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๋งŒ๋“ค ํ›„ ๋Œ€์ž… - ๋‹ค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋“ค์ด๋‹ˆ๊น
        • Tuple<string, string, string> t = ParseMemebers(r);๋กœ ParsePacket์—์„œ ๋ฐ›๊ณ  ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋“ค, ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ Read, ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ Write๋ฅผ ๊ฐ๊ฐ t.Item1, t.Item2, t.Item3๋กœ ๋Œ€์ž…
      • genPackets += string.Format(PacketFormat.packetFormat, packetName, t.Item1, t.Item2, t.Item3)๋กœ ๋ณ€ํ™˜ ๋จ
  • ParseMember
    • string memberCode = ""; string readCode = ""; string writeCode = "";๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ  while(r.Read())๋ฅผ ๋Œ๋ฉด Tuple<string, string, string>์„ ๋ฐท์–ด์ฃผ๊ฒŒ ๋งŒ๋“ฆ
    • r.Read()๋ฅผ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ PDL์„ ํ•œ์ค„ํ•œ์ค„์”ฉ ๊ธ์–ด์˜ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ if(string.IsNullOrEmpty(memberCode) == false) memberCode += Environment.NewLine;์œผ๋กœ Enter๋ฅผ ์ณ์คŒ
    • if(string.IsNullOrEmpty(readCode) == false) readCode += Environment.NewLine; if(string.IsNullOrEmpty(writeCode) == false) writeCode += Environment.NewLine;๋„ ์ถ”๊ฐ€
string memberType = r.Name.ToLower();
switch (memberType)
{
    case "byte":
    case "sbyte":
        memberCode += string.Format(PacketFormat.memberFormat, memberType, memberName);
        readCode += string.Format(PacketFormat.readByteFormat, memberName, memberType);
        writeCode += string.Format(PacketFormat.writeByteFormat, memberName, memberType);
        break;
    case "bool":
    case "short":
    case "ushort":
    case "int":
    case "long":
    case "float":
    case "double":
        memberCode += string.Format(PacketFormat.memberFormat, memberType, memberName);
        readCode += string.Format(PacketFormat.readFormat, memberName, ToMemberType(memberType), memberType);
        writeCode += string.Format(PacketFormat.writeFormat, memberName, memberType);
        break;
    case "string":
        memberCode += string.Format(PacketFormat.memberFormat, memberType, memberName);
        readCode += string.Format(PacketFormat.readStringFormat, memberName);
        writeCode += string.Format(PacketFormat.writeStringFormat, memberName);
        break;
    case "list":
        Tuple<string, string, string> t = ParseList(r);
        memberCode += t.Item1;
        readCode += t.Item2;
        writeCode += t.Item3;
        break;
    default:
        break;
}

PDL์„ ํ•œ์ค„ํ•œ์ค„ ํŒŒ์‹ฑํ•˜๋ฉด์„œ ๊ธ์€ํ›„ memberCode, readCode, writeCode๋ฅผ ๋ง๋ถ™์—ฌ์„œ ์™„์„ฑ์„ ํ•œํ›„ Tuple๋กœ ์ „๋‹ฌํ•ด ์ฃผ๋Š” ๋ถ€๋ถ„

memberCode = memberCode.Replace("\n", "\n\t");
readCode = readCode.Replace("\n", "\n\t\t");
writeCode = writeCode.Replace("\n", "\n\t\t");

์ •๋ ฌ์„ ๋งž์ถฐ์ฃผ๋Š” ๊ฒƒ์œผ๋กœ memberCode.Replace("\n", "\n\t");์€ ์—”ํ„ฐ๊ฐ€ ์žˆ๋Š” ์• ๋“ค์€ ์—”ํ„ฐํ›„ ํƒญ์„ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”๋ผ๋Š” ๋œป

๊ทธํ›„ List๋„ ๊ฐ™์€ ์ž‘์—…..

๐Ÿช๋ถ„์„ ๋ฐ ์ž‘๋™ํŒŒ์•…ํ•˜๊ธฐ

์ผ๋‹จ PDL.xml์„ ๋งŒ๋“ค์—ˆ๊ณ  PacketGenerator์˜ Program์—์„œ XmlReader๋ฅผ ํ†ตํ•ด PDL์„ ํŒŒ์‹ฑ

Program์—์„œ ParsePacket๊ณผ ParseMembers๋ฅผ ํ†ตํ•ด genPackets์˜ ๋‚ด์šฉ๋ฌผ์„ ์™„์„ฑ ํ›„ File.WriteAllText(โ€œGenPackets.csโ€, genPackets);๋กœ ํŒŒ์ผ ์ƒ์„ฑ

xml์€ ์›๋ณธ ๋ฐ์ดํ„ฐ์˜ ๋Œ€๋žต์ ์ธ ๊ตฌ์กฐ๋ฅผ ์•Œ๊ฒŒํ•ด์ฃผ๋Š” ๊ฒƒ

๊ตฌ์กฐ๋ฅผ ์•Œ์•˜์œผ๋‹ˆ xml์„ ํŒŒ์‹ฑ์„ํ•ด์„œ ์›๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋™ ์ƒ์„ฑํ•˜๋Š” ํˆด์„ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค!! xml ํŒŒ์‹ฑ์€ XmlReader์˜ Read()๋กœ ํŒŒ์‹ฑ์„ ํ•˜๋Š” ๊ฒƒ์ด๊ณ  ํŒŒ์‹ฑ์˜ ๋‚ด์šฉ์€ r.Name๊ณผ r[โ€œnameโ€]์ด ์žˆ๋‹ค ๊ฐ๊ฐ ํƒ€์ž…๊ณผ name์— ๋“ค์–ด๊ฐ„ ๊ฐ’์ด ๋‚˜์˜จ๋‹ค

xml์„ ํŒŒ์‹ฑํ•˜๋Š”๋ฐ packet ๋ถ€๋ถ„๊ณผ member๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋‰œ๋‹ค

  • ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํŒจํ‚ท์ด ์žˆ์œผ๋ฏ€๋กœ ํ•œํŒจํ‚ท๋‹น class๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•จ์ด๋ผ ์ƒ๊ฐ๋œ๋‹ค (class๊ฐ€ ์•„๋‹ˆ๋”๋ผ๊ณ  ๊ตฌ๋ถ„ํ•ด์ฃผ๋Š” ์—ญํ• ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ ๊ฑฐ๊ฐ™๋‹ค)

๊ทธ๋ ‡๋‹ค๋Š” ๊ฒƒ์€ parsePacket์— ๋“ค์–ด์˜ค๋ฉด ๊ทธ ํŒจํ‚ท์•ˆ์˜ members๋ฅผ ํŒŒ์‹ฑ์„ ํ•ด์•ผ ๋œ๋‹ค๋Š” ๊ฒƒ!

ParseMemebers๋กœ r.Depth + 1์ธ ๋ถ€๋ถ„๋“ค๋งŒ ํŒŒ์‹ฑ์„ํ•˜์—ฌ Packet๋‚ด์˜ ๋ฉค๋ฒ„๋“ค๋งŒ ํŒŒ์‹ฑ์„ ํ•˜๊ฒŒ ํ•œ๋‹ค

๋ฉค๋ฒ„๋“ค์˜ r[โ€œnameโ€]๊ณผ r.Name์„ ํŒŒ์‹ฑ์„ํ•˜๊ณ  ํƒ€์ž…์— ๋”ฐ๋ผ ์ž๋™ํ™” ๋˜๋Š” ๋ถ€๋ถ„์„ ๋งŒ๋“ค์–ด ์ฃผ๋ฉด ๋œ๋‹ค

  • ํƒ€์ž…์ด๋ผํ•˜๋ฉด xml์„ ํ†ตํ•ด ์•Œ์ˆ˜์žˆ๋Š” ์›๋ณธ ๋ฐ์ดํ„ฐ์˜ ํ‹€์ด๋‹ค
  • ๊ทธ ํ‹€์—์„œ ์ž๋™ํ™” ๋˜๋Š” ๋ถ€๋ถ„์„ ์ง‘์–ด ๋„ฃ์œผ๋ฉด ์›๋ณธ๊ณผ ๊ฐ™์•„์ง€๋Š” ๊ฒƒ์ด๋‹ค

๊ฐ ํƒ€์ž… ์ผ€์ด์Šค๋งˆ๋‹ค ๊ฐ’์„ ๋„ฃ์–ด์ค˜์•ผ ํ•˜๋Š”๋ฐ ์ด๋•Œ PacketFormat์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค PacketFormat ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ ์ž๋™ํ™”ํ•  ๋‚ด์šฉ์„ ๋ฐ”๊ฟ”์น˜๊ธฐ ํ•˜๋Š” ๊ฒƒ!!

๊ฐ๊ฐ์˜ ํƒ€์ž…์— ๋งž๊ฒŒ ๋‚ด์šฉ์ด ๋ฐ”๋€Œ๋‹ˆ ๊ทธ๊ฑฐ์— ๋งž์ถฐ์„œ memberCode, readCode, writeCode๋ฅผ ๋งŒ๋“ค์–ด ๋ฐ€์–ด ๋„ฃ์–ด์ค€๋‹ค

๋ชจ๋“  ์ผ€์ด์Šค๋ฅผ ๋Œ๊ณ ๋‚˜์„œ ์™„์„ฑ๋œ ํฐ~~~ ๋ฌธ์ž์—ด์€ WriteAllText๋ฅผ ํ†ตํ•ด csํŒŒ์ผ๋กœ ๋งŒ๋“ ๋‹ค

์ž๋™ํ™” ๋.

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: