Add file checks to digfort

Checking file type and existence before we start avoids a few issues,
and prevents (highly) misleading feedback of "done" regardless of action
taken.  Closes #453
develop
PeridexisErrant 2014-12-18 09:50:00 +11:00
parent 8e6fcac92e
commit dba7d1bd5d
2 changed files with 48 additions and 32 deletions

@ -1,6 +1,7 @@
DFHack Future DFHack Future
Internals Internals
Fixes Fixes
digfort.rb: check file type and existence before starting
New Plugins New Plugins
New Scripts New Scripts
Misc Improvements Misc Improvements

@ -1,11 +1,26 @@
# designate an area for digging according to a plan in csv format # designate an area for digging according to a plan in csv format
raise "usage: digfort <plan filename>" if not $script_args[0] fname = $script_args[0].to_s
planfile = File.read($script_args[0]) print(fname)
if not $script_args[0] then
puts " Usage: digfort <plan filename>"
throw :script_finished
end
if not fname[-4..-1] == ".csv" then
puts " The plan file must be in .csv format."
throw :script_finished
end
if not File.file?(fname) then
puts " The specified file does not exist."
throw :script_finished
end
planfile = File.read(fname)
if df.cursor.x == -30000 if df.cursor.x == -30000
puts "place the game cursor to the top-left corner of the design" puts "place the game cursor to the top-left corner of the design"
throw :script_finished throw :script_finished
end end
# a sample CSV file # a sample CSV file
@ -30,17 +45,17 @@ EOS
offset = [0, 0] offset = [0, 0]
tiles = [] tiles = []
planfile.each_line { |l| planfile.each_line { |l|
if l =~ /#.*start\s*\(\s*(-?\d+)\s*[,;]\s*(-?\d+)/ if l =~ /#.*start\s*\(\s*(-?\d+)\s*[,;]\s*(-?\d+)/
raise "Error: multiple start() comments" if offset != [0, 0] raise "Error: multiple start() comments" if offset != [0, 0]
offset = [$1.to_i, $2.to_i] offset = [$1.to_i, $2.to_i]
end end
l = l.chomp.sub(/#.*/, '') l = l.chomp.sub(/#.*/, '')
next if l == '' next if l == ''
tiles << l.split(/[;,]/).map { |t| tiles << l.split(/[;,]/).map { |t|
t = t.strip t = t.strip
(t[0] == ?") ? t[1..-2] : t (t[0] == ?") ? t[1..-2] : t
} }
} }
x = x0 = df.cursor.x - offset[0] x = x0 = df.cursor.x - offset[0]
@ -48,23 +63,23 @@ y = df.cursor.y - offset[1]
z = df.cursor.z z = df.cursor.z
tiles.each { |line| tiles.each { |line|
next if line.empty? or line == [''] next if line.empty? or line == ['']
line.each { |tile| line.each { |tile|
t = df.map_tile_at(x, y, z) t = df.map_tile_at(x, y, z)
s = t.shape_basic s = t.shape_basic
case tile case tile
when 'd'; t.dig(:Default) if s == :Wall when 'd'; t.dig(:Default) if s == :Wall
when 'u'; t.dig(:UpStair) if s == :Wall when 'u'; t.dig(:UpStair) if s == :Wall
when 'j'; t.dig(:DownStair) if s == :Wall or s == :Floor when 'j'; t.dig(:DownStair) if s == :Wall or s == :Floor
when 'i'; t.dig(:UpDownStair) if s == :Wall when 'i'; t.dig(:UpDownStair) if s == :Wall
when 'h'; t.dig(:Channel) if s == :Wall or s == :Floor when 'h'; t.dig(:Channel) if s == :Wall or s == :Floor
when 'r'; t.dig(:Ramp) if s == :Wall when 'r'; t.dig(:Ramp) if s == :Wall
when 'x'; t.dig(:No) when 'x'; t.dig(:No)
end end
x += 1 x += 1
} }
x = x0 x = x0
y += 1 y += 1
} }
puts 'done' puts ' done'