Skip to content

Commit

Permalink
Add SCRIPT_EXECUTABLE and SCRIPT_EXECUTABLE_PATH constants. Fix compi…
Browse files Browse the repository at this point in the history
…ler errors on Linux.
  • Loading branch information
m1maker committed Oct 9, 2024
1 parent 52a71ea commit f7ad6f2
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 68 deletions.
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ project ("NGT")
add_compile_definitions("NGT" UNICODE)
add_compile_definitions("NGTW" UNICODE)
add_compile_definitions("NGTW" NGTW)
set(CMAKE_INSTALL_PREFIX Release)


string(TIMESTAMP BUILD_TIMESTAMP "%Y-%m-%d %H:%M:%S")
Expand Down Expand Up @@ -240,16 +241,18 @@ if(WIN32)
set(NGT_SOURCES ${NGT_SOURCES} GUI/GUI.cpp)
endif()
add_executable(${PROJECT_NAME} ${NGT_SOURCES})
set_target_properties(${PROJECT_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "Release/"
install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION "Release"
)

if(WIN32)
add_executable(${PROJECT_NAME}W ${NGT_SOURCES})
add_compile_definitions(${PROJECT_NAME}W NGTW)
install(TARGETS ${PROJECT_NAME}W
RUNTIME DESTINATION "Release"
)
set_target_properties(${PROJECT_NAME}W PROPERTIES
WIN32_EXECUTABLE TRUE # This sets the subsystem to WINDOWS
RUNTIME_OUTPUT_DIRECTORY "Release/"
)
else()
add_custom_command(
Expand Down
3 changes: 3 additions & 0 deletions Release/Tests/script_executable.ngt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
void main(){
cout<<SCRIPT_EXECUTABLE;
}
2 changes: 1 addition & 1 deletion Release/Tests/sound_pool_test.ngt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../include/sound_pool"
#include "../include/sound_pool.as"
void main(){
sound_pool sp;
show_window("Test");
Expand Down
2 changes: 2 additions & 0 deletions SRC/Scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ void RegisterScripting(asIScriptEngine* engine) {
engine->RegisterObjectMethod("context", "uint64 get_return_address()const", asMETHOD(asIScriptContext, GetReturnAddress), asCALL_THISCALL);
engine->RegisterObjectMethod("context", "uint64 get_address_of_return_value()const", asMETHOD(asIScriptContext, GetAddressOfReturnValue), asCALL_THISCALL);
engine->RegisterObjectMethod("context", "string get_exception_info(bool=true)const", asFUNCTION(ContextGetExceptionInfo), asCALL_GENERIC);
engine->RegisterGlobalFunction("context@ get_active_context()property", asFUNCTION(asGetActiveContext), asCALL_CDECL);



engine->RegisterObjectType("module", sizeof(asIScriptModule), asOBJ_REF | asOBJ_NOCOUNT);
Expand Down
80 changes: 40 additions & 40 deletions SRC/datetime/datetime.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma warning(disable : 4996) //_CRT_SECURE_NO_WARNINGS
#include "datetime.h"
#include "../autowrapper/aswrappedcall.h"
#include <string.h>
#include "datetime.h"
#include <assert.h>
#include <new>
#include <string.h>

using namespace std;
using namespace std::chrono;
Expand All @@ -12,11 +12,11 @@ BEGIN_AS_NAMESPACE

// TODO: Allow setting the timezone to use

static tm time_point_to_tm(const std::chrono::time_point<std::chrono::system_clock> &tp)
static tm time_point_to_tm(const std::chrono::time_point<std::chrono::system_clock>& tp)
{
time_t t = system_clock::to_time_t(tp);
tm local;

// Use the universal timezone
#ifdef _MSC_VER
gmtime_s(&local, &t);
Expand All @@ -28,7 +28,7 @@ static tm time_point_to_tm(const std::chrono::time_point<std::chrono::system_clo
}

// Returns true if successful. Doesn't modify tp if not successful
static bool tm_to_time_point(const tm &_tm, std::chrono::time_point<std::chrono::system_clock> &tp)
static bool tm_to_time_point(const tm& _tm, std::chrono::time_point<std::chrono::system_clock>& tp)
{
tm localTm = _tm;

Expand All @@ -39,7 +39,7 @@ static bool tm_to_time_point(const tm &_tm, std::chrono::time_point<std::chrono:
time_t t = mktime(&localTm);
if (t == -1)
return false;

// Adjust the time_t since epoch with the difference of the local timezone to the universal timezone
// TODO: localtime, gmtime, and mktime are not threadsafe
t += (mktime(localtime(&t)) - mktime(gmtime(&t)));
Expand All @@ -48,15 +48,15 @@ static bool tm_to_time_point(const tm &_tm, std::chrono::time_point<std::chrono:
return true;
}

CDateTime::CDateTime() : tp(std::chrono::system_clock::now())
CDateTime::CDateTime() : tp(std::chrono::system_clock::now())
{
}

CDateTime::CDateTime(const CDateTime &o) : tp(o.tp)
CDateTime::CDateTime(const CDateTime& o) : tp(o.tp)
{
}

CDateTime &CDateTime::operator=(const CDateTime &o)
CDateTime& CDateTime::operator=(const CDateTime& o)
{
tp = o.tp;
return *this;
Expand Down Expand Up @@ -108,7 +108,7 @@ bool CDateTime::setDate(asUINT year, asUINT month, asUINT day)
std::chrono::time_point<std::chrono::system_clock> newTp;
if (!tm_to_time_point(local, newTp))
return false;

// Check if the date was actually valid
tm local2 = time_point_to_tm(newTp);

Expand Down Expand Up @@ -151,7 +151,7 @@ CDateTime::CDateTime(asUINT year, asUINT month, asUINT day, asUINT hour, asUINT
setTime(hour, minute, second);
}

asINT64 CDateTime::operator-(const CDateTime &dt) const
asINT64 CDateTime::operator-(const CDateTime& dt) const
{
return (tp - dt.tp).count() / std::chrono::system_clock::period::den * std::chrono::system_clock::period::num;
}
Expand All @@ -163,13 +163,13 @@ CDateTime CDateTime::operator+(asINT64 seconds) const
return dt;
}

CDateTime &CDateTime::operator+=(asINT64 seconds)
CDateTime& CDateTime::operator+=(asINT64 seconds)
{
tp += std::chrono::system_clock::duration(seconds * std::chrono::system_clock::period::den / std::chrono::system_clock::period::num);
return *this;
}

CDateTime operator+(asINT64 seconds, const CDateTime &other)
CDateTime operator+(asINT64 seconds, const CDateTime& other)
{
return other + seconds;
}
Expand All @@ -179,65 +179,65 @@ CDateTime CDateTime::operator-(asINT64 seconds) const
return *this + -seconds;
}

CDateTime &CDateTime::operator-=(asINT64 seconds)
CDateTime& CDateTime::operator-=(asINT64 seconds)
{
return *this += -seconds;
}

CDateTime operator-(asINT64 seconds, const CDateTime &other)
CDateTime operator-(asINT64 seconds, const CDateTime& other)
{
return other + -seconds;
}

bool CDateTime::operator==(const CDateTime &other) const
bool CDateTime::operator==(const CDateTime& other) const
{
return tp == other.tp;
}

bool CDateTime::operator<(const CDateTime &other) const
bool CDateTime::operator<(const CDateTime& other) const
{
return tp < other.tp;
}

static int opCmp(const CDateTime &a, const CDateTime &b)
static int opCmp(const CDateTime& a, const CDateTime& b)
{
if (a < b) return -1;
if (a == b) return 0;
return 1;
}

static void Construct(CDateTime *mem)
static void Construct(CDateTime* mem)
{
new(mem) CDateTime();
}

static void ConstructCopy(CDateTime *mem, const CDateTime &o)
static void ConstructCopy(CDateTime* mem, const CDateTime& o)
{
new(mem) CDateTime(o);
}

static void ConstructSet(CDateTime *mem, asUINT year, asUINT month, asUINT day, asUINT hour, asUINT minute, asUINT second)
static void ConstructSet(CDateTime* mem, asUINT year, asUINT month, asUINT day, asUINT hour, asUINT minute, asUINT second)
{
new(mem) CDateTime(year, month, day, hour, minute, second);
}

static void ConstructSet_Generic(asIScriptGeneric *gen)
static void ConstructSet_Generic(asIScriptGeneric* gen)
{
CDateTime *date = (CDateTime*)gen->GetObject();
asUINT year = *(asUINT*)gen->GetAddressOfArg(0);
asUINT month = *(asUINT*)gen->GetAddressOfArg(1);
asUINT day = *(asUINT*)gen->GetAddressOfArg(2);
asUINT hour = *(asUINT*)gen->GetAddressOfArg(3);
asUINT minute = *(asUINT*)gen->GetAddressOfArg(4);
asUINT second = *(asUINT*)gen->GetAddressOfArg(5);
ConstructSet(date, year, month, day, hour, minute, second);
CDateTime* date = (CDateTime*)gen->GetObject();
asUINT year = *(asUINT*)gen->GetAddressOfArg(0);
asUINT month = *(asUINT*)gen->GetAddressOfArg(1);
asUINT day = *(asUINT*)gen->GetAddressOfArg(2);
asUINT hour = *(asUINT*)gen->GetAddressOfArg(3);
asUINT minute = *(asUINT*)gen->GetAddressOfArg(4);
asUINT second = *(asUINT*)gen->GetAddressOfArg(5);
ConstructSet(date, year, month, day, hour, minute, second);
}

void RegisterScriptDateTime(asIScriptEngine *engine)
void RegisterScriptDateTime(asIScriptEngine* engine)
{
int r = engine->RegisterObjectType("datetime", sizeof(CDateTime), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits<CDateTime>()); assert(r >= 0);

if(strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY")==0)
if (strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY") == 0)
{
r = engine->RegisterObjectBehaviour("datetime", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(Construct), asCALL_CDECL_OBJLAST); assert(r >= 0);
r = engine->RegisterObjectBehaviour("datetime", asBEHAVE_CONSTRUCT, "void f(const datetime &in)", asFUNCTION(ConstructCopy), asCALL_CDECL_OBJFIRST); assert(r >= 0);
Expand All @@ -251,14 +251,14 @@ void RegisterScriptDateTime(asIScriptEngine *engine)
r = engine->RegisterObjectMethod("datetime", "uint get_second() const property", asMETHOD(CDateTime, getSecond), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "bool setDate(uint year, uint month, uint day)", asMETHOD(CDateTime, setDate), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "bool setTime(uint hour, uint minute, uint second)", asMETHOD(CDateTime, setTime), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "int64 opSub(const datetime &in) const", asMETHODPR(CDateTime, operator-, (const CDateTime &other) const, asINT64), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "int64 opSub(const datetime &in) const", asMETHODPR(CDateTime, operator-, (const CDateTime & other) const, asINT64), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime opAdd(int64 seconds) const", asMETHOD(CDateTime, operator+), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime opAdd_r(int64 seconds) const", asFUNCTIONPR(operator+, (asINT64 seconds, const CDateTime &other), CDateTime), asCALL_CDECL_OBJLAST); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime opAdd_r(int64 seconds) const", asFUNCTIONPR(operator+, (asINT64 seconds, const CDateTime & other), CDateTime), asCALL_CDECL_OBJLAST); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime &opAddAssign(int64 seconds)", asMETHOD(CDateTime, operator+=), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime opSub(int64 seconds) const", asMETHODPR(CDateTime, operator-, (asINT64) const, CDateTime), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime opSub_r(int64 seconds) const", asFUNCTIONPR(operator-, (asINT64 seconds, const CDateTime &other), CDateTime), asCALL_CDECL_OBJLAST); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime opSub_r(int64 seconds) const", asFUNCTIONPR(operator-, (asINT64 seconds, const CDateTime & other), CDateTime), asCALL_CDECL_OBJLAST); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime &opSubAssign(int64 seconds)", asMETHOD(CDateTime, operator-=), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "bool opEquals(const datetime &in) const", asMETHODPR(CDateTime, operator==, (const CDateTime &other) const, bool), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "bool opEquals(const datetime &in) const", asMETHODPR(CDateTime, operator==, (const CDateTime & other) const, bool), asCALL_THISCALL); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "int opCmp(const datetime &in) const", asFUNCTION(opCmp), asCALL_CDECL_OBJFIRST); assert(r >= 0);
}
else
Expand All @@ -275,14 +275,14 @@ void RegisterScriptDateTime(asIScriptEngine *engine)
r = engine->RegisterObjectMethod("datetime", "uint get_second() const property", WRAP_MFN(CDateTime, getSecond), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "bool setDate(uint year, uint month, uint day)", WRAP_MFN(CDateTime, setDate), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "bool setTime(uint hour, uint minute, uint second)", WRAP_MFN(CDateTime, setTime), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "int64 opSub(const datetime &in) const", WRAP_MFN_PR(CDateTime, operator-, (const CDateTime &other) const, asINT64), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "int64 opSub(const datetime &in) const", WRAP_MFN_PR(CDateTime, operator-, (const CDateTime & other) const, asINT64), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime opAdd(int64 seconds) const", WRAP_MFN(CDateTime, operator+), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime opAdd_r(int64 seconds) const", WRAP_OBJ_LAST_PR(operator+, (asINT64 seconds, const CDateTime &other), CDateTime), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime opAdd_r(int64 seconds) const", WRAP_OBJ_LAST_PR(operator+, (asINT64 seconds, const CDateTime & other), CDateTime), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime &opAddAssign(int64 seconds)", WRAP_MFN(CDateTime, operator+=), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime opSub(int64 seconds) const", WRAP_MFN_PR(CDateTime, operator-, (asINT64) const, CDateTime), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime opSub_r(int64 seconds) const", WRAP_OBJ_LAST_PR(operator-, (asINT64 seconds, const CDateTime &other), CDateTime), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime opSub_r(int64 seconds) const", WRAP_OBJ_LAST_PR(operator-, (asINT64 seconds, const CDateTime & other), CDateTime), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "datetime &opSubAssign(int64 seconds)", WRAP_MFN(CDateTime, operator-=), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "bool opEquals(const datetime &in) const", WRAP_MFN_PR(CDateTime, operator==, (const CDateTime &other) const, bool), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "bool opEquals(const datetime &in) const", WRAP_MFN_PR(CDateTime, operator==, (const CDateTime & other) const, bool), asCALL_GENERIC); assert(r >= 0);
r = engine->RegisterObjectMethod("datetime", "int opCmp(const datetime &in) const", WRAP_OBJ_FIRST(opCmp), asCALL_GENERIC); assert(r >= 0);
}
}
Expand Down
41 changes: 25 additions & 16 deletions SRC/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#include <Poco/Exception.h>
#define SDL_MAIN_HANDLED

#define NGT_BYTECODE_ENCRYPTION_KEY "0Z1Eif2JShwWsaAfgw1EfiOwudDAnNg6"
#define NGT_BYTECODE_ENCRYPTION_KEY "0Z1Eif2JShwWsaAfgw1EfiOwudDAnNg6WdsIuwyTgsJAiw(us)wjHdc87&6w()"

using Poco::Util::Application;
using Poco::Util::Option;
Expand All @@ -54,7 +54,7 @@ using Poco::Util::AbstractConfiguration;
using Poco::Util::OptionCallback;
using Poco::AutoPtr;


static std::string get_exe_path();
int IncludeCallback(const char* include, const char* from, CScriptBuilder* builder, void* userParam) {
// 1. Resolve the relative path
std::string absoluteIncludePath = Poco::Path(from).append("../" + std::string(include)).toString(); // Construct an absolute path
Expand All @@ -65,7 +65,7 @@ int IncludeCallback(const char* include, const char* from, CScriptBuilder* build
}

// 3. Try the 'include' directory
std::string includeDirectoryPath = Poco::Path("include", include).toString();
std::string includeDirectoryPath = Poco::Path(get_exe_path() + "/include", include).toString();
if (builder->AddSectionFromFile(includeDirectoryPath.c_str()) > -1) {
g_ScriptMessagesError = "";
return 0;
Expand Down Expand Up @@ -118,6 +118,10 @@ static void crypt(std::vector<asBYTE>& bytes) {
static std::string get_exe() {
return Poco::Util::Application::instance().config().getString("application.path");
}
static std::string get_exe_path() {
return Poco::Util::Application::instance().config().getString("application.dir");
}

static std::vector<std::string> string_split(const std::string& delim, const std::string& str)
{
std::vector<std::string> array;
Expand All @@ -141,18 +145,22 @@ static std::vector<std::string> string_split(const std::string& delim, const std
}

std::vector<std::string> defines;
void TrimWhitespace(std::string& str) {
str.erase(0, str.find_first_not_of(" \t\r\n")); // Trim leading whitespace
str.erase(str.find_last_not_of(" \t\r\n") + 1); // Trim trailing whitespace
}



int PragmaCallback(const std::string& pragmaText, CScriptBuilder& builder, void* userParam) {
const std::string definePrefix = " define ";
if (pragmaText.compare(0, definePrefix.length(), definePrefix) == 0) {
std::string word = pragmaText.substr(definePrefix.length());

word.erase(0, word.find_first_not_of(" \t\r\n"));
word.erase(word.find_last_not_of(" \t\r\n") + 1);
if (pragmaText.starts_with(definePrefix)) {
std::string word = pragmaText.substr(definePrefix.length());
TrimWhitespace(word);
defines.push_back(word);
return 0;
}

return -1;
}

Expand Down Expand Up @@ -294,14 +302,14 @@ asIScriptModule* Compile(asIScriptEngine* engine, const char* inputFile)
{
builder.SetPragmaCallback(PragmaCallback, nullptr);
builder.SetIncludeCallback(IncludeCallback, nullptr);
asIScriptModule* module = engine->GetModule("ngtgame", asGM_ALWAYS_CREATE);
int result = builder.StartNewModule(engine, "ngtgame");
asIScriptModule* module = engine->GetModule(get_exe().c_str(), asGM_ALWAYS_CREATE);
int result = builder.StartNewModule(engine, get_exe().c_str());
result = builder.AddSectionFromFile(inputFile);
if (defines.size() > 0) {
// Now, restart all, because we need to define all words before adding section
builder.ClearAll();
module = engine->GetModule("ngtgame", asGM_ALWAYS_CREATE);
result = builder.StartNewModule(engine, "ngtgame");
module = engine->GetModule(get_exe().c_str(), asGM_ALWAYS_CREATE);
result = builder.StartNewModule(engine, get_exe().c_str());
for (uint64_t i = 0; i < defines.size(); ++i) {
builder.DefineWord(defines[i].c_str());
}
Expand Down Expand Up @@ -342,7 +350,7 @@ static int Load(asIScriptEngine* engine, std::vector<asBYTE> code)
{
int r;
CBytecodeStream stream;
asIScriptModule* mod = engine->GetModule("ngtgame");
asIScriptModule* mod = engine->GetModule(get_exe().c_str());
if (mod == 0)
{
engine->WriteMessage("Product.ngt", 0, 0, asMSGTYPE_ERROR, "Failed to retrieve the compiled bytecode");
Expand Down Expand Up @@ -535,6 +543,8 @@ class NGTScripting {
scriptEngine->RegisterGlobalFunction("int exec(const string &in)", asFUNCTIONPR(ExecSystemCmd, (const string&), int), asCALL_CDECL);
scriptEngine->RegisterGlobalFunction("int exec(const string &in, string &out)", asFUNCTIONPR(ExecSystemCmd, (const string&, string&), int), asCALL_CDECL);
scriptEngine->RegisterGlobalProperty("const bool SCRIPT_COMPILED", (void*)&SCRIPT_COMPILED);
scriptEngine->RegisterGlobalFunction("string get_SCRIPT_EXECUTABLE()property", asFUNCTION(get_exe), asCALL_CDECL);
scriptEngine->RegisterGlobalFunction("string get_SCRIPT_EXECUTABLE_PATH()property", asFUNCTION(get_exe_path), asCALL_CDECL);
m_ctxMgr = new CContextMgr();
m_ctxMgr->RegisterCoRoutineSupport(scriptEngine);

Expand Down Expand Up @@ -702,7 +712,6 @@ class NGTEntry : public Application {
#ifdef _WIN32
timeEndPeriod(1);
#endif
exit_engine(0);

}

Expand Down Expand Up @@ -866,9 +875,9 @@ class NGTEntry : public Application {
void executeBytecode() {
SCRIPT_COMPILED = true;
// Execute the script
module = app->scriptEngine->GetModule("ngtgame", asGM_ALWAYS_CREATE);
module = app->scriptEngine->GetModule(get_exe().c_str(), asGM_ALWAYS_CREATE);
int result;
module = app->scriptEngine->GetModule("ngtgame");
module = app->scriptEngine->GetModule(get_exe().c_str());
if (module)
{
std::fstream read_file(this_exe.c_str(), std::ios::binary | std::ios::in);
Expand Down
Loading

0 comments on commit f7ad6f2

Please sign in to comment.