project_files/frontlib/model/weapon.c
changeset 7177 bf6cf4dd847a
child 7179 f84805e6df03
equal deleted inserted replaced
7175:038e3415100a 7177:bf6cf4dd847a
       
     1 #include "weapon.h"
       
     2 
       
     3 #include "../ini/inihelper.h"
       
     4 #include "../iniparser/iniparser.h"
       
     5 #include "../logging.h"
       
     6 #include "../util.h"
       
     7 
       
     8 #include <stdlib.h>
       
     9 #include <ctype.h>
       
    10 
       
    11 int set_field(char field[WEAPONS_COUNT+1], const char *line, bool no9) {
       
    12 	// Validate the new string
       
    13 	for(int i=0; i<WEAPONS_COUNT && line[i]; i++) {
       
    14 		if(line[i] < '0' || line[i] > '9' || (no9 && line[i] == '9')) {
       
    15 			flib_log_e("Invalid character in weapon config string \"%.*s\", position %i", WEAPONS_COUNT, line, i);
       
    16 			return -1;
       
    17 		}
       
    18 	}
       
    19 
       
    20 	bool lineEnded = false;
       
    21 	for(int i=0; i<WEAPONS_COUNT; i++) {
       
    22 		if(!lineEnded && !line[i]) {
       
    23 			flib_log_w("Incomplete weapon config line \"%s\", filling with zeroes.", line);
       
    24 			lineEnded = true;
       
    25 		}
       
    26 		if(lineEnded) {
       
    27 			field[i] = '0';
       
    28 		} else {
       
    29 			field[i] = line[i];
       
    30 		}
       
    31 	}
       
    32 	field[WEAPONS_COUNT] = 0;
       
    33 	return 0;
       
    34 }
       
    35 
       
    36 static flib_weaponset *flib_weaponset_create_str(const char *name, const char *loadoutStr, const char *crateProbStr, const char *crateAmmoStr, const char *delayStr) {
       
    37 	flib_weaponset *result = NULL;
       
    38 	if(!name || !loadoutStr || !crateProbStr || !crateAmmoStr || !delayStr) {
       
    39 		flib_log_e("null parameter in flib_weaponset_create_str");
       
    40 	} else {
       
    41 		flib_weaponset *newSet = calloc(1, sizeof(flib_weaponset));
       
    42 		char *nameCopy = flib_strdupnull(name);
       
    43 		if(newSet && nameCopy) {
       
    44 			newSet->name = nameCopy;
       
    45 			nameCopy = NULL;
       
    46 			bool error = false;
       
    47 			error |= set_field(newSet->loadout, loadoutStr, false);
       
    48 			error |= set_field(newSet->crateprob, crateProbStr, false);
       
    49 			error |= set_field(newSet->crateammo, crateAmmoStr, false);
       
    50 			error |= set_field(newSet->delay, delayStr, false);
       
    51 			if(!error) {
       
    52 				result = newSet;
       
    53 				newSet = NULL;
       
    54 			}
       
    55 		}
       
    56 		free(nameCopy);
       
    57 		flib_weaponset_destroy(newSet);
       
    58 	}
       
    59 	return result;
       
    60 }
       
    61 
       
    62 void flib_weaponset_destroy(flib_weaponset *cfg) {
       
    63 	if(cfg) {
       
    64 		free(cfg->name);
       
    65 		free(cfg);
       
    66 	}
       
    67 }
       
    68 
       
    69 flib_weaponset *flib_weaponset_create(const char *name) {
       
    70 	return flib_weaponset_create_str(name, AMMOLINE_DEFAULT_QT, AMMOLINE_DEFAULT_PROB, AMMOLINE_DEFAULT_CRATE, AMMOLINE_DEFAULT_DELAY);
       
    71 }
       
    72 
       
    73 flib_weaponset *flib_weaponset_from_ini(const char *filename) {
       
    74 	flib_weaponset *result = NULL;
       
    75 	if(!filename) {
       
    76 		flib_log_e("null parameter in flib_weaponset_from_ini");
       
    77 	} else {
       
    78 		dictionary *settingfile = iniparser_load(filename);
       
    79 		if(!settingfile) {
       
    80 			flib_log_e("Error loading weapon scheme file %s", filename);
       
    81 		} else {
       
    82 			bool error = false;
       
    83 			char *name = inihelper_getstring(settingfile, &error, "weaponset", "name");
       
    84 			char *loadout = inihelper_getstring(settingfile, &error, "weaponset", "loadout");
       
    85 			char *crateprob = inihelper_getstring(settingfile, &error, "weaponset", "crateprob");
       
    86 			char *crateammo = inihelper_getstring(settingfile, &error, "weaponset", "crateammo");
       
    87 			char *delay = inihelper_getstring(settingfile, &error, "weaponset", "delay");
       
    88 			if(error) {
       
    89 				flib_log_e("Missing key in weapon scheme file %s", filename);
       
    90 			} else {
       
    91 				result = flib_weaponset_create_str(name, loadout, crateprob, crateammo, delay);
       
    92 			}
       
    93 		}
       
    94 		iniparser_freedict(settingfile);
       
    95 	}
       
    96 	return result;
       
    97 }
       
    98 
       
    99 int flib_weaponset_to_ini(const char *filename, const flib_weaponset *set) {
       
   100 	int result = -1;
       
   101 	if(!filename || !set) {
       
   102 		flib_log_e("null parameter in flib_weaponset_to_ini");
       
   103 	} else {
       
   104 		dictionary *dict = dictionary_new(0);
       
   105 		if(dict) {
       
   106 			bool error = false;
       
   107 			// Add the sections
       
   108 			error |= iniparser_set(dict, "weaponset", NULL);
       
   109 
       
   110 			// Add the values
       
   111 			error |= inihelper_setstr(dict, "weaponset", "name", set->name);
       
   112 			error |= inihelper_setstr(dict, "weaponset", "loadout", set->loadout);
       
   113 			error |= inihelper_setstr(dict, "weaponset", "crateprob", set->crateprob);
       
   114 			error |= inihelper_setstr(dict, "weaponset", "crateammo", set->crateammo);
       
   115 			error |= inihelper_setstr(dict, "weaponset", "delay", set->delay);
       
   116 			if(!error) {
       
   117 				FILE *inifile = fopen(filename, "wb");
       
   118 				if(inifile) {
       
   119 					iniparser_dump_ini(dict, inifile);
       
   120 					fclose(inifile);
       
   121 					result = 0;
       
   122 				}
       
   123 			}
       
   124 			dictionary_del(dict);
       
   125 		}
       
   126 	}
       
   127 	return result;
       
   128 }