Skip to content

refactor(recorder): extract helpers for replay header writes and stats logging#2543

Open
bobtista wants to merge 2 commits intoTheSuperHackers:mainfrom
bobtista:bobtista/refactor/simplify-recorder-header-logging
Open

refactor(recorder): extract helpers for replay header writes and stats logging#2543
bobtista wants to merge 2 commits intoTheSuperHackers:mainfrom
bobtista:bobtista/refactor/simplify-recorder-header-logging

Conversation

@bobtista
Copy link
Copy Markdown

@bobtista bobtista commented Apr 6, 2026

Extract two helpers to reduce duplication across logGameStart, logPlayerDisconnect,
logCRCMismatch, and logGameEnd:

  • writeAtHeaderOffset: handles the repeated seek → write → restore-position
    pattern used to update replay header fields (start time, end time, frame count,
    desync flag, disconnect flags)

  • openStatsLogFile: handles the repeated GetComputerName + path construction +
    fopen pattern used for debug stats logging (RTS_DEBUG only)

No behavior change. Net reduction of ~70 lines per codebase.

@greptile-apps
Copy link
Copy Markdown

greptile-apps bot commented Apr 6, 2026

Greptile Summary

This PR extracts two static helper functions—writeAtHeaderOffset and openStatsLogFile—to eliminate repeated seek/write/restore and GetComputerName/fopen patterns across logGameStart, logPlayerDisconnect, logCRCMismatch, and logGameEnd. The refactoring preserves all existing behavior, including the createDirectory call and fallback-to-user-data-dir logic in logGameStart, and the identical change is applied symmetrically to both the Generals and Zero Hour (GeneralsMD) variants.

Confidence Score: 5/5

Safe to merge — pure refactor with no behavior change and no new issues introduced.

Both helpers are straightforward extractions of well-understood patterns. The fallback path in logGameStart is correctly preserved. logGameEnd now calls writeAtHeaderOffset twice instead of once (one extra size()+seek() round-trip), which is functionally equivalent since the file size doesn't change between header writes. No correctness, security, or data-integrity issues found.

No files require special attention.

Important Files Changed

Filename Overview
Generals/Code/GameEngine/Source/Common/Recorder.cpp Extracts writeAtHeaderOffset and openStatsLogFile helpers; logGameStart fallback path preserved correctly
GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp Identical refactoring applied to Zero Hour variant; same correctness assessment

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A([logGameStart]) --> B{m_file valid?}
    B -- no --> Z([return])
    B -- yes --> C[time → startTime]
    C --> D["writeAtHeaderOffset\n(seek → write → seek back)"]
    D --> E{RTS_DEBUG AND\nTheNetwork AND\nm_saveStats?}
    E -- no --> Z2([return])
    E -- yes --> F[createDirectory\nm_baseStatsDir]
    F --> G["openStatsLogFile()\nm_baseStatsDir + computerName + .txt"]
    G --> H{logFP valid?}
    H -- yes --> J[fprintf + fclose]
    H -- no --> I["fallback:\nm_baseStatsDir = getPath_UserData()"]
    I --> K["openStatsLogFile() retry"]
    K --> L{logFP valid?}
    L -- no --> Z3([return])
    L -- yes --> J
Loading

Reviews (1): Last reviewed commit: "refactor(recorder): replicate to General..." | Re-trigger Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant