- °³¿ä
- enum °ª »Ì¾Æ³»±â
- ÆÄÀ̽㠹öÀü
- ·ç¾Æ ¹öÀü
- ·çºñ ¹öÀü
- ÀÀ¿ëÇϱâ
1 °³¿ä
°ÔÀÓ ¾ÈÀÇ Æ¯Á¤ ±¸¼º¿ä¼Ò¸¦ ¿ÜºÎ ½ºÅ©¸³Æ®·Î »©³¾ ¶§ ¾à°£ °ñÄ©°Å¸®°¡ µÇ´Â °ÍÀÌ °ÔÀÓ ¾È¿¡¼ ¾²ÀÌ´Â »ó¼ö´Ù. ¿¹¸¦ µé¾î Ç÷¹À̾î Àκ¥Å丮ÀÇ ÃÖ´ë Å©±â¶ó´øÁö, °¢Á¾ ´É·ÂÄ¡ ÃÖ´ë°ª °°Àº °Íµé ¸»ÀÌ´Ù. ½ºÅ©¸³Æ® Äڵ带 ÀÛ¼ºÇÒ ¶§ ÀÌ·± »ó¼öµéÀ» Á÷Á¢ ½áÁÖ´Â ¹æ¹ýµµ ÀÖÁö¸¸, C/C++ ¼Ò½º¿¡¼ ÀÚµ¿À¸·Î »Ì¾Æ³»´Â °ÍÀÌ ÁÁ´Ù. ¿¡·¯°¡ ¹ß»ýÇÒ È®·üµµ ÁÙ°í, »ó¼öµéÀÌ º¯°æµÇ¾úÀ» ¶§µµ ½±°Ô ó¸®ÇÒ ¼ö Àֱ⠶§¹®ÀÌ´Ù.
ÀÌ·± ÀÛ¾÷ ÀÚü´Â C/C++·Î Â¥±âº¸´Ù´Â Á¤±Ô½Ä 󸮰¡ °¡´ÉÇÑ ½ºÅ©¸³Æ® ¾ð¾î¸¦ ÀÌ¿ëÇÏ´Â °ÍÀÌ ÁÁ´Ù. ¿Ïº®ÇÑ ÆÄ¼¸¦ ¸¸µé¸é ÁÁ°ÚÁö¸¸, µüÈ÷ ¸Å´Þ¸± ÇÊ¿ä´Â ¾øÀ¸¸ç, ¿øÇÏ´Â °ªµé¸¸ »Ì¾Æ³¾ ¼ö ÀÖÀ» Á¤µµ¶ó¸é, ¶«»§ÀÌ¶óµµ ±¦Âú´Ù°í »ý°¢ÇÑ´Ù.
2 enum °ª »Ì¾Æ³»±â
C/C++ ¼Ò½º¿¡¼ ¿°Å°ªÀ» »Ì¾Æ³»¾î Lua ¼Ò½º¸¦ ¸¸µé¾î³»´Â ½ºÅ©¸³Æ®ÀÌ´Ù.
2.1 ÆÄÀ̽㠹öÀü
# -*- coding: euc-kr -*-
# @file extract_enums.py
# @author excel96
# @brief C++ ¼Ò½º¿¡¼ ¿°Å°ª µ¥ÀÌÅ͸¦ LUA ¼Ò½º·Î »Ì¾Æ³»±â À§ÇÑ ½ºÅ©¸³Æ®
import re
import string
# ¹®ÀÚ¿¿¡¼ C++ identifier¸¦ »Ì¾Æ³½´Ù.
# "this_is_identifier = 0;" --> "this_is_identifier"
def extract_id(text):
m = re.search(r"[a-zA-Z_][a-zA-Z0-9_]*", text)
if m != None:
return m.group(0)
return ""
# ¹®ÀÚ¿¿¡¼ ÄÚ¸àÆ® ¹®ÀÚ¿À» »Ì¾Æ³½´Ù.
# ¿ø·¡ ¶óÀÎ ÁÖ¼®Àº "//" µÚÂÊÀÇ ¸ðµç ¹®ÀÚ¿ÀÌÁö¸¸,
# ÇöÀç ¼Ò½ºÀÇ Æ¯¼ö¼º(doxygen)À» °í·ÁÇÏ¿© "///<"¿¡ ´ëÇÑ Ã³¸®µµ Áý¾î³Ö¾ú´Ù.
# "// this is comment" --> "this is comment"
# "///< this is comment" --> "this is comment"
def extract_comment(text):
m = re.search(r"(//[/<]*)(.*)", text)
if m != None:
return m.group(2).strip()
return ""
# ÁÖ¾îÁø N°³ÀÇ ÆÄÀϵ鿡¼ ¿°Å°ªÀ» »Ì¾Æ³»¾î,
# ÇϳªÀÇ Ãâ·Â ÆÄÀÏ¿¡´Ù ±× °ªµéÀ» ±â·ÏÇÑ´Ù.
def generate(input_filenames, output_filename):
# Ãâ·Â ÆÄÀÏÀ» ¿¬´Ù.
output = file(output_filename, "w")
# ÀÔ·Â ÆÄÀÏ ¸ñ·Ï ¸ðµÎ¸¦ ¿¾î¼ ¿°Å°ªÀ» Ãâ·Â ÆÄÀÏ¿¡´Ù ±â·ÏÇÑ´Ù.
for filename in input_filenames:
input = file(filename, "r")
bEnum = 0 # ÇöÀç ¿°Å°ª ³»ºÎ¸¦ ó¸® ÁßÀΰ¡?
count = 0 # ¿°Å°ª ó¸® ÁßÀ̶ó¸é ÇöÀç ¿°Å°ªÀÇ ½ÇÁ¦ °ªÀº?
for line in input.readlines():
# ¿°Å°ª ó¸® ÁßÀ̶ó¸é...
if bEnum:
# ¿°Å°ª ±¸¹®ÀÇ ³¡¿¡ µµ´ÞÇß´ÂÁö üũÇÑ´Ù.
if re.search("^};$", line) != None:
output.write("\n")
bEnum = 0
# ¿°Å°ª ±¸¹®ÀÇ ³¡ÀÌ ¾Æ´Ï¶ó¸é, ¿°Å°ª°ú ±× °ªÀ» ±â·ÏÇÑ´Ù.
else:
id = extract_id(line)
if len(id) > 0:
text = id + " = " + str(count)
# ¿°Å°ª µÚÂÊ¿¡ ºÙÀº ÁÖ¼®À» ó¸®ÇÑ´Ù.
comment = extract_comment(line)
if len(comment) > 0:
text = text + " -- " + comment
output.write(text + "\n")
count = count + 1
# ¿°Å°ª ±¸¹®ÀÌ »õ·Î ½ÃÀ۵ǴÂÁö üũÇÑ´Ù.
elif re.search(r"(^enum)([\t ]+[a-zA-Z_][a-zA-Z0-9_]*)*", line) != None:
bEnum = 1
count = 0
input.close()
output.close()
# ÇØ´çÇÏ´Â ÆÄÀÏÀÇ µé¿©¾²±â¸¦ ¿¹»Ú°Ô(-_-) ¸¸µç´Ù.
def decorate(filename):
f = file(filename, "r")
originals = f.readlines()
seperator = "="
comment = "//"
lhs_max_length = 0
for line in originals:
pos = string.find(line, seperator)
if pos != -1:
lhs = line[:pos].strip()
rhs = line[pos+1:].strip()
lhs_max_length = max(lhs_max_length, len(lhs))
f.close()
f = file(filename, "w")
for line in originals:
pos = string.find(line, seperator)
text = line
if pos != -1:
lhs = line[:pos].strip()
rhs = line[pos+1:].strip()
needed_space = lhs_max_length - len(lhs);
for i in range(0, needed_space):
lhs = lhs + " ";
text = lhs + " " + seperator + " " + rhs + "\n"
f.write(text)
f.close()
input_files = [
"D:\\Project\\Binary\\Shared\\_NetLib\\TypesSkill.h",
"D:\\Project\\Binary\\Shared\\_NetLib\\TypesCreature.h"
]
generate(input_files, "test.lua")
decorate("test.lua")
2.2 ·ç¾Æ ¹öÀü
--[[
@file extract_enums.lua
@author excel96
@brief C++ ¼Ò½º¿¡¼ ¿°Å°ª µ¥ÀÌÅ͸¦ LUA ¼Ò½º·Î »Ì¾Æ³»±â À§ÇÑ ½ºÅ©¸³Æ®
--]]
-- ¹®ÀÚ¿¿¡¼ ¾ÕµÞÂÊÀÇ ºó °ø°£À» ³¯¸°´Ù.
function strip(text)
return string.gsub(text, "^[ \t\r\n]+|[ \t\r\n]+$", "")
end
-- ¹®ÀÚ¿¿¡¼ C++ identifier¸¦ »Ì¾Æ³½´Ù.
-- "this_is_identifier = 0;" --> "this_is_identifier"
function extract_id(text)
if text == nil then return "" end
local b, e = string.find(text, "[a-zA-Z_][a-zA-Z0-9_]*")
if b ~= nil then return strip(string.sub(text, b, e)) end
return ""
end
-- ¹®ÀÚ¿¿¡¼ ÄÚ¸àÆ® ¹®ÀÚ¿À» »Ì¾Æ³½´Ù.
-- ¿ø·¡ ¶óÀÎ ÁÖ¼®Àº "//" µÚÂÊÀÇ ¸ðµç ¹®ÀÚ¿ÀÌÁö¸¸,
-- ÇöÀç ¼Ò½ºÀÇ Æ¯¼ö¼º(doxygen)À» °í·ÁÇÏ¿© "///<"¿¡ ´ëÇÑ Ã³¸®µµ Áý¾î³Ö¾ú´Ù.
-- "// this is comment" --> "this is comment"
-- "///< this is comment" --> "this is comment"
function extract_comment(text)
local _, _, m1, m2 = string.find(text, "(//[/<]*)(.*)")
if m2 ~= nil then return strip(m2) end
return ""
end
-- ÁÖ¾îÁø N°³ÀÇ ÆÄÀϵ鿡¼ ¿°Å°ªÀ» »Ì¾Æ³»¾î,
-- ÇϳªÀÇ Ãâ·Â ÆÄÀÏ¿¡´Ù ±× °ªµéÀ» ±â·ÏÇÑ´Ù.
function generate(input_filenames, output_filename)
-- Ãâ·Â ÆÄÀÏÀ» ¿¬´Ù.
local output = assert(io.open(output_filename, "w"))
-- ÀÔ·Â ÆÄÀÏ ¸ñ·Ï ¸ðµÎ¸¦ ¿¾î¼ ¿°Å°ªÀ» Ãâ·Â ÆÄÀÏ¿¡´Ù ±â·ÏÇÑ´Ù.
for _, filename in pairs(input_filenames) do
local bEnum = false -- ÇöÀç ¿°Å°ª ³»ºÎ¸¦ ó¸® ÁßÀΰ¡?
local count = 0 -- ¿°Å°ª ó¸® ÁßÀ̶ó¸é ÇöÀç ¿°Å°ªÀÇ ½ÇÁ¦ °ªÀº?
for line in io.lines(filename) do
-- ¿°Å°ª ó¸® ÁßÀ̶ó¸é...
if bEnum then
-- ¿°Å°ª ±¸¹®ÀÇ ³¡¿¡ µµ´ÞÇß´ÂÁö üũÇÑ´Ù.
if string.find(line, "};") ~= nil then
output:write("\n")
bEnum = false
-- ¿°Å°ª ±¸¹®ÀÇ ³¡ÀÌ ¾Æ´Ï¶ó¸é, ¿°Å°ª°ú ±× °ªÀ» ±â·ÏÇÑ´Ù.
else
local id = extract_id(line)
if string.len(id) > 0 then
local text = id .. " = " .. tostring(count)
-- ¿°Å°ª µÚÂÊ¿¡ ºÙÀº ÁÖ¼®À» ó¸®ÇÑ´Ù.
local comment = extract_comment(line)
if string.len(comment) > 0 then
text = text .. " -- " .. comment
end
output:write(text .. "\n")
count = count + 1
end
end
-- ¿°Å°ª ±¸¹®ÀÌ »õ·Î ½ÃÀ۵ǴÂÁö üũÇÑ´Ù.
elseif string.find(line, "^enum") ~= nil then
bEnum = true
count = 0
end
end
end
output:close()
end
-- ÇØ´çÇÏ´Â ÆÄÀÏÀÇ µé¿©¾²±â¸¦ ¿¹»Ú°Ô(-_-) ¸¸µç´Ù.
function decorate(filename)
local originals = {}
local seperator = "="
local comment = "//"
local lhs_max_length = 0
for line in io.lines(filename) do
local b, e = string.find(line, seperator)
if b ~= nil then
local lhs = strip(string.sub(line, 1, b-1))
local rhs = strip(string.sub(line, b+1, string.len(line)))
lhs_max_length = math.max(lhs_max_length, string.len(lhs))
end
table.insert(originals, line)
end
f = assert(io.open(filename, "w"))
for _, line in pairs(originals) do
local text = line
local b, e = string.find(line, seperator)
if b ~= nil then
local lhs = strip(string.sub(line, 1, b-1))
local rhs = strip(string.sub(line, b+1, string.len(line)))
local needed_space = lhs_max_length - string.len(lhs)
for i=1,needed_space do
lhs = lhs .. " "
end
text = lhs .. seperator .. rhs .. "\n"
else
text = text .. "\n"
end
f:write(text)
end
f:close()
end
input_files = {
"D:\\Project\\Binary\\Shared\\_NetLib\\TypesSkill.h",
"D:\\Project\\Binary\\Shared\\_NetLib\\TypesCreature.h"
}
generate(input_files, "test.lua")
decorate("test.lua")
2.3 ·çºñ ¹öÀü
# \file extract_enums.py
# \brief C++ ¼Ò½º¿¡¼ ¿°Å°ª µ¥ÀÌÅ͸¦ LUA ¼Ò½º·Î »Ì¾Æ³»±â À§ÇÑ ½ºÅ©¸³Æ®
# ¹®ÀÚ¿¿¡¼ C++ identifier¸¦ »Ì¾Æ³½´Ù.
# "this_is_identifier = 0;" --> "this_is_identifier"
def extract_id(text)
match = text.scan(/[a-zA-Z_][a-zA-Z0-9_]*/)
return match.length > 0 ? match[0] : nil
end
# ¹®ÀÚ¿¿¡¼ C++ identifier¸¦ »Ì¾Æ³½´Ù.
# "this_is_identifier = 0;" --> "this_is_identifier"
def extract_id_ex(text)
match = text.scan(/([a-zA-Z_][a-zA-Z0-9_]*)([ \t]*=[ \t]*)/)
return match.length > 0 ? match[0][0] : nil
end
# ¹®ÀÚ¿¿¡¼ " = value" ±¸¹®À» ãÀº ´ÙÀ½, "value" ºÎºÐÀ» »Ì¾Æ³½´Ù.
# "a = 32" --> "32"
def extract_value(text)
match = text.scan(/(=)([ \t\r\n]*)([\"a-zA-Z0-9_.]*)/)
return match.length > 0 ? match[0][2] : nil
end
# ¹®ÀÚ¿ÀÌ ¼ýÀڷθ¸ ÀÌ·ç¾îÁ³´ÂÁöÀÇ ¿©ºÎ¸¦ ¹ÝȯÇÑ´Ù.
def is_digit(text)
return text.scan(/^[0-9]+$/).length > 0
end
# ¹®ÀÚ¿¿¡¼ ÄÚ¸àÆ® ¹®ÀÚ¿À» »Ì¾Æ³½´Ù.
# ¿ø·¡ ¶óÀÎ ÁÖ¼®Àº "//" µÚÂÊÀÇ ¸ðµç ¹®ÀÚ¿ÀÌÁö¸¸,
# ÇöÀç ¼Ò½ºÀÇ Æ¯¼ö¼º(doxygen)À» °í·ÁÇÏ¿© "///<"¿¡ ´ëÇÑ Ã³¸®µµ Áý¾î³Ö¾ú´Ù.
# "// this is comment" --> "this is comment"
# "///> this is comment" --> "this is comment"
def extract_comment(text)
match = text.scan(/(\/\/[\/<]*)(.*)/)
return match.length > 0 ? match[0][1].strip : nil
end
# ÇØ´çÇÏ´Â ÆÄÀÏÀÇ µé¿©¾²±â¸¦ ¿¹»Ú°Ô(-_-) ¸¸µç´Ù.
def decorate(filename)
originals = IO.readlines(filename)
seperator = "="
comment = "//"
lhs_max_length = 0
for line in originals
pos = line.index(seperator)
if pos != nil
lhs = line[0, pos].to_s.strip # nilÀÎ °æ¿ì°¡ ÀÖ¾î¼ to_s ó¸®
rhs = line[pos+1..-1].to_s.strip # nilÀÎ °æ¿ì°¡ ÀÖ¾î¼ to_s ó¸®
lhs_max_length = [lhs_max_length, lhs.length].max
end
end
f = File.open(filename, "w")
for line in originals
text = line
pos = line.index(seperator)
if pos != nil
lhs = line[0, pos].to_s.strip # nilÀÎ °æ¿ì°¡ ÀÖ¾î¼ to_s ó¸®
rhs = line[pos+1..-1].to_s.strip # nilÀÎ °æ¿ì°¡ ÀÖ¾î¼ to_s ó¸®
lhs = lhs + (" " * (lhs_max_length - lhs.length))
text = lhs.to_s + " " + seperator + " " + rhs.to_s + "\n"
end
f.write(text)
end
f.close
end
# ÁÖ¾îÁø N°³ÀÇ ÆÄÀϵ鿡¼ ¿°Å°ªÀ» »Ì¾Æ³»¾î,
# ÇϳªÀÇ Ãâ·Â ÆÄÀÏ¿¡´Ù ±× °ªµéÀ» ±â·ÏÇÑ´Ù.
def generate(input_filenames, output_filename)
# Ãâ·Â ÆÄÀÏÀ» ¿¬´Ù.
output = File.open(output_filename, "w")
# Çì´õ¸¦ Àû¾îÁØ´Ù.
output.write("--[[\n")
output.write("\\file " + output_filename + "\n")
output.write("\\author excel96\n")
output.write("\\brief ÀÚµ¿À¸·Î »ý¼ºµÈ ÆÄÀÏÀÔ´Ï´Ù.\n")
output.write("À߸øµÈ °ÍÀÌ ÀÖ´Ù¸é ¼öµ¿À¸·Î °íÄ¡Áö ¸»°í ½ºÅ©¸³Æ®¸¦ ¼ÕºÁ¾ß ÇÕ´Ï´Ù.\n")
output.write("--]]\n\n")
# ÀÔ·Â ÆÄÀÏ ¸ñ·Ï ¸ðµÎ¸¦ ¿¾î¼ ¿°Å°ªÀ» Ãâ·Â ÆÄÀÏ¿¡´Ù ±â·ÏÇÑ´Ù.
for filename in input_filenames
bEnum = false # ÇöÀç ¿°Å°ª ³»ºÎ¸¦ ó¸® ÁßÀΰ¡?
count = 0 # ¿°Å°ª ó¸® ÁßÀ̶ó¸é ÇöÀç ¿°Å°ªÀÇ ½ÇÁ¦ °ªÀº?
# ¿øº» ÆÄÀÏ¿¡ ´ëÇÑ Á¤º¸¸¦ ±â·ÏÇÑ´Ù.
header = "-- EXTRACTED FROM " + filename
output.write("-" * (header.length + 1) + "\n")
output.write(header + "\n")
output.write("-" * (header.length + 1) + "\n\n")
for line in IO.readlines(filename)
# ¿°Å°ª ó¸® ÁßÀ̶ó¸é...
if bEnum
# ¿°Å°ª ±¸¹®ÀÇ ³¡¿¡ µµ´ÞÇß´ÂÁö üũÇÑ´Ù.
if line.scan(/^\s*\}\s*;/).length > 0
output.write("\n")
bEnum = false
# ¿°Å°ª ±¸¹®ÀÇ ³¡ÀÌ ¾Æ´Ï¶ó¸é, ¿°Å°ª°ú ±× °ªÀ» ±â·ÏÇÑ´Ù.
else
if id = extract_id(line)
text = id
# °ª ¹®ÀÚ¿ÀÌ ÀÖ´ÂÁö È®ÀÎÇÑ´Ù.
if value = extract_value(line)
if is_digit(value)
text = text + " = " + value
count = value.to_i
else
text = text + " = " + value
end
else
text = text + " = " + count.to_s
end
# µÚÂÊ¿¡ ºÙÀº ÁÖ¼®À» ó¸®ÇÑ´Ù.
if comment = extract_comment(line)
text = text + " -- " + comment
end
output.write(text + "\n")
count = count + 1
end
end
# ¿°Å°ª ±¸¹®ÀÌ »õ·Î ½ÃÀ۵ǴÂÁö üũÇÑ´Ù.
elsif line.scan(/(^[ \t]*enum)([\t ]+[a-zA-Z_][a-zA-Z0-9_]*)*/).length > 0
bEnum = 1
count = 0
# »ó¼ö ±¸¹®ÀÎÁö¸¦ üũÇÑ´Ù.
elsif line.scan(/(static|)\s*const\s*(static|)\s*(unsigned|)\s*(int|float)\s*[A-Za-z0-9_]+\s*=\s*[\"A-Za-z0-9.]+;/).length > 0
id = extract_id_ex(line)
value = extract_value(line)
text = id + " = " + value
# µÚÂÊ¿¡ ºÙÀº ÁÖ¼®À» ó¸®ÇÑ´Ù.
if comment = extract_comment(line)
text = text + " -- " + comment
end
output.write(text + "\n\n")
end
end
end
output.close
# µé¿©¾²±â¸¦ ¼Õº»´Ù.
decorate(output_filename)
end
files1 = [
"D:\\Project\\XM\\Server\\ZTestAI\\Globals.h",
"D:\\Project\\XM\\Server\\ZTestAI\\NPC.h"
]
generate(files1, "Constant.lua")
3 ÀÀ¿ëÇϱâ
lua ¹®¹ý ÆÄÀÏ¿¡´Ù »ç¿ëÀÚ Å°¿öµå¸¦ Áý¾î³ÖÀº ¸ð½À (lua.properties ÆÄÀÏ ÀÚµ¿ »ý¼º)
Scite³ª VIM °°ÀÌ ¹®¹ý ÇÏÀ̶óÀÌÆ®´× Á¤º¸°¡ ¿ÜºÎ ÆÄÀÏ·Î ºüÁ®ÀÖ´Â ¿¡µðÅÍÀÇ °æ¿ì, ½ºÅ©¸³Æ®¿¡¼ ÇØ´çÇÏ´Â ¹®¹ý Á¤º¸ ÆÄÀϵµ °°ÀÌ »ý¼ºÇØÁÖ¸é, »ç¿ëÀÚ Å°¿öµå¸¦ ´Ù¸¥ »ö±ò·Î Ç¥½ÃÇÒ ¼ö ÀÖ´Ù. ¹°·Ð Á÷Á¢ ¹®¹ý ÆÄÀÏÀ» ÆíÁýÇØµµ °°Àº È¿°ú¸¦ º¼ ¼ö ÀÖ°ÚÁö¸¸, »ó¼öµéÀÌ ¾÷µ¥ÀÌÆ®µÉ ¶§¸¶´Ù ¹®¹ý ÆÄÀÏ ´Ù½Ã ¾÷µ¥ÀÌÆ®ÇÏ´Â °Íµµ ¸øÇÒ ÁþÀ̹ǷÎ, ½ºÅ©¸³Æ® ¾È¿¡¼ ó¸®ÇÏ´Â °ÍÀÌ ÁÁÀ» °ÍÀÌ´Ù.
SeriousMoin v1 (koMoinMoin 1.0a4 Modified)