github.com/racerxdl/gonx@v0.0.0-20210103083128-c5afc43bcbd2/nx/env/env.go (about) 1 package env 2 3 import ( 4 "github.com/racerxdl/gonx/nx/nxtypes" 5 "github.com/racerxdl/gonx/svc" 6 "unsafe" 7 ) 8 9 type configEntry struct { 10 Key uint32 11 Flags uint32 12 Value [2]uint64 13 } 14 15 var argvPtr uintptr 16 var syscallHints [2]uint64 17 var appletType = nxtypes.AppletTypeDefault 18 var processHandle uint64 19 var lastLoadResult uint64 20 var randomSeed [2]uint64 21 var userIdStorage uintptr 22 var hosVersion uint64 23 24 func LoadEnv() error { 25 ctxPtr := svc.GetContextPtr() 26 if ctxPtr != 0 { 27 ptr := ctxPtr 28 // See https://switchbrew.org/w/index.php?title=Homebrew_ABI 29 entry := (*configEntry)(unsafe.Pointer(ptr)) 30 for entry.Key != envEntryTypeEndOfList { 31 switch entry.Key { 32 case envEntryTypeMainThreadHandle: // Handled by tinyGo 33 case envEntryTypeNextLoadPath: 34 case envEntryTypeOverrideHeap: // Handled by tinyGo 35 case envEntryTypeOverrideService: 36 case envEntryTypeArgv: 37 argvPtr = uintptr(entry.Value[1]) 38 case envEntryTypeSyscallAvailableHint: 39 syscallHints[0] = entry.Value[0] 40 syscallHints[1] = entry.Value[1] 41 case envEntryTypeAppletType: 42 appletType = nxtypes.AppletType(entry.Value[0]) 43 case envEntryTypeAppletWorkaround: 44 case envEntryTypeReserved9: 45 case envEntryTypeProcessHandle: 46 processHandle = entry.Value[0] 47 case envEntryTypeLastLoadResult: 48 lastLoadResult = entry.Value[0] 49 case envEntryTypeRandomSeed: 50 randomSeed[0] = entry.Value[0] 51 randomSeed[1] = entry.Value[1] 52 case envEntryTypeUserIdStorage: 53 userIdStorage = uintptr(entry.Value[0]) 54 case envEntryTypeHosVersion: 55 hosVersion = entry.Value[0] 56 } 57 ptr += unsafe.Sizeof(configEntry{}) 58 entry = (*configEntry)(unsafe.Pointer(ptr)) 59 } 60 } 61 62 return nil 63 } 64 65 // GetAppletType returns the Applet Type reported by homebrew launcher 66 func GetAppletType() nxtypes.AppletType { 67 return nxtypes.AppletType(appletType) 68 }