westwood_lib 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b0beb28de31c8bb1b6c1cf1b212e5d40943d4cda81160ec4865583607a3e026e
4
+ data.tar.gz: ce64840e6061eb2d24bccba0c5c177b3ae726baa3f70134f60715dce112f0198
5
+ SHA512:
6
+ metadata.gz: d09073c3213c85add16ba846c490ab5aea66cfd04721ddb52bed86d34e1ea91d299886e57ce2c01fc27a7d086013fd1ec1692098557bb01ff9f7e75ab42bf4c4
7
+ data.tar.gz: 1eb37dfad92f6989cb8f1945b38f411d22b38eb0648eb56a34da2ef83ba7d7a489517683d0e9a82cea675ec8f9132631f46bc8d2aa9e6a11fc70922213350bcc
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Cyberarm
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # WestwoodLib
2
+
3
+ A library for working with Westwood's file formats used in Command & Conquer: Renegade.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ ```bash
10
+ bundle add westwood_lib
11
+ ```
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ ```bash
16
+ gem install westwood_lib
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cyberarm/westwood_lib.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[test standard]
@@ -0,0 +1,30 @@
1
+ require_relative "../ww_mix"
2
+
3
+ module WestwoodLib
4
+ # Index contents of all .mix and .dat files in the specified directory
5
+ class WWMixIndex
6
+ attr_reader :path, :entries
7
+
8
+ def initialize(path:)
9
+ @path = path
10
+ @entries = {}
11
+
12
+ Dir.glob("#{path}/*.*").each do |file|
13
+ next unless File.extname(file.downcase) =~ /\.mix|\.dat/
14
+
15
+ mix = WWMix.new(path: file)
16
+ unless mix.load
17
+ puts "Failed to load #{file}: #{mix.error_reason}"
18
+ next
19
+ end
20
+
21
+ mix.entries.each do |entry|
22
+ @entries[entry.name.upcase] ||= []
23
+ @entries[entry.name.upcase] << entry
24
+ end
25
+ end
26
+
27
+ @entries.freeze
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WestwoodLib
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,283 @@
1
+ require "digest"
2
+ require "stringio"
3
+
4
+ module WestwoodLib
5
+ class WWMix
6
+ MIX1_HEADER = 0x3158494D
7
+ MIX2_HEADER = 0x3258494D
8
+
9
+ MixHeader = Struct.new(
10
+ :mime_type, # int32
11
+ :file_data_offset, # int32
12
+ :file_names_offset, # int32
13
+ :_reserved # int32
14
+ )
15
+
16
+ EntryInfoHeader = Struct.new(
17
+ :crc32, # uint32
18
+ :content_offset, # uint32
19
+ :content_length # uint32
20
+ )
21
+
22
+ class Entry
23
+ attr_accessor :path, :name, :info, :blob, :is_blob
24
+
25
+ def initialize(name:, path:, info:, blob: nil)
26
+ @name = name
27
+ @path = path
28
+ @info = info
29
+ @blob = blob
30
+
31
+ @info.content_length = blob.size if blob?
32
+ end
33
+
34
+ def blob?
35
+ @blob
36
+ end
37
+
38
+ def calculate_crc32
39
+ Digest::CRC32.hexdigest(@name.upcase).upcase.to_i(16)
40
+ end
41
+
42
+ # Write entry's data to stream.
43
+ # Caller is responsible for ensuring stream is valid for writing
44
+ def copy_to(stream)
45
+ if blob?
46
+ return false if @blob.size.zero?
47
+
48
+ stream.write(blob)
49
+ return true
50
+ else
51
+ if read
52
+ stream.write(@blob)
53
+ @blob = nil
54
+ return true
55
+ end
56
+ end
57
+
58
+ false
59
+ end
60
+
61
+ def read
62
+ return false unless File.exist?(@path)
63
+ return false if File.directory?(@path)
64
+ return false if File.size(@path) < @info.content_offset + @info.content_length
65
+
66
+ @blob = File.binread(@path, @info.content_length, @info.content_offset)
67
+
68
+ true
69
+ end
70
+ end
71
+
72
+ attr_reader :path, :encrypted, :entries, :error_reason
73
+
74
+ def initialize(path:, encrypted: false)
75
+ @path = path
76
+ @encrypted = encrypted
77
+ @entries = []
78
+
79
+ @error_reason = ""
80
+ end
81
+
82
+ # Load entries from MIX file. Entry data is NOT loaded.
83
+ # @return true on success or false on failure. Check `error_reason` for why.
84
+ def load
85
+ unless File.exist?(@path)
86
+ @error_reason = format("Path does not exist: %s", @path)
87
+ return false
88
+ end
89
+
90
+ if File.directory?(@path)
91
+ @error_reason = format("Path is a directory: %s", @path)
92
+ return false
93
+ end
94
+
95
+ File.open(@path, "rb") do |f|
96
+ header = MixHeader.new(0, 0, 0, 0)
97
+ header.mime_type = read_i32(f)
98
+ header.file_data_offset = read_i32(f)
99
+ header.file_names_offset = read_i32(f)
100
+ header._reserved = read_i32(f)
101
+
102
+ unless header.mime_type == MIX1_HEADER || header.mime_type == MIX2_HEADER
103
+ @error_reason = format("Invalid mime type: %d", header.mime_type)
104
+ return false
105
+ end
106
+
107
+ @encrypted = header.mime_type == MIX2_HEADER
108
+
109
+ # Read entry info
110
+ f.pos = header.file_data_offset
111
+ file_count = read_i32(f)
112
+
113
+ file_count.times do |i|
114
+ entry_info = EntryInfoHeader.new(0, 0, 0)
115
+ entry_info.crc32 = read_u32(f)
116
+ entry_info.content_offset = read_u32(f)
117
+ entry_info.content_length = read_u32(f)
118
+
119
+ @entries << Entry.new(name: "", path: @path, info: entry_info)
120
+ end
121
+
122
+ # Read entry names
123
+ f.pos = header.file_names_offset
124
+ file_count = read_i32(f)
125
+
126
+ file_count.times do |i|
127
+ @entries[i].name = read_string(f)
128
+ end
129
+ end
130
+
131
+ true
132
+ end
133
+
134
+ def save
135
+ unless @entries.size.positive?
136
+ @error_reason = "No entries to write."
137
+ return false
138
+ end
139
+
140
+ if File.directory?(@path)
141
+ @error_reason = format("Path is a directory: %s", @path)
142
+ return false
143
+ end
144
+
145
+ File.open(@path, "wb") do |f|
146
+ header = MixHeader.new(encrypted? ? MIX2_HEADER : MIX1_HEADER, 0, 0, 0)
147
+
148
+ # write mime type
149
+ write_i32(f, header.mime_type)
150
+
151
+ f.pos = 16
152
+
153
+ # sort entries by crc32 of their name
154
+ sort_entries
155
+
156
+ # write file blobs
157
+ @entries.each do |entry|
158
+ # store current io position
159
+ pos = f.pos
160
+
161
+ # copy entry to stream
162
+ entry.copy_to(f)
163
+
164
+ # update entry with new offset
165
+ entry.info.content_offset = pos
166
+
167
+ # add alignment padding
168
+ padding = (-f.pos & 7)
169
+ padding.times do |i|
170
+ write_u8(f, 0)
171
+ end
172
+ end
173
+
174
+ # Save file data offset
175
+ header.file_data_offset = f.pos
176
+
177
+ # write number of entries
178
+ write_i32(f, @entries.size)
179
+
180
+ # write entries file data
181
+ @entries.each do |entry|
182
+ write_u32(f, entry.info.crc32)
183
+ write_u32(f, entry.info.content_offset)
184
+ write_u32(f, entry.info.content_length)
185
+ end
186
+
187
+ # save file names offset
188
+ header.file_names_offset = f.pos
189
+
190
+ # write number of entries
191
+ write_i32(f, @entries.size)
192
+
193
+ # write entry names
194
+ @entries.each do |entry|
195
+ write_string(f, entry.name)
196
+ end
197
+
198
+ # jump to io_position 4
199
+ f.pos = 4
200
+ # write rest of header
201
+
202
+ write_i32(f, header.file_data_offset)
203
+ write_i32(f, header.file_names_offset)
204
+ write_i32(f, header._reserved)
205
+ end
206
+
207
+ true
208
+ end
209
+
210
+ def valid?
211
+ # ALL entries MUST have unique case-insensitive names
212
+ @entries.each do |a|
213
+ @entries.each do |b|
214
+ next if a == b
215
+
216
+ return false if a.name.upcase == b.name.upcase
217
+ end
218
+ end
219
+
220
+ true
221
+ end
222
+
223
+ def encrypted?
224
+ @encrypted
225
+ end
226
+
227
+ def add_file(path:, replace: false)
228
+ return false unless File.exist?(path)
229
+ return false if File.directory?(path)
230
+
231
+ entry = Entry.new(name: File.basename(path), path: path, info: EntryInfoHeader.new(0, 0, File.size(path)))
232
+ add_entry(entry: entry, replace: replace)
233
+ end
234
+
235
+ def add_blob(path:, blob:, replace: false)
236
+ info = EntryInfoHeader.new(0, 0, blob.size)
237
+ entry = Entry.new(name: File.basename(path), path: path, info: info, blob: blob)
238
+ into.crc32 = @entries.last.calculate_crc32
239
+
240
+ add_entry(entry: entry, replace: replace)
241
+ end
242
+
243
+ def add_entry(entry:, replace: false)
244
+ duplicate = @entries.find { |e| e.name.upcase == entry.name.upcase }
245
+
246
+ if duplicate
247
+ if replace
248
+ @entries.delete(duplicate)
249
+ else
250
+ return false
251
+ end
252
+ end
253
+
254
+ @entries << entry
255
+ true
256
+ end
257
+
258
+ def sort_entries
259
+ return false if @entries.any? { |e| e.info.crc32 == 0 }
260
+
261
+ @entries.sort! { |a, b| a.info.crc32 <=> b.info.crc32 }
262
+
263
+ true
264
+ end
265
+
266
+ def read_i32(f) = f.read(4).unpack1("l")
267
+ def read_u32(f) = f.read(4).unpack1("L")
268
+ def read_u8(f) = f.read(1).unpack1("C")
269
+ def read_string(f)
270
+ f.read(read_u8(f)).strip
271
+ end
272
+
273
+ def write_i32(f, value) = f.write([value].pack("l"))
274
+ def write_u32(f, value) = f.write([value].pack("L"))
275
+ def write_u8(f, value) = f.write([value].pack("C"))
276
+ def write_string(f, string)
277
+ length = string.size + 1 # include null byte
278
+ write_u8(f, length)
279
+ f.write(string)
280
+ write_u8(f, 0) # null byte
281
+ end
282
+ end
283
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "westwood_lib/version"
4
+
5
+ require_relative "westwood_lib/ww_mix"
6
+
7
+ require_relative "westwood_lib/tools/ww_mix_index"
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: westwood_lib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Cyberarm
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: digest-crc
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 0.7.0
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 0.7.0
26
+ email:
27
+ - matthewlikesrobots@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - LICENSE.txt
33
+ - README.md
34
+ - Rakefile
35
+ - lib/westwood_lib.rb
36
+ - lib/westwood_lib/tools/ww_mix_index.rb
37
+ - lib/westwood_lib/version.rb
38
+ - lib/westwood_lib/ww_mix.rb
39
+ homepage: https://github.com/cyberarm/westwood_lib
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ allowed_push_host: https://rubygems.org
44
+ homepage_uri: https://github.com/cyberarm/westwood_lib
45
+ source_code_uri: https://github.com/cyberarm/westwood_lib
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 3.2.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 4.0.22
61
+ specification_version: 4
62
+ summary: 'A library for working with Westwood''s file formats used in Command & Conquer:
63
+ Renegade.'
64
+ test_files: []