Mercurial > love-experiments
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 |
| rev | line source |
|---|---|
| 1096 | 1 -- Usage: |
| 2 -- lua auto.lua <name1> <name2> .. <nameN> | |
| 3 -- | |
| 4 -- Example: | |
| 5 -- lua auto.lua boot graphics | |
| 6 | |
| 7 local max_width = 18 | |
| 8 local pattern = [[ | |
| 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 | 28 |
| 29 namespace love | |
| 30 { | |
| 31 | |
| 32 // [%s] | |
| 33 const unsigned char %s[] = | |
| 34 { | |
| 35 %s | |
| 36 }; // [%s] | |
| 37 } // love]] | |
| 38 --formatting parameters: | |
| 39 -- - input file name | |
| 40 -- - c variable name | |
| 41 -- - array contents | |
| 42 -- - input file name | |
| 43 | |
| 44 | |
| 45 function auto(name) | |
| 46 --the input file name | |
| 47 local src = name .. ".lua" | |
| 48 --and the output one | |
| 49 local dst = name .. ".lua.h" | |
| 50 --the name of the variable | |
| 51 local cpp_name = name .. "_lua" | |
| 52 | |
| 53 --do a minimal code check | |
| 54 --(syntax errors, really) | |
| 55 loadfile(src) | |
| 56 --no error catching? no | |
| 57 --we have the main loop doing that for us | |
| 58 | |
| 59 --what character is this on this line? | |
| 60 local counter = 0 | |
| 61 | |
| 62 local function tohex(c) | |
| 63 counter = counter + 1 | |
| 64 --if we've reached the maximum width (or 0) | |
| 65 --then we'll carry on and add a newline | |
| 66 if counter % max_width == 0 then | |
| 67 return ("\n\t0x%02x, "):format(c:byte()) | |
| 68 end | |
| 69 --otherwise we just use the hex of the current byte | |
| 70 return ("0x%02x, "):format(c:byte()) | |
| 71 end | |
| 72 | |
| 73 --let's open the input file | |
| 74 local src_file = io.open(src, "rb") | |
| 75 --create an output string | |
| 76 local out_data = "" | |
| 77 --go through the input file line-by-line | |
| 78 for line in src_file:lines() do | |
| 79 --if the line is non-empty | |
| 80 if #line > 0 then | |
| 81 --set the counter to -1 | |
| 82 --this will start a new line (see tohex) | |
| 83 counter = -1 | |
| 84 --append the output to what we had, plus a newline character (0x0a is newline) | |
| 85 out_data = ("%s%s0x0a,"):format(out_data, line:gsub("\r", ""):gsub(".", tohex)) | |
| 86 end | |
| 87 end | |
| 88 --close our input | |
| 89 src_file:close() | |
| 90 | |
| 91 --open, write and close the output | |
| 92 out_file = io.open(dst, "wb") | |
| 93 --see pattern above | |
| 94 out_file:write(pattern:format(src, cpp_name, out_data, src)) | |
| 95 out_file:close() | |
| 96 | |
| 97 --tell the world we succeeded! | |
| 98 print(name .. ": Success") | |
| 99 end | |
| 100 | |
| 101 --usage | |
| 102 if #arg == 0 then | |
| 103 return print("Usage: lua auto.lua <name1> <name2> .. <name3>") | |
| 104 end | |
| 105 | |
| 106 --the 'main' procedure | |
| 107 for i, v in ipairs(arg) do | |
| 108 --run the auto function for every argument | |
| 109 --but do it with pcall, to catch errors | |
| 110 local ok, err = pcall(auto, v:gsub("%.lua$","")) | |
| 111 if not ok then | |
| 112 --inform people we've failed | |
| 113 print(v .. ": " .. err) | |
| 114 end | |
| 115 end | |
| 116 |
