example building
This commit is contained in:
+115
@@ -0,0 +1,115 @@
|
||||
import argparse
|
||||
import csv
|
||||
import os
|
||||
|
||||
parser = argparse.ArgumentParser(description="Item CSV to .h defines")
|
||||
parser.add_argument("--csv", required=True, help="Path to item CSV file")
|
||||
parser.add_argument("--output", required=True, help="Path to output .h file")
|
||||
args = parser.parse_args()
|
||||
|
||||
def type_enum(name):
|
||||
return "ITEM_TYPE_" + name.upper()
|
||||
|
||||
def id_enum(name):
|
||||
return "ITEM_ID_" + name.upper()
|
||||
|
||||
# Load CSV
|
||||
item_ids = []
|
||||
item_types = []
|
||||
rows = {}
|
||||
|
||||
with open(args.csv, newline="", encoding="utf-8") as f:
|
||||
reader = csv.DictReader(f)
|
||||
if "id" not in reader.fieldnames or "type" not in reader.fieldnames:
|
||||
raise ValueError("CSV must have 'id' and 'type' columns")
|
||||
for row in reader:
|
||||
item_id, item_type = row["id"], row["type"]
|
||||
if item_id not in item_ids:
|
||||
item_ids.append(item_id)
|
||||
if item_type not in item_types:
|
||||
item_types.append(item_type)
|
||||
rows[item_id] = row
|
||||
|
||||
# Assign enum values: types and IDs share a single counter so values never collide
|
||||
count = 0
|
||||
type_values = {}
|
||||
id_values = {}
|
||||
|
||||
count += 1 # 0 = NULL
|
||||
for t in item_types:
|
||||
type_values[t] = count
|
||||
count += 1
|
||||
# ITEM_TYPE_COUNT = count; item IDs continue from here
|
||||
type_count = count
|
||||
|
||||
count += 1 # type_count = ITEM_ID_NULL
|
||||
for i in item_ids:
|
||||
id_values[i] = count
|
||||
count += 1
|
||||
id_count = count
|
||||
|
||||
# Count items per type
|
||||
type_item_counts = { t: 0 for t in item_types }
|
||||
for i in item_ids:
|
||||
type_item_counts[rows[i]["type"]] += 1
|
||||
|
||||
# Build output
|
||||
out = [
|
||||
"#pragma once",
|
||||
'#include "dusk.h"',
|
||||
"",
|
||||
"typedef enum {",
|
||||
" ITEM_TYPE_NULL = 0,",
|
||||
]
|
||||
for t in item_types:
|
||||
out.append(f" {type_enum(t)} = {type_values[t]},")
|
||||
out += [
|
||||
f" ITEM_TYPE_COUNT = {type_count}",
|
||||
"} itemtype_t;",
|
||||
"",
|
||||
"typedef enum {",
|
||||
f" ITEM_ID_NULL = {type_count},",
|
||||
]
|
||||
for i in item_ids:
|
||||
out.append(f" {id_enum(i)} = {id_values[i]},")
|
||||
out += [
|
||||
f" ITEM_ID_COUNT = {id_count}",
|
||||
"} itemid_t;",
|
||||
"",
|
||||
"typedef struct {",
|
||||
" itemid_t id;",
|
||||
" itemtype_t type;",
|
||||
" const char_t *name;",
|
||||
"} item_t;",
|
||||
"",
|
||||
"static const item_t ITEMS[] = {",
|
||||
]
|
||||
for i in item_ids:
|
||||
row = rows[i]
|
||||
out += [
|
||||
f" [{id_enum(i)}] = {{",
|
||||
f" .id = {id_enum(i)},",
|
||||
f" .type = {type_enum(row['type'])},",
|
||||
f" .name = \"{i}\",",
|
||||
" },",
|
||||
]
|
||||
out += [
|
||||
"};",
|
||||
"",
|
||||
"static const uint8_t ITEM_TYPE_COUNTS[] = {",
|
||||
]
|
||||
for t in item_types:
|
||||
out.append(f" [{type_enum(t)}] = {type_item_counts[t]},")
|
||||
out += [
|
||||
"};",
|
||||
"",
|
||||
]
|
||||
max_type_count = max(type_item_counts.values()) if type_item_counts else 0
|
||||
out += [
|
||||
f"#define ITEM_TYPE_COUNT_MAX {max_type_count}",
|
||||
"",
|
||||
]
|
||||
|
||||
os.makedirs(os.path.dirname(args.output), exist_ok=True)
|
||||
with open(args.output, "w", encoding="utf-8") as f:
|
||||
f.write("\n".join(out))
|
||||
Reference in New Issue
Block a user