C++
#include <switch.h>
#include <libnx.h>
#include <devkitpro.h>
#include <swipc.h>
#include <stratosphere.h>
// Install an NSP game to an external USB drive.
void InstallNSPGameToUSB(const char* nsp_file, const char* usb_path) {
// Open the NSP game file.
FILE* file = fopen(nsp_file, "rb");
if (!file) {
return;
}
// Get the file size.
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
fseek(file, 0, SEEK_SET);
// Allocate a buffer to store the NSP game file.
uint8_t* buffer = (uint8_t*)malloc(size);
if (!buffer) {
fclose(file);
return;
}
// Read the NSP game file into the buffer.
fread(buffer, 1, size, file);
fclose(file);
// Open the USB device.
handle_t usb_device;
swipc_usb_open(&usb_device, usb_path);
// Write the NSP game file to the USB device.
swipc_usb_write(usb_device, buffer, size);
// Close the USB device.
swipc_usb_close(usb_device);
// Free the buffer.
free(buffer);
}
// Uninstall an NSP game from an external USB drive.
void UninstallNSPGameFromUSB(const char* usb_path) {
// Open the USB device.
handle_t usb_device;
swipc_usb_open(&usb_device, usb_path);
// Remove the NSP game file from the USB device.
swipc_usb_remove(usb_device, "NSP");
// Close the USB device.
swipc_usb_close(usb_device);
}
// Run an NSP game from an external USB drive.
void RunNSPGameFromUSB(const char* usb_path) {
// Open the USB device.
handle_t usb_device;
swipc_usb_open(&usb_device, usb_path);
// Read the NSP game file from the USB device.
size_t size = swipc_usb_get_file_size(usb_device, "NSP");
uint8_t* buffer = (uint8_t*)malloc(size);
swipc_usb_read(usb_device, buffer, size, 0);
// Close the USB device.
swipc_usb_close(usb_device);
// Load the NSP game file into memory.
nx_applet_load_package_memory(buffer, size, 0);
// Start the NSP game.
nx_applet_start();
// Wait for the NSP game to exit.
nx_applet_wait();
// Free the buffer.
free(buffer);
}
int main(void) {
// Initialize the Nintendo Switch.
switch_init();
// Get the path to the NSP game file.
char* nsp_file = switch_select_file();
if (!nsp_file) {
switch_exit();
return 1;
}
// Get the path to the external USB drive.
char* usb_path = switch_select_path();
if (!usb_path) {
switch_exit();
return 1;
}
// Install the NSP game to the external USB drive.
InstallNSPGameToUSB(nsp_file, usb_path);
// Run the NSP game from the external USB drive.
RunNSPGameFromUSB(usb_path);
// Uninstall the NSP game from the external USB drive.
UninstallNSPGameFromUSB(usb_path);
// Exit the Nintendo Switch.
switch_exit();
return 0;
}
Or this
C++
#include <devkitA64.h>
#include <libnx.h>
#include <swIPC.h>
#include <stratosphere.h>
#include <string>
#include <vector>
using namespace std;
// The path to the USB drive
const char* usbPath = "/mnt/usb0";
// The title ID of the game to install
const char* titleId = "01003A600C2A0000";
// The path to the game file
const char* gamePath = "/path/to/game.xci";
// Installs the game to the USB drive
void installGame(const char* titleId, const char* gamePath) {
// Initialize the stratosphere library
stratosphereInit();
// Open the game file
FILE* gameFile = fopen(gamePath, "rb");
if (gameFile == nullptr) {
// Handle error
return;
}
// Get the size of the game file
fseek(gameFile, 0, SEEK_END);
size_t gameSize = ftell(gameFile);
fseek(gameFile, 0, SEEK_SET);
// Allocate a buffer for the game file
char* gameBuffer = (char*)malloc(gameSize);
if (gameBuffer == nullptr) {
// Handle error
return;
}
// Read the game file into the buffer
fread(gameBuffer, 1, gameSize, gameFile);
fclose(gameFile);
// Install the game to the USB drive
stratosphereInstallGame(titleId, gameBuffer, gameSize);
// Free the game buffer
free(gameBuffer);
}
// Runs the game from the USB drive
void runGame(const char* titleId) {
// Initialize the swIPC library
swIPCInit();
// Open the game process
Handle gameProcess = swIPCOpenProcess(titleId);
if (gameProcess == 0) {
// Handle error
return;
}
// Start the game process
swIPCStartProcess(gameProcess);
// Wait for the game process to exit
swIPCWaitProcess(gameProcess);
}
int main() {
// Initialize the devkitA64 library
devkitA64Init();
// Install the game to the USB drive
installGame(titleId, gamePath);
// Run the game from the USB drive
runGame(titleId);
// Finalize the devkitA64 library
devkitA64Exit();
return 0;
}
#include <switch.h>
#include <libnx.h>
#include <devkitpro.h>
#include <swipc.h>
#include <stratosphere.h>
// Install an NSP game to an external USB drive.
void InstallNSPGameToUSB(const char* nsp_file, const char* usb_path) {
// Open the NSP game file.
FILE* file = fopen(nsp_file, "rb");
if (!file) {
return;
}
// Get the file size.
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
fseek(file, 0, SEEK_SET);
// Allocate a buffer to store the NSP game file.
uint8_t* buffer = (uint8_t*)malloc(size);
if (!buffer) {
fclose(file);
return;
}
// Read the NSP game file into the buffer.
fread(buffer, 1, size, file);
fclose(file);
// Open the USB device.
handle_t usb_device;
swipc_usb_open(&usb_device, usb_path);
// Write the NSP game file to the USB device.
swipc_usb_write(usb_device, buffer, size);
// Close the USB device.
swipc_usb_close(usb_device);
// Free the buffer.
free(buffer);
}
// Uninstall an NSP game from an external USB drive.
void UninstallNSPGameFromUSB(const char* usb_path) {
// Open the USB device.
handle_t usb_device;
swipc_usb_open(&usb_device, usb_path);
// Remove the NSP game file from the USB device.
swipc_usb_remove(usb_device, "NSP");
// Close the USB device.
swipc_usb_close(usb_device);
}
// Run an NSP game from an external USB drive.
void RunNSPGameFromUSB(const char* usb_path) {
// Open the USB device.
handle_t usb_device;
swipc_usb_open(&usb_device, usb_path);
// Read the NSP game file from the USB device.
size_t size = swipc_usb_get_file_size(usb_device, "NSP");
uint8_t* buffer = (uint8_t*)malloc(size);
swipc_usb_read(usb_device, buffer, size, 0);
// Close the USB device.
swipc_usb_close(usb_device);
// Load the NSP game file into memory.
nx_applet_load_package_memory(buffer, size, 0);
// Start the NSP game.
nx_applet_start();
// Wait for the NSP game to exit.
nx_applet_wait();
// Free the buffer.
free(buffer);
}
int main(void) {
// Initialize the Nintendo Switch.
switch_init();
// Get the path to the NSP game file.
char* nsp_file = switch_select_file();
if (!nsp_file) {
switch_exit();
return 1;
}
// Get the path to the external USB drive.
char* usb_path = switch_select_path();
if (!usb_path) {
switch_exit();
return 1;
}
// Install the NSP game to the external USB drive.
InstallNSPGameToUSB(nsp_file, usb_path);
// Run the NSP game from the external USB drive.
RunNSPGameFromUSB(usb_path);
// Uninstall the NSP game from the external USB drive.
UninstallNSPGameFromUSB(usb_path);
// Exit the Nintendo Switch.
switch_exit();
return 0;
}
Post automatically merged:
Or this
C++
#include <devkitA64.h>
#include <libnx.h>
#include <swIPC.h>
#include <stratosphere.h>
#include <string>
#include <vector>
using namespace std;
// The path to the USB drive
const char* usbPath = "/mnt/usb0";
// The title ID of the game to install
const char* titleId = "01003A600C2A0000";
// The path to the game file
const char* gamePath = "/path/to/game.xci";
// Installs the game to the USB drive
void installGame(const char* titleId, const char* gamePath) {
// Initialize the stratosphere library
stratosphereInit();
// Open the game file
FILE* gameFile = fopen(gamePath, "rb");
if (gameFile == nullptr) {
// Handle error
return;
}
// Get the size of the game file
fseek(gameFile, 0, SEEK_END);
size_t gameSize = ftell(gameFile);
fseek(gameFile, 0, SEEK_SET);
// Allocate a buffer for the game file
char* gameBuffer = (char*)malloc(gameSize);
if (gameBuffer == nullptr) {
// Handle error
return;
}
// Read the game file into the buffer
fread(gameBuffer, 1, gameSize, gameFile);
fclose(gameFile);
// Install the game to the USB drive
stratosphereInstallGame(titleId, gameBuffer, gameSize);
// Free the game buffer
free(gameBuffer);
}
// Runs the game from the USB drive
void runGame(const char* titleId) {
// Initialize the swIPC library
swIPCInit();
// Open the game process
Handle gameProcess = swIPCOpenProcess(titleId);
if (gameProcess == 0) {
// Handle error
return;
}
// Start the game process
swIPCStartProcess(gameProcess);
// Wait for the game process to exit
swIPCWaitProcess(gameProcess);
}
int main() {
// Initialize the devkitA64 library
devkitA64Init();
// Install the game to the USB drive
installGame(titleId, gamePath);
// Run the game from the USB drive
runGame(titleId);
// Finalize the devkitA64 library
devkitA64Exit();
return 0;
}
Last edited by Mokiki,