Skip to content
This repository has been archived by the owner on Apr 15, 2023. It is now read-only.

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
devmaars committed Feb 19, 2023
2 parents c4d9d29 + 370753d commit 489ac9e
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 24 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.3.0] - 2023-02-19

### Added

- New feature Toggle All Values
- New feature Patch All Values
- New feature Restore All Values
- New feature Patcher.patch to apply a patch without adding it to the patcher instance
- New feature patchOnStart in value config to automatically apply the patch when Patcher:run() is called
- Patcher config now accepts menuBuilder as function to build custom menu items in UI (see in example [here](EXAMPLE.md))

## [2.2.0] - 2023-02-19

### Added
Expand Down
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ Create a new Patcher instance.
- `?off` (string) - The text to display when the patch is disabled.
- `?showUiButton` (boolean) - Show the UI button in the menu (default: false). *[see](https://gameguardian.net/help/classgg.html#add52a86cbf6695bb421cc86f4aa0e695)*

- `?menuBuilder(value, config)` (function) - A function to build the menu for the value (default: nil).
- `value` (table) - The value table. with all the fields from **Patcher:add(value)** method and **gg.getValues()** result. *[see](https://gameguardian.net/help/classgg.html#aae2b60904e15c3612a0d2d6385e0e3e3)*

- `config` (table) - The configuration table with all the fields from **Patcher:new(config)** method.


- `return` (Patcher) - The Patcher instance.

Example:
Expand Down Expand Up @@ -149,6 +155,7 @@ Add a new value to the patcher instance.
- `?freeze` (boolean) - Freeze the value (default: false)
- `?state` (boolean) - The initial state of the value (default: false).
- `?processPause` (boolean) - Pause the process before applying the patch and resume it after applying the patch (default: false). *[see](https://gameguardian.net/help/classgg.html#a14e502f895d2e989ebb31dc101f1b325)*
- `?patchOnStart` (boolean) - Apply the patch when the script is started (default: false).



Expand All @@ -157,13 +164,15 @@ Example:
```lua
local Patcher = require("Patcher")

local il2cpp = Patcher.getBaseAddr("libil2cpp.so")

local p = Patcher.new({
title = "Custom Title",
})

p:add({
name = "Damage Multiplier",
address = 0x18643A8,
address = il2cpp + 0x18643A8,
patch = "01 04 A0 E3 1E FF 2F E1r",
})
```
Expand All @@ -190,19 +199,46 @@ Example:
```lua
local Patcher = require("Patcher")

local il2cpp = Patcher.getBaseAddr("libil2cpp.so")

local p = Patcher.new({
title = "Custom Title",
})

p:add({
name = "Damage Multiplier",
address = 0x18643A8,
address = il2cpp + 0x18643A8,
patch = "01 04 A0 E3 1E FF 2F E1r",
})

p:run()
```

<br>

#### `Patcher.patch(address, hex, freeze, processPause)`

Apply a patch to the specified address and freeze the value if specified. (without adding the value to the patcher instance and wont keep track of the value)

**Parameters**

- `address` (number) - The address of the value.
- `hex` (string) - The patch to apply to the value.
- `?freeze` (boolean) - Freeze the value (default: false)
- `?processPause` (boolean) - Pause the process before applying the patch and resume it after applying the patch (default: false). *[see](https://gameguardian.net/help/classgg.html#a14e502f895d2e989ebb31dc101f1b325)*

Example:

```lua
local Patcher = require("Patcher")

local il2cpp = Patcher.getBaseAddr("libil2cpp.so")

Patcher.patch(il2cpp + 0x18643A8, "01 04 A0 E3 1E FF 2F E1r")
Patcher.patch(il2cpp + 0x18643A8, "01 04 A0 E3 1E FF 2F E1r", true)
Patcher.patch(il2cpp + 0x18643A8, "01 04 A0 E3 1E FF 2F E1r", false, true)
```


## Contributing

Expand Down
48 changes: 26 additions & 22 deletions src/index.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ function Patcher.require(version)
end
end

--- Patch a memory address with a hex string.
function Patcher.patch(address, hex, freeze, processPause)
if processPause then gg.processPause() end
if not address then util.error("No address was provided") end
if not hex then util.error("No hex was provided") end
gg.setHex(address, hex:gsub(" ", ""), freeze)
if gg.isProcessPaused() then gg.processResume() end
end

--- Get the base address of the executable memory.
function Patcher.getBaseAddr(filter)
if not filter then
Expand Down Expand Up @@ -76,41 +85,36 @@ function Patcher:run()
return
end

-- Patch all values on start if needed
self.values:forEach(function(v)
if v.patchOnStart then gg.toggleValue(v) end
end)

--- Main function for the patcher.
local function main()
--- Build the menu items string.
local menuItems = self.values:map(function(v)
return util.concat(v.state and self.config.on or self.config.off, " ", v.name)
return self.config.menuBuilder and self.config.menuBuilder(v, self.config) or
util.concat(v.state and self.config.on or self.config.off, " ", v.name)
end)
menuItems[#menuItems + 1] = "Exit"

table.insert(menuItems, "Actions Menu")
table.insert(menuItems, "Exit Script")

local ch = gg.choice(menuItems, 0, self.config.title)

if not ch then return end
if ch == #menuItems - 1 then return util.actionMenu(self.values) end
if ch == #menuItems then util.cleanExit() end

--- Toggle the selected value.
local value = self.values[ch]

-- TODO: Refactor this
if value.processPause then
gg.processPause()
end

if value.state then
gg.setHex(value.address, value.original, value.freeze)
else
gg.setHex(value.address, value.patch, value.freeze)
end

if gg.isProcessPaused() then
gg.processResume()
end

value.state = not value.state
gg.toggleValue(value)
gg.toast(util.concat(value.state and self.config.on or self.config.off, " ", value.name))
end

if self.config.showUiButton then
gg.keepAliveUiButton(main)
end
--- Keep script alive and show UI button if needed.
if self.config.showUiButton then gg.keepAliveUiButton(main) end
gg.keepAlive(main)
end

Expand Down
20 changes: 20 additions & 0 deletions src/lib/gg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ end

--- Set the hex value of a memory address.
gg.setHex = function(address, hex, freeze)
gg.sleep(100)

local values = {}

for i = 1, #hex - 1, 2 do
Expand Down Expand Up @@ -74,4 +76,22 @@ gg.keepAliveUiButton = function(fn)
end
end

gg.toggleValue = function(value)
if value.processPause then
gg.processPause()
end

if value.state then
gg.setHex(value.address, value.original, value.freeze)
else
gg.setHex(value.address, value.patch, value.freeze)
end

if gg.isProcessPaused() then
gg.processResume()
end

value.state = not value.state
end

return gg
7 changes: 7 additions & 0 deletions src/lib/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ table.map = function(t, fn)
return r
end

--- Run fn for each element of t
table.forEach = function(t, fn)
for k, v in pairs(t) do
fn(v, k)
end
end

return table
31 changes: 31 additions & 0 deletions src/utils/util.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
local util = {}

--- Perform selected action on all values.
util.actionMenu = function(values)
gg.setVisible(true)

local ch = gg.choice({
"Toggle All",
"Patch All",
"Restore All",
"Return to Main Menu"
}, 0, "Actions Menu")
if not ch or ch == 4 then return end

if ch == 1 then
values:forEach(function(v) gg.toggleValue(v) end)
return gg.toast("All values toggled")
end

if ch == 2 then
values:forEach(function(v) if not v.state then gg.toggleValue(v) end end)
return gg.toast("All values patched")
end

if ch == 3 then
values:forEach(function(v) if v.state then gg.toggleValue(v) end end)
return gg.toast("All values restored")
end
end


--- Alert the user and exit the script.
util.error = function(msg)
gg.alert(msg, "Exit")
Expand Down Expand Up @@ -33,4 +62,6 @@ util.cleanExit = function()
os.exit()
end



return util

0 comments on commit 489ac9e

Please sign in to comment.