-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacf_manager.rb
288 lines (273 loc) · 10 KB
/
acf_manager.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
#!/usr/bin/env ruby
# encoding: UTF-8
require 'steam_codec'
require 'optparse'
require 'set'
require 'pathname'
os = Gem::Platform.local.os
$isWindows = (os == 'mingw32' or os == 'mingw64')
# Normalize path so that there's no double slashes
# and for Windows replace backslashes to forwardslashes
# @param path [String] path
# @return [String] normalized path
def normalizePath(path)
path.gsub!('\\','/') if $isWindows
path.gsub!(/\/\/+/,'/')
path.chomp('/')
end
def fromLinuxRegistry(path)
value = nil
File.open(Dir.home+'/.steam/registry.vdf', 'r:UTF-8') do |file|
registry = SteamCodec::VDF::loadFromFile(file)
break unless registry and registry.Registry
value = registry.Registry.get(path)
end
value
rescue StandardError => e
$stderr.puts e.message
end
def getSteamPath
platform = Gem::Platform.local
case platform.os
when 'mingw32', 'mingw64'
require 'win32/registry'
registryPath = 'SOFTWARE\Valve\Steam'
registryPath = 'SOFTWARE\Wow6432Node\Valve\Steam' if platform.cpu == 'x64'
begin
Win32::Registry::HKEY_LOCAL_MACHINE.open(registryPath, Win32::Registry::KEY_READ) do |reg|
return normalizePath(reg['InstallPath'].to_s)
end
rescue Win32::Registry::Error => e
$stderr.puts e.message
end
when 'darwin'
return Dir.home+'/Library/Application Support/Steam'
else
$stderr.puts "Uknown OS: #{platform.os}" if platform.os != 'linux'
installPath = fromLinuxRegistry('Registry/HKLM/SOFTWARE/Valve/Steam/InstallPath')
installPath = Dir.home+'/.local/share/Steam' unless installPath
return normalizePath(installPath.to_s)
end
end
def getOptions
options = {
:SteamPaths => [], :AppDirs => [], :Action => :export,
:Fields => ['AppID', 'StateFlags', 'InstallDir', 'SizeOnDisk', 'BuildId', 'UserConfig.Name', 'UserConfig.Installed', 'UserConfig.AppInstallDir'],
:Mode => :downloaded, :Format => :csv, :File => nil }
parser = OptionParser.new do |opts|
opts.banner = 'Usage: acf_manager.rb [options]'
opts.on('-p', '--paths steam', Array, 'Paths to Steam directories') do |paths|
paths.each do |path|
options[:SteamPaths] << normalizePath(path)
end
end
opts.on('-a','--apps paths', Array, 'Paths to SteamApps directories') do |paths|
paths.each do |path|
options[:AppDirs] << normalizePath(path)
end
end
opts.on('-e','--execute ACTION', [:export, :list], 'Execute specified action (export, list)') do |action|
options[:Action] = action
end
opts.on('-f','--fields fields', Array, 'Specify which fields to export') do |fields|
options[:Fields] = fields
end
opts.on('-m','--mode MODE', [:downloaded, :installed, :unreferenced], "Mode for `list` (downloaded, installed,\n#{' '*37}unreferenced)") do |mode|
options[:Mode] = mode
end
opts.on('-o','--output FORMAT', [:csv, :yml, :json, :xml, :vdf], 'Output format (csv, yml, json, xml, vdf)') do |format|
options[:Format] = format
end
opts.on('-s','--save FILE', 'File where to save output') do |file|
options[:File] = file
end
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
return false
end
end
begin
parser.parse!
rescue OptionParser::ParseError => e
$stderr.puts e.message
return false
end
return options
end
def rowToHash(header, rowData)
hashRow = {}
header.each_index do |i|
headerName = header[i].split('.')
headerCount = headerName.length
current = hashRow
headerName.each_index do |j|
current[headerName[j]] = {} unless current[headerName[j]]
if j == headerCount - 1
current[headerName[j]] = rowData[i]
else
current = current[headerName[j]]
end
end
end
hashRow
end
def tableToArray(header, rows)
data = []
rows.each do |row|
data << rowToHash(header, row)
end
data
end
def formatter(format, &block)
header = []
data = []
yield(header, data)
case format
when :yml
require 'yaml'
return YAML.dump(tableToArray(header, data))
when :json
require 'json'
return JSON.dump(tableToArray(header, data))
when :xml
require 'gyoku'
xmlContent = '<?xml version="1.0" encoding="UTF-8"?>'
xmlContent += "\n<result>\n"
xmlContent += Gyoku::xml({ :entry => tableToArray(header, data)}, { :key_converter => :none, :builder => { :indent => 2} })
return xmlContent+'</result>'
when :vdf
$stderr.puts "Sorry, format `#{format.to_s}` is not implemented yet."
end
require 'csv'
CSV.generate({:encoding => 'UTF-8', :col_sep => ',', :force_quotes => false, :headers => true}) do |csv|
csv << header
data.each do |row|
csv << row
end
end
end
def main
options = getOptions
return false unless options
options[:SteamPaths] << getSteamPath if options[:SteamPaths].count == 0
validSteamPaths = []
options[:SteamPaths].each do |steamPath|
if File.directory?(steamPath)
validSteamPaths << steamPath
options[:AppDirs] << steamPath+'/SteamApps'
begin
File.open(steamPath+'/config/config.vdf', 'r:UTF-8') do |file|
configData = SteamCodec::VDF::loadFromFile(file)
break unless configData
steamConfig = configData.get('InstallConfigStore/Software/Valve/Steam')
break unless steamConfig
steamFolders = steamConfig.asArray('BaseInstallFolder')
options[:SteamPaths] += steamFolders
steamFolders.each do |folder|
next unless File.directory?(folder)
path = normalizePath(folder)
validSteamPaths << path
options[:AppDirs] << path+'/SteamApps'
end
end
rescue StandardError => e
$stderr.puts e.message
end
else
$stderr.puts "Steam path #{steamPath} does\'t exist"
end
end
options[:AppDirs] << '.' if Dir['appmanifest_*.acf'].count > 0
if options[:AppDirs].count == 0
$stderr.puts 'There\'s no SteamApps directory specified'
return false
end
result = formatter(options[:Format]) do |header, data|
if options[:Action] == :list
case options[:Mode]
when :downloaded
header << 'AppID'
header << 'UserConfig.Name'
header << 'InstallDir'
header << 'AppDir'
when :installed
header << 'AppID'
header << 'UserConfig.Name'
header << 'InstallDir'
header << 'UserConfig.AppInstallDir'
when :unreferenced
header << 'InstallDir'
header << 'AppPath'
end
else
options[:Fields].each do |field|
header << field
end
end
options[:AppDirs].each do |appDir|
if not File.directory?(appDir)
$stderr.puts "#{appDir} Doesn't exist!"
next
end
gameDirs = Set.new
gameData = []
Dir.glob(appDir+'/appmanifest_*.acf') do |filename|
File.open(filename, 'r:UTF-8') do |file|
acf = SteamCodec::ACF::loadFromFile(file)
if acf
if options[:Action] == :list
case options[:Mode]
when :downloaded
gameData << [acf.AppID, acf.UserConfig.Name, acf.InstallDir, appDir]
when :installed
gameData << [acf.AppID, acf.UserConfig.Name, acf.InstallDir, normalizePath(acf.UserConfig.AppInstallDir)] if acf.UserConfig.Installed
when :unreferenced
if not acf.InstallDir.nil?
gameDirs << normalizePath(acf.InstallDir)
else
$stderr.puts "#{filename} : Invalid ACF! Doesn't have `installdir` key"
end
end
else
entry = []
header.each do |name|
entry << acf.get(name)
end
gameData << entry
end
else
$stderr.puts "#{filename} : Invalid ACF"
end
end
end
if options[:Action] == :list and options[:Mode] == :unreferenced
sorted = gameDirs.sort_by { |dir| dir.downcase }
Pathname.glob(appDir+'/common/*/').each do |dir|
name = dir.basename.to_s
data << [name.to_s, dir.cleanpath.to_s] unless sorted.include?(name)
end
else
gameData.sort_by do |d|
if d.first.is_a?(Numeric)
d.first
else
d.first.to_s
end
end.each do |entry|
data << entry
end
end
end
end
if options[:File]
extension = ''
extension = '.' + options[:Format].to_s unless options[:File].include? '.'
File.open(options[:File] + extension, 'w:UTF-8') do |file|
file.write(result)
end
else
puts result
end
true
end
main unless $spec