Skip to content

Commit

Permalink
fix MH1 INST packer / unpacker
Browse files Browse the repository at this point in the history
  • Loading branch information
sor3nt@gmail.com committed Jun 10, 2018
1 parent c4a1c05 commit ffc8855
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 73 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#Changelog

**2018-06-10**: Bugfix **v0.0.3** Release:
* Fix Manhunt 1 Inst Packer / Unpacker
* Add Changelog

**2018-06-10**: Bugfix **v0.0.2** Release:

* Fix Manhunt 1 MLS Packer / Unpacker

**2018-06-10**: Initial **v0.0.1** Release:

* Add GLG Packer / Unpacker
* Add Inst Packer / Unpacker
* Add MLS Paker / Unpacker
* Basic code

16 changes: 12 additions & 4 deletions src/Command/PackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

// Inst file
}else if (
(strpos($content, "glgRecord") !== false) &&
(strpos($content, "record") !== false) &&
(strpos($content, "internalName") !== false) &&
(strpos($content, "entityClass") !== false)
){
Expand All @@ -103,7 +103,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
}


$this->packInst( $content, $saveTo);
$question = new ChoiceQuestion(
'Please provide the game (defaults to mh1 and mh2)',
array('mh1', 'mh2'),
'0'
);

$game = strtolower($helper->ask($input, $output, $question));

$this->packInst( $content, $saveTo, $game);

}else{
die("unable to detect file or unsupported");
Expand Down Expand Up @@ -188,8 +196,8 @@ private function packGLG($content, $saveTo){
file_put_contents($saveTo, $content);

}
private function packInst($content, $saveTo){
$content = $this->inst->pack( \json_decode($content, true) );
private function packInst($content, $saveTo, $game){
$content = $this->inst->pack( \json_decode($content, true), $game );
file_put_contents($saveTo, $content);

}
Expand Down
11 changes: 10 additions & 1 deletion src/Command/UnpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,16 @@ protected function execute(InputInterface $input, OutputInterface $output)

$output->writeln("INST file detected");

$unpacked = $this->inst->unpack( $content );

$question = new ChoiceQuestion(
'Please provide the game (defaults to mh1 and mh2)',
array('mh1', 'mh2'),
'0'
);

$game = strtolower($helper->ask($input, $output, $question));

$unpacked = $this->inst->unpack( $content, $game );

$outputTo = $folder . '/' . $filename . "." . $ext . ".json";

Expand Down
145 changes: 77 additions & 68 deletions src/Service/Archive/Inst.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Inst {
* Manhunt 2 - entity_pc.inst pack/unpack
*/

public function unpack($data){
public function unpack($data, $game){

$binary = new Binary( $data );

Expand All @@ -28,14 +28,14 @@ public function unpack($data){
$pos = 0;
foreach ($sizes as $size) {
$size = $size->toInt();
$records[] = $this->parseRecord( $content->substr($pos, $size) );
$records[] = $this->parseRecord( $content->substr($pos, $size), $game );
$pos += $size;
}

return $records;
}

public function pack( $records ){
public function pack( $records, $game ){

$result = new Binary();

Expand Down Expand Up @@ -92,38 +92,43 @@ public function pack( $records ){
$entry->addHex( str_repeat('70', $entityClass->getMissedBytes() - 1 ) );
}

/*
* Append parameters
*/
foreach ($record['parameters'] as $parameter) {
if ($game == "mh2"){

/*
* Append parameters
*/
foreach ($record['parameters'] as $parameter) {

$entry->addBinary(pack('L' , $parameter['parameterId']));
$entry->addBinary(pack('L' , $parameter['parameterId']));

$type = new Binary($parameter['type']);
$type->addMissedBytes('00');
$type = new Binary($parameter['type']);
$type->addMissedBytes('00');

$entry->append($type);
$entry->append($type);

switch ($parameter['type']) {
case 'flo':
$entry->addBinary(pack("f", $parameter['value']));
break;
case 'boo':
case 'int':
$entry->addBinary( pack("L", $parameter['value']) );
break;
case 'str':
switch ($parameter['type']) {
case 'flo':
$entry->addBinary(pack("f", $parameter['value']));
break;
case 'boo':
case 'int':
$entry->addBinary( pack("L", $parameter['value']) );
break;
case 'str':

$value = new Binary($parameter['value']);
$missedToFinal4Byte = $value->getMissedBytes() - 1;
$value = new Binary($parameter['value']);
$missedToFinal4Byte = $value->getMissedBytes() - 1;

$entry->append($value);
$entry->append($value);

$entry->addHex('00');
$entry->addHex( str_repeat('70', $missedToFinal4Byte ) );
$entry->addHex('00');
$entry->addHex( str_repeat('70', $missedToFinal4Byte ) );

break;
break;
}
}
}else{
$entry->addHex($record['parameters']);
}

$recordBin[] = $entry;
Expand All @@ -145,13 +150,13 @@ public function pack( $records ){
}


public function parseRecord( Binary $record ){
public function parseRecord( Binary $record, $game ){

/** @var Binary $remain */
/** @var Binary $rotation */

/**
* Find the Glg Record
* Find the Record
*/
$glgRecord = $record->substr(0, "\x00", $remain);
$remain = $remain->skipBytes($glgRecord->getMissedBytes());
Expand All @@ -178,51 +183,55 @@ public function parseRecord( Binary $record ){
$remain = $remain->skipBytes($entityClass->getMissedBytes());


/**
* Find parameters
*/
$params = [];
do {

// always 4-byte long
$parameterId = $remain->substr(0, 4, $remain);

if ($remain->length()){
if ($game == "mh2"){
/**
* Find parameters
*/
$params = [];
do {

// always 4-byte long
$type = $remain->substr(0, 4, $remain);

// float, boolean, integer are always 4-byte long
// string need to be calculated
switch ($type->toString()) {
case 'flo':
$value = $remain->substr(0, 4, $remain)->toFloat();
break;
case 'boo':
$value = $remain->substr(0, 4, $remain)->toBoolean();
break;
case 'int':
$value = $remain->substr(0, 4, $remain)->toInt();
break;
case 'str':

$value = $remain->substr(0, "\x00", $remain);
$remain = $remain->skipBytes($value->getMissedBytes());
$value = $value->toString();
break;
default:
var_dump($internalName);
die("type unknown " . $type->toHex());
$parameterId = $remain->substr(0, 4, $remain);
if ($remain->length()){

// always 4-byte long
$type = $remain->substr(0, 4, $remain);

// float, boolean, integer are always 4-byte long
// string need to be calculated
switch ($type->toString()) {
case 'flo':
$value = $remain->substr(0, 4, $remain)->toFloat();
break;
case 'boo':
$value = $remain->substr(0, 4, $remain)->toBoolean();
break;
case 'int':
$value = $remain->substr(0, 4, $remain)->toInt();
break;
case 'str':

$value = $remain->substr(0, "\x00", $remain);
$remain = $remain->skipBytes($value->getMissedBytes());
$value = $value->toString();
break;
default:
var_dump($internalName);
die("type unknown " . $type->toHex());
}

$params[] = [
'parameterId' => $parameterId->toInt(),
'type' => $type->toString(),
'value' => $value
];
}

$params[] = [
'parameterId' => $parameterId->toInt(),
'type' => $type->toString(),
'value' => $value
];
}
}while($remain->length());

}while($remain->length());
}else{
$params = $remain->toHex();
}

return [
'record' => $glgRecord->toBinary(),
Expand Down

0 comments on commit ffc8855

Please sign in to comment.