Is this proper code for external harddrive installing?

Mokiki

Active Member
OP
Newcomer
Joined
May 8, 2018
Messages
29
Trophies
0
Age
46
XP
373
Country
United States
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;
}
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,

masagrator

The patches guy
Developer
Joined
Oct 14, 2018
Messages
6,327
Trophies
3
XP
12,164
Country
Poland
First of all, use code blocks.

Second, you could just compile code instead of asking? This way you will know.

And even if this would compile, you really expect that 3 GB of RAM will store any game that is bigger than 3GB? Because this is what you are trying to do, load whole xci/nsp to RAM instead of streaming.

There are already tools for that, why do you want to reinvent the wheel?

Edit: now when I'm looking at this code closer, do you use some ChatGPT or you are making this up yourself? There is ton of non existing shit and useless stuff in such small code snippets. WTF is "devkitA64Init" or "stratosphereInit"? Stratosphere is written in C++, so why header is ".h"? WTF is "libnx.h", "devkitA64.h" or "devkitpro.h"?

Are you mocking us or are you a bot? If you will answer with another code snippet, I will report you as bot since not only you have no idea what you are doing, you are also making shit up.
 
Last edited by masagrator,

barabanov

Member
Newcomer
Joined
Apr 23, 2021
Messages
18
Trophies
0
Age
34
XP
185
Country
Brazil
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;
}
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;
}
what you want?
install games via usb? there are some tools for this

or

install in hdd/hard drive and play WITHOUT install on microsd/emunand? just like Wiiflow in Wii?
 

Jayinem

Well-Known Member
Member
Joined
Dec 16, 2021
Messages
721
Trophies
0
XP
1,747
Country
United States
I believe the option to play games off an external hdd has been dead since SX OS died 3 or so years ago. Unless there's a new way that I was unaware of.
 

Hayato213

Newcomer
Member
Joined
Dec 26, 2015
Messages
20,096
Trophies
1
XP
21,301
Country
United States
SX OS only support up to firmware 11.0.0 and the team behind it been dead for the last few years. Don't expect Atmosphere to get XCI mounting or install to HDD support.
 
  • Love
Reactions: impeeza

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    Psionic Roshambo @ Psionic Roshambo: @BigOnYa, FarCry 5 is an awesome game with multiple endings!