Skip to content

Commit

Permalink
Downgrade to c++17, fix build script
Browse files Browse the repository at this point in the history
  • Loading branch information
nekiro committed Nov 7, 2024
1 parent b6baab5 commit c7ca715
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 55 deletions.
2 changes: 1 addition & 1 deletion ProtobufLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
<PrecompiledHeaderFile>definitions.h</PrecompiledHeaderFile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<DisableSpecificWarnings>4251;4244</DisableSpecificWarnings>
<LanguageStandard>stdcpplatest</LanguageStandard>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down
12 changes: 3 additions & 9 deletions build-lib.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# build.ps1

# Set paths
$SolutionPath = ".\ProtobufLib.sln"
$OutputDir = ".\output"
$IncludeDir = "$OutputDir\include"
$LibDir = "$OutputDir\lib"

# Create output directories
Remove-Item -Recurse -Force $OutputDir
New-Item -ItemType Directory -Force -Path $OutputDir
New-Item -ItemType Directory -Force -Path $IncludeDir
New-Item -ItemType Directory -Force -Path "$LibDir\x86"
Expand All @@ -18,11 +18,6 @@ $Configs = @(
@{Platform="x64"; Config="Release"}
)

$HeaderFiles = @(
"src\spriteappearances.h",
"src\appearances.h"
)

foreach ($cfg in $Configs) {
# Build using MSBuild
& "C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe" `
Expand All @@ -32,9 +27,8 @@ foreach ($cfg in $Configs) {
/t:Rebuild `
/m

foreach ($file in $HeaderFiles) {
Copy-Item $file -Destination $IncludeDir -Force
}
# Copy headers
Copy-Item "src\*.h" -Destination $IncludeDir -Force

# Copy libs based on platform
if ($cfg.Platform -eq "x86") {
Expand Down
92 changes: 64 additions & 28 deletions src/appearances.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,74 @@ using TibiaAppearance = tibia::protobuf::appearances::Appearance;
namespace nekiro_proto
{

/**
* @enum ObjectType
* @brief Enum representing different types of objects.
*/
enum ObjectType {
OBJECT_TYPE_ITEM = 0,
OBJECT_TYPE_LOOKTYPE = 1,
OBJECT_TYPE_EFFECT = 2,
OBJECT_TYPE_MISSILE = 3,
OBJECT_TYPE_ITEM = 0, /**< Represents an item object type. */
OBJECT_TYPE_LOOKTYPE = 1, /**< Represents a looktype object type. */
OBJECT_TYPE_EFFECT = 2, /**< Represents an effect object type. */
OBJECT_TYPE_MISSILE = 3, /**< Represents a missile object type. */
};

/**
* @class Appearances
* @brief Class for handling appearances in the Tibia game.
*/
class EXPORT Appearances {
public:
Appearances() {};
Appearances(const std::string& path) {
parseAppearancesFromFile(path);
}
Appearances(std::stringstream& input) {
parseAppearancesFromMemory(input);
}

void parseAppearancesFromFile(const std::string& path);
void parseAppearancesFromMemory(std::stringstream& input);

const std::vector<TibiaAppearance>& getAppearances(ObjectType type) {
if (!isLoaded) {
throw std::exception("Load appearances first");
}

return appearances[type];
}

private:
std::array<std::vector<TibiaAppearance>, OBJECT_TYPE_MISSILE + 1> appearances;

bool isLoaded = false;
public:
/**
* @brief Default constructor.
*/
Appearances() = default;

/**
* @brief Constructor that initializes appearances from a file.
* @param path Path to the file containing appearances data.
*/
Appearances(const std::string& path) {
parseAppearancesFromFile(path);
}

/**
* @brief Constructor that initializes appearances from memory.
* @param input Stringstream containing appearances data.
*/
Appearances(std::stringstream& input) {
parseAppearancesFromMemory(input);
}

/**
* @brief Parses appearances data from a file.
* @param path Path to the file containing appearances data.
*/
void parseAppearancesFromFile(const std::string& path);

/**
* @brief Parses appearances data from memory.
* @param input Stringstream containing appearances data.
*/
void parseAppearancesFromMemory(std::stringstream& input);

/**
* @brief Gets the appearances of a specific object type.
* @param type The type of object.
* @return A vector of appearances of the specified object type.
* @throws std::exception if appearances are not loaded.
*/
const std::vector<TibiaAppearance>& getAppearances(ObjectType type) {
if (!isLoaded) {
throw std::exception("Load appearances first");
}

return appearances[type];
}

private:
std::array<std::vector<TibiaAppearance>, OBJECT_TYPE_MISSILE + 1> appearances; /**< Array of vectors storing appearances for each object type. */

bool isLoaded = false; /**< Flag indicating whether appearances are loaded. */
};

}
Expand Down
1 change: 0 additions & 1 deletion src/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include <fstream>
#include <array>
#include <exception>
#include <format>
#include <map>

#endif
18 changes: 13 additions & 5 deletions src/spriteappearances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ namespace nekiro_proto
void SpriteAppearances::loadSpriteSheets(const std::string& dir, bool loadData /* true*/)
{
if (!fs::is_directory(dir)) {
throw std::exception(std::format("Given directory isn't directory. ({})", dir).c_str());
std::stringstream ss;
ss << "Given directory isn't directory. (" << dir << ")";
throw std::exception(ss.str().c_str());
}

fs::path catalogPath = fs::path(dir) / fs::path("catalog-content.json");
if (!fs::exists(catalogPath)) {
throw std::exception(std::format("catalog-content.json is not present in given directory. ({})", catalogPath.string()).c_str());
std::stringstream ss;
ss << "catalog-content.json is not present in given directory. (" << catalogPath.string() << ")";
throw std::exception(ss.str().c_str());
}

std::ifstream file(catalogPath, std::ios::in);
Expand Down Expand Up @@ -69,7 +73,7 @@ void SpriteAppearances::loadSpriteSheets(const std::string& dir, bool loadData /
}
}

void SpriteAppearances::loadSpriteSheet(SpriteSheetPtr sheet)
void SpriteAppearances::loadSpriteSheet(const SpriteSheetPtr& sheet)
{
if (sheet->loaded) {
return;
Expand Down Expand Up @@ -126,7 +130,9 @@ void SpriteAppearances::loadSpriteSheet(SpriteSheetPtr sheet)

lzma_ret ret = lzma_raw_decoder(&stream, filters);
if (ret != LZMA_OK) {
throw std::exception(std::format("failed to initialize lzma raw decoder result: {}", static_cast<int>(ret)).c_str());
std::stringstream ss;
ss << "failed to initialize lzma raw decoder result: " << static_cast<int>(ret);
throw std::exception(ss.str().c_str());
}

std::unique_ptr<uint8_t[]> decompressed = std::make_unique<uint8_t[]>(LZMA_UNCOMPRESSED_SIZE); // uncompressed size, bmp file + 122 bytes header
Expand All @@ -138,7 +144,9 @@ void SpriteAppearances::loadSpriteSheet(SpriteSheetPtr sheet)

ret = lzma_code(&stream, LZMA_RUN);
if (ret != LZMA_STREAM_END) {
throw std::exception(std::format("failed to decode lzma buffer result: {}", static_cast<int>(ret)).c_str());
std::stringstream ss;
ss << "failed to decode lzma buffer result: " << static_cast<int>(ret);
throw std::exception(ss.str().c_str());
}

lzma_end(&stream); // free memory
Expand Down
62 changes: 53 additions & 9 deletions src/spriteappearances.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ struct EXPORT Sprite {
SpriteSize size;
};

class EXPORT SpriteSheet {
class EXPORT SpriteSheet
{
public:
SpriteSheet(int firstId, int lastId, SpriteLayout spriteLayout, const std::string& path) : firstId(firstId), lastId(lastId), spriteLayout(spriteLayout), path(path) {}

Expand Down Expand Up @@ -110,22 +111,65 @@ using SpriteSheetPtr = std::shared_ptr<SpriteSheet>;
using BmpImgPtr = std::shared_ptr<BmpImg>;
using SpritePtr = std::shared_ptr<Sprite>;

class EXPORT SpriteAppearances {
public:
// sheets
void loadSpriteSheets(const std::string& dir, bool loadData = true);
void loadSpriteSheet(SpriteSheetPtr sheet);
class EXPORT SpriteAppearances
{
public:
/**
* @brief Loads all sprite sheets from the specified directory.
*
* @param dir The directory containing the sprite sheets.
* @param loadData If true, loads the data of the sprite sheets.
*/
void loadSpriteSheets(const std::string& dir, bool loadData = true);

/**
* @brief Loads a single sprite sheet.
*
* @param sheet The sprite sheet to load.
*/
void loadSpriteSheet(const SpriteSheetPtr& sheet);

/**
* @brief Retrieves the sprite sheet containing the specified sprite ID.
*
* @param id The ID of the sprite.
* @param load If true, loads the sprite sheet if not already loaded.
* @return SpriteSheetPtr The sprite sheet containing the sprite ID.
*/
SpriteSheetPtr getSheetBySpriteId(int id, bool load = true);

// sprites
/**
* @brief Exports the image of the specified sprite to a file.
*
* @param id The ID of the sprite.
* @param path The file path to save the sprite image.
*/
void exportSpriteImage(int id, const std::string& path);

/**
* @brief Retrieves the sprite with the specified ID.
*
* @param id The ID of the sprite.
* @return SpritePtr The sprite with the specified ID.
*/
SpritePtr getSprite(int id);

int getSpritesCount() {
/**
* @brief Gets the total number of sprites.
*
* @return int The total number of sprites.
*/
int getSpritesCount() const {
return spritesCount;
}

private:
private:
/**
* @brief Retrieves the image of the specified sprite.
*
* @param id The ID of the sprite.
* @return BmpImgPtr The image of the specified sprite.
*/
BmpImgPtr getSpriteImage(int id);

int spritesCount = 0;
Expand Down
3 changes: 1 addition & 2 deletions vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
"dependencies": ["protobuf", "nlohmann-json", "liblzma"],
"builtin-baseline": "2c401863dd54a640aeb26ed736c55489c079323b"
"dependencies": ["protobuf", "nlohmann-json", "liblzma"]
}

0 comments on commit c7ca715

Please sign in to comment.