The Utility module provides essential helper functions, object utilities, and ID generation services for the Game Macro system.
| English Version | 中文版本 |
The Utility module provides:
The utility system is organized into specialized utility components:
Main utility functions for common operations.
Object manipulation and management utilities.
Unique identifier generation system.
Provides common utility functions used throughout the system.
Provides object manipulation and management utilities.
Provides unique identifier generation services.
Trims whitespace from both ends of a string.
Parameters:
str (String): Input stringReturns: Trimmed string
Formats a string with provided arguments.
Parameters:
format (String): Format stringargs... (Variadic): Format argumentsReturns: Formatted string
Splits a string by delimiter.
Parameters:
str (String): Input stringdelimiter (String): Delimiter characterReturns: Array of split strings
Generates a random number between min and max.
Parameters:
min (Number): Minimum valuemax (Number): Maximum valueReturns: Random number
Clamps a value between min and max.
Parameters:
value (Number): Input valuemin (Number): Minimum valuemax (Number): Maximum valueReturns: Clamped value
Rounds a number to specified decimal places.
Parameters:
value (Number): Input valuedecimals (Integer): Number of decimal placesReturns: Rounded number
Gets the file extension from a filename.
Parameters:
filename (String): FilenameReturns: File extension
Joins path parts into a complete path.
Parameters:
parts... (Variadic): Path componentsReturns: Joined path string
Checks if a file exists.
Parameters:
filepath (String): File pathReturns: Boolean indicating file existence
Gets the current timestamp in milliseconds.
Parameters: None
Returns: Current timestamp
Sleeps for specified milliseconds.
Parameters:
ms (Integer): Milliseconds to sleepReturns: None
Gets system information.
Parameters: None
Returns: System information map
Checks if a value is empty.
Parameters:
value (Any): Value to checkReturns: Boolean indicating emptiness
Checks if a value is a number.
Parameters:
value (Any): Value to checkReturns: Boolean indicating if value is a number
Checks if a value is a string.
Parameters:
value (Any): Value to checkReturns: Boolean indicating if value is a string
Creates a new object with specified properties.
Parameters:
properties (Map): Object propertiesReturns: New object
Creates a deep clone of an object.
Parameters:
obj (Map): Object to cloneReturns: Cloned object
Merges source object into target object.
Parameters:
target (Map): Target objectsource (Map): Source objectReturns: Merged object
Gets a property from an object using dot notation.
Parameters:
obj (Map): Source objectpropertyPath (String): Property pathReturns: Property value
Sets a property in an object using dot notation.
Parameters:
obj (Map): Target objectpropertyPath (String): Property pathvalue (Any): Value to setReturns: Modified object
Deletes a property from an object.
Parameters:
obj (Map): Target objectpropertyPath (String): Property pathReturns: Modified object
Checks if an object has a property.
Parameters:
obj (Map): Object to checkpropertyPath (String): Property pathReturns: Boolean indicating property existence
Validates an object against a schema.
Parameters:
obj (Map): Object to validateschema (Map): Validation schemaReturns: Boolean indicating validation success
Serializes an object to a string.
Parameters:
obj (Map): Object to serializeReturns: Serialized string
Deserializes a string to an object.
Parameters:
str (String): Serialized stringReturns: Deserialized object
Generates the next sequential ID.
Parameters: None
Returns: Sequential ID
Resets the sequential ID counter.
Parameters: None
Returns: Boolean indicating success
Generates a UUID.
Parameters: None
Returns: UUID string
Validates a UUID string.
Parameters:
uuid (String): UUID to validateReturns: Boolean indicating UUID validity
Generates a timestamp-based ID.
Parameters: None
Returns: Timestamp ID
Parses a timestamp ID to extract timestamp.
Parameters:
timestampId (String): Timestamp IDReturns: Parsed timestamp
Generates a custom format ID.
Parameters:
prefix (String): ID prefixsuffix (String): ID suffixReturns: Custom ID
Sets the custom ID generation format.
Parameters:
format (String): Custom format stringReturns: Boolean indicating success
; 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 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)
; 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}")
; 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()
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"
}
}
The utility module includes comprehensive error handling:
The module provides debugging capabilities:
; Enable utility debugging
Utils_EnableDebug()
; Get debug information
debugInfo := Utils_GetDebugInfo()
Utility operations can be logged for troubleshooting: