mirror of
https://github.com/chocolate-doom/research.git
synced 2024-11-10 07:11:34 +00:00
Split string detection routines into common code.
Subversion-branch: /research Subversion-revision: 1888
This commit is contained in:
parent
799ac01fb8
commit
a81d4cff46
5 changed files with 94 additions and 74 deletions
|
@ -77,15 +77,96 @@ THING_FLAGS2 = %w{
|
|||
MF2_DONTDRAW
|
||||
}
|
||||
|
||||
def set_config(name)
|
||||
# HHE seems to stop when it has found this many strings:
|
||||
|
||||
NUM_STRINGS=785
|
||||
|
||||
def find_config(name)
|
||||
configs = [ Heretic_1_0, Heretic_1_2, Heretic_1_3 ]
|
||||
|
||||
for config in configs
|
||||
if name == config::NAME
|
||||
include config
|
||||
return
|
||||
return config
|
||||
end
|
||||
end
|
||||
|
||||
raise "Unknown configuration: #{name}"
|
||||
end
|
||||
|
||||
def set_config(name)
|
||||
config = find_config(name)
|
||||
include config
|
||||
end
|
||||
|
||||
def read_block(file)
|
||||
result = ""
|
||||
|
||||
4.times do
|
||||
break if file.eof?
|
||||
c = file.readchar
|
||||
result += sprintf("%c", c)
|
||||
end
|
||||
|
||||
end_index = result.index(0)
|
||||
if end_index
|
||||
result = result[0, end_index]
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
def bad_string?(str)
|
||||
str.each_byte do |b|
|
||||
if b >= 0x80
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
def find_strings(filename, config)
|
||||
|
||||
strings = []
|
||||
|
||||
File.open(filename) do |file|
|
||||
current_str = ""
|
||||
|
||||
file.seek(config::STRINGS_OFFSET)
|
||||
|
||||
offset = 0
|
||||
start_offset = nil
|
||||
|
||||
while strings.length < NUM_STRINGS
|
||||
block = read_block(file)
|
||||
|
||||
if start_offset == nil
|
||||
start_offset = offset
|
||||
end
|
||||
|
||||
current_str += block
|
||||
|
||||
# End of string?
|
||||
|
||||
if block.length < 4
|
||||
|
||||
# Extended-ASCII characters cannot be output
|
||||
# to JSON.
|
||||
|
||||
if bad_string?(current_str)
|
||||
current_str = nil
|
||||
end
|
||||
|
||||
strings.push([start_offset, current_str])
|
||||
|
||||
current_str = ""
|
||||
start_offset = nil
|
||||
end
|
||||
|
||||
offset += 4
|
||||
end
|
||||
end
|
||||
|
||||
strings
|
||||
end
|
||||
|
||||
|
|
|
@ -3,78 +3,17 @@
|
|||
require "json"
|
||||
require "common"
|
||||
|
||||
# HHE seems to stop when it has found this many strings:
|
||||
config = find_config(ARGV[1])
|
||||
strings = find_strings(ARGV[0], config)
|
||||
|
||||
NUM_STRINGS=785
|
||||
for string in strings
|
||||
|
||||
set_config(ARGV[1])
|
||||
string_text = string[1].to_json
|
||||
|
||||
def read_block(file)
|
||||
result = ""
|
||||
|
||||
4.times do
|
||||
break if file.eof?
|
||||
c = file.readchar
|
||||
result += sprintf("%c", c)
|
||||
end
|
||||
|
||||
end_index = result.index(0)
|
||||
if end_index
|
||||
result = result[0, end_index]
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
def bad_string?(str)
|
||||
str.each_byte do |b|
|
||||
if b >= 0x80
|
||||
return true
|
||||
end
|
||||
if string[1] == nil
|
||||
string_text = "NULL"
|
||||
end
|
||||
|
||||
return false
|
||||
printf " { %6i, %s },\n", string[0], string_text
|
||||
end
|
||||
|
||||
File.open(ARGV[0]) do |file|
|
||||
current_str = ""
|
||||
|
||||
file.seek(STRINGS_OFFSET)
|
||||
|
||||
offset = 0
|
||||
start_offset = nil
|
||||
num_strings = 0
|
||||
|
||||
while num_strings < NUM_STRINGS
|
||||
block = read_block(file)
|
||||
|
||||
if start_offset == nil
|
||||
start_offset = offset
|
||||
end
|
||||
|
||||
current_str += block
|
||||
|
||||
# End of string?
|
||||
|
||||
if block.length < 4
|
||||
|
||||
# Extended-ASCII characters cannot be output
|
||||
# to JSON.
|
||||
|
||||
if bad_string?(current_str)
|
||||
current_str = "XXXXX BAD STRING"
|
||||
end
|
||||
|
||||
printf " { %6i, %s },\n", start_offset,
|
||||
current_str.to_json
|
||||
current_str = ""
|
||||
start_offset = nil
|
||||
num_strings += 1
|
||||
end
|
||||
|
||||
offset += 4
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -551,7 +551,7 @@
|
|||
{ 9972, "P_RemoveActivePlat: can't find plat!" },
|
||||
{ 10012, "Too many mace spots." },
|
||||
{ 10036, "" },
|
||||
{ 10040, "XXXXX BAD STRING" },
|
||||
{ 10040, NULL },
|
||||
{ 10052, "P_GroupLines: miscounted" },
|
||||
{ 10080, "-timer" },
|
||||
{ 10088, "FLTWAWA1" },
|
||||
|
|
|
@ -581,7 +581,7 @@
|
|||
{ 11900, "P_RemoveActivePlat: can't find plat!" },
|
||||
{ 11940, "Too many mace spots." },
|
||||
{ 11964, "" },
|
||||
{ 11968, "XXXXX BAD STRING" },
|
||||
{ 11968, NULL },
|
||||
{ 11980, "P_GroupLines: miscounted" },
|
||||
{ 12008, "-timer" },
|
||||
{ 12016, "FLTWAWA1" },
|
||||
|
|
|
@ -581,7 +581,7 @@
|
|||
{ 11900, "P_RemoveActivePlat: can't find plat!" },
|
||||
{ 11940, "Too many mace spots." },
|
||||
{ 11964, "" },
|
||||
{ 11968, "XXXXX BAD STRING" },
|
||||
{ 11968, NULL },
|
||||
{ 11980, "P_GroupLines: miscounted" },
|
||||
{ 12008, "-timer" },
|
||||
{ 12016, "FLTWAWA1" },
|
||||
|
|
Loading…
Reference in a new issue