Advertisement
tomdodd4598

Untitled

Aug 6th, 2023
1,642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.91 KB | None | 0 0
  1. class WorldObjectParser(lines: Iterable<String>) : Parser(lines) {
  2.  
  3.     constructor(file: File) : this(file.readLines())
  4.  
  5.     override fun parse(lineIter: Iterator<String>) {
  6.         fun nextLine() = lineIter.next()
  7.  
  8.         fun stringValue(line: String): String {
  9.             val split = line.split("'")
  10.             return split[split.size - 2]
  11.         }
  12.  
  13.         fun floatTupleSplit(line: String, type: String): Pair<String, Sequence<Float>> {
  14.             val (name, str) = line.splitToSequence(type).map { it.trim() }.toList()
  15.             return Pair(name, str.split("(", ")")[1].splitToSequence(",").map{ it.toFloat() })
  16.         }
  17.  
  18.         fun scalarName(line: String, type: String) = line.split(type)[0].trim()
  19.  
  20.         fun parseNextCompound(inner: CompoundNode) {
  21.             nextLine() // {
  22.             stack.push(inner)
  23.             parse(lineIter)
  24.             stack.pop()
  25.         }
  26.  
  27.         val node = stack.peek()
  28.  
  29.         while (lineIter.hasNext()) {
  30.             val line = nextLine()
  31.             if (line == "}") {
  32.                 break
  33.             }
  34.  
  35.             val splitWhitespace = line.splitByWhitespace()
  36.             val firstWord = splitWhitespace.first()
  37.             val lastWord = splitWhitespace.last()
  38.  
  39.             val boolValue by lazy { lastWord.lowercase().toBooleanStrictOrNull() }
  40.             val intValue by lazy { lastWord.toIntOrNull() }
  41.             val floatValue by lazy { lastWord.toFloatOrNull() }
  42.             val doubleValue by lazy { lastWord.toDoubleOrNull() }
  43.  
  44.             fun putConstData(): ConstDataNode {
  45.                 val name = stringValue(line)
  46.                 val constData = ConstDataNode()
  47.                 constData.def[firstWord] = StringNode(name)
  48.                 node[name] = constData
  49.                 return constData
  50.             }
  51.  
  52.             fun parseConstData(constData: ConstDataNode) {
  53.                 nextLine() // ... ConstData
  54.                 parseNextCompound(constData.inner)
  55.             }
  56.  
  57.             // Type String '...'
  58.             if (firstWord == "Type") {
  59.                 // TODO: (World) Handle World Rules
  60.                 parseConstData(putConstData())
  61.             }
  62.  
  63.             // Definition String '...'
  64.             else if (firstWord == "Definition") {
  65.                 val constData = putConstData()
  66.                 val entity = stringValue(nextLine())
  67.                 constData.def["EntityType"] = StringNode(entity)
  68.                 if (entity.isNotEmpty()) {
  69.                     constData.def["FactoryType"] = StringNode(stringValue(nextLine()))
  70.                     parseConstData(constData)
  71.                 }
  72.             }
  73.  
  74.             /* TODO:
  75.                 (WorldObject)
  76.                 ... Mesh Attribute Manager
  77.                 ... GUIInfo Chunk
  78.                 ... Lightning Damage Potential
  79.                 ... Wind Damage Potential
  80.                 ... Meteor Damage Potential
  81.                 ... ImpactSoundsHull Wood
  82.                 ... ImpactSoundsHull ReInforced Wood
  83.                 ... ImpactSoundsHull Iron
  84.                 ... ImpactSoundsSail Cloth
  85.                 ... ImpactSoundsWall Stone
  86.                 ... ImpactSoundsDragon Scale
  87.             */
  88.  
  89.             else if (line.contains("Matrix33")) {
  90.                 val (name, floats) = floatTupleSplit(line, "Matrix33")
  91.                 val rows = floats.chunked(3).map { it.toFloatArray() }.toList()
  92.                 node[name] = MatrixNode(floatMatrixOf(rows[0], rows[1], rows[2]))
  93.             }
  94.  
  95.             else if (line.contains("Colour")) {
  96.                 val (name, floats) = floatTupleSplit(line, "Colour")
  97.                 node[name] = VectorNode(floats.toList().toFloatArray())
  98.             }
  99.  
  100.             else if (line.contains("Vector3")) {
  101.                 val (name, floats) = floatTupleSplit(line, "Vector3")
  102.                 node[name] = VectorNode(floats.toList().toFloatArray())
  103.             }
  104.  
  105.             else if (line.contains("Coord")) {
  106.                 val (name, floats) = floatTupleSplit(line, "Coord")
  107.                 node[name] = CoordNode(floats.toList().toFloatArray())
  108.             }
  109.  
  110.             // ... String '...'
  111.             else if (lastWord.contains("'")) {
  112.                 node[scalarName(line, "String")] = StringNode(stringValue(line))
  113.             }
  114.  
  115.             // ... Bool '...'
  116.             else if (boolValue != null) {
  117.                 node[scalarName(line, "Bool")] = BoolNode(boolValue!!)
  118.             }
  119.  
  120.             // ... Int '...'
  121.             else if (intValue != null) {
  122.                 node[scalarName(line, "Int")] = IntNode(intValue!!)
  123.             }
  124.  
  125.             // ... Float '...'
  126.             else if (floatValue != null) {
  127.                 node[scalarName(line, "Float")] = FloatNode(floatValue!!)
  128.             }
  129.  
  130.             // ... Double '...'
  131.             else if (doubleValue != null) {
  132.                 node[scalarName(line, "Double")] = DoubleNode(doubleValue!!)
  133.             }
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement