Dusk texture creator
This commit is contained in:
+43
-45
@@ -1,60 +1,58 @@
|
||||
import argparse
|
||||
import os
|
||||
import csv
|
||||
import os
|
||||
|
||||
parser = argparse.ArgumentParser(description="Input CSV to .h defines")
|
||||
parser.add_argument("--csv", required=True, help="Path to Input CSV file")
|
||||
parser.add_argument("--output", required=True, help="Path to output .h file")
|
||||
args = parser.parse_args()
|
||||
|
||||
def csvIdToEnumName(inputId):
|
||||
return "INPUT_ACTION_" + inputId.upper()
|
||||
def id_enum(name):
|
||||
return "INPUT_ACTION_" + name.upper()
|
||||
|
||||
# Load up CSV file.
|
||||
outHeader = "#pragma once\n"
|
||||
outHeader += '#include "dusk.h"\n\n'
|
||||
|
||||
with open(args.csv, newline="", encoding="utf-8") as csvfile:
|
||||
reader = csv.DictReader(csvfile)
|
||||
|
||||
# CSV must have id column.
|
||||
# Load CSV
|
||||
input_ids = []
|
||||
with open(args.csv, newline="", encoding="utf-8") as f:
|
||||
reader = csv.DictReader(f)
|
||||
if "id" not in reader.fieldnames:
|
||||
raise Exception("CSV file must have 'id' column")
|
||||
|
||||
# For each ID
|
||||
inputIds = []
|
||||
inputIdValues = {}
|
||||
raise ValueError("CSV must have an 'id' column")
|
||||
for row in reader:
|
||||
inputId = row["id"]
|
||||
if inputId not in inputIds:
|
||||
inputIds.append(inputId)
|
||||
input_id = row["id"]
|
||||
if input_id not in input_ids:
|
||||
input_ids.append(input_id)
|
||||
|
||||
# For each ID, create enum entry.
|
||||
count = 0
|
||||
outHeader += "typedef enum {\n"
|
||||
outHeader += f" INPUT_ACTION_NULL = 0x{count:x},\n\n"
|
||||
count += 1
|
||||
for inputId in inputIds:
|
||||
inputIdValues[inputId] = count
|
||||
outHeader += f" {csvIdToEnumName(inputId)} = 0x{count:x},\n"
|
||||
count += 1
|
||||
outHeader += f"\n INPUT_ACTION_COUNT = 0x{count:x}\n"
|
||||
outHeader += "} inputaction_t;\n\n"
|
||||
# Assign enum values
|
||||
id_values = {input_id: i + 1 for i, input_id in enumerate(input_ids)}
|
||||
|
||||
# Write IDs to char array.
|
||||
outHeader += f"static const char_t* INPUT_ACTION_IDS[] = {{\n"
|
||||
for inputId in inputIds:
|
||||
outHeader += f" [{csvIdToEnumName(inputId)}] = \"{inputId}\",\n"
|
||||
outHeader += f"}};\n\n"
|
||||
# Build output
|
||||
out = [
|
||||
"#pragma once",
|
||||
'#include "dusk.h"',
|
||||
"",
|
||||
"typedef enum {",
|
||||
" INPUT_ACTION_NULL = 0x0,",
|
||||
"",
|
||||
]
|
||||
for input_id in input_ids:
|
||||
out.append(f" {id_enum(input_id)} = 0x{id_values[input_id]:x},")
|
||||
out += [
|
||||
"",
|
||||
f" INPUT_ACTION_COUNT = 0x{len(input_ids) + 1:x}",
|
||||
"} inputaction_t;",
|
||||
"",
|
||||
"static const char_t *INPUT_ACTION_IDS[] = {",
|
||||
]
|
||||
for input_id in input_ids:
|
||||
out.append(f" [{id_enum(input_id)}] = \"{input_id}\",")
|
||||
out += [
|
||||
"};",
|
||||
"",
|
||||
"static const char_t *INPUT_ACTION_SCRIPT =",
|
||||
]
|
||||
for input_id in input_ids:
|
||||
out.append(f" \"{id_enum(input_id)} = {id_values[input_id]}\\n\"")
|
||||
out += [";", ""]
|
||||
|
||||
# Lua Script
|
||||
outHeader += f"static const char_t *INPUT_ACTION_SCRIPT = \n"
|
||||
for inputId in inputIds:
|
||||
# Reference the enum
|
||||
outHeader += f" \"{csvIdToEnumName(inputId)} = {inputIdValues[inputId]}\\n\"\n"
|
||||
outHeader += f";\n\n"
|
||||
|
||||
# Write to output file.
|
||||
os.makedirs(os.path.dirname(args.output), exist_ok=True)
|
||||
with open(args.output, "w", encoding="utf-8") as outFile:
|
||||
outFile.write(outHeader)
|
||||
with open(args.output, "w", encoding="utf-8") as f:
|
||||
f.write("\n".join(out))
|
||||
|
||||
Reference in New Issue
Block a user