Skip to content

Commit

Permalink
Make MHK 2 packing and unpacking finally work
Browse files Browse the repository at this point in the history
  • Loading branch information
SKevo18 committed Feb 11, 2024
1 parent 02ec969 commit bc93304
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 38 deletions.
2 changes: 1 addition & 1 deletion pkg/transformers/mhk_1.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ func transformMhk1(action string, dataFileLocation string, rootFolder string) er
case "unpack":
return unpackMhk1(dataFileLocation, rootFolder)
default:
return errors.New("Invalid action!")
return errors.New("invalid action")
}
}
30 changes: 11 additions & 19 deletions pkg/transformers/mhk_2.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ import (

// Packs MHK2 data file from given `inputFolder` into `dataFileLocation`.
func packMhk2(dataFileLocation string, inputPath string) error {
log.Printf("Creating `%s`...", dataFileLocation)
outFile, err := os.Create(dataFileLocation)
files, err := walkFiles(inputPath)
if err != nil {
return err
}
defer outFile.Close()

files, err := walkFiles(inputPath)
log.Printf("Packing %d files...", len(files))
outFile, err := os.Create(dataFileLocation)
if err != nil {
return err
}
log.Printf("Packing %d files...", len(files))
defer outFile.Close()

// header
log.Println("Generating header...")
Expand All @@ -35,12 +34,10 @@ func packMhk2(dataFileLocation string, inputPath string) error {

// save file entry data
log.Println("Saving file entries data...")
offset := int64(0x40) + int64(len(files)*0x80)
offset := int64(0x40 + len(files) * 0x80)
for _, file := range files {
fileEntry := make([]byte, 0x80)
relativePath, _ := filepath.Rel(inputPath, file.Filename)
pathWithPrefix := strings.ReplaceAll(relativePath, "/", "\\")
copy(fileEntry, pathWithPrefix)
copy(fileEntry, strings.ReplaceAll(file.Filename, "/", "\\"))

binary.LittleEndian.PutUint64(fileEntry[0x68:], uint64(offset))
binary.LittleEndian.PutUint64(fileEntry[0x6C:], uint64(file.Filesize))
Expand All @@ -65,18 +62,13 @@ func packMhk2(dataFileLocation string, inputPath string) error {
}

// write data
paddingLength := 0x100 - (len(fileData) % 0x100)
if paddingLength == 0x100 {
paddingLength = 0 // no padding needed if `fileData` is already aligned
}
padding := make([]byte, paddingLength)

log.Printf("Writing `%s`...", file.Filename)
padding := make([]byte, file.Filesize % 0x100)
if _, err := outFile.Write(append(fileData, padding...)); err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -160,15 +152,15 @@ func unpackMhk2(dataFileLocation string, outputPath string) error {
// Extracts the file position from a file entry block.
func getPosition(fileEntry []byte) (uint32, error) {
if len(fileEntry) < 104 {
return 0, errors.New("Input data must have at least 104 bytes")
return 0, errors.New("input data must have at least 104 bytes")
}
return binary.LittleEndian.Uint32(fileEntry[0x68:0x6C]), nil
}

// Extracts the file length from a file entry block.
func getFileLength(fileEntry []byte) (uint32, error) {
if len(fileEntry) < 108 {
return 0, errors.New("Input data must have at least 108 bytes")
return 0, errors.New("input data must have at least 108 bytes")
}
return binary.LittleEndian.Uint32(fileEntry[0x6C:0x70]), nil
}
Expand Down Expand Up @@ -240,6 +232,6 @@ func transformMhk2(action string, dataFileLocation string, rootFolder string) er
case "unpack":
return unpackMhk2(dataFileLocation, rootFolder)
default:
return errors.New("Invalid action!")
return errors.New("invalid action")
}
}
2 changes: 1 addition & 1 deletion pkg/transformers/mhk_3.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ func transformMhk3(action string, dataFileLocation string, rootFolder string) er
case "unpack":
return unpackMhk3(dataFileLocation, rootFolder)
default:
return errors.New("Invalid action!")
return errors.New("invalid action")
}
}
2 changes: 1 addition & 1 deletion pkg/transformers/mhk_4.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ func transformMhk4(action string, dataFileLocation string, rootFolder string) er
case "unpack":
return unpackMhk4(dataFileLocation, rootFolder)
default:
return errors.New("Invalid action!")
return errors.New("invalid action")
}
}
13 changes: 6 additions & 7 deletions pkg/transformers/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type FileEntry struct {
Filesize int64
}

// Walks through files in `rootFolder` and returns array of file entries.
// Walks through files in `rootFolder` and returns array of relative file entries.
func walkFiles(rootFolder string) ([]FileEntry, error) {
var files []FileEntry

Expand All @@ -40,7 +40,7 @@ func walkFiles(rootFolder string) ([]FileEntry, error) {
return nil
})
if err != nil {
return nil, errors.New(fmt.Sprintf("error walking through files: %s", err))
return nil, fmt.Errorf("error walking through files: %s", err)
}

return files, nil
Expand All @@ -52,7 +52,7 @@ func walkFiles(rootFolder string) ([]FileEntry, error) {
func xorData(data []byte, key []byte) []byte {
keyLength := len(key)
for i := range data {
data[i] ^= key[i%keyLength]
data[i] ^= key[i % keyLength]
}
return data
}
Expand All @@ -72,14 +72,13 @@ func Transform(action string, gameId string, dataFileLocation string, rootFolder
case "mhk_4", "mhk_thunder":
transformFunction = transformMhk4
default:
return errors.New("Invalid game ID! Please, use one of the following: `mhk_extra`, `mhk_1`, `mhk_2`, `schatzjaeger`, `mhk_3`, `mhk_4`, `mhk_thunder`.")
return errors.New("invalid game ID! Please, use one of the following: `mhk_extra`, `mhk_1`, `mhk_2`, `schatzjaeger`, `mhk_3`, `mhk_4`, `mhk_thunder`")
}

var err error
err = transformFunction(action, dataFileLocation, rootFolder)
err := transformFunction(action, dataFileLocation, rootFolder)

if err != nil {
return errors.New(fmt.Sprintf("Error transforming data: %s", err))
return fmt.Errorf("error transforming data: %s", err)
}

return nil
Expand Down
17 changes: 8 additions & 9 deletions pkg/transformers/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package transformers

import (
"archive/zip"
"errors"
"fmt"
"io"
"os"
Expand All @@ -12,13 +11,13 @@ import (
// Unzip a file into a folder.
func unzipFile(zipFile string, outputFolder string) error {
if err := os.MkdirAll(outputFolder, os.ModePerm); err != nil {
return errors.New(fmt.Sprintf("error creating output folder: %s", err))
return fmt.Errorf("error creating output folder: %s", err)
}

// unzip
zipReader, err := zip.OpenReader(zipFile)
if err != nil {
return errors.New(fmt.Sprintf("error opening ZIP file: %s", err))
return fmt.Errorf("error opening ZIP file: %s", err)
}
defer zipReader.Close()

Expand All @@ -28,23 +27,23 @@ func unzipFile(zipFile string, outputFolder string) error {
// read
fileReader, err := file.Open()
if err != nil {
return errors.New(fmt.Sprintf("error opening file: %s", err))
return fmt.Errorf("error opening file: %s", err)
}
defer fileReader.Close()

if err = os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
return errors.New(fmt.Sprintf("error creating directory: %s", err))
return fmt.Errorf("error creating directory: %s", err)
}

// write
outFile, err := os.Create(filePath)
if err != nil {
return errors.New(fmt.Sprintf("error creating file: %s", err))
return fmt.Errorf("error creating file: %s", err)
}
defer outFile.Close()

if _, err = io.Copy(outFile, fileReader); err != nil {
return errors.New(fmt.Sprintf("error copying file: %s", err))
return fmt.Errorf("error copying file: %s", err)
}
}

Expand All @@ -56,7 +55,7 @@ func zipFolder(folder string, zipFile string) error {
// open
outFile, err := os.Create(zipFile)
if err != nil {
return errors.New(fmt.Sprintf("error creating ZIP file: %s", err))
return fmt.Errorf("error creating ZIP file: %s", err)
}
defer outFile.Close()
zipWriter := zip.NewWriter(outFile)
Expand Down Expand Up @@ -100,7 +99,7 @@ func zipFolder(folder string, zipFile string) error {
return nil
})
if err != nil {
return errors.New(fmt.Sprintf("error while walking through files: %s", err))
return fmt.Errorf("error while walking through files: %s", err)
}

return nil
Expand Down

0 comments on commit bc93304

Please sign in to comment.