Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Commit

Permalink
Move merging functionality to holocure
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniruKun committed Oct 22, 2022
1 parent 9ce1938 commit 55b1df6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ E.g. `tasukeru-windows-amd64.exe saveA.dat save.dat` will produce the patched `s
On Unix systems you can quickly inspect a save file with

```sh
base64 --decode -i save.dat`
base64 --decode -i save.dat
```
32 changes: 31 additions & 1 deletion holocure/holocure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package holocure

import (
b64 "encoding/base64"
"fmt"
"os"
"path/filepath"
)
Expand All @@ -22,7 +23,7 @@ func FindSaveBlockStartEnd(data *[]byte) (start, end int) {
if char == 0x7B && (*data)[offset+1] == 0x20 {
start = offset
}
if char == 0x7D && (*data)[offset+1] == 0x00 {
if char == 0x7D && (*data)[offset-1] == 0x20 {
end = offset
}
}
Expand All @@ -45,6 +46,35 @@ func WriteSaveFile(filePath string, data []byte) error {
return os.WriteFile(filePath, []byte(targetEnc), 0644)
}

// Merges the source save file path into the target one.
// Returns the raw bytes of the patches save.
func MergeSaves(sourceSaveFilePath, targetSaveFilePath string) []byte {
fmt.Println("reading origin save file", sourceSaveFilePath)
srcDec, err := DecodeSaveFile(sourceSaveFilePath)
check(err)
var start, end int

start, end = FindSaveBlockStartEnd(&srcDec)
srcSaveBlock := srcDec[start : end+1]

targetDec, err := DecodeSaveFile(targetSaveFilePath)
check(err)

start, _ = FindSaveBlockStartEnd(&targetDec)

// iterate over the source save block and overwrite the dst save block with its data
for i, char := range srcSaveBlock {
targetOffset := start + i

if targetOffset >= len(targetDec) {
targetDec = append(targetDec, char)
} else {
targetDec[targetOffset] = char
}
}
return targetDec
}

func check(e error) {
if e != nil {
panic(e)
Expand Down
26 changes: 2 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,47 +59,25 @@ func main() {
os.Exit(1)
}

var start, end int
var sourceSaveFilePath, targetSaveFilePath string

sourceSaveFilePath = args[1]
fmt.Println("reading origin save file", sourceSaveFilePath)

srcDec, err := holocure.DecodeSaveFile(sourceSaveFilePath)
check(err)

start, end = holocure.FindSaveBlockStartEnd(&srcDec)
srcSaveBlock := srcDec[start : end+1]

if len(args) == 3 {
targetSaveFilePath = args[2]
} else {
targetSaveFilePath = holocure.SaveFilePath()
}

targetDec, err := holocure.DecodeSaveFile(targetSaveFilePath)
check(err)

start, _ = holocure.FindSaveBlockStartEnd(&targetDec)

// iterate over the source save block and overwrite the dst save block with its data
for i, char := range srcSaveBlock {
targetOffset := start + i

if targetOffset >= len(targetDec) {
targetDec = append(targetDec, char)
} else {
targetDec[targetOffset] = char
}
}
targetDec := holocure.MergeSaves(sourceSaveFilePath, targetSaveFilePath)

// fmt.Println("patched save:", string(targetDec))
fmt.Println()

var confirmed bool = prompter.YN("import new save file? インポートOK?", true)

if confirmed {
err = holocure.WriteSaveFile(targetSaveFilePath, targetDec)
err := holocure.WriteSaveFile(targetSaveFilePath, targetDec)
check(err)
fmt.Println("save file imported succesfully!")
waitQuit()
Expand Down

0 comments on commit 55b1df6

Please sign in to comment.