github.com/racerxdl/gonx@v0.0.0-20210103083128-c5afc43bcbd2/nintendoswitch/switch.go (about)

     1  package nintendoswitch
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/racerxdl/gonx/nx/env"
     6  	"github.com/racerxdl/gonx/services/am"
     7  	"github.com/racerxdl/gonx/services/display"
     8  	"github.com/racerxdl/gonx/services/gpu"
     9  	"github.com/racerxdl/gonx/services/sm"
    10  	"github.com/racerxdl/gonx/services/vi"
    11  	"github.com/racerxdl/gonx/svc"
    12  )
    13  
    14  // Configure initializes all necessary services for gonx works
    15  // Make sure you call Cleanup before exiting the program or it may crash the HorizonOS
    16  func Configure() (err error) {
    17  	x0 := svc.GetContextPtr()
    18  	x1 := svc.GetMainThreadHandle()
    19  
    20  	if x0 != 0 && x1 == (1<<64-1) {
    21  		fmt.Println("TODO: exception to handle")
    22  		svc.Break(0x5EF0D30, 0x1234, 0x4321)
    23  		select {}
    24  	}
    25  
    26  	smInit := false
    27  	amInit := false
    28  	gpuInit := false
    29  	viInit := false
    30  	displayInit := false
    31  
    32  	defer func() {
    33  		if err != nil {
    34  			if displayInit {
    35  				display.Finalize()
    36  			}
    37  			if viInit {
    38  				vi.Finalize()
    39  			}
    40  			if gpuInit {
    41  				gpu.Finalize()
    42  			}
    43  			if amInit {
    44  				am.Finalize()
    45  			}
    46  			if smInit {
    47  				sm.Finalize()
    48  			}
    49  		}
    50  	}()
    51  
    52  	err = env.LoadEnv()
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	err = sm.Init()
    58  	if err != nil {
    59  		return err
    60  	}
    61  	smInit = true
    62  
    63  	err = am.Init()
    64  	if err != nil {
    65  		return err
    66  	}
    67  	amInit = true
    68  
    69  	err = gpu.Init()
    70  	if err != nil {
    71  		return err
    72  	}
    73  	gpuInit = true
    74  
    75  	err = vi.Init()
    76  	if err != nil {
    77  		return err
    78  	}
    79  	viInit = true
    80  
    81  	err = display.Init()
    82  	if err != nil {
    83  		return err
    84  	}
    85  	displayInit = true
    86  
    87  	return nil
    88  }
    89  
    90  // Cleanup cleans up all resources created by Configure
    91  // It is important to cleanup resources otherwise Horizon might crash!
    92  func Cleanup() {
    93  	display.Finalize()
    94  	vi.Finalize()
    95  	gpu.Finalize()
    96  	sm.Finalize()
    97  }