-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
106 lines (78 loc) · 3.41 KB
/
generate.py
File metadata and controls
106 lines (78 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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! :)")