-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.gradle
246 lines (226 loc) · 9.15 KB
/
helper.gradle
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
import groovy.json.*
def assets = new File('src/main/resources/assets/')
def modinfo = new File(assets, 'mcmod.info')
task genMusic {
group = 'helper'
doLast {
def dirs = assets.listFiles()
if (dirs == null) return
def reader = new JsonSlurper()
for (def dir : dirs) {
def soundDir = new File(dir, 'sounds')
def sounds = new File(dir, 'sound.json')
if (!soundDir.exists()) continue
def ls = []
discoverSounds(soundDir, ls)
def p = soundDir.toPath()
def info
if (sounds.exists() && sounds.isFile())
info = reader.parseText(sounds.text)
else info = new TreeMap()
for (def fp : ls) {
def rela = p.relativize(fp)
def path = rela.toString()
path = path.substring(0, path.lastIndexOf('.'))
def name = path.replace('/', '.')
if (!info.containsKey(name))
info.put(name, [
category: 'player',
sounds : [[name: path, stream: true]]
])
}
if (info.isEmpty()) continue
sounds.text = JsonOutput.prettyPrint(JsonOutput.toJson(info))
}
}
}
def discoverSounds(dir, ls) {
File[] musics = dir.listFiles(new FileFilter()
{
boolean accept(File pathname) {
if (pathname.isDirectory()) {
discoverSounds(pathname, ls)
return false
}
return pathname.getName().endsWith(".ogg")
}
})
if (musics != null && musics.length != 0)
for (def music : musics)
ls.add(music.toPath())
}
task checkAssets {
group = 'helper'
doLast {
if (!modinfo.exists()) tasks.generateModInfo.execute()
println 'Generating textures'
def json = new JsonSlurper().parseText(modinfo.text)
def modList
if (json instanceof List)
modList = json
else modList = [json]
for (def mod : modList) {
def modid = mod.modid
if (modid == null || !modid instanceof String) {
println 'Cannot find modid in mcmod.info, Skip!'
continue
}
def modroot = new File(assets, modid)
if (!modroot.exists() || modroot.isFile()) modroot.mkdir()
def defs = new File(modroot, 'defs')
if (!defs.exists() || defs.isFile()) defs.mkdirs()
def bDir = new File(defs, 'blocks')
if (!bDir.exists()) bDir.mkdirs()
def blocks = resolveBlocks(bDir)
def iDir = new File(defs, 'items')
if (!iDir.exists()) iDir.mkdirs()
def items = resolveItems(iDir)
if (blocks != null && !blocks.isEmpty())
checkBlockState(modid, blocks, new File(modroot, 'blockstates'))
if (items != null && !items.isEmpty())
checkItemModel(modid, items, new File(modroot, 'models/item'))
}
}
}
def resolveItems(dir, itemList = []) {
println 'resolving ' + dir.getAbsolutePath()
if (!dir.exists() || dir.isFile()) return itemList
def files = dir.listFiles()
if (files == null) return itemList
for (def file : files) {
println 'resolving ' + file.getAbsolutePath()
if (file.isDirectory()) {
resolveItems(dir, itemList)
continue
}
def jsonContent = file.text
if (jsonContent == null || !(jsonContent instanceof String) || jsonContent.isEmpty()) continue
def json = new JsonSlurper().parseText(jsonContent)
for (def item : json) {
def id = item.id
def meta = 0
if (item.type == 'stack') meta = item.subTypes
itemList.add([id: id, meta: meta])
}
}
return itemList
}
def checkItemModel(modid, items, dir) {
if (!dir.exists()) dir.mkdirs()
for (def item : items) {
System.out.println('checking model ' + modid + ':' + item)
def subTypes = [item.id]
subTypes.add(item.meta)
for (int i = 0; i < subTypes.size(); i++) {
def realName = i > 1 ? item + '_' + subTypes[i] : item.id
def modelFile = new File(realName + '.json')
def model
if (!modelFile.exists()) {
def txt = JsonOutput.prettyPrint(
JsonOutput.toJson([parent: "item/generated", textures: [layer0: modid + ':items/' + realName]]))
new File(dir, realName + '.json').text = txt
model = new JsonSlurper().parseText(txt)
} else
model = new JsonSlurper().parseText(modelFile.text)
def texture = model.textures
if (texture == null) return
for (def texLoc : texture)
checkTexture modid, texLoc.value.substring(texLoc.value.indexOf(':') + 1)
}
}
}
def resolveBlocks(dir, blockList = []) {
if (!dir.exists() || dir.isFile()) return blockList
def files = dir.listFiles()
if (files == null) return blockList
for (def f : files) {
if (f.isDirectory()) {
resolveBlocks(dir, blockList)
continue
}
def jsonContent = f.text
if (jsonContent == null || !(jsonContent instanceof String) || jsonContent.isEmpty()) continue
def json = new JsonSlurper().parseText(jsonContent)
for (def block : json) {
blockList.add(block.id)
}
}
return blockList
}
def checkBlockState(modid, blocks, dir) {
for (def block : blocks) {
def blockState = new File(dir, block + '.json')
if (!blockState.exists()) blockState.text = '{\n' +
' "variants": {\n' +
' "normal": { "model":' + block + '}\n' +
' }\n' +
'}'
state = new JsonSlurper().parseText(blockState.text)
for (def entry : state.variants)
checkBlockModel(modid, entry.model, new File(assets, modid + '/models/block'))
}
}
def checkBlockModel(modid, block, dir) {
blockModelFile = new File(dir, block)
if (!blockModelFile.exists()) blockModelFile.text = '{\n' +
' "parent": "block/cube_all",\n' +
' "textures": {\n' +
' "all": blocks/' + block + '\n' +
' }\n' +
'}'
model = new JsonSlurper().parseText(blockModelFile.text)
for (def tex : model.textures)
checkTexture modid, tex.value
}
def checkTexture(modid, loc) {
println 'checking texture ' + modid + ':' + loc
println loc
def missing
def dir = new File('src/main/resources/assets/' + modid + '/textures')
def file = new File(dir, loc + '.png')
file.getParentFile().mkdirs()
if (!file.exists())
new File(dir, loc + '.png.missing').createNewFile()
else if ((missing = new File(dir, loc + '.png.missing')).exists())
missing.delete()
}
task genModInfo {
group = 'helper'
doLast {
println 'generating template mcmod.info'
def dir = new File('src/main/resources/assets/')
def info = new File(dir, 'mcmod.info')
if (info.exists())
println 'mcmod.info already exist!'
else {
def p = project.getProject()
def modid = p.findProperty('modid')
def infoObj = [[
modid : modid == null ? archivesBaseName.toLowerCase() : modid,
name : p.hasProperty('name') ? archivesBaseName : p.findProperty('name'),
description : p.hasProperty('description') ? archivesBaseName : p.findProperty('description'),
mcversion : minecraft.version,
version : version,
url : !project.hasProperty('url') ? "" : p.findProperty('url'),
updateUrl : !project.hasProperty('updateUrl') ? "" : p.findProperty
('updateUrl'),
authorList : [],
credits : project.group.toString(),
logoFile : "",
screenshots : [],
parent : "",
requiredMods : ["Forge-" + minecraft.version],
dependencies : [],
dependants : [],
useDependencyInformation: true,
items : [],
blocks : [],
entities : []
]
]
def json = JsonOutput.prettyPrint(JsonOutput.toJson(infoObj))
dir.mkdirs()
info.write(json)
}
}
}