Homebrew Does this .py code make sense

Mokiki

Active Member
OP
Newcomer
Joined
May 8, 2018
Messages
29
Trophies
0
Age
46
XP
373
Country
United States
import subprocess

def install_nsp(nsp_file, usb_drive):
"""Installs an NSP file to a USB drive.

Args:
nsp_file: The path to the NSP file.
usb_drive: The path to the USB drive.
"""

command = ["tinfoil", "install", nsp_file, "--output-dir", usb_drive]
subprocess.run(command, check=True)

def run_nsp(nsp_file, usb_drive):
"""Runs an NSP file from a USB drive.

Args:
nsp_file: The path to the NSP file on the USB drive.
usb_drive: The path to the USB drive.
"""

command = ["tinfoil", "run", nsp_file, "--usb-storage", usb_drive]
subprocess.run(command, check=True)

def main():
"""The main function."""

nsp_file = input("Enter the path to the NSP file: ")
usb_drive = input("Enter the path to the USB drive: ")

install_nsp(nsp_file, usb_drive)
run_nsp(nsp_file, usb_drive)

if __name__ == "__main__":
main()
 

mrdude

Developer
Developer
Joined
Dec 11, 2015
Messages
3,071
Trophies
1
Age
56
XP
8,237

Mokiki

Active Member
OP
Newcomer
Joined
May 8, 2018
Messages
29
Trophies
0
Age
46
XP
373
Country
United States
To run python on a switch you would need to install a python interpreter, there's this here:

https://github.com/nx-python/PyNX

You can also apparently run lua scripting:

https://github.com/MstrVLT/switch_lua

As for the OP, that's just a tiny snippet of python code - where's the entire code and all the libraries?
import nx
import os

def install_nsp(nsp_file, usb_drive):
"""Installs an NSP game to a USB drive.

Args:
nsp_file: The path to the NSP file.
usb_drive: The path to the USB drive.
"""

# Create a temporary directory to store the extracted NSP files.
temp_dir = os.path.join(usb_drive, ".nsp_temp")
os.makedirs(temp_dir, exist_ok=True)

# Extract the NSP files to the temporary directory.
nx.archive.extract_nsp(nsp_file, temp_dir)

# Move the NSP files to the appropriate location on the USB drive.
nx.file.move(temp_dir, usb_drive)

# Delete the temporary directory.
os.rmdir(temp_dir)

def run_nsp(nsp_file, usb_drive):
"""Runs an NSP game from a USB drive.

Args:
nsp_file: The path to the NSP file.
usb_drive: The path to the USB drive.
"""

# Mount the USB drive.
nx.mount.mount_usb_drive(usb_drive)

# Start the NSP game.
nx.app.start_title(nsp_file)

# Unmount the USB drive.
nx.mount.unmount_usb_drive(usb_drive)

def main():
"""The main entry point for the homebrew app."""

# Get the path to the USB drive.
usb_drive = nx.interface.get_usb_drive_path()

# If the USB drive is not mounted, prompt the user to insert it.
if not nx.mount.is_usb_drive_mounted(usb_drive):
nx.console.log("Please insert a USB drive.")
nx.input.wait_for_button_press()

# Get the path to the NSP file.
nsp_file = nx.file.select_file()

# Install the NSP game to the USB drive.
install_nsp(nsp_file, usb_drive)

# Run the NSP game from the USB drive.
run_nsp(nsp_file, usb_drive)

if __name__ == "__main__":
main()



It just says:
To compile this Python application into a homebrew app, you can use the following command:

nx-build main.py
Post automatically merged:

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;
}
 
Last edited by Mokiki,

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • Veho @ Veho:
    It's not a Nintendo / iQue official product, it's a 3rd party custom.
    +1
  • Veho @ Veho:
    Nothing special about it other than it's more comfortable than the Lite
    for people with beefy hands.
    +1
  • Jayro @ Jayro:
    I have yaoi anime hands, very lorge but slender.
  • Jayro @ Jayro:
    I'm Slenderman.
  • Veho @ Veho:
    I have hands.
  • BakerMan @ BakerMan:
    imagine not having hands, cringe
    +1
  • AncientBoi @ AncientBoi:
    ESPECIALLY for things I do to myself :sad:.. :tpi::rofl2: Or others :shy::blush::evil:
    +1
  • The Real Jdbye @ The Real Jdbye:
    @SylverReZ if you could find a v5 DS ML you would have the best of both worlds since the v5 units had the same backlight brightness levels as the DS Lite unlockable with flashme
  • The Real Jdbye @ The Real Jdbye:
    but that's a long shot
  • The Real Jdbye @ The Real Jdbye:
    i think only the red mario kart edition phat was v5
  • BigOnYa @ BigOnYa:
    A woman with no arms and no legs was sitting on a beach. A man comes along and the woman says, "I've never been hugged before." So the man feels bad and hugs her. She says "Well i've also never been kissed before." So he gives her a kiss on the cheek. She says "Well I've also never been fucked before." So the man picks her up, and throws her in the ocean and says "Now you're fucked."
    +2
  • BakerMan @ BakerMan:
    lmao
  • BakerMan @ BakerMan:
    anyways, we need to re-normalize physical media

    if i didn't want my games to be permanent, then i'd rent them
    +1
  • BigOnYa @ BigOnYa:
    Agreed, that why I try to buy all my games on disc, Xbox anyways. Switch games (which I pirate tbh) don't matter much, I stay offline 24/7 anyways.
  • AncientBoi @ AncientBoi:
    I don't pirate them, I Use Them :mellow:. Like I do @BigOnYa 's couch :tpi::evil::rofl2:
    +1
  • cearp @ cearp:
    @BakerMan - you can still "own" digital media, arguably easier and better than physical since you can make copies and backups, as much as you like.

    The issue is DRM
  • cearp @ cearp:
    You can buy drm free games / music / ebooks, and if you keep backups of your data (like documents and family photos etc), then you shouldn't lose the game. but with a disk, your toddler could put it in the toaster and there goes your $60

    :rofl2:
  • cearp @ cearp:
    still, I agree physical media is nice to have. just pointing out the issue is drm
  • rqkaiju2 @ rqkaiju2:
    i like physical media because it actually feels like you own it. thats why i plan on burning music to cds
  • cearp @ cearp:
    It's nice to not have to have a lot of physical things though, saves space
    +1
  • AncientBoi @ AncientBoi:
    Nor clothes 🤮 . Saves on time, soap, water and money having to wash them. :D
  • SylverReZ @ SylverReZ:
    @rqkaiju2, Physical media is a great source for archiving your data, none of that cloud storage shiz.
    +1
  • AncientBoi @ AncientBoi:
    [squeezes @SylverReZ onto a physical media, then archives you in my old stuff box] :tpi::rofl2::tpi:
    +1
    AncientBoi @ AncientBoi: [squeezes @SylverReZ onto a physical media, then archives you in my old stuff box]... +1