Skip to content

Commit

Permalink
Add fopen/fread/fwrite/fclose to CoDScript
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfooman committed Mar 22, 2014
1 parent 530dcc2 commit 056abcd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
4 changes: 4 additions & 0 deletions gsc.cpp
Expand Up @@ -153,6 +153,10 @@ Scr_Function scriptFunctions[] = {
{"rundll" , gsc_utils_rundll , 0},
{"Cmd_ExecuteString" , gsc_utils_ExecuteString , 0},
{"scandir" , gsc_utils_scandir , 0},
{"fopen" , gsc_utils_fopen , 0},
{"fread" , gsc_utils_fread , 0},
{"fwrite" , gsc_utils_fwrite , 0},
{"fclose" , gsc_utils_fclose , 0},
#endif

#if COMPILE_TCC == 1
Expand Down
53 changes: 51 additions & 2 deletions gsc_utils.cpp
Expand Up @@ -3,6 +3,7 @@
#if COMPILE_UTILS == 1

#include <dirent.h> // dir stuff
#include <assert.h>

// 1.2 0x080F6D5A
int utils_hook_player_eject(int player) { // player 0 = 0x08679380 + 0x11c = 0x0867949c
Expand Down Expand Up @@ -161,7 +162,7 @@ void gsc_utils_rundll() {

void gsc_utils_ExecuteString() {
char *str;
if ( ! stackGetParams((char *)"s", &str)) {
if ( ! stackGetParams("s", &str)) {
stackPushUndefined();
return;
}
Expand All @@ -172,7 +173,7 @@ void gsc_utils_ExecuteString() {

void gsc_utils_scandir() {
char *dirname;
if ( ! stackGetParams((char *)"s", &dirname)) {
if ( ! stackGetParams("s", &dirname)) {
stackPushUndefined();
return;
}
Expand All @@ -191,4 +192,52 @@ void gsc_utils_scandir() {
closedir(dir);
}

void gsc_utils_fopen() {
char *filename, *mode;
if ( ! stackGetParams("ss", &filename, &mode)) {
stackPushUndefined();
return;
}
FILE *file = fopen(filename, mode);
stackPushInt((int)file);
}
void gsc_utils_fread() {
FILE *file;
if ( ! stackGetParams("i", &file)) {
stackPushUndefined();
return;
}
assert(file);
char buffer[256];
int ret = fread(buffer, 1, 255, file);
if ( ! ret) {
stackPushUndefined();
return;
}
buffer[ret] = '\0';
stackPushString(buffer);
}

void gsc_utils_fwrite() {
FILE *file;
char *buffer;
if ( ! stackGetParams("is", &file, &buffer)) {
stackPushUndefined();
return;
}
assert(file);
int bytesWritten = fwrite(buffer, 1, strlen(buffer), file);
stackPushInt(bytesWritten);
}

void gsc_utils_fclose() {
FILE *file;
if ( ! stackGetParams("i", &file)) {
stackPushUndefined();
return;
}
assert(file);
stackPushInt( fclose(file) );
}

#endif
5 changes: 5 additions & 0 deletions gsc_utils.hpp
Expand Up @@ -29,6 +29,11 @@ void gsc_utils_rundll();
void gsc_utils_ExecuteString();
void gsc_utils_scandir();

void gsc_utils_fopen();
void gsc_utils_fread();
void gsc_utils_fwrite();
void gsc_utils_fclose();

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 056abcd

Please sign in to comment.