2008-07-11 10:18:43 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "engine2.h"
|
|
|
|
#include "x86/jit_x86.h"
|
|
|
|
#include "jit_version.h"
|
|
|
|
#include "zlib/zlib.h"
|
|
|
|
#include "BaseRuntime.h"
|
|
|
|
#include "sp_vm_engine.h"
|
|
|
|
|
|
|
|
using namespace SourcePawn;
|
|
|
|
|
|
|
|
SourcePawnEngine2::SourcePawnEngine2()
|
|
|
|
{
|
|
|
|
m_Profiler = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPluginRuntime *SourcePawnEngine2::LoadPlugin(ICompilation *co, const char *file, int *err)
|
|
|
|
{
|
|
|
|
sp_file_hdr_t hdr;
|
|
|
|
uint8_t *base;
|
|
|
|
int z_result;
|
|
|
|
int error;
|
|
|
|
BaseRuntime *pRuntime;
|
|
|
|
|
|
|
|
FILE *fp = fopen(file, "rb");
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
error = SP_ERROR_NOT_FOUND;
|
|
|
|
goto return_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Rewind for safety */
|
|
|
|
fread(&hdr, sizeof(sp_file_hdr_t), 1, fp);
|
|
|
|
|
|
|
|
if (hdr.magic != SPFILE_MAGIC)
|
|
|
|
{
|
|
|
|
error = SP_ERROR_FILE_FORMAT;
|
|
|
|
goto return_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (hdr.compression)
|
|
|
|
{
|
|
|
|
case SPFILE_COMPRESSION_GZ:
|
|
|
|
{
|
|
|
|
uint32_t uncompsize = hdr.imagesize - hdr.dataoffs;
|
|
|
|
uint32_t compsize = hdr.disksize - hdr.dataoffs;
|
|
|
|
uint32_t sectsize = hdr.dataoffs - sizeof(sp_file_hdr_t);
|
|
|
|
uLongf destlen = uncompsize;
|
|
|
|
|
|
|
|
char *tempbuf = (char *)malloc(compsize);
|
|
|
|
void *uncompdata = malloc(uncompsize);
|
|
|
|
void *sectheader = malloc(sectsize);
|
|
|
|
|
|
|
|
fread(sectheader, sectsize, 1, fp);
|
|
|
|
fread(tempbuf, compsize, 1, fp);
|
|
|
|
|
|
|
|
z_result = uncompress((Bytef *)uncompdata, &destlen, (Bytef *)tempbuf, compsize);
|
|
|
|
free(tempbuf);
|
|
|
|
if (z_result != Z_OK)
|
|
|
|
{
|
|
|
|
free(sectheader);
|
|
|
|
free(uncompdata);
|
|
|
|
error = SP_ERROR_DECOMPRESSOR;
|
|
|
|
goto return_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
base = (uint8_t *)malloc(hdr.imagesize);
|
|
|
|
memcpy(base, &hdr, sizeof(sp_file_hdr_t));
|
|
|
|
memcpy(base + sizeof(sp_file_hdr_t), sectheader, sectsize);
|
|
|
|
free(sectheader);
|
|
|
|
memcpy(base + hdr.dataoffs, uncompdata, uncompsize);
|
|
|
|
free(uncompdata);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SPFILE_COMPRESSION_NONE:
|
|
|
|
{
|
|
|
|
base = (uint8_t *)malloc(hdr.imagesize);
|
|
|
|
rewind(fp);
|
|
|
|
fread(base, hdr.imagesize, 1, fp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
error = SP_ERROR_DECOMPRESSOR;
|
|
|
|
goto return_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-15 07:22:26 +02:00
|
|
|
pRuntime = new BaseRuntime();
|
|
|
|
if ((error = pRuntime->CreateFromMemory(&hdr, base)) != SP_ERROR_NONE)
|
2008-07-11 10:18:43 +02:00
|
|
|
{
|
2008-08-15 07:22:26 +02:00
|
|
|
delete pRuntime;
|
|
|
|
goto return_error;
|
2008-07-11 10:18:43 +02:00
|
|
|
}
|
|
|
|
|
2008-08-15 07:22:26 +02:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = strlen(file);
|
|
|
|
for (size_t i = len - 1; i >= 0 && i < len; i--)
|
2008-07-11 10:18:43 +02:00
|
|
|
{
|
2008-08-15 07:22:26 +02:00
|
|
|
if (file[i] == '/'
|
|
|
|
#if defined WIN32
|
|
|
|
|| file[i] == '\\'
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
pRuntime->m_pPlugin->name = strdup(&file[i+1]);
|
|
|
|
break;
|
|
|
|
}
|
2008-07-11 10:18:43 +02:00
|
|
|
}
|
|
|
|
|
2008-08-15 07:22:26 +02:00
|
|
|
if (pRuntime->m_pPlugin->name == NULL)
|
2008-07-11 10:18:43 +02:00
|
|
|
{
|
2008-08-15 07:22:26 +02:00
|
|
|
pRuntime->m_pPlugin->name = strdup(file);
|
2008-07-11 10:18:43 +02:00
|
|
|
}
|
|
|
|
|
2008-08-15 07:22:26 +02:00
|
|
|
pRuntime->ApplyCompilationOptions(co);
|
|
|
|
|
2008-08-28 18:59:41 +02:00
|
|
|
fclose(fp);
|
|
|
|
|
2008-07-11 10:18:43 +02:00
|
|
|
return pRuntime;
|
|
|
|
|
|
|
|
return_error:
|
|
|
|
*err = error;
|
2008-08-28 18:59:41 +02:00
|
|
|
fclose(fp);
|
2008-07-11 10:18:43 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPVM_NATIVE_FUNC SourcePawnEngine2::CreateFakeNative(SPVM_FAKENATIVE_FUNC callback, void *pData)
|
|
|
|
{
|
2008-08-15 07:22:26 +02:00
|
|
|
return g_Jit.CreateFakeNative(callback, pData);
|
2008-07-11 10:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SourcePawnEngine2::DestroyFakeNative(SPVM_NATIVE_FUNC func)
|
|
|
|
{
|
2008-08-15 07:22:26 +02:00
|
|
|
g_Jit.DestroyFakeNative(func);
|
2008-07-11 10:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *SourcePawnEngine2::GetEngineName()
|
|
|
|
{
|
|
|
|
return "SourcePawn 1.1, jit-x86";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *SourcePawnEngine2::GetVersionString()
|
|
|
|
{
|
|
|
|
return SVN_FULL_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
IProfiler *SourcePawnEngine2::GetProfiler()
|
|
|
|
{
|
|
|
|
return m_Profiler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourcePawnEngine2::SetProfiler(IProfiler *profiler)
|
|
|
|
{
|
|
|
|
m_Profiler = profiler;
|
|
|
|
}
|
|
|
|
|
|
|
|
IDebugListener *SourcePawnEngine2::SetDebugListener(IDebugListener *listener)
|
|
|
|
{
|
|
|
|
return g_engine1.SetDebugListener(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int SourcePawnEngine2::GetAPIVersion()
|
|
|
|
{
|
|
|
|
return SOURCEPAWN_ENGINE2_API_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
ICompilation *SourcePawnEngine2::StartCompilation()
|
|
|
|
{
|
2008-08-15 07:22:26 +02:00
|
|
|
return g_Jit.StartCompilation();
|
2008-07-11 10:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *SourcePawnEngine2::GetErrorString(int err)
|
|
|
|
{
|
|
|
|
return g_engine1.GetErrorString(err);
|
|
|
|
}
|
2008-08-15 07:22:26 +02:00
|
|
|
|
|
|
|
bool SourcePawnEngine2::Initialize()
|
|
|
|
{
|
|
|
|
return g_Jit.InitializeJIT();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourcePawnEngine2::Shutdown()
|
|
|
|
{
|
|
|
|
g_Jit.ShutdownJIT();
|
|
|
|
}
|