blib/lib/Games/Freelancer/UTF.pm

filter: Statementstmt 92.9% 118 / 127 Branchbran 69.4% 25 / 36 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 19 / 19 Podpod 100% 11 / 11 totaltotal 87.6% 178 / 203 CC 24 SCAR 14.5
CC
24
Cov
87%
UTFreadUTFrek
7.5
UTFwriteUTFrek
7.0
UTFreadUTF
6.5
CRAP 4.2
↑ Summary
1 ###############################################################################
2 #UTF.pm
3 #Last Change: 2008-12-08
4 #Copyright (c) 2008 Marc-Seabstian "Maluku" Lucksch
5 #Version 0.21
6 ####################
7 #This file is part of the Plasma project, a parser library for an all-purpose
8 #ASCII file format. More information can be found on the project web site
9 #at http://plasma.sf.net/ .
10 #
11 #UTF.pm is published under the terms of the MIT license, which basically
12 #means "Do with it whatever you want". For more information, see the license.txt
13 #file that should be enclosed with plasma distributions. A copy of the license
14 #is (at the time of this writing) also available at
15 #http://www.opensource.org/licenses/mit-license.php .
16 ###############################################################################
17
18 package Games::Freelancer::UTF;
19 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 {
21 1 ▶ no warnings qw/portable/; #Why is this so hard... :(
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 local $^W=0; #Grrr @ Tie::InsertOrderHash;
23 1 require Tie::InsertOrderHash;
24 }
25 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
28
29 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30
31 1 our $VERSION = 1.001;
32
33 =head1 NAME
34
35 Games::Freelancer::UTF - Perl extension for working with Microsoft UTF Files used in the Game Freelancer.
36
37 =head1 Synopsis
38
39 use Games::Freelancer::UTF;
40 open FILE,"model.cmp"; #or .utf, .3db, .txm, .mat, .ale, .vms, .dfm or maybe some more
41 binmode FILE;
42 my $content = do {local $/; <FILE>};
43 close FILE;
44 my $tree=UTFread($content);
45 $code = UTFwriteUTF($tree);
46 open FILE, ">out.cmp"
47 binmode FILE;
48 print FILE, $code;
49 close FILE;
50
51
52 =head1 DESCRIPTION
53
54 This Module provides the ability to decode UTF files for the Mircrosoft game "Freelancer"
55
56 Those are named UTF files because of their header.
57
58 They are just trees that are encoded in binary, there might be a possibility that these files are used somewhere else, too.
59
60 In "Freelancer" they are used for models, meshes, materials, textures, effects and a lot more.
61
62 You can even use this to save hashes of hashes, but I highly recommend using something else, like L<Storable> for this.
63
64 =head1 WARNING
65
66 The read routines return a tied InsertOrderHash, so just keep the reference you got and work with it instead of using
67
68 my %mysuperhash = %{UTFread($crypted)}
69 %mysuperhash{NewEntry} = "Data"
70 # Now the order is destroyed
71 # This is better:
72 my $tree=UTFread($crypted);
73 $tree->{NewEntry} = "Data"
74
75
76 I have no idea how important the order of the elements is for Freelancer, but better keep it this way.
77 Of course all subhashes are also tied
78 #Bad code example
79 $tree->{copyme}={%{$tree}};
80 #Good code:
81 tie my %newhash,'Tie::InsertOrderHash';
82 %newhash=(%{$tree}) #Copy tree, preserves order, at least last time I tested
83 $tree->{copyme}=\%newhash;
84 #not this:
85 $tree->{copyme}={%newhash}; #Looses tiedness too.
86
87 =head1 FUNCTIONS
88
89 =cut
90
91 # Perl-Port by Maluku (fl@maluku.de)
92 1 our @ISA=qw/Exporter/;
93 1 our @EXPORT=qw/UTFread UTFwrite/;
94 1 our @EXPORT_OK=qw/UTFread UTFwrite/;
95
96 1 my $datas;
97 my $strings;
98 1 my $pointer;
99 1 my %strings;
100 1 my %datas;
101 1 my %offsets;
102
103
104 =head2 $tree = UTFread ($data);
105
106 Reads an UTF file content into a tied tree:
107
108 use Games::Freelancer::UTF;
109 use Data::Dumper;
110 open FILE,"model.cmp";
111 binmode FILE;
112 my $content = do {local $/; <FILE>};
113 close FILE;
114 my $tree=UTFread($content);
115 print Data::Dumper->Dump([$tree]);
116
117 =cut
118
119 1 ▶sub UTFread {
Subroutine UTFread: called CC=1 SCAR=0.0
Pod documented
120 1 my $d = UTFreadUTF(@_);
121 1 return $d;
122 }
123
124
125 =head2 $data = UTFwrite ($tree);
126
127 Reads an UTF file content into a tied tree:
128
129 use Games::Freelancer::UTF;
130 open FILE,"model.cmp";
131 binmode FILE;
132 my $content = do {local $/; <FILE>};
133 close FILE;
134 my $tree=UTFread($content);
135 #... Do something with $tree ..., for example moving a hardpoint:
136 foreach my $entr (grep /\.3db/,keys %{$tree->{"\\"}}) {
137 foreach (keys %{$tree->{"\\"}->{$entr}->{Hardpoints}->{"Fixed"}}) {
138 #moves all fixed hardpoints along (0.2,0.2,0.2):
139 $tree->{"\\"}->{$entr}->{Hardpoints}->{"Fixed"}->{$_}->{Position}=pack("f*",map {$_+0.2} unpack("f*",$tree->{"\\"}->{$entr}->{Hardpoints}->{"Fixed"}->{$_}->{Position}));
140 }
141 }
142 # Now write it down.
143 open FILE,">model2.cmp";
144 binmode FILE;
145 print FILE UTFwrite($tree);
146 close FILE;
147 =cut
148
149 1 ▶sub UTFwrite {
Subroutine UTFwrite: called CC=1 SCAR=0.0
Pod documented
150 1 return UTFwriteUTF(@_);
151 }
152
153 =head1 INTERNAL FUNCTIONS (use with care)
154
155 =head2 get (SOURCE, OFFSET, LENTGH)
156
157 Extracts a string of LENGTH at OFFSET of the SOURCE.
158
159 Used for general file reading.
160
161 =cut
162
163 #Internal functions:
164
165 #$chars = get ($string,$offset,$amount);
166 #returns $amount (or less) chars of $string starting from $offset.
167 #Also increases $offset.
168
169
170 1 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
171 1 my $off=$_[1];
172 1 $_[1]+=$_[2];
173 1 return substr($_[0],$off,$_[2]);
174 }
175
176 =head2 string (OFFSET)
177
178 Extracts a string \0 delimited string at OFFSET out of the stringlibrary of the file.
179
180 Used for key names
181
182 =cut
183
184 #my $string = string($offset)
185 #Returns a string of stringlib, which is a compilation of \0 strings starting a $offset.
186 #Returns string without the trailing \0.
187
188 32 ▶sub string {
Subroutine string: called CC=2 SCAR=8.4
Pod documented
189 32 ▶ if ($_[0] >= length $strings) { #THIS IS AN ERROR IN THE UTF FILE
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] >= length $strings) 0 32 32
190 0 warnings::warnif("Requested a string from outside of the string lib, this is an error in the file or something the parser doesn't know about.");
191 0 return "" ;
192 }
193 32 my $shift=index($strings,"\0",$_[0]) - $_[0];
194 32 return substr($strings,$_[0],$shift);
195 }
196
197 =head2 data (OFFSET, LENGTH)
198
199 Extracts data from an OFFSET with specific LENGTH out of the datalibrary of the file.
200
201 Used for data nodes
202
203 =cut
204
205 #my $data=data($start,$length);
206 #Returns a string of the datalib, which is just a bunch of data, starting at $start with a length of $length.
207
208 17 ▶sub data {
Subroutine data: called CC=2 SCAR=9.2
Pod documented
209 17 ▶ if ($_[0] >= length $datas) {#THIS IS AN ERROR IN THE UTF FILE
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] >= length $datas) 0 17 17
210 0 warnings::warnif("Requested data from outside of the data lib, this is an error in the file or something the parser doesn't know about.");
211 0 return "" ;
212 }
213 17 return substr($datas,$_[0],$_[1]);
214 }
215
216
217 =head2 UTFreadUTFrek( TREE, NODEID )
218
219 Parses a node with NODEID out of the binary TREE, then calls itself with all the childnodes and siblingnodes
220
221 =cut
222
223 #Parses a UTF node recursive:
224 #node
225 #{
226 # dword sibling_offset
227 # dword string_offset
228 # dword flags
229 # dword zero (seems to be always zero, meaning unknown)
230 # dword child_offset
231 # dword allocated_size
232 # dword size1
233 # dword size2
234 # dword time1
235 # dword time2
236 # dword time3
237 #} = 44 bytes
238
239
240
241 32 ▶sub UTFreadUTFrek {
Subroutine UTFreadUTFrek: called CC=7 SCAR=20.1
Pod documented
242 1 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
243 32 local $_;
244 32 my $tree=shift;
245 32 my $i=shift;
246 32 ▶ if ($offsets{$i}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offsets{$i}++) 0 32 32
247 0 warnings::warnif("Somehow the file managed to request the same node again, this is ignored");
248 0 return {};
249 }
250 32 ▶ if ($i > length($tree)-44) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > length($tree) - 44) 0 32 32
251 0 warnings::warnif("Requested a node from outside of the TREE, this is an error in the file or something the parser doesn't know about.");
252 0 return {};
253 }
254 32 tie my %data => 'Tie::InsertOrderHash';
255 32 my ($silb, $name, $flags, $z, $childoffset, $alloc, $size, $size2, $time1, $time2, $time3) = unpack("VVVVVVVVVVV", substr($tree,$i));
256 # Now we must use all the stuff here or warning will annoy us:
257 32 ($time1, $time2, $time3) = ($time1, $time2, $time3);
258 32 ▶ $size=$size2 if ($size2 < $size);
Branch: 1 branch
BranchTrueFalseTotal
if $size2 < $size 0 32 32
259 32 ▶ if ($flags & 0x10 and not $flags & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 16 and not $flags & 128) { } 15 17 32
Condition66%2/3
$flags & 16 and not $flags & 128
!ll&&!rl&&r
17015
MC/DC50%1/2
$flags & 16 and not $flags & 128
$flags & 16 not $flags & 128
Truth table66%2/3
A and not(B)
A= $flags & 16B= not $flags & 128
ABresult
0X0
100
111
260 15 $data{string($name)}=UTFreadUTFrek($tree,$childoffset);
261 }
262 else {
263 17 $data{string($name)}=data($childoffset,$size);
264 }
265 32 ▶ if ($silb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($silb) 16 16 32
266 16 my $data=UTFreadUTFrek($tree,$silb);
267 #print $data,"->$i-->$silb\n";
268 16 %data=(%data,%{$data});
269 }
270 32 return \%data;
271
272 }
273
274 =head2 packString (STRING)
275
276 Saves a STRING to the stringlib and returns an offset. Tests if the string already exists.
277
278 Used for writing Nodenames (keys)
279
280 =cut
281
282 #$offset = packString($string)
283
284 #Packs a string and returns the offset.
285 #Also adds the string to the stringlib
286 32 ▶sub packString {
Subroutine packString: called CC=2 SCAR=6.9
Pod documented
287 32 my $string = shift;
288 32 ▶ if (exists $strings{$string}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $strings{$string}) { } 7 25 32
289 7 return $strings{$string};
290 }
291 else {
292 25 $strings{$string} = length($strings);
293 25 $strings.=$string."\0";
294 25 return $strings{$string};
295 }
296
297 }
298
299 =head2 packData (DATA)
300
301 Saves a string with DATA to the datalib and returns an offset. Tests if the data already exists.
302
303 Used for writing nodedata (values)
304
305 =cut
306
307 #$offset = packData($data)
308
309 #Packs a piece of data and returns the offset.
310 #Also adds the data to the datalib
311 17 ▶sub packData {
Subroutine packData: called CC=2 SCAR=6.9
Pod documented
312 17 my $data = shift;
313 17 ▶ if (exists $datas{$data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $datas{$data}) { } 3 14 17
314 3 return $datas{$data};
315 }
316 else {
317 14 $datas{$data} = length($datas);
318 #$datas.=$data."\0";
319 14 $datas.=$data;
320 14 return $datas{$data};
321 }
322
323 }
324
325 #Writes UTF nodes recursive.
326
327 =head2 UTFwriteUTFrek (DATA, NAME, SIBLING)
328
329 Writes hashref or a scalar into the tree.
330
331 SIBLING is true if there is a next sibling node (different output on nodes without a next one)
332
333 Calls itself again for each entry of a hashref. (Writes the children)
334
335 =cut
336
337 32 ▶sub UTFwriteUTFrek {
Subroutine UTFwriteUTFrek: called CC=7 SCAR=19.5
Pod documented
338 1 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
339 32 local $_;
340 32 my $tree=shift;
341 32 my $name=shift;
342 32 my $silb=shift;
343 32 $pointer+=44;
344 32 my $start=$pointer;
345 32 ▶ croak "Can't pack other then scalar or Hashrefs" if ref $tree and ref $tree ne "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $tree and ref $tree ne "HASH" 0 32 32
Condition66%2/3
ref $tree and ref $tree ne "HASH"
!ll&&!rl&&r
17150
MC/DC0%0/2
ref $tree and ref $tree ne "HASH"
ref $tree ref $tree ne "HASH"
Truth table66%2/3
A and B
A= ref $treeB= ref $tree ne "HASH"
ABresult
0X0
100
111
346 32 ▶ return pack ("VVVVVVVVVVV",$silb?$pointer:0, packString($name), 0x80, 0, packData($tree), length($tree), length($tree), length($tree), 0, 0, 0) unless ref $tree;
Branch: 2 branches
BranchTrueFalseTotal
unless ref $tree 17 15 32
$silb ? : 11 6 17
347 15 my $code="";
348 15 my @list = keys(%$tree);
349 #print "name = $name, pointer = $pointer, data = ".(ref ($tree) || "scalar")."\n";
350 15 foreach (0 .. $#list) {
351 31 $code.=UTFwriteUTFrek($tree->{$list[$_]},$list[$_],($_!=$#list));
352 }
353 #print "name = $name, pointer = $pointer, data = ".(ref ($tree) || "scalar")."\n";
354 15 ▶ return pack ("VVVVVVVVVVV",$silb?$pointer:0, packString($name), 0x10, 0, $start, 0,0,0,0,0,0).$code;
Branch: 1 branch
BranchTrueFalseTotal
$silb ? : 5 10 15
355 }
356
357
358 #header
359 #{
360 # dword "UTF "
361 # dword 0x101
362 # dword tree_segment_offset
363 # dword size_of_tree_segment
364 # dword header_offset? (0) ##I think its here also a First element of the treeoffset
365 # dword size_of_header (44) ##I think it is more a size of entry
366 # dword string_segment_offset
367 # dword space_allocated_for_string_segment
368 # dword size_of_string_segment_actually_used
369 # dword data_segment_offset
370 # dword unknown (seems to be zero most of the times) #Possible first entry of data segment (after deletion of an entry)
371 #} = 44 bytes
372
373 #Reads the UTF header, extracts data and string libraries and starts parsing the nodes
374
375 =head2 UTFreadUTF ( DATA )
376
377 Extracts and parses an UTF header from the scalar DATA.
378
379 Splits the file in TREE, STRINGLIB and DATALIB according to the header and then calls UTFreadUTFrek on the TREE.
380
381 =cut
382
383 1 ▶sub UTFreadUTF{
Subroutine UTFreadUTF: called CC=6 SCAR=18.7
Pod documented
384 1 my $code=shift;
385 1 my $i=0;
386 1 %offsets = ();
387 1 ▶ if (substr($code,$i,4) eq "UTF ") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($code, $i, 4) eq 'UTF ') { } 1 0 1
388 1 $i+=4;
389 1 my ($ver,$treeoffset,$treesize,$treefirst,$treeelemsize,$stringoffset,$stringspace,$stringsize,$dataoffset,$datafirst)=unpack("VVVVVVVVVV",get($code,$i,40));
390 # We don't use those now, not sure what they are for anyway
391 1 ▶ $datafirst = 0 unless $datafirst;
Branch: 1 branch
BranchTrueFalseTotal
unless $datafirst 1 0 1
392 1 ▶ $treefirst = 0 unless $treefirst;
Branch: 1 branch
BranchTrueFalseTotal
unless $treefirst 1 0 1
393 1 ▶ $treeelemsize = 0 unless $treeelemsize;
Branch: 1 branch
BranchTrueFalseTotal
unless $treeelemsize 0 1 1
394 # We don't need this one either, do we?
395 1 ▶ $stringsize = 0 unless $stringsize;
Branch: 1 branch
BranchTrueFalseTotal
unless $stringsize 0 1 1
396 # Splitting the parts
397 1 $strings=substr($code,$stringoffset,$stringspace);
398 1 $datas=substr($code,$dataoffset);
399 1 my $tree=substr($code,$treeoffset,$treesize);
400 1 return UTFreadUTFrek($tree,0);
401 }
402 else {
403 0 croak "NOT a UTF File";
404 }
405
406 }
407
408 #Writes an UTF file with header and nodes.
409
410 =head2 UTFwriteUTF (TREE(HASHREF) )
411
412 Calls UTFwriteUTFrek and then return the header, TREE, STRINGLIB and DATALIB
413
414 =cut
415
416 1 ▶sub UTFwriteUTF{
Subroutine UTFwriteUTF: called CC=3 SCAR=11.0
Pod documented
417 1 my $tree=shift;
418 1 my $i=0;
419 1 $strings="";
420 1 $datas="";
421 1 my $code = "";
422 1 %strings = ();
423 1 %datas = ();
424 1 $pointer=0;
425 1 my @list = keys(%$tree);
426 1 foreach (0 .. $#list) {
427 1 $code.=UTFwriteUTFrek($tree->{$list[$_]},$list[$_],($_!=$#list));
428 }
429 1 my $string=$strings;
430 1 $string.="\0" for(length($strings) .. (int(length($strings)/32)+1)*32); #Just some fun stuff.
431 1 return "UTF ".pack("VVVVVVVVVV",0x101,44+12,length($code),0,44,44+12+length($code),length($string),length($strings),44+12+length($code)+length($string),0)."000000000000".$code.$string.$datas;
432
433 }
434
435 1 1;
436
437 __END__
↑ Summary