# utils

Usage:

`utils.{function}`

This table exposes various utility functions.

***

## Base64Encode <a href="#base64-encode" id="base64-encode"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Encode a string to Base64 format.

**Arguments**

| Name  | Type     | Description    |
| ----- | -------- | -------------- |
| `str` | `string` | Source string. |

**Returns**

| Type     | Description            |
| -------- | ---------------------- |
| `string` | Base64-encoded string. |

**Example**

```lua
local enc = utils.Base64Encode('Hello!'); -- SGVsbG8h
```

***

## Base64Decode <a href="#base64-decode" id="base64-decode"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Decode Base64-encoded string.

**Arguments**

| Name  | Type     | Description            |
| ----- | -------- | ---------------------- |
| `str` | `string` | Base64-encoded string. |

**Returns**

| Type     | Description    |
| -------- | -------------- |
| `string` | Source string. |

**Example**

```lua
local dec = utils.Base64Decode('SGVsbG8h'); -- Hello!
```

***

## GetUnixTime <a href="#get-unix-time" id="get-unix-time"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Returns current time as UNIX timestamp.

**Arguments**

None.

**Returns**

| Type  | Description |
| ----- | ----------- |
| `int` | Timestamp.  |

**Example**

```lua
local ts = utils.GetUnixTime();
```

***

## GetDate <a href="#get-unix-time" id="get-unix-time"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Returns current system date and time.

**Arguments**

None.

**Returns**

| Type    | Description                                                                                          |
| ------- | ---------------------------------------------------------------------------------------------------- |
| `table` | The date object. Fields are `year`, `month`, `day`, `hour`, `minute`, `second` and `daylightSavings` |

**Example**

```lua
local dnt = utils.GetDate();
print(dnt.hour, dnt.minute, dnt.second)
```

***

## Murmur2 <a href="#murmur2" id="murmur2"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Returns MURMUR2-hashed string.

**Arguments**

| Name  | Type     | Description    |
| ----- | -------- | -------------- |
| `str` | `string` | Source string. |

**Returns**

| Type  | Description |
| ----- | ----------- |
| `int` | Hash.       |

**Example**

```lua
local hash = utils.murmur2('Hello');
```

***

## Fnv1a

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Returns FNV1A-hashed string.

**Arguments**

| Name  | Type     | Description    |
| ----- | -------- | -------------- |
| `str` | `string` | Source string. |

**Returns**

| Type  | Description |
| ----- | ----------- |
| `int` | Hash.       |

**Example**

```lua
local hash = utils.Fnv1a('Hello');
```

***

## FindExport <a href="#find-export" id="find-export"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1][<kbd><mark style="background-color:red;">Insecure only<mark style="background-color:red;"></kbd>](#user-content-fn-2)[^2]

Returns an address to an export in an image.

**Arguments**

| Name  | Type     | Description       |
| ----- | -------- | ----------------- |
| `mod` | `string` | Image to look in. |
| `exp` | `string` | Export symbol.    |

**Returns**

| Type  | Description                 |
| ----- | --------------------------- |
| `int` | Address, or `0` on failure. |

**Example**

```lua
local message_box = utils.FindExport('user32.dll', 'MessageBoxA');
```

***

## FindPattern <a href="#find-pattern" id="find-pattern"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1][<kbd><mark style="background-color:red;">Insecure only<mark style="background-color:red;"></kbd>](#user-content-fn-2)[^2]

Searches for a code pattern in an image.

**Arguments**

| Name      | Type     | Description         |
| --------- | -------- | ------------------- |
| `mod`     | `string` | Image to search in. |
| `pattern` | `string` | Code pattern.       |

**Returns**

| Type  | Description                 |
| ----- | --------------------------- |
| `int` | Address, or `0` on failure. |

**Example**

```lua
local something = utils.FindPattern('engine2.dll', 'DE AD ? ? ? ? BE EF');
```

***

## ClipboardGet <a href="#clipboard-get" id="clipboard-get"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1][<kbd><mark style="background-color:red;">Insecure only<mark style="background-color:red;"></kbd>](#user-content-fn-2)[^2]

Returns current clipboard content.

**Arguments**

None.

**Returns**

| Type     | Description        |
| -------- | ------------------ |
| `string` | Clipboard content. |

**Example**

```lua
local clip = utils.ClipboardGet();
```

***

## ClipboardSet <a href="#clipboard-set" id="clipboard-set"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1][<kbd><mark style="background-color:red;">Insecure only<mark style="background-color:red;"></kbd>](#user-content-fn-2)[^2]

Sets new clipboard content.

**Arguments**

| Name  | Type     | Description  |
| ----- | -------- | ------------ |
| `str` | `string` | New content. |

**Returns**

Nothing.

**Example**

```lua
utils.ClipboardSet('Hello!');
```

***

## ArrayToString <a href="#clipboard-set" id="clipboard-set"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Converts an array of bytes into a string.

**Arguments**

| Name    | Type    | Description    |
| ------- | ------- | -------------- |
| `bytes` | `table` | Array of bytes |

**Returns**

| Type     | Description          |
| -------- | -------------------- |
| `string` | The converted string |

**Example**

```lua
-- Prints 'Hello world'
print(utils.ArrayToString({72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100}))
```

***

## StringToArray <a href="#clipboard-set" id="clipboard-set"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Converts a string into an array of bytes (ASCII representation).

**Arguments**

| Name  | Type     | Description          |
| ----- | -------- | -------------------- |
| `str` | `string` | Text to be converted |

**Returns**

| Type    | Description         |
| ------- | ------------------- |
| `table` | The converted table |

**Example**

```lua
-- Prints '72 105'
for _, v in ipairs(utils.StringToArray('Hi')) do
	print(v)
end
```

***

## JsonEncode

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Encodes a lua table into JSON.

**Arguments**

| Name  | Type    | Description           |
| ----- | ------- | --------------------- |
| `obj` | `table` | Table to be converted |

**Returns**

| Type     | Description             |
| -------- | ----------------------- |
| `string` | The JSON-encoded string |

**Example**

```lua
-- Prints '{"text": "abc"}'
print(utils.JsonEncode({['text'] = 'abc'}))
```

***

## JsonDecode

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Decodes a JSON string into a Lua table, returns `nil` if invalid.

**Arguments**

| Name  | Type     | Description     |
| ----- | -------- | --------------- |
| `str` | `string` | The JSON string |

**Returns**

| Type     | Description                                  |
| -------- | -------------------------------------------- |
| `table?` | The decoded JSON object, or `nil` if invalid |

**Example**

```lua
local obj = utils.JsonDecode("[1.0, \"Hello\", false]")

-- Prints '1 Hello false'
print(obj[1], obj[2], obj[3])
```

***

## FileExists

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1][<kbd><mark style="background-color:red;">Insecure only<mark style="background-color:red;"></kbd>](#user-content-fn-2)[^2]

Returns whether a file exists or not. It is relative to your `game/csgo` folder.

**Arguments**

| Name   | Type     | Description       |
| ------ | -------- | ----------------- |
| `path` | `string` | Path to the file. |

**Returns**

| Type      | Description                    |
| --------- | ------------------------------ |
| `boolean` | Whether the file exists or not |

**Example**

```lua
if utils.FileExists('fatality/my_config.txt') then
    print('My custom configuration exists')
end
```

***

## FileRead <a href="#clipboard-set" id="clipboard-set"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1][<kbd><mark style="background-color:red;">Insecure only<mark style="background-color:red;"></kbd>](#user-content-fn-2)[^2]

Reads the contents of a file. It is relative to your `game/csgo` folder.

**Arguments**

| Name   | Type     | Description       |
| ------ | -------- | ----------------- |
| `path` | `string` | Path to the file. |

**Returns**

| Type            | Description                    |
| --------------- | ------------------------------ |
| `table<number>` | Contents of the file, in bytes |

**Example**

```lua
if utils.FileRead('file.txt') then
    local contents = utils.ArrayToString(utils.FileRead('file.txt'))
    print(contents)
end
```

***

## FileWrite <a href="#clipboard-set" id="clipboard-set"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1][<kbd><mark style="background-color:red;">Insecure only<mark style="background-color:red;"></kbd>](#user-content-fn-2)[^2]

Writes any contents to a file. It is relative to your `game/csgo` folder.

**Arguments**

| Name       | Type            | Description                    |
| ---------- | --------------- | ------------------------------ |
| `path`     | `string`        | Path to the file.              |
| `contents` | `table<number>` | Contents of the file, in bytes |

**Returns**

Nothing.

**Example**

```lua
utils.FileWrite('file.txt', utils.StringToArray('Hello world'))
```

***

## FileRemove <a href="#clipboard-set" id="clipboard-set"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1][<kbd><mark style="background-color:red;">Insecure only<mark style="background-color:red;"></kbd>](#user-content-fn-2)[^2]

Removes a file. It is relative to your `game/csgo` folder.

**Arguments**

| Name   | Type     | Description       |
| ------ | -------- | ----------------- |
| `path` | `string` | Path to the file. |

**Returns**

Nothing.

**Example**

```lua
utils.FileRemove('oh-no.txt')
```

***

## FileRename <a href="#clipboard-set" id="clipboard-set"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1][<kbd><mark style="background-color:red;">Insecure only<mark style="background-color:red;"></kbd>](#user-content-fn-2)[^2]

Renames a file, also allows to move files. It is relative to your `game/csgo` folder.

**Arguments**

| Name      | Type     | Description           |
| --------- | -------- | --------------------- |
| `path`    | `string` | Path to the file.     |
| `newPath` | `string` | Path to the new file. |

**Returns**

Nothing.

**Example**

```lua
utils.FileRename('old.txt', 'new.txt')
```

***

## FileEnumerate <a href="#clipboard-set" id="clipboard-set"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1][<kbd><mark style="background-color:red;">Insecure only<mark style="background-color:red;"></kbd>](#user-content-fn-2)[^2]

Finds all files with a certain wildcard or name. It is relative to your `game/csgo` folder.

**Arguments**

| Name       | Type     | Description           |
| ---------- | -------- | --------------------- |
| `wildcard` | `string` | File name or wildcard |

**Returns**

| Type            | Description              |
| --------------- | ------------------------ |
| `table<string>` | All found files, if any. |

**Example**

```lua
local configs = utils.FileEnumerate('fatality/myscript/*.cfg')
```

***

## FileCreateDirectories <a href="#clipboard-set" id="clipboard-set"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1][<kbd><mark style="background-color:red;">Insecure only<mark style="background-color:red;"></kbd>](#user-content-fn-2)[^2]

Creates one or multiple directories. It is relative to your `game/csgo` folder.

**Arguments**

| Name   | Type     | Description                 |
| ------ | -------- | --------------------------- |
| `path` | `string` | Path to the new directories |

**Returns**

Nothing.

**Example**

```lua
utils.FileCreateDirectories('fatality/myscript')
```

***

## DbSave <a href="#clipboard-set" id="clipboard-set"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Saves any data in a Lua table to a local database.&#x20;

{% hint style="info" %}
This method internally converts data into JSON using the same logic as `JsonEncode` .
{% endhint %}

**Arguments**

| Name   | Type     | Description                          |
| ------ | -------- | ------------------------------------ |
| `data` | `table`  | The table containing desired data    |
| `path` | `string` | The path or identifier for this data |

**Returns**

| Type      | Description                  |
| --------- | ---------------------------- |
| `boolean` | `true` if successfully saved |

**Example**

```lua
if utils.DbSave({ my_text = 'hello!' }, 'my_data') then
    print('Successfully saved!')
end
```

***

## DbLoad <a href="#clipboard-set" id="clipboard-set"></a>

[<kbd><mark style="background-color:purple;">**Function**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Loads any data from a local database.

**Arguments**

| Name      | Type     | Description                                            |
| --------- | -------- | ------------------------------------------------------ |
| `path`    | `string` | Path to the file.                                      |
| `default` | `table`  | The default data if path is empty, defaults to `nil` . |

**Returns**

| Type     | Description                                                            |
| -------- | ---------------------------------------------------------------------- |
| `table?` | The saved LUA table, `nil` if not found and no default value is passed |

**Example**

```lua
local my_data = utils.DbLoad('my_data')
if my_data then
    for k, v in pairs(my_data) do print(k, v) end
end
```

[^1]: This field is a function and must be invoked using a dot (.)

[^2]:
