use of C++23 code
Signed-off-by: Peter Siegmund <mars3142@noreply.mars3142.dev>
This commit is contained in:
74
components/cartridge/include/cartridge/binary_reader.h
Normal file
74
components/cartridge/include/cartridge/binary_reader.h
Normal file
@@ -0,0 +1,74 @@
|
||||
// MIT License
|
||||
// Copyright (c) 2026 by Peter Siegmund (mars3142)
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <span>
|
||||
|
||||
namespace cartridge {
|
||||
|
||||
enum class SeekOrigin { Begin, Current, End };
|
||||
|
||||
enum class Endian { Little, Big };
|
||||
|
||||
class BinaryReader {
|
||||
public:
|
||||
explicit BinaryReader(std::span<const uint8_t> data,
|
||||
Endian endian = Endian::Little);
|
||||
|
||||
void seek(int offset, SeekOrigin origin);
|
||||
[[nodiscard]] uint8_t getByte();
|
||||
[[nodiscard]] int16_t getShort();
|
||||
[[nodiscard]] uint16_t getUShort();
|
||||
[[nodiscard]] int32_t getLong();
|
||||
[[nodiscard]] uint32_t getULong();
|
||||
[[nodiscard]] double getDouble();
|
||||
[[nodiscard]] std::string getASCIIZ();
|
||||
[[nodiscard]] std::span<const uint8_t> data() const { return _data; }
|
||||
|
||||
private:
|
||||
template <typename T> [[nodiscard]] T readInt(size_t size);
|
||||
[[nodiscard]] static bool isSystemLittleEndian();
|
||||
static void swapBytes(uint8_t *data, size_t size);
|
||||
|
||||
std::span<const uint8_t> _data;
|
||||
Endian _endian;
|
||||
size_t _index;
|
||||
};
|
||||
|
||||
template <typename T> T BinaryReader::readInt(const size_t size) {
|
||||
if (_index + size > _data.size())
|
||||
throw std::out_of_range("readInt out of range");
|
||||
T value = 0;
|
||||
if (_endian == Endian::Little) {
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
value |= static_cast<T>(_data[_index + i]) << (8 * i);
|
||||
}
|
||||
} else {
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
value = (value << 8) | _data[_index + i];
|
||||
}
|
||||
}
|
||||
_index += size;
|
||||
return value;
|
||||
}
|
||||
|
||||
} // namespace cartridge
|
||||
94
components/cartridge/include/cartridge/cartridge.h
Normal file
94
components/cartridge/include/cartridge/cartridge.h
Normal file
@@ -0,0 +1,94 @@
|
||||
// MIT License
|
||||
// Copyright (c) 2026 by Peter Siegmund (mars3142)
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cartridge/binary_reader.h"
|
||||
#include "cartridge/lat_lng.h"
|
||||
#include "cartridge/media.h"
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace cartridge {
|
||||
|
||||
class Cartridge {
|
||||
public:
|
||||
// Factory method to create Cartridge from BinaryReader
|
||||
static std::unique_ptr<Cartridge> create(BinaryReader &reader);
|
||||
|
||||
// Getters
|
||||
[[nodiscard]] double altitude() const;
|
||||
[[nodiscard]] const std::string &author() const;
|
||||
[[nodiscard]] const LatLng &latLng() const;
|
||||
[[nodiscard]] const std::string &cartridgeName() const;
|
||||
[[nodiscard]] const std::string &cartridgeDesc() const;
|
||||
[[nodiscard]] const std::string &cartridgeGuid() const;
|
||||
[[nodiscard]] const std::string &typeOfCartridge() const;
|
||||
[[nodiscard]] const std::string &playerName() const;
|
||||
[[nodiscard]] const std::string &startLocationDesc() const;
|
||||
[[nodiscard]] const std::string &version() const;
|
||||
[[nodiscard]] const std::string &company() const;
|
||||
[[nodiscard]] const std::string &recommendDevice() const;
|
||||
[[nodiscard]] const std::string &completionCode() const;
|
||||
|
||||
// Media methods
|
||||
std::unique_ptr<Media> getMedia(int objectId);
|
||||
[[nodiscard]] int mediaCount() const;
|
||||
std::unique_ptr<Media> splashScreen();
|
||||
std::unique_ptr<Media> smallIcon();
|
||||
std::unique_ptr<Media> luac();
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const Cartridge &other) const;
|
||||
bool operator!=(const Cartridge &other) const;
|
||||
|
||||
Cartridge(std::string cartridgeGuid, double altitude, std::string author,
|
||||
std::string cartridgeDesc, std::string cartridgeName,
|
||||
std::string company, std::string completionCode, LatLng latLng,
|
||||
std::string playerName, std::string recommendDevice,
|
||||
int smallIconId, int splashScreenId, std::string startLocationDesc,
|
||||
std::string typeOfCartridge, std::string version,
|
||||
std::map<int, int> references, std::vector<uint8_t> bytes);
|
||||
|
||||
private:
|
||||
// Fields
|
||||
std::string m_cartridgeGuid;
|
||||
double m_altitude;
|
||||
std::string m_author;
|
||||
std::string m_cartridgeDesc;
|
||||
std::string m_cartridgeName;
|
||||
std::string m_company;
|
||||
std::string m_completionCode;
|
||||
LatLng m_latLng;
|
||||
std::string m_playerName;
|
||||
std::string m_recommendDevice;
|
||||
int m_smallIconId;
|
||||
int m_splashScreenId;
|
||||
std::string m_startLocationDesc;
|
||||
std::string m_typeOfCartridge;
|
||||
std::string m_version;
|
||||
std::map<int, int> m_references;
|
||||
std::vector<uint8_t> m_bytes;
|
||||
|
||||
int m_lastObject = -1;
|
||||
std::unique_ptr<Media> m_lastMedia;
|
||||
};
|
||||
|
||||
} // namespace cartridge
|
||||
36
components/cartridge/include/cartridge/lat_lng.h
Normal file
36
components/cartridge/include/cartridge/lat_lng.h
Normal file
@@ -0,0 +1,36 @@
|
||||
// MIT License
|
||||
// Copyright (c) 2026 by Peter Siegmund (mars3142)
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace cartridge {
|
||||
class LatLng {
|
||||
double m_latitude;
|
||||
double m_longitude;
|
||||
|
||||
static std::string _format(double value, const std::string& suffix);
|
||||
|
||||
public:
|
||||
LatLng(double latitude, double longitude);
|
||||
[[nodiscard]] std::string latitude() const;
|
||||
[[nodiscard]] std::string longitude() const;
|
||||
[[nodiscard]] std::string toString() const;
|
||||
};
|
||||
}
|
||||
66
components/cartridge/include/cartridge/media.h
Normal file
66
components/cartridge/include/cartridge/media.h
Normal file
@@ -0,0 +1,66 @@
|
||||
// MIT License
|
||||
// Copyright (c) 2026 by Peter Siegmund (mars3142)
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cartridge/binary_reader.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace cartridge {
|
||||
|
||||
enum class ObjectType {
|
||||
Deleted = -1,
|
||||
Luac = 0,
|
||||
Bmp = 1,
|
||||
Png = 2,
|
||||
Jpg = 3,
|
||||
Gif = 4,
|
||||
Wav = 17,
|
||||
Mp3 = 18,
|
||||
Fdl = 19,
|
||||
Snd = 20,
|
||||
Ogg = 21,
|
||||
Swf = 33,
|
||||
Txt = 49,
|
||||
Invalid = -9999
|
||||
};
|
||||
|
||||
class Media {
|
||||
public:
|
||||
Media(std::string objectType, const std::vector<uint8_t> &data);
|
||||
|
||||
static std::unique_ptr<Media> create(BinaryReader &reader, int objectId,
|
||||
int address);
|
||||
|
||||
bool operator==(const Media &other) const;
|
||||
[[nodiscard]] std::size_t hash() const;
|
||||
|
||||
[[nodiscard]] const std::vector<uint8_t> &getData() const { return data; }
|
||||
[[nodiscard]] const std::string &getObjectType() const { return objectType; }
|
||||
|
||||
private:
|
||||
static std::vector<uint8_t> readMediaData(BinaryReader &reader);
|
||||
static std::string getObjectTypeString(int objectType);
|
||||
|
||||
std::string objectType;
|
||||
std::vector<uint8_t> data;
|
||||
};
|
||||
|
||||
} // namespace cartridge
|
||||
28
components/cartridge/include/cartridge/parser.h
Normal file
28
components/cartridge/include/cartridge/parser.h
Normal file
@@ -0,0 +1,28 @@
|
||||
// MIT License
|
||||
// Copyright (c) 2026 by Peter Siegmund (mars3142)
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "cartridge/cartridge.h"
|
||||
|
||||
namespace cartridge {
|
||||
|
||||
std::unique_ptr<Cartridge> parseData(const std::vector<uint8_t>& bytes);
|
||||
|
||||
std::unique_ptr<Cartridge> parseFile(const std::string& filePath);
|
||||
|
||||
std::unique_ptr<Cartridge> parseCartridge();
|
||||
|
||||
} // namespace cartridge
|
||||
Reference in New Issue
Block a user