Advertisement
albrat

Railworks Tile Merger

Apr 22nd, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.56 KB | None | 0 0
  1. --[[  Comment Code
  2.  
  3. Author : Alan Heath
  4. Program Vers : 0.9a
  5. Program Name : Terrain Merger for Railworks Routes
  6.  
  7.  
  8. essential Information
  9.  
  10. xml file Header         = 270 chars long ish. negative values increase file size.
  11. xml File Length         = 139594
  12. xml File End            = 61
  13.  
  14. Xml File Lines          = 2058
  15. Xml File data end       = 139533
  16. Xml File Data length    = 139263 - 1 for end char  (139262)
  17. Xml Data block size     = 16
  18. xml file spacer         = [space] - 20 [hex code 20]
  19. xml file block size     = 8192
  20. xml data count          = 8192
  21.  
  22. function overview.
  23.  
  24. fwrite  - write the information to file, with any unpackable data from tables afterwards
  25.           Defined by a %s in the "" marks eg. : fwrite("test %s", table[10])
  26. sleep   - Put the Process in a pause state for x seconds.
  27.  
  28.  
  29.  
  30. --]]
  31. --Initialize
  32.  
  33. io.open("ser_xml.txt","w") io.close() -- refresh the ser_xml file
  34. io.open("ser_bin.txt","w") io.close() -- refresh the ser_bin file
  35.  
  36. -- Load saved varibles file.
  37. local rwexist = false
  38. -- Setup our files local varibles.
  39.  
  40. local datb = {}  -- our source database
  41. local datc = {}  -- our donor database
  42. local datd = {}  -- our output database
  43. local data = ""  -- define our data
  44. local terrfile = "" -- define the terrain file
  45. local match = false  -- we default to no matches
  46. local qplist = {}  -- Our Directory database
  47. local posn = 0 -- used in posn function
  48. local clock = os.clock
  49. local perrs = ""
  50. local prerrs = ""
  51. local processed = {} --processed filenames
  52. local plist = {}
  53.  
  54. -- Primary functions
  55.  
  56. function perrep(f) -- Save errors to the errlog and print to the console.
  57.     perrs = io.open("errorlog.log", "a")
  58.     prerrs = os.date(" %H:%M:%S : ") .. f
  59.     print(prerrs)
  60.     perrs:write(prerrs, "\n")
  61.     perrs:close()
  62. end
  63.  
  64. function setup()
  65.     io.popen([[mkdir .\source\ ]])
  66.     io.popen([[mkdir .\donor\ ]])
  67.     io.popen([[mkdir .\output\ ]])
  68. end
  69.  
  70. function init()
  71.    
  72.     local loadup = io.open("settings.dat", "r")
  73.     if (loadup == nil) then
  74.         loadup = io.open("settings.dat", "w")
  75.         loadup:write(1)
  76.         loadup:close()
  77.         print("Firstrun Complete")
  78.         os.exit()
  79.     end
  80.    
  81.     p = io.popen([[dir "..\." /b /aa]]) -- gather directory listing for railworks folder.
  82.    
  83.     for file in p:lines() do --  cycle through Dir list
  84.         if file == "RailWorks.exe" or file == "serz.exe" then
  85.             rwexist=true
  86.             break
  87.             end -- If we find RW.exe then we found it.
  88.     end
  89.  
  90. if not rwexist then -- Failed to find Railworks.exe
  91.     perrep("Railworks.exe not located.")
  92.     perrep("Assuming no use of Serz.exe to un-compress binary files.")
  93.     perrep("This process will have to be performed manually.")
  94. end
  95. -- "RailWorks.exe"
  96. loadup:close() -- Close saved varibles
  97. end
  98.  
  99. -- Begin Functions list
  100.  
  101. function listfile(a)  -- list files in directory according to switch "a"
  102.  
  103.     if a == "source" then
  104.            -- Due to the command we have to define the directory.
  105.         return io.popen([[dir ".\source\" /b /aa]])
  106.     elseif a == "donor" then
  107.          
  108.         return io.popen([[dir ".\donor\" /b /aa]])
  109.     else
  110.         p = ""
  111.         return p
  112.     end
  113. end
  114.  
  115. function listdir(...)
  116.  
  117.     p = io.popen([[dir ".\" /b /ad]]) -- /ad list directory only
  118.     return p
  119.  
  120. end
  121.  
  122.  
  123. function sourceopen(filename) -- open our source file read only
  124.     filename = ".\\source\\" .. filename
  125.     return io.open(filename, "r")
  126. end
  127.  
  128. function donoropen(filename) -- Open our donor file read only
  129.     filename = ".\\donor\\" .. filename
  130.     return io.open(filename, "r")
  131. end
  132.  
  133. function outputopen(filename) -- Open our Output file read write
  134.     filename = ".\\output\\" .. filename
  135.     return io.open(filename, "w")
  136. end
  137.  
  138. function fwrite (fmt, ...)
  139.     return io.write(string.format(fmt, unpack(arg)))
  140. end
  141.  
  142.  
  143.    function findpos (s)
  144.     posn = string.find(s, '<d:blob d:size="65536">') +23 -- length of d:blob ...
  145.     return posn
  146.    end
  147.  
  148. --[[ old function for testing purposes
  149.  
  150. function paused()
  151. -- ask on continue.
  152. local answer
  153. repeat
  154.   io.write("continue with this operation (y/n)? ")
  155.   io.flush()
  156.   answer=io.read()
  157. until answer=="y" or answer=="n"
  158. return answer
  159. end
  160.  
  161. --]]
  162.  
  163.  
  164. function sleep( _nTime )
  165.     local t0 = clock()
  166.     while clock() - t0 <= _nTime do end
  167. end
  168.  
  169.  
  170. -- Uncompress bin files with serz
  171. function serz(filen, loc)
  172.  
  173.     filecall = " ..\\serz.exe " .. ".\\"..loc.."\\" .. filen .. " /xml:.\\"..loc.."\\" .. string.sub(filen,1,15) .. "xml >>ser_xml.txt "
  174.     --print(filecall)
  175.    
  176.     errs = io.popen(filecall)
  177.     --print(errs)
  178.     sleep(0.25)
  179.    
  180. end
  181. -- compress files to bin files with serz
  182. function binserz(filen)
  183.  
  184.     filecall = " ..\\serz.exe " .. ".\\output\\" .. filen .. " /bin:.\\output\\" .. string.sub(filen,1,15) .. "bin >>ser_bin.txt "
  185.     errs = io.popen(filecall)
  186.     sleep(0.25)
  187.  
  188. end
  189.  
  190. function extcheck(f, loc)
  191.     safe = true
  192.     extension = string.sub(f, 16, 18)
  193.     if (extension == "bin" and rwexist) then
  194.         serz(f, loc)
  195.         f = string.sub(f,1,15).."xml"
  196.     elseif (extension == "bin" and not rwexist) then
  197.  
  198.     perrep("bin file found and Railworks.exe not present. Error #1")
  199.     -- error(" Terminated due to Bin file presence in folder and Railworks.exe was not located. for more information open errorlog.log ")
  200.     safe = false
  201.     end
  202.     --print(extension)
  203.     --print ("new "..f)
  204.     return f, safe
  205. end
  206.  
  207. function repcheck (f, lst)
  208.     for _,i in ipairs(lst) do
  209.    
  210.         --print("rep"..f)
  211.         if (i == string.sub(f,1,15).."xml") then return true end
  212.         --print("i = "..i,"f = "..f)
  213.         --print("sub f = "..string.sub(f,1,15))
  214.     end
  215.     return false
  216. end
  217.  
  218. function checkdone(c,f,db)
  219. --print(c,f,db, #db)
  220. if #db <= 1 then return false end
  221.     if db == nil then return false end -- catch a 0 value database
  222.     for _,i in ipairs(db) do
  223.         --print(" print i : "..i)
  224.         if f == i then return true end
  225.     end
  226.     return false
  227. end
  228.  
  229. -- End functions list
  230.  
  231.  
  232.  
  233. -- Our Main program.
  234.  
  235. function main()
  236. ---[[   temporary comment out the main code
  237. --      This was done to test functions and other testing features.
  238.    
  239. --  terrfile:seek(set, 270)  -- Skip the file header. depreciated. AH-09.02.15
  240.     qplist = listfile("source")  -- //// call to SERZ is causing data loss in plist. AH 22:52 09.02.15
  241.     local countfile = 1
  242. --  print(plist)
  243.     local qpcount = 1
  244.     print("Files found ...")
  245.     for file in qplist:lines() do
  246.         plist[qpcount] = file
  247.         print(plist[qpcount])
  248.         qpcount = qpcount + 1
  249.     end
  250. --  for file in plist:lines() do
  251.  
  252.     for _, file in ipairs(plist) do
  253.    
  254.         check = repcheck(file, plist)
  255.         if not check then
  256.             filer, v = extcheck(file, "source")  -- Get info on file extension.  name.xml , true / false
  257.         else filer = string.sub(file, 1, 15).."xml"
  258.         end
  259.         print("file : "..file, "File xml : "..filer)
  260.  -- is the file listed already?
  261.         print(check)
  262.         --if check == true then v = false end
  263.        
  264.         --print(countfile, file, processed)
  265.        
  266.         --q = checkdone(countfile, file, processed)
  267.        
  268.         --print(q)
  269.        
  270.         --if q == false then v = true end -- catch pre-processed files
  271.        
  272.         --print(check)
  273.         --if v then  -- if true then we have a viable filename.
  274.        
  275.         --end
  276.             --print("countfile : "..countfile)
  277.             processed[countfile] = filer
  278.             --print("processed : "..processed[countfile])
  279.             --print("filename : "..file)
  280.             countfile = countfile + 1
  281.            
  282.            
  283.             terrfile = sourceopen(filer)
  284.             data = terrfile:read("*a")
  285.             terrfile:close()
  286.             -- This fails under different header sizes.  139263 should be added to the header length.
  287.             -- data = string.sub(data,264, 139527)  -- fails on negative values
  288.            
  289.             -- Lets set the position in the file to the end of the <d:blob d:size="65536">
  290.             startpos = findpos(data)  
  291.             --print(startpos)
  292.             --terrfile:seek(set, startpos)
  293.            
  294.             filehead = string.sub(data, 0, startpos -1)
  295.             filefoot = string.sub(data, startpos + 139263)
  296.             data = string.sub(data,startpos, (startpos + 139262)) -- trim to our data only
  297.            
  298.            
  299.             local a = 1
  300.             datb[0] = filer
  301.             for i in string.gmatch(data, "%S+") do
  302.                 datb[a] = i
  303.                 --datb[a] = string.sub(datb[a], 0, 16)  -- Trim file to 16 bits // obsolete trim code
  304.                 a = a+1
  305.             end -- end for i in
  306.             a = a - 1
  307.             local b = 1
  308.             data = {}
  309.            
  310.             filer = string.sub(filer, 1, 15).."bin"
  311.             filer, _ = extcheck(filer, "donor")
  312.             terrfile = donoropen(filer)
  313.             --print(data)
  314.  
  315.             data = terrfile:read("*a")
  316.             terrfile:close()
  317.             startpos = findpos(data)
  318.             data = string.sub(data,startpos, (startpos + 139262)) -- trim to our data only
  319.             datc[0] = file
  320.            
  321.             for i in string.gmatch(data, "%S+") do
  322.                 datc[b] = i
  323.                 b = b + 1
  324.             end -- end for i in
  325.            
  326.             b = b - 1
  327.            
  328.             for i = 1, #datb do
  329.                 for o = 1, 16, 2 do
  330.                
  331.                 if string.sub(datb[i], o, o+2) == "00" then
  332.                     datd[i] = datc[i]
  333.                 else
  334.                     datd[i] = datb[i]
  335.                 end
  336.                
  337.                 end
  338.  
  339.             end  -- end output generation
  340.             zxa = 1
  341.             outdata = filehead
  342.             --print(filehead)
  343.            
  344.             for i in ipairs(datd) do
  345.                 outdata = outdata .. datd[zxa] .. " "
  346.                 qxa = math.floor(zxa/4)
  347.                 qxb = zxa / 4
  348.                 --print(" number : ",qxa, qxb)
  349.                 if qxa == qxb then outdata = outdata .. "\n" end -- every 4 newline.
  350.                 zxa = zxa + 1
  351.             end
  352.             outdata = outdata .. filefoot
  353.             output = ".\\output\\" .. datb[0]
  354.             output = io.open(output, "w")
  355.             output:write(outdata)
  356.             output:close()
  357.             outfile = datb[0]
  358.             binserz(outfile)
  359.            
  360.         --end -- end if v
  361.        
  362.     end  -- end for file in plist
  363. --[[    file looping must be inside the if V loop.
  364.  
  365.     print("  Countfile  : " .. countfile )
  366.     --processing code here
  367.     print(" datb 0 : " .. datb[0] )
  368.     for i = 1, (8192*(countfile-1)) do  -- 8192 entries. in datb table.
  369.         print(datb[i])
  370.         -- Make check code here and replace the 0's
  371.        
  372.        
  373.     end
  374. --]]
  375.     --]]
  376.     --print("counter : ".. countfile)
  377.  
  378. end  -- end main ()
  379.  
  380.  
  381.  
  382. -- End of the main program call.
  383.  
  384.  
  385.  
  386. -- End of startup and setup
  387.  
  388. init()  -- Initialize our program
  389. main()  -- Call the program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement