|
| 1 | +using CESDK; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Linq; |
| 6 | + |
| 7 | +namespace CheatEngineAdditions |
| 8 | +{ |
| 9 | + public class CheatEngineAdditions : CESDKPluginClass |
| 10 | + { |
| 11 | + public override string GetPluginName() |
| 12 | + { |
| 13 | + return "Cheat Engine Additions"; |
| 14 | + } |
| 15 | + |
| 16 | + public override bool EnablePlugin() |
| 17 | + { |
| 18 | + sdk.lua.Register("GetRelativeAddress", (IntPtr luaState) => |
| 19 | + { |
| 20 | + sdk.lua.GetGlobal("getOpenedProcessID"); |
| 21 | + sdk.lua.Call(luaState, 0, 1); |
| 22 | + int pid = (int)sdk.lua.ToInteger(luaState, -1); |
| 23 | + sdk.lua.Pop(luaState, 1); |
| 24 | + if (pid == 0) |
| 25 | + { |
| 26 | + sdk.lua.PushString(luaState, "0x0"); |
| 27 | + sdk.lua.DoString(@"showMessage('Not attached to a process! Failed to get relative address')"); |
| 28 | + return 0; |
| 29 | + } |
| 30 | + Process process = Process.GetProcessById(pid); |
| 31 | + if (process == null) |
| 32 | + { |
| 33 | + sdk.lua.PushString(luaState, "0x0"); |
| 34 | + sdk.lua.DoString(@"showMessage('Process is null! Failed to get relative address')"); |
| 35 | + return 0; |
| 36 | + } |
| 37 | + long address = sdk.lua.ToInteger(luaState, 1); |
| 38 | + ProcessModule processModule = process.Modules.Cast<ProcessModule>().FirstOrDefault(m => address >= m.BaseAddress.ToInt64() && address < m.BaseAddress.ToInt64() + m.ModuleMemorySize); |
| 39 | + if (processModule == null) |
| 40 | + { |
| 41 | + sdk.lua.PushString(luaState, "0x0"); |
| 42 | + sdk.lua.DoString(@"showMessage('Process Module is null! Failed to get relative address')"); |
| 43 | + return 0; |
| 44 | + } |
| 45 | + sdk.lua.PushString(luaState, $"0x{(address - processModule.BaseAddress.ToInt64()).ToString("X")}"); |
| 46 | + return 1; |
| 47 | + }); |
| 48 | + sdk.lua.Register("GenerateSignature", (IntPtr luaState) => |
| 49 | + { |
| 50 | + sdk.lua.GetGlobal("getOpenedProcessID"); |
| 51 | + sdk.lua.Call(luaState, 0, 1); |
| 52 | + int pid = (int)sdk.lua.ToInteger(luaState, -1); |
| 53 | + sdk.lua.Pop(luaState, 1); |
| 54 | + if (pid == 0) |
| 55 | + { |
| 56 | + sdk.lua.PushString(luaState, ""); |
| 57 | + sdk.lua.DoString(@"showMessage('Not attached to a process! Failed to get relative address')"); |
| 58 | + return 0; |
| 59 | + } |
| 60 | + long address = sdk.lua.ToInteger(luaState, 1); |
| 61 | + long length = sdk.lua.ToInteger(luaState, 2); |
| 62 | + if (length <= 0) |
| 63 | + { |
| 64 | + sdk.lua.PushString(luaState, ""); |
| 65 | + sdk.lua.DoString(@"showMessage('Invalid length for a signature!')"); |
| 66 | + return 0; |
| 67 | + } |
| 68 | + int intLength = (int)length; |
| 69 | + sdk.lua.GetGlobal("ReadBytes"); |
| 70 | + sdk.lua.PushInteger(luaState, address); |
| 71 | + sdk.lua.PushInteger(luaState, length); |
| 72 | + sdk.lua.Call(luaState, 2, intLength); |
| 73 | + sdk.lua.GetGlobal("ReadBytes"); |
| 74 | + sdk.lua.PushInteger(luaState, address); |
| 75 | + sdk.lua.PushInteger(luaState, length); |
| 76 | + sdk.lua.Call(luaState, 2, intLength); |
| 77 | + byte[] bytes = new byte[length]; |
| 78 | + for (int i = 0; i < length; i++) bytes[i] = (byte)sdk.lua.ToInteger(luaState, -(intLength) + i); |
| 79 | + sdk.lua.Pop(luaState, (int)length); |
| 80 | + List<string> signature = new List<string>(); |
| 81 | + for (int i = 0; i < bytes.Length; i++) |
| 82 | + { |
| 83 | + byte b = bytes[i]; |
| 84 | + if (b == 0x00 || b == 0x90) |
| 85 | + { |
| 86 | + signature.Add("??"); |
| 87 | + continue; |
| 88 | + } |
| 89 | + if ((b == 0xE8 || b == 0xE9) && i + 4 < bytes.Length) |
| 90 | + { |
| 91 | + signature.Add(b.ToString("X2")); |
| 92 | + signature.Add("??"); |
| 93 | + signature.Add("??"); |
| 94 | + signature.Add("??"); |
| 95 | + signature.Add("??"); |
| 96 | + i += 4; |
| 97 | + continue; |
| 98 | + } |
| 99 | + signature.Add(b.ToString("X2")); |
| 100 | + } |
| 101 | + sdk.lua.PushString(luaState, string.Join(" ", signature)); |
| 102 | + return 1; |
| 103 | + }); |
| 104 | + sdk.lua.Register("RefineSignature", (IntPtr luaState) => |
| 105 | + { |
| 106 | + long address = sdk.lua.ToInteger(luaState, 1); |
| 107 | + string oldSignature = sdk.lua.ToString(luaState, 2); |
| 108 | + if (string.IsNullOrEmpty(oldSignature)) |
| 109 | + { |
| 110 | + sdk.lua.PushString(luaState, ""); |
| 111 | + sdk.lua.DoString(@"showMessage('Old Signature is empty or null!')"); |
| 112 | + return 0; |
| 113 | + } |
| 114 | + string[] oldSignatureBytes = oldSignature.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray(); |
| 115 | + sdk.lua.GetGlobal("GenerateSignature"); |
| 116 | + sdk.lua.PushInteger(luaState, address); |
| 117 | + sdk.lua.PushInteger(luaState, oldSignatureBytes.Length); |
| 118 | + sdk.lua.Call(luaState, 2, 1); |
| 119 | + string newSignature = sdk.lua.ToString(luaState, -1); |
| 120 | + sdk.lua.Pop(luaState, 1); |
| 121 | + if (string.IsNullOrEmpty(newSignature)) |
| 122 | + { |
| 123 | + sdk.lua.PushString(luaState, ""); |
| 124 | + sdk.lua.DoString(@"showMessage('New Signature is empty or null!')"); |
| 125 | + return 0; |
| 126 | + } |
| 127 | + string[] newSignatureBytes = newSignature.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray(); |
| 128 | + string[] refinedSignature = oldSignatureBytes.Zip(newSignatureBytes, (oldByte, newByte) => oldByte == "??" || newByte == "??" || !oldByte.Equals(newByte, StringComparison.OrdinalIgnoreCase) ? "??" : oldByte).ToArray(); |
| 129 | + sdk.lua.PushString(luaState, string.Join(" ", refinedSignature)); |
| 130 | + return 1; |
| 131 | + }); |
| 132 | + sdk.lua.Register("ClearAddressesByFilter", (IntPtr luaState) => |
| 133 | + { |
| 134 | + string type = sdk.lua.ToString(luaState, 1); |
| 135 | + sdk.lua.DoString($@" |
| 136 | + local type = '{type}' |
| 137 | + local foundList = getMainForm().FoundList3 |
| 138 | + local menuItemRemove |
| 139 | + for i = 0, foundList.PopupMenu.Items.Count - 1 do |
| 140 | + if foundList.PopupMenu.Items[i].Name == ""Removeselectedaddresses1"" then |
| 141 | + menuItemRemove = foundList.PopupMenu.Items[i] |
| 142 | + break |
| 143 | + end |
| 144 | + end |
| 145 | + for i = foundList.Items.Count - 1, 0, -1 do |
| 146 | + local item = foundList.Items[i] |
| 147 | + if item then |
| 148 | + local value = tonumber(item.SubItems[0]) |
| 149 | + if type == ""invalid"" and (not value or value ~= value or math.abs(value) > 1e100) then |
| 150 | + item.Selected = true |
| 151 | + elseif value then |
| 152 | + if type == ""positive"" and value > 0 then |
| 153 | + item.Selected = true |
| 154 | + elseif type == ""negative"" and value < 0 then |
| 155 | + item.Selected = true |
| 156 | + elseif type == ""zero"" and value == 0 then |
| 157 | + item.Selected = true |
| 158 | + elseif filterType == 'decimals' and value % 1 ~= 0 then |
| 159 | + item.Selected = true |
| 160 | + elseif filterType == 'nodecimals' and value % 1 == 0 then |
| 161 | + item.Selected = true |
| 162 | + else |
| 163 | + item.Selected = false |
| 164 | + end |
| 165 | + else |
| 166 | + item.Selected = false |
| 167 | + end |
| 168 | + end |
| 169 | + end |
| 170 | + menuItemRemove.DoClick() |
| 171 | + "); |
| 172 | + return 1; |
| 173 | + }); |
| 174 | + sdk.lua.DoString(@" |
| 175 | + local menu = MainForm.Menu |
| 176 | + local cheatEngineAdditionsTab = createMenuItem(menu) |
| 177 | + cheatEngineAdditionsTab.Caption='Cheat Engine Additions' |
| 178 | + menu.Items.insert(MainForm.miHelp.MenuIndex, cheatEngineAdditionsTab) |
| 179 | + local memoryView = getMemoryViewForm() |
| 180 | + local disassemblerView = memoryView.DisassemblerView |
| 181 | + local popupMenu = disassemblerView.PopupMenu |
| 182 | +
|
| 183 | + function copyRelativeAddress() |
| 184 | + local relativeAddress = GetRelativeAddress(disassemblerView.SelectedAddress) |
| 185 | + if relativeAddress and relativeAddress ~= ""0x0"" then |
| 186 | + writeToClipboard(relativeAddress) |
| 187 | + showMessage(""Relative Address copied to clipboard "" .. relativeAddress) |
| 188 | + end |
| 189 | + end |
| 190 | +
|
| 191 | + local copyRelativeAddressMenuItem = createMenuItem(popup) |
| 192 | + copyRelativeAddressMenuItem.Caption = ""Copy Relative Address (Ctrl + Q)"" |
| 193 | + copyRelativeAddressMenuItem.OnClick = function(menuItem) |
| 194 | + copyRelativeAddress() |
| 195 | + end |
| 196 | +
|
| 197 | + createHotkey(function() |
| 198 | + copyRelativeAddress() |
| 199 | + end, { VK_CONTROL, VK_Q }) |
| 200 | +
|
| 201 | + function generateSignature() |
| 202 | + local signatureLength = inputQuery(""Signature Length"", ""What do you want the length of the signature to be?"", ""10"") |
| 203 | + if signatureLength then |
| 204 | + local signature = GenerateSignature(disassemblerView.SelectedAddress, signatureLength) |
| 205 | + if signature ~= nil and signature ~= """" then |
| 206 | + writeToClipboard(signature) |
| 207 | + showMessage(""Signature copied to clipboard "" .. signature) |
| 208 | + end |
| 209 | + end |
| 210 | + end |
| 211 | +
|
| 212 | + local generateSignatureMenuItem = createMenuItem(popup) |
| 213 | + generateSignatureMenuItem.Caption = ""Generate Signature (Ctrl + E)"" |
| 214 | + generateSignatureMenuItem.OnClick = function(menuItem) |
| 215 | + generateSignature() |
| 216 | + end |
| 217 | +
|
| 218 | + createHotkey(function() |
| 219 | + generateSignature() |
| 220 | + end, { VK_CONTROL, VK_E }) |
| 221 | +
|
| 222 | + function refineSignature() |
| 223 | + local oldSignature = inputQuery(""Old Signature"", ""Please enter the old signature to refine"", """") |
| 224 | + if oldSignature then |
| 225 | + local refinedSignature = RefineSignature(disassemblerView.SelectedAddress, oldSignature) |
| 226 | + if refinedSignature ~= nil and refinedSignature ~= """" then |
| 227 | + writeToClipboard(refinedSignature) |
| 228 | + showMessage(""Refined Signature copied to clipboard "" .. refinedSignature) |
| 229 | + end |
| 230 | + end |
| 231 | + end |
| 232 | +
|
| 233 | + local refineSignatureMenuItem = createMenuItem(popup) |
| 234 | + refineSignatureMenuItem.Caption = ""Refine Signature (Ctrl + F)"" |
| 235 | + refineSignatureMenuItem.OnClick = function(menuItem) |
| 236 | + refineSignature() |
| 237 | + end |
| 238 | +
|
| 239 | + createHotkey(function() |
| 240 | + refineSignature() |
| 241 | + end, { VK_CONTROL, VK_F }) |
| 242 | +
|
| 243 | + local insertIndex = 0 |
| 244 | + for i = 0, popupMenu.Items.Count - 1 do |
| 245 | + if popupMenu.Items[i].Caption == ""Copy to clipboard"" then |
| 246 | + insertIndex = i |
| 247 | + break |
| 248 | + end |
| 249 | + end |
| 250 | +
|
| 251 | + popupMenu.Items.insert(insertIndex, copyRelativeAddressMenuItem) |
| 252 | + popupMenu.Items.insert(insertIndex, refineSignatureMenuItem) |
| 253 | + popupMenu.Items.insert(insertIndex, generateSignatureMenuItem) |
| 254 | + |
| 255 | + local clearPositiveNumbersMenuItem = createMenuItem(menu) |
| 256 | + clearPositiveNumbersMenuItem.Caption='Clear Positive Numbers'; |
| 257 | + clearPositiveNumbersMenuItem.OnClick=function(menuItem) |
| 258 | + ClearAddressesByFilter(""positive"") |
| 259 | + end |
| 260 | + cheatEngineAdditionsTab.add(clearPositiveNumbersMenuItem) |
| 261 | +
|
| 262 | + local clearNegativeNumbersMenuItem = createMenuItem(menu) |
| 263 | + clearNegativeNumbersMenuItem.Caption='Clear Negative Numbers'; |
| 264 | + clearNegativeNumbersMenuItem.OnClick=function(menuItem) |
| 265 | + ClearAddressesByFilter(""negative"") |
| 266 | + end |
| 267 | + cheatEngineAdditionsTab.add(clearNegativeNumbersMenuItem) |
| 268 | +
|
| 269 | + local clearZerosMenuItem = createMenuItem(menu) |
| 270 | + clearZerosMenuItem.Caption='Clear Zeros'; |
| 271 | + clearZerosMenuItem.OnClick=function(menuItem) |
| 272 | + ClearAddressesByFilter(""zero"") |
| 273 | + end |
| 274 | + cheatEngineAdditionsTab.add(clearZerosMenuItem) |
| 275 | +
|
| 276 | + local clearInvalidsMenuItem = createMenuItem(menu) |
| 277 | + clearInvalidsMenuItem.Caption='Clear Invalid Numbers'; |
| 278 | + clearInvalidsMenuItem.OnClick=function(menuItem) |
| 279 | + ClearAddressesByFilter(""invalid"") |
| 280 | + end |
| 281 | + cheatEngineAdditionsTab.add(clearInvalidsMenuItem) |
| 282 | +
|
| 283 | + local clearDeciminalNumbersMenuItem = createMenuItem(menu) |
| 284 | + clearDeciminalNumbersMenuItem.Caption='Clear Deciminal Numbers'; |
| 285 | + clearDeciminalNumbersMenuItem.OnClick=function(menuItem) |
| 286 | + ClearAddressesByFilter(""nodeciminal"") |
| 287 | + end |
| 288 | + cheatEngineAdditionsTab.add(clearDeciminalNumbersMenuItem) |
| 289 | +
|
| 290 | + local clearNonDeciminalNumbersMenuItem = createMenuItem(menu) |
| 291 | + clearNonDeciminalNumbersMenuItem.Caption='Clear Non Deciminal Numbers'; |
| 292 | + clearNonDeciminalNumbersMenuItem.OnClick=function(menuItem) |
| 293 | + ClearAddressesByFilter(""deciminal"") |
| 294 | + end |
| 295 | + cheatEngineAdditionsTab.add(clearNonDeciminalNumbersMenuItem) |
| 296 | + "); |
| 297 | + return true; |
| 298 | + } |
| 299 | + |
| 300 | + public override bool DisablePlugin() |
| 301 | + { |
| 302 | + return true; |
| 303 | + } |
| 304 | + } |
| 305 | +} |
0 commit comments