#include #include #include #include #include #include #include #include #include "libsearch4j.h" using namespace std; //globals HBITMAP g_splash = NULL; //the main splash bitmap char step[10] = "TMP"; //the current step char progress[4] = "0"; //progress in the current step HWND splashWnd; HINSTANCE mainInstance; int mainCmdShow; bool window_loaded = false; void set_splash_step( char* new_step ) { strcpy( step, new_step ); InvalidateRect(splashWnd, NULL, FALSE); UpdateWindow(splashWnd); } void set_splash_progress( int percent_progress ) { percent_progress = ( percent_progress / 10 ) * 10; char p[4] = {0}; strcpy( progress, itoa( percent_progress, p, 10 ) ); InvalidateRect(splashWnd, NULL, FALSE); //RedrawWindow(splashWnd, NULL, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ERASENOW | RDW_UPDATENOW | RDW_INTERNALPAINT); UpdateWindow(splashWnd); } void spoof_progress( int interval ) { int tenPercentWait = interval / 11; for ( int i=0; i <= 100; i+=10 ) { set_splash_progress( i ); Sleep( tenPercentWait ); } } //extracts a resource in chunks int extractResource( const char * basename, const char * type, char * file, int no_chunks ) { HMODULE hModule = GetModuleHandle(NULL); set_splash_progress( 0 ); for ( int i=0; i 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return 0; } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { const int SPOOF_TIME = 1000; string command; srand( time(0) ); mainInstance = hInstance; mainCmdShow = nCmdShow; //start the window HANDLE hThread; DWORD dwThreadId, dwThrdParam = 1; hThread = CreateThread( NULL, // no security attributes 0, // use default stack size ProcessWindow, // thread function &dwThrdParam, // argument to thread function 0, // use default creation flags &dwThreadId); // returns the thread identifier if ( hThread == NULL ) { return -1; //ErrorExit( "CreateThread failed." ); } CloseHandle( hThread ); while ( !window_loaded ) { Sleep( 100 ); } //create the temp folder and change to it set_splash_step( "TMP" ); spoof_progress( SPOOF_TIME ); char* tmp = getenv( "TMP" ); char id[6]; for( int i=0; i<5; i++ ) id[i] = (char)((rand()%26) + 97); id[5] = '\0'; //id for this instance string tempdir = ""; tempdir.append( tmp ); tempdir.append( "\\Greenstone3-" ); tempdir.append( id ); _mkdir( tempdir.c_str() ); SetCurrentDirectory( tempdir.c_str() ); //find java //set_splash_status( hInstance, statusWnd, "STATUS_SEARCHING4J", 1000 ); set_splash_step( "SEARCHING" ); spoof_progress( SPOOF_TIME ); bool use_minimum = true; Jvm minimum; minimum.setVersionFromString( "@java.min.version@" ); string hint = ""; bool verbose = true; Jvm foundJvm; bool jvmFound = find( foundJvm, use_minimum, minimum, hint, verbose ); //if the jvm was not found, try to fix it and find it if ( !jvmFound ) { //did not find a good java string message = "Greenstone requires java @java.min.version@ or greater, but "; //tell them if java is absent or just too old if ( find( foundJvm, false, minimum, "", false ) ) { message.append( "your java is too old."); } else { message.append( "java could not be found on your computer." ); } message.append( "\n\n" ); //is this an installer with the bundled JRE? set_splash_step( "XJAVA" ); int extract_result = extractResource( "JAVA", "EXE", "@java.installer@", 2 ); if ( extract_result == 0 ) { //yes, JRE is bundled message.append( "This installer is bundled with a suitible version of java (bundled.version.java)\n"); message.append( "The installer program for this java will now be launched.\n" ); string title = "Must install java first"; MessageBox(NULL, message.c_str(), title.c_str(), MB_OK); ShellExecute(NULL, "open", "@java.installer@", NULL, NULL, SW_SHOWNORMAL); jvmFound = true; //assume the java installation went well } else { //no, JRE is not bundled set_splash_step( "SEARCHING" ); set_splash_progress( 100 ); //we are done searching message.append( "Please install java (@java.min.version@ or newer) and try again.\n" ); message.append( "Or, download a Greentsone3 installer with bundled java and use that instead of this one" ); string title = "Installation Failed: Couldn't find java"; MessageBox(NULL, message.c_str(), title.c_str(), MB_OK); } } //if we have found it by now, launch the installer if ( jvmFound ) { //extract the jar string jarLocation = ""; jarLocation.append( tempdir ); jarLocation.append( "\\greenstone3.jar" ); //set_splash_status( hInstance, statusWnd, "STATUS_JAR", 0 ); set_splash_step( "XJAR" ); extractResource( "JAR", "JAR", (char*) jarLocation.c_str(), 12 ); //launch the jar set_splash_step( "LAUNCHING" ); spoof_progress( SPOOF_TIME ); string cmd = "\""; cmd.append( foundJvm.getWinExecutable() ); cmd.append( "\" -jar greenstone3.jar" ); //hide splash screen ShowWindow(splashWnd, SW_HIDE); //run the jar int launch_exit_code = process( cmd, true ); //report how it went if ( launch_exit_code != 0 ) { string title = "Installation Error"; string message = "The installation exited with an error. Java may not be installed properly, or the installation may have been interrupted partway through. Please try again."; MessageBox(NULL, message.c_str(), title.c_str(), MB_OK); } } //clean up the temp directory _unlink("greenstone3.jar"); _unlink("ant.install.log"); _unlink("@java.installer@"); SetCurrentDirectory(".."); _rmdir( tempdir.c_str() ); return 0; }