203 lines
6.7 KiB
C++
203 lines
6.7 KiB
C++
|
|
// keygen.cpp -- Win32 GUI license key generator for 时序图设计器.
|
||
|
|
//
|
||
|
|
// Enter a MAC address (e.g. AA-BB-CC-DD-EE-FF), press "生成", and copy the
|
||
|
|
// resulting single-line license string into a .lic file under the app's
|
||
|
|
// `license` folder.
|
||
|
|
//
|
||
|
|
// Algorithm (must match lib/license/license_service.dart):
|
||
|
|
// 1. strip every non-alphanumeric char, upper-case -> AABBCCDDEEFF
|
||
|
|
// 2. each char: 'A'..'Z' -> 01..26, '0'..'9' -> 00..09 (2 digits)
|
||
|
|
// -> 010102020303040405050606
|
||
|
|
// 3. append the literal "26" -> ...060626
|
||
|
|
// 4. reverse the whole string -> 62606050504040303020201010
|
||
|
|
//
|
||
|
|
// Build: see build.bat (uses MSVC cl.exe, /SUBSYSTEM:WINDOWS).
|
||
|
|
|
||
|
|
#ifndef UNICODE
|
||
|
|
#define UNICODE
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#include <windows.h>
|
||
|
|
#include <commctrl.h>
|
||
|
|
|
||
|
|
#include <algorithm>
|
||
|
|
#include <string>
|
||
|
|
|
||
|
|
#pragma comment(lib, "user32.lib")
|
||
|
|
#pragma comment(lib, "gdi32.lib")
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
constexpr int kIdMac = 1001;
|
||
|
|
constexpr int kIdGen = 1002;
|
||
|
|
constexpr int kIdResult = 1003;
|
||
|
|
constexpr int kIdCopy = 1004;
|
||
|
|
|
||
|
|
HWND g_mac = nullptr;
|
||
|
|
HWND g_result = nullptr;
|
||
|
|
|
||
|
|
std::wstring EncodeMac(const std::wstring& mac) {
|
||
|
|
std::wstring clean;
|
||
|
|
for (wchar_t ch : mac) {
|
||
|
|
if ((ch >= L'0' && ch <= L'9') || (ch >= L'a' && ch <= L'z') ||
|
||
|
|
(ch >= L'A' && ch <= L'Z')) {
|
||
|
|
clean.push_back(static_cast<wchar_t>(towupper(ch)));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
std::wstring fwd;
|
||
|
|
for (wchar_t ch : clean) {
|
||
|
|
int v = -1;
|
||
|
|
if (ch >= L'0' && ch <= L'9') {
|
||
|
|
v = ch - L'0';
|
||
|
|
} else if (ch >= L'A' && ch <= L'Z') {
|
||
|
|
v = ch - L'A' + 1;
|
||
|
|
}
|
||
|
|
if (v < 0) continue;
|
||
|
|
fwd.push_back(static_cast<wchar_t>(L'0' + (v / 10)));
|
||
|
|
fwd.push_back(static_cast<wchar_t>(L'0' + (v % 10)));
|
||
|
|
}
|
||
|
|
fwd += L"26";
|
||
|
|
std::wstring rev(fwd.rbegin(), fwd.rend());
|
||
|
|
return rev;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::wstring GetText(HWND h) {
|
||
|
|
const int len = GetWindowTextLengthW(h);
|
||
|
|
std::wstring s(len > 0 ? len : 0, L'\0');
|
||
|
|
if (len > 0) GetWindowTextW(h, &s[0], len + 1);
|
||
|
|
return s;
|
||
|
|
}
|
||
|
|
|
||
|
|
void DoGenerate() {
|
||
|
|
const std::wstring mac = GetText(g_mac);
|
||
|
|
const std::wstring lic = EncodeMac(mac);
|
||
|
|
SetWindowTextW(g_result, lic.c_str());
|
||
|
|
}
|
||
|
|
|
||
|
|
void DoCopy(HWND owner) {
|
||
|
|
const std::wstring lic = GetText(g_result);
|
||
|
|
if (lic.empty()) return;
|
||
|
|
if (!OpenClipboard(owner)) return;
|
||
|
|
EmptyClipboard();
|
||
|
|
const size_t bytes = (lic.size() + 1) * sizeof(wchar_t);
|
||
|
|
HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, bytes);
|
||
|
|
if (mem != nullptr) {
|
||
|
|
void* dst = GlobalLock(mem);
|
||
|
|
if (dst != nullptr) {
|
||
|
|
memcpy(dst, lic.c_str(), bytes);
|
||
|
|
GlobalUnlock(mem);
|
||
|
|
SetClipboardData(CF_UNICODETEXT, mem);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
CloseClipboard();
|
||
|
|
}
|
||
|
|
|
||
|
|
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
|
||
|
|
switch (msg) {
|
||
|
|
case WM_CREATE: {
|
||
|
|
HFONT font = static_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT));
|
||
|
|
auto label = [&](const wchar_t* text, int x, int y, int w, int h) {
|
||
|
|
HWND c = CreateWindowExW(0, L"STATIC", text, WS_CHILD | WS_VISIBLE, x,
|
||
|
|
y, w, h, hwnd, nullptr, nullptr, nullptr);
|
||
|
|
SendMessageW(c, WM_SETFONT, reinterpret_cast<WPARAM>(font), TRUE);
|
||
|
|
};
|
||
|
|
label(L"MAC 地址(例如 AA-BB-CC-DD-EE-FF):", 16, 16, 360, 20);
|
||
|
|
g_mac = CreateWindowExW(WS_EX_CLIENTEDGE, L"EDIT", L"", WS_CHILD |
|
||
|
|
WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL, 16, 40,
|
||
|
|
420, 28, hwnd, reinterpret_cast<HMENU>(static_cast<INT_PTR>(kIdMac)),
|
||
|
|
nullptr, nullptr);
|
||
|
|
SendMessageW(g_mac, WM_SETFONT, reinterpret_cast<WPARAM>(font), TRUE);
|
||
|
|
|
||
|
|
HWND gen = CreateWindowExW(0, L"BUTTON", L"生成授权串",
|
||
|
|
WS_CHILD | WS_VISIBLE | WS_TABSTOP |
|
||
|
|
BS_DEFPUSHBUTTON,
|
||
|
|
448, 40, 120, 28, hwnd,
|
||
|
|
reinterpret_cast<HMENU>(static_cast<INT_PTR>(kIdGen)), nullptr,
|
||
|
|
nullptr);
|
||
|
|
SendMessageW(gen, WM_SETFONT, reinterpret_cast<WPARAM>(font), TRUE);
|
||
|
|
|
||
|
|
label(L"授权串(保存为 .lic 文件,放入 license 文件夹):", 16, 84, 460, 20);
|
||
|
|
g_result = CreateWindowExW(WS_EX_CLIENTEDGE, L"EDIT", L"",
|
||
|
|
WS_CHILD | WS_VISIBLE | ES_READONLY |
|
||
|
|
ES_AUTOHSCROLL,
|
||
|
|
16, 108, 552, 28, hwnd,
|
||
|
|
reinterpret_cast<HMENU>(static_cast<INT_PTR>(kIdResult)), nullptr,
|
||
|
|
nullptr);
|
||
|
|
SendMessageW(g_result, WM_SETFONT, reinterpret_cast<WPARAM>(font), TRUE);
|
||
|
|
|
||
|
|
HWND copy = CreateWindowExW(0, L"BUTTON", L"复制",
|
||
|
|
WS_CHILD | WS_VISIBLE | WS_TABSTOP, 16, 150,
|
||
|
|
120, 30, hwnd,
|
||
|
|
reinterpret_cast<HMENU>(static_cast<INT_PTR>(kIdCopy)), nullptr,
|
||
|
|
nullptr);
|
||
|
|
SendMessageW(copy, WM_SETFONT, reinterpret_cast<WPARAM>(font), TRUE);
|
||
|
|
|
||
|
|
label(L"提示:生成后把结果粘贴到 license\\<机器名>.lic(仅一行)。",
|
||
|
|
16, 192, 560, 20);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
case WM_COMMAND: {
|
||
|
|
switch (LOWORD(wparam)) {
|
||
|
|
case kIdGen:
|
||
|
|
DoGenerate();
|
||
|
|
return 0;
|
||
|
|
case kIdCopy:
|
||
|
|
DoCopy(hwnd);
|
||
|
|
return 0;
|
||
|
|
default:
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case WM_CLOSE:
|
||
|
|
DestroyWindow(hwnd);
|
||
|
|
return 0;
|
||
|
|
case WM_DESTROY:
|
||
|
|
PostQuitMessage(0);
|
||
|
|
return 0;
|
||
|
|
default:
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
return DefWindowProcW(hwnd, msg, wparam, lparam);
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR, int show) {
|
||
|
|
INITCOMMONCONTROLSEX icc{};
|
||
|
|
icc.dwSize = sizeof(icc);
|
||
|
|
icc.dwICC = ICC_STANDARD_CLASSES;
|
||
|
|
InitCommonControlsEx(&icc);
|
||
|
|
|
||
|
|
WNDCLASSEXW wc{};
|
||
|
|
wc.cbSize = sizeof(wc);
|
||
|
|
wc.style = CS_HREDRAW | CS_VREDRAW;
|
||
|
|
wc.lpfnWndProc = WndProc;
|
||
|
|
wc.hInstance = instance;
|
||
|
|
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
|
||
|
|
wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
|
||
|
|
wc.lpszClassName = L"SeqStudioKeygen";
|
||
|
|
RegisterClassExW(&wc);
|
||
|
|
|
||
|
|
const int w = 600, h = 260;
|
||
|
|
const int x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
|
||
|
|
const int y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
|
||
|
|
HWND hwnd = CreateWindowExW(0, wc.lpszClassName, L"时序图设计器 · 授权码生成器",
|
||
|
|
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |
|
||
|
|
WS_MINIMIZEBOX,
|
||
|
|
x, y, w, h, nullptr, nullptr, instance, nullptr);
|
||
|
|
if (hwnd == nullptr) return 1;
|
||
|
|
|
||
|
|
ShowWindow(hwnd, show);
|
||
|
|
UpdateWindow(hwnd);
|
||
|
|
|
||
|
|
MSG msg;
|
||
|
|
while (GetMessageW(&msg, nullptr, 0, 0)) {
|
||
|
|
if (!IsDialogMessageW(hwnd, &msg)) {
|
||
|
|
TranslateMessage(&msg);
|
||
|
|
DispatchMessageW(&msg);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return static_cast<int>(msg.wParam);
|
||
|
|
}
|