Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Comment Code
- Author : Alan Heath
- Program Vers : 0.9a
- Program Name : Terrain Merger for Railworks Routes
- essential Information
- xml file Header = 270 chars long ish. negative values increase file size.
- xml File Length = 139594
- xml File End = 61
- Xml File Lines = 2058
- Xml File data end = 139533
- Xml File Data length = 139263 - 1 for end char (139262)
- Xml Data block size = 16
- xml file spacer = [space] - 20 [hex code 20]
- xml file block size = 8192
- xml data count = 8192
- function overview.
- fwrite - write the information to file, with any unpackable data from tables afterwards
- Defined by a %s in the "" marks eg. : fwrite("test %s", table[10])
- sleep - Put the Process in a pause state for x seconds.
- --]]
- --Initialize
- io.open("ser_xml.txt","w") io.close() -- refresh the ser_xml file
- io.open("ser_bin.txt","w") io.close() -- refresh the ser_bin file
- -- Load saved varibles file.
- local rwexist = false
- -- Setup our files local varibles.
- local datb = {} -- our source database
- local datc = {} -- our donor database
- local datd = {} -- our output database
- local data = "" -- define our data
- local terrfile = "" -- define the terrain file
- local match = false -- we default to no matches
- local qplist = {} -- Our Directory database
- local posn = 0 -- used in posn function
- local clock = os.clock
- local perrs = ""
- local prerrs = ""
- local processed = {} --processed filenames
- local plist = {}
- -- Primary functions
- function perrep(f) -- Save errors to the errlog and print to the console.
- perrs = io.open("errorlog.log", "a")
- prerrs = os.date(" %H:%M:%S : ") .. f
- print(prerrs)
- perrs:write(prerrs, "\n")
- perrs:close()
- end
- function setup()
- io.popen([[mkdir .\source\ ]])
- io.popen([[mkdir .\donor\ ]])
- io.popen([[mkdir .\output\ ]])
- end
- function init()
- local loadup = io.open("settings.dat", "r")
- if (loadup == nil) then
- loadup = io.open("settings.dat", "w")
- loadup:write(1)
- loadup:close()
- print("Firstrun Complete")
- os.exit()
- end
- p = io.popen([[dir "..\." /b /aa]]) -- gather directory listing for railworks folder.
- for file in p:lines() do -- cycle through Dir list
- if file == "RailWorks.exe" or file == "serz.exe" then
- rwexist=true
- break
- end -- If we find RW.exe then we found it.
- end
- if not rwexist then -- Failed to find Railworks.exe
- perrep("Railworks.exe not located.")
- perrep("Assuming no use of Serz.exe to un-compress binary files.")
- perrep("This process will have to be performed manually.")
- end
- -- "RailWorks.exe"
- loadup:close() -- Close saved varibles
- end
- -- Begin Functions list
- function listfile(a) -- list files in directory according to switch "a"
- if a == "source" then
- -- Due to the command we have to define the directory.
- return io.popen([[dir ".\source\" /b /aa]])
- elseif a == "donor" then
- return io.popen([[dir ".\donor\" /b /aa]])
- else
- p = ""
- return p
- end
- end
- function listdir(...)
- p = io.popen([[dir ".\" /b /ad]]) -- /ad list directory only
- return p
- end
- function sourceopen(filename) -- open our source file read only
- filename = ".\\source\\" .. filename
- return io.open(filename, "r")
- end
- function donoropen(filename) -- Open our donor file read only
- filename = ".\\donor\\" .. filename
- return io.open(filename, "r")
- end
- function outputopen(filename) -- Open our Output file read write
- filename = ".\\output\\" .. filename
- return io.open(filename, "w")
- end
- function fwrite (fmt, ...)
- return io.write(string.format(fmt, unpack(arg)))
- end
- function findpos (s)
- posn = string.find(s, '<d:blob d:size="65536">') +23 -- length of d:blob ...
- return posn
- end
- --[[ old function for testing purposes
- function paused()
- -- ask on continue.
- local answer
- repeat
- io.write("continue with this operation (y/n)? ")
- io.flush()
- answer=io.read()
- until answer=="y" or answer=="n"
- return answer
- end
- --]]
- function sleep( _nTime )
- local t0 = clock()
- while clock() - t0 <= _nTime do end
- end
- -- Uncompress bin files with serz
- function serz(filen, loc)
- filecall = " ..\\serz.exe " .. ".\\"..loc.."\\" .. filen .. " /xml:.\\"..loc.."\\" .. string.sub(filen,1,15) .. "xml >>ser_xml.txt "
- --print(filecall)
- errs = io.popen(filecall)
- --print(errs)
- sleep(0.25)
- end
- -- compress files to bin files with serz
- function binserz(filen)
- filecall = " ..\\serz.exe " .. ".\\output\\" .. filen .. " /bin:.\\output\\" .. string.sub(filen,1,15) .. "bin >>ser_bin.txt "
- errs = io.popen(filecall)
- sleep(0.25)
- end
- function extcheck(f, loc)
- safe = true
- extension = string.sub(f, 16, 18)
- if (extension == "bin" and rwexist) then
- serz(f, loc)
- f = string.sub(f,1,15).."xml"
- elseif (extension == "bin" and not rwexist) then
- perrep("bin file found and Railworks.exe not present. Error #1")
- -- error(" Terminated due to Bin file presence in folder and Railworks.exe was not located. for more information open errorlog.log ")
- safe = false
- end
- --print(extension)
- --print ("new "..f)
- return f, safe
- end
- function repcheck (f, lst)
- for _,i in ipairs(lst) do
- --print("rep"..f)
- if (i == string.sub(f,1,15).."xml") then return true end
- --print("i = "..i,"f = "..f)
- --print("sub f = "..string.sub(f,1,15))
- end
- return false
- end
- function checkdone(c,f,db)
- --print(c,f,db, #db)
- if #db <= 1 then return false end
- if db == nil then return false end -- catch a 0 value database
- for _,i in ipairs(db) do
- --print(" print i : "..i)
- if f == i then return true end
- end
- return false
- end
- -- End functions list
- -- Our Main program.
- function main()
- ---[[ temporary comment out the main code
- -- This was done to test functions and other testing features.
- -- terrfile:seek(set, 270) -- Skip the file header. depreciated. AH-09.02.15
- qplist = listfile("source") -- //// call to SERZ is causing data loss in plist. AH 22:52 09.02.15
- local countfile = 1
- -- print(plist)
- local qpcount = 1
- print("Files found ...")
- for file in qplist:lines() do
- plist[qpcount] = file
- print(plist[qpcount])
- qpcount = qpcount + 1
- end
- -- for file in plist:lines() do
- for _, file in ipairs(plist) do
- check = repcheck(file, plist)
- if not check then
- filer, v = extcheck(file, "source") -- Get info on file extension. name.xml , true / false
- else filer = string.sub(file, 1, 15).."xml"
- end
- print("file : "..file, "File xml : "..filer)
- -- is the file listed already?
- print(check)
- --if check == true then v = false end
- --print(countfile, file, processed)
- --q = checkdone(countfile, file, processed)
- --print(q)
- --if q == false then v = true end -- catch pre-processed files
- --print(check)
- --if v then -- if true then we have a viable filename.
- --end
- --print("countfile : "..countfile)
- processed[countfile] = filer
- --print("processed : "..processed[countfile])
- --print("filename : "..file)
- countfile = countfile + 1
- terrfile = sourceopen(filer)
- data = terrfile:read("*a")
- terrfile:close()
- -- This fails under different header sizes. 139263 should be added to the header length.
- -- data = string.sub(data,264, 139527) -- fails on negative values
- -- Lets set the position in the file to the end of the <d:blob d:size="65536">
- startpos = findpos(data)
- --print(startpos)
- --terrfile:seek(set, startpos)
- filehead = string.sub(data, 0, startpos -1)
- filefoot = string.sub(data, startpos + 139263)
- data = string.sub(data,startpos, (startpos + 139262)) -- trim to our data only
- local a = 1
- datb[0] = filer
- for i in string.gmatch(data, "%S+") do
- datb[a] = i
- --datb[a] = string.sub(datb[a], 0, 16) -- Trim file to 16 bits // obsolete trim code
- a = a+1
- end -- end for i in
- a = a - 1
- local b = 1
- data = {}
- filer = string.sub(filer, 1, 15).."bin"
- filer, _ = extcheck(filer, "donor")
- terrfile = donoropen(filer)
- --print(data)
- data = terrfile:read("*a")
- terrfile:close()
- startpos = findpos(data)
- data = string.sub(data,startpos, (startpos + 139262)) -- trim to our data only
- datc[0] = file
- for i in string.gmatch(data, "%S+") do
- datc[b] = i
- b = b + 1
- end -- end for i in
- b = b - 1
- for i = 1, #datb do
- for o = 1, 16, 2 do
- if string.sub(datb[i], o, o+2) == "00" then
- datd[i] = datc[i]
- else
- datd[i] = datb[i]
- end
- end
- end -- end output generation
- zxa = 1
- outdata = filehead
- --print(filehead)
- for i in ipairs(datd) do
- outdata = outdata .. datd[zxa] .. " "
- qxa = math.floor(zxa/4)
- qxb = zxa / 4
- --print(" number : ",qxa, qxb)
- if qxa == qxb then outdata = outdata .. "\n" end -- every 4 newline.
- zxa = zxa + 1
- end
- outdata = outdata .. filefoot
- output = ".\\output\\" .. datb[0]
- output = io.open(output, "w")
- output:write(outdata)
- output:close()
- outfile = datb[0]
- binserz(outfile)
- --end -- end if v
- end -- end for file in plist
- --[[ file looping must be inside the if V loop.
- print(" Countfile : " .. countfile )
- --processing code here
- print(" datb 0 : " .. datb[0] )
- for i = 1, (8192*(countfile-1)) do -- 8192 entries. in datb table.
- print(datb[i])
- -- Make check code here and replace the 0's
- end
- --]]
- --]]
- --print("counter : ".. countfile)
- end -- end main ()
- -- End of the main program call.
- -- End of startup and setup
- init() -- Initialize our program
- main() -- Call the program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement