Skip to content
Merged
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
3 changes: 2 additions & 1 deletion inkcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ list(APPEND SOURCES
system.cpp
value.h value.cpp
string_table.h string_table.cpp avl_array.h
header.cpp
)
source_group(Collections REGULAR_EXPRESSION collections/.*)
add_library(inkcpp ${SOURCES})
Expand All @@ -36,4 +37,4 @@ install(TARGETS inkcpp DESTINATION lib)

# Unreal installation
install(DIRECTORY "include/" DESTINATION "inkcpp/Source/inkcpp/Public/ink/" COMPONENT unreal)
install(FILES ${SOURCES} DESTINATION "inkcpp/Source/inkcpp/Private/ink/" COMPONENT unreal)
install(FILES ${SOURCES} DESTINATION "inkcpp/Source/inkcpp/Private/ink/" COMPONENT unreal)
38 changes: 38 additions & 0 deletions inkcpp/header.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "header.h"
#include "version.h"

namespace ink::internal {

header header::parse_header(const char *data)
{
header res;
const char* ptr = data;
res.endien = *reinterpret_cast<const header::endian_types*>(ptr);
ptr += sizeof(header::endian_types);

using v_t = decltype(header::ink_version_number);
using vcpp_t = decltype(header::ink_bin_version_number);

if (res.endien == header::endian_types::same) {
res.ink_version_number =
*reinterpret_cast<const v_t*>(ptr);
ptr += sizeof(v_t);
res.ink_bin_version_number =
*reinterpret_cast<const vcpp_t*>(ptr);

} else if (res.endien == header::endian_types::differ) {
res.ink_version_number =
swap_bytes(*reinterpret_cast<const v_t*>(ptr));
ptr += sizeof(v_t);
res.ink_bin_version_number =
swap_bytes(*reinterpret_cast<const vcpp_t*>(ptr));
} else {
throw ink_exception("Failed to parse endian encoding!");
}

if (res.ink_bin_version_number != InkBinVersion) {
throw ink_exception("InkCpp-version mismatch: file was compiled with different InkCpp-version!");
}
return res;
}
}
7 changes: 6 additions & 1 deletion inkcpp/runner_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "command.h"
#include "choice.h"
#include "globals_impl.h"
#include "header.h"

namespace ink::runtime
{
Expand All @@ -23,11 +24,15 @@ namespace ink::runtime::internal
template<typename T>
inline T runner_impl::read()
{
using header = ink::internal::header;
// Sanity
inkAssert(_ptr + sizeof(T) <= _story->end(), "Unexpected EOF in Ink execution");

// Read memory
T val = *(const T*)_ptr;
if (_story->get_header().endien == header::endian_types::differ) {
val = header::swap_bytes(val);
}

// Advance ip
_ptr += sizeof(T);
Expand Down Expand Up @@ -1067,4 +1072,4 @@ namespace ink::runtime::internal
return out;
}
#endif
}
}
11 changes: 8 additions & 3 deletions inkcpp/story_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "platform.h"
#include "runner_impl.h"
#include "globals_impl.h"
#include "version.h"

#ifdef INK_ENABLE_STL
#include <iostream>
Expand All @@ -24,6 +25,7 @@ namespace ink::runtime

namespace ink::runtime::internal
{

#ifdef INK_ENABLE_STL
unsigned char* read_file_into_memory(const char* filename, size_t* read)
{
Expand Down Expand Up @@ -177,8 +179,11 @@ namespace ink::runtime::internal

void story_impl::setup_pointers()
{
// String table is after the version information
_string_table = (char*)_file + sizeof(int);
using header = ink::internal::header;
_header = header::parse_header(reinterpret_cast<char*>(_file));

// String table is after the header
_string_table = (char*)_file + header::Size;

// Pass over strings
const char* ptr = _string_table;
Expand Down Expand Up @@ -252,4 +257,4 @@ namespace ink::runtime::internal
}
}*/
}
}
}
8 changes: 7 additions & 1 deletion inkcpp/story_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
#include <config.h>
#include "types.h"
#include "story.h"
#include "header.h"

namespace ink::runtime::internal
{
// Ink story. Constant once constructed. Can be shared safely between multiple runner instances
class story_impl : public story
{
public:

#ifdef INK_ENABLE_STL
story_impl(const char* filename);
#endif
Expand All @@ -34,6 +36,8 @@ namespace ink::runtime::internal
virtual globals new_globals() override;
virtual runner new_runner(globals store = nullptr) override;


const ink::internal::header& get_header() const { return _header; }
private:
void setup_pointers();

Expand All @@ -42,6 +46,8 @@ namespace ink::runtime::internal
unsigned char* _file;
size_t _length;

ink::internal::header _header;

// string table
const char* _string_table;

Expand All @@ -63,4 +69,4 @@ namespace ink::runtime::internal
// whether we need to delete our binary data after we destruct
bool _managed;
};
}
}
10 changes: 9 additions & 1 deletion inkcpp_compiler/binary_emitter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "binary_emitter.h"

#include "header.h"
#include "version.h"

#include <vector>
#include <map>
#include <fstream>
Expand Down Expand Up @@ -173,7 +176,12 @@ namespace ink::compiler::internal
void binary_emitter::output(std::ostream& out)
{
// Write the ink version
out.write((const char*)&_ink_version, sizeof(int));
// TODO: define this order in header?
using header = ink::internal::header;
header::endian_types same = header::endian_types::same;
out.write((const char*)&same, sizeof(decltype(same)));
out.write((const char*)&_ink_version, sizeof(decltype(_ink_version)));
out.write((const char*)&ink::InkBinVersion, sizeof(decltype(ink::InkBinVersion)));

// Write the string table
_strings.write_to(out);
Expand Down
2 changes: 1 addition & 1 deletion inkcpp_compiler/emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ namespace ink::compiler::internal
// ink version
int _ink_version;
};
}
}
31 changes: 31 additions & 0 deletions shared/private/header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "system.h"

namespace ink::internal {

struct header {
static header parse_header(const char* data);

template<typename T>
static T swap_bytes(const T& value) {
char data[sizeof(T)];
for (int i = 0; i < sizeof(T); ++i) {
data[i] = reinterpret_cast<const char*>(&value)[sizeof(T)-1-i];
}
return *reinterpret_cast<const T*>(data);
}

enum class endian_types: uint16_t {
none = 0,
same = 0x0001,
differ = 0x0100
} endien = endian_types::none;
uint32_t ink_version_number = 0;
uint32_t ink_bin_version_number = 0;
static constexpr size_t Size = ///< actual data size of Header,
/// because padding of struct may
/// differ between platforms
sizeof(uint16_t) + 2 * sizeof(uint32_t);
};
}
7 changes: 7 additions & 0 deletions shared/public/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include "system.h"

namespace ink {
constexpr uint32_t InkBinVersion = 0;
};