Skip to content

Commit

Permalink
secret commands for nogui compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgaturus committed Jun 13, 2024
1 parent 810b728 commit 4c21844
Showing 1 changed file with 69 additions and 31 deletions.
100 changes: 69 additions & 31 deletions src/nopack.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from os import
fileExists, `/`, copyDir,
removeDir, createDir, dirExists
from strutils import parseInt, split
import std/os

proc gorge0(cmd: string): string {.compileTime.} =
result = gorge(cmd)
Expand Down Expand Up @@ -72,8 +70,11 @@ proc msgError(reason, msg: string) =
# ----------------

proc listfile(filename: string): File =
if not open(result, filename, fmRead):
if not open(result, filename, fmRead) or result.endOfFile():
msgWarning("SKIP", filename & " not found")
# Close File and Clear
result.close()
result = nil

proc packfile(filename: string): File =
if not open(result, filename, fmWrite):
Expand Down Expand Up @@ -153,8 +154,14 @@ proc packCursors() {.raises: [IOError].} =
pack = packfile("data" / "cursors.dat")
# Write Pack Signature
header(pack, formatCursor)
# Rasterize Fallback if not Found
if isNil(list):
let chunk = nopack_load_fallback(formatCursor)
# Write Fallback Chunk to File
write(pack, chunk)
nopack_load_dealloc(chunk)
return
# Rasterize Cursors From List
if isNil(list): return
for line in lines(list):
let
info = line.infoCursor()
Expand Down Expand Up @@ -209,20 +216,13 @@ proc packIcons(format: ImageFormat) {.raises: [IOError].} =
# -------------------

proc folder(line: string): tuple[src, dst: string, extern: bool] =
let
s = split(line, " : ")
s1 = split(s[0], "-:-")
# Check if Path is Valid
if s.len != 2 and s1.len != 2:
let s = split(line, " : ")
if s.len != 2:
error(line, "invalid path")
# Extract Paths
result.src = s1[0]
result.dst = s1[1]
# Extract Extern
try:
result.extern = parseInt(s[1]) > 0
except ValueError:
error(result.src, "invalid path mode")
result.src = s[0]
result.dst = s[1]
# Check if Path is Absolute
result.extern = isAbsolute(result.src)

proc copy() {.raises: [OSError, IOError].} =
# Prepare Folder List
Expand All @@ -238,28 +238,66 @@ proc copy() {.raises: [OSError, IOError].} =
if not dirExists(f.src):
error(f.src, "not found")
# Debug Information
let sym = if f.extern: " >> " else: " -> "
msgInfo("COPY", f.src & sym & "data" / f.dst)
msgInfo("COPY", f.src & " : " & "data" / f.dst)
# Copy Folder to Destination
f.dst = "data" / f.dst
copyDir(f.src, f.dst)

# -------------------
# Main Packer Options
# -------------------

proc optionReset() {.raises: [OSError, IOError].} =
if not dirExists("pack"):
createDir("pack")
# Create or Clear File Lists
open("pack/icons.list", fmWrite).close()
open("pack/cursors.list", fmWrite).close()
open("pack/paths.list", fmWrite).close()

proc optionWrite(list, line: string) {.raises: [IOError].} =
let f = open(list, fmAppend)
# Write Line and Close File
f.writeLine(line)
f.close()

proc optionPack() {.raises: [OSError, IOError].} =
echo "nogui data packer 0.3"
echo "mrgaturus 2024"
# Clear Data Folder
echo "Creating data folder..."
removeDir("data")
createDir("data")
# Pack Icons and Copy Paths
echo "\nPacking icons.dat..."; packIcons(formatAlpha)
echo "\nPacking cursors.dat..."; packCursors()
echo "\nCopying paths..."; copy()

# --------------------
# Main Packer Function
# --------------------

proc dispatch() {.raises: [OSError, IOError].} =
let argc = paramCount()
if argc == 0:
optionPack()
return
let
cmd = paramStr(1)
path = paramStr(2)
# Change to Current Path
setCurrentDir(path)
# Check reset or write
case cmd
of "reset": optionReset()
of "icon": optionWrite "pack/icons.list", paramStr(3)
of "cursor": optionWrite "pack/cursors.list", paramStr(3)
of "path": optionWrite "pack/paths.list", paramStr(3)
# Pack Icons as Fallback
else: optionPack()

proc main() =
echo "nogui data packer 0.3"
echo "mrgaturus 2024"
try:
# Clear Data Folder
echo "Creating data folder..."
removeDir("data")
createDir("data")
# Pack Icons and Copy Paths
echo "\nPacking icons.dat..."; packIcons(formatAlpha)
echo "\nPacking cursors.dat..."; packCursors()
echo "\nCopying paths..."; copy()
try: dispatch()
# Handle Packing Exceptions
except IOError as error:
msgError("IO ERROR", error.msg)
Expand Down

0 comments on commit 4c21844

Please sign in to comment.