project_files/frontlib/frontlib.c
changeset 7173 7c2eb284f9f1
parent 7171 906e72caea7b
child 7175 038e3415100a
equal deleted inserted replaced
7171:906e72caea7b 7173:7c2eb284f9f1
     1 #include "frontlib.h"
     1 #include "frontlib.h"
     2 #include "logging.h"
     2 #include "logging.h"
     3 #include "socket.h"
     3 #include "socket.h"
     4 #include "ipcconn.h"
     4 #include "ipc.h"
     5 
     5 
     6 #include <SDL.h>
     6 #include <SDL.h>
     7 #include <SDL_net.h>
     7 #include <SDL_net.h>
     8 #include <stdio.h>
     8 #include <stdio.h>
     9 #include <stdint.h>
     9 #include <stdint.h>
    10 #include <stdlib.h>
    10 #include <stdlib.h>
       
    11 #include <assert.h>
    11 
    12 
    12 static int flib_initflags;
    13 static int flib_initflags;
    13 
    14 
    14 int flib_init(int flags) {
    15 int flib_init(int flags) {
    15 	flib_initflags = flags;
    16 	flib_initflags = flags;
    37 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    38 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    38 		SDL_Quit();
    39 		SDL_Quit();
    39 	}
    40 	}
    40 }
    41 }
    41 
    42 
       
    43 static void onConfigQuery(void *context) {
       
    44 	flib_log_i("Sending config...");
       
    45 	flib_ipc ipc = (flib_ipc)context;
       
    46 	flib_ipc_send_messagestr(ipc, "TL");
       
    47 	flib_ipc_send_messagestr(ipc, "eseed loremipsum");
       
    48 	flib_ipc_send_messagestr(ipc, "escript Missions/Training/Basic_Training_-_Bazooka.lua");
       
    49 }
       
    50 
       
    51 static void onDisconnect(void *context) {
       
    52 	flib_log_i("Connection closed.");
       
    53 	flib_ipc_destroy((flib_ipc*)context);
       
    54 }
       
    55 
       
    56 static void onGameEnd(void *context, int gameEndType) {
       
    57 	switch(gameEndType) {
       
    58 	case GAME_END_FINISHED:
       
    59 		flib_log_i("Game finished.");
       
    60 		flib_constbuffer demobuf = flib_ipc_getdemo(context);
       
    61 		flib_log_i("Writing demo (%u bytes)...", demobuf.size);
       
    62 		FILE *file = fopen("testdemo.dem", "wb");
       
    63 		fwrite(demobuf.data, 1, demobuf.size, file);
       
    64 		fclose(file);
       
    65 		file = NULL;
       
    66 		break;
       
    67 	case GAME_END_HALTED:
       
    68 		flib_log_i("Game halted.");
       
    69 		break;
       
    70 	case GAME_END_INTERRUPTED:
       
    71 		flib_log_i("Game iterrupted.");
       
    72 		break;
       
    73 	}
       
    74 }
       
    75 
    42 int main(int argc, char *argv[]) {
    76 int main(int argc, char *argv[]) {
    43 	flib_init(0);
    77 	flib_init(0);
    44 
    78 
    45 	flib_ipcconn ipc = flib_ipcconn_create(true, "Medo42");
    79 	flib_ipc ipc = flib_ipc_create(true, "Medo42");
    46 	char data[256];
    80 	assert(ipc);
    47 	while(flib_ipcconn_state(ipc) != IPC_NOT_CONNECTED) {
    81 	flib_ipc_onConfigQuery(ipc, &onConfigQuery, ipc);
    48 		flib_ipcconn_tick(ipc);
    82 	flib_ipc_onDisconnect(ipc, &onDisconnect, &ipc);
    49 		int size = flib_ipcconn_recv_message(ipc, data);
    83 	flib_ipc_onGameEnd(ipc, &onGameEnd, ipc);
    50 		if(size>0) {
    84 
    51 			data[size]=0;
    85 	while(ipc) {
    52 			switch(data[0]) {
    86 		flib_ipc_tick(ipc);
    53 			case 'C':
       
    54 				flib_log_i("Sending config...");
       
    55 				flib_ipcconn_send_messagestr(ipc, "TL");
       
    56 				flib_ipcconn_send_messagestr(ipc, "eseed loremipsum");
       
    57 				flib_ipcconn_send_messagestr(ipc, "escript Missions/Training/Basic_Training_-_Bazooka.lua");
       
    58 				break;
       
    59 			case '?':
       
    60 				flib_log_i("Sending pong...");
       
    61 				flib_ipcconn_send_messagestr(ipc, "!");
       
    62 				break;
       
    63 			case 'Q':
       
    64 				flib_log_i("Game interrupted.");
       
    65 				break;
       
    66 			case 'q':
       
    67 				flib_log_i("Game finished.");
       
    68 				flib_constbuffer demobuf = flib_ipcconn_getdemo(ipc);
       
    69 				flib_log_i("Writing demo (%u bytes)...", demobuf.size);
       
    70 				FILE *file = fopen("testdemo.dem", "wb");
       
    71 				fwrite(demobuf.data, 1, demobuf.size, file);
       
    72 				fclose(file);
       
    73 				file = NULL;
       
    74 				break;
       
    75 			case 'H':
       
    76 				flib_log_i("Game halted.");
       
    77 				break;
       
    78 			}
       
    79 		}
       
    80 	}
    87 	}
    81 	flib_log_i("IPC connection lost.");
    88 	flib_log_i("Shutting down...");
    82 	flib_ipcconn_destroy(&ipc);
       
    83 	flib_quit();
    89 	flib_quit();
    84 	return 0;
    90 	return 0;
    85 }
    91 }