game-macro

Utility Module Documentation

The Utility module provides essential helper functions, object utilities, and ID generation services for the Game Macro system.

English Version 中文版本

Overview

The Utility module provides:

Architecture

The utility system is organized into specialized utility components:

Utils.ahk (General Utilities)

Main utility functions for common operations.

Obj.ahk (Object Utilities)

Object manipulation and management utilities.

IdGen.ahk (ID Generation)

Unique identifier generation system.

Integration Points

Key Components

General Utilities (Utils.ahk)

Provides common utility functions used throughout the system.

Utility Function Categories

Object Utilities (Obj.ahk)

Provides object manipulation and management utilities.

Object Utility Functions

ID Generation (IdGen.ahk)

Provides unique identifier generation services.

ID Generation Types

API Reference

General Utilities (Utils.ahk)

String Operations

Utils_Trim(str)

Trims whitespace from both ends of a string.

Parameters:

Returns: Trimmed string

Utils_FormatString(format, args…)

Formats a string with provided arguments.

Parameters:

Returns: Formatted string

Utils_SplitString(str, delimiter)

Splits a string by delimiter.

Parameters:

Returns: Array of split strings

Mathematical Functions

Utils_Random(min, max)

Generates a random number between min and max.

Parameters:

Returns: Random number

Utils_Clamp(value, min, max)

Clamps a value between min and max.

Parameters:

Returns: Clamped value

Utils_Round(value, decimals)

Rounds a number to specified decimal places.

Parameters:

Returns: Rounded number

File Operations

Utils_GetFileExtension(filename)

Gets the file extension from a filename.

Parameters:

Returns: File extension

Utils_JoinPath(parts…)

Joins path parts into a complete path.

Parameters:

Returns: Joined path string

Utils_FileExists(filepath)

Checks if a file exists.

Parameters:

Returns: Boolean indicating file existence

System Utilities

Utils_GetTimestamp()

Gets the current timestamp in milliseconds.

Parameters: None

Returns: Current timestamp

Utils_Sleep(ms)

Sleeps for specified milliseconds.

Parameters:

Returns: None

Utils_GetSystemInfo()

Gets system information.

Parameters: None

Returns: System information map

Validation Functions

Utils_IsEmpty(value)

Checks if a value is empty.

Parameters:

Returns: Boolean indicating emptiness

Utils_IsNumber(value)

Checks if a value is a number.

Parameters:

Returns: Boolean indicating if value is a number

Utils_IsString(value)

Checks if a value is a string.

Parameters:

Returns: Boolean indicating if value is a string

Object Utilities (Obj.ahk)

Object Creation

Obj_Create(properties)

Creates a new object with specified properties.

Parameters:

Returns: New object

Obj_Clone(obj)

Creates a deep clone of an object.

Parameters:

Returns: Cloned object

Obj_Merge(target, source)

Merges source object into target object.

Parameters:

Returns: Merged object

Object Manipulation

Obj_GetProperty(obj, propertyPath)

Gets a property from an object using dot notation.

Parameters:

Returns: Property value

Obj_SetProperty(obj, propertyPath, value)

Sets a property in an object using dot notation.

Parameters:

Returns: Modified object

Obj_DeleteProperty(obj, propertyPath)

Deletes a property from an object.

Parameters:

Returns: Modified object

Object Validation

Obj_HasProperty(obj, propertyPath)

Checks if an object has a property.

Parameters:

Returns: Boolean indicating property existence

Obj_ValidateStructure(obj, schema)

Validates an object against a schema.

Parameters:

Returns: Boolean indicating validation success

Object Serialization

Obj_Serialize(obj)

Serializes an object to a string.

Parameters:

Returns: Serialized string

Obj_Deserialize(str)

Deserializes a string to an object.

Parameters:

Returns: Deserialized object

ID Generation (IdGen.ahk)

Sequential ID Generation

IdGen_NextSequential()

Generates the next sequential ID.

Parameters: None

Returns: Sequential ID

IdGen_ResetSequential()

Resets the sequential ID counter.

Parameters: None

Returns: Boolean indicating success

UUID Generation

IdGen_GenerateUUID()

Generates a UUID.

Parameters: None

Returns: UUID string

IdGen_ValidateUUID(uuid)

Validates a UUID string.

Parameters:

Returns: Boolean indicating UUID validity

Timestamp ID Generation

IdGen_GenerateTimestampId()

Generates a timestamp-based ID.

Parameters: None

Returns: Timestamp ID

IdGen_ParseTimestampId(timestampId)

Parses a timestamp ID to extract timestamp.

Parameters:

Returns: Parsed timestamp

Custom ID Generation

IdGen_GenerateCustomId(prefix, suffix)

Generates a custom format ID.

Parameters:

Returns: Custom ID

IdGen_SetCustomFormat(format)

Sets the custom ID generation format.

Parameters:

Returns: Boolean indicating success

Usage Examples

General Utilities Usage

; String operations
trimmed := Utils_Trim("  hello world  ") ; "hello world"
formatted := Utils_FormatString("Hello {1}, you have {2} messages", "User", 5)
parts := Utils_SplitString("a,b,c,d", ",") ; ["a", "b", "c", "d"]

; Mathematical functions
random := Utils_Random(1, 100)
clamped := Utils_Clamp(150, 0, 100) ; 100
rounded := Utils_Round(3.14159, 2) ; 3.14

; File operations
extension := Utils_GetFileExtension("document.txt") ; ".txt"
fullPath := Utils_JoinPath("C:", "Users", "Documents", "file.txt")
exists := Utils_FileExists("C:\file.txt")

; System utilities
timestamp := Utils_GetTimestamp()
Utils_Sleep(1000) ; Sleep for 1 second
systemInfo := Utils_GetSystemInfo()

; Validation functions
isEmpty := Utils_IsEmpty("") ; true
isNumber := Utils_IsNumber("123") ; true
isString := Utils_IsString("hello") ; true

Object Utilities Usage

; Object creation
person := Obj_Create({"name": "John", "age": 30})
clone := Obj_Clone(person)
merged := Obj_Merge({"a": 1}, {"b": 2}) ; {"a": 1, "b": 2}

; Object manipulation
name := Obj_GetProperty(person, "name") ; "John"
Obj_SetProperty(person, "address.city", "New York")
Obj_DeleteProperty(person, "age")

; Object validation
hasName := Obj_HasProperty(person, "name") ; true
isValid := Obj_ValidateStructure(person, {"name": "string", "age": "number"})

; Object serialization
serialized := Obj_Serialize(person)
deserialized := Obj_Deserialize(serialized)

ID Generation Usage

; Sequential IDs
id1 := IdGen_NextSequential() ; 1
id2 := IdGen_NextSequential() ; 2
IdGen_ResetSequential()

; UUID generation
uuid := IdGen_GenerateUUID()
isValid := IdGen_ValidateUUID(uuid)

; Timestamp IDs
timestampId := IdGen_GenerateTimestampId()
parsedTime := IdGen_ParseTimestampId(timestampId)

; Custom IDs
customId := IdGen_GenerateCustomId("USER", "ID")
IdGen_SetCustomFormat("{prefix}-{timestamp}-{sequential}")

Integration with Other Modules

; Utility functions in configuration management
profilePath := Utils_JoinPath(App["ProfilesDir"], Utils_FormatString("{1}.json", profileName))

; Object utilities in rule engine
rule := Obj_Create({
    "condition": {
        "type": "pixel",
        "x": 100,
        "y": 200,
        "color": "0xFF0000"
    },
    "action": {
        "type": "skill",
        "skillId": 1
    }
})

; ID generation for entities
skillId := IdGen_NextSequential()
buffId := IdGen_GenerateUUID()

Configuration Integration

Utility Settings

Utility settings can be configured in the main configuration:

App["UtilityConfig"] := {
    StringFormat: "{1}",
    DefaultRandomSeed: 12345,
    IDGeneration: {
        SequentialStart: 1,
        UUIDFormat: "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx",
        TimestampFormat: "yyyyMMddHHmmss"
    }
}

Performance Considerations

Optimization Strategies

  1. Function Efficiency: Utility functions are optimized for performance
  2. Memory Management: Efficient memory usage in object operations
  3. Caching: Appropriate caching for frequently used operations
  4. Validation Optimization: Fast validation algorithms

Best Practices

Error Handling

The utility module includes comprehensive error handling:

Debugging Features

Utility Debug Interface

The module provides debugging capabilities:

; Enable utility debugging
Utils_EnableDebug()

; Get debug information
debugInfo := Utils_GetDebugInfo()

Logging Integration

Utility operations can be logged for troubleshooting:

Dependencies