cli work, partial flags implementation

This commit is contained in:
Stefan Stefanov 2024-04-29 12:44:30 +03:00
parent 0ef07b299c
commit 82ee56ef03
4 changed files with 96 additions and 61 deletions

View file

@ -29,9 +29,6 @@ main :: proc() {
utils.print_help()
return
}
// if help, ok: args[utils.CLIFlagType.Help]; ok {
// fmt.println("Help called!")
// }
ase_file, ase_ok := os.read_entire_file(IMPORT_PATH)
if !ase_ok {

View file

@ -1,6 +1,7 @@
package game
import ase "./aseprite"
import "core:encoding/json"
import "core:fmt"
import "core:os"
import fp "core:path/filepath"
@ -9,6 +10,15 @@ import "core:strings"
import rl "vendor:raylib"
import stbrp "vendor:stb/rect_pack"
import utils "./utils"
when ODIN_OS == .Windows {
os_file_separator :: "\\"
} else {
os_file_separator :: "/"
}
CellData :: struct {
layer_index: u16,
opacity: u8,
@ -441,3 +451,68 @@ metadata_source_code_generate :: proc(
return sb
}
save_metadata :: proc(
settings: utils.CLIPackerSettings,
atlas_entries: []AtlasEntry,
atlas_metadata: []SpriteAtlasMetadata,
) {
metadata, ok := settings.metadata.(utils.CLIMetadataSettings);if !ok do return
if json_path, ok := metadata.json_path.(string); ok {
json_bytes, jerr := json.marshal(atlas_metadata)
if jerr == nil {
os.write_entire_file(json_path, json_bytes)
} else {
fmt.println("Failed to marshall metadata")
}
}
if source_code_path, ok := metadata.source_code_path.(string); ok {
sb := metadata_source_code_generate(atlas_metadata, odin_source_generator_metadata)
source_code_output_str := strings.to_string(sb)
os.write_entire_file(source_code_path, transmute([]byte)source_code_output_str)
}
}
save_output :: proc() {
output_path, ok := g_mem.output_folder_path.(string)
if !ok {
fmt.println("Output path is empty!")
return
} else if output_path == "" {
fmt.println("Output path is empty!")
return
}
image := rl.LoadImageFromTexture(g_mem.atlas_render_texture_target.texture)
rl.ImageFlipVertical(&image)
output_path = strings.concatenate({output_path, os_file_separator, "atlas.png"})
cstring_output_path := strings.clone_to_cstring(output_path)
rl.ExportImage(image, cstring_output_path)
if metadata, ok := g_mem.atlas_metadata.([dynamic]SpriteAtlasMetadata); ok {
if json_metadata, jok := json.marshal(metadata); jok == nil {
os.write_entire_file(
strings.concatenate({output_path, os_file_separator, "metadata.json"}),
json_metadata,
)
} else {
fmt.println("Failed to marshall the atlas metadata to a json!")
}
// TODO(stefan): Think of a more generic alternative to just straight output to a odin file
// maybe supply a config.json that defines the start, end, line by line entry and enum format strings
// this way you can essentially support any language
sb := generate_odin_enums_and_atlas_offsets_file_sb(metadata[:])
odin_metadata := strings.to_string(sb)
os.write_entire_file(
strings.concatenate({output_path, os_file_separator, "metadata.odin"}),
transmute([]byte)odin_metadata,
)
} else {
fmt.println("No metadata to export!")
}
}

View file

@ -1,57 +0,0 @@
package game
import "core:fmt"
import "core:strings"
import "core:os"
import "core:encoding/json"
import rl "vendor:raylib"
when ODIN_OS == .Windows {
os_file_separator :: "\\"
} else {
os_file_separator :: "/"
}
save_output :: proc() {
if output_path, ok := g_mem.output_folder_path.(string); ok {
if len(output_path) == 0 {
fmt.println("Output path is empty!")
return
}
image := rl.LoadImageFromTexture(g_mem.atlas_render_texture_target.texture)
rl.ImageFlipVertical(&image)
output_path := strings.concatenate({output_path, os_file_separator, "atlas.png"})
cstring_output_path := strings.clone_to_cstring(output_path)
rl.ExportImage(image, cstring_output_path)
if metadata, ok := g_mem.atlas_metadata.([dynamic]SpriteAtlasMetadata); ok {
if json_metadata, jok := json.marshal(metadata); jok == nil {
os.write_entire_file(
strings.concatenate({output_path, os_file_separator, "metadata.json"}),
json_metadata,
)
} else {
fmt.println("Failed to marshall the atlas metadata to a json!")
}
// TODO(stefan): Think of a more generic alternative to just straight output to a odin file
// maybe supply a config.json that defines the start, end, line by line entry and enum format strings
// this way you can essentially support any language
sb := generate_odin_enums_and_atlas_offsets_file_sb(metadata[:])
odin_metadata := strings.to_string(sb)
os.write_entire_file(
strings.concatenate({output_path, os_file_separator, "metadata.odin"}),
transmute([]byte)odin_metadata,
)
} else {
fmt.println("No metadata to export!")
}
} else {
fmt.println("Output path is empty!")
}
}

View file

@ -33,12 +33,32 @@ CLI_FLAG_DESCRIPTIONS := [CLIFlagType]string {
.InputFiles = "(real) path the source files for the packer (realpaths only), for multiple files you can provide one string of concateneted paths, separated by a ';'",
.InputFolder = "(real) path to a folder full of source files. This is an alternative to the -i[,input-files] flag",
.OutputFolder = "(real) path to the output folder for all the resulting files to be saved to.",
.EnableMetadataOutput = "On by default. Whether or not to export metadata (JSON or source files with the offsets for the packer sprites in the atlas)",
.EnableMetadataOutput = "Whether or not to export metadata (JSON or source files with the offsets for the packer sprites in the atlas)",
.ConfigPath = "(real) path to a config file (json) that contains string definitions for exporting custom source files. More on this in the docs.",
.MetadataJSONOutputPath = "(real) path for the resulting JSON that will be generated for the atlas. It overrides the name & location in regards to the -o[,output-folder] flag",
.SourceCodeOutputPathOutputPath = "(real) path for the resulting source code file that will be generated for the atlas. It overrides the name & location in regards to the -o[,output-folder] flag",
}
CLIOutputSettings :: struct {
// Where the output files will be written (atlas.png, json output, etc)
output_folder_path: Maybe(string),
// If files were chosen as input - their paths
source_location_to_pack: Maybe(string),
// If a folder was chosen as input - the path
source_files_to_pack: Maybe([]string),
}
CLIMetadataSettings :: struct {
json_path: Maybe(string),
source_code_path: Maybe(string),
}
CLIPackerSettings :: struct {
output: Maybe(CLIOutputSettings),
metadata: Maybe(CLIMetadataSettings),
json_config_path: Maybe(string),
}
CLIFlag :: struct {
flag: string,
value: string,