Welcome to PenUltima Online. Click to login or register.

File: OS.em

Description: POL System Environment Functions
Last Modified: 10/17/2023

File Constants:

// set_script_option constants
const SCRIPTOPT_NO_INTERRUPT := 1; // if 1, script runs until it sleeps
const SCRIPTOPT_DEBUG := 2; // if 1, prints any debug info included
const SCRIPTOPT_NO_RUNAWAY := 3; // if 1, doesn't warn about runaway conditions
const SCRIPTOPT_CAN_ACCESS_OFFLINE_MOBILES := 4;
const SCRIPTOPT_AUXSVC_ASSUME_STRING := 5;
const SCRIPTOPT_SURVIVE_ATTACHED_DISCONNECT := 6; // if 1, do not kill script if attached character's client disconnects
const HTTPREQUEST_EXTENDED_RESPONSE := 0x0001; // return Dictionary with various response data instead of a String of response body

Modules:

Clear_Event_Queue()
Explanation
Empties the current script's event queue
Return values
1 on success
Create_Debug_Context()
Explanation
TBD
Return values
TBD
Debugger()
Explanation
puts the script in debug state
Return values
1 on success
Events_Waiting()
Explanation
Returns the number of events waiting in the event queue for this script.
Return values
Integer number of events waiting
GetEnvironmentVariable(name:="")
Parameters:
Name Type
name String name of environment variable, or empty string
Explanation
Returns the value of given environment variable, or if no name is given it returns a dictionary with all allowed environment variables.
pol.cfg AllowedEnvironmentVariablesAccess setting can be used to restrict access to the environmental variables. Provide a comma-separated list to limit scripts' access to only the variables provided. Use * to allow all variables. Default value: empty (no access)
Return values
String value of given enviroment variable, or if no name is given Dictionary{String, String}
Related Configs
pol.cfg
GetPid()
Explanation
Returns the Process ID for this script.
Return values
Integer Process ID
GetProcess(pid := -1)
Parameters:
Name Type
pid Integer process ID of a script (optional)
Explanation
Gets a script process object for the given script's PID.
If no PID is supplied uses PID of the current script.
Return values
A Script object
Errors
"Process not found"
"Invalid parameter type"
Related
Script
HTTPRequest(url, method := "GET", options := struct{}, flags := 0)
Parameters:
Name Type
url URL
method HTTP Method to use for request (eg. GET, POST)
options A struct containing options for the request. Available options are: 'data', a string representing the POST data; 'headers', a struct containing headers to send, where the key is the header's name and value is the header's value
flags Flags to modify behavior.
Explanation
Creates an HTTP request, and returns the returned body as a string, or if HTTPREQUEST_EXTENDED_RESPONSE is passed as flags, returns a Dictionary with response data.
Return values
If HTTPREQUEST_EXTENDED_RESPONSE is passed as flags, returns a Dictionary with members: 'status', the numeric HTTP status code; 'statusText', the reason phrase; 'headers', a Dictionary of key-values for each header; 'body', the response text body. Otherwise, returns response body as a string.
Is_Critical()
Explanation
Returns true if the script is critical (by using set_critical())
Return values
1 if critical, else 0.
LoadExportedScript( script_name, params := {} )
Parameters:
Name Type
script_name String name and path of script to run
params array of objects to pass to the script. Array will be unpacked and as single arguments passed (optional)
Explanation
Starts a new script, to be able to later call exported functions. Non-critical or critical depending on the current state of the calling script. Blocks until the program part of the script is finished. See ExportedScript documentation how to call exported functions.
Return values
Array{ExportedScript,return value} The value returned is the return value of the started script, or 1 if no value was returned
Related
ExportedScript
OpenConnection( host, port, scriptdef, params:=0, assume_string:=0, keep_connection:=0 )
Parameters:
Name Type
host Target host
port Target port
scriptdef Name of the script to be started when the connection is established
params A struct of parameters to be sent to the script
assume_string Integer if set to 1 all communication from connection will be sent/received as raw strings.
keep_connection Integer if set to 1 the connection will not timeout and will be kept alive until the auxiliary script exists.
ignore_line_breaks Integer if set to 1 the connection script will receive events for all incoming socket data and not be restricted to data ending in line breaks, with CRLFs included in the event's value. The connection's transmit method will also not append a CRLF to the message.
Explanation
Creates an outgoing TCP/IP connection to the host/port, once connection is open the scriptdef is run and
any params defined in the struct will be passed to that script. The script type should be in the form of an Auxilry Script.
Return values
1 on success
OpenURL( character, url )
Parameters:
Name Type
character Player Character Ref
url String
Explanation
Sends an Open URL packet to the client, to open the url in their Default Browser.
Return values
1 on success
Errors
"No client attached"
PerformanceMeasure(delta_seconds := 10, max_scripts := 100)
Parameters:
Name Type
delta_seconds seconds to observe
max_scripts maximum number of scripts to be returned
Explanation
Monitors for given number of seconds the instructions of all scripts.
Returns struct with the following members:
total_number_observed : number of scripts alive in the timespan
total_instructions : sum of all instructions of the observed scripts
scripts : array of structs (max number is given max_scripts param) sorted by instructions amount
each scripts entry has the following members:
name : script name, if attached with name/npctemplate
instructions : number of executed instructions
pid : PID of the script
percent : percent of the total instruction amount
Return values
see above
Run_Script( script_name, param := 0 )
Parameters:
Name Type
script_name String name and path of script to run
param object to pass to the script. Only one param may be passed. (optional)
Explanation
Starts a new NON-CRITICAL script. Runs until the script exits, returns the return value of the started script.
Tip: write your script to expect a struct or array as its only parameter, so you may pass multiple pieces of data through the struct or array.
Return values
The return value of the started script, or 1 if no value was returned
Errors
"Unable to read script"
"Script exited with an error condition"
"Script does not exist"
"Script descriptor error"
Run_Script_To_Completion( script_name, param := 0 )
Parameters:
Name Type
script_name String name and path of script to run
param object to pass to the script. Only one param may be passed. (optional)
Explanation
Starts a new CRITICAL script. Runs until the script exits, returns the return value of the started script.
Tip: write your script to expect a struct or array as its only parameter, so you may pass multiple pieces of data through the struct or array.
Every 1000 instructions, "Script X running.." is printed to the console
Return values
The return value of the started script, or 1 if no value was returned
Errors
"Unable to read script"
"Script exited with an error condition"
"Script does not exist"
"Script descriptor error"
Set_Critical( critical )
Parameters:
Name Type
critical Boolean (0/1)
Explanation
critical scripts run if they are not blocked, without interruption.
An infinite loop in a critical script will hang the server
Use this in blocks by setting 'critical' to 1 before the critical code and to 0 after it.
Use this function sparingly, only in parts of code that must execute without interruption
Return values
1 on success
Errors
"Invalid parameter type"
Set_Debug( debug )
Parameters:
Name Type
debug Boolean (0/1)
Explanation
if debug=1, and the script was compiled with 'ecompile -i [script].src', each script source line will be printed as it is executed.
if debug=0, disables this output.
Return values
1 on success
Errors
"Invalid parameter type"
Set_Event_Queue_Size(size)
Parameters:
Name Type
size Integer new size
Explanation
Changes the maximum number of events the current script will keep in the queue (additional events will be discarded).
If not called, the default size is 20 events.
Return values
Integer old queue size.
Errors
"Invalid parameter type"
Set_Priority( priority )
Parameters:
Name Type
priority Integer 1..255
Explanation
the priority of a script is how many instructions it executes before switching to another script.
default script priority is 1.
Return values
Integer previous priority
Errors
"Invalid parameter type"
Set_Script_Option( optnum, optval )
Parameters:
Name Type
optnum Integer constant
optval boolean (0/1)
Explanation
Sets one or more options on or off for this script
Constants for this function:
const SCRIPTOPT_NO_INTERRUPT := 1;      // if 1, script runs until it sleeps
const SCRIPTOPT_DEBUG        := 2;      // if 1, prints any debug info included
const SCRIPTOPT_NO_RUNAWAY   := 3;      // if 1, doesn't warn about runaway conditions
const SCRIPTOPT_CAN_ACCESS_OFFLINE_MOBILES := 4;
set_script_option(SCRIPTOPT_NO_INTERRUPT,1) is the same as set_critical(1)
set_script_option(SCRIPTOPT_DEBUG,1) is the same as set_debug(1)
Return values
previous value
Errors
"Unknown Script Option"
"Invalid parameter type"
Sleep( num_seconds )
Parameters:
Name Type
num_seconds Integer
Explanation
Puts the current script to sleep for num_seconds. No further instructions in this script will be processed in this script until the time expires.
DO NOT USE IN A CRITICAL BLOCK! Your server will freeze for the duration!
If this script is attached to a character (like a UseScript), calling detach() before the sleep will allow other scripts to run while this script sleeps.
Return values
0 after sleep
Sleepms( num_milliseconds )
Parameters:
Name Type
num_milliseconds Integer
Explanation
Puts the current script to sleep for num_milliseconds. No further instructions in this script will be processed in this script until the time expires.
DO NOT USE IN A CRITICAL BLOCK! Your server will freeze for the duration!
If this script is attached to a character (like a UseScript), calling detach() before the sleep will allow other scripts to run while this script sleeps.
You're not likely to get any faster resolution than 10ms
Return values
0 after sleep
Start_Script( script_name, param := 0 )
Parameters:
Name Type
script_name String name and path of script to run
param object to pass to the script. Only one param may be passed. (optional)
Explanation
Starts a new script running.
Tip: write your script to expect a struct or array as its only parameter, so you may pass multiple pieces of data through the struct or array.
Return values
A Script object for the started script on success.
Errors
"Error in script name"
"Script X does not exist."
"Unable to start script"
"Invalid parameter type"
Related
Script
Start_Skill_Script( chr, attr_name, script_name := "", param := 0 )
Parameters:
Name Type
chr Character to start the script for
attr_name String of AttributeID to start the script for e.g. 'Anatomy'
script_name If given, starts this script instead of the default one
param object to pass to the script. Only one param may be passed. (optional)
Explanation
starts for given chr and attr_name definied script (overrideable with param script_name)
param is any UObject given to skill script as second param (first is chr)
performs the same checks like normal skillusage (death,frozen,skilldelay,...)
Tip: write your script to expect a struct or array as its only parameter, so you may pass multiple pieces of data through the struct or array.
Return values
A Script object for the started script on success.
Related
Script
SysLog( text, log_verbose:=1, console_color:="" )
Parameters:
Name Type
text String
log_verbose Integer (1/0)
console_color xterm text formatting string e.g. one of the CONSOLE_COLOR constants
Explanation
write text to the console, and to the log file includes context (calling script name). Context will be skipped if parameter log_verbose is set to 0
Parameter console_color needs linux or windows 11, when given ends the line with resetting the formatting ("\x1b[0m"). Via pol.cfg EnableColoredOutput color prints can be disabled. Only the console output receives the formatting strings, the pol.log output is not touched.
Return values
1 on success
Unload_Scripts(scriptname := "")
Parameters:
Name Type
scriptname String filename of script without extension (optional)
Explanation
unload scripts from the script cache (they will be reloaded from disk on demand) currently running scripts will continue as normal.
Passing "" will unload all scripts.
Return values
Number of scripts unloaded
Errors
"Invalid parameter type"
Related
Script
Wait_For_Event( num_seconds_timeout )
Parameters:
Name Type
num_seconds_timeout Integer
Explanation
The primary access to a script's event queue. If there is an event waiting, the function immediately returns with the event (could be a string, int, struct, etc).
If no event is waiting, the script sleeps for num_seconds_timeout or until a new event arrives. If the timeout is reached, the function returns 0.
if timeout is 0, returns immediately
Return values
object on event recv, 0 on timeout