westwood_lib 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b0beb28de31c8bb1b6c1cf1b212e5d40943d4cda81160ec4865583607a3e026e
4
- data.tar.gz: ce64840e6061eb2d24bccba0c5c177b3ae726baa3f70134f60715dce112f0198
3
+ metadata.gz: 8af5ddff1f13dc74d25533be3d887fa4c2330373d37696de7915780e674756c3
4
+ data.tar.gz: 26e57c874beb96556cfdc63f2973fc2aa38502c13b5c8e0b0796ec238234a916
5
5
  SHA512:
6
- metadata.gz: d09073c3213c85add16ba846c490ab5aea66cfd04721ddb52bed86d34e1ea91d299886e57ce2c01fc27a7d086013fd1ec1692098557bb01ff9f7e75ab42bf4c4
7
- data.tar.gz: 1eb37dfad92f6989cb8f1945b38f411d22b38eb0648eb56a34da2ef83ba7d7a489517683d0e9a82cea675ec8f9132631f46bc8d2aa9e6a11fc70922213350bcc
6
+ metadata.gz: 8824175875b75fa083b7b132f04afef2114a197e71ea814eefd094c9dc08be7999313d6c5fcb1f7e5f07d9ced6a639de9e38e3afc9b8f78773bf2a18bda07236
7
+ data.tar.gz: 528b982dd74118403d8100977cfecb1b3b6726bd1909b922a679f55f919b95c69a41c1379c4b2943e95ddd8cb03ab9c03f376f9f9cd2415933989aadcc253f1e
data/README.md CHANGED
@@ -18,7 +18,41 @@ gem install westwood_lib
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ ### WWMix
22
+ Read and write to westwood's MIX1 archive format
23
+ ```ruby
24
+ require "westwood_lib"
25
+
26
+ mix = WestwoodLib::WWMix.new(path: "/path/to/file.mix")
27
+ mix.load
28
+
29
+ pp mix.entries
30
+ ```
31
+
32
+ ### WWTranslateDB
33
+ Read contents of "strings.tdb"
34
+ ```ruby
35
+ require "westwood_lib"
36
+
37
+ File.open("/path/to/strings.tdb") do |io|
38
+ db = WestwoodLib::WWTranslateDB.new(io: io)
39
+ db.load
40
+
41
+ pp db.categories
42
+ ```
43
+
44
+ ### WWW3D
45
+ Load contents of .w3d files (meshes, pivots, etc.)
46
+ ```ruby
47
+ require "westwood_lib"
48
+
49
+ File.open("/path/to/file.w3d") do |io|
50
+ w3d = WestwoodLib::WWW3D.new(io: io)
51
+ w3d.load
52
+
53
+ pp w3d.meshes.count
54
+ end
55
+ ```
22
56
 
23
57
  ## Development
24
58
 
@@ -26,5 +26,30 @@ module WestwoodLib
26
26
 
27
27
  @entries.freeze
28
28
  end
29
+
30
+ def find(entry_name)
31
+ entries = select(entry_name)
32
+
33
+ # TODO: Check that we've selected the "correct" entry
34
+ # in this order (i.e. loose files, then reverse alphabetical*):
35
+ # ["loose_file.w3d", "C&C_MAP.mix", "Always3.dat", "Always2.dat", "Always.dat"]
36
+ return entries&.last
37
+ end
38
+
39
+ def select(entry_name)
40
+ @entries.find { |k, v| k == entry_name.upcase }&.last
41
+ end
42
+
43
+ def find_proxy(pivot_name)
44
+ w3d_name = format("%s.W3D", pivot_name.split("~", 2).first)
45
+
46
+ find(w3d_name)
47
+ end
48
+
49
+ def select_proxy(pivot_name)
50
+ w3d_name = format("%s.W3D", pivot_name.split("~", 2).first)
51
+
52
+ select(w3d_name)
53
+ end
29
54
  end
30
55
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WestwoodLib
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -0,0 +1,380 @@
1
+ require "stringio"
2
+
3
+ module WestwoodLib
4
+ # Read Westwood's chunk based file formats
5
+ class WWChunkFile
6
+ SIZEOF_CHUNK_HEADER = 8
7
+ SIZEOF_MICROCHUNK_HEADER = 2
8
+
9
+ W3D_CHUNK_ENUMS = {}
10
+
11
+ Chunk = Struct.new(
12
+ :id, # uint32_t
13
+ :size, # uint32_t
14
+ :chunk_end # added helper: uint32_t
15
+ )
16
+
17
+ def initialize(io:)
18
+ @io = io
19
+ @io_size = @io.size
20
+
21
+ # keeps track of active chunks
22
+ @chunks = []
23
+ # keeps track of how much of a chunk has been read/written
24
+ @chunk_position = []
25
+ # index of active chunk
26
+ @chunk_index = 0
27
+
28
+ # chunk header for active micro chunk
29
+ @micro_chunk = nil
30
+ # keep track how much of a micro chunk has been read/written
31
+ @micro_chunk_position = 0
32
+ end
33
+
34
+ # naive helper function
35
+ def each_chunk(&block)
36
+ while (open_chunk)
37
+ block&.call(current_chunk)
38
+
39
+ close_chunk
40
+ end
41
+ end
42
+
43
+ # Mimic C API
44
+ def open_chunk(&block)
45
+ raise "A micro chunk is open! Cannot read next chunk until micro chunk is closed." if @micro_chunk
46
+
47
+ # Return false if the parent chunk has been completely eaten and so there are no more subchunks to open.
48
+ return false if @chunk_index > 0 && current_chunk_position == current_chunk_length
49
+
50
+ # Return false if we're unable to read the required number of bytes for chunk header
51
+ return false if @io_size - @io.pos < SIZEOF_CHUNK_HEADER
52
+
53
+ @chunks[@chunk_index] = read_chunk_header
54
+
55
+ @chunk_position[@chunk_index] = 0
56
+ @chunk_index += 1
57
+
58
+ if block_given?
59
+ block.call
60
+
61
+ close_chunk
62
+ end
63
+
64
+ true
65
+ end
66
+
67
+ def close_chunk
68
+ raise "A micro chunk is open! Cannot close chunk until micro chunk is closed." if @micro_chunk
69
+
70
+ current_size = current_chunk_length
71
+
72
+ if (current_chunk_position < current_size)
73
+ self.raw_pos = current_chunk.chunk_end
74
+ end
75
+
76
+ @chunk_index -= 1
77
+
78
+ if @chunk_index.positive?
79
+ self.current_chunk_position += current_size + SIZEOF_CHUNK_HEADER
80
+ end
81
+
82
+ true
83
+ end
84
+
85
+ def open_micro_chunk(&block)
86
+ raise "A micro chunk is already open! Only one may be open at a time." if @micro_chunk
87
+
88
+ return false unless readable?(SIZEOF_MICROCHUNK_HEADER)
89
+
90
+ @micro_chunk = read_micro_chunk_header
91
+ @micro_chunk_position = 0
92
+
93
+ if block_given?
94
+ block.call
95
+
96
+ close_micro_chunk
97
+ end
98
+
99
+ true
100
+ end
101
+
102
+ def close_micro_chunk
103
+ raise "No micro chunk currently open" unless @micro_chunk
104
+
105
+ if current_micro_chunk_position < current_micro_chunk_length
106
+ self.raw_pos = @micro_chunk.chunk_end
107
+
108
+ # Update position in normal chunk
109
+ if @chunk_index.positive?
110
+ self.current_chunk_position += current_micro_chunk_length - current_micro_chunk_position
111
+ end
112
+ end
113
+
114
+ @micro_chunk = nil
115
+ @micro_chunk_position = 0
116
+
117
+ true
118
+ end
119
+
120
+ # TODO: writing api
121
+ def begin_chunk(id)
122
+ raise NotImplementedError
123
+ end
124
+
125
+ # TODO: writing api
126
+ def end_chunk
127
+ raise NotImplementedError
128
+ end
129
+
130
+ # TODO: writing api
131
+ def begin_micro_chunk(id)
132
+ raise "micro chunk id value must not exceed 255 (uint8_t)" unless id <= 255
133
+
134
+ raise NotImplementedError
135
+ end
136
+
137
+ # TODO: writing api
138
+ def end_micro_chunk
139
+ raise NotImplementedError
140
+ end
141
+
142
+ def current_chunk
143
+ raise "No active chunk" unless @chunk_index >= 1
144
+
145
+ @chunks[@chunk_index - 1]
146
+ end
147
+
148
+ def current_chunk_id
149
+ raise "No active chunk" unless @chunk_index >= 1
150
+
151
+ @chunks[@chunk_index - 1].id
152
+ end
153
+
154
+ def current_chunk_length
155
+ raise "No active chunk" unless @chunk_index >= 1
156
+
157
+ @chunks[@chunk_index - 1].size
158
+ end
159
+
160
+ def current_chunk_position
161
+ raise "No active chunk" unless @chunk_index >= 1
162
+
163
+ @chunk_position[@chunk_index - 1]
164
+ end
165
+
166
+ def current_chunk_position=(n)
167
+ raise "No active chunk" unless @chunk_index >= 1
168
+
169
+ @chunk_position[@chunk_index - 1] = n
170
+ end
171
+
172
+ def current_chunk_depth
173
+ @chunk_index
174
+ end
175
+
176
+ def current_micro_chunk
177
+ raise "No active micro chunk" unless @micro_chunk
178
+
179
+ @micro_chunk
180
+ end
181
+
182
+ def current_micro_chunk_id
183
+ raise "No active micro chunk" unless @micro_chunk
184
+
185
+ @micro_chunk.id
186
+ end
187
+
188
+ def current_micro_chunk_length
189
+ raise "No active micro chunk" unless @micro_chunk
190
+
191
+ @micro_chunk.size
192
+ end
193
+
194
+ def current_micro_chunk_position
195
+ raise "No active micro chunk" unless @micro_chunk
196
+
197
+ @micro_chunk_position
198
+ end
199
+
200
+ def debug_chunk(enum_hash = self.class::W3D_CHUNK_ENUMS)
201
+ data = [:chunk, enum_hash[current_chunk_id] || current_chunk_id, @chunk_index, current_chunk_position, current_chunk_length, @io.pos]
202
+ format("%s%s", " " * @chunk_index, data.join(", "))
203
+ end
204
+
205
+ def debug_micro_chunk(enum_hash = self.class::W3D_CHUNK_ENUMS)
206
+ data = [:micro_chunk, enum_hash[current_micro_chunk_id] || current_micro_chunk_id, @chunk_index, current_micro_chunk_position, current_micro_chunk_length, @io.pos]
207
+ format("%s%s", " " * (@chunk_index + 1), data.join(", "))
208
+ end
209
+
210
+ def pos
211
+ @io.pos
212
+ end
213
+
214
+ # bypass chunk and micro chunk boundry checks
215
+ def raw_pos=(n)
216
+ @io.pos = n
217
+ end
218
+
219
+ def pos=(n)
220
+ @io.pos = n
221
+ end
222
+
223
+ def size
224
+ @io_size
225
+ end
226
+
227
+ def read_chunk_header
228
+ Chunk.new(
229
+ raw_read_uint32,
230
+ chunk_size = (raw_read_uint32 & 0x7FFFFFFF),
231
+ @io.pos + chunk_size
232
+ )
233
+ end
234
+
235
+ def read_micro_chunk_header
236
+ Chunk.new(
237
+ read_uint8,
238
+ chunk_size = read_uint8,
239
+ @io.pos + chunk_size
240
+ )
241
+ end
242
+
243
+ # returns true if the current chunk or micro chunk can have `length` bytes read without exceeding its length.
244
+ def readable?(length)
245
+ return false unless @chunk_index >= 1
246
+
247
+ if current_chunk_position + length > current_chunk_length
248
+ return false
249
+ end
250
+
251
+ if @micro_chunk && current_micro_chunk_position + length > current_micro_chunk_length
252
+ return false
253
+ end
254
+
255
+ if @io.pos + length > @io_size
256
+ return false
257
+ end
258
+
259
+ true
260
+ end
261
+
262
+ def read(length)
263
+ raise "No active chunk" unless @chunk_index >= 1
264
+
265
+ # DEBUG
266
+ # pp [current_chunk_position, current_chunk_length, length]
267
+
268
+ if current_chunk_position + length > current_chunk_length
269
+ raise "Requested read (#{length} bytes) would exceed current chunk size"
270
+ end
271
+
272
+ if @micro_chunk && current_micro_chunk_position + length > current_micro_chunk_length
273
+ raise "Requested read (#{length} bytes) would exceed current micro chunk size"
274
+ end
275
+
276
+ if @io.pos + length > @io_size
277
+ raise "Requested read (#{length} bytes) would exceed IO size"
278
+ end
279
+
280
+ self.current_chunk_position += length
281
+
282
+ if @micro_chunk
283
+ @micro_chunk_position += length
284
+ end
285
+
286
+ @io.read(length)
287
+ end
288
+
289
+ def read_fixed_string(length)
290
+ @io.read(length).unpack1("Z*").force_encoding("UTF-8")
291
+ end
292
+
293
+ def read_fixed_wide_string(length)
294
+ @io.read(length).unpack1("A*").force_encoding("UTF-16")
295
+ end
296
+
297
+ # Search ahead for null byte then reset cursor and read string contents
298
+ def read_string
299
+ pos = @io.pos
300
+
301
+ while (byte = @io.getbyte)
302
+ break if byte.zero?
303
+
304
+ raise "Unexpected EOL" unless byte
305
+ end
306
+
307
+ string_length = @io.pos - pos
308
+
309
+ # reset cursor to initial position
310
+ @io.pos = pos
311
+
312
+ read_fixed_string(string_length - 1)
313
+ end
314
+
315
+ def read_uint8
316
+ read(1).unpack1("C")
317
+ end
318
+
319
+ def read_int8
320
+ read(1).unpack1("c")
321
+ end
322
+
323
+ def read_uint16
324
+ read(2).unpack1("S")
325
+ end
326
+
327
+ def read_int16
328
+ read(2).unpack1("s")
329
+ end
330
+
331
+ # Read directly from IO without tracking read bytes
332
+ def raw_read_uint32
333
+ @io.read(4).unpack1("L<")
334
+ end
335
+
336
+ def read_uint32
337
+ read(4).unpack1("L<")
338
+ end
339
+
340
+ def read_uint32_array(count)
341
+ read(4 * count).unpack("L<#{count}")
342
+ end
343
+
344
+ def read_int32
345
+ read(4).unpack1("l<")
346
+ end
347
+
348
+ def read_float
349
+ read(4).unpack1("e")
350
+ end
351
+
352
+ def read_vector2f
353
+ read(8).unpack("e2")
354
+ end
355
+
356
+ def read_vector3f
357
+ read(12).unpack("e3")
358
+ end
359
+
360
+ def read_vector4f
361
+ read(16).unpack("e4")
362
+ end
363
+
364
+ def read_vector4_uint8
365
+ read(4).unpack("C4")
366
+ end
367
+
368
+ def read_rgb
369
+ r, g, b, a = read_vector4_uint8
370
+
371
+ WWW3D::RGBA.new(r, g, b, nil)
372
+ end
373
+
374
+ def read_rgba
375
+ r, g, b, a = read_vector4_uint8
376
+
377
+ WWW3D::RGBA.new(r, g, b, a)
378
+ end
379
+ end
380
+ end
@@ -0,0 +1,250 @@
1
+ require_relative "ww_chunk_file"
2
+
3
+ module WestwoodLib
4
+ class WWTranslateDB < WWChunkFile
5
+ LANG_ID_ENGLISH = 0
6
+ LANG_ID_FRENCH = 1
7
+ LANG_ID_GERMAN = 2
8
+ LANG_ID_SPANISH = 3
9
+ LANG_ID_CHINESE = 4
10
+ LANG_ID_JAPANESE = 5
11
+ LANG_ID_KOREAN = 6
12
+
13
+ CHUNK_ID_TRANSLATE_DB = 0x00090000
14
+ CHUNK_ID_TRANSLATE_OBJ = 0x00090001
15
+ CHUNK_ID_TDBCATEGORY = 0x00090002
16
+ CHUNK_ID_STRING_TWIDDLER = 0x00090003
17
+
18
+ CHUNK_ID_VARIABLES = 0x07141200
19
+ CHUNK_ID_OBJECTS = 0x07141201
20
+ CHUNK_ID_CATEGORIES = 0x07141202
21
+
22
+ SIMPLEFACTORY_CHUNK_ID_OBJ_POINTER = 0x00100100
23
+ SIMPLEFACTORY_CHUNK_ID_OBJ_DATA = 0x00100101
24
+
25
+ CATEGORY_CHUNK_ID_VARIABLES = 0x11221022
26
+ CATEGORY_CHUNK_ID_BASE_CLASS = 0x11221023
27
+
28
+ TRANSLATEOBJ_CHUNK_ID_VARIABLES = 0x06141108
29
+ TRANSLATEOBJ_CHUNK_ID_BASE_CLASS = 0x06141109
30
+ TRANSLATEOBJ_CHUNK_ID_ENGLISH_STRING = 0x0614110A
31
+ TRANSLATEOBJ_CHUNK_ID_TRANSLATED_STRING = 0x0614110B
32
+
33
+ STRINGTWIDDLER_CHUNK_ID_VARIABLES = 0x07310319
34
+ STRINGTWIDDLER_CHUNK_ID_BASE_CLASS = 0x0731031A
35
+
36
+ VAR_ID_ID = 1
37
+ VAR_ID_NAME = 2
38
+
39
+ VAR_ID_VERSION_NUMBER = 1
40
+ VAR_ID_LANGUAGE_ID = 2
41
+
42
+ VAR_ID_ID_DESC = 2
43
+ VAR_ID_STRING = 3
44
+ VAR_ID_ENGLISH_STRING = 4
45
+ VAR_ID_SOUND_ID = 5
46
+ VAR_ID_ANIMATION_NAME = 6
47
+ VAR_ID_CATEGORY_ID = 7
48
+
49
+ # generate "enums" from constants
50
+ W3D_CHUNK_ENUMS = constants.select { |sym| sym.to_s.include?("CHUNK_ID_") }.map { |sym| [const_get(sym), sym] }.to_h
51
+ W3D_LANGUAGE_ENUMS = constants.select { |sym| sym.to_s.start_with?("LANG_ID_") }.map { |sym| [const_get(sym), sym] }.to_h
52
+ W3D_VARIABLE_ENUMS = constants.select { |sym| sym.to_s.start_with?("VAR_ID_") }.map { |sym| [const_get(sym), sym] }.to_h
53
+
54
+ TranslateObject = Struct.new(
55
+ :english_string, # string
56
+ :id_description, # string
57
+ :id, # uint32_t
58
+ :sound_id, # uint32_t
59
+ :category_id, # uint32_t
60
+ :animation_name, # string
61
+ :translated_strings # string[]
62
+ )
63
+
64
+ attr_reader :categories, :objects
65
+
66
+ def initialize(io:)
67
+ super
68
+
69
+ @version_number = -1
70
+ @language_id = -1
71
+
72
+ @categories = []
73
+ @objects = []
74
+ end
75
+
76
+ def load
77
+ # Jump ahead of unknown non chunk data header?
78
+ # @io.pos = 21
79
+
80
+ open_chunk do
81
+ raise "Unexpected chunk type: #{current_chunk_id}, expected: #{CHUNK_ID_TRANSLATE_DB}" unless current_chunk_id == CHUNK_ID_TRANSLATE_DB
82
+
83
+ puts debug_chunk if WESTWOOD_LIB_DEBUG
84
+
85
+ while (open_chunk)
86
+ case current_chunk_id
87
+ when CHUNK_ID_VARIABLES
88
+ puts debug_chunk if WESTWOOD_LIB_DEBUG
89
+ parse_variables
90
+ when CHUNK_ID_OBJECTS
91
+ puts debug_chunk if WESTWOOD_LIB_DEBUG
92
+ parse_objects
93
+ when CHUNK_ID_CATEGORIES
94
+ puts debug_chunk if WESTWOOD_LIB_DEBUG
95
+ parse_categories
96
+ else
97
+ puts "? #{debug_chunk.sub(" ", "")}" if WESTWOOD_LIB_DEBUG
98
+ end
99
+
100
+ close_chunk
101
+ end
102
+ end
103
+
104
+ true
105
+ end
106
+
107
+ def parse_categories
108
+ while (open_chunk)
109
+ puts debug_chunk if WESTWOOD_LIB_DEBUG
110
+
111
+ case current_chunk_id
112
+ when CHUNK_ID_TDBCATEGORY
113
+ while(open_chunk)
114
+ case current_chunk_id
115
+ when SIMPLEFACTORY_CHUNK_ID_OBJ_POINTER
116
+ when SIMPLEFACTORY_CHUNK_ID_OBJ_DATA
117
+ puts debug_chunk if WESTWOOD_LIB_DEBUG
118
+
119
+ parse_category
120
+ else
121
+ puts "?++ #{debug_chunk.sub(" ", "")}" if WESTWOOD_LIB_DEBUG
122
+ end
123
+
124
+ close_chunk
125
+ end
126
+ else
127
+ puts "?+ #{debug_chunk.sub(" ", "")}" if WESTWOOD_LIB_DEBUG
128
+ end
129
+
130
+ close_chunk
131
+ end
132
+ end
133
+
134
+ def parse_category
135
+ while(open_chunk)
136
+ case current_chunk_id
137
+ when CATEGORY_CHUNK_ID_BASE_CLASS
138
+ puts debug_chunk if WESTWOOD_LIB_DEBUG
139
+ when CATEGORY_CHUNK_ID_VARIABLES
140
+ puts debug_chunk if WESTWOOD_LIB_DEBUG
141
+
142
+ category_id = -1
143
+ open_micro_chunk do
144
+ puts debug_micro_chunk if WESTWOOD_LIB_DEBUG
145
+ category_id = read_uint32
146
+ end
147
+
148
+ # category name
149
+ open_micro_chunk do
150
+ puts debug_micro_chunk if WESTWOOD_LIB_DEBUG
151
+ category_name = read_fixed_string(current_micro_chunk_length)
152
+ @categories[category_id] = category_name
153
+ end
154
+ else
155
+ puts "?+++ #{debug_chunk.sub(" ", "")}" if WESTWOOD_LIB_DEBUG
156
+ end
157
+
158
+ close_chunk
159
+ end
160
+ end
161
+
162
+ def parse_objects
163
+ while(open_chunk)
164
+ case current_chunk_id
165
+ when CHUNK_ID_TRANSLATE_OBJ
166
+ puts debug_chunk if WESTWOOD_LIB_DEBUG
167
+ while(open_chunk)
168
+ case current_chunk_id
169
+ when SIMPLEFACTORY_CHUNK_ID_OBJ_POINTER
170
+ when SIMPLEFACTORY_CHUNK_ID_OBJ_DATA
171
+ puts debug_chunk if WESTWOOD_LIB_DEBUG
172
+ parse_object
173
+ else
174
+ puts "?+++ #{debug_chunk.sub(" ", "")}" if WESTWOOD_LIB_DEBUG
175
+ end
176
+
177
+ close_chunk
178
+ end
179
+ else
180
+ puts "?++ #{debug_chunk.sub(" ", "")}" if WESTWOOD_LIB_DEBUG
181
+ end
182
+
183
+ close_chunk
184
+ end
185
+ end
186
+
187
+ def parse_object
188
+ translate_object = TranslateObject.new
189
+ translate_object.translated_strings ||= []
190
+
191
+ while(open_chunk)
192
+ case current_chunk_id
193
+ when TRANSLATEOBJ_CHUNK_ID_BASE_CLASS
194
+ when TRANSLATEOBJ_CHUNK_ID_VARIABLES
195
+ puts debug_chunk if WESTWOOD_LIB_DEBUG
196
+ while(open_micro_chunk)
197
+ case current_micro_chunk_id
198
+ when VAR_ID_ID
199
+ puts debug_micro_chunk(W3D_VARIABLE_ENUMS) if WESTWOOD_LIB_DEBUG
200
+ translate_object.id = read_uint32
201
+ when VAR_ID_ID_DESC
202
+ puts debug_micro_chunk(W3D_VARIABLE_ENUMS) if WESTWOOD_LIB_DEBUG
203
+ translate_object.id_description = read_fixed_string(current_micro_chunk_length)
204
+ when VAR_ID_ENGLISH_STRING
205
+ puts debug_micro_chunk(W3D_VARIABLE_ENUMS) if WESTWOOD_LIB_DEBUG
206
+ translate_object.english_string = read_fixed_string(current_micro_chunk_length)
207
+ when VAR_ID_SOUND_ID
208
+ puts debug_micro_chunk(W3D_VARIABLE_ENUMS) if WESTWOOD_LIB_DEBUG
209
+ translate_object.sound_id = read_uint32
210
+ when VAR_ID_ANIMATION_NAME
211
+ puts debug_micro_chunk(W3D_VARIABLE_ENUMS) if WESTWOOD_LIB_DEBUG
212
+ translate_object.animation_name = read_fixed_string(current_micro_chunk_length)
213
+ when VAR_ID_CATEGORY_ID
214
+ puts debug_micro_chunk(W3D_VARIABLE_ENUMS) if WESTWOOD_LIB_DEBUG
215
+ translate_object.category_id = read_uint32
216
+ else
217
+ puts "?+++ #{debug_chunk(W3D_VARIABLE_ENUMS).sub(" ", "")}" if WESTWOOD_LIB_DEBUG
218
+ end
219
+
220
+ close_micro_chunk
221
+ end
222
+ when TRANSLATEOBJ_CHUNK_ID_ENGLISH_STRING
223
+ puts debug_chunk if WESTWOOD_LIB_DEBUG
224
+ translate_object.english_string = read_fixed_string(current_chunk_length)
225
+ when TRANSLATEOBJ_CHUNK_ID_TRANSLATED_STRING
226
+ puts debug_chunk if WESTWOOD_LIB_DEBUG
227
+ translate_object.translated_strings << read_fixed_wide_string(current_chunk_length)
228
+ else
229
+ puts "?+++ #{debug_chunk.sub(" ", "")}" if WESTWOOD_LIB_DEBUG
230
+ end
231
+
232
+ close_chunk
233
+ end
234
+
235
+ @objects << translate_object
236
+ end
237
+
238
+ def parse_variables
239
+ open_micro_chunk do
240
+ puts debug_micro_chunk if WESTWOOD_LIB_DEBUG
241
+ @version_number = read_uint32
242
+ end
243
+
244
+ open_micro_chunk do
245
+ puts debug_micro_chunk if WESTWOOD_LIB_DEBUG
246
+ @language_id = read_uint32
247
+ end
248
+ end
249
+ end
250
+ end
@@ -0,0 +1,267 @@
1
+ require "stringio"
2
+
3
+ require_relative "../ww_w3d"
4
+
5
+ module WestwoodLib
6
+ # Loader for .w3d files
7
+ # See: https://github.com/w3dhub/OpenW3D/blob/main/Code/ww3d2/w3d_file.h
8
+ class WWW3D
9
+ class WWMesh
10
+ attr_reader :header, :material_info, :triangles, :vertices, :vertex_normals, :vertex_shade_indices,
11
+ :vertex_materials, :shaders, :textures, :material_passes
12
+
13
+ # separated, see TODO
14
+ attr_reader :aabb_header, :aabb_nodes, :aabb_polygon_indices
15
+
16
+ def initialize(io:, chunk:)
17
+ @io = io
18
+ @chunk = chunk
19
+
20
+ @header = MeshHeader.new
21
+ @material_info = MaterialInfo.new
22
+ @triangles = []
23
+ @vertices = []
24
+ @vertex_normals = []
25
+ @vertex_shade_indices = []
26
+ @vertex_materials = []
27
+ @shaders = []
28
+ @textures = []
29
+ @material_passes = []
30
+
31
+ # TODO: This should probably abstracted into its own AABBTree class
32
+ @aabb_header = nil
33
+ @aabb_nodes = []
34
+ @aabb_polygon_indices = []
35
+
36
+ load
37
+ end
38
+
39
+ def load
40
+ while (@io.open_chunk)
41
+ current_chunk = @io.current_chunk
42
+ puts format " :%s", W3D_CHUNK_ENUMS[current_chunk.id] || current_chunk.id if WESTWOOD_LIB_DEBUG
43
+
44
+ case @io.current_chunk.id
45
+ when W3D_CHUNK_MESH_HEADER3
46
+ parse_header
47
+ when W3D_CHUNK_VERTICES
48
+ parse_vertices
49
+ when W3D_CHUNK_VERTEX_NORMALS
50
+ parse_vertex_normals
51
+ when W3D_CHUNK_TRIANGLES
52
+ parse_triangles
53
+ when W3D_CHUNK_VERTEX_SHADE_INDICES
54
+ parse_vertex_shade_indices
55
+ when W3D_CHUNK_MATERIAL_INFO
56
+ parse_material_info
57
+ when W3D_CHUNK_VERTEX_MATERIALS
58
+ parse_vertex_materials
59
+ when W3D_CHUNK_SHADERS
60
+ parse_shaders
61
+ when W3D_CHUNK_TEXTURES
62
+ parse_textures
63
+ when W3D_CHUNK_MATERIAL_PASS # TODO
64
+ parse_material_pass
65
+ when W3D_CHUNK_AABTREE
66
+ parse_chunk_aabb_tree
67
+ end
68
+
69
+ @io.close_chunk
70
+ end
71
+
72
+ true
73
+ end
74
+
75
+ def parse_header
76
+ @header.version = @io.read_uint32
77
+ @header.attributes = @io.read_uint32
78
+ @header.mesh_name = @io.read_fixed_string(W3D_NAME_LENGTH)
79
+ @header.container_name = @io.read_fixed_string(W3D_NAME_LENGTH)
80
+ @header.triangle_count = @io.read_uint32
81
+ @header.vertice_count = @io.read_uint32
82
+ @header.material_count = @io.read_uint32
83
+ @header.damage_stages_count = @io.read_uint32
84
+ @header.sort_level = @io.read_int32
85
+ @header.prelit_version = @io.read_uint32
86
+ @header._reserved = @io.read_uint32
87
+ @header.vertex_channels = @io.read_uint32
88
+ @header.face_channels = @io.read_uint32
89
+ @header.min_corner = @io.read_vector3f
90
+ @header.max_corner = @io.read_vector3f
91
+ @header.sphere_center = @io.read_vector3f
92
+ @header.sphere_radius = @io.read_float
93
+
94
+ raise "Nope." unless @header._reserved.zero?
95
+ end
96
+
97
+ def parse_vertices
98
+ @header.vertice_count.times do |i|
99
+ @vertices << @io.read_vector3f
100
+ end
101
+ end
102
+
103
+ def parse_vertex_normals
104
+ @header.vertice_count.times do |i|
105
+ @vertex_normals << @io.read_vector3f
106
+ end
107
+ end
108
+
109
+ def parse_triangles
110
+ @header.triangle_count.times do |i|
111
+ @triangles << Triangle.new(
112
+ @io.read_uint32_array(3),
113
+ @io.read_uint32,
114
+ @io.read_vector3f,
115
+ @io.read_float,
116
+ )
117
+ end
118
+ end
119
+
120
+ def parse_vertex_shade_indices
121
+ @header.vertice_count.times do |i|
122
+ @vertex_shade_indices << @io.read_uint32
123
+ end
124
+ end
125
+
126
+ def parse_material_info
127
+ @material_info = MaterialInfo.new(
128
+ @io.read_uint32,
129
+ @io.read_uint32,
130
+ @io.read_uint32,
131
+ @io.read_uint32
132
+ )
133
+ end
134
+
135
+ def parse_vertex_materials
136
+ # loop through materials
137
+ @header.material_count.times do
138
+ @io.open_chunk do
139
+ material = VertexMaterial.new
140
+
141
+ # loop through material data
142
+ while(@io.open_chunk)
143
+ case @io.current_chunk_id
144
+ when W3D_CHUNK_VERTEX_MATERIAL_NAME
145
+ material.name = @io.read_string
146
+ when W3D_CHUNK_VERTEX_MATERIAL_INFO
147
+ material.attributes = @io.read_uint32
148
+ material.ambient = @io.read_rgb
149
+ material.diffuse = @io.read_rgb
150
+ material.specular = @io.read_rgb
151
+ material.emissive = @io.read_rgb
152
+ material.shininess = @io.read_float
153
+ material.opacity = @io.read_float
154
+ material.translucency = @io.read_float
155
+
156
+ @vertex_materials << material
157
+ end
158
+
159
+ @io.close_chunk
160
+ end
161
+ end
162
+ end
163
+ end
164
+
165
+ def parse_shaders
166
+ @material_info.shader_count.times do
167
+ shader = WWW3D::Shader.new
168
+
169
+ shader.depth_compare = @io.read_uint8
170
+ shader._depth_mask = @io.read_uint8
171
+ shader.color_mask = @io.read_uint8
172
+ shader.destination_blend = @io.read_uint8
173
+ shader._fog_function = @io.read_uint8
174
+ shader.primary_gradient = @io.read_uint8
175
+ shader.secondary_gradient = @io.read_uint8
176
+ shader.source_blend = @io.read_uint8
177
+ shader.texturing = @io.read_uint8
178
+ shader.detail_color_function = @io.read_uint8
179
+ shader.detail_alpha_function = @io.read_uint8
180
+ shader._shader_preset = @io.read_uint8
181
+ shader.alpha_test = @io.read_uint8
182
+ shader.post_detail_color_function = @io.read_uint8
183
+ shader.post_detail_alpha_function = @io.read_uint8
184
+ shader._reserved = @io.read_uint8
185
+
186
+ @shaders << shader
187
+ end
188
+ end
189
+
190
+ def parse_textures
191
+ @material_info.texture_count.times do
192
+ @io.open_chunk do
193
+ texture_info = WWW3D::TextureInfo.new
194
+
195
+ while (@io.open_chunk)
196
+ case @io.current_chunk_id
197
+ when W3D_CHUNK_TEXTURE_NAME
198
+ texture_info.name = @io.read_string
199
+
200
+ @textures << texture_info
201
+ when W3D_CHUNK_TEXTURE_INFO
202
+ texture_info.attributes = @io.read_uint16
203
+ texture_info.animation_type = @io.read_uint16
204
+ texture_info.frame_count = @io.read_uint32
205
+ texture_info.frame_rate = @io.read_float
206
+ end
207
+
208
+ @io.close_chunk
209
+ end
210
+ end
211
+ end
212
+ end
213
+
214
+ # FIXME: ToDo. Materials are complicated :)
215
+ def parse_material_pass
216
+ current_texture_stage = 0
217
+
218
+ while(@io.open_chunk)
219
+ errored = false
220
+
221
+ case @io.current_chunk_id
222
+ when W3D_CHUNK_VERTEX_MATERIAL_IDS
223
+ when W3D_CHUNK_SHADER_IDS
224
+ when W3D_CHUNK_DCG
225
+ when W3D_CHUNK_DIG
226
+ when W3D_CHUNK_SCG
227
+ when W3D_CHUNK_TEXTURE_STAGE
228
+ end
229
+
230
+ @io.close_chunk
231
+ end
232
+ end
233
+
234
+ def parse_chunk_aabb_tree
235
+ aabbtree_header = AABBTreeHeader.new
236
+
237
+ # read header
238
+ @io.open_chunk do
239
+ aabbtree_header.node_count = @io.read_uint32
240
+ aabbtree_header.poly_count = @io.read_uint32
241
+
242
+ @aabbtree_header = aabbtree_header
243
+ end
244
+
245
+ while(@io.open_chunk)
246
+ case @io.current_chunk_id
247
+ when W3D_CHUNK_AABTREE_POLYINDICES
248
+ @aabb_polygon_indices = @io.read_uint32_array(aabbtree_header.node_count)
249
+ when W3D_CHUNK_AABTREE_NODES
250
+ aabbtree_header.node_count.times do
251
+ node = AABBTreeNode.new
252
+
253
+ node.min = @io.read_vector3f
254
+ node.max = @io.read_vector3f
255
+ node.front_or_poly_zero = @io.read_uint32
256
+ node.back_or_poly_count = @io.read_uint32
257
+
258
+ @aabb_nodes << node
259
+ end
260
+ end
261
+
262
+ @io.close_chunk
263
+ end
264
+ end
265
+ end
266
+ end
267
+ end
@@ -0,0 +1,361 @@
1
+ require "stringio"
2
+
3
+ require_relative "ww_chunk_file"
4
+
5
+ module WestwoodLib
6
+ # Loader for .w3d files
7
+ # See: https://github.com/w3dhub/OpenW3D/blob/main/Code/ww3d2/w3d_file.h
8
+ class WWW3D < WWChunkFile
9
+ W3D_NAME_LENGTH = 16
10
+
11
+ W3D_CHUNK_MESH = 0x00000000
12
+ W3D_CHUNK_VERTICES = 0x00000002
13
+ W3D_CHUNK_VERTEX_NORMALS = 0x00000003
14
+ W3D_CHUNK_MESH_USER_TEXT = 0x0000000C
15
+ W3D_CHUNK_VERTEX_INFLUENCES = 0x0000000E
16
+ W3D_CHUNK_VERTEX_INFLUENCES_EXTENDED = 0x00000C03
17
+ W3D_CHUNK_MESH_HEADER3 = 0x0000001F
18
+ W3D_CHUNK_TRIANGLES = 0x00000020
19
+ W3D_CHUNK_VERTEX_SHADE_INDICES = 0x00000022
20
+
21
+ W3D_CHUNK_PRELIT_UNLIT = 0x00000023
22
+ W3D_CHUNK_PRELIT_VERTEX = 0x00000024
23
+ W3D_CHUNK_PRELIT_LIGHTMAP_MULTI_PASS = 0x00000025
24
+ W3D_CHUNK_PRELIT_LIGHTMAP_MULTI_TEXTURE = 0x00000026
25
+
26
+ W3D_CHUNK_MATERIAL_INFO = 0x00000028
27
+
28
+ W3D_CHUNK_SHADERS = 0x00000029
29
+
30
+ W3D_CHUNK_VERTEX_MATERIALS = 0x0000002A
31
+ W3D_CHUNK_VERTEX_MATERIAL = 0x0000002B
32
+ W3D_CHUNK_VERTEX_MATERIAL_NAME = 0x0000002C
33
+ W3D_CHUNK_VERTEX_MATERIAL_INFO = 0x0000002D
34
+ W3D_CHUNK_VERTEX_MAPPER_ARGS0 = 0x0000002E
35
+ W3D_CHUNK_VERTEX_MAPPER_ARGS1 = 0x0000002F
36
+
37
+ W3D_CHUNK_TEXTURES = 0x00000030
38
+ W3D_CHUNK_TEXTURE = 0x00000031
39
+ W3D_CHUNK_TEXTURE_NAME = 0x00000032
40
+ W3D_CHUNK_TEXTURE_INFO = 0x00000033
41
+
42
+ W3D_CHUNK_MATERIAL_PASS = 0x00000038
43
+ W3D_CHUNK_VERTEX_MATERIAL_IDS = 0x00000039
44
+ W3D_CHUNK_SHADER_IDS = 0x0000003A
45
+ W3D_CHUNK_DCG = 0x0000003B
46
+ W3D_CHUNK_DIG = 0x0000003C
47
+ W3D_CHUNK_SCG = 0x0000003E
48
+ W3D_CHUNK_FXSHADER_IDS = 0x0000003F
49
+
50
+ W3D_CHUNK_TEXTURE_STAGE = 0x00000048
51
+ W3D_CHUNK_TEXTURE_IDS = 0x00000049
52
+ W3D_CHUNK_STAGE_TEXCOORDS = 0x0000004A
53
+ W3D_CHUNK_PER_FACE_TEXCOORD_IDS = 0x0000004
54
+
55
+ W3D_CHUNK_FX_SHADERS = 0x00000050
56
+ W3D_CHUNK_FX_SHADER = 0x00000051
57
+ W3D_CHUNK_FX_SHADER_INFO = 0x00000052
58
+ W3D_CHUNK_FX_SHADER_CONSTANT = 0x00000053
59
+
60
+ W3D_CHUNK_DEFORM = 0x00000058
61
+ W3D_CHUNK_DEFORM_SET = 0x00000059
62
+ W3D_CHUNK_DEFORM_KEYFRAME = 0x0000005A
63
+ W3D_CHUNK_DEFORM_DATA = 0x0000005B
64
+
65
+ W3D_CHUNK_TANGENTS = 0x00000060
66
+ W3D_CHUNK_BINORMALS = 0x00000061
67
+
68
+ W3D_CHUNK_PS2_SHADERS = 0x00000080
69
+
70
+ W3D_CHUNK_AABTREE = 0x00000090
71
+ W3D_CHUNK_AABTREE_HEADER = 0x00000091
72
+ W3D_CHUNK_AABTREE_POLYINDICES = 0x00000092
73
+ W3D_CHUNK_AABTREE_NODES = 0x00000093
74
+
75
+ W3D_CHUNK_HIERARCHY = 0x00000100
76
+ W3D_CHUNK_HIERARCHY_HEADER = 0x00000101
77
+ W3D_CHUNK_PIVOTS = 0x00000102
78
+ W3D_CHUNK_PIVOT_FIXUPS = 0x00000103
79
+
80
+ W3D_CHUNK_ANIMATION = 0x00000200
81
+ W3D_CHUNK_ANIMATION_HEADER = 0x00000201
82
+ W3D_CHUNK_ANIMATION_CHANNEL = 0x00000202
83
+ W3D_CHUNK_BIT_CHANNEL = 0x00000203
84
+
85
+ W3D_CHUNK_COMPRESSED_ANIMATION = 0x00000280
86
+ W3D_CHUNK_COMPRESSED_ANIMATION_HEADER = 0x00000281
87
+ W3D_CHUNK_COMPRESSED_ANIMATION_CHANNEL = 0x00000282
88
+ W3D_CHUNK_COMPRESSED_BIT_CHANNEL = 0x00000283
89
+
90
+ W3D_CHUNK_MORPH_ANIMATION = 0x000002C0
91
+ W3D_CHUNK_MORPHANIM_HEADER = 0x000002C1
92
+ W3D_CHUNK_MORPHANIM_CHANNEL = 0x000002C2
93
+ W3D_CHUNK_MORPHANIM_POSENAME = 0x000002C3
94
+ W3D_CHUNK_MORPHANIM_KEYDATA = 0x000002C4
95
+ W3D_CHUNK_MORPHANIM_PIVOTCHANNELDATA = 0x000002C5
96
+
97
+ W3D_CHUNK_HMODEL = 0x00000300
98
+ W3D_CHUNK_HMODEL_HEADER = 0x00000301
99
+ W3D_CHUNK_NODE = 0x00000302
100
+ W3D_CHUNK_COLLISION_NODE = 0x00000303
101
+ W3D_CHUNK_SKIN_NODE = 0x00000304
102
+ OBSOLETE_W3D_CHUNK_HMODEL_AUX_DATA = 0x00000305
103
+ OBSOLETE_W3D_CHUNK_SHADOW_NODE = 0x00000306
104
+
105
+ W3D_CHUNK_LODMODEL = 0x00000400
106
+ W3D_CHUNK_LODMODEL_HEADER = 0x00000401
107
+ W3D_CHUNK_LOD = 0x00000402
108
+
109
+ W3D_CHUNK_COLLECTION = 0x00000420
110
+ W3D_CHUNK_COLLECTION_HEADER = 0x00000421
111
+ W3D_CHUNK_COLLECTION_OBJ_NAME = 0x00000422
112
+ W3D_CHUNK_PLACEHOLDER = 0x00000423
113
+ W3D_CHUNK_TRANSFORM_NODE = 0x00000424
114
+
115
+ W3D_CHUNK_POINTS = 0x00000440
116
+
117
+ W3D_CHUNK_LIGHT = 0x00000460
118
+ W3D_CHUNK_LIGHT_INFO = 0x00000461
119
+ W3D_CHUNK_SPOT_LIGHT_INFO = 0x00000462
120
+ W3D_CHUNK_NEAR_ATTENUATION = 0x00000463
121
+ W3D_CHUNK_FAR_ATTENUATION = 0x00000464
122
+ W3D_CHUNK_SPOT_LIGHT_INFO_5_0 = 0x00000465
123
+ W3D_CHUNK_PULSE = 0x00000466
124
+
125
+ W3D_CHUNK_EMITTER = 0x00000500
126
+ W3D_CHUNK_EMITTER_HEADER = 0x00000501
127
+ W3D_CHUNK_EMITTER_USER_DATA = 0x00000502
128
+ W3D_CHUNK_EMITTER_INFO = 0x00000503
129
+ W3D_CHUNK_EMITTER_INFOV2 = 0x00000504
130
+ W3D_CHUNK_EMITTER_PROPS = 0x00000505
131
+ OBSOLETE_W3D_CHUNK_EMITTER_COLOR_KEYFRAME = 0x00000506
132
+ OBSOLETE_W3D_CHUNK_EMITTER_OPACITY_KEYFRAME = 0x00000507
133
+ OBSOLETE_W3D_CHUNK_EMITTER_SIZE_KEYFRAME = 0x00000508
134
+ W3D_CHUNK_EMITTER_LINE_PROPERTIES = 0x00000509
135
+ W3D_CHUNK_EMITTER_ROTATION_KEYFRAMES = 0x0000050a
136
+ W3D_CHUNK_EMITTER_FRAME_KEYFRAMES = 0x0000050b
137
+ W3D_CHUNK_EMITTER_BLUR_TIME_KEYFRAMES = 0x0000050c
138
+ W3D_CHUNK_EMITTER_EXTRA_INFO = 0x0000050d
139
+
140
+ W3D_CHUNK_AGGREGATE = 0x00000600
141
+ W3D_CHUNK_AGGREGATE_HEADER = 0x00000601
142
+ W3D_CHUNK_AGGREGATE_INFO = 0x00000602
143
+ W3D_CHUNK_TEXTURE_REPLACER_INFO = 0x00000603
144
+ W3D_CHUNK_AGGREGATE_CLASS_INFO = 0x00000604
145
+
146
+ W3D_CHUNK_HLOD = 0x00000700
147
+ W3D_CHUNK_HLOD_HEADER = 0x00000701
148
+ W3D_CHUNK_HLOD_LOD_ARRAY = 0x00000702
149
+ W3D_CHUNK_HLOD_SUB_OBJECT_ARRAY_HEADER = 0x00000703
150
+ W3D_CHUNK_HLOD_SUB_OBJECT = 0x00000704
151
+ W3D_CHUNK_HLOD_AGGREGATE_ARRAY = 0x00000705
152
+ W3D_CHUNK_HLOD_PROXY_ARRAY = 0x00000706
153
+ W3D_CHUNK_HLOD_LIGHT_ARRAY = 0x00000707
154
+
155
+ W3D_CHUNK_BOX = 0x00000740
156
+ W3D_CHUNK_SPHERE = 0x00000741
157
+ W3D_CHUNK_RING = 0x00000742
158
+
159
+ W3D_CHUNK_NULL_OBJECT = 0x00000750
160
+
161
+ W3D_CHUNK_LIGHTSCAPE = 0x00000800
162
+ W3D_CHUNK_LIGHTSCAPE_LIGHT = 0x00000801
163
+ W3D_CHUNK_LIGHT_TRANSFORM = 0x00000802
164
+
165
+ W3D_CHUNK_DAZZLE = 0x00000900
166
+ W3D_CHUNK_DAZZLE_NAME = 0x00000901
167
+ W3D_CHUNK_DAZZLE_TYPENAME = 0x00000902
168
+
169
+ W3D_CHUNK_SOUNDROBJ = 0x00000A00
170
+ W3D_CHUNK_SOUNDROBJ_HEADER = 0x00000A01
171
+ W3D_CHUNK_SOUNDROBJ_DEFINITION = 0x00000A01
172
+
173
+ W3D_CHUNK_SECONDARY_VERTICES = 0x00000C00
174
+ W3D_CHUNK_SECONDARY_VERTEX_NORMALS = 0x00000C01
175
+
176
+ # generate "enums" from constants
177
+ W3D_CHUNK_ENUMS = constants.select { |sym| sym.to_s.start_with?("W3D_CHUNK_") }.map { |sym| [const_get(sym), sym] }.to_h
178
+
179
+ HierarchyHeader = Struct.new(
180
+ :version, # uint32_t
181
+ :name, # string[W3D_NAME_LENGTH]
182
+ :pivot_count, # uint32_t
183
+ :center, # vector3f
184
+ )
185
+
186
+ MeshHeader = Struct.new(
187
+ :version, # uint32_t
188
+ :attributes, # uint32_t
189
+ :mesh_name, # string
190
+ :container_name, # string
191
+ :triangle_count, # uint32_t
192
+ :vertice_count, # uint32_t
193
+ :material_count, # uint32_t
194
+ :damage_stages_count, # uint32_t
195
+ :sort_level, # int32_t
196
+ :prelit_version, # uint32_t
197
+ :_reserved, # uint32_t
198
+ :vertex_channels, # uint32_t
199
+ :face_channels, # uint32_t
200
+ :min_corner, # vector3f
201
+ :max_corner, # vector3f
202
+ :sphere_center, # vector3f
203
+ :sphere_radius # float
204
+ )
205
+
206
+ TextureCoordinate = Struct.new(
207
+ :u, # float
208
+ :v # float
209
+ )
210
+
211
+ # RGB color struct has a padding byte so just use RGBA and set A to nil there
212
+ RGBA = Struct.new(
213
+ :r, # uint8_t
214
+ :g, # uint8_t
215
+ :b, # uint8_t
216
+ :a # uint8_t
217
+ )
218
+
219
+ VertexMaterial = Struct.new(
220
+ :name, # OPTIONAL string (loaded separately in W3D_CHUNK_VERTEX_MATERIAL_NAME)
221
+ :attributes, # uint32_t
222
+ :ambient, # RGB
223
+ :diffuse, # RGB
224
+ :specular, # RGB
225
+ :emissive, # RGB
226
+ :shininess, # float
227
+ :opacity, # float
228
+ :translucency # float
229
+ )
230
+
231
+ MaterialInfo = Struct.new(
232
+ :pass_count, # uint32_t
233
+ :vertex_material_count, # uint32_t
234
+ :shader_count, # uint32_t
235
+ :texture_count # uint32_t
236
+ )
237
+
238
+ Shader = Struct.new(
239
+ :depth_compare, # uint8_t
240
+ :_depth_mask, # uint8_t : OBSOLETE
241
+ :color_mask, # uint8_t
242
+ :destination_blend, # uint8_t
243
+ :_fog_function, # uint8_t : OBSOLETE
244
+ :primary_gradient, # uint8_t
245
+ :secondary_gradient, # uint8_t
246
+ :source_blend, # uint8_t
247
+ :texturing, # uint8_t
248
+ :detail_color_function, # uint8_t
249
+ :detail_alpha_function, # uint8_t
250
+ :_shader_preset, # uint8_t : OBSOLETE
251
+ :alpha_test, # uint8_t
252
+ :post_detail_color_function, # uint8_t
253
+ :post_detail_alpha_function, # uint8_t
254
+ :_reserved # uint8_t
255
+ )
256
+
257
+ TextureInfo = Struct.new(
258
+ :name, # string
259
+ :attributes, # uint16_t : OPTIONAL
260
+ :animation_type, # uint16_t : OPTIONAL
261
+ :frame_count, # uint32_t : OPTIONAL
262
+ :frame_rate, # float : OPTIONAL
263
+ )
264
+
265
+ Triangle = Struct.new(
266
+ :vertex_ids, # uint32_t[3]
267
+ :attributes, # uint32_t
268
+ :normal, # vector3f
269
+ :distance # float
270
+ )
271
+
272
+ AABBTreeHeader = Struct.new(
273
+ :node_count, # uint32_t
274
+ :poly_count, # uint32_t
275
+ :_reserved, # uint32_t (uint32_t padding[6])
276
+ :_reserved1, # uint32_t
277
+ :_reserved2, # uint32_t
278
+ :_reserved3, # uint32_t
279
+ :_reserved4, # uint32_t
280
+ :_reserved5, # uint32_t
281
+ )
282
+
283
+ AABBTreeNode = Struct.new(
284
+ :min, # vector3f
285
+ :max, # vector3f
286
+ :front_or_poly_zero, # uint32_t
287
+ :back_or_poly_count, # uint32_t
288
+ )
289
+
290
+ Pivot = Struct.new(
291
+ :name, # string[W3D_NAME_LENGTH]
292
+ :parent_idx, # uint32_t
293
+ :translation, # vector3f
294
+ :euler_angles, # vector3f
295
+ :rotation, # vector4f (Quaternion)
296
+ :is_proxy, # helper
297
+ )
298
+
299
+ attr_reader :hierarchy_header, :pivots, :meshes
300
+
301
+ def initialize(io:)
302
+ super
303
+
304
+ @hierarchy_header = nil
305
+ @meshes = []
306
+ @pivots = []
307
+ end
308
+
309
+ def load
310
+ each_chunk do |chunk|
311
+ pp W3D_CHUNK_ENUMS[chunk.id] if WESTWOOD_LIB_DEBUG
312
+
313
+ case chunk.id
314
+ when W3D_CHUNK_HIERARCHY
315
+ parse_hierarchy
316
+ when W3D_CHUNK_MESH
317
+ @meshes << WWMesh.new(io: self, chunk: chunk)
318
+ when W3D_CHUNK_HLOD
319
+ nil
320
+ end
321
+ end
322
+
323
+ true
324
+ end
325
+
326
+ def parse_hierarchy
327
+ header = HierarchyHeader.new
328
+
329
+ open_chunk do
330
+ raise "Expected W3D_CHUNK_HIERARCHY_HEADER chunk" unless current_chunk_id == W3D_CHUNK_HIERARCHY_HEADER
331
+
332
+ header.version = read_uint32
333
+ header.name = read_fixed_string(W3D_NAME_LENGTH)
334
+ header.pivot_count = read_uint32
335
+ header.center = read_vector3f
336
+
337
+ @hierarchy_header = header
338
+ end
339
+
340
+ while(open_chunk)
341
+ case current_chunk_id
342
+ when W3D_CHUNK_PIVOTS
343
+ header.pivot_count.times do
344
+ pivot = Pivot.new
345
+ pivot.name = read_fixed_string(W3D_NAME_LENGTH)
346
+ pivot.parent_idx = read_uint32
347
+ pivot.translation = read_vector3f
348
+ pivot.euler_angles = read_vector3f
349
+ pivot.rotation = read_vector4f
350
+
351
+ pivot.is_proxy = pivot.name.include?("~")
352
+
353
+ @pivots << pivot
354
+ end
355
+ end
356
+
357
+ close_chunk
358
+ end
359
+ end
360
+ end
361
+ end
data/lib/westwood_lib.rb CHANGED
@@ -1,7 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ WESTWOOD_LIB_DEBUG = false
4
+
3
5
  require_relative "westwood_lib/version"
4
6
 
5
7
  require_relative "westwood_lib/ww_mix"
8
+ require_relative "westwood_lib/ww_chunk_file"
9
+ require_relative "westwood_lib/ww_w3d"
10
+ require_relative "westwood_lib/ww_w3d/ww_mesh"
11
+ require_relative "westwood_lib/ww_translate_db"
6
12
 
7
13
  require_relative "westwood_lib/tools/ww_mix_index"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: westwood_lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyberarm
@@ -35,7 +35,11 @@ files:
35
35
  - lib/westwood_lib.rb
36
36
  - lib/westwood_lib/tools/ww_mix_index.rb
37
37
  - lib/westwood_lib/version.rb
38
+ - lib/westwood_lib/ww_chunk_file.rb
38
39
  - lib/westwood_lib/ww_mix.rb
40
+ - lib/westwood_lib/ww_translate_db.rb
41
+ - lib/westwood_lib/ww_w3d.rb
42
+ - lib/westwood_lib/ww_w3d/ww_mesh.rb
39
43
  homepage: https://github.com/cyberarm/westwood_lib
40
44
  licenses:
41
45
  - MIT
@@ -57,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
61
  - !ruby/object:Gem::Version
58
62
  version: '0'
59
63
  requirements: []
60
- rubygems_version: 4.0.22
64
+ rubygems_version: 4.0.3
61
65
  specification_version: 4
62
66
  summary: 'A library for working with Westwood''s file formats used in Command & Conquer:
63
67
  Renegade.'