This repository has been archived by the owner on Feb 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move HoloCure related logic into sep package
- Loading branch information
Showing
2 changed files
with
65 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters