Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class WorldObjectParser(lines: Iterable<String>) : Parser(lines) {
- constructor(file: File) : this(file.readLines())
- override fun parse(lineIter: Iterator<String>) {
- fun nextLine() = lineIter.next()
- fun stringValue(line: String): String {
- val split = line.split("'")
- return split[split.size - 2]
- }
- fun floatTupleSplit(line: String, type: String): Pair<String, Sequence<Float>> {
- val (name, str) = line.splitToSequence(type).map { it.trim() }.toList()
- return Pair(name, str.split("(", ")")[1].splitToSequence(",").map{ it.toFloat() })
- }
- fun scalarName(line: String, type: String) = line.split(type)[0].trim()
- fun parseNextCompound(inner: CompoundNode) {
- nextLine() // {
- stack.push(inner)
- parse(lineIter)
- stack.pop()
- }
- val node = stack.peek()
- while (lineIter.hasNext()) {
- val line = nextLine()
- if (line == "}") {
- break
- }
- val splitWhitespace = line.splitByWhitespace()
- val firstWord = splitWhitespace.first()
- val lastWord = splitWhitespace.last()
- val boolValue by lazy { lastWord.lowercase().toBooleanStrictOrNull() }
- val intValue by lazy { lastWord.toIntOrNull() }
- val floatValue by lazy { lastWord.toFloatOrNull() }
- val doubleValue by lazy { lastWord.toDoubleOrNull() }
- fun putConstData(): ConstDataNode {
- val name = stringValue(line)
- val constData = ConstDataNode()
- constData.def[firstWord] = StringNode(name)
- node[name] = constData
- return constData
- }
- fun parseConstData(constData: ConstDataNode) {
- nextLine() // ... ConstData
- parseNextCompound(constData.inner)
- }
- // Type String '...'
- if (firstWord == "Type") {
- // TODO: (World) Handle World Rules
- parseConstData(putConstData())
- }
- // Definition String '...'
- else if (firstWord == "Definition") {
- val constData = putConstData()
- val entity = stringValue(nextLine())
- constData.def["EntityType"] = StringNode(entity)
- if (entity.isNotEmpty()) {
- constData.def["FactoryType"] = StringNode(stringValue(nextLine()))
- parseConstData(constData)
- }
- }
- /* TODO:
- (WorldObject)
- ... Mesh Attribute Manager
- ... GUIInfo Chunk
- ... Lightning Damage Potential
- ... Wind Damage Potential
- ... Meteor Damage Potential
- ... ImpactSoundsHull Wood
- ... ImpactSoundsHull ReInforced Wood
- ... ImpactSoundsHull Iron
- ... ImpactSoundsSail Cloth
- ... ImpactSoundsWall Stone
- ... ImpactSoundsDragon Scale
- */
- else if (line.contains("Matrix33")) {
- val (name, floats) = floatTupleSplit(line, "Matrix33")
- val rows = floats.chunked(3).map { it.toFloatArray() }.toList()
- node[name] = MatrixNode(floatMatrixOf(rows[0], rows[1], rows[2]))
- }
- else if (line.contains("Colour")) {
- val (name, floats) = floatTupleSplit(line, "Colour")
- node[name] = VectorNode(floats.toList().toFloatArray())
- }
- else if (line.contains("Vector3")) {
- val (name, floats) = floatTupleSplit(line, "Vector3")
- node[name] = VectorNode(floats.toList().toFloatArray())
- }
- else if (line.contains("Coord")) {
- val (name, floats) = floatTupleSplit(line, "Coord")
- node[name] = CoordNode(floats.toList().toFloatArray())
- }
- // ... String '...'
- else if (lastWord.contains("'")) {
- node[scalarName(line, "String")] = StringNode(stringValue(line))
- }
- // ... Bool '...'
- else if (boolValue != null) {
- node[scalarName(line, "Bool")] = BoolNode(boolValue!!)
- }
- // ... Int '...'
- else if (intValue != null) {
- node[scalarName(line, "Int")] = IntNode(intValue!!)
- }
- // ... Float '...'
- else if (floatValue != null) {
- node[scalarName(line, "Float")] = FloatNode(floatValue!!)
- }
- // ... Double '...'
- else if (doubleValue != null) {
- node[scalarName(line, "Double")] = DoubleNode(doubleValue!!)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement