- main.go를 main()만 남기고 함수 분리 (game.go, protocol.go, ui.go) - 재시도 로직을 retryWithBackoff 공통 함수로 통합 - redeemTicketFrom 별도 HTTP 클라이언트 → apiClient 사용으로 통일 - doDownload에서 resumeOffset 이중 계산 제거 - extractZip에서 stripTopDir/extractFile 함수 분리 - downloadWithProgress에서 createProgressWindow 함수 분리 - DLL 선언을 DLL별로 그룹화, 상수를 역할별로 분리 - 전체 주석 한국어 통일 및 섹션 구분 추가 버그 수정: - ensureLauncher가 설치 경로 대신 실행 중인 경로를 해시하던 문제 수정 - uninstall 시 실행 중인 exe 삭제 실패 → 백그라운드 cmd로 대체 - moveContents에서 os.Remove 에러를 무시하던 문제 수정 - install/uninstall 메시지 통일, exitWithError 헬퍼 추가 - .gitignore에 *.exe 통일, ANALYSIS.md 삭제 - 빌드 명령에 git 태그 기반 버전 주입 추가 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
460 lines
14 KiB
Go
460 lines
14 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"runtime"
|
|
"syscall"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// ── Win32 메시지 상수 ────────────────────────────────────────
|
|
|
|
const (
|
|
wmDestroy uint32 = 0x0002
|
|
wmClose uint32 = 0x0010
|
|
wmSetFont uint32 = 0x0030
|
|
wmSetText uint32 = 0x000C
|
|
wmCtlColorStatic uint32 = 0x0138
|
|
wmAppDone uint32 = 0x8001 // 다운로드 완료 시 사용하는 커스텀 메시지
|
|
)
|
|
|
|
// ── 윈도우 스타일 상수 ───────────────────────────────────────
|
|
|
|
const (
|
|
wsPopup uintptr = 0x80000000
|
|
wsCaption uintptr = 0x00C00000
|
|
wsSysMenu uintptr = 0x00080000
|
|
wsChild uintptr = 0x40000000
|
|
wsVisible uintptr = 0x10000000
|
|
ssCenter uintptr = 0x00000001
|
|
)
|
|
|
|
// ── 프로그레스바 상수 ────────────────────────────────────────
|
|
|
|
const (
|
|
pbsSmooth uintptr = 0x01
|
|
pbmSetRange32 uint32 = 0x0406
|
|
pbmSetPos uint32 = 0x0402
|
|
pbmSetBarColor uint32 = 0x0409
|
|
pbmSetBkColor uint32 = 0x2001
|
|
)
|
|
|
|
// ── 기타 Win32 상수 ──────────────────────────────────────────
|
|
|
|
const (
|
|
setBkModeTransparent = 1
|
|
|
|
swShow = 5
|
|
smCxScreen = 0
|
|
smCyScreen = 1
|
|
|
|
mbOK uintptr = 0x00000000
|
|
mbInfo uintptr = 0x00000040
|
|
mbError uintptr = 0x00000010
|
|
mbYesNo uintptr = 0x00000004
|
|
mbQ uintptr = 0x00000020
|
|
idYes = 6
|
|
|
|
iccProgressClass uint32 = 0x00000020
|
|
)
|
|
|
|
// ── 색상 ─────────────────────────────────────────────────────
|
|
|
|
// rgb COLORREF(0x00BBGGRR) 값을 생성한다.
|
|
func rgb(r, g, b uint8) uintptr {
|
|
return uintptr(r) | (uintptr(g) << 8) | (uintptr(b) << 16)
|
|
}
|
|
|
|
// 웹사이트 색상과 동일한 팔레트
|
|
var (
|
|
colorBg = rgb(46, 44, 47) // #2E2C2F
|
|
colorText = rgb(200, 200, 200) // 밝은 회색
|
|
colorAccent = rgb(186, 205, 176) // #BACDB0
|
|
colorProgressBg = rgb(65, 63, 67) // bg보다 약간 밝은 색
|
|
)
|
|
|
|
// ── DLL 및 프로시저 ──────────────────────────────────────────
|
|
|
|
var (
|
|
user32 = windows.NewLazySystemDLL("user32.dll")
|
|
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
|
|
gdi32 = windows.NewLazySystemDLL("gdi32.dll")
|
|
comctl32 = windows.NewLazySystemDLL("comctl32.dll")
|
|
uxtheme = windows.NewLazySystemDLL("uxtheme.dll")
|
|
)
|
|
|
|
// user32.dll
|
|
var (
|
|
messageBoxWProc = user32.NewProc("MessageBoxW")
|
|
registerClassExWProc = user32.NewProc("RegisterClassExW")
|
|
createWindowExWProc = user32.NewProc("CreateWindowExW")
|
|
showWindowProc = user32.NewProc("ShowWindow")
|
|
updateWindowProc = user32.NewProc("UpdateWindow")
|
|
getMessageWProc = user32.NewProc("GetMessageW")
|
|
translateMsgProc = user32.NewProc("TranslateMessage")
|
|
dispatchMsgWProc = user32.NewProc("DispatchMessageW")
|
|
sendMessageWProc = user32.NewProc("SendMessageW")
|
|
postMessageWProc = user32.NewProc("PostMessageW")
|
|
defWindowProcWProc = user32.NewProc("DefWindowProcW")
|
|
destroyWindowProc = user32.NewProc("DestroyWindow")
|
|
postQuitMsgProc = user32.NewProc("PostQuitMessage")
|
|
getSystemMetricsProc = user32.NewProc("GetSystemMetrics")
|
|
getDpiForSystemProc = user32.NewProc("GetDpiForSystem")
|
|
setProcessDpiAwarenessContextProc = user32.NewProc("SetProcessDpiAwarenessContext")
|
|
findWindowWProc = user32.NewProc("FindWindowW")
|
|
setForegroundWindowProc = user32.NewProc("SetForegroundWindow")
|
|
)
|
|
|
|
// kernel32.dll
|
|
var (
|
|
createMutexWProc = kernel32.NewProc("CreateMutexW")
|
|
getModuleHandleWProc = kernel32.NewProc("GetModuleHandleW")
|
|
)
|
|
|
|
// gdi32.dll
|
|
var (
|
|
createFontIndirectWProc = gdi32.NewProc("CreateFontIndirectW")
|
|
createSolidBrushProc = gdi32.NewProc("CreateSolidBrush")
|
|
setTextColorProc = gdi32.NewProc("SetTextColor")
|
|
setBkModeProc = gdi32.NewProc("SetBkMode")
|
|
deleteObjectProc = gdi32.NewProc("DeleteObject")
|
|
)
|
|
|
|
// comctl32.dll / uxtheme.dll
|
|
var (
|
|
initCommonControlsExProc = comctl32.NewProc("InitCommonControlsEx")
|
|
setWindowThemeProc = uxtheme.NewProc("SetWindowTheme")
|
|
)
|
|
|
|
// ── Win32 구조체 ─────────────────────────────────────────────
|
|
|
|
type wndClassExW struct {
|
|
cbSize uint32
|
|
style uint32
|
|
lpfnWndProc uintptr
|
|
cbClsExtra int32
|
|
cbWndExtra int32
|
|
hInstance uintptr
|
|
hIcon uintptr
|
|
hCursor uintptr
|
|
hbrBackground uintptr
|
|
lpszMenuName *uint16
|
|
lpszClassName *uint16
|
|
hIconSm uintptr
|
|
}
|
|
|
|
type msgW struct {
|
|
hwnd uintptr
|
|
message uint32
|
|
wParam uintptr
|
|
lParam uintptr
|
|
time uint32
|
|
ptX int32
|
|
ptY int32
|
|
}
|
|
|
|
type initCommonControlsExS struct {
|
|
dwSize uint32
|
|
dwICC uint32
|
|
}
|
|
|
|
type logFontW struct {
|
|
lfHeight int32
|
|
lfWidth int32
|
|
lfEscapement int32
|
|
lfOrientation int32
|
|
lfWeight int32
|
|
lfItalic byte
|
|
lfUnderline byte
|
|
lfStrikeOut byte
|
|
lfCharSet byte
|
|
lfOutPrecision byte
|
|
lfClipPrecision byte
|
|
lfQuality byte
|
|
lfPitchAndFamily byte
|
|
lfFaceName [32]uint16
|
|
}
|
|
|
|
// ── 윈도우 핸들 (진행 창에서 사용) ──────────────────────────
|
|
|
|
var (
|
|
wndProcCb uintptr
|
|
titleLabelHwnd uintptr
|
|
progressLabelHwnd uintptr
|
|
progressBarHwnd uintptr
|
|
hBrushBg uintptr
|
|
)
|
|
|
|
func init() {
|
|
wndProcCb = syscall.NewCallback(progressWndProc)
|
|
}
|
|
|
|
// ── DPI ──────────────────────────────────────────────────────
|
|
|
|
// enableDPIAwareness Per-Monitor V2 DPI 인식을 선언한다.
|
|
func enableDPIAwareness() {
|
|
// DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = -4 (Windows 10 1703+)
|
|
setProcessDpiAwarenessContextProc.Call(^uintptr(3))
|
|
}
|
|
|
|
func getSystemDPI() uint32 {
|
|
dpi, _, _ := getDpiForSystemProc.Call()
|
|
if dpi == 0 {
|
|
return 96
|
|
}
|
|
return uint32(dpi)
|
|
}
|
|
|
|
// dpiScale 96 DPI 기준 픽셀 값을 현재 DPI에 맞게 변환한다.
|
|
func dpiScale(px int, dpi uint32) uintptr {
|
|
return uintptr(int(float64(px)*float64(dpi)/96.0 + 0.5))
|
|
}
|
|
|
|
// ── 폰트 ─────────────────────────────────────────────────────
|
|
|
|
func createUIFont(pointSize int, dpi uint32, bold bool) uintptr {
|
|
weight := int32(400) // FW_NORMAL
|
|
if bold {
|
|
weight = 700 // FW_BOLD
|
|
}
|
|
lf := logFontW{
|
|
lfHeight: -int32(pointSize) * int32(dpi) / 72,
|
|
lfWeight: weight,
|
|
lfCharSet: 1, // DEFAULT_CHARSET
|
|
lfQuality: 5, // CLEARTYPE_QUALITY
|
|
}
|
|
face, _ := windows.UTF16FromString("Segoe UI")
|
|
copy(lf.lfFaceName[:], face)
|
|
font, _, _ := createFontIndirectWProc.Call(uintptr(unsafe.Pointer(&lf)))
|
|
return font
|
|
}
|
|
|
|
// ── 메시지 박스 ──────────────────────────────────────────────
|
|
|
|
// exitWithError 오류 메시지를 표시하고 프로그램을 종료한다.
|
|
func exitWithError(msg string) {
|
|
msgBox("One of the plans 런처 - 오류", msg, mbOK|mbError)
|
|
os.Exit(1)
|
|
}
|
|
|
|
func msgBox(title, text string, flags uintptr) int {
|
|
t, err := windows.UTF16PtrFromString(title)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
m, err := windows.UTF16PtrFromString(text)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
ret, _, _ := messageBoxWProc.Call(0, uintptr(unsafe.Pointer(m)), uintptr(unsafe.Pointer(t)), flags)
|
|
return int(ret)
|
|
}
|
|
|
|
// ── 단일 인스턴스 ────────────────────────────────────────────
|
|
|
|
func acquireSingleInstance() bool {
|
|
name, _ := windows.UTF16PtrFromString("Global\\A301LauncherMutex")
|
|
_, _, err := createMutexWProc.Call(0, 0, uintptr(unsafe.Pointer(name)))
|
|
// ERROR_ALREADY_EXISTS = 183
|
|
if errno, ok := err.(syscall.Errno); ok && errno == 183 {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func activateExistingWindow() {
|
|
className, _ := windows.UTF16PtrFromString("A301Progress")
|
|
hwnd, _, _ := findWindowWProc.Call(uintptr(unsafe.Pointer(className)), 0)
|
|
if hwnd != 0 {
|
|
setForegroundWindowProc.Call(hwnd)
|
|
}
|
|
}
|
|
|
|
// ── 진행 창 (프로그레스 윈도우) ──────────────────────────────
|
|
|
|
func initCommonControls() {
|
|
icc := initCommonControlsExS{
|
|
dwSize: uint32(unsafe.Sizeof(initCommonControlsExS{})),
|
|
dwICC: iccProgressClass,
|
|
}
|
|
initCommonControlsExProc.Call(uintptr(unsafe.Pointer(&icc)))
|
|
}
|
|
|
|
// progressWndProc 진행 창의 메시지 처리 프로시저.
|
|
func progressWndProc(hwnd, uMsg, wParam, lParam uintptr) uintptr {
|
|
switch uint32(uMsg) {
|
|
case wmClose:
|
|
ret := msgBox("One of the plans 런처", "다운로드를 취소하시겠습니까?", mbYesNo|mbQ)
|
|
if ret == idYes {
|
|
downloadCancelled.Store(true)
|
|
destroyWindowProc.Call(hwnd)
|
|
}
|
|
return 0
|
|
case wmDestroy:
|
|
postQuitMsgProc.Call(0)
|
|
return 0
|
|
case wmAppDone:
|
|
destroyWindowProc.Call(hwnd)
|
|
return 0
|
|
case wmCtlColorStatic:
|
|
hdc := wParam
|
|
setBkModeProc.Call(hdc, setBkModeTransparent)
|
|
if lParam == titleLabelHwnd {
|
|
setTextColorProc.Call(hdc, colorAccent)
|
|
} else {
|
|
setTextColorProc.Call(hdc, colorText)
|
|
}
|
|
return hBrushBg
|
|
}
|
|
ret, _, _ := defWindowProcWProc.Call(hwnd, uMsg, wParam, lParam)
|
|
return ret
|
|
}
|
|
|
|
// setProgress 진행 창의 텍스트와 퍼센트를 갱신한다.
|
|
func setProgress(text string, pct int) {
|
|
if text != "" {
|
|
t, err := windows.UTF16PtrFromString(text)
|
|
if err == nil {
|
|
sendMessageWProc.Call(progressLabelHwnd, uintptr(wmSetText), 0, uintptr(unsafe.Pointer(t)))
|
|
}
|
|
}
|
|
if pct >= 0 {
|
|
sendMessageWProc.Call(progressBarHwnd, uintptr(pbmSetPos), uintptr(pct), 0)
|
|
}
|
|
}
|
|
|
|
// createProgressWindow 다크 테마 진행 창을 생성하고 핸들을 반환한다.
|
|
func createProgressWindow(dpi uint32) (hwnd uintptr, err error) {
|
|
s := func(px int) uintptr { return dpiScale(px, dpi) }
|
|
|
|
hBrushBg, _, _ = createSolidBrushProc.Call(colorBg)
|
|
hInstance, _, _ := getModuleHandleWProc.Call(0)
|
|
|
|
className, _ := windows.UTF16PtrFromString("A301Progress")
|
|
wc := wndClassExW{
|
|
cbSize: uint32(unsafe.Sizeof(wndClassExW{})),
|
|
lpfnWndProc: wndProcCb,
|
|
hInstance: hInstance,
|
|
lpszClassName: className,
|
|
hbrBackground: hBrushBg,
|
|
}
|
|
atom, _, _ := registerClassExWProc.Call(uintptr(unsafe.Pointer(&wc)))
|
|
if atom == 0 {
|
|
return 0, fmt.Errorf("윈도우 클래스 등록 실패")
|
|
}
|
|
|
|
// 화면 중앙에 배치
|
|
screenW, _, _ := getSystemMetricsProc.Call(smCxScreen)
|
|
screenH, _, _ := getSystemMetricsProc.Call(smCyScreen)
|
|
winW := s(440)
|
|
winH := s(152)
|
|
x := (screenW - winW) / 2
|
|
y := (screenH - winH) / 2
|
|
|
|
titleStr, _ := windows.UTF16PtrFromString("One of the plans 런처")
|
|
hwnd, _, _ = createWindowExWProc.Call(
|
|
0,
|
|
uintptr(unsafe.Pointer(className)),
|
|
uintptr(unsafe.Pointer(titleStr)),
|
|
wsPopup|wsCaption|wsSysMenu|wsVisible,
|
|
x, y, winW, winH,
|
|
0, 0, hInstance, 0,
|
|
)
|
|
if hwnd == 0 {
|
|
return 0, fmt.Errorf("다운로드 창 생성 실패")
|
|
}
|
|
|
|
// 폰트
|
|
titleFont := createUIFont(13, dpi, true)
|
|
statusFont := createUIFont(9, dpi, false)
|
|
|
|
// 타이틀 라벨
|
|
staticClass, _ := windows.UTF16PtrFromString("STATIC")
|
|
titleText, _ := windows.UTF16PtrFromString("One of the plans")
|
|
titleLabelHwnd, _, _ = createWindowExWProc.Call(
|
|
0,
|
|
uintptr(unsafe.Pointer(staticClass)),
|
|
uintptr(unsafe.Pointer(titleText)),
|
|
wsChild|wsVisible|ssCenter,
|
|
s(20), s(14), winW-s(40), s(28),
|
|
hwnd, 0, hInstance, 0,
|
|
)
|
|
sendMessageWProc.Call(titleLabelHwnd, uintptr(wmSetFont), titleFont, 1)
|
|
|
|
// 상태 라벨
|
|
initText, _ := windows.UTF16PtrFromString("게임 파일을 다운로드하는 중...")
|
|
progressLabelHwnd, _, _ = createWindowExWProc.Call(
|
|
0,
|
|
uintptr(unsafe.Pointer(staticClass)),
|
|
uintptr(unsafe.Pointer(initText)),
|
|
wsChild|wsVisible|ssCenter,
|
|
s(20), s(52), winW-s(40), s(20),
|
|
hwnd, 0, hInstance, 0,
|
|
)
|
|
sendMessageWProc.Call(progressLabelHwnd, uintptr(wmSetFont), statusFont, 1)
|
|
|
|
// 프로그레스바
|
|
progressClass, _ := windows.UTF16PtrFromString("msctls_progress32")
|
|
progressBarHwnd, _, _ = createWindowExWProc.Call(
|
|
0,
|
|
uintptr(unsafe.Pointer(progressClass)),
|
|
0,
|
|
wsChild|wsVisible|pbsSmooth,
|
|
s(20), s(82), winW-s(40), s(18),
|
|
hwnd, 0, hInstance, 0,
|
|
)
|
|
sendMessageWProc.Call(progressBarHwnd, uintptr(pbmSetRange32), 0, 100)
|
|
|
|
// 비주얼 스타일 비활성화 후 커스텀 색상 적용
|
|
empty, _ := windows.UTF16PtrFromString("")
|
|
setWindowThemeProc.Call(progressBarHwnd, uintptr(unsafe.Pointer(empty)), uintptr(unsafe.Pointer(empty)))
|
|
sendMessageWProc.Call(progressBarHwnd, uintptr(pbmSetBarColor), 0, colorAccent)
|
|
sendMessageWProc.Call(progressBarHwnd, uintptr(pbmSetBkColor), 0, colorProgressBg)
|
|
|
|
showWindowProc.Call(hwnd, swShow)
|
|
updateWindowProc.Call(hwnd)
|
|
|
|
return hwnd, nil
|
|
}
|
|
|
|
// downloadWithProgress 진행 창을 표시하면서 파일을 다운로드하고 추출한다.
|
|
// Win32 메시지 루프를 사용하므로 반드시 메인 고루틴에서 호출해야 한다.
|
|
func downloadWithProgress(downloadURL, destDir string) error {
|
|
runtime.LockOSThread()
|
|
defer runtime.UnlockOSThread()
|
|
|
|
initCommonControls()
|
|
dpi := getSystemDPI()
|
|
|
|
hwnd, err := createProgressWindow(dpi)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer deleteObjectProc.Call(hBrushBg)
|
|
|
|
downloadCancelled.Store(false)
|
|
|
|
// 별도 고루틴에서 다운로드, 완료 시 wmAppDone 메시지로 창 닫기
|
|
errCh := make(chan error, 1)
|
|
go func() {
|
|
errCh <- doDownload(downloadURL, destDir)
|
|
postMessageWProc.Call(hwnd, uintptr(wmAppDone), 0, 0)
|
|
}()
|
|
|
|
// Win32 메시지 루프
|
|
var m msgW
|
|
for {
|
|
ret, _, _ := getMessageWProc.Call(uintptr(unsafe.Pointer(&m)), 0, 0, 0)
|
|
if ret == 0 || ret == ^uintptr(0) {
|
|
break
|
|
}
|
|
translateMsgProc.Call(uintptr(unsafe.Pointer(&m)))
|
|
dispatchMsgWProc.Call(uintptr(unsafe.Pointer(&m)))
|
|
}
|
|
|
|
return <-errCh
|
|
}
|