Advertisement
Guest User

Untitled

a guest
Jan 6th, 2017
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #Noesis Python model import+export test module, imports/exports some data from/to a made-up format
  2.  
  3. from inc_noesis import *
  4. from array import array
  5.  
  6. import noesis
  7.  
  8. #rapi methods should only be used during handler callbacks
  9. import rapi
  10.  
  11. #registerNoesisTypes is called by Noesis to allow the script to register formats.
  12. #Do not implement this function in script files unless you want them to be dedicated format modules!
  13. def registerNoesisTypes():
  14. handle = noesis.register("RAGE PS3 Cell drawable", ".cdr")
  15. noesis.setHandlerTypeCheck(handle, psaCheckType)
  16. noesis.setHandlerLoadModel(handle, psaLoadModel) #see also noepyLoadModelRPG
  17. #noesis.setHandlerWriteModel(handle, noepyWriteModel)
  18. #noesis.setHandlerWriteAnim(handle, noepyWriteAnim)
  19. noesis.logPopup()
  20. return 1
  21.  
  22. #check if it's this type based on the data
  23.  
  24. def psaCheckType(data):
  25. bs = NoeBitStream(data)
  26. bs.setEndian(NOE_BIGENDIAN)
  27. IdMagic = bs.readInt()
  28. print("UdMagic: " + hex(IdMagic))
  29. if IdMagic != 0x4C5B8100:
  30. return 0
  31. return 1
  32.  
  33. def removeCPUAddress(data):
  34. address = data-0x50000000
  35. return address
  36.  
  37. def removeGPUAddress(data):
  38. address = (data-0x60000000)+0x3000
  39. return address
  40.  
  41. #load the model
  42. def psaLoadModel(data, mdlList):
  43. f = open('index-data-decompressed.block', 'wb+')
  44. ctx = rapi.rpgCreateContext()
  45. bs = NoeBitStream(data)
  46. bs.setEndian(NOE_BIGENDIAN)
  47. rapi.rpgSetOption(noesis.RPGOPT_BIGENDIAN, 1)
  48. rapi.rpgSetOption(noesis.RPGOPT_TRIWINDBACKWARD, 1)
  49. fvfTableOffset = 0x1D10
  50. bs.seek(fvfTableOffset, NOESEEK_ABS)
  51. bs.seek(0x08, NOESEEK_REL)
  52. vertexCount = bs.readUShort()
  53. indexCount = bs.readUShort()
  54. print('Vertex Count: ' + str(vertexCount))
  55. print('Index Count: ' + str(indexCount))
  56. bs.seek(0x04, NOESEEK_REL)
  57. indexOffset = bs.readUInt()
  58. indexOffset = removeCPUAddress(indexOffset)
  59. print('Index Offset: ' + hex(indexOffset))
  60. indexSize = bs.readUShort()
  61. print('index Size: ' + hex(indexSize))
  62. unk01 = bs.readUShort()
  63. unkOffset = bs.readUInt()
  64. unkOffset = removeCPUAddress(unkOffset)
  65. print('Unknown Offset: ' + hex(unkOffset))
  66. unkown01Offset = bs.readUInt()
  67. unkSize = bs.readUShort()
  68. print('Unknown Size: ' + hex(unkSize))
  69. bs.seek(0xA, NOESEEK_REL)
  70. vertexDataOffset = bs.readUInt()
  71. vertexDataOffset = removeGPUAddress(vertexDataOffset)
  72. print('Vertex Offset: ' + hex(vertexDataOffset))
  73. vertexDataSize = bs.readUInt()
  74. print('Vertex Data Size: ' + hex(vertexDataSize))
  75.  
  76. bs.seek(indexOffset, NOESEEK_ABS)
  77. edgeData = bs.readBytes(indexSize)
  78. edgeDecomp = rapi.decompressEdgeIndices(edgeData, indexCount)
  79. for i in range(0, indexCount): #decompressEdgeIndices returns indices in little-endian, so swap back to big because rpg is in bigendian mode
  80. t = edgeDecomp[i*2]
  81. edgeDecomp[i*2] = edgeDecomp[i*2 + 1]
  82. edgeDecomp[i*2 + 1] = t
  83. # Need to know where are vertex really located we have two options: VertexDataOffset and
  84. # VertexOffset according to the decompressed index there are 72x verts so vert count is 2D6=726 and
  85. # with (vert count * 16) in asteroid.cdr size should 0x2D60 that even corresponds to the length of
  86. # the vertexData block that we are getting on vertexDataOffset starting.
  87. f.write(edgeDecomp)
  88. bs.seek(vertexDataOffset, NOESEEK_ABS)
  89. #vertexSize = 0x10 * vertexCount
  90. print('Vertex Size: ' + hex(vertexDataSize))
  91. vertbuff = bs.readBytes(vertexDataSize)
  92. print('vertbuff Size: ' + hex(len(vertbuff)))
  93. rapi.rpgBindPositionBufferOfs(vertbuff, noesis.RPGEODATA_FLOAT, 0x10, 0)
  94. rapi.rpgSetName(str(i))
  95. #rapi.rpgCommitTriangles(None, noesis.RPGEODATA_USHORT, (vertexSize // 16), noesis.RPGEO_POINTS, 1)
  96. rapi.rpgCommitTriangles(edgeDecomp, noesis.RPGEODATA_USHORT,vertexCount, noesis.RPGEO_TRIANGLE, 1)
  97. mdl = rapi.rpgConstructModel()
  98. mdlList.append(mdl)
  99. f.close()
  100. rapi.rpgClearBufferBinds()
  101. return 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement