-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
128 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/mroach/n64-go/rom" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
var overwrite bool | ||
var convertCmd = &cobra.Command{ | ||
Use: "convert", | ||
Short: "Converts a ROM to native Big-Endian Z64 format", | ||
Args: cobra.MinimumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
inpath := args[0] | ||
dirname, filename := path.Split(inpath) | ||
baseFilename := basename(filename) | ||
outFilename := baseFilename + ".z64" | ||
outpath := path.Join(dirname, outFilename) | ||
|
||
if !overwrite { | ||
if _, err := os.Stat(outpath); err == nil { | ||
return errors.New(outpath + " already exists") | ||
} | ||
} | ||
|
||
if err := rom.ConvertRomFormat(inpath, outpath); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(inpath, "=>", outpath) | ||
return nil | ||
}, | ||
} | ||
|
||
convertCmd.Flags().BoolVarP(&overwrite, "force", "f", false, "Overwrite destination file if it exists") | ||
rootCmd.AddCommand(convertCmd) | ||
} | ||
|
||
func basename(filename string) string { | ||
if pos := strings.LastIndexByte(filename, '.'); pos != -1 { | ||
return filename[:pos] | ||
} | ||
return filename | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package rom | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"os" | ||
) | ||
|
||
func ConvertRomFormat(inpath string, outpath string) error { | ||
const bufferSize = 2048 | ||
|
||
info, err := FromPath(inpath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.File.Format == "z64" { | ||
return errors.New("File is already in the native Z64 format") | ||
} | ||
|
||
source, err := os.Open(inpath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dest, err := os.Create(outpath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
buf := make([]byte, bufferSize) | ||
n, err := source.Read(buf) | ||
if err != nil && err != io.EOF { | ||
return err | ||
} | ||
if n == 0 { | ||
break | ||
} | ||
|
||
buf = maybeReverseBytes(buf, info.File.Format) | ||
if _, err := dest.Write(buf[:n]); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func maybeReverseBytes(bytes []byte, romFormat string) []byte { | ||
if romFormat == "v64" { | ||
return reverseBytes(bytes, 2) | ||
} | ||
|
||
if romFormat == "n64" { | ||
return reverseBytes(bytes, 4) | ||
} | ||
|
||
return bytes | ||
} | ||
|
||
func reverseBytes(bytes []byte, size int) (reversed []byte) { | ||
for _, chunk := range chunk(bytes, size) { | ||
for i := len(chunk) - 1; i >= 0; i = i - 1 { | ||
reversed = append(reversed, chunk[i]) | ||
} | ||
} | ||
|
||
return reversed | ||
} | ||
|
||
func chunk(bytes []byte, chunkSize int) (chunks [][]byte) { | ||
for chunkSize < len(bytes) { | ||
bytes, chunks = bytes[chunkSize:], append(chunks, bytes[0:chunkSize:chunkSize]) | ||
} | ||
return append(chunks, bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters