Skip to content

Instantly share code, notes, and snippets.

@knowlet
Created November 4, 2026 08:08
Show Gist options
  • Select an option

  • Save knowlet/0f17cc0e115cfaf8856e5cfd16a04333 to your computer and use it in GitHub Desktop.

Select an option

Save knowlet/0f17cc0e115cfaf8856e5cfd16a04333 to your computer and use it in GitHub Desktop.
小小 CS 中文版 FRONT LINE Ver.1.49
/* FRONT LINE Ver.1.49 dynamic trainer.
Patches the running process only. It never opens FrontLine.exe for writing.
RVAs are file VAs minus the preferred image base 0x400000, taken from objdump
of the shipped PE (not from a decompiler). ASLR is handled by adding the
live module base. */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tlhelp32.h>
#include <wchar.h>
#include <string.h>
#define IMAGE_SIZE_EXPECT 0x5b000u
#define SIG_RVA 0x36e9u
#define HP_RVA 0x47afcu
#define PTS_RVA 0x51ea4u
#define PTS_CAP 0x98967fu
#define ID_TITLE 10
#define ID_STATUS 100
#define ID_HP 101
#define ID_PTS 102
#define ID_ATTACH 110
#define ID_RESET 111
#define ID_GOD 201
#define ID_MAG 202
#define ID_NADE 203
#define ID_RELOAD 204
#define ID_BUY 205
#define ID_LOCKPTS 206
#define ID_FOOT1 301
#define ID_FOOT2 302
#define TIMER_ID 1
typedef struct Site {
DWORD rva;
BYTE off[8];
BYTE on[8];
UINT len;
} Site;
typedef struct Feature {
int id;
const Site *sites;
int count;
BOOL applied;
} Feature;
static const BYTE kSig[5] = {0x83, 0xF8, 0x01, 0x7C, 0x5D};
/* 0x407130 sub eax,edx -> nop; 0x40717d add edx,-2 -> add edx,0 */
static const Site kGod[] = {
{0x7130, {0x2B, 0xC2}, {0x90, 0x90}, 2},
{0x717D, {0x83, 0xC2, 0xFE}, {0x83, 0xC2, 0x00}, 3},
};
/* 0x4036ee dec eax -> nop. The cmp eax,1 gate stays, so an empty mag still will not fire. */
static const Site kMag[] = {
{0x36EE, {0x48}, {0x90}, 1},
};
/* 0x403860 dec eax -> nop. The count starts at 1, so the same gate stays open. */
static const Site kNade[] = {
{0x3860, {0x48}, {0x90}, 1},
};
/* 0x404e47 je -> jmp, into the existing reserve==-1 path (refill mag, do not subtract). */
static const Site kReload[] = {
{0x4E47, {0x74, 0x2A}, {0xEB, 0x2A}, 2},
};
/* Immediate of the new-game mov [points],0 at 0x4013c7. The relocated address before it is left alone. */
static const Site kPtsInit[] = {
{0x13C7, {0x00, 0x00, 0x00, 0x00}, {0x7F, 0x96, 0x98, 0x00}, 4},
};
/* Ignore the "not enough points" jumps and the three point subtractions. */
static const Site kBuy[] = {
{0x2FBE, {0x0F, 0x8C, 0xA2, 0x00, 0x00, 0x00}, {0x90, 0x90, 0x90, 0x90, 0x90, 0x90}, 6},
{0x2FCA, {0x2B, 0xCF}, {0x90, 0x90}, 2},
{0x3012, {0x7C, 0x52}, {0x90, 0x90}, 2},
{0x301A, {0x2B, 0xC8}, {0x90, 0x90}, 2},
{0x3087, {0x7C, 0x42}, {0x90, 0x90}, 2},
{0x308B, {0x2B, 0xCA}, {0x90, 0x90}, 2},
};
static Feature g_features[] = {
{ID_GOD, kGod, 2, FALSE},
{ID_MAG, kMag, 1, FALSE},
{ID_NADE, kNade, 1, FALSE},
{ID_RELOAD, kReload, 1, FALSE},
{ID_BUY, kBuy, 6, FALSE},
{ID_LOCKPTS, kPtsInit, 1, FALSE},
};
static HINSTANCE g_inst;
static HWND g_wnd;
static HWND g_status;
static HWND g_hp;
static HWND g_pts;
static HFONT g_font;
static HFONT g_titleFont;
static HANDLE g_proc;
static DWORD g_pid;
static DWORD g_base;
static DWORD g_imageSize;
static wchar_t g_sticky[256];
static int g_dpi = 96;
static BOOL g_autoBlock;
static int S(int px) { return MulDiv(px, g_dpi, 96); }
static void setText(HWND h, const wchar_t *s) {
wchar_t cur[256];
if (!h) return;
GetWindowTextW(h, cur, 256);
if (wcscmp(cur, s) != 0) SetWindowTextW(h, s);
}
static void setSticky(const wchar_t *s) {
wcsncpy(g_sticky, s, 255);
g_sticky[255] = 0;
setText(g_status, g_sticky);
}
static void clearSticky(void) { g_sticky[0] = 0; }
static BOOL procAlive(void) {
DWORD code = 0;
if (!g_proc) return FALSE;
if (!GetExitCodeProcess(g_proc, &code)) return FALSE;
return code == STILL_ACTIVE;
}
static void detach(void) {
if (g_proc) CloseHandle(g_proc);
g_proc = NULL;
g_pid = 0;
g_base = 0;
g_imageSize = 0;
for (int i = 0; i < (int)(sizeof g_features / sizeof g_features[0]); i++)
g_features[i].applied = FALSE;
}
static BOOL memRead(DWORD addr, void *buf, SIZE_T n) {
SIZE_T got = 0;
if (!g_proc) return FALSE;
return ReadProcessMemory(g_proc, (LPCVOID)(uintptr_t)addr, buf, n, &got) && got == n;
}
static BOOL memWrite(DWORD addr, const void *buf, SIZE_T n) {
DWORD old = 0;
SIZE_T wrote = 0;
if (!g_proc) return FALSE;
if (!VirtualProtectEx(g_proc, (LPVOID)(uintptr_t)addr, n, PAGE_EXECUTE_READWRITE, &old))
return FALSE;
BOOL ok = WriteProcessMemory(g_proc, (LPVOID)(uintptr_t)addr, buf, n, &wrote) && wrote == n;
FlushInstructionCache(g_proc, (LPCVOID)(uintptr_t)addr, n);
DWORD dummy = 0;
VirtualProtectEx(g_proc, (LPVOID)(uintptr_t)addr, n, old, &dummy);
return ok;
}
static int suspendGame(DWORD *tids, int cap) {
int n = 0;
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (snap == INVALID_HANDLE_VALUE) return 0;
THREADENTRY32 te;
te.dwSize = sizeof te;
if (Thread32First(snap, &te)) {
do {
if (te.th32OwnerProcessID != g_pid || n >= cap) continue;
HANDLE th = OpenThread(THREAD_SUSPEND_RESUME, FALSE, te.th32ThreadID);
if (!th) continue;
if (SuspendThread(th) != (DWORD)-1) tids[n++] = te.th32ThreadID;
CloseHandle(th);
} while (Thread32Next(snap, &te));
}
CloseHandle(snap);
return n;
}
static void resumeGame(const DWORD *tids, int n) {
for (int i = 0; i < n; i++) {
HANDLE th = OpenThread(THREAD_SUSPEND_RESUME, FALSE, tids[i]);
if (!th) continue;
ResumeThread(th);
CloseHandle(th);
}
}
static BOOL imageOk(DWORD base) {
BYTE mz[2];
DWORD lfanew = 0, pe = 0, soi = 0;
WORD machine = 0;
BYTE sig[5];
if (!memRead(base, mz, 2) || mz[0] != 'M' || mz[1] != 'Z') return FALSE;
if (!memRead(base + 0x3c, &lfanew, 4) || lfanew > 0x1000) return FALSE;
if (!memRead(base + lfanew, &pe, 4) || pe != 0x4550) return FALSE;
if (!memRead(base + lfanew + 4, &machine, 2) || machine != 0x14c) return FALSE;
if (!memRead(base + lfanew + 24 + 56, &soi, 4) || soi != IMAGE_SIZE_EXPECT) return FALSE;
if (!memRead(base + SIG_RVA, sig, 5) || memcmp(sig, kSig, 5) != 0) return FALSE;
g_imageSize = soi;
return TRUE;
}
static DWORD findPid(void) {
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snap == INVALID_HANDLE_VALUE) return 0;
PROCESSENTRY32W pe;
pe.dwSize = sizeof pe;
DWORD pid = 0;
if (Process32FirstW(snap, &pe)) {
do {
if (_wcsicmp(pe.szExeFile, L"FrontLine.exe") == 0) {
pid = pe.th32ProcessID;
break;
}
} while (Process32NextW(snap, &pe));
}
CloseHandle(snap);
return pid;
}
static DWORD moduleBaseFrom(DWORD pid, DWORD flags) {
HANDLE snap = CreateToolhelp32Snapshot(flags, pid);
if (snap == INVALID_HANDLE_VALUE) return 0;
MODULEENTRY32W me;
me.dwSize = sizeof me;
DWORD base = 0;
if (Module32FirstW(snap, &me)) {
do {
if (_wcsicmp(me.szModule, L"FrontLine.exe") == 0) {
base = (DWORD)(uintptr_t)me.modBaseAddr;
break;
}
} while (Module32NextW(snap, &me));
}
CloseHandle(snap);
return base;
}
static DWORD moduleBase(DWORD pid) {
DWORD base = moduleBaseFrom(pid, TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32);
if (!base) base = moduleBaseFrom(pid, TH32CS_SNAPMODULE);
if (!base) base = moduleBaseFrom(pid, TH32CS_SNAPMODULE32);
return base;
}
/* Returns 1 on a new attachment, 0 if already attached, -1 on failure. */
static int attachGame(void) {
if (procAlive() && g_base) return 0;
detach();
DWORD pid = findPid();
if (!pid) {
setSticky(L"尚未執行 FrontLine.exe。請先啟動遊戲,再按「附加」。");
return -1;
}
HANDLE proc = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION |
PROCESS_QUERY_INFORMATION,
FALSE, pid);
if (!proc) {
setSticky(L"無法開啟遊戲行程。請用與遊戲相同的權限執行此外掛;若遊戲以系統管理員執行,此外掛也要。");
return -1;
}
g_proc = proc;
g_pid = pid;
DWORD base = moduleBase(pid);
if (!base || !imageOk(base)) {
detach();
g_autoBlock = TRUE;
setSticky(L"找到 FrontLine.exe,但不是這份 Ver.1.49 的程式碼,已停止寫入。");
return -1;
}
g_base = base;
g_autoBlock = FALSE;
clearSticky();
return 1;
}
static Feature *featureById(int id) {
for (int i = 0; i < (int)(sizeof g_features / sizeof g_features[0]); i++) {
if (g_features[i].id == id) return &g_features[i];
}
return NULL;
}
/* 0 ok, 1 byte mismatch, 2 write failed, 3 not attached */
static int applyFeature(Feature *f, BOOL enable) {
if (!f) return 3;
if (!procAlive() || !g_base) return 3;
BYTE cur[8];
int todo[8];
if (f->count > 8) return 1;
for (int i = 0; i < f->count; i++) {
const Site *s = &f->sites[i];
if (s->len == 0 || s->len > 8 || s->rva + s->len > g_imageSize) return 1;
if (!memRead(g_base + s->rva, cur, s->len)) return 2;
const BYTE *want = enable ? s->on : s->off;
const BYTE *expect = enable ? s->off : s->on;
if (memcmp(cur, want, s->len) == 0) {
todo[i] = 0;
} else if (memcmp(cur, expect, s->len) == 0) {
todo[i] = 1;
} else {
return 1;
}
}
DWORD tids[64];
int n = suspendGame(tids, 64);
for (int i = 0; i < f->count; i++) {
if (!todo[i]) continue;
const Site *s = &f->sites[i];
const BYTE *want = enable ? s->on : s->off;
if (!memWrite(g_base + s->rva, want, s->len)) {
resumeGame(tids, n);
return 2;
}
}
resumeGame(tids, n);
f->applied = enable;
return 0;
}
static void restoreAll(void) {
if (!procAlive()) {
detach();
return;
}
for (int i = 0; i < (int)(sizeof g_features / sizeof g_features[0]); i++) {
if (g_features[i].applied) applyFeature(&g_features[i], FALSE);
}
}
static BOOL checked(int id) {
HWND h = GetDlgItem(g_wnd, id);
return h && SendMessageW(h, BM_GETCHECK, 0, 0) == BST_CHECKED;
}
static void setChecked(int id, BOOL on) {
HWND h = GetDlgItem(g_wnd, id);
if (h) SendMessageW(h, BM_SETCHECK, on ? BST_CHECKED : BST_UNCHECKED, 0);
}
static void formatGrouped(wchar_t *dst, int cap, const wchar_t *label, unsigned long v, BOOL ok) {
if (!ok) {
_snwprintf(dst, cap, L"%s—", label);
dst[cap - 1] = 0;
return;
}
wchar_t num[32];
_snwprintf(num, 32, L"%lu", v);
num[31] = 0;
int len = (int)wcslen(num);
wchar_t grouped[48];
int g = 0;
for (int i = 0; i < len; i++) {
if (i > 0 && (len - i) % 3 == 0 && g < 46) grouped[g++] = L',';
grouped[g++] = num[i];
}
grouped[g] = 0;
_snwprintf(dst, cap, L"%s%s", label, grouped);
dst[cap - 1] = 0;
}
static void refreshReadout(void) {
wchar_t line[128];
if (!procAlive() || !g_base) {
formatGrouped(line, 128, L"生命 ", 0, FALSE);
setText(g_hp, line);
formatGrouped(line, 128, L"補給點 ", 0, FALSE);
setText(g_pts, line);
if (!g_sticky[0]) {
if (findPid())
setText(g_status, L"已偵測到 FrontLine.exe。按「附加」後才會讀取與修補。");
else
setText(g_status, L"尚未執行 FrontLine.exe。請先啟動遊戲,再按「附加」。");
}
return;
}
DWORD hp = 0, pts = 0;
BOOL hpOk = memRead(g_base + HP_RVA, &hp, 4);
BOOL ptsOk = memRead(g_base + PTS_RVA, &pts, 4);
formatGrouped(line, 128, L"生命 ", hp, hpOk);
setText(g_hp, line);
formatGrouped(line, 128, L"補給點 ", pts, ptsOk);
setText(g_pts, line);
if (!g_sticky[0]) {
_snwprintf(line, 128, L"已附加 PID %lu 基底 0x%08lX", g_pid, g_base);
line[127] = 0;
setText(g_status, line);
}
if (checked(ID_LOCKPTS) && ptsOk && pts != PTS_CAP) {
DWORD cap = PTS_CAP;
memWrite(g_base + PTS_RVA, &cap, 4);
}
}
static void applyCheckedFeatures(void) {
for (int i = 0; i < (int)(sizeof g_features / sizeof g_features[0]); i++) {
Feature *f = &g_features[i];
BOOL on = checked(f->id);
if (on == f->applied) continue;
int rc = applyFeature(f, on);
if (rc == 0) continue;
setChecked(f->id, f->applied);
if (rc == 1)
setSticky(L"位元組與 Ver.1.49 不符,這項沒有寫入。");
else if (rc == 2)
setSticky(L"寫入記憶體失敗。");
else
setSticky(L"尚未附加遊戲,這項沒有寫入。");
}
}
static void onToggle(int id) {
clearSticky();
Feature *f = featureById(id);
if (!f) return;
BOOL on = checked(id);
if (attachGame() < 0) {
setChecked(id, FALSE);
return;
}
int rc = applyFeature(f, on);
if (rc == 1) {
setChecked(id, f->applied);
setSticky(L"位元組與 Ver.1.49 不符,這項沒有寫入。");
} else if (rc == 2) {
setChecked(id, f->applied);
setSticky(L"寫入記憶體失敗。");
} else if (rc == 3) {
setChecked(id, FALSE);
setSticky(L"尚未附加遊戲,這項沒有寫入。");
}
refreshReadout();
}
static BOOL CALLBACK paintFont(HWND child, LPARAM lp) {
(void)lp;
int id = GetDlgCtrlID(child);
SendMessageW(child, WM_SETFONT, (WPARAM)(id == ID_TITLE ? g_titleFont : g_font), TRUE);
return TRUE;
}
static HWND addStatic(const wchar_t *text, int x, int y, int w, int h, int id, DWORD extra) {
return CreateWindowExW(0, L"STATIC", text,
WS_CHILD | WS_VISIBLE | SS_LEFT | SS_NOPREFIX | extra, S(x), S(y), S(w),
S(h), g_wnd, (HMENU)(INT_PTR)id, g_inst, NULL);
}
static HWND addCheck(const wchar_t *text, int x, int y, int id) {
return CreateWindowExW(0, L"BUTTON", text, WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX,
S(x), S(y), S(420), S(24), g_wnd, (HMENU)(INT_PTR)id, g_inst, NULL);
}
static HWND addButton(const wchar_t *text, int x, int y, int w, int id) {
return CreateWindowExW(0, L"BUTTON", text, WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
S(x), S(y), S(w), S(32), g_wnd, (HMENU)(INT_PTR)id, g_inst, NULL);
}
static void buildUi(void) {
addStatic(L"FRONT LINE 外掛", 20, 14, 440, 32, ID_TITLE, 0);
g_status = addStatic(L"", 20, 48, 440, 58, ID_STATUS, SS_EDITCONTROL);
g_hp = addStatic(L"生命 —", 20, 114, 200, 24, ID_HP, 0);
g_pts = addStatic(L"補給點 —", 230, 114, 230, 24, ID_PTS, 0);
addButton(L"附加", 20, 146, 140, ID_ATTACH);
addButton(L"還原全部", 176, 146, 140, ID_RESET);
CreateWindowExW(0, L"BUTTON", L"修補", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, S(16), S(192),
S(448), S(236), g_wnd, NULL, g_inst, NULL);
addCheck(L"無敵(受到攻擊不扣血)", 32, 220, ID_GOD);
addCheck(L"無限彈匣", 32, 252, ID_MAG);
addCheck(L"無限手榴彈", 32, 284, ID_NADE);
addCheck(L"換彈不消耗備彈", 32, 316, ID_RELOAD);
addCheck(L"免費購買彈藥與手榴彈", 32, 348, ID_BUY);
addCheck(L"鎖定補給點為 9,999,999", 32, 380, ID_LOCKPTS);
addStatic(L"只改目前這場遊戲的記憶體,不會寫入 FrontLine.exe。", 20, 440, 440, 22, ID_FOOT1, 0);
addStatic(L"關閉視窗時會還原程式碼修補。補給點維持當時的數值。", 20, 464, 440, 22, ID_FOOT2, 0);
EnumChildWindows(g_wnd, paintFont, 0);
setText(g_status, L"尚未執行 FrontLine.exe。請先啟動遊戲,再按「附加」。");
}
static LRESULT CALLBACK wndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
switch (msg) {
case WM_CREATE:
g_wnd = hwnd;
buildUi();
SetTimer(hwnd, TIMER_ID, 400, NULL);
return 0;
case WM_COMMAND:
if (HIWORD(wp) != BN_CLICKED) break;
if (LOWORD(wp) == ID_ATTACH) {
g_autoBlock = FALSE;
clearSticky();
if (attachGame() >= 0) {
applyCheckedFeatures();
refreshReadout();
}
return 0;
}
if (LOWORD(wp) == ID_RESET) {
clearSticky();
for (int i = 0; i < (int)(sizeof g_features / sizeof g_features[0]); i++)
setChecked(g_features[i].id, FALSE);
setChecked(ID_LOCKPTS, FALSE);
restoreAll();
refreshReadout();
return 0;
}
if (LOWORD(wp) >= ID_GOD && LOWORD(wp) <= ID_LOCKPTS) {
onToggle(LOWORD(wp));
return 0;
}
break;
case WM_TIMER:
if (!procAlive()) {
BOOL want = checked(ID_LOCKPTS);
for (int i = 0; i < (int)(sizeof g_features / sizeof g_features[0]); i++)
want = want || checked(g_features[i].id);
detach();
if (want && !g_autoBlock) {
clearSticky();
if (attachGame() >= 0) applyCheckedFeatures();
}
}
refreshReadout();
return 0;
case WM_DESTROY:
KillTimer(hwnd, TIMER_ID);
restoreAll();
detach();
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd, msg, wp, lp);
}
int WINAPI wWinMain(HINSTANCE inst, HINSTANCE prev, LPWSTR cmd, int show) {
(void)prev;
(void)cmd;
g_inst = inst;
SetProcessDPIAware();
HDC screen = GetDC(NULL);
if (screen) {
g_dpi = GetDeviceCaps(screen, LOGPIXELSX);
if (g_dpi < 96) g_dpi = 96;
ReleaseDC(NULL, screen);
}
g_font = CreateFontW(-S(16), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, L"Microsoft JhengHei UI");
g_titleFont = CreateFontW(-S(22), 0, 0, 0, FW_SEMIBOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, L"Microsoft JhengHei UI");
WNDCLASSW wc;
memset(&wc, 0, sizeof wc);
wc.lpfnWndProc = wndProc;
wc.hInstance = inst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = L"FrontLineTrainer";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassW(&wc);
RECT rc = {0, 0, S(480), S(512)};
AdjustWindowRect(&rc, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, FALSE);
HWND hwnd = CreateWindowExW(0, L"FrontLineTrainer", L"FRONT LINE 外掛",
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top,
NULL, NULL, inst, NULL);
if (!hwnd) return 1;
ShowWindow(hwnd, show);
UpdateWindow(hwnd);
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
if (g_font) DeleteObject(g_font);
if (g_titleFont) DeleteObject(g_titleFont);
return (int)msg.wParam;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment