Skip to content

Commit f40235c

Browse files
authored
refactor(recorder): Remove code duplications related to file write and logging in RecorderClass (#2543)
1 parent 982719a commit f40235c

2 files changed

Lines changed: 120 additions & 256 deletions

File tree

Generals/Code/GameEngine/Source/Common/Recorder.cpp

Lines changed: 60 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -71,58 +71,61 @@ static const UnsignedInt desyncOffset = frameCountOffset + sizeof(UnsignedInt);
7171
static const UnsignedInt quitEarlyOffset = desyncOffset + sizeof(Bool);
7272
static const UnsignedInt disconOffset = quitEarlyOffset + sizeof(Bool);
7373

74-
void RecorderClass::logGameStart(AsciiString options)
74+
static void writeAtOffset(File* file, Int offset, const void* data, Int dataSize)
7575
{
76-
if (!m_file)
77-
return;
78-
79-
time(&startTime);
80-
UnsignedInt fileSize = m_file->size();
81-
// move to appropriate offset
82-
if ( m_file->seek(startTimeOffset, File::seekMode::START) == startTimeOffset )
76+
UnsignedInt fileSize = file->size();
77+
DEBUG_ASSERTCRASH((UnsignedInt)(offset + dataSize) <= fileSize, ("writeAtOffset would exceed file size!"));
78+
if (file->seek(offset, File::seekMode::START) == offset)
8379
{
84-
// save off start time
85-
replay_time_t tmp = (replay_time_t)startTime;
86-
m_file->write(&tmp, sizeof(tmp));
80+
file->write(data, dataSize);
8781
}
88-
// move back to end of stream
8982
#ifdef DEBUG_CRASHING
9083
Int res =
9184
#endif
92-
m_file->seek(fileSize, File::seekMode::START);
85+
file->seek(fileSize, File::seekMode::START);
9386
DEBUG_ASSERTCRASH(res == fileSize, ("Could not seek to end of file!"));
87+
}
88+
89+
#if defined(RTS_DEBUG)
90+
static FILE* openStatsLogFile()
91+
{
92+
unsigned long bufSize = MAX_COMPUTERNAME_LENGTH + 1;
93+
char computerName[MAX_COMPUTERNAME_LENGTH + 1];
94+
if (!GetComputerName(computerName, &bufSize))
95+
{
96+
strcpy(computerName, "unknown");
97+
}
98+
AsciiString statsFile = TheGlobalData->m_baseStatsDir;
99+
statsFile.concat(computerName);
100+
statsFile.concat(".txt");
101+
return fopen(statsFile.str(), "a+");
102+
}
103+
#endif
104+
105+
void RecorderClass::logGameStart(AsciiString options)
106+
{
107+
if (!m_file)
108+
return;
109+
110+
time(&startTime);
111+
replay_time_t tmp = (replay_time_t)startTime;
112+
writeAtOffset(m_file, startTimeOffset, &tmp, sizeof(tmp));
94113

95114
#if defined(RTS_DEBUG)
96115
if (TheNetwork && TheGlobalData->m_saveStats)
97116
{
98-
//if (TheLAN)
117+
TheFileSystem->createDirectory(TheGlobalData->m_baseStatsDir);
118+
FILE *logFP = openStatsLogFile();
119+
if (!logFP)
99120
{
100-
unsigned long bufSize = MAX_COMPUTERNAME_LENGTH + 1;
101-
char computerName[MAX_COMPUTERNAME_LENGTH + 1];
102-
if (!GetComputerName(computerName, &bufSize))
103-
{
104-
strcpy(computerName, "unknown");
105-
}
106-
AsciiString statsFile = TheGlobalData->m_baseStatsDir;
107-
TheFileSystem->createDirectory(statsFile);
108-
statsFile.concat(computerName);
109-
statsFile.concat(".txt");
110-
FILE *logFP = fopen(statsFile.str(), "a+");
111-
if (!logFP)
112-
{
113-
// try again locally
114-
TheWritableGlobalData->m_baseStatsDir = TheGlobalData->getPath_UserData();
115-
statsFile = TheGlobalData->m_baseStatsDir;
116-
statsFile.concat(computerName);
117-
statsFile.concat(".txt");
118-
logFP = fopen(statsFile.str(), "a+");
119-
}
120-
if (logFP)
121-
{
122-
struct tm *t2 = localtime(&startTime);
123-
fprintf(logFP, "\nGame start at %s\tOptions are %s\n", asctime(t2), options.str());
124-
fclose(logFP);
125-
}
121+
TheWritableGlobalData->m_baseStatsDir = TheGlobalData->getPath_UserData();
122+
logFP = openStatsLogFile();
123+
}
124+
if (logFP)
125+
{
126+
struct tm *t2 = localtime(&startTime);
127+
fprintf(logFP, "\nGame start at %s\tOptions are %s\n", asctime(t2), options.str());
128+
fclose(logFP);
126129
}
127130
}
128131
#endif
@@ -138,35 +141,14 @@ void RecorderClass::logPlayerDisconnect(UnicodeString player, Int slot)
138141
{
139142
return;
140143
}
141-
UnsignedInt fileSize = m_file->size();
142-
// move to appropriate offset
144+
Bool flag = TRUE;
143145
Int playerSlotDisconOffset = disconOffset + slot * sizeof(Bool);
144-
if ( m_file->seek(playerSlotDisconOffset, File::seekMode::START) == playerSlotDisconOffset )
145-
{
146-
// save off discon status
147-
Bool flag = TRUE;
148-
m_file->write(&flag, sizeof(flag));
149-
}
150-
// move back to end of stream
151-
#ifdef DEBUG_CRASHING
152-
Int res =
153-
#endif
154-
m_file->seek(fileSize, File::seekMode::START);
155-
DEBUG_ASSERTCRASH(res == fileSize, ("Could not seek to end of file!"));
146+
writeAtOffset(m_file, playerSlotDisconOffset, &flag, sizeof(flag));
156147

157148
#if defined(RTS_DEBUG)
158149
if (TheGlobalData->m_saveStats)
159150
{
160-
unsigned long bufSize = MAX_COMPUTERNAME_LENGTH + 1;
161-
char computerName[MAX_COMPUTERNAME_LENGTH + 1];
162-
if (!GetComputerName(computerName, &bufSize))
163-
{
164-
strcpy(computerName, "unknown");
165-
}
166-
AsciiString statsFile = TheGlobalData->m_baseStatsDir;
167-
statsFile.concat(computerName);
168-
statsFile.concat(".txt");
169-
FILE *logFP = fopen(statsFile.str(), "a+");
151+
FILE *logFP = openStatsLogFile();
170152
if (logFP)
171153
{
172154
time_t t;
@@ -184,35 +166,14 @@ void RecorderClass::logCRCMismatch()
184166
if (!m_file)
185167
return;
186168

187-
UnsignedInt fileSize = m_file->size();
188-
// move to appropriate offset
189-
if ( m_file->seek(desyncOffset, File::seekMode::START) == desyncOffset )
190-
{
191-
// save off desync status
192-
Bool flag = TRUE;
193-
m_file->write(&flag, sizeof(flag));
194-
}
195-
// move back to end of stream
196-
#ifdef DEBUG_CRASHING
197-
Int res =
198-
#endif
199-
m_file->seek(fileSize, File::seekMode::START);
200-
DEBUG_ASSERTCRASH(res == fileSize, ("Could not seek to end of file!"));
169+
Bool flag = TRUE;
170+
writeAtOffset(m_file, desyncOffset, &flag, sizeof(flag));
201171

202172
#if defined(RTS_DEBUG)
203173
if (TheGlobalData->m_saveStats)
204174
{
205175
m_wasDesync = TRUE;
206-
unsigned long bufSize = MAX_COMPUTERNAME_LENGTH + 1;
207-
char computerName[MAX_COMPUTERNAME_LENGTH + 1];
208-
if (!GetComputerName(computerName, &bufSize))
209-
{
210-
strcpy(computerName, "unknown");
211-
}
212-
AsciiString statsFile = TheGlobalData->m_baseStatsDir;
213-
statsFile.concat(computerName);
214-
statsFile.concat(".txt");
215-
FILE *logFP = fopen(statsFile.str(), "a+");
176+
FILE *logFP = openStatsLogFile();
216177
if (logFP)
217178
{
218179
time_t t;
@@ -233,51 +194,22 @@ void RecorderClass::logGameEnd()
233194
time_t t;
234195
time(&t);
235196
UnsignedInt frameCount = TheGameLogic->getFrame();
236-
UnsignedInt fileSize = m_file->size();
237-
// move to appropriate offset
238-
if ( m_file->seek(endTimeOffset, File::seekMode::START) == endTimeOffset )
239-
{
240-
// save off end time
241-
replay_time_t tmp = (replay_time_t)t;
242-
m_file->write(&tmp, sizeof(tmp));
243-
}
244-
// move to appropriate offset
245-
if ( m_file->seek(frameCountOffset, File::seekMode::START) == frameCountOffset )
246-
{
247-
// save off frameCount
248-
m_file->write(&frameCount, sizeof(frameCount));
249-
}
250-
// move back to end of stream
251-
#ifdef DEBUG_CRASHING
252-
Int res =
253-
#endif
254-
m_file->seek(fileSize, File::seekMode::START);
255-
DEBUG_ASSERTCRASH(res == fileSize, ("Could not seek to end of file!"));
197+
replay_time_t tmp = (replay_time_t)t;
198+
writeAtOffset(m_file, endTimeOffset, &tmp, sizeof(tmp));
199+
writeAtOffset(m_file, frameCountOffset, &frameCount, sizeof(frameCount));
256200

257201
#if defined(RTS_DEBUG)
258202
if (TheNetwork && TheGlobalData->m_saveStats)
259203
{
260-
//if (TheLAN)
204+
FILE *logFP = openStatsLogFile();
205+
if (logFP)
261206
{
262-
unsigned long bufSize = MAX_COMPUTERNAME_LENGTH + 1;
263-
char computerName[MAX_COMPUTERNAME_LENGTH + 1];
264-
if (!GetComputerName(computerName, &bufSize))
265-
{
266-
strcpy(computerName, "unknown");
267-
}
268-
AsciiString statsFile = TheGlobalData->m_baseStatsDir;
269-
statsFile.concat(computerName);
270-
statsFile.concat(".txt");
271-
FILE *logFP = fopen(statsFile.str(), "a+");
272-
if (logFP)
273-
{
274-
struct tm *t2 = localtime(&t);
275-
time_t duration = t - startTime;
276-
Int minutes = duration/60;
277-
Int seconds = duration%60;
278-
fprintf(logFP, "Game end at %s(%d:%2.2d elapsed time)\n", asctime(t2), minutes, seconds);
279-
fclose(logFP);
280-
}
207+
struct tm *t2 = localtime(&t);
208+
time_t duration = t - startTime;
209+
Int minutes = duration/60;
210+
Int seconds = duration%60;
211+
fprintf(logFP, "Game end at %s(%d:%2.2d elapsed time)\n", asctime(t2), minutes, seconds);
212+
fclose(logFP);
281213
}
282214
}
283215
#endif

0 commit comments

Comments
 (0)