-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank_parser.rb
executable file
·290 lines (217 loc) · 8.25 KB
/
bank_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
78
79
80
81
82
83
84
85
86
87
88
89
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
require 'rubygems'
gem "nokogiri", "~> 1.5.6"
gem "osc", "~> 0.1.4"
gem "listen", "~> 0.7.2"
require 'listen'
require 'nokogiri'
require 'osc'
include OSC
# global scope - OSC destination address/port
$serverHost
$serverPort
# global scope hash so when units/buildings complete we can indicate that to listeners by sending a 0 for the count
# e.g. when a Spawning Pool finishes, we will continue to send 'Spawning Pool', 0
$aggregateProductionHash = Hash.new()
# when true prints out all data parsed out of XML
PRINT_DATA = true
# path to the root folder that contains subdirectories that should contain our bank files
BANK_PATH = File.expand_path('~/Library/Application Support/Blizzard/StarCraft II/')
# polling latency for guard/listen to check for changes to bank files (in seconds)
POLLING_LATENCY_SECONDS = 0.05
# every OSC message is prefixed by this, followed by the bank name
OSC_ADDRESS_PREFIX = '/lorkCraft/'
# used for debug print messages
START_TIME = Time.now
# helper method to get seconds since program started executing (returned as a float)
def secondsSinceStart()
(Time.now - START_TIME).to_f
end
# given filename identified by bankName, sends all key-value pairs in the bank file via OSC
def sendBank(absolutePath, bankName)
# special bank handling functions
if(bankName == 'unitsBuiltBank.SC2Bank')
processUnitsBuiltBank(absolutePath, bankName)
return
elsif(bankName == 'abilitiesUsedBank.SC2Bank')
processAbilityUsedBank(absolutePath, bankName)
return
elsif(bankName == 'cameraBank.SC2Bank')
processCameraFocusBank(absolutePath, bankName)
return
elsif(bankName == 'unitProductionBank.SC2Bank' or bankName == 'buildingProductionBank.SC2Bank')
processProductionBank(absolutePath, bankName)
return
end
doc = Nokogiri::XML(open(absolutePath))
for key in doc.css('Key')
keyName = key['name']
keyValue = key.css('Value')
if(keyValue == nil)
next
end
# parse as int first, then try parsing as string, then trying parsing as fixed/floating point
value = keyValue.first['int']
valueType = "i"
if value.nil?
value = keyValue.first['string'] if value.nil?
valueType = "s"
end
if value.nil?
value = keyValue.first['fixed'] if value.nil?
valueType = "f"
end
sendOSCMessage(OSC_ADDRESS_PREFIX + bankName, keyName, 's', value, valueType)
end
puts '' if PRINT_DATA # makes logs a bit easier to digest
end
# special processor for camera bank
def processCameraFocusBank(absolutePath, bankName)
doc = Nokogiri::XML(open(absolutePath))
xCoor = -1.0;
yCoor = -1.0;
for key in doc.css('Key')
keyName = key['name']
keyValue = key.css('Value')
if(keyValue == nil)
next
end
if(keyName == 'cameraFocusX')
xCoor = keyValue.first['fixed']
elsif(keyName == 'cameraFocusY')
yCoor = keyValue.first['fixed']
else
puts "ERROR: unexpected name in XML: #{keyName}"
end
end
sendOSCMessage(OSC_ADDRESS_PREFIX + bankName, xCoor, 'f', yCoor, 'f')
puts '' if PRINT_DATA # makes logs a bit easier to digest
end
# special processor for abilities used bank
def processAbilityUsedBank(absolutePath, bankName)
doc = Nokogiri::XML(open(absolutePath))
for key in doc.css('Key')
unitName = key['name']
keyValue = key.css('Value')
if(keyValue == nil)
next
end
abilityUsed = keyValue.first['string']
# remove the common prefix from the unit name passed
unitName.gsub!('Unit/Name/', '')
sendOSCMessage(OSC_ADDRESS_PREFIX + bankName, unitName, 's', abilityUsed, 's')
end
puts '' if PRINT_DATA # makes logs a bit easier to digest
end
# special processor function for the unitsBuiltBank
def processUnitsBuiltBank(absolutePath, bankName)
doc = Nokogiri::XML(open(absolutePath))
# hash to keep track of units, default to 0 so we don't need to check if the unit has already been added
unitHash = Hash.new
unitHash.default = 0;
# construct the hash
for key in doc.css('Key')
# note: we don't care about the "name" here as it's just an arbitrary unique integer written to the bank
keyValue = key.css('Value')
if(keyValue == nil)
next
end
unitName = keyValue.first['string']
# remove the common prefix from the unit name passed
unitName.gsub!('Unit/Name/', '')
unitHash[unitName] = unitHash[unitName] + 1
end
unitHash.each_pair do |unitName, unitCount|
sendOSCMessage(OSC_ADDRESS_PREFIX + bankName, unitName, 's', unitCount, 'i')
end
puts '' if PRINT_DATA # makes logs a bit easier to digest
end
# special processor function for the units and building production banks
def processProductionBank(absolutePath, bankName)
doc = Nokogiri::XML(open(absolutePath))
# hash to keep track of progress of units/buildings being built
productionProgressHash = Hash.new
productionProgressHash.default = 0.0;
# construct the hash getting the unit/building name and build progress
for key in doc.css('Key')
# "name" is a unique integer + Unit/Name/ + name of the unit/building
unitName = key['name']
unitName.gsub!('Unit/Name/', '')
keyValue = key.css('Value')
if(keyValue == nil)
next
end
buildProgress = keyValue.first['fixed']
productionProgressHash[unitName] = buildProgress
end
# clear the aggregateProductionHash before we check on what's still building
# things that were previously building but no longer building will be accurately reported as "0" in progress with this
$aggregateProductionHash.each_pair do |name, count|
$aggregateProductionHash[name] = 0
end
if (productionProgressHash.size > 0)
productionProgressHash.each_pair do |name, buildProgress|
sendOSCMessage(OSC_ADDRESS_PREFIX + bankName, name, 's', buildProgress, 'f')
# after sending the build progress for this unit/building, increment the number of units/buildings of this type
# being tracked in the aggregate production hash
nameWithoutId = name.gsub(/.*?(?=-)/im, '').gsub('-', '') # '1-Pylon' to just 'Pylon'
$aggregateProductionHash[nameWithoutId] = $aggregateProductionHash[nameWithoutId] + 1
end
puts '' if PRINT_DATA # makes logs a bit easier to digest
else
if (bankName == 'unitProductionBank.SC2Bank')
sendOSCMessage(OSC_ADDRESS_PREFIX + bankName, 'no_unit_production', 's', '0.0', 'f')
else
sendOSCMessage(OSC_ADDRESS_PREFIX + bankName, 'no_structure_production', 's', '0.0', 'f')
end
end
# now send our aggregate production data
$aggregateProductionHash.each_pair do |name, count|
sendOSCMessage(OSC_ADDRESS_PREFIX + bankName, name, 's', count, 'i')
end
puts '' if PRINT_DATA # makes logs a bit easier to digest
end
# sends OSC message to address with 2 values: key and value. valueType defines value's type for OSC message tags; and
# keyType defines key's type
def sendOSCMessage(address, key, keyType, value, valueType)
conn = UDPSocket.new
# not sure why the OSC pack method does not handle this conversion for us...
if valueType == 'i'
value = Integer(value)
end
msg = Message.new(address, keyType + valueType, key, value)
conn.send(msg.encode, 0, $serverHost, $serverPort)
puts '%.3f' % secondsSinceStart() + ': ' + address + ' (' + msg.tags.gsub!(',', '') + '): ' + "#{key}, #{value}" if PRINT_DATA
end
# main file system listener function
def listenForBankUpdates()
Listen.to(BANK_PATH, :filter => /\.SC2Bank$/, :latency => POLLING_LATENCY_SECONDS, :force_polling => true) do |modified, added|
filesChanged = Set.new
# add modified and add files to our set
modified.each { |modifiedPath| filesChanged.add(modifiedPath) }
added.each { |addedPath| filesChanged.add(addedPath) }
begin
# parse banks for each bank file
filesChanged.each { |absolutePath| sendBank(absolutePath, File.basename(absolutePath)) }
rescue Exception => e
# handle e
# puts "ERROR: exception caught: " + e + "\n"
end
end
end
#
# "main" function
#
# quit unless our script gets the OSC destination address and port
unless ARGV.length == 2
puts "USAGE: ruby bank_parser.rb OSCDestinationAddress OSCDestinationPort\n"
exit
end
serverHost = ARGV[0]
if(serverHost == "localhost")
$serverHost == "127.0.0.1"
else
$serverHost = ARGV[0]
end
$serverPort = ARGV[1]
$aggregateProductionHash.default = 0;
listenForBankUpdates()