Skip to content

Commit fd5b11d

Browse files
committed
* Factored out duplicate compression code in levelEditorHistory.
1 parent fd84787 commit fd5b11d

File tree

2 files changed

+37
-54
lines changed

2 files changed

+37
-54
lines changed

TheForceEngine/TFE_Editor/LevelEditor/levelEditorData.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2920,7 +2920,7 @@ namespace LevelEditor
29202920
// Write sectors only.
29212921
const IndexPair* pair = sectorIds.data();
29222922
s_sectorIds.resize(count);
2923-
for (s32 i = 0; i < count; i++)
2923+
for (u32 i = 0; i < count; i++)
29242924
{
29252925
s_sectorIds[i] = pair[i].i0;
29262926
}
@@ -2931,10 +2931,10 @@ namespace LevelEditor
29312931
const s32 i0 = pair->i0;
29322932
assert(i0 >= 0 && i0 < (s32)s_level.sectors.size());
29332933
EditorSector* sector = &s_level.sectors[i0];
2934-
writeS32(sector->name.length());
2934+
writeU32((u32)sector->name.length());
29352935
if (sector->name.length())
29362936
{
2937-
writeData(sector->name.data(), sector->name.length());
2937+
writeData(sector->name.data(), (u32)sector->name.length());
29382938
}
29392939
const SectorAttrib attrib =
29402940
{

TheForceEngine/TFE_Editor/LevelEditor/levelEditorHistory.cpp

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "levelEditorHistory.h"
22
#include "sharedState.h"
3+
#include "error.h"
34
#include <TFE_System/system.h>
45
#include <TFE_Editor/LevelEditor/levelEditor.h>
56
#include <TFE_Editor/LevelEditor/levelEditorData.h>
@@ -31,6 +32,9 @@ namespace LevelEditor
3132

3233
// Used to merge certain commands.
3334
static TFE_Editor::SnapshotBuffer s_workBuffer[2];
35+
36+
// Helper Functions.
37+
u32 compressBuffer();
3438

3539
// Command Functions
3640
void cmd_applySectorSnapshot();
@@ -107,7 +111,7 @@ namespace LevelEditor
107111
history_step(1);
108112
edit_clearSelections(false);
109113
}
110-
114+
111115
////////////////////////////////
112116
// Editor API
113117
////////////////////////////////
@@ -121,16 +125,8 @@ namespace LevelEditor
121125
if (s_workBuffer[0].empty()) { return; }
122126

123127
const u32 uncompressedSize = (u32)s_workBuffer[0].size();
124-
u32 compressedSize = 0;
125-
if (zstd_compress(s_workBuffer[1], s_workBuffer[0].data(), uncompressedSize, 4))
126-
{
127-
compressedSize = (u32)s_workBuffer[1].size();
128-
}
129-
// ERROR
130-
if (!compressedSize)
131-
{
132-
return;
133-
}
128+
const u32 compressedSize = compressBuffer();
129+
if (!compressedSize) { return; }
134130

135131
CMD_BEGIN(LCmd_Sector_Snapshot, name);
136132
{
@@ -149,16 +145,8 @@ namespace LevelEditor
149145
if (s_workBuffer[0].empty()) { return; }
150146

151147
const u32 uncompressedSize = (u32)s_workBuffer[0].size();
152-
u32 compressedSize = 0;
153-
if (zstd_compress(s_workBuffer[1], s_workBuffer[0].data(), uncompressedSize, 4))
154-
{
155-
compressedSize = (u32)s_workBuffer[1].size();
156-
}
157-
// ERROR
158-
if (!compressedSize)
159-
{
160-
return;
161-
}
148+
const u32 compressedSize = compressBuffer();
149+
if (!compressedSize) { return; }
162150

163151
CMD_BEGIN(LCmd_ObjList_Snapshot, name);
164152
{
@@ -188,16 +176,8 @@ namespace LevelEditor
188176
if (s_workBuffer[0].empty()) { return; }
189177

190178
const u32 uncompressedSize = (u32)s_workBuffer[0].size();
191-
u32 compressedSize = 0;
192-
if (zstd_compress(s_workBuffer[1], s_workBuffer[0].data(), uncompressedSize, 4))
193-
{
194-
compressedSize = (u32)s_workBuffer[1].size();
195-
}
196-
// ERROR
197-
if (!compressedSize)
198-
{
199-
return;
200-
}
179+
const u32 compressedSize = compressBuffer();
180+
if (!compressedSize) { return; }
201181

202182
CMD_BEGIN(LCmd_Sector_Wall_Snapshot, name);
203183
{
@@ -215,6 +195,7 @@ namespace LevelEditor
215195

216196
u16 prevCmd, prevName;
217197
history_getPrevCmdAndName(prevCmd, prevName);
198+
// Cannot combine commands if new textures are added to the level.
218199
if (prevCmd == LCmd_Sector_Attrib_Snapshot && texList.list.empty() && prevName == name && !idsChanged)
219200
{
220201
history_removeLast();
@@ -226,16 +207,8 @@ namespace LevelEditor
226207
if (s_workBuffer[0].empty()) { return; }
227208

228209
const u32 uncompressedSize = (u32)s_workBuffer[0].size();
229-
u32 compressedSize = 0;
230-
if (zstd_compress(s_workBuffer[1], s_workBuffer[0].data(), uncompressedSize, 4))
231-
{
232-
compressedSize = (u32)s_workBuffer[1].size();
233-
}
234-
// ERROR
235-
if (!compressedSize)
236-
{
237-
return;
238-
}
210+
const u32 compressedSize = compressBuffer();
211+
if (!compressedSize) { return; }
239212

240213
CMD_BEGIN(LCmd_Sector_Attrib_Snapshot, name);
241214
{
@@ -256,16 +229,8 @@ namespace LevelEditor
256229
if (s_workBuffer[0].empty()) { return; }
257230

258231
const u32 uncompressedSize = (u32)s_workBuffer[0].size();
259-
u32 compressedSize = 0;
260-
if (zstd_compress(s_workBuffer[1], s_workBuffer[0].data(), uncompressedSize, 4))
261-
{
262-
compressedSize = (u32)s_workBuffer[1].size();
263-
}
264-
// ERROR
265-
if (!compressedSize)
266-
{
267-
return;
268-
}
232+
const u32 compressedSize = compressBuffer();
233+
if (!compressedSize) { return; }
269234

270235
CMD_BEGIN(LCmd_Set_Textures, name);
271236
{
@@ -343,4 +308,22 @@ namespace LevelEditor
343308
level_unpackFeatureTextureSnapshot(uncompressedSize, s_workBuffer[0].data());
344309
}
345310
}
311+
312+
//////////////////////////////////////////////////
313+
// Internal
314+
//////////////////////////////////////////////////
315+
u32 compressBuffer()
316+
{
317+
u32 compressedSize = 0;
318+
if (zstd_compress(s_workBuffer[1], s_workBuffer[0].data(), (u32)s_workBuffer[0].size(), 4))
319+
{
320+
compressedSize = (u32)s_workBuffer[1].size();
321+
}
322+
if (!compressedSize)
323+
{
324+
LE_ERROR("Snapshot compression failed.");
325+
return 0;
326+
}
327+
return compressedSize;
328+
}
346329
}

0 commit comments

Comments
 (0)