SourceForge: love/love: changeset 133:0e3987070a2a
Added a script for converting Lua files to char arrays.
authorrude
Sun Nov 08 16:59:18 2009 +0100 (2 weeks ago)
changeset 1330e3987070a2a
parent 130d84e14bb7032
child 1345dbdebba10f8
Added a script for converting Lua files to char arrays.
src/scripts/auto.lua
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/scripts/auto.lua	Sun Nov 08 16:59:18 2009 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +-- Usage:
     1.5 +-- lua auto.lua <name1> <name2> .. <nameN>
     1.6 +-- 
     1.7 +-- Example:
     1.8 +-- lua auto.lua boot graphics
     1.9 +
    1.10 +max_width = 15
    1.11 +
    1.12 +function auto(name)
    1.13 +	
    1.14 +	local src = name .. ".lua" 
    1.15 +	local dst = name .. ".lua.h" 
    1.16 +	local cpp_name = name .. "_lua"
    1.17 +	
    1.18 +	-- Read source Lua file
    1.19 +	local src_file = io.open(src, "rb")
    1.20 +	local src_data = src_file:read("*a")
    1.21 +	local src_len = #src_data
    1.22 +	src_file:close()
    1.23 +	
    1.24 +	local lines = {}
    1.25 +	local line = {}
    1.26 +	table.insert(lines, line)
    1.27 +	
    1.28 +	for i=1,src_len do
    1.29 +		table.insert(line, string.byte(src_data, i))
    1.30 +		if math.mod(i, max_width) == 0 then
    1.31 +			line = {}
    1.32 +			table.insert(lines, line)
    1.33 +		end
    1.34 +	end
    1.35 +	
    1.36 +	local src_output = "const char "..cpp_name.."[] = \n{\n"
    1.37 +		
    1.38 +	for i,line in ipairs(lines) do
    1.39 +		local concat = {}
    1.40 +		for j,b in ipairs(line) do
    1.41 +			table.insert(concat, string.format("0x%02X,", b))
    1.42 +		end
    1.43 +		src_output = src_output .. "\t" .. table.concat(concat, "").."\n"
    1.44 +	end
    1.45 +	
    1.46 +	src_output = src_output .. "};"
    1.47 +	
    1.48 +	local include = true
    1.49 +	
    1.50 +	-- Read dst
    1.51 +	local dst_lines = {}
    1.52 +	for line in io.lines(dst) do
    1.53 +			
    1.54 +		if line == "// ["..src.."]" then
    1.55 +			include = false
    1.56 +			table.insert(dst_lines, line)
    1.57 +			table.insert(dst_lines, src_output)
    1.58 +		end
    1.59 +		
    1.60 +		if line == "// [/"..src.."]" then
    1.61 +			include = true
    1.62 +		end
    1.63 +		
    1.64 +		if include then
    1.65 +			table.insert(dst_lines, line)
    1.66 +		end
    1.67 +	end
    1.68 +
    1.69 +	local tmp_data = table.concat(dst_lines, "\n")
    1.70 +	
    1.71 +	-- Overwrite the old file only if they are different.
    1.72 +	local dst_file = io.open(dst, "rb")
    1.73 +	dst_data = dst_file:read("*a")
    1.74 +	dst_file:close()
    1.75 +	
    1.76 +	if tmp_data == dst_data then
    1.77 +		print(name .. ": no change")
    1.78 +	else
    1.79 +	
    1.80 +		local dst_file = io.open(dst, "wb")
    1.81 +		dst_file:write(tmp_data)
    1.82 +		dst_file:close()
    1.83 +	
    1.84 +		print(name .. ": updated")
    1.85 +	end
    1.86 +
    1.87 +end
    1.88 +
    1.89 +if #arg == 0 then
    1.90 +	print("Usage: lua auto.lua <name1> <name2> .. <name3>")
    1.91 +else
    1.92 +	for i, v in ipairs(arg) do
    1.93 +		auto(v)
    1.94 +	end
    1.95 +end
    1.96 +