-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.rb
282 lines (242 loc) · 10.1 KB
/
run.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
=begin
Written by Irvin.
Distributed under the terms of the GNU General Public License v2.
=end
require 'rubygems'
require 'zip'
require 'yaml'
require 'json'
require 'fileutils'
require 'perfect_toml'
#
# This class generates mods.yml file, which is derived from mcmod.info/litemod.json/mods.toml/MANIFEST.MF.
#
class Config
# Constructor
def initialize( modsPath = "./mods/", configPath = "./config/", configFile = "mods.yml" )
@modsPath = modsPath
@configFile = configFile
@configPath = configPath
if ( ! Dir.exist? @configPath )
Dir.mkdir( @configPath )
end
if ( ! File.exist? @configPath + @configFile )
file = File.new( @configPath + @configFile, "w+" )
end
if ( ! Dir.exist? @modsPath )
puts "Error! Path to mods folder does not exist!"
return nil
end
end
# Adds mods to config file
def append( filename = "", modslug = "", version = "" )
fileAppend = File.new( @configPath + @configFile, 'a' ) #r+
fileRead = File.new( @configPath + @configFile, 'r' )
entry = {
modslug => {
"version" => version,
"filename" => filename
}
}
# Append to file if file is empty. This is necessary to avoid generate initial yml file.
if ( File.zero? fileRead )
fileAppend.write ( entry.to_yaml )
fileRead.close()
fileAppend.close()
# If mods.yml is not empty, append by combining mods.yml file. This is necessary to prevent headers from being printed.
else
data = YAML.load( fileRead )
merged = data.merge( entry )
fileWrite = File.new( @configPath + @configFile, 'w' )
fileWrite.write( YAML.dump ( merged ) )
fileWrite.close()
end
end
# If mods.yml already has mod listed, update. If not, go to append method to populate that mod.
def update( filename = "", modslug = "", version = "" )
file = File.new( @configPath + @configFile, 'r+' )
data = YAML.load( file )
if ( filename == "." || filename == ".." )
return nil
end
if ( ! File.zero? file )
if ( data.key?( modslug ) )
data[modslug]["filename"] = filename
data[modslug]["version"] = version
fileWrite = File.new( @configPath + @configFile, 'w' )
fileWrite.write ( data.to_yaml )
fileWrite.close()
file.close()
return nil
end
end
file.close()
append( filename, modslug, version )
end
# Get information from mcmod.info and litemod.json and pass that information to update method, which will pass to append method if necessary.
def populate()
puts "Working on directory: #{@modsPath}"
# Initalize variables
modslug = ""
version = ""
filename = ""
found = false
# Loop through all files in the mods directory
Dir.foreach( @modsPath ) do | current |
absolute = @modsPath.to_s + current.to_s
puts "Currently populating: #{current}"
# If the file ends with .jar, for forge mods.
if ( File.extname( current ) == ".jar" )
Zip::File.open ( absolute ) do | zip_file |
if ( zip_file.find_entry( "mcmod.info" ) != nil )
info = zip_file.read( "mcmod.info" )
begin
json = JSON.parse( info )[ 0 ]
modslug = json['modid']
version = json['version']
found = true
zip_file.close()
rescue # Some mod devs have broken mcmod.info or weird parsing. This will prevent exceptions.
zip_file.close()
found = false
end
elsif ( zip_file.find_entry( "META-INF/mods.toml" ) != nil )
info = zip_file.read( "META-INF/mods.toml" )
begin
toml = PerfectTOML.parse( info )['mods'][0]
modslug = toml[ "modId" ]
version = toml[ "version" ]
if ( version == "${file.jarVersion}" )
if ( zip_file.find_entry( "META-INF/MANIFEST.MF" ) != nil )
version = Hash[zip_file.read( "META-INF/MANIFEST.MF" ).split("\n").map{|i|i.split(': ')}]["Implementation-Version"].chomp
end
end
found = true
zip_file.close()
rescue
found = false
zip_file.close()
end
elsif ( zip_file.find_entry( "fabric.mod.json" ) != nil )
info = zip_file.read( "fabric.mod.json" )
begin
toml = JSON.parse( info )
modslug = toml[ "id" ]
version = toml[ "version" ]
found = true
zip_file.close()
rescue
found = false
zip_file.close()
end
else # catch case for no matches
found = false
zip_file.close()
end
end
# If the file ends with .litemod, for litemods.
elsif ( File.extname( current ) == ".litemod" )
Zip::File.open ( absolute ) do | zip_file |
if ( zip_file.find_entry ( "litemod.json" ) )
found = true
info = zip_file.read( "litemod.json" )
json = JSON.parse( info )
modslug = json['name']
version = json['mcversion'] + "-" + json['version']
end
end
end
filename = current
# If the loop above was not able to obtain information automatically, default back to filename.
if ( found == false )
modslug = current
version = "N/A"
end
update( filename, modslug.downcase, version )
end
end
end
#
# This class uses generated mods.yml and creates .zip files for solder installation.
#
class Solder
# Constructor
def initialize ( modsPath = "./mods/", configPath = "./config/", configFile = "mods.yml", outputPath = "./output/" )
@modsPath = modsPath
@configPath = configPath
@configFile = configFile
@outputPath = outputPath
end
# Create folders in the output directory for archiving
def populate_folders
configFile = File.new( @configPath + @configFile, 'r' )
configHash = YAML.load( configFile )
if ( ! File.zero? configFile )
configHash.each do | key, value |
directory = @outputPath + key + "/" + "mods"
puts directory
FileUtils.mkdir_p( directory )
end
end
end
# Create a copy of mods in the mods folder, to the directory we've just created.
def populate_mods
configFile = File.new( @configPath + @configFile, 'r' )
configHash = YAML.load( configFile )
if ( ! File.zero? configFile )
configHash.each do | key, value |
file = @modsPath + configHash[key]["filename"]
destination = @outputPath + key + "/" + "mods" + "/"
if ( ! File.exist? destination + configHash[key]["filename"] )
FileUtils.cp( file, destination )
end
end
end
end
# Create an archive (.zip) that includes mods folder and mod itself.
def populate_zips
configFile = File.new( @configPath + @configFile, 'r' )
configHash = YAML.load( configFile )
if ( ! File.zero? configFile )
configHash.each do | key, value |
zipFile = @outputPath.to_s + key.to_s + "/" + key.to_s + "-" + configHash[key]["version"].to_s + ".zip"
path = @outputPath + key + "/" + "mods" + "/"
File.delete( zipFile ) if File.exist?( zipFile )
Zip::File.open( zipFile, Zip::File::CREATE ) do | zip_file |
zip_file.mkdir( "mods" )
zip_file.add( "mods/" + configHash[key]["filename"], path + configHash[key]["filename"] )
zip_file.close()
end
end
end
end
# For now, this only gets rid of mods folder and mod itself.
def cleanup
configFile = File.new( @configPath + @configFile, 'r' )
configHash = YAML.load( configFile )
if ( ! File.zero? configFile )
configHash.each do | key, value |
path = @outputPath + key + "/" + "mods" + "/"
FileUtils.rm_rf( path )
end
end
end
end
#
# Essentially main method.
#
# Create new object for config
config = Config.new()
config.populate()
# Stop and prompt the user to edit the config
puts "Please edit the config file to ensure that all mods have correct: "
puts " * name: "
puts " * version: "
puts "Hit ENTER when you are done."
answer = gets.chomp!
# Create new object for Solder
action = Solder.new()
action.populate_folders()
action.populate_mods()
action.populate_zips()
action.cleanup()