/*****************************************************************************
** MKSTUBS - Marty 1996
** A stub generator for the special event handler.
** This little file will read the zone files, and create a special stub file
** which then can be used to program your special events for the objects
** Also makes an objtable and mobtable for all them.
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include "files.h"
#include "types.h"
/****************************************************************************
** Message shown when the files can't be found.\n
***************************************************************************/
void usage(void)
{ printf("MKSTUBS - pDirt Special events stubs generator 1996 Marty.\n"
"This file will read the file ../DATA/WORLD/files and generate stubs for all \n"
"zones. Or you can specify the zone name and it will only generate stubs\n"
"for that zone.\n");
}
/****************************************************************************
** Small utility routine to make the table files
****************************************************************************/
void uppercase(char *str)
{ int i=0;
while (str[i] != '\0')
{ str[i] = toupper(str[i]);
i++;
}
}
/*****************************************************************************
** Create the stubs and objtable & mobtable for a specified zone.
** f is the opened zonefile, str is the zonename, and the last two are
** filepointers to the objtable and mobile table file.
****************************************************************************/
void makestubsfor(FILE *f,char *str,FILE *objtable,FILE *mobtable)
{ FILE *stubfilemob = NULL ,*stubfileobj = NULL;
char line[100];
char id[10],name[30];
char fn[80];
char upcase_zone[80];
char upcase_name[30];
int mobs = 0, objs = 0;
Boolean obj = False;
/* Copy zone name, and make it uppercase */
strcpy(upcase_zone,str);
uppercase(upcase_zone);
fgets(line,100,f);
while (!feof(f))
{
sscanf(line," %s = %s",id,name); /* Scan line */
if (strcmp(id,"%objects") == 0)
{
/* We are in an object section, so i think it is save to assume that
there will be objects in this file, so lets open the obj stub file
*/
sprintf(fn,"%s.obj.stubs",str);
stubfileobj = fopen(fn,"w+");
/* print a header */
fprintf(stubfileobj,"\
/*****************************************************************************\n\
** Automatically created stubfile for all mobiles in zone %s. \n\
** Do not edit this file, mkstub by Marty 1996\n\
*****************************************************************************/\n",
str);
obj = True;
}
else if (strcmp(id,"%mobiles") == 0)
{ /* Same idea as the object section above */
sprintf(fn,"%s.mob.stubs",str);
stubfilemob = fopen(fn,"w+");
fprintf(stubfilemob,"\
/*****************************************************************************\n\
** Automatically created stubfile for all mobiles in zone %s. \n\
** Do not edit this file, mkstub by Marty 1996\n\
*****************************************************************************/\n",
str);
obj = False;
}
/* If the line was a start of a new object or mobile, then the id
* would have been either 'Name' or 'name', so we make a stub
*/
if (strcmp(id,"Name") == 0 || strcmp (id,"name") == 0)
{
fprintf((obj ? stubfileobj : stubfilemob),"\
EVENT_HANDLER(%s_%s_%s)\n\
{ switch (event)\n\
default: break;\n\
}\n\
}\n\n",(obj ? "obj" : "mob"), str,name);
/* Also add it to the table */
strcpy(upcase_name,name);
uppercase(upcase_name);
if (obj)
fprintf(objtable," { OBJ_%s_%s\t\t, obj_%s_%s}, \n",
upcase_zone,upcase_name,str,name);
else
fprintf(mobtable," { MOB_%s_%s\t\t,mob_%s_%s}, \n",
upcase_zone,upcase_name,str,name);
if (obj)
objs++;
else
mobs++;
}
fgets(line,100,f);
}
/* Done reading file, so report what we did, and close the files */
printf(" %d mobiles and %d objects\n",mobs,objs);
if (stubfilemob != NULL) fclose(stubfilemob);
if (stubfileobj != NULL) fclose(stubfileobj);
/* Check if the file contains some usefull stuff, otherwise
delete it.
*/
if (objs == 0)
{ sprintf(fn,"%s.obj.stubs",str);
unlink(fn);
}
if (mobs == 0)
{ sprintf(fn,"%s.mob.stubs",str);
unlink(fn);
}
}
/* Dump header part of the table file */
void makemobtablefile(FILE *fp)
{ fprintf(fp,"\
#ifndef _MOBTABLE_H_\n\
#define _MOBTABLE_H_\n\n\
MAPPING(mob_table) = \n\
{\n");
}
/* Dump header part of the objtable file */
void makeobjtablefile(FILE *fp)
{ /* Write headers */
fprintf(fp,"\
#ifndef _OBJTABLE_H_\n\
#define _OBJTABLE_H_\n\n\
MAPPING(obj_table) = \n\
{\n");
}
/****************************************************************************
** Open objtable and mobtable file, and read the '../data/WORLD/files' file
** which hold all the zone names.
** If we can open the zone file, then make stubs for that zone.
***************************************************************************/
void readzones(FILE *fp)
{ FILE *f;
FILE *objtable;
FILE *mobtable;
char zn[80];
char fn[120];
objtable = fopen("objtable.h","w+");
mobtable = fopen("mobtable.h","w+");
makeobjtablefile(objtable);
makemobtablefile(mobtable);
fgets(zn,80,fp);
while (!feof(fp))
{ /* Strip \n or \r from the buffer */
if (zn[strlen(zn)-1] == '\n' || zn[strlen(zn)-1] == '\r')
zn[strlen(zn)-1] = '\0';
/* Try to open the file */
sprintf(fn,"%s/WORLD/%s.zone",DATA_DIR,zn);
f = fopen(fn,"r");
if (f == NULL)
{ printf("Could not find %s\n",fn);
fgets(zn,80,fp);
continue;
}
/* File is open, so lets make stubs */
printf("Creating stubs for %s",zn);
makestubsfor(f,zn,objtable,mobtable);
fclose(f);
fgets(zn,80,fp);
}
/* Nicely close the objtable and mobtable */
fprintf(objtable,"\
{ OBJ_VOID_VOID , NULL}\n\
}\n\n\
#endif\n");
fprintf(mobtable,"\
}\n\n\
#endif\n");
fclose(mobtable);
fclose(objtable);
}
void main(int argc,char **argv)
{ FILE *filesfp;
if (argc == 1)
{
filesfp = fopen(DATA_DIR"WORLD/files","r");
if (filesfp == NULL)
{ printf("Error opening 'files'\n");
}
else
{ readzones(filesfp);
printf("Done");
}
fclose(filesfp);
}
else if (argc == 2)
{ char fn[80];
FILE *of, *mf;
sprintf(fn,"%s/WORLD/%s.zone",DATA_DIR,argv[1]);
filesfp = fopen(fn,"r");
if (filesfp == NULL)
{ usage();
printf("Could not open %s\n",fn);
return;
}
printf("Generating stubs for %s: ",argv[1]);
of = fopen("objtable.h","w+");
mf = fopen("mobtable.h","w+");
makeobjtablefile(of);
makemobtablefile(mf);
makestubsfor(filesfp,argv[1],of,mf);
fprintf(of,"\n}\n\n#endif\n");
fprintf(mf,"\n}\n\n#endif\n");
fclose(of);
fclose(mf);
fclose(filesfp);
}
else usage();
}