Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Core/GameEngine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ set(GAMEENGINE_SRC
# Include/Common/GameType.h
Include/Common/GameUtility.h
# Include/Common/Geometry.h
# Include/Common/JSONChunkInput.h
# Include/Common/JSONChunkOutput.h
# Include/Common/GlobalData.h
# Include/Common/Handicap.h
# Include/Common/IgnorePreferences.h
Expand Down Expand Up @@ -646,6 +648,8 @@ set(GAMEENGINE_SRC
# Source/Common/System/CriticalSection.cpp
# Source/Common/System/DataChunk.cpp
Source/Common/System/Debug.cpp
# Source/Common/System/JSONChunkInput.cpp
# Source/Common/System/JSONChunkOutput.cpp
# Source/Common/System/Directory.cpp
# Source/Common/System/DisabledTypes.cpp
# Source/Common/System/encrypt.cpp
Expand Down Expand Up @@ -1145,6 +1149,15 @@ set(GAMEENGINE_SRC
# Source/Precompiled/PreRTS.cpp
)

if(NOT IS_VS6_BUILD)
list(APPEND GAMEENGINE_SRC
${CMAKE_CURRENT_SOURCE_DIR}/Include/Common/JSONChunkInput.h
${CMAKE_CURRENT_SOURCE_DIR}/Include/Common/JSONChunkOutput.h
${CMAKE_CURRENT_SOURCE_DIR}/Source/Common/System/JSONChunkInput.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Source/Common/System/JSONChunkOutput.cpp
)
endif()

if(RTS_GAMEMEMORY_ENABLE)
# Uses the original Game Memory implementation.
list(APPEND GAMEENGINE_SRC
Expand All @@ -1164,6 +1177,21 @@ else()
endif()


if (NOT IS_VS6_BUILD)
find_package(nlohmann_json CONFIG)
if (TARGET nlohmann_json::nlohmann_json)
# Uses nlohmann-json from vcpkg
set(NLOHMANN_JSON_TARGET nlohmann_json::nlohmann_json)
else()
# Uses nlohmann-json from github repository
include(${CMAKE_SOURCE_DIR}/cmake/nlohmann-json.cmake)
set(NLOHMANN_JSON_TARGET nlohmann_json)
endif()
else()
# VC6 doesn't support C++11, so nlohmann-json is not available
set(NLOHMANN_JSON_TARGET "")
endif()

add_library(corei_gameengine_private INTERFACE)
add_library(corei_gameengine_public INTERFACE)

Expand All @@ -1179,6 +1207,7 @@ target_link_libraries(corei_gameengine_private INTERFACE

target_compile_definitions(corei_gameengine_private INTERFACE
IG_DEBUG_STACKTRACE
$<$<NOT:$<BOOL:${IS_VS6_BUILD}>>:RTS_HAS_JSON_CHUNK>
)

target_precompile_headers(corei_gameengine_private INTERFACE
Expand All @@ -1198,4 +1227,5 @@ target_link_libraries(corei_gameengine_public INTERFACE
d3d8lib
gamespy::gamespy
stlport
$<$<NOT:$<BOOL:${IS_VS6_BUILD}>>:${NLOHMANN_JSON_TARGET}>
)
115 changes: 115 additions & 0 deletions Core/GameEngine/Include/Common/JSONChunkInput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
** Command & Conquer Generals(tm)
** Copyright 2025 TheSuperHackers
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#ifdef RTS_HAS_JSON_CHUNK

#include "Common/GameMemory.h"
#include "Common/Dict.h"
#include <nlohmann/json.hpp>

typedef unsigned short DataChunkVersionType;

class JSONChunkInput;

struct JSONChunkInfo
{
AsciiString label;
AsciiString parentLabel;
DataChunkVersionType version;
Int dataSize;
};

typedef Bool (*JSONChunkParserPtr)( JSONChunkInput &file, JSONChunkInfo *info, void *userData );

class JSONParser : public MemoryPoolObject
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(JSONParser, "JSONParser")
public:
JSONParser *next;

JSONChunkParserPtr parser;
AsciiString label;
AsciiString parentLabel;
void *userData;
};
EMPTY_DTOR(JSONParser)

class JSONInputChunk : public MemoryPoolObject
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(JSONInputChunk, "JSONInputChunk")
public:
JSONInputChunk* next;
AsciiString label;
DataChunkVersionType version;
nlohmann::json* data;
size_t dataIndex;
};
EMPTY_DTOR(JSONInputChunk)

class JSONChunkInput
{
protected:
nlohmann::json m_root;
JSONParser* m_parserList;
JSONInputChunk* m_chunkStack;
std::vector<nlohmann::json*> m_chunkArray;
size_t m_currentChunkIndex;

void clearChunkStack( void );

public:
void *m_currentObject;
void *m_userData;

public:
JSONChunkInput( const char* jsonData, size_t jsonSize );
~JSONChunkInput();

void registerParser( const AsciiString& label, const AsciiString& parentLabel, JSONChunkParserPtr parser, void *userData = NULL );

Bool parse( void *userData = NULL );

Bool isValidFileType(void);
AsciiString openDataChunk(DataChunkVersionType *ver );
void closeDataChunk( void );

Bool atEndOfFile( void );
Bool atEndOfChunk( void );

void reset( void );

AsciiString getChunkLabel( void );
DataChunkVersionType getChunkVersion( void );
unsigned int getChunkDataSize( void );
unsigned int getChunkDataSizeLeft( void );

Real readReal(void);
Int readInt(void);
Byte readByte(void);

AsciiString readAsciiString(void);
UnicodeString readUnicodeString(void);
Dict readDict(void);
void readArrayOfBytes(char *ptr, Int len);

NameKeyType readNameKey(void);
};

#endif // RTS_HAS_JSON_CHUNK
67 changes: 67 additions & 0 deletions Core/GameEngine/Include/Common/JSONChunkOutput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
** Command & Conquer Generals(tm)
** Copyright 2025 TheSuperHackers
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#ifdef RTS_HAS_JSON_CHUNK

#include "Common/GameMemory.h"
#include "Common/Dict.h"
#include <nlohmann/json.hpp>
#include <string>

typedef unsigned short DataChunkVersionType;

class JSONOutputChunk : public MemoryPoolObject
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(JSONOutputChunk, "JSONOutputChunk")
public:
JSONOutputChunk* next;
AsciiString label;
nlohmann::json* data;
};
EMPTY_DTOR(JSONOutputChunk)

class JSONChunkOutput
{
protected:
nlohmann::json m_root;
nlohmann::json m_toc;
JSONOutputChunk* m_chunkStack;
UnsignedInt m_nextID;

public:
JSONChunkOutput( void );
~JSONChunkOutput();

void openDataChunk( const char *name, DataChunkVersionType ver );
void closeDataChunk( void );

void writeReal(Real r);
void writeInt(Int i);
void writeByte(Byte b);
void writeAsciiString(const AsciiString& string);
void writeUnicodeString(UnicodeString string);
void writeArrayOfBytes(char *ptr, Int len);
void writeDict(const Dict& d);
void writeNameKey(const NameKeyType key);

std::string getJSONString( void );
};

#endif // RTS_HAS_JSON_CHUNK
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ static PoolSizeRec PoolSizes[] =
{ "Mapping", 128, 32 },
{ "OutputChunk", 32, 32 },
{ "InputChunk", 32, 32 },
{ "JSONOutputChunk", 128, 32 },
{ "JSONParser", 128, 32 },
{ "JSONInputChunk", 128, 32 },
{ "AnimateWindow", 32, 32 },
{ "GameFont", 32, 32 },
{ "NetCommandRef", 256, 32 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,9 @@ static PoolSizeRec PoolSizes[] =
{ "Mapping", 384, 64 },
{ "OutputChunk", 32, 32 },
{ "InputChunk", 32, 32 },
{ "JSONOutputChunk", 128, 32 },
{ "JSONParser", 128, 32 },
{ "JSONInputChunk", 128, 32 },
{ "AnimateWindow", 32, 32 },
{ "GameFont", 32, 32 },
{ "NetCommandRef", 256, 32 },
Expand Down
Loading
Loading