sourcepawn/jit/engine2.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 4188 89782d5fc91b
permissions -rw-r--r--
Update CS:GO team score gamedata.
     1 // vim: set ts=4 sw=4 tw=99 noet:
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 #include <string.h>
     5 #include "engine2.h"
     6 #include "x86/jit_x86.h"
     7 #include "zlib/zlib.h"
     8 #include "BaseRuntime.h"
     9 #include "sp_vm_engine.h"
    10 #include "watchdog_timer.h"
    11 #include <sourcemod_version.h>
    12 
    13 using namespace SourcePawn;
    14 
    15 SourcePawnEngine2::SourcePawnEngine2()
    16 {
    17 	m_Profiler = NULL;
    18 	jit_enabled_ = true;
    19 }
    20 
    21 IPluginRuntime *SourcePawnEngine2::LoadPlugin(ICompilation *co, const char *file, int *err)
    22 {
    23 	sp_file_hdr_t hdr;
    24 	uint8_t *base;
    25 	int z_result;
    26 	int error;
    27 	BaseRuntime *pRuntime;
    28 
    29 	FILE *fp = fopen(file, "rb");
    30 
    31 	if (!fp)
    32 	{
    33 		error = SP_ERROR_NOT_FOUND;
    34 		goto return_error;
    35 	}
    36 
    37 	/* Rewind for safety */
    38 	fread(&hdr, sizeof(sp_file_hdr_t), 1, fp);
    39 
    40 	if (hdr.magic != SPFILE_MAGIC)
    41 	{
    42 		error = SP_ERROR_FILE_FORMAT;
    43 		goto return_error;
    44 	}
    45 
    46 	switch (hdr.compression)
    47 	{
    48 	case SPFILE_COMPRESSION_GZ:
    49 		{
    50 			uint32_t uncompsize = hdr.imagesize - hdr.dataoffs;
    51 			uint32_t compsize = hdr.disksize - hdr.dataoffs;
    52 			uint32_t sectsize = hdr.dataoffs - sizeof(sp_file_hdr_t);
    53 			uLongf destlen = uncompsize;
    54 
    55 			char *tempbuf = (char *)malloc(compsize);
    56 			void *uncompdata = malloc(uncompsize);
    57 			void *sectheader = malloc(sectsize);
    58 
    59 			fread(sectheader, sectsize, 1, fp);
    60 			fread(tempbuf, compsize, 1, fp);
    61 
    62 			z_result = uncompress((Bytef *)uncompdata, &destlen, (Bytef *)tempbuf, compsize);
    63 			free(tempbuf);
    64 			if (z_result != Z_OK)
    65 			{
    66 				free(sectheader);
    67 				free(uncompdata);
    68 				error = SP_ERROR_DECOMPRESSOR;
    69 				goto return_error;
    70 			}
    71 
    72 			base = (uint8_t *)malloc(hdr.imagesize);
    73 			memcpy(base, &hdr, sizeof(sp_file_hdr_t));
    74 			memcpy(base + sizeof(sp_file_hdr_t), sectheader, sectsize);
    75 			free(sectheader);
    76 			memcpy(base + hdr.dataoffs, uncompdata, uncompsize);
    77 			free(uncompdata);
    78 			break;
    79 		}
    80 	case SPFILE_COMPRESSION_NONE:
    81 		{
    82 			base = (uint8_t *)malloc(hdr.imagesize);
    83 			rewind(fp);
    84 			fread(base, hdr.imagesize, 1, fp);
    85 			break;
    86 		}
    87 	default:
    88 		{
    89 			error = SP_ERROR_DECOMPRESSOR;
    90 			goto return_error;
    91 		}
    92 	}
    93 
    94 	pRuntime = new BaseRuntime();
    95 	if ((error = pRuntime->CreateFromMemory(&hdr, base)) != SP_ERROR_NONE)
    96 	{
    97 		delete pRuntime;
    98 		goto return_error;
    99 	}
   100 
   101 	size_t len;
   102 	
   103 	len = strlen(file);
   104 	for (size_t i = len - 1; i < len; i--)
   105 	{
   106 		if (file[i] == '/' 
   107 		#if defined WIN32
   108 			|| file[i] == '\\'
   109 		#endif
   110 		)
   111 		{
   112 			pRuntime->SetName(&file[i+1]);
   113 			break;
   114 		}
   115 	}
   116 
   117 	if (!pRuntime->plugin()->name)
   118 	{
   119 		pRuntime->SetName(file);
   120 	}
   121 
   122 	pRuntime->ApplyCompilationOptions(co);
   123 
   124 	fclose(fp);
   125 
   126 	return pRuntime;
   127 
   128 return_error:
   129 	*err = error;
   130 	if (fp != NULL)
   131 	{
   132     	fclose(fp);
   133 	}
   134 
   135 	return NULL;
   136 }
   137 
   138 SPVM_NATIVE_FUNC SourcePawnEngine2::CreateFakeNative(SPVM_FAKENATIVE_FUNC callback, void *pData)
   139 {
   140 	return g_Jit.CreateFakeNative(callback, pData);
   141 }
   142 
   143 void SourcePawnEngine2::DestroyFakeNative(SPVM_NATIVE_FUNC func)
   144 {
   145 	g_Jit.DestroyFakeNative(func);
   146 }
   147 
   148 const char *SourcePawnEngine2::GetEngineName()
   149 {
   150 	return "SourcePawn 1.2, jit-x86";
   151 }
   152 
   153 const char *SourcePawnEngine2::GetVersionString()
   154 {
   155 	return SOURCEMOD_VERSION;
   156 }
   157 
   158 IProfiler *SourcePawnEngine2::GetProfiler()
   159 {
   160 	return m_Profiler;
   161 }
   162 
   163 void SourcePawnEngine2::SetProfiler(IProfiler *profiler)
   164 {
   165 	m_Profiler = profiler;
   166 }
   167 
   168 IDebugListener *SourcePawnEngine2::SetDebugListener(IDebugListener *listener)
   169 {
   170 	return g_engine1.SetDebugListener(listener);
   171 }
   172 
   173 unsigned int SourcePawnEngine2::GetAPIVersion()
   174 {
   175 	return SOURCEPAWN_ENGINE2_API_VERSION;
   176 }
   177 
   178 ICompilation *SourcePawnEngine2::StartCompilation()
   179 {
   180 	return g_Jit.StartCompilation();
   181 }
   182 
   183 const char *SourcePawnEngine2::GetErrorString(int err)
   184 {
   185 	return g_engine1.GetErrorString(err);
   186 }
   187 
   188 bool SourcePawnEngine2::Initialize()
   189 {
   190 	return g_Jit.InitializeJIT();
   191 }
   192 
   193 void SourcePawnEngine2::Shutdown()
   194 {
   195 	g_WatchdogTimer.Shutdown();
   196 	g_Jit.ShutdownJIT();
   197 }
   198 
   199 IPluginRuntime *SourcePawnEngine2::CreateEmptyRuntime(const char *name, uint32_t memory)
   200 {
   201 	int err;
   202 	BaseRuntime *rt;
   203 
   204 	rt = new BaseRuntime();
   205 	if ((err = rt->CreateBlank(memory)) != SP_ERROR_NONE)
   206 	{
   207 		delete rt;
   208 		return NULL;
   209 	}
   210 
   211 	rt->SetName(name != NULL ? name : "<anonymous>");
   212 
   213 	rt->ApplyCompilationOptions(NULL);
   214 	
   215 	return rt;
   216 }
   217 
   218 bool SourcePawnEngine2::InstallWatchdogTimer(size_t timeout_ms)
   219 {
   220 	return g_WatchdogTimer.Initialize(timeout_ms);
   221 }
   222