Skip to content

Instantly share code, notes, and snippets.

@qaisjp

qaisjp/pathfinding.go

Last active Aug 29, 2015
Embed
What would you like to do?
package main
import (
"io/ioutil"
"log"
"os"
"path"
"strings"
)
var PATHS []string = []string{
// Parent application path
path_app + "../",
// User applications dir
"$HOME/Applications/GTA San Andreas.app",
"$HOME/Applications/Grand Theft Auto - San Andreas.app",
// Steam
"$HOME/Library/Application Support/Steam/SteamApps/common/grand theft auto - san andreas",
// Macintosh HD applications
"/Applications/GTA San Andreas.app",
"/Applications/Grand Theft Auto - San Andreas.app",
}
// OPTIMISE PLEASE
func findPath() (string, bool) {
findpaths := true
if args := os.Args; (len(args) > 1) && (args[1] == "forcepathselect") {
findpaths = false
}
path := ""
if findpaths {
log.Print("Checking pathfile... ")
path = getPathfileResponse()
if isGTAInstalled(path) { // mtamac.app parent dir?
log.Println("Valid path found in pathfile")
return path, true
}
for _, p := range PATHS {
log.Println("Checking if GTA is inside: ", p)
if isGTAInstalled(os.ExpandEnv(p)) {
savePathFile(p)
return p, true
}
}
}
if !isGTAInstalled(path) { // Give up, use AppleScript to ask them.
log.Println("Couldn't detect, browsing for file")
gta := strings.Replace(cmd("/usr/bin/osascript", path_resources+"browse.scpt"), "\n", "", -1)
path = gta
}
if !isGTAInstalled(path) {
log.Println("GTA could not be found.")
alert("3", "Multi Theft Auto could not run.", "Please specify a valid Grand Theft Auto path!")
return "", false
}
savePathFile(path)
log.Println("Manual browse success!")
return path, true
}
func getPathfileResponse() string {
// First check if it exists
exists, _ := pathExists(path_pathfile)
if !exists {
log.Println("Pathfile doesn't exist!")
return ""
}
bytes, err := ioutil.ReadFile(path_pathfile)
if err != nil {
log.Println("Pathfile read error!")
return ""
}
// It does exist, try and read the contents and push it out
log.Println("Returning pathfile contents", string(bytes))
return string(bytes)
}
func savePathFile(p string) {
exists, _ := pathExists(path_pathfile)
if exists {
// if getPathfileResponse() == p {
// log.Println("Path didn't change. Skipping recreation.")
// return
// }
os.Remove(path_pathfile)
}
file, err := os.Create(path_pathfile)
if err != nil {
log.Println("Pathfile creation failed!")
}
_, err = file.WriteString(p)
if err != nil {
log.Println("Pathfile writing failed!")
}
log.Println("Pathfile saved!")
file.Close()
}
func isGTAInstalled(p string) bool {
// It's either a dummy or a bug, so
if p == "" {
return false
}
if isValidGTAExePath(p) {
return true
}
_, ext := path.Split(p)
return (ext == ".app") && (checkAppInstallation(p))
}
func checkAppInstallation(p string) bool {
return isValidGTAExePath(p + "/Contents/Resources/transgaming/c_drive/Program Files/Rockstar Games/GTA San Andreas")
}
// Checks if an executable or directory is a valid installation of GTA
func isValidGTAExePath(p string) bool {
leadpath, file := path.Split(p)
img, _ := pathExists(leadpath + "/models/gta3.img")
if img {
return true
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment