auto.lua 3.2 KB

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