int ss_Free() { SGS_CTX = g_C; /* clean the application */ if( SGS_FAILED( sgs_GlobalCall( C, "cleanup", 0, 0 ) ) ) { sgs_Msg( C, SGS_ERROR, "Failed to clean the application." ); return -1; } sgs_membuf_destroy( &g_tmpbuf, C ); if( GEnabledProfiler ) { sgs_ProfDump( C, &P ); sgs_ProfClose( C, &P ); } if( GEnabledDebugging ) sgs_CloseIDbg( C, &D ); sgs_DestroyEngine( C ); ss_FreeGraphics( C ); return 0; }
int main( int argc, char** argv ) { printf( "\n//\n/// SGScript / CPPBC test\n//\n" ); SGS_CTX = sgs_CreateEngine(); printf( "\n> push Vec3(1,2,3)" ); pushVec3( C, 1, 2, 3 ); printf( "\n> print object: " ); sgs_PushItem( C, -1 ); sgs_GlobalCall( C, "print", 1, 0 ); printf( "\n> print property 'Vec3.length': " ); sgs_PushProperty( C, -1, "length" ); sgs_GlobalCall( C, "print", 1, 0 ); printf( "\n> print result of method 'Vec3.getLength()': " ); sgs_PushItem( C, -1 ); sgs_PushProperty( C, -1, "getLength" ); sgs_ThisCall( C, 0, 1 ); sgs_GlobalCall( C, "print", 1, 0 ); printf( "\n> print object after method 'Vec3.setLength(4.5)': " ); sgs_PushItem( C, -1 ); sgs_PushReal( C, 4.5 ); sgs_PushProperty( C, -2, "setLength" ); sgs_ThisCall( C, 1, 0 ); sgs_GlobalCall( C, "print", 1, 0 ); printf( "\n" ); sgs_DestroyEngine( C ); return 0; }
char* runsgs( const char* script ) { if( !outbuf ) { outbuf = malloc( MAX_OUTPUT_SIZE ); } SGS_CTX = sgs_CreateEngine(); outbuf_at = outbuf; sgs_SetOutputFunc( C, output_to_buffer, NULL ); sgs_SetErrOutputFunc( C, output_to_buffer, NULL ); sgs_LoadLib_Fmt( C ); /* no need for I/O - can't use it from the browser */ sgs_LoadLib_Math( C ); sgs_LoadLib_OS( C ); sgs_LoadLib_RE( C ); sgs_LoadLib_String( C ); xgm_module_entry_point( C ); sgs_ExecString( C, script ); sgs_DestroyEngine( C ); *outbuf_at = 0; return outbuf; }
int main( int argc, char* argv[] ) { int i; BYTE buf[ 4096 ], *path, *data; DWORD read, written, scriptsize = 0; HANDLE fh; path = buf; /* open EXE file */ read = GetModuleFileName( NULL, (CHAR*) buf, sizeof( buf ) ); if( read >= sizeof( buf ) ) { path = malloc( read + 1 ); GetModuleFileName( NULL, (CHAR*) buf, sizeof( buf ) ); } fh = CreateFileFast( (CHAR*) buf, FILE_READ, OPEN_EXISTING ); if( path != buf ) free( path ); if( fh == INVALID_HANDLE_VALUE ) return E_FAIL; SetFilePointer( fh, -4, NULL, FILE_END ); ReadFile( fh, &scriptsize, sizeof( scriptsize ), &read, NULL ); /* read the following data */ if( scriptsize == 0 ) { if( argc == 3 ) { HANDLE hsgs, hout = CreateFileFast( argv[ 1 ], FILE_WRITE, CREATE_ALWAYS ); if( !hout ) { MessageBox( 0, "Could not open executable file for writing", APPNAME, MB_ICONERROR ); CloseHandle( fh ); return E_FAIL; } hsgs = CreateFileFast( argv[ 2 ], FILE_READ, OPEN_EXISTING ); if( !hsgs ) { MessageBox( 0, "Could not open script file for reading", APPNAME, MB_ICONERROR ); CloseHandle( hout ); CloseHandle( fh ); return E_FAIL; } SetFilePointer( fh, 0, NULL, FILE_BEGIN ); while( ReadFile( fh, buf, sizeof( buf ), &read, NULL ) && read ) WriteFile( hout, buf, read, &written, NULL ); while( ReadFile( hsgs, buf, sizeof( buf ), &read, NULL ) && read ) WriteFile( hout, buf, read, &written, NULL ); scriptsize = GetFileSize( hsgs, NULL ); WriteFile( hout, &scriptsize, 4, &written, NULL ); CloseHandle( fh ); CloseHandle( hsgs ); CloseHandle( hout ); MessageBox( 0, "File saved!", APPNAME, MB_ICONINFORMATION ); return S_OK; } else { const char* info = "To create an executable from .sgs" ", run sgsexe <output-file.exe> <script-file.sgs>." "\n\nglobal 'argv' will be the array of arguments"; MessageBox( 0, info, APPNAME, MB_ICONINFORMATION ); CloseHandle( fh ); return E_ABORT; } } data = malloc( scriptsize ); SetFilePointer( fh, -4-(LONG)scriptsize, NULL, FILE_END ); ReadFile( fh, data, scriptsize, &read, NULL ); CloseHandle( fh ); { SGS_CTX = sgs_CreateEngine(); for( i = 0; i < argc; ++i ) sgs_PushString( C, argv[ i ] ); sgs_CreateArray( C, NULL, argc ); sgs_SetGlobalByName( C, "argv", sgs_StackItem( C, -1 ) ); sgs_Pop( C, 1 ); sgs_SetGlobalByName( C, "argc", sgs_MakeInt( argc ) ); sgs_ExecBuffer( C, (char*) data, scriptsize ); sgs_DestroyEngine( C ); } free( data ); return 0; }