Jump to content
7erra

CfgFunctions Generator (Python)

Recommended Posts

CfgFunctions Generator

This is a little Python script I wrote to generate a CfgFunctions.hpp file which can be included in the main config (either description.ext or config.cpp). This script assumes the following project structure:

  • A functions folder in the mission/mod directory
  • The python script resides in the "functions" folder
  • Subfolders with the name of the category
  • Function files in those category folders
  • Function files start with "fn_"

Here is an example:

Folder structure

mission.vr/
├─ description.ext
├─ functions/
│  ├─ Category1/
│  │  ├─ fn_myFunction.sqf
│  ├─ Category2/
│  │  ├─ fn_anotherFunction.sqf
│  │  ├─ fn_aFunction.sqf
│  ├─ generate.py
│  ├─ CfgFunctions.hpp

Output (in CfgFunctions.hpp):

#ifdef DEBUG_ENABLED_FULL
allowFunctionsRecompile = 1;
allowFunctionsLog = 1;
#endif
class CfgFunctions
{
	class MIS
	{
		class Category1
		{
			class myFunction;
		};
		class Category2
		{
			class aFunction;
			class anotherFunction;
		};
	};
};

A new file called "CfgFunctions.hpp" will be created in the "functions" folder. It can be added to the config by adding the following line to your description.ext/config.cpp:

#include "functions\CfgFunctions.hpp"

 

Installation

  1. Install Python
  2. Create a new file called "generate.py" in your functions directory
  3. Paste the code from below into the file

 

Usage

  1. Change the line "tag = 'MIS'" to your tag (only needed to be done once)
  2. Open a command-line in the functions directory
  3. Execute
python3 "generate.py"

 

License

MIT

 

generate.py

from pathlib import Path
tag = 'MIS'
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())
for cat in categories:
   content.extend([f'\t\tclass {cat.name}', "\t\t{"])
   # Get all functions from the files
   function_files = [f.stem.replace('fn_', '') for f in cat.iterdir() if f.is_file() and f.name.startswith('fn_')]
   function_files.sort(key=lambda x: x.upper())
   for f in function_files:
       content.append(f'\t\t\tclass {f};')
   content.append('\t\t};')
content.extend(["\t};","};"])
output = '\n'.join(content)
file_cfg.write_text(output)

 

  • Thanks 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×