mirror of
https://github.com/Ylianst/MeshAgent
synced 2026-01-05 18:13:38 +00:00
Major agent update.
This commit is contained in:
@@ -30,7 +30,92 @@ limitations under the License.
|
||||
#endif
|
||||
|
||||
#define ILibDuktape_ModSearch_ModuleFile (void*)0xFF
|
||||
#define ILibDuktape_ModSearch_ModuleObject (void*)0xFE
|
||||
#define ILibDuktape_ModSearch_JSInclude "\xFF_ModSearch_JSINCLUDE"
|
||||
#define ILibDuktape_ModSearch_ModulePath "\xFF_ModSearch_Path"
|
||||
|
||||
duk_ret_t ILibDuktape_ModSearch_GetJSModule(duk_context *ctx, char *id)
|
||||
{
|
||||
ILibHashtable table = NULL;
|
||||
int idLen = (int)strnlen_s(id, 1024);
|
||||
char *retVal = NULL;
|
||||
|
||||
duk_push_heap_stash(ctx); // [stash]
|
||||
if (duk_has_prop_string(ctx, -1, "ModSearchTable"))
|
||||
{
|
||||
duk_get_prop_string(ctx, -1, "ModSearchTable"); // [stash][ptr]
|
||||
table = (ILibHashtable)duk_to_pointer(ctx, -1);
|
||||
duk_pop(ctx); // [stash]
|
||||
}
|
||||
else
|
||||
{
|
||||
table = ILibHashtable_Create();
|
||||
duk_push_pointer(ctx, table); // [stash][ptr]
|
||||
duk_put_prop_string(ctx, -2, "ModSearchTable"); // [stash]
|
||||
}
|
||||
duk_pop(ctx); // ...
|
||||
|
||||
retVal = ILibHashtable_Get(table, ILibDuktape_ModSearch_ModuleFile, id, idLen);
|
||||
if (retVal == NULL)
|
||||
{
|
||||
duk_push_heap_stash(ctx);
|
||||
char *mpath;
|
||||
duk_size_t mpathLen;
|
||||
mpath = Duktape_GetStringPropertyValueEx(ctx, -1, ILibDuktape_ModSearch_ModulePath, NULL, &mpathLen);
|
||||
duk_pop(ctx);
|
||||
|
||||
char *fileName = ILibMemory_AllocateA(idLen + 4 + mpathLen + 1);
|
||||
if (mpath == NULL)
|
||||
{
|
||||
sprintf_s(fileName, idLen + 4, "%s.js", id);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf_s(fileName, idLen + 5 + mpathLen, "%s/%s.js", mpath, id);
|
||||
}
|
||||
int dataLen = ILibReadFileFromDiskEx(&retVal, fileName);
|
||||
if (dataLen > 0) { duk_push_lstring(ctx, retVal, dataLen); free(retVal); }
|
||||
else
|
||||
{
|
||||
return(0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
duk_push_string(ctx, retVal);
|
||||
}
|
||||
return(1);
|
||||
}
|
||||
void ILibDuktape_ModSearch_AddModuleObject(duk_context *ctx, char *id, void *heapptr)
|
||||
{
|
||||
int idLen = (int)strnlen_s(id, 1024);
|
||||
ILibHashtable table = NULL;
|
||||
|
||||
duk_push_heap_stash(ctx); // [stash]
|
||||
if (duk_has_prop_string(ctx, -1, "ModSearchTable"))
|
||||
{
|
||||
duk_get_prop_string(ctx, -1, "ModSearchTable"); // [stash][ptr]
|
||||
table = (ILibHashtable)duk_to_pointer(ctx, -1);
|
||||
duk_pop(ctx); // [stash]
|
||||
}
|
||||
else
|
||||
{
|
||||
table = ILibHashtable_Create();
|
||||
duk_push_pointer(ctx, table); // [stash][ptr]
|
||||
duk_put_prop_string(ctx, -2, "ModSearchTable"); // [stash]
|
||||
}
|
||||
|
||||
duk_push_heapptr(ctx, heapptr);
|
||||
duk_put_prop_string(ctx, -2, Duktape_GetStashKey(heapptr));
|
||||
heapptr = ILibHashtable_Put(table, ILibDuktape_ModSearch_ModuleObject, id, idLen, heapptr);
|
||||
|
||||
if (heapptr != NULL)
|
||||
{
|
||||
// Remove the old object that was in the table
|
||||
duk_del_prop_string(ctx, -1, Duktape_GetStashKey(heapptr));
|
||||
}
|
||||
duk_pop(ctx); // ...
|
||||
}
|
||||
int ILibDuktape_ModSearch_AddModule(duk_context *ctx, char *id, char *module, int moduleLen)
|
||||
{
|
||||
ILibHashtable table = NULL;
|
||||
@@ -89,8 +174,20 @@ duk_ret_t mod_Search_Files(duk_context *ctx, char* id)
|
||||
char fileName[255];
|
||||
char *data;
|
||||
int dataLen;
|
||||
char *mpath = NULL;
|
||||
|
||||
sprintf_s(fileName, sizeof(fileName), "%s.js", id);
|
||||
duk_push_heap_stash(ctx);
|
||||
mpath = Duktape_GetStringPropertyValue(ctx, -1, ILibDuktape_ModSearch_ModulePath, NULL);
|
||||
duk_pop(ctx);
|
||||
|
||||
if (mpath == NULL)
|
||||
{
|
||||
sprintf_s(fileName, sizeof(fileName), "%s.js", id);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf_s(fileName, sizeof(fileName), "%s/%s.js", mpath, id);
|
||||
}
|
||||
dataLen = ILibReadFileFromDiskEx(&data, fileName);
|
||||
if (dataLen > 0)
|
||||
{
|
||||
@@ -118,6 +215,13 @@ duk_ret_t mod_Search_Files(duk_context *ctx, char* id)
|
||||
return DUK_RET_ERROR;
|
||||
}
|
||||
}
|
||||
void ILibDuktape_ModSearch_AddHandler_AlsoIncludeJS(duk_context *ctx, char *js, size_t jsLen)
|
||||
{
|
||||
duk_push_heap_stash(ctx); // [stash]
|
||||
duk_push_lstring(ctx, js, jsLen); // [stash][str]
|
||||
duk_put_prop_string(ctx, -2, ILibDuktape_ModSearch_JSInclude); // [stash]
|
||||
duk_pop(ctx); // ...
|
||||
}
|
||||
|
||||
duk_ret_t mod_Search(duk_context *ctx)
|
||||
{
|
||||
@@ -128,6 +232,7 @@ duk_ret_t mod_Search(duk_context *ctx)
|
||||
void *chain;
|
||||
ILibSimpleDataStore mDS = NULL;
|
||||
char *module;
|
||||
void *j;
|
||||
|
||||
if (!duk_is_string(ctx, 0)) { return ILibDuktape_Error(ctx, "mod_search(): Invalid 'ID' parameter"); }
|
||||
id = (char*)duk_get_lstring(ctx, 0, &idLen);
|
||||
@@ -142,8 +247,17 @@ duk_ret_t mod_Search(duk_context *ctx)
|
||||
duk_push_heap_stash(ctx);
|
||||
duk_get_prop_string(ctx, -1, "ModSearchTable");
|
||||
table = (ILibHashtable)duk_to_pointer(ctx, -1);
|
||||
func = (ILibDuktape_ModSearch_PUSH_Object)ILibHashtable_Get(table, NULL, id, (int)idLen);
|
||||
|
||||
// First check if there is a JS override
|
||||
j = ILibHashtable_Get(table, ILibDuktape_ModSearch_ModuleObject, id, (int)idLen);
|
||||
if (j != NULL)
|
||||
{
|
||||
duk_push_heapptr(ctx, j);
|
||||
duk_put_prop_string(ctx, 3, "exports");
|
||||
return(0);
|
||||
}
|
||||
|
||||
func = (ILibDuktape_ModSearch_PUSH_Object)ILibHashtable_Get(table, NULL, id, (int)idLen);
|
||||
if (func == NULL)
|
||||
{
|
||||
if ((module = (char*)ILibHashtable_Get(table, ILibDuktape_ModSearch_ModuleFile, id, (int)idLen)) != NULL)
|
||||
@@ -173,18 +287,30 @@ duk_ret_t mod_Search(duk_context *ctx)
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf_s(key, sizeof(key), "Module: %s (NOT FOUND in DB)", id);
|
||||
duk_push_string(ctx, key);
|
||||
duk_throw(ctx);
|
||||
return DUK_RET_ERROR;
|
||||
return mod_Search_Files(ctx, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Init this temp value, to detect if the module wants to add JS code
|
||||
duk_push_heap_stash(ctx);
|
||||
duk_del_prop_string(ctx, -1, ILibDuktape_ModSearch_JSInclude);
|
||||
duk_pop(ctx);
|
||||
|
||||
func(ctx, chain);
|
||||
duk_put_prop_string(ctx, 3, "exports");
|
||||
return 0;
|
||||
|
||||
duk_push_heap_stash(ctx);
|
||||
if (duk_has_prop_string(ctx, -1, ILibDuktape_ModSearch_JSInclude))
|
||||
{
|
||||
duk_get_prop_string(ctx, -1, ILibDuktape_ModSearch_JSInclude);
|
||||
return(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
void ILibDuktape_ModSearch_Destroy(duk_context *ctx, void *user)
|
||||
@@ -202,6 +328,23 @@ void ILibDuktape_ModSearch_Destroy(duk_context *ctx, void *user)
|
||||
duk_pop(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
duk_ret_t ILibDuktape_ModSearch_setModulePath(duk_context *ctx)
|
||||
{
|
||||
if (duk_is_string(ctx, 0))
|
||||
{
|
||||
duk_push_heap_stash(ctx);
|
||||
duk_dup(ctx, 0);
|
||||
duk_put_prop_string(ctx, -2, ILibDuktape_ModSearch_ModulePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
return(ILibDuktape_Error(ctx, "Invalid Path"));
|
||||
}
|
||||
|
||||
return(0);
|
||||
}
|
||||
|
||||
void ILibDuktape_ModSearch_Init(duk_context * ctx, void * chain, ILibSimpleDataStore mDB)
|
||||
{
|
||||
duk_module_duktape_init(ctx);
|
||||
@@ -225,5 +368,10 @@ void ILibDuktape_ModSearch_Init(duk_context * ctx, void * chain, ILibSimpleDataS
|
||||
duk_put_prop_string(ctx, -2, "modSearch"); // [globalString]
|
||||
duk_pop(ctx); // ...
|
||||
|
||||
duk_push_global_object(ctx); // [g]
|
||||
ILibDuktape_CreateInstanceMethod(ctx, "setModulePath", ILibDuktape_ModSearch_setModulePath, 1);
|
||||
duk_pop(ctx); // ...
|
||||
|
||||
|
||||
ILibDuktape_Helper_AddHeapFinalizer(ctx, ILibDuktape_ModSearch_Destroy, NULL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user