Advertisement
Guest User

bobthediosaur

a guest
Nov 27th, 2011
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. --------------------------------------------------------------------------------
  2. --------------------------------------------------------------------------------
  3. --
  4. -- file: unitdefs.lua
  5. -- brief: unitdef parser
  6. -- author: Dave Rodgers
  7. --
  8. -- Copyright (C) 2007.
  9. -- Licensed under the terms of the GNU GPL, v2 or later.
  10. --
  11. --------------------------------------------------------------------------------
  12. --------------------------------------------------------------------------------
  13.  
  14. local unitDefs = {}
  15.  
  16. local shared = {} -- shared amongst the lua unitdef enviroments
  17.  
  18. local preProcFile = 'gamedata/unitdefs_pre.lua'
  19. local postProcFile = 'gamedata/unitdefs_post.lua'
  20.  
  21. local FBI = FBIparser or VFS.Include('gamedata/parse_fbi.lua')
  22. local TDF = TDFparser or VFS.Include('gamedata/parse_tdf.lua')
  23. local DownloadBuilds = VFS.Include('gamedata/download_builds.lua')
  24.  
  25. local system = VFS.Include('gamedata/system.lua')
  26.  
  27. local function FileSearch(startingDir, fileType)
  28. local files = {}
  29. local function AddSubDir(dir)
  30. for _,file in ipairs(VFS.DirList(dir, fileType)) do
  31. files[#files + 1] = file
  32. end
  33. for _,sd in ipairs(VFS.SubDirs(dir)) do
  34. AddSubDir(sd)
  35. end
  36. end
  37.  
  38. AddSubDir(startingDir)
  39.  
  40. return files
  41. end
  42.  
  43. --------------------------------------------------------------------------------
  44. --------------------------------------------------------------------------------
  45. --
  46. -- Run a pre-processing script if one exists
  47. --
  48.  
  49. if (VFS.FileExists(preProcFile)) then
  50. Shared = shared -- make it global
  51. UnitDefs = unitDefs -- make it global
  52. VFS.Include(preProcFile)
  53. UnitDefs = nil
  54. Shared = nil
  55. end
  56.  
  57.  
  58. --------------------------------------------------------------------------------
  59. --------------------------------------------------------------------------------
  60. --
  61. -- Load the FBI/SWU unitdef files
  62. --
  63.  
  64. local fbiFiles = {}
  65.  
  66.  
  67. local tdfFiles = FileSearch('features/', '*.tdf')
  68.  
  69.  
  70. for _, filename in ipairs(fbiFiles) do
  71. local ud, err = FBI.Parse(filename)
  72. if (ud == nil) then
  73. Spring.Echo('Error parsing ' .. filename .. ': ' .. err)
  74. elseif (ud.unitname == nil) then
  75. Spring.Echo('Missing unitName in ' .. filename)
  76. else
  77. ud.filename = filename
  78. ud.unitname = string.lower(ud.unitname)
  79. unitDefs[ud.unitname] = ud
  80. end
  81. end
  82.  
  83.  
  84. --------------------------------------------------------------------------------
  85. --------------------------------------------------------------------------------
  86. --
  87. -- Load the raw LUA format unitdef files
  88. -- (these will override the FBI/SWU versions)
  89. --
  90.  
  91.  
  92. local luaFiles = FileSearch('units/', '*.lua')
  93.  
  94. for _, filename in ipairs(luaFiles) do
  95. local udEnv = {}
  96. udEnv._G = udEnv
  97. udEnv.Shared = shared
  98. udEnv.GetFilename = function() return filename end
  99. setmetatable(udEnv, { __index = system })
  100. local success, uds = pcall(VFS.Include, filename, udEnv)
  101. if (not success) then
  102. Spring.Echo('Error parsing ' .. filename .. ': ' .. uds)
  103. elseif (type(uds) ~= 'table') then
  104. Spring.Echo('Bad return table from: ' .. filename)
  105. else
  106. for udName, ud in pairs(uds) do
  107. if ((type(udName) == 'string') and (type(ud) == 'table')) then
  108. ud.filename = filename
  109. unitDefs[udName] = ud
  110. else
  111. Spring.Echo('Bad return table entry from: ' .. filename)
  112. end
  113. end
  114. end
  115. end
  116.  
  117.  
  118. --------------------------------------------------------------------------------
  119. --------------------------------------------------------------------------------
  120. --
  121. -- Insert the download build entries
  122. --
  123.  
  124. DownloadBuilds.Execute(unitDefs)
  125.  
  126.  
  127. --------------------------------------------------------------------------------
  128. --------------------------------------------------------------------------------
  129. --
  130. -- Run a post-processing script if one exists
  131. --
  132.  
  133. if (VFS.FileExists(postProcFile)) then
  134. Shared = shared -- make it global
  135. UnitDefs = unitDefs -- make it global
  136. VFS.Include(postProcFile)
  137. UnitDefs = nil
  138. Shared = nil
  139. end
  140.  
  141.  
  142. --------------------------------------------------------------------------------
  143. --------------------------------------------------------------------------------
  144. --
  145. -- Basic checks to kill unitDefs that will crash ".give all"
  146. --
  147.  
  148. for name, def in pairs(unitDefs) do
  149. local cob = 'scripts/' .. name .. '.cob'
  150.  
  151. local obj = def.objectName or def.objectname
  152. if (obj == nil) then
  153. unitDefs[name] = nil
  154. Spring.Echo('WARNING: removed ' .. name ..
  155. ' unitDef, missing objectname param')
  156. for k,v in pairs(def) do print('',k,v) end
  157. else
  158. local objfile = 'objects3d/' .. obj
  159. if ((not VFS.FileExists(objfile)) and
  160. (not VFS.FileExists(objfile .. '.3do')) and
  161. (not VFS.FileExists(objfile .. '.s3o'))) then
  162. unitDefs[name] = nil
  163. Spring.Echo('WARNING: removed ' .. name
  164. .. ' unitDef, missing model file (' .. obj .. ')')
  165. end
  166. end
  167. end
  168.  
  169.  
  170. for name, def in pairs(unitDefs) do
  171. local badOptions = {}
  172. local buildOptions = def.buildOptions or def.buildoptions
  173. if (buildOptions) then
  174. for i, option in ipairs(buildOptions) do
  175. if (unitDefs[option] == nil) then
  176. table.insert(badOptions, i)
  177. Spring.Echo('WARNING: removed the "' .. option ..'" entry'
  178. .. ' from the "' .. name .. '" build menu')
  179. end
  180. end
  181. if (#badOptions > 0) then
  182. local removed = 0
  183. for _, badIndex in ipairs(badOptions) do
  184. table.remove(buildOptions, badIndex - removed)
  185. removed = removed + 1
  186. end
  187. end
  188. end
  189. end
  190.  
  191.  
  192. --------------------------------------------------------------------------------
  193. --------------------------------------------------------------------------------
  194.  
  195. return unitDefs
  196.  
  197. --------------------------------------------------------------------------------
  198. --------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement