Log In  
Follow
freefowl
[ :: Read More :: ]

Cart #quickerloader_slowboot-2 | 2023-01-15 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1


Made a menu to navigate picowesome while running pico.pi by gamaral. Using splore to navigate large directories of carts was tedious. Mainly because there was no way to back out of a directory besides scrolling to the top.

One challenge I ran into was there was no way to use ls() on a nested directory or run cd from the cart (that I could find). So my initial version had the entire gamelist in the menu cart. This worked well but the file was to big to upload here and felt not in the spirit of writing this app as a pico-8 cart.

So I wrote a python script to move and rename all the carts to one directory so they could all be accessible by ls(). This caused a notable slower initial menu cart load time on a raspberry pi 3. Pico.pi takes ~18s to boot and then ~12s after pico-8 does its boot chime. With those carts nested in directories that 12s is saved.

So I'm going to refactor back to nested directories but I wanted to post this version because it is a minimum viable product. Now I'm just going to focus on writing the new version of the quick menu.

Here's my python script. If there's interest I can explain how to use it in the comment below.

from os import walk, path
from pathlib import Path
import string
from shutil import copy2

def main():
    catagories = \
        ['action adventure', 'beat em up', 'gun fps', 'misc', 'platform', \
        'race', 'shoot em up', 'sport', 'adventure visual', \
        'celeste hacks', 'hacks', 'music rythm', 'puzzle', 'rpg', \
        'simulation', 'strategy gestion', 'vs fighting', 'tools']
    carts = list_files("./carts")
    new_carts = add_prefix_to_files(carts, catagories)
    copy_to_output('./carts/', './output/carts/', carts, new_carts)

def copy_to_output(base_directory, output_directory, filenames, new_filenames):
    # mkdir $output_directory
    Path(output_directory).mkdir(parents=True, exist_ok=True)

    dir_index = 0
    #print(filenames.keys())

    for directory in filenames.keys():
        file_index = 0
        for filename in filenames[directory]:
            # Source directory
            src = base_directory + directory +'/'
            # Source filename
            src += filenames[directory][file_index]
            # Destination directory
            dst = output_directory
            # Destination filename
            dst +=  new_filenames[directory][file_index].lower()
            copy2(src, dst)
            file_index += 1
        dir_index += 1

def add_prefix_to_files(filenames, directories):
    new_filenames = {}
    dir_index = 0
    for directory in directories:
        file_index = 0
        new_filenames[directory] = []
        for filename in filenames[directory]:
            prefix = '.'
            prefix += alphanumeric_formatted(dir_index, 1) # directory code
            prefix += alphanumeric_formatted(file_index, 2) # file number code
            new_filenames[directory].append(prefix + filename)
            file_index += 1
        dir_index += 1
    return new_filenames

letters = "-" + string.digits + string.ascii_uppercase + "_" + string.ascii_lowercase
def alphanumeric_formatted(n, digits):
    """ Return string of int as 64 bit alphanumeric with a specific number of digits

    >>> alphanumeric_formatted(4098, 4)
    '-0-1'
    >>> alphanumeric_formatted(4098,2)
    '-1'

    """
    s = int_to_64bit_alphanumeric(n)
    if len(s) > digits:
        return s[len(s)-digits:]
    return s.rjust(digits, letters[0])

def int_to_64bit_alphanumeric(n):
    """ Return string of int as a 64 bit alphanumeric

    >>> int_to_64bit_alphanumeric(65)
    '00'
    >>> int_to_64bit_alphanumeric(0)
    '-'
    >>> int_to_64bit_alphanumeric(1)
    '0'
    >>> int_to_64bit_alphanumeric(4097)
    '0-0'

    """
    if n > 63:
        return int_to_64bit_alphanumeric(n>>6) + int_to_64bit_alphanumeric(n&63)
    return letters[n]

def list_files(startpath):
    carts = {}
    for root, dirs, files in walk(startpath):
        # Only grab files if directory has no files
        if dirs == []:
            carts[path.basename(root)] = sorted(files)
    return carts

if __name__=="__main__":
    main()
P#124327 2023-01-15 00:31 ( Edited 2023-01-15 01:52)

[ :: Read More :: ]

I wanted to commit to a git repo without leaving pico 8 so wrote bash script and about 30 tokens to add to the p8 file. Both are here (or at the bottom of the post):

https://gist.github.com/cutecprog/827ef8a82d65a1a27f133fd89392f1b0

Start pico8 by running the bash script. Then to use. First load your p8 file.

> load your_file.p8
> run all text after will be in the commit message no quotes needed
commit message set

Game runs.
Exit the game.

> save

All p8 files are auto added to repo. This command runs in the terminal

$ git commit -am "commit message set at run"  # Runs when a file is saved

Put in p8 file

-- > run breadcrumb text
-- write stat(6) to file to use
-- as git commit message
-- call in _init()
function add_commit_message()
  -- this saves 1 token
  local msg = stat(6)
  -- check if message entered
  if msg ~= "~~~" 
      and msg ~= "" then
    -- write message to file
    printh(msg, "msg", true)
    -- report success (opt)
    ?"commit message set"
    -- sleep for 30 frames (opt)
    for i=1, 30 do flip() end
    -- reset breadcrumb
    run("~~~")
  end
end

Bash script

#! /bin/bash

# Run script to call pico8
~/pico-8/pico8 &
while true; do
    inotifywait -qq -e CLOSE_WRITE ~/.lexaloffle/pico-8/carts/*.p8
    cd ~/.lexaloffle/pico-8/carts
    git add *.p8
    if test -f msg.p8l; then
        git commit -a -F msg.p8l
        rm msg.p8l
    else
        git commit -am "autocommit"
    fi
done
P#105049 2022-01-14 02:12 ( Edited 2022-01-14 02:21)

Follow Lexaloffle:          
Generated 2024-04-20 07:17:16 | 0.209s | Q:10