int main(int argc,char *argv[]) { extern int amx_ConsoleInit(AMX *amx); extern int amx_ConsoleCleanup(AMX *amx); extern int amx_CoreInit(AMX *amx); extern int amx_CoredCleanp(AMX *amx); size_t memsize; void *program; AMX amx; int index, err; cell amx_addr, *phys_addr; char output[128]; if (argc != 4) PrintUsage(argv[0]); if ((memsize = aux_ProgramSize(argv[1])) == 0) PrintUsage(argv[0]); program = malloc(memsize); if (program == NULL) ErrorExit(NULL, AMX_ERR_MEMORY); err = aux_LoadProgram(&amx, argv[1], program); if (err) ErrorExit(&amx, err); amx_ConsoleInit(amx); err = amx_CoreInit(amx); if (err) ErrorExit(&amx, err); err = amx_FindPublic(&amx, argv[2], &index); if (err) ErrorExit(&amx, err); err = amx_Allot(&amx, strlen(argv[3]) + 1, &amx_addr, &phys_addr); if (err) ErrorExit(&amx, err); amx_SetString(phys_addr, argv[3], 0); err = amx_Exec(&amx, NULL, index, 1, amx_addr); if (err) ErrorExit(&amx, err); amx_GetString(output, phys_addr); amx_Release(&amx, amx_addr); printf("%s returns %s\n", argv[1], output); amx_ConsoleCleanup(&amx); amx_CoreCleanup(&amx); amx_Cleanup(&amx); free(program); return 0; }
int main(int argc, char *argv[]) { size_t memsize; void *program; AMX amx; cell ret = 0; int err; if (argc != 2 || (memsize = aux_ProgramSize(argv[1])) == 0) ErrorExit("Usage: PRUN3 <filename>\n\n" "The filename must include the extension", 0); program = VirtualAlloc(NULL, memsize, MEM_RESERVE, PAGE_READWRITE); if (program == NULL) ErrorExit("Failed to reserve memory", 0); __try { err = aux_LoadProgram(&amx, argv[1], program); if (err != AMX_ERR_NONE) ErrorExit("Load error %d (invalid file format or version mismatch)", err); signal(SIGINT,sigabort); amx_CoreInit(&amx); err = aux_RegisterNatives(&amx); if (err != AMX_ERR_NONE) ErrorExit("The program uses native functions that this run-time does not provide", 0); err = amx_Exec(&amx, &ret, AMX_EXEC_MAIN); while (err == AMX_ERR_SLEEP) err = amx_Exec(&amx, &ret, AMX_EXEC_CONT); if (err != AMX_ERR_NONE) printf("Run time error %d on address %ld\n", err, amx.cip); else if (ret != 0) printf("%s returns %ld\n", argv[1], (long)ret); } __except (aux_CommitMemory(GetExceptionInformation(), program, memsize)) { /* nothing */ } /* try */ /* Decommitting memory that is not committed does not fail, so you can just * decommit all memory. Releasing memory that is partially committed fails, * even if you also include the MEM_RELEASE flag (tested on WIndows98), so * you must call VirtualFree twice in a row. */ VirtualFree(program, memsize, MEM_DECOMMIT); VirtualFree(program, 0, MEM_RELEASE); amx_CoreCleanup(&amx); return 0; }
int initialize_pawn_machine (pawn_machine *machine, const char* fname) { int err; size_t memsize; void* buffer; machine->initialized = 0; memsize = aux_ProgramSize (fname); if (memsize == 0) { log_error ("Unable to determine memory size for Pawn file %s", fname); return 0; } buffer = malloc (memsize); if (buffer == NULL) { log_error ("unable to allocate memory for Pawn file %s", fname); return 0; } err = aux_LoadProgram (&(machine->amx), fname, buffer); if (err != AMX_ERR_NONE) { free (buffer); log_error ("unable to load Pawn file %s", fname); return 0; } // The amx_*Init functions return an error when not all natives // used in the amx script are defined, so we only need to check // the last Init (when we have defined our entire library). amx_ConsoleInit (&(machine->amx)); amx_FloatInit (&(machine->amx)); amx_StringInit (&(machine->amx)); err = amx_ElInit (&(machine->amx)); if (err != AMX_ERR_NONE) { free (buffer); log_error ("Unable to initialize all native functions for Pawn file %s", fname); return 0; } machine->buf_size = memsize; machine->buffer = buffer; machine->initialized = 1; return 1; }
bool CNetwork::LoadScript(char *filename) { size_t memsize = aux_ProgramSize(filename); if(memsize == 0) { printf("Script file not found or corrupted\n"); return false; } memset((void*)&inimod_amx, 0, sizeof(AMX)); void * program = malloc(memsize); if (program == NULL) { printf("Memory allocation for script failed\n"); return false; } if(!LoadProgram(filename,program)) { printf("Loading script into Abstract Machine failed\n"); return false; } amx_CoreInit(&inimod_amx); amx_FloatInit(&inimod_amx); amx_StringInit(&inimod_amx); //amx_FileInit(&inimod_amx); amx_TimeInit(&inimod_amx); return true; }