Skip to content

Commit

Permalink
Add Pi CLI
Browse files Browse the repository at this point in the history
First supported command is "convert". It converts files from one format into another.

With this feature you can convert pico-8 file into Pi formats: sprite-sheet.png and audio.sfx
  • Loading branch information
elgopher committed Oct 2, 2023
1 parent 7141243 commit 591363b
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 0 deletions.
57 changes: 57 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// (c) 2022-2023 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)

package main

import (
"fmt"
"os"

"github.com/urfave/cli/v2"

"github.com/elgopher/pi/cli/internal/convert"
)

func main() {
app := cli.App{
Usage: "Pi Command Line Interface",
Commands: []*cli.Command{
convertCmd(),
},
}
err := app.Run(os.Args)
if err != nil {
_, _ = os.Stderr.WriteString(err.Error())
}
}

func convertCmd() *cli.Command {
return &cli.Command{
Name: "convert",
Usage: "Converts one file format into another one",
Description: "Format of input and output file is deducted based on files extension.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
Usage: "Format of input file. Overrides what CLI deducted based on input file extension. For now, the only supported input format is p8.",
},
},
ArgsUsage: "input.file output.file",
Action: func(context *cli.Context) error {
inputFile := context.Args().Get(0)
outputFile := context.Args().Get(1)

if context.Args().Len() > 2 {
return fmt.Errorf("too many arguments")
}

command := convert.Command{
InputFormat: convert.InputFormat(context.String("format")),
InputFile: inputFile,
OutputFile: outputFile,
}
return command.Run()
},
}
}
11 changes: 11 additions & 0 deletions cli/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/elgopher/pi/cli

go 1.20

require github.com/urfave/cli/v2 v2.25.7

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)
8 changes: 8 additions & 0 deletions cli/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
63 changes: 63 additions & 0 deletions cli/internal/convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// (c) 2022-2023 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)

package convert

import (
"fmt"
"strings"

"github.com/elgopher/pi/cli/internal/convert/internal/p8"
)

type InputFormat string

const (
InputFormatP8 = "p8"
)

type Command struct {
InputFormat
InputFile string
OutputFile string
}

func (o Command) Run() error {
if o.InputFormat != "" && o.InputFormat != InputFormatP8 {
return fmt.Errorf("input format %s not supported", o.InputFormat)
}

if o.InputFormat == "" {
if strings.HasSuffix(o.InputFile, ".p8") {
o.InputFormat = InputFormatP8
} else {
return fmt.Errorf("cannot deduct the format of %s input file", o.InputFile)
}
}

if o.InputFile == "" {
return fmt.Errorf("input file not provided")
}

if o.OutputFile == "" {
return fmt.Errorf("output file not provided")
}

fmt.Printf("Converting %s to %s... ", o.InputFile, o.OutputFile)
fmt.Printf("Using %s input format... ", o.InputFormat)
if err := o.convert(); err != nil {
return err
}
fmt.Println("Done")
return nil
}

func (o Command) convert() error {
if o.InputFormat == InputFormatP8 {
if err := p8.ConvertToAudioSfx(o.InputFile, o.OutputFile); err != nil {
return err
}
}

return nil
}
112 changes: 112 additions & 0 deletions cli/internal/convert/internal/p8/p8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package p8

import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"

"github.com/elgopher/pi/audio"
)

func ConvertToAudioSfx(inputFile, outputFile string) error {
p8file, err := Load(inputFile)
if err != nil {
return fmt.Errorf("loading p8 file failed: %w", err)
}

effects, err := p8file.SoundEffects()
if err != nil {
return fmt.Errorf("loading sound effects failed: %w", err)
}
audio.Sfx = effects

patterns, err := p8file.MusicPatterns()
if err != nil {
return fmt.Errorf("loading music patterns failed: %w", err)
}
audio.Pat = patterns

bytes, err := audio.Save()
if err != nil {
return fmt.Errorf("saving audio failed: %w", err)
}

err = os.WriteFile(outputFile, bytes, 0644)
if err != nil {
return fmt.Errorf("writing %s failed: %w", outputFile, err)
}

return nil
}

func Load(filename string) (*File, error) {
file := &File{}

f, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("opening %s file failed: %w", filename, err)
}

reader := bufio.NewReader(f)
_, err = readHeader(reader)
if err != nil {
return nil, err
}

for {
err = file.readSection(reader)
if err == io.EOF {
return file, nil
}
}
}

func readHeader(r *bufio.Reader) (version int, err error) {
firstLine, err := r.ReadString('\n')
if err != nil {
return 0, fmt.Errorf("error reading first line from p8 file header: %w", err)
}

if firstLine != "pico-8 cartridge // http://www.pico-8.com" {
return 0, fmt.Errorf("input file is not p8 cartridge file. Header expected")
}

versionLine, err := r.ReadString('\n')
if !strings.HasPrefix(versionLine, "version ") {
return 0, fmt.Errorf("input file is not p8 cartridge file. Version in header expected, but not found")
}

versionString := strings.SplitN(versionLine, " ", 2)[1]

version, err = strconv.Atoi(versionString)
if err != nil {
return 0, fmt.Errorf("input file is not p8 cartridge file. Version number in header expected, but found %s", versionString)
}

return version, nil
}

type File struct {
}

func (p *File) readSection(reader *bufio.Reader) error {
sectionName, err := reader.ReadString('\n')
if err != nil {
return err
}

fmt.Println(sectionName)

return nil
}

func (p *File) SoundEffects() ([64]audio.SoundEffect, error) {
return [64]audio.SoundEffect{}, nil
}

func (p *File) MusicPatterns() ([64]audio.Pattern, error) {
return [64]audio.Pattern{}, nil
}
13 changes: 13 additions & 0 deletions cli/internal/convert/internal/test/code.p8
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pico-8 cartridge // http://www.pico-8.com
version 41
__lua__
function _draw()
end
function _lua
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
9 changes: 9 additions & 0 deletions cli/internal/convert/internal/test/minimal.p8
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pico-8 cartridge // http://www.pico-8.com
version 41
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
15 changes: 15 additions & 0 deletions cli/internal/convert/internal/test/music.p8
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pico-8 cartridge // http://www.pico-8.com
version 41
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__sfx__
000100000000027050270502705027050260502605026050260502605026050260502605026050260502605026050260502605027050270502705027050280500000000000000000000000000000000000000000
00100000000000000000000000003505032050310502e0502d0502b0502a050290502805027050260502505024050240502305000000000000000000000000000000000000000000000000000000000000000000
__music__
00 01424344

Loading

0 comments on commit 591363b

Please sign in to comment.