Skip to content

Instantly share code, notes, and snippets.

@KazKoboDev
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KazKoboDev/e6066bcec20c49f97ff8 to your computer and use it in GitHub Desktop.
Save KazKoboDev/e6066bcec20c49f97ff8 to your computer and use it in GitHub Desktop.
This script builds a new version of the NoProjectileBlockDamage mod for Starbound when provided with an unpacked projectiles directory in the same space as the script itself.
require 'fileutils'
require 'json'
template = '[
{
"op": "test",
"path": "/actionOnReap/LOC/file",
"value": "PLACEHOLDER"
},
{
"op": "remove",
"path": "/actionOnReap/LOC"
}
]'
puts "#{Time.now.strftime("%H:%M:%S")} : Scanning for files with block damage attributes"
configs = Array.new
Dir.glob("#{Dir.pwd}/projectiles/**/*.config").each do |config|
file = File.read(config)
if file.include? "explosiveDamageAmount"
configs.push(config.split("/").last.split(".").first)
end
end
regex_str = configs.join("|")
puts "#{Time.now.strftime("%H:%M:%S")} : Preparing Buildspace"
if File.exist?("#{Dir.pwd}/NPBD/projectiles")
FileUtils.rm_rf(Dir.glob("#{Dir.pwd}/NPBD/projectiles/*"))
else
FileUtils.mkdir_p("#{Dir.pwd}/NPBD/projectiles")
end
puts "#{Time.now.strftime("%H:%M:%S")} : Starting build"
Dir.glob("#{Dir.pwd}/projectiles/**/*.projectile").each do |projectile|
file = File.read(projectile)
next unless file.include? '"action" : "config"'
next unless file =~ /#{regex_str}/
FileUtils.mkdir_p(projectile.split("projectiles").last.insert(0,"#{Dir.pwd}/NPBD/projectiles").split("/").take(projectile.split("/").length).join("/"))
data = JSON.parse(file)
hash = data["actionOnReap"].find { |x| x['action'] == 'config' } if file.include? "actionOnReap"
hash = data["actionOnHit"].find { |x| x['action'] == 'config' } if file.include? "actionOnHit"
config = hash["file"]
patch = projectile.split("/").insert(3, "NPBD").join("/") + ".patch"
modified_template = template.sub("PLACEHOLDER", config)
# basic counter to locate hash location in OnReap/OnHit array
counter = 0
if file.include? "actionOnHit"
data["actionOnHit"].each do |line|
if line["action"] == "config"
modified_template.gsub!("LOC", counter.to_s)
end
counter += 1
end
else
data["actionOnReap"].each do |line|
if line["action"] == "config"
modified_template.gsub!("LOC", counter.to_s)
end
counter += 1
end
end
modified_template.gsub!("actionOnReap", "actionOnHit") if file.include? "actionOnHit"
File.open(patch, 'w+'){ |p|
p.write(modified_template)
}
file = nil # weird bugfix, doesn't iterate correctly otherwise
end
puts "#{Time.now.strftime("%H:%M:%S")} : Build finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment