Jump to content
  • 0
Sign in to follow this  
GmOcean

String constants

Question

How would I go about adding support for string constants?

I know that in script.c there is this code block, which deals with the creation of constants, which is called every time the server starts up.

void script_set_constant(const char* name, int value, bool isparameter) {	int n = script->add_str(name);	if( script->str_data[n].type == C_NOP ) {// new		script->str_data[n].type = isparameter ? C_PARAM : C_INT;		script->str_data[n].val  = value;	} else if( script->str_data[n].type == C_PARAM || script->str_data[n].type == C_INT ) {// existing parameter or constant		ShowError("script_set_constant: Attempted to overwrite existing %s '%s' (old value=%d, new value=%d).n", ( script->str_data[n].type == C_PARAM ) ? "parameter" : "constant", name, script->str_data[n].val, value);	} else {// existing name		ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).n", isparameter ? "parameter" : "constant", name, script->op2name(script->str_data[n].type));	}}

 

Going off of this, I'd need to edit 3 things as far as I can tell.

The first being str_data_struct in script.h

// String buffer structures.// str_data stores string informationstruct str_data_struct {	enum c_op type;	int str;	int backpatch;	int label;	bool (*func)(struct script_state *st);	int val;	int next;};

I'm assuming I need to add a const char *string  in there, but I haven't been able to get that to work.

Then second, i'd need to modify this in script.c

/*========================================== * Reading constant databases * const.txt *------------------------------------------*/void read_constdb(void) {	FILE *fp;	char line[1024],name[1024],val[1024];	int type;	sprintf(line, "%s/const.txt", map->db_path);	fp=fopen(line, "r");	if(fp==NULL) {		ShowError("can't read %sn", line);		return ;	}	while (fgets(line, sizeof(line), fp)) {		if (line[0] == '/' && line[1] == '/')			continue;		type = 0;		if (sscanf(line, "%1023[A-Za-z0-9_],%1023[-0-9xXA-Fa-f],%d", name, val, &type) >=2		 || sscanf(line, "%1023[A-Za-z0-9_] %1023[-0-9xXA-Fa-f] %d", name, val, &type) >=2		) {			script->set_constant(name, (int)strtol(val, NULL, 0), (bool)type);		}	}	fclose(fp);}

More specifically:

		if (sscanf(line, "%1023[A-Za-z0-9_],%1023[-0-9xXA-Fa-f],%d", name, val, &type) >=2		 || sscanf(line, "%1023[A-Za-z0-9_] %1023[-0-9xXA-Fa-f] %d", name, val, &type) >=2		) {			script->set_constant(name, (int)strtol(val, NULL, 0), (bool)type);		}

to represent the changes of having " before and after the value, so that it's seen as a string rather than an integer.

 

Then finally, editing script_set_constant in script.c so that, it properly stores the string value instead of an int.

 

But, I have no idea where to even begin besides this. I mean, I could be completely wrong with what I think needs to be modified, but in theory it should be possible, since we're already capable of reading strings in other dbs. This one shouldn't be any different if you break it down, it's just matter of how the src handles it :/

Edited by GmOcean

Share this post


Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.