From ea6dabdd780e0884bff2fc9554e8eb18673cf97c Mon Sep 17 00:00:00 2001
From: Benjamin Collins <kion@dashgl.com>
Date: Tue, 11 Aug 2020 16:21:09 +0900
Subject: [PATCH] chunk parse

---
 NinjaModel.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 81 insertions(+), 3 deletions(-)

diff --git a/NinjaModel.py b/NinjaModel.py
index 6a478db..2aa9384 100644
--- a/NinjaModel.py
+++ b/NinjaModel.py
@@ -202,6 +202,7 @@ class NinjaModel: # {
 			}
 			self.bone.apply(vertex['pos'])
 			self.index_lookup[vertex_ofs] = len(self.vertex_list)
+			vertex_ofs += 1
 			self.vertex_list.append(vertex)
 
 		pos = self.file.tell()
@@ -232,6 +233,9 @@ class NinjaModel: # {
 
 			if chunk_head == 255:
 				break
+			elif chunk_head == 0:
+				print("Null Chunk")
+				continue
 			elif chunk_head >= 17 and chunk_head <= 23:
 				print("Chunk: Material")
 				bytes = self.file.read(2)
@@ -284,8 +288,11 @@ class NinjaModel: # {
 				filter_sample = chunk_body >> 14 & 0x03
 			elif chunk_head >= 64 and chunk_head <= 66:
 				print("Chunk Strip!!!")
-				bytes = self.file.read(2)
-				chunk_body = struct.unpack('H', bytes)[0]
+
+				bytes = self.file.read(4)
+				h = struct.unpack('HH', bytes)
+				chunk_len = h[0] * 2
+				chunk_body = h[1]
 
 				ignore_light = chunk_flag & 0x01
 				ignore_specular = chunk_flag & 0x02
@@ -299,10 +306,81 @@ class NinjaModel: # {
 				user_offset = chunk_body >> 14
 				print("Strip count: %d" % strip_count)
 
-				break
+				for i in range (strip_count):
+
+					bytes = self.file.read(2)
+					strip_len = struct.unpack('h', bytes)[0]
+					counter_clockwise = strip_len < 0
+					strip_len = abs(strip_len)
+					strip = []
+
+					for k in range (strip_len):
+						bytes = self.file.read(2)
+						index = struct.unpack('H', bytes)[0]
+						index = self.index_lookup[index]
+
+						if chunk_head == 64:
+							strip.append({
+								'index' : index
+							})
+							continue
+
+						bytes = self.file.read(4)
+						uv = struct.unpack('hh', bytes)
+						u = uv[0]
+						v = uv[1]
+
+						if chunk_head == 65:
+							u = u / 255.0
+							v = v / 255.0
+						elif chunk_head == 66:
+							u = u / 1023.0
+							v = v / 1023.0
+
+						strip.append({
+							'index' : index,
+							'uv' : [ u, v ]
+						})
+
+					for i in range(len(strip) - 2): #{
+						
+						if i % 2 == 0 and counter_clockwise:
+							a = strip[i + 0]
+							b = strip[i + 1]
+							c = strip[i + 2]
+						else :
+							a = strip[i + 1]
+							b = strip[i + 0]
+							c = strip[i + 2]
+						
+						va = self.vertex_list[a['index']]
+						vb = self.vertex_list[a['index']]
+						vc = self.vertex_list[a['index']]
+
+						self.face_list.append({
+							'index' : (
+								a['index'],
+								b['index'],
+								b['index']
+							),
+							'norm' : (
+								va['norm'],
+								vb['norm'],
+								vc['norm']
+							),
+							uv : (
+								a['uv'],
+								b['uv'],
+								c['uv']
+							)
+						})
+				# }
+
+				print("Strip End: 0x%08x" % self.file.tell())
 			else:
 				print("Unknown Chunk Type %d" % chunk_head)
 				print("Unknown Chunk Flag %d" % chunk_flag)
+				print("Unknown File Position: 0x%08x" % self.file.tell())
 				break
 
 			#end While