-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.gradle
210 lines (180 loc) · 6.25 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
import java.util.regex.Pattern
apply plugin: 'java'
// This is where mod properties are stored
def modInfoFile = file('mod.info')
project.ext.modInfoProperties = new Properties()
// Semantic versioning scheme
project.ext.semVersion = Pattern.compile('\\d+\\.\\d+\\.\\d+')
void validateModInfo() {
// map of all available mod properties
def propertiesMap = [
'url' : false,
'description': false,
'name' : true,
'modversion' : 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}")
}
else if (key == 'url')
{
try {
if (new URL(property).host != 'github.com') {
throw new InvalidUserDataException("Mod URL \'${property}\' is not a valid Github repo URL")
}
}
catch (MalformedURLException e) {
throw new InvalidUserDataException("Mod URL \'${property}\' is not a valid URL", e)
}
}
else if (key == 'modversion')
{
if (!(semVersion as Pattern).matcher(property).find()) {
throw new InvalidUserDataException("Mod version \'${property}\' " +
'does not follow semantic versioning rules: https://semver.org/')
}
}
})//@formatter:on
}
def saveModInfo = tasks.register('saveModInfo') {
it.description 'Save mod metadata to file.'
it.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=" + modInfoProperties.get('modversion'),
"pzversion=${modInfoProperties.get('pzversion')}",
] as String[]).forEach({ l -> writer.writeLine(l) })
}
}
it.dependsOn(tasks.named('zomboidVersion'), loadModInfo)
}
tasks.register('initModInfo') {
it.description 'Initialize mod metadata information.'
it.group 'mod'
it.onlyIf {
!modInfoFile.exists()
}
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 github repository URL: \n', addproperty: 'mod.url')
modInfoProperties.setProperty('url', ant.properties.get('mod.url') as String)
// starting mod version is 0.1.0 (sem-ver)
modInfoProperties.setProperty('modversion', '0.1.0')
}
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)
})
def sUrl = modInfoProperties.getProperty('url')
if (sUrl && !sUrl.isBlank()) {
def url = new URL(sUrl)
def urlPath = url.path.startsWith('/') ? url.path.substring(1) : url.path
def pathElements = urlPath.split('/')
if (pathElements.length != 2) {
throw new InvalidUserDataException("Unexpected mod url format '${urlPath}'")
}
project.ext.set('repo.owner', pathElements[0])
project.ext.set('repo.name', pathElements[1])
}
}
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: ${project.version}")
}
}
task createModStructure {
description 'Create default mod directory structure.'
group 'mod'
doLast {
def media = sourceSets.named('media').get()
media.java.getSrcDirs().forEach({
project.mkdir(it)
})
media.resources.srcDirs.forEach({
project.mkdir(it)
})
}
}
tasks.register('applyModTemplate', Copy.class) {
it.description 'Apply Project Zomboid mod template.'
it.group 'mod'
it.into projectDir
it.from 'docs/template'
}