generated from cocolabs/pz-zmod
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.gradle
232 lines (200 loc) · 7.41 KB
/
mod.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
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.Path
apply plugin: 'java'
def getPathsRelativeToModule(moduleName, srcDirSet) {
def module = rootDir.toPath().resolve(moduleName)
def map = new HashMap<String, String>()
def srcDirs = srcDirSet.srcDirs.stream().withCloseable {
it.filter({ f -> f.exists() }).collect()
}
for (File srcDir : (srcDirs as List<File>))
{
Files.walk(srcDir.toPath()).withCloseable
{
def paths = it.filter({ Files.isRegularFile(it as Path) }).collect()
for (Path path : (paths as List<Path>))
{
def srcDirPath = srcDir.toPath()
def relativePath = srcDirPath.relativize(path)
def srcDirName = module.relativize(srcDirPath)
map.put(relativePath.toString(), srcDirName.toString())
}
}
}
return map
}
static def getRelativeCopyPath(fcd, relativePathMap) {
def path = relativePathMap.get(Paths.get(fcd.path).toString())
if (path != null) {
return fcd.relativePath.prepend(path)
}
else throw new Exception("Unable to relativize copy path: $fcd.path")
}
task mediaClasses(type: Copy, overwrite: true) {
includeEmptyDirs = false
from(sourceSets.media.java.srcDirs) {
def mSources = getPathsRelativeToModule('media', sourceSets.media.java)
eachFile {
def fcd = it as FileCopyDetails
fcd.setRelativePath(getRelativeCopyPath(fcd, mSources))
}
}
into "$buildDir/classes/java/media"
}
processMediaResources {
includeEmptyDirs = false
def mResources = getPathsRelativeToModule('media', sourceSets.media.resources)
eachFile {
def fcd = it as FileCopyDetails
fcd.setRelativePath(getRelativeCopyPath(fcd, mResources))
}
}
// This is where mod properties are stored
def modInfoFile = file('mod.info')
project.ext.modInfoProperties = new Properties()
void validateModInfo() {
// map of all available mod properties
def propertiesMap = [
'url': false,
'description': false,
'name': true,
//game version is checked in zdoc.gradle
//mod.pzversion: String.class,
]
//@formatter:off
propertiesMap.entrySet().forEach(
{
def key = it.key
def property = modInfoProperties.getProperty(key)
if (property == null || property.trim().empty)
{
def log = "Missing mod property \'${key}\'"
if ((it.value as boolean)) {
throw new InvalidUserDataException(log)
}
else logger.warn("WARN: ${log}")
}
})//@formatter:on
def modUrl = modInfoProperties.getProperty('url')
if (modUrl != null && !modUrl.trim().empty)
{
try {
new URL(modUrl)
}
catch (MalformedURLException e) {
throw new InvalidUserDataException("Mod property \'url\' " +
"is not a valid value for property \'${modUrl}\'", e)
}
}
}
def saveModInfo = tasks.register('saveModInfo') {
description('Save mod metadata to file.')
group('mod')
it.doLast {
// validate properties before saving them
validateModInfo()
// game version is not set via user input
def sGameVersion = (project.ext.get('mod.pzversion') as String).replaceAll('\\r\\n?', '')
modInfoProperties.setProperty('pzversion', sGameVersion)
// trim all mod property values
modInfoProperties.entrySet().forEach({
modInfoProperties.setProperty(it.key as String, (it.value as String).trim())
})
// ensure that mod.info file exists before writing to it
if (!modInfoFile.exists())
{
if (!modInfoFile.createNewFile()) {
throw new IOException('Unable to create mod.info file')
}
}
// save mod properties to mod.info file
modInfoFile.withWriter('utf-8') { writer ->
def modUrl = modInfoProperties.get('url') as String
Arrays.asList([
"name=${modInfoProperties.get('name')}",
"poster=poster.png",
"description=${modInfoProperties.get('description')}",
"id=$rootProject.name",
// remove backslash used to escape key/value separators
"url=${modUrl != null ? modUrl.replaceAll('\\\\\\\\:', ':') : ''}",
"modversion=" + rootProject.version,
"pzversion=${modInfoProperties.get('pzversion')}",
] as String[]).forEach({ l -> writer.writeLine(l) })
}
}
it.dependsOn(tasks.named('zomboidVersion'), loadModInfo)
}
tasks.register('initModInfo') {
description('Initialize mod metadata information.')
group('mod')
it.doLast {
ant.input(message: 'Enter mod name: \n', addproperty: 'mod.name')
modInfoProperties.setProperty('name', ant.properties.get('mod.name') as String)
ant.input(message: 'Enter mod description: \n', addproperty: 'mod.description')
modInfoProperties.setProperty('description', ant.properties.get('mod.description') as String)
ant.input(message: 'Enter mod repo/workshop URL: \n', addproperty: 'mod.url')
modInfoProperties.setProperty('url', ant.properties.get('mod.url') as String)
}
it.finalizedBy(saveModInfo)
}
task loadModInfo {
description 'Load mod metadata information.'
group 'mod'
if (modInfoFile.exists())
{
// load properties from properties file
modInfoFile.withInputStream {
modInfoProperties.load(it)
}
// load mod properties as project extra properties
modInfoProperties.entrySet().forEach({
logger.info("Loading property ${it.key}:${it.value}")
project.ext.set("mod.${(it.key as String)}", it.value as String)
})
}
else logger.warn('WARN: Unable to find mod.info file')
}
/**
* Returns property name for use in console output
* @param name property name
*/
String getDisplayProperty(String name) {
String property = project.ext.get(name)
return property.empty ? '<not specified>' : property
}
task showModInfo {
description('Print mod metadata information.')
group('mod')
onlyIf {
modInfoFile.exists()
}
doLast {
[ 'mod.pzversion', 'mod.name' ].forEach({
if (!project.ext.has(it)) {
throw new InvalidUserDataException("Missing mod property ${it}, " +
"please (re)initialize mod properties.")
}
})
logger.lifecycle("This is a mod for Project Zomboid (${getDisplayProperty('mod.pzversion')})")
logger.lifecycle('------------------------------------------------')
logger.lifecycle("Name: ${getDisplayProperty('mod.name')}")
logger.lifecycle("Description: ${getDisplayProperty('mod.description')}")
logger.lifecycle("URL: ${getDisplayProperty('mod.url')}")
logger.lifecycle("ID: ${rootProject.name}")
logger.lifecycle("Version: v${project.version}")
}
}
task createModStructure {
group 'mod'
description 'Create default mod directory structure.'
doLast {
def media = sourceSets.named('media').get()
media.java.getSrcDirs().forEach({
project.mkdir(it)
})
media.resources.srcDirs.forEach({
project.mkdir(it)
})
}
}