annotate src/scripts/auto.lua @ 1448:108ee2e81f65 default tip

Fix a case typo, imageData.h -> ImageData.h
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sat, 18 Jan 2014 13:57:44 +0100
parents e01d98087577
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1096
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
1 -- Usage:
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
2 -- lua auto.lua <name1> <name2> .. <nameN>
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
3 --
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
4 -- Example:
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
5 -- lua auto.lua boot graphics
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
6
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
7 local max_width = 18
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
8 local pattern = [[
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
9 /**
1294
e01d98087577 Fixed the lua header generator script to output the correct year in the copyright text
Alex Szpakowski <slime73@gmail.com>
parents: 1282
diff changeset
10 * Copyright (c) 2006-2013 LOVE Development Team
1282
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
11 *
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
12 * This software is provided 'as-is', without any express or implied
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
13 * warranty. In no event will the authors be held liable for any damages
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
14 * arising from the use of this software.
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
15 *
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
16 * Permission is granted to anyone to use this software for any purpose,
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
17 * including commercial applications, and to alter it and redistribute it
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
18 * freely, subject to the following restrictions:
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
19 *
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
20 * 1. The origin of this software must not be misrepresented; you must not
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
21 * claim that you wrote the original software. If you use this software
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
22 * in a product, an acknowledgment in the product documentation would be
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
23 * appreciated but is not required.
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
24 * 2. Altered source versions must be plainly marked as such, and must not be
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
25 * misrepresented as being the original software.
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
26 * 3. This notice may not be removed or altered from any source distribution.
b7f097053d74 Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents: 1096
diff changeset
27 **/
1096
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
28
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
29 namespace love
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
30 {
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
31
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
32 // [%s]
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
33 const unsigned char %s[] =
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
34 {
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
35 %s
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
36 }; // [%s]
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
37 } // love]]
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
38 --formatting parameters:
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
39 -- - input file name
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
40 -- - c variable name
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
41 -- - array contents
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
42 -- - input file name
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
43
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
44
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
45 function auto(name)
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
46 --the input file name
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
47 local src = name .. ".lua"
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
48 --and the output one
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
49 local dst = name .. ".lua.h"
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
50 --the name of the variable
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
51 local cpp_name = name .. "_lua"
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
52
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
53 --do a minimal code check
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
54 --(syntax errors, really)
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
55 loadfile(src)
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
56 --no error catching? no
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
57 --we have the main loop doing that for us
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
58
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
59 --what character is this on this line?
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
60 local counter = 0
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
61
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
62 local function tohex(c)
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
63 counter = counter + 1
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
64 --if we've reached the maximum width (or 0)
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
65 --then we'll carry on and add a newline
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
66 if counter % max_width == 0 then
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
67 return ("\n\t0x%02x, "):format(c:byte())
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
68 end
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
69 --otherwise we just use the hex of the current byte
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
70 return ("0x%02x, "):format(c:byte())
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
71 end
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
72
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
73 --let's open the input file
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
74 local src_file = io.open(src, "rb")
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
75 --create an output string
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
76 local out_data = ""
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
77 --go through the input file line-by-line
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
78 for line in src_file:lines() do
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
79 --if the line is non-empty
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
80 if #line > 0 then
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
81 --set the counter to -1
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
82 --this will start a new line (see tohex)
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
83 counter = -1
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
84 --append the output to what we had, plus a newline character (0x0a is newline)
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
85 out_data = ("%s%s0x0a,"):format(out_data, line:gsub("\r", ""):gsub(".", tohex))
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
86 end
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
87 end
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
88 --close our input
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
89 src_file:close()
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
90
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
91 --open, write and close the output
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
92 out_file = io.open(dst, "wb")
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
93 --see pattern above
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
94 out_file:write(pattern:format(src, cpp_name, out_data, src))
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
95 out_file:close()
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
96
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
97 --tell the world we succeeded!
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
98 print(name .. ": Success")
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
99 end
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
100
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
101 --usage
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
102 if #arg == 0 then
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
103 return print("Usage: lua auto.lua <name1> <name2> .. <name3>")
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
104 end
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
105
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
106 --the 'main' procedure
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
107 for i, v in ipairs(arg) do
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
108 --run the auto function for every argument
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
109 --but do it with pcall, to catch errors
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
110 local ok, err = pcall(auto, v:gsub("%.lua$",""))
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
111 if not ok then
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
112 --inform people we've failed
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
113 print(v .. ": " .. err)
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
114 end
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
115 end
86a44f2bc38f CRLF to LF
Bart van Strien <bart.bes+projects@gmail.com>
parents: 996
diff changeset
116