# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Run `python generate_error_code_blueprint.py --help` to learn how to use this program. This script reads GameKit's error code header and generates an Unreal Blueprint Library that enables Error Codes in blueprints. """ import argparse import logging import os from pathlib import Path import re from typing import List def main(): parser = argparse.ArgumentParser( description='Generate Blueprints for Error codes', epilog="Example Usage:\n" f"{os.path.basename(__file__)} D:/workspace/GameKit/src/GameKitUnrealSandbox/AwsGameKitUnrealGame/Plugins", formatter_class=argparse.RawTextHelpFormatter) parser.add_argument("plugin_full_path", type=str, help="Full path to the AWS GameKit plugin. See 'Example Usage' below for details.") logging.basicConfig( format='%(levelname)s: %(message)s', level=logging.INFO ) arguments = parser.parse_args() plugin_root = Path(arguments.plugin_full_path) generate_error_code_blueprint(plugin_root) def generate_error_code_blueprint(plugin_root: Path): """ Generate the Blueprint header and code and write them to disk. """ inputFile = os.path.join(plugin_root, 'AwsGameKit', 'Libraries', 'include', 'aws', 'gamekit', 'core', 'errors.h') outputHeaderFile = os.path.join(plugin_root, 'AwsGameKit', 'Source', 'AwsGameKitCore', 'Public', 'Core', 'AwsGameKitErrorCodes.h') outputCodeFile = os.path.join(plugin_root, 'AwsGameKit', 'Source', 'AwsGameKitCore', 'Private', 'Core', 'AwsGameKitErrorCodes.cpp') error_code_pattern = "\s*static const unsigned int ([\w_]+)\s+=\s+[\w_]+;\s*" error_codes = [] logging.info(f'Reading error codes from {inputFile}') with open(inputFile, 'r') as source: for line in source: regexMatch = re.search(error_code_pattern, line) if regexMatch: error_codes.append(regexMatch.group(1)) logging.info(f'Writing blueprint header to {outputHeaderFile}') with open(outputHeaderFile, 'w') as header: header.write(_header_template(error_codes)) logging.info(f'Writing blueprint code to {outputCodeFile}') with open(outputCodeFile, 'w') as code: code.write(_code_template(error_codes)) logging.info(f'Finished Blueprints for Error codes') def _header_template(error_codes_list: List[str]) -> str: """ Create the Blueprint header. """ def _make_func(error_code: str) -> List[str]: return [ "UFUNCTION(BlueprintPure, Category = \"AWS GameKit | Status Codes\")", f"static int32 {error_code}() {{ return GameKit::{error_code}; }}\n" ] def _make_content() -> str: content = [] for error_code in error_codes_list: content.extend(_make_func(error_code)) return '\n '.join(content) return f"""// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 /** @file * @brief The Blueprint-friendly GameKit status codes. This was generated by a script, do not modify!" */ #pragma once // GameKit #include // Unreal #include "UObject/Object.h" #include "Containers/UnrealString.h" #include "Kismet/BlueprintFunctionLibrary.h" #include "AwsGameKitErrorCodes.generated.h" /** * @brief The Blueprint-friendly GameKit status codes. */ UCLASS() class AWSGAMEKITCORE_API UAwsGameKitErrorCodes : public UBlueprintFunctionLibrary {{ GENERATED_BODY() static const FString UnknownError; static const TMap CodeToFriendlyName; public: UFUNCTION(BlueprintPure, Category = "AWS GameKit | Status Codes") static const FString& GetGameKitErrorCodeFriendlyName(int32 ErrorCode) {{ const FString* name = CodeToFriendlyName.Find(ErrorCode); return name != nullptr ? *name : UnknownError; }} {_make_content()} }}; """ def _code_template(error_codes_list: List[str]) -> str: """ Create the blueprint code. """ def _make_content() -> str: lines = [] for e in error_codes_list: lines.append(f" {{ static_cast(GameKit::{e}), \"{e}\" }}") return ",\n".join(lines) return f"""// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 /** @file * @brief The Blueprint-friendly GameKit status codes. This was generated by a script, do not modify!" */ // GameKit #include "AwsGameKitErrorCodes.h" #include const FString UAwsGameKitErrorCodes::UnknownError = "UnknownErrorCode"; const TMap UAwsGameKitErrorCodes::CodeToFriendlyName = {{ {_make_content()} }}; """ if __name__ == '__main__': main()