blob: 6c30a535b1a3f0226d30febfd379c736385a6a5e [file] [log] [blame] [raw]
Florian Nücke5ffe4252013-11-20 01:45:32 +01001package li.cil.oc.util
2
3import net.minecraftforge.common.Configuration
4import scala.language.implicitConversions
5
6object ExtendedConfiguration {
7 implicit def extendedConfiguration(config: Configuration) = new ExtendedConfiguration(config)
8
9 class ExtendedConfiguration(config: Configuration) {
10 def fetch(path: String, default: Boolean, comment: String) = {
11 val (category, name) = parse(path)
12 config.get(category, name, default, wrapComment(category, comment)).getBoolean(default)
13 }
14
15 def fetch(path: String, default: Int, comment: String) = {
16 val (category, name) = parse(path)
17 config.get(category, name, default, wrapComment(category, comment)).getInt(default)
18 }
19
Florian Nücke920d4852013-11-20 12:31:03 +010020 def fetch(path: String, default: Float, comment: String) = {
21 val (category, name) = parse(path)
22 config.get(category, name, default, wrapComment(category, comment)).getDouble(default).toFloat
23 }
24
Florian Nücke5ffe4252013-11-20 01:45:32 +010025 def fetch(path: String, default: Double, comment: String) = {
26 val (category, name) = parse(path)
27 config.get(category, name, default, wrapComment(category, comment)).getDouble(default)
28 }
29
30 def fetch(path: String, default: String, comment: String) = {
31 val (category, name) = parse(path)
32 config.get(category, name, default, wrapComment(category, comment)).getString
33 }
34
Florian Nücked936cab2013-11-21 02:27:18 +010035 def fetch(path: String, default: Array[Int], comment: String) = {
36 val (category, name) = parse(path)
37 config.get(category, name, default, wrapComment(category, comment)).getIntList
38 }
39
Florian Nücke5ffe4252013-11-20 01:45:32 +010040 private def parse(path: String) = {
41 val (category, name) = path.splitAt(path.lastIndexOf("."))
42 (category, name.substring(1))
43 }
44
45 private def wrapComment(category: String, comment: String) = {
46 val indent = 1 + category.count(_ == '.')
47 val wrapRegEx = ("""(.{1,""" + (78 - indent * 4 - 2) + """})(\s|\z)""").r
48 val cleaned = comment.replace("\r\n", " ").replace("\n", " ").replace("\r", " ").replace("[nl]", "\n").trim()
49 wrapRegEx.replaceAllIn(cleaned, m => m.group(1).trim() + "\n").stripLineEnd
50 }
51 }
52
53}