Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gave interface a facelift, and separated db user info into separate const group #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 123 additions & 14 deletions cli/main.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,146 @@
package main

import (
"NintendoChannel/csdata"
"NintendoChannel/dllist"
"NintendoChannel/thumbnail"
"fmt"
"NintendoChannel/csdata" // Generate CSData
"NintendoChannel/dllist" // Generate DLList and Game Info
"NintendoChannel/thumbnail" // Generate thumbnails
"fmt" // Print text
"os" // Clear screen
"os/exec" // Open URL in default browser
"runtime" // Get OS
"strings" // Repeat string
"time" // Time for current year

"github.com/fatih/color" // Colorful text
"golang.org/x/term" // Terminal size
)

func main() {
fmt.Println("WiiLink Nintendo Channel File Generator")
// Prints header for the program
func printHeader() {
clearScreen()

// Print header text
currentYear := time.Now().Year()
header := fmt.Sprintf("WiiLink Nintendo Channel File Generator - (c) %d WiiLink", currentYear)
fmt.Println(bold(header))

// Get console width
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
panic(err)
}

// Print a line of '=' across the console
fmt.Println(bold(strings.Repeat("=", width)))
fmt.Println()
fmt.Println("1. DLList and game info\n2. 1. DLList only\n3. Thumbnails\n4. CSData")
}

// Clear the console screen
func clearScreen() {
fmt.Print("\033[H\033[2J")
}

// Make text bold
func bold(text string) string {
return "\033[1m" + text + "\033[0m"
}

func main() {
printHeader()

// Print menu with possible options
color.HiGreen(bold("What would you like to do?"))
fmt.Println()
fmt.Printf("Choose: ")

fmt.Println("1. Generate DLList and Game Info")
fmt.Println("2. Generate DLList Only")
fmt.Println("3. Generate Thumbnails")
fmt.Print("4. Generate CSData\n\n")

// Add Github link and WiiLink website link
fmt.Printf("5. Visit GitHub Repository (%s)\n", color.HiGreenString("https://github.com/WiiLink24/NintendoChannel"))
fmt.Printf("6. Visit WiiLink Website (%s)\n\n", color.HiGreenString("https://wiilink24.com"))

fmt.Print("7. Exit Program\n\n")

// Get user input
var selection int
fmt.Scanln(&selection)
fmt.Print(bold("Choose: "))
fmt.Scan(&selection)

// Handle user input
switch selection {
case 1:
printHeader()
dllist.MakeDownloadList(true)
break
case 2:
printHeader()
dllist.MakeDownloadList(false)
break
case 3:
printHeader()
thumbnail.WriteThumbnail()
break
case 4:
printHeader()
csdata.CreateCSData()
break
case 5:
// Open Github link in default browser
err := OpenBrowser("https://github.com/WiiLink24/NintendoChannel")
if err != nil {
fmt.Println("Failed to open link:", err)
}
fmt.Println()
fmt.Println("Opening link in default browser...")
fmt.Println()

time.Sleep(2 * time.Second) // Wait two seconds before looping

main()
case 6:
// Open WiiLink website link in default browser
err := OpenBrowser("https://wiilink24.com")
if err != nil {
fmt.Println("Failed to open link:", err)
}
fmt.Println()
fmt.Println("Opening link in default browser...")
fmt.Println()

time.Sleep(2 * time.Second) // Wait two seconds before looping

main()
case 7:
fmt.Println()
fmt.Println("Exiting...")
fmt.Println()

time.Sleep(1 * time.Second) // Wait a second before exiting

clearScreen()
default:
fmt.Println("\nInvalid Selection")
fmt.Println()
color.HiRed(bold("Invalid selection! Please try again."))
fmt.Println()

time.Sleep(2 * time.Second) // Wait two seconds before looping

main()
}
}

// OpenBrowser opens the specified URL in the default browser of the user's system
func OpenBrowser(url string) error {
var err error

switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("Platform is unsupported.")
}

return err
}
55 changes: 36 additions & 19 deletions csdata/csdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package csdata
import (
"bytes"
"encoding/binary"
"github.com/SketchMaster2001/libwc24crypt"
"github.com/wii-tools/lzx/lz10"
"fmt"
"hash/crc32"
"os"
"unicode/utf16"

"github.com/SketchMaster2001/libwc24crypt"
colorFmt "github.com/fatih/color"
"github.com/wii-tools/lzx/lz10"
)

type Header struct {
Expand Down Expand Up @@ -37,6 +40,21 @@ var (
iv = []byte{70, 70, 20, 40, 143, 110, 36, 6, 184, 107, 135, 239, 96, 45, 80, 151}
)

// Make text bold
func bold(text string) string {
return "\033[1m" + text + "\033[0m"
}

func checkError(err error) {
if err != nil {
// ERROR! bold and red
colorFmt.HiRed(bold("An error has occurred!"))
fmt.Println()
fmt.Printf(bold("Reason: "))
panic(err)
}
}

func CreateCSData() {
// First append the DLListID to a
var DLListID [256]byte
Expand All @@ -59,27 +77,30 @@ func CreateCSData() {
}

var pics [][]byte

// Read pic1, pic2, and pic3 from files
pic1, err := os.ReadFile("pic1.tpl")
if err != nil {
panic(err)
}
checkError(err)

pic2, err := os.ReadFile("pic2.tpl")
if err != nil {
panic(err)
}
checkError(err)

pic3, err := os.ReadFile("pic1.tpl")
if err != nil {
panic(err)
}
checkError(err)

// Append the relevant parts of each pic to the pics slice
pics = append(pics, pic1[64:], pic2[64:], pic3[64:])

text := []string{"WiiLink Nintendo Channel!", "Demae Channel released", "WiiLink goes global"}
// Text that appears on the banner on the Wii Menu, corresponding to each pic
bannerTickerText := []string{
"WiiLink Nintendo Channel!", // Goes on pic1
"Everybody Votes Channel is back!", // Goes on pic2
"WiiLink available on Dolphin!", // Goes on pic3
}

for i := 0; i < 3; i++ {
var textArray [51]uint16
tempText := utf16.Encode([]rune(text[i]))
tempText := utf16.Encode([]rune(bannerTickerText[i]))
copy(textArray[:], tempText)

var offset uint32
Expand Down Expand Up @@ -127,12 +148,8 @@ func CreateCSData() {

rsaKey, err := os.ReadFile("nc.pem")
encrypted, err := libwc24crypt.EncryptWC24(compress, key, iv, rsaKey)
if err != nil {
panic(err)
}
checkError(err)

err = os.WriteFile("csdata.bin", encrypted, 0666)
if err != nil {
panic(err)
}
checkError(err)
}
31 changes: 26 additions & 5 deletions dllist/dllist.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (
"context"
"encoding/binary"
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/wii-tools/lzx/lz10"
"hash/crc32"
"io"
"log"
"os"
"runtime"
"sync"

colorFmt "github.com/fatih/color"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/wii-tools/lzx/lz10"
)

type List struct {
Expand Down Expand Up @@ -43,9 +45,17 @@ type List struct {
imageBuffer *bytes.Buffer
}

// Make text bold
func bold(text string) string {
return "\033[1m" + text + "\033[0m"
}

func checkError(err error) {
if err != nil {
log.Fatalf("Nintendo Channel file generator has encountered a fatal error! Reason: %v\n", err)
// ERROR! bold and red
colorFmt.HiRed(bold("An error has occurred!"))
fmt.Println()
log.Fatalf(bold("Nintendo Channel file generator has encountered a fatal error!\n\n" + bold("Reason: ") + err.Error() + "\n"))
}
}

Expand All @@ -55,16 +65,27 @@ var (
generateTitles = true
)

// Database credentials (you'll need to change these for your own database)
// Learn how to set up a PostgreSQL database here: https://www.postgresql.org/docs/13/tutorial-start.html
const (
dbUser = "user"
dbPassword = "password"
dbHost = "127.0.0.1"
dbName = "nintendochannel"
)

func MakeDownloadList(_generateTitles bool) {
generateTitles = _generateTitles

// Initialize database
dbString := fmt.Sprintf("postgres://%s:%s@%s/%s", "noahpistilli", "2006", "127.0.0.1", "nc")
dbString := fmt.Sprintf("postgres://%s:%s@%s/%s", dbUser, dbPassword, dbHost, dbName)

dbConf, err := pgxpool.ParseConfig(dbString)
checkError(err)

pool, err = pgxpool.ConnectConfig(ctx, dbConf)
checkError(err)

// Ensure this Postgresql connection is valid.
defer pool.Close()
gametdb.PrepareGameTDB()
info.GetTimePlayed(&ctx, pool)
Expand Down
25 changes: 16 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ require (
)

require (
github.com/h2non/bimg v1.1.9
github.com/jackc/pgx/v4 v4.17.2
golang.org/x/image v0.2.0
golang.org/x/text v0.5.0
github.com/jackc/pgx/v4 v4.18.1
golang.org/x/image v0.13.0
golang.org/x/text v0.13.0
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/sys v0.13.0 // indirect
)

require (
github.com/fatih/color v1.15.0
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgconn v1.14.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/puddle v1.3.0 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/term v0.13.0
)
Loading