Florian Nücke | 5ffe425 | 2013-11-20 01:45:32 +0100 | [diff] [blame] | 1 | package li.cil.oc.util |
| 2 | |
| 3 | import net.minecraftforge.common.Configuration |
| 4 | import scala.language.implicitConversions |
| 5 | |
| 6 | object 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ücke | 920d485 | 2013-11-20 12:31:03 +0100 | [diff] [blame] | 20 | 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ücke | 5ffe425 | 2013-11-20 01:45:32 +0100 | [diff] [blame] | 25 | 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ücke | d936cab | 2013-11-21 02:27:18 +0100 | [diff] [blame] | 35 | 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ücke | 5ffe425 | 2013-11-20 01:45:32 +0100 | [diff] [blame] | 40 | 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 | } |