Initial release, requesting feedback and suggestions! I modified @keen vortex's awesome CfgFunctions generator script to include nested folders (subfolders) as well to allow greater clarity of folder and file structure in complex missions. Required dependencies and installation & configuration guide here (the same guide still applies): https://forums.bohemia.net/forums/topic/236191-cfgfunctions-generator-python/
generate.py: ```python
from pathlib import Path
import glob
def format_function_class(sqf_file: Path):
function_name = ""
function_path = ""
subcategory = ""
subcategory_folder = ""
return_value = ""
if sqf_file.is_file():
function_path = sqf_file.relative_to(folder_functions.parent)
depth = len(function_path.parents)
if (sqf_file.name.startswith('fn_')):
function_path = sqf_file.relative_to(folder_functions.parent)
if depth > 3:
subcategory_folder = function_path.parents[depth - (depth - (depth - 4))]
subcategory = format_subcategory(subcategory_folder)
function_name = sqf_file.stem.replace('fn_', '')
return_value = nested_folder_function_name(subcategory, function_name, function_path)
elif depth == 3:
subcategory_folder = function_path.parent
function_name = sqf_file.stem.replace('fn_', '')
return_value = core_function_name(subcategory, function_name, function_path)
else:
print(f"### WARNING! Function {function_name} didn't get included to CfgFunctions. It needs to be located in a subfolder of \\functions folder.")
else:
print(f"### WARNING! Function name didn't start with \"fn_\". It was not added to CfgFunctions. Function path: {function_path}")
else:
print("### ERROR: Generic error! Something went wrong when generating CfgFunctions. Double check the contents of it.")
return return_value
def format_subcategory(subcategory: Path):
prefix = subcategory.parent
subcategory = str(subcategory).replace((str(prefix)), '')
subcategory = subcategory[1:]
return subcategory
def nested_folder_function_name(subcategory: Path, function_name: str, function_path: Path):
return f"class {subcategory}_{function_name}" + f" {{ file = "{function_path}"; }};"
def core_function_name(subcategory: Path, function_name: str, function_path: Path):
return f"class {function_name}" + f" {{ file = "{function_path}"; }};"
MAIN SCRIPT START
DEFINE YOUR OWN TAG INSIDE THE VARIABLE BELOW
tag = 'YOUR_TAG_HERE'
folder_functions = Path(file).parent.resolve()
file_cfg = folder_functions / 'CfgFunctions.hpp'
content = [
"#ifdef DEBUG_ENABLED_FULL",
"allowFunctionsRecompile = 1;",
"allowFunctionsLog = 1;",
"#endif",
"",
"class CfgFunctions",
"{",
"",
f"\tclass {tag}",
"\t{"
]
Get all categories by looking at the folders
categories = [x for x in folder_functions.iterdir() if x.is_dir()]
categories.sort(key=lambda x: x.name.upper())
content.append("")
for cat in categories:
print("")
print(f"### CATEGORY ADDED: {cat.name}")
print("")
content.extend([f'\t\tclass {cat.name}', "\t\t{"])
subfolders_files = glob.glob(str(cat) + '/**/*.sqf', recursive=True)
if len(subfolders_files) > 0:
for f in subfolders_files:
sqf_file = Path(f)
formatted_class = format_function_class(sqf_file)
# print(f"Adding the following to CfgFunctions: {formatted_class}")
if formatted_class != "" or formatted_class:
content.append(f'\t\t\t{formatted_class}')
content.append('\t\t};\n')
content.extend(["\t};","","};"])
output = '\n'.join(content)
file_cfg.write_text(output)
print("")
print("###### CfgFunctions is now ready! :)")
(Edit #2: **3rd improved version** updated 8th March 2023 11:48 UTC)