Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- StringNameList = {}
- StringNameUsage = {}
- $actionables = 0
- $maxlen = 0
- def print_error(error, path = nil, line_index = nil, line_text = nil)
- $actionables += 1
- puts "\e[31mError: #{error}\e[0m"
- puts "at #{File.basename(path)}:#{line_index}" if path
- puts line_text if line_text
- puts ""
- end
- def extract_string(line, from = 0)
- quote = line.index('"', from) + 1
- line[quote...line.index('"', quote)]
- end
- def extract_stringname(path, line, i)
- name = ""
- if line.include?("=")
- name = line[0...line.index("=")]
- else
- name = line[0...line.index("(")]
- end
- string = extract_string(line)
- if StringNameList.include? string
- print_error("Duplicate StringName: #{string}", path, i)
- else
- StringNameList[name.strip] = string
- $maxlen = [$maxlen, string.length].max
- end
- end
- def scan_stringname_source(path)
- File.readlines(path).each.with_index do |line, i|
- if line.include?("StaticCString::create")
- extract_stringname(path, line, i)
- elsif line.include?('"') and not line.start_with?("#") and not line.start_with?("/*")
- print_error("StringName created in unusual way", path, i, line)
- extract_stringname(path, line, i)
- end
- end
- end
- def check_string_name(line, prefix)
- return if not line.include?(prefix)
- start = line.index(prefix) + prefix.length
- string = line[start...line.index(")", start)]
- StringNameUsage[string] += 1
- end
- def scan_source_file(path)
- File.readlines(path).each.with_index do |line, i|
- next if line.start_with?("#") or line.start_with?("/*")
- if line.include?("StringNames::get_singleton()->")
- print_error("Legacy StringName usage", path, i, line)
- end
- check_string_name(line, "CoreStringName(")
- check_string_name(line, "SceneStringName(")
- check_string_name(line, "EditorStringName(")
- quote = line.index('"')
- while quote
- string = extract_string(line, quote)
- if string.length < 2 or string.length > $maxlen or string.include?(" ") or string.include?("D_METHOD") or string.include?("PropertyInfo")
- quote = line.index('"', quote + string.length + 2)
- next
- end
- key = StringNameList.key(string)
- if key
- StringNameUsage[key] += 1
- print_error("Potential replacement: #{string}", path, i, line)
- break
- end
- quote = line.index('"', quote + string.length + 2)
- end
- end
- end
- def scan_directory(path)
- path.delete_prefix!("./")
- (Dir.entries(path) - [".", ".."]).each do |entry|
- next if entry.start_with?(".")
- entry_path = File.join(path, entry)
- if File.exist?(entry_path) and File.extname(entry) == ".cpp" and not entry.include?(".gen")
- scan_source_file(entry_path)
- next
- end
- if Dir.exist?(entry_path)
- scan_directory(entry_path)
- end
- end
- end
- scan_stringname_source("core/core_string_names.cpp")
- scan_stringname_source("scene/scene_string_names.cpp")
- scan_stringname_source("editor/editor_string_names.cpp")
- StringNameList.each_key do |string|
- StringNameUsage[string] = 0
- end
- scan_directory(".")
- StringNameUsage.each_pair do |string, usage|
- if usage == 0
- print_error("Unused StringName: #{string}")
- elsif usage == 1
- print_error("StringName used only once: #{string}")
- end
- end
- puts "Finished"
- puts "Found #{$actionables} actionable items." if $actionables > 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement