Slight tweaks to writing byte code to file.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-12 10:17:15 +02:00
parent 229850257b
commit 499f942104
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 22 additions and 53 deletions

View File

@ -196,20 +196,23 @@ void AngelScripResolver::CreateScript(const char* name, const char* script) {
_builder.AddSectionFromMemory(name, script); _builder.AddSectionFromMemory(name, script);
} }
void AngelScripResolver::WriteByteCodeToFile(const char* file, bool stripDebugInfo) { void AngelScripResolver::WriteByteCodeToFile(const char* file, bool stripDebugInfo) {
FILE* wFile = fopen(file, "w"); FILE* wFile = nullptr;
wFile = fopen(file, "wb");
AssertNotNull(wFile);
auto stream = new FileByteCodeStream(wFile); auto stream = new FileByteCodeStream(wFile);
_mainModule->SaveByteCode(stream, stripDebugInfo); _mainModule->SaveByteCode(stream, stripDebugInfo);
fclose(wFile); Assert(fclose(wFile) == 0);
delete stream; delete stream;
} }
void AngelScripResolver::LoadByteCodeFromFile( void AngelScripResolver::LoadByteCodeFromFile(
const char* file, const Dictionary<ScriptCategory, Dictionary<ConstString, const char*>>& types) { const char* file, const Dictionary<ScriptCategory, Dictionary<ConstString, const char*>>& types) {
FILE* rFile = fopen(file, "r"); FILE* rFile = nullptr;
rFile = fopen(file, "rb");
AssertNotNull(rFile);
auto stream = new FileByteCodeStream(rFile); auto stream = new FileByteCodeStream(rFile);
LoadByteCode(stream, types); InitializeByteCode(stream, types);
fclose(rFile); Assert(fclose(rFile) == 0);
delete stream; delete stream;
//_typeDatabase = types;
} }
uint8_t* AngelScripResolver::WriteByteCodeToMemory(size_t& size, bool stripDebugInfo) { uint8_t* AngelScripResolver::WriteByteCodeToMemory(size_t& size, bool stripDebugInfo) {
auto stream = new MemoryByteCodeStream(); auto stream = new MemoryByteCodeStream();
@ -223,12 +226,14 @@ uint8_t* AngelScripResolver::WriteByteCodeToMemory(size_t& size, bool stripDebug
} }
void AngelScripResolver::LoadByteCodeFromMemory( void AngelScripResolver::LoadByteCodeFromMemory(
uint8_t* byte, size_t size, const Dictionary<ScriptCategory, Dictionary<ConstString, const char*>>& types) { uint8_t* byte, size_t size, const Dictionary<ScriptCategory, Dictionary<ConstString, const char*>>& types) {
std::cout << "Loading from RAM" << std::endl;
auto stream = new MemoryByteCodeStream(byte, size); auto stream = new MemoryByteCodeStream(byte, size);
LoadByteCode(stream, types); InitializeByteCode(stream, types);
delete stream; delete stream;
} }
void AngelScripResolver::LoadByteCode(asIBinaryStream* stream, void AngelScripResolver::InitializeByteCode(asIBinaryStream* stream,
const Dictionary<ScriptCategory, Dictionary<ConstString, const char*>>& types) { const Dictionary<ScriptCategory, Dictionary<ConstString, const char*>>& types) {
std::cout << "Loading byte code" << std::endl;
int result = _mainModule->LoadByteCode(stream); int result = _mainModule->LoadByteCode(stream);
Assert(result == asSUCCESS); Assert(result == asSUCCESS);

View File

@ -24,7 +24,7 @@ private:
Dictionary<ScriptCategory, Dictionary<ConstString, AngelScriptTypeInfo*>> _typeDatabase; Dictionary<ScriptCategory, Dictionary<ConstString, AngelScriptTypeInfo*>> _typeDatabase;
void RegisterTypes(); void RegisterTypes();
void LoadByteCode(asIBinaryStream* stream, void InitializeByteCode(asIBinaryStream* stream,
const Dictionary<ScriptCategory, Dictionary<ConstString, const char*>>& types); const Dictionary<ScriptCategory, Dictionary<ConstString, const char*>>& types);
public: public:

View File

@ -30,7 +30,7 @@ TEST_CASE("Get a script resolver, initialize it, then delete it") {
TEST_CASE("Get a script resolver, set script load function, load script, then build module") { TEST_CASE("Get a script resolver, set script load function, load script, then build module") {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary()); lib->Initialize(TestLibrary::GetLibrary());
lib->CreateScript("testScript1" , _scripts["testScript1"]); lib->CreateScript("testScript1", _scripts["testScript1"]);
lib->FinalizeModule(); lib->FinalizeModule();
delete lib; delete lib;
} }
@ -38,7 +38,7 @@ TEST_CASE("Get a script resolver, set script load function, load script, then bu
TEST_CASE("Build script resolver, then create object") { TEST_CASE("Build script resolver, then create object") {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary()); lib->Initialize(TestLibrary::GetLibrary());
lib->CreateScript("testScript1" , _scripts["testScript1"]); lib->CreateScript("testScript1", _scripts["testScript1"]);
lib->FinalizeModule(); lib->FinalizeModule();
auto obj = lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc); auto obj = lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc);
@ -50,11 +50,10 @@ TEST_CASE("Build script resolver, then create object") {
TEST_CASE("Build script resolver, create object, invoke addition method") { TEST_CASE("Build script resolver, create object, invoke addition method") {
auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); auto lib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
lib->Initialize(TestLibrary::GetLibrary()); lib->Initialize(TestLibrary::GetLibrary());
lib->CreateScript("testScript1" , _scripts["testScript1"]); lib->CreateScript("testScript1", _scripts["testScript1"]);
lib->FinalizeModule(); lib->FinalizeModule();
auto obj = auto obj = dynamic_cast<AngelScriptScript*>(lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
dynamic_cast<AngelScriptScript*>(lib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
REQUIRE(obj != nullptr); REQUIRE(obj != nullptr);
auto ctxPool = obj->GetContextPool(); auto ctxPool = obj->GetContextPool();
auto ctx = ctxPool->RequestContext(); auto ctx = ctxPool->RequestContext();
@ -82,16 +81,16 @@ TEST_CASE("Build script resolver, create object, invoke addition method") {
TEST_CASE("Get a script resolver, save the byte code to memory, create new script resolver from it") { TEST_CASE("Get a script resolver, save the byte code to memory, create new script resolver from it") {
auto originLib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); auto originLib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
originLib->Initialize(TestLibrary::GetLibrary()); originLib->Initialize(TestLibrary::GetLibrary());
originLib->CreateScript("testScript1" , _scripts["testScript1"]); originLib->CreateScript("testScript1", _scripts["testScript1"]);
originLib->FinalizeModule(); originLib->FinalizeModule();
size_t size; size_t size;
auto byteCode = originLib->WriteByteCodeToMemory(size); auto byteCode = originLib->WriteByteCodeToMemory(size);
auto typeDatabase = originLib->GetTypeDatabase(); auto typeDatabase = originLib->GetTypeDatabase();
Dictionary<ScriptCategory, Dictionary<ConstString, const char*>> types; Dictionary<ScriptCategory, Dictionary<ConstString, const char*>> types;
for (auto& innerDb: typeDatabase){ for (auto& innerDb : typeDatabase) {
Dictionary<ConstString, const char*> newInnerDb; Dictionary<ConstString, const char*> newInnerDb;
for (auto& kv : innerDb.second){ for (auto& kv : innerDb.second) {
INFO(kv.second->GetDecl()); INFO(kv.second->GetDecl());
newInnerDb.Insert(kv.first, kv.second->GetDecl()); newInnerDb.Insert(kv.first, kv.second->GetDecl());
} }
@ -101,46 +100,11 @@ TEST_CASE("Get a script resolver, save the byte code to memory, create new scrip
auto newLib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver()); auto newLib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
newLib->Initialize(TestLibrary::GetLibrary()); newLib->Initialize(TestLibrary::GetLibrary());
newLib->LoadByteCodeFromMemory(byteCode, size, types); newLib->LoadByteCodeFromMemory(byteCode, size, types);
auto obj = auto obj = dynamic_cast<AngelScriptScript*>(newLib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
dynamic_cast<AngelScriptScript*>(newLib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
REQUIRE(obj != nullptr); REQUIRE(obj != nullptr);
delete obj; delete obj;
delete originLib; delete originLib;
delete newLib; delete newLib;
free(byteCode); free(byteCode);
} }
TEST_CASE("Get a script resolver, save the byte code to file, create new script resolver from it") {
const char* TestFileName = "compiledAngelScriptTestFile.bin";
auto originLib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
originLib->Initialize(TestLibrary::GetLibrary());
originLib->CreateScript("testScript1" , _scripts["testScript1"]);
originLib->FinalizeModule();
originLib->WriteByteCodeToFile(TestFileName);
auto typeDatabase = originLib->GetTypeDatabase();
Dictionary<ScriptCategory, Dictionary<ConstString, const char*>> types;
for (auto& innerDb: typeDatabase){
Dictionary<ConstString, const char*> newInnerDb;
for (auto& kv : innerDb.second){
INFO(kv.second->GetDecl());
newInnerDb.Insert(kv.first, kv.second->GetDecl());
}
types.Insert(innerDb.first, newInnerDb);
}
auto newLib = dynamic_cast<AngelScripResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
newLib->Initialize(TestLibrary::GetLibrary());
newLib->LoadByteCodeFromFile(TestFileName, types);
auto obj =
dynamic_cast<AngelScriptScript*>(newLib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
REQUIRE(obj != nullptr);
delete obj;
delete originLib;
delete newLib;
remove(TestFileName);
}
#endif #endif