This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
437f385
commit 7d3d9c6
Showing
1 changed file
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#pragma once | ||
#include "s_functab.h" | ||
|
||
#include <istream> | ||
#include <ostream> | ||
#include <vector> | ||
#include <zlib.h> | ||
|
||
namespace storm | ||
{ | ||
namespace script_cache | ||
{ | ||
struct Define | ||
{ | ||
std::string name; | ||
uint32_t type; | ||
uintptr_t value; | ||
}; | ||
|
||
struct FunctionLocalVariable | ||
{ | ||
LocalVarInfo info; | ||
bool is_extern; | ||
}; | ||
|
||
struct Function | ||
{ | ||
FuncInfo info; | ||
std::vector<FunctionLocalVariable> arguments; | ||
std::vector<LocalVarInfo> local_variables; | ||
}; | ||
|
||
struct EventHandler | ||
{ | ||
std::string event_name; | ||
std::string function_name; | ||
}; | ||
|
||
class Reader final | ||
{ | ||
public: | ||
explicit Reader(std::string_view data) | ||
: data_(data), offset_(0) | ||
{ | ||
} | ||
|
||
template <typename T> | ||
T *ReadData() | ||
{ | ||
const auto size = sizeof(T); | ||
if (offset_ + size > data_.size()) | ||
{ | ||
return nullptr; | ||
} | ||
|
||
auto address = data_.data() + offset_; | ||
offset_ += size; | ||
return address; | ||
} | ||
|
||
std::string_view ReadBytes() | ||
{ | ||
auto size = ReadData<size_t>(); | ||
if (!size) | ||
{ | ||
return {}; | ||
} | ||
|
||
if (offset_ + *size > data_.size()) | ||
{ | ||
return {}; | ||
} | ||
|
||
const auto address = data_.data() + offset_; | ||
offset_ += *size; | ||
return {address, *size}; | ||
} | ||
|
||
private: | ||
std::string_view data_; | ||
size_t offset_; | ||
}; | ||
|
||
class Writer | ||
{ | ||
public: | ||
template <typename T> | ||
void WriteData(const T &data) | ||
{ | ||
const auto size = data_.size(); | ||
data_.resize(size + sizeof data); | ||
std::memcpy(data_.data() + size, &data, sizeof(T)); | ||
} | ||
|
||
void WriteBytes(std::string_view data) | ||
{ | ||
WriteData(data.size()); | ||
const auto size = data_.size(); | ||
data_.resize(size + data.size()); | ||
std::memcpy(data_.data() + size, data.data(), data.size()); | ||
} | ||
|
||
private: | ||
std::vector<char> data_; | ||
}; | ||
} | ||
|
||
struct ScriptCache | ||
{ | ||
std::vector<script_cache::Define> defines; | ||
std::vector<std::string> files; | ||
std::vector<script_cache::Function> functions; | ||
std::vector<VarInfo> variables; | ||
std::vector<script_cache::EventHandler> event_handlers; | ||
std::vector<char> code; | ||
}; | ||
} |