Class: Six::Arma::Tools::Pbo

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/six/arma/tools/pbodll.rb

Constant Summary collapse

FORMATS =

BINARIZE, AUTOLINT, ATTACHTEXT, NONE = ‘B’, ‘L’, ‘T’, ‘N’ ARMA, CWC, ELITE, RESISTANCE = ‘A’, ‘C’, ‘E’, ‘R’

{:arma => 'A', :cwc => 'C', :elite => 'E', :resistance => 'R'}
RAPIFY =
{:binarize => 'B', :autolint => 'L', :attachtext => 'T', :none => 'N'}
DEFAULT_UNPACK =
{:extract_to_prefix => true, :derapify => true, :prompt_overwrite => true, :silent => true, :list => false}
DEFAULT_PACK =
{:format => :arma, :rapify => :none, :autoprefix => true}
DEFAULT_RAPIFY =
{:file => nil, :format => :arma, :rapify => :arma, :autoprefix => true}
DEFAULT_BINARIZE =
{:autoprefix => true}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, destination = source, options = {}) ⇒ Pbo

Returns a new instance of Pbo.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/six/arma/tools/pbodll.rb', line 17

def initialize(source, destination = source, options = {})
  source.gsub!("\\", "/")
  destination.gsub!("\\", "/")
  @source = source
  @options = options
  source[/(.*)\//]
  if destination == $1
    @same = true
  end
  @destination = destination
end

Instance Attribute Details

#destinationObject

allows instance.attribute to be read and written (e.g convert.destination)



15
16
17
# File 'lib/six/arma/tools/pbodll.rb', line 15

def destination
  @destination
end

#sourceObject (readonly)

allows instance.attribute to be read (e.g convert.source)



14
15
16
# File 'lib/six/arma/tools/pbodll.rb', line 14

def source
  @source
end

Instance Method Details

#auto_prefixObject

TODO: Calculate prefix from script_component’s include? define PREFIX prefix



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/six/arma/tools/pbodll.rb', line 52

def auto_prefix
  @source[/(.*)\/(.*)/]
  folder, filename = $1, $2
  tag = ''
  log.info "AutoPrefix: #{@source}"
  if FileTest.exist?(File.join(folder, filename, '$PBOPREFIX$'))
    # TODO: .lines might be bugged in 1.8.6 windows
    File.open(File.join(folder, filename, '$PBOPREFIX$'), 'r') { |file| file.lines.each { |line| tag = $1 if line[/x\\(\w*)\\/] } }
  end
  if tag.nil? || tag.empty?
    File.dirname(@source)[/^.*\/x\/(\w*)\//]
    tag = $1
  end
  unless tag.nil? || tag.empty?
    tag = "#{tag}_"
    mv(File.join(@destination, "#{filename}.pbo"), File.join(@destination,"#{tag}#{filename}.pbo"))
  end
  if FileTest.exist?(File.join(@destination, "#{filename}.log"))
    dest = File.join(@destination, "#{tag}#{filename}.log")
    # TODO: Move the log regardless of autoprefix!
    dest = File.join(settings[:log], "#{tag}#{filename}.log") if settings[:log]
    lf = File.join(@destination, "#{filename}.log")
    mv(lf, dest) unless lf == dest
  end
  "#{tag}#{filename}.pbo"
end

#binarize(params = {}) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/six/arma/tools/pbodll.rb', line 191

def binarize(params = {})
  # Rapify with Mikero tools
  # Rename config.cpp to config.hpp
  # Binarize with BinPBO - include file must include *.bin
  # Pack with BinPBO
  # Rename config.hpp to config.cpp
  params = DEFAULT_BINARIZE.merge params
  bla = false
  log.info "Binarizing: #{@source}"
  if FileTest.exist?(File.join(@source, 'config.cpp'))
    bla = true
    rapify(:file => 'config.cpp') # TODO: Rapify All - MakePBO does :O
    mv(File.join(@source, 'config.cpp'), File.join(@source, 'config.hpp'))
  end
  binpbo
  if bla
    mv(File.join(@source, 'config.hpp'), File.join(@source, 'config.cpp'))
    rm_f(File.join(@source, 'config.bin'))
  end
  if params[:autoprefix]
    auto_prefix
  else
    "#{@source}.pbo"[/(.*)\/(.*)/]
    $2
  end
end

#binpboObject

TODO: Define PROJECT elsewhere? (Config?)



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/six/arma/tools/pbodll.rb', line 166

def binpbo
  params = "-BINARIZE -CLEAR -DEBUG"
  params += " -TEMP \"#{settings[:temp]}\"" if settings[:temp]
  params += " -INCLUDE \"#{settings[:include]}\"" if settings[:include]

  @source[/(.:[\/][\w0-9@]*)[\/]./]
  proj = settings[:project] ? settings[:project] : $1

  unless proj.nil?
    if (Dir[File.join(@source, "**/*.p3d")] + Dir[File.join(@source, "**/*.wrp")]).size > 0 # TODO: Configurable?
      params += " -PROJECT \"#{proj}\""
    end
  end

  prefix = check_prefix(@source, '__AUTO__')
  params += " -PREFIX #{prefix}" unless prefix.empty?
  log.info "BinPBO: #{@source}"
  exec = "\"#{tool('binpbo.exe')}\" \"#{@source}\" \"#{@destination}\" #{params}".gsub('/', '\\')
  log.info exec
  output = %x[#{exec}]
  log.debug output
  output
end

#check_prefix(source, prefix) ⇒ Object

Checks prefix, returns read from file prefix, empty prefix, or given prefix

source

Source folder to look for PREFIX_FILE file

prefix

String: Empty, ‘__AUTO__’ or specific prefix can be given



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/six/arma/tools/pbodll.rb', line 37

def check_prefix(source, prefix)
  return prefix unless prefix == '__AUTO__'
  if FileTest.exist?("#{source}/#{settings[:prefix]}")
    pf = File.open("#{source}/#{settings[:prefix]}") { |file| file.readlines.map { |line| line.chomp } }
    if pf[0].empty?
      return ''
    else
      return pf[0]
    end
  else
    return ''
  end
end

#configObject



30
# File 'lib/six/arma/tools/pbodll.rb', line 30

def config; Six::Arma::Tools::config; end

#logObject



29
# File 'lib/six/arma/tools/pbodll.rb', line 29

def log; Six::Arma::Tools::log; end

#pack(params = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/six/arma/tools/pbodll.rb', line 120

def pack(params = {})
  params = DEFAULT_PACK.merge params
  cli_params = [FORMATS[params[:format]], RAPIFY[params[:rapify]]]
  cmd = "#{tool('makepbo.exe')} -#{cli_params.join('')} \"#{@source}\""

  # TODO: Handle exceptions, maybe with FileTest
  log.info "Packing: #{@source}"
  exec = cmd
  log.info exec
  output = %x[#{exec}]
  log.debug output
  unless @same || @destination == File.dirname(@source)
    # TODO: Better to have makepbo accept a destination.. Otherwise it overwrites/removes addons already present in source!
    puts "#{@source} #{@destination}"
    FileUtils::mv("#{@source}.pbo", @destination) if @destination
  end
  if params[:autoprefix]
    auto_prefix
  else
    "#{source}.pbo"[/(.*)\/(.*)/]
    $2
  end
end

#pathsObject



32
# File 'lib/six/arma/tools/pbodll.rb', line 32

def paths; config[:paths]; end

#rapify(params = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/six/arma/tools/pbodll.rb', line 145

def rapify(params = {})
  params = DEFAULT_RAPIFY.merge params
  cli_params = [FORMATS[params[:format]], RAPIFY[params[:rapify]]]
  curdir = Dir.pwd
  Dir.chdir(@source)
  # TODO: Handle exceptions, maybe with FileTest
  if params[:file]
    log.info "Rapifying: #{@source}"
    exec = "#{tool('rapify.exe')} -#{cli_params.join('')} \"#{params[:file]}\""
    log.info exec
    output = %x[#{exec}]
    log.debug output
    mv("#{params[:file]}.bin", "#{params[:file][/\w*\./]}bin") if FileTest.exist?("#{params[:file]}.bin")
  else
    # TODO: Handle 'Rapify all in @source and sub'
  end
  Dir.chdir(curdir)
  output
end

#settingsObject



31
# File 'lib/six/arma/tools/pbodll.rb', line 31

def settings; config[:settings]; end

#sign(params = {}) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/six/arma/tools/pbodll.rb', line 218

def sign(params = {})
  # TODO: Prefix calculation.... use auto_prefix?
  params[:key] = @options[:key] unless params[:key]
  params[:key] = settings[:key] unless params[:key]
  if params[:key]
    unless File.exists?(params[:key])
      Dir.chdir(File.dirname(params[:key])) { system "#{tool('dscreatekey.exe')} #{File.basename(params[:key].sub(".biprivatekey", ""))}" }
    end
    params[:key].gsub!('/', '\\')
    @source[/(.*)\/(.*)/]
    path = $1
    file = $2
    file += ".pbo" unless file[/\.pbo\Z/]
    # TODO: Export path to config
    log.info "Signing"
    Dir.chdir(path) { system "#{tool('dssignfile.exe')} #{params[:key]} #{file}" }
  end
end

#tool(program) ⇒ Object

If path defined in the config, use it, if not defined, expect program to be in path



80
81
82
83
84
85
86
87
# File 'lib/six/arma/tools/pbodll.rb', line 80

def tool(program)
  program[/(.*)\./]
  if paths[$1.to_sym]
    File.join(paths[$1.to_sym], program)
  else
    program
  end
end

#unpack(params = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/six/arma/tools/pbodll.rb', line 90

def unpack(params = {})
  params = DEFAULT_UNPACK.merge params
  cmd = []
  cmd << 'k' unless params[:extract_to_prefix]
  cmd << 'n' unless params[:silent]
  cmd << 'r' unless params[:derapify]
  #cmd << 'a' if params[:extract_to_prefix]
  cmd << 'l' if params[:list]
  cmd = [" -#{cmd.join('')}"] if cmd.size > 0
  cmd << " -f \"#{params[:files_to_extract]}\"" if params[:files_to_extract]
  cmd << " -w #{params[:output]}" if params[:output]
  if @source[/\.pbo/]
    @source[/(\w*)\.pbo/]
    destination = @destination

    log.info "Unpacking: #{@source} to #{destination}"
    output = %x[#{tool('extractpbo.exe')}#{cmd.join('')} "#{@source}" "#{destination}"]
  else
    pbos = Dir.glob("#{source}/*.pbo")
    pbos.each do |pbo|
      puts pbo
      obj = Pbo.new(pbo, @destination)
      obj.unpack
    end
  end
  log.debug output
  output
end