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

Commit

Permalink
Move HoloCure related logic into sep package
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniruKun committed Oct 22, 2022
1 parent c83d6d6 commit 9ce1938
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 40 deletions.
52 changes: 52 additions & 0 deletions holocure/holocure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package holocure

import (
b64 "encoding/base64"
"os"
"path/filepath"
)

const defaultSaveFileName = "save.dat"

// Returns the default HoloCure save file location for the current platform.
func SaveFilePath() string {
dir, err := os.UserCacheDir()
check(err)
return filepath.Join(dir, "HoloCure", defaultSaveFileName)
}

// It seems that the start offset will always be the same
// across machines, but safer to find save block dynamically
func FindSaveBlockStartEnd(data *[]byte) (start, end int) {
for offset, char := range *data {
if char == 0x7B && (*data)[offset+1] == 0x20 {
start = offset
}
if char == 0x7D && (*data)[offset+1] == 0x00 {
end = offset
}
}
return
}

func Decode(data []byte) ([]byte, error) {
return b64.URLEncoding.DecodeString(string(data))
}

func DecodeSaveFile(filePath string) ([]byte, error) {
srcDat, err := os.ReadFile(filePath)
check(err)
srcDec, err := Decode(srcDat)
return srcDec, err
}

func WriteSaveFile(filePath string, data []byte) error {
targetEnc := b64.URLEncoding.EncodeToString(data)
return os.WriteFile(filePath, []byte(targetEnc), 0644)
}

func check(e error) {
if e != nil {
panic(e)
}
}
53 changes: 13 additions & 40 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package main

import (
b64 "encoding/base64"
"fmt"
"os"
"path/filepath"

"github.com/DaniruKun/tasukeru/holocure"
"github.com/Songmu/prompter"
)

const defaultSaveFileName = "save.dat"
const version = "1.0"

func check(e error) {
Expand Down Expand Up @@ -44,31 +42,14 @@ I am not affiliated with Cover Corp. or Kay Yu in any way.
fmt.Printf(header, version)
}

func holoCureSaveFilePath() string {
dir, err := os.UserCacheDir()
check(err)
return filepath.Join(dir, "HoloCure", defaultSaveFileName)
}

// generally it seems that the start offset will always be the same
// across machines, but safer to find save block dynamically
func getSaveBlockStartEnd(srcDec *[]byte) (start, end int) {
for offset, char := range *srcDec {
if char == 0x7B && (*srcDec)[offset+1] == 0x20 {
start = offset
}
if char == 0x7D && (*srcDec)[offset+1] == 0x00 {
end = offset
}
}
return
}

func main() {
printHeader()
args := os.Args

if len(args) < 2 {

// Run the UI

// normally when used as drag n drop on windows, will be exactly 2
fmt.Println("not enough arguments provided")
fmt.Println("did you Drag n Drop the source save file onto tasukeru.exe ?")
Expand All @@ -79,34 +60,27 @@ func main() {
}

var start, end int
var sourceSaveFilePath, targetSaveFilePath string

sourceSaveFilePath := args[1]

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

srcDat, err := os.ReadFile(sourceSaveFilePath)
check(err)
srcDec, err := b64.URLEncoding.DecodeString(string(srcDat))
srcDec, err := holocure.DecodeSaveFile(sourceSaveFilePath)
check(err)

start, end = getSaveBlockStartEnd(&srcDec)

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

var targetFilePath string

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

targetDat, err := os.ReadFile(targetFilePath)
check(err)
targetDec, err := b64.URLEncoding.DecodeString(string(targetDat))
targetDec, err := holocure.DecodeSaveFile(targetSaveFilePath)
check(err)

start, _ = getSaveBlockStartEnd(&targetDec)
start, _ = holocure.FindSaveBlockStartEnd(&targetDec)

// iterate over the source save block and overwrite the dst save block with its data
for i, char := range srcSaveBlock {
Expand All @@ -125,8 +99,7 @@ func main() {
var confirmed bool = prompter.YN("import new save file? インポートOK?", true)

if confirmed {
targetEnc := b64.URLEncoding.EncodeToString(targetDec)
err = os.WriteFile(targetFilePath, []byte(targetEnc), 0644)
err = holocure.WriteSaveFile(targetSaveFilePath, targetDec)
check(err)
fmt.Println("save file imported succesfully!")
waitQuit()
Expand Down

0 comments on commit 9ce1938

Please sign in to comment.