Free Hero Mesh

Check-in [69c81d7254]
Login
This is a mirror of the main repository for Free Hero Mesh. New tickets and changes will not be accepted at this mirror.
Overview
Comment:Implement loading level titles (and level version and level code)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 69c81d7254f771782ea537ddf907decb0477afb6
User & Date: user on 2018-08-19 21:53:45
Other Links: manifest | tags
Context
2018-08-24
15:12
Add OF_BIZARRO flag, change OF_TRACEIN/OF_TRACEOUT to CF_TRACEIN/CF_TRACEOUT, fix a bug in update logic for CLASSES vtab check-in: 966114bc2f user: user tags: trunk
2018-08-19
21:53
Implement loading level titles (and level version and level code) check-in: 69c81d7254 user: user tags: trunk
2018-08-18
01:44
Implement level loading; also several bug fixes needed for it to work properly. check-in: 126d717829 user: user tags: trunk
Changes

Modified class.c from [435ac101e6] to [a15a0e55c6].

197
198
199
200
201
202
203
204

205
206
207
208
209
210
211
197
198
199
200
201
202
203

204
205
206
207
208
209
210
211







-
+







          n<<=4;
          c=fgetc(classfp);
          if(c>='0' && c<='9') n|=c-'0';
          else if(c>='A' && c<='F') n|=c+10-'A';
          else if(c>='a' && c<='f') n|=c+10-'a';
          else ParseError("Invalid string escape: \\x%X%c\n",n>>4,c);
          if(n<32) tokenstr[i++]=31;
          tokenstr[i++]=n;
          tokenstr[i++]=n?:255;
          break;
        default: ParseError("Invalid string escape: \\%c\n",c);
      }
    } else if(c=='"') {
      if(isimg) ParseError("Unterminated \\i escape in string literal\n");
      tokenstr[i]=0;
      return;

Modified function.c from [37ed16a163] to [b08b25d50c].

91
92
93
94
95
96
97




















































98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113































































114
115
116
117
118
119
120
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
















+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+








static void fn_cvalue(sqlite3_context*cxt,int argc,sqlite3_value**argv) {
  int a;
  if(sqlite3_value_type(*argv)==SQLITE_NULL) return;
  a=sqlite3_value_int(*argv)&0xFFFF;
  sqlite3_result_int64(cxt,a|((sqlite3_int64)TY_CLASS<<32));
}

static void fn_heromesh_escape(sqlite3_context*cxt,int argc,sqlite3_value**argv) {
  const unsigned char*u=sqlite3_value_blob(*argv);
  int un=sqlite3_value_bytes(*argv);
  char*e;
  int en=0;
  int i=0;
  int c;
  int isimg=0;
  if(!u) return;
  if(!un) {
    sqlite3_result_zeroblob(cxt,0);
    return;
  }
  e=sqlite3_malloc((un<<2)+1);
  if(!e) {
    sqlite3_result_error_nomem(cxt);
    return;
  }
  while(i<un) switch(c=u[i++]) {
    case 1 ... 8: e[en++]='\\'; e[en++]=c+'0'-1; break;
    case 10: e[en++]='\\'; e[en++]='n'; break;
    case 11: e[en++]='\\'; e[en++]='l'; break;
    case 12: e[en++]='\\'; e[en++]='c'; break;
    case 14: e[en++]='\\'; e[en++]='i'; isimg=1; break;
    case 15: e[en++]='\\'; e[en++]='b'; break;
    case 16: e[en++]='\\'; e[en++]='q'; break;
    case 31:
      if(i==un) break;
      c=u[i++];
      e[en++]='\\';
      e[en++]='x';
      e[en++]=(c>>4)<10?(c>>4)+'0':(c>>4)+'A'-10;
      e[en++]=c<10?c+'0':c+'A'-10;
      break;
    case 32 ... 127:
      if(c=='\\') {
        if(!isimg) e[en++]=c;
        isimg=0;
      } else if(c=='"') {
        e[en++]='\\';
      }
      e[en++]=c;
      break;
    default:
      e[en++]='\\';
      e[en++]='x';
      e[en++]=(c>>4)<10?(c>>4)+'0':(c>>4)+'A'-10;
      e[en++]=c<10?c+'0':c+'A'-10;
  }
  sqlite3_result_text(cxt,sqlite3_realloc(e,en+1)?:e,en,sqlite3_free);
}

static void fn_heromesh_type(sqlite3_context*cxt,int argc,sqlite3_value**argv) {
  static const char*const n[16]={
    [TY_NUMBER]="number",
    [TY_CLASS]="class",
    [TY_MESSAGE]="message",
    [TY_LEVELSTRING]="string",
    [TY_STRING]="string",
    [TY_SOUND]="sound",
    [TY_USOUND]="sound",
  };
  int i;
  if(sqlite3_value_type(*argv)!=SQLITE_INTEGER) return;
  i=sqlite3_value_int64(*argv)>>32;
  sqlite3_result_text(cxt,i<0||i>TY_MAXTYPE?"object":n[i]?:"???",-1,SQLITE_STATIC);
}

static void fn_heromesh_unescape(sqlite3_context*cxt,int argc,sqlite3_value**argv) {
  const unsigned char*e=sqlite3_value_text(*argv);
  int en=sqlite3_value_bytes(*argv);
  char*u;
  int un=0;
  int c,n;
  int isimg=0;
  if(!e) return;
  if(!*e) {
    sqlite3_result_text(cxt,"",0,SQLITE_STATIC);
    return;
  }
  u=sqlite3_malloc(en+1);
  if(!u) {
    sqlite3_result_error_nomem(cxt);
    return;
  }
  while(c=*e++) {
    if(c=='\\') {
      if(isimg) {
        u[un++]=c;
        isimg=0;
      } else switch(c=*e++) {
        case '0' ... '7': u[un++]=c-'0'+1; break;
        case 'b': u[un++]=15; break;
        case 'c': u[un++]=12; break;
        case 'i': u[un++]=14; isimg=1; break;
        case 'l': u[un++]=11; break;
        case 'n': u[un++]=10; break;
        case 'q': u[un++]=16; break;
        case 'x':
          c=*e++;
          if(c>='0' && c<='9') n=c-'0';
          else if(c>='A' && c<='F') n=c+10-'A';
          else if(c>='a' && c<='f') n=c+10-'a';
          else break;
          n<<=4;
          c=*e++;
          if(c>='0' && c<='9') n|=c-'0';
          else if(c>='A' && c<='F') n|=c+10-'A';
          else if(c>='a' && c<='f') n|=c+10-'a';
          else break;
          if(n<32) u[un++]=31;
          u[un++]=n?:255;
          break;
        default: u[un++]=c;
      }
    } else {
      u[un++]=c;
    }
  }
  done:
  sqlite3_result_blob(cxt,u,un,sqlite3_free);
}

static void fn_level(sqlite3_context*cxt,int argc,sqlite3_value**argv) {
  sqlite3_result_int(cxt,*(Uint16*)sqlite3_user_data(cxt));
}

static void fn_level_title(sqlite3_context*cxt,int argc,sqlite3_value**argv) {
  if(level_title) sqlite3_result_blob(cxt,level_title,strlen(level_title),SQLITE_TRANSIENT);
}

static void fn_load_level(sqlite3_context*cxt,int argc,sqlite3_value**argv) {
  const char*s;
  if(!main_options['x']) {
    sqlite3_result_error(cxt,"LOAD_LEVEL function requires -x switch",-1);
    return;
  }
749
750
751
752
753
754
755

756


757


758
759
760
761
762
763
764
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884







+

+
+

+
+







  .xUpdate=vt1_objects_update,
);

void init_sql_functions(sqlite3_int64*ptr0,sqlite3_int64*ptr1) {
  sqlite3_create_function(userdb,"BASENAME",0,SQLITE_UTF8|SQLITE_DETERMINISTIC,0,fn_basename,0,0);
  sqlite3_create_function(userdb,"CLASS_DATA",2,SQLITE_UTF8|SQLITE_DETERMINISTIC,0,fn_class_data,0,0);
  sqlite3_create_function(userdb,"CVALUE",1,SQLITE_UTF8|SQLITE_DETERMINISTIC,0,fn_cvalue,0,0);
  sqlite3_create_function(userdb,"HEROMESH_ESCAPE",1,SQLITE_UTF8|SQLITE_DETERMINISTIC,0,fn_heromesh_escape,0,0);
  sqlite3_create_function(userdb,"HEROMESH_TYPE",1,SQLITE_UTF8|SQLITE_DETERMINISTIC,0,fn_heromesh_type,0,0);
  sqlite3_create_function(userdb,"HEROMESH_UNESCAPE",1,SQLITE_UTF8|SQLITE_DETERMINISTIC,0,fn_heromesh_unescape,0,0);
  sqlite3_create_function(userdb,"LEVEL",0,SQLITE_UTF8,&level_ord,fn_level,0,0);
  sqlite3_create_function(userdb,"LEVEL_CACHEID",0,SQLITE_UTF8|SQLITE_DETERMINISTIC,ptr0,fn_cacheid,0,0);
  sqlite3_create_function(userdb,"LEVEL_ID",0,SQLITE_UTF8,&level_id,fn_level,0,0);
  sqlite3_create_function(userdb,"LEVEL_TITLE",0,SQLITE_UTF8,0,fn_level_title,0,0);
  sqlite3_create_function(userdb,"LOAD_LEVEL",1,SQLITE_UTF8,0,fn_load_level,0,0);
  sqlite3_create_function(userdb,"MODSTATE",0,SQLITE_UTF8,0,fn_modstate,0,0);
  sqlite3_create_function(userdb,"MVALUE",1,SQLITE_UTF8|SQLITE_DETERMINISTIC,0,fn_mvalue,0,0);
  sqlite3_create_function(userdb,"NVALUE",1,SQLITE_UTF8|SQLITE_DETERMINISTIC,0,fn_zero_extend,0,0);
  sqlite3_create_function(userdb,"OVALUE",1,SQLITE_UTF8,0,fn_ovalue,0,0);
  sqlite3_create_function(userdb,"PFHEIGHT",0,SQLITE_UTF8,&pfheight,fn_pfsize,0,0);
  sqlite3_create_function(userdb,"PFWIDTH",0,SQLITE_UTF8,&pfwidth,fn_pfsize,0,0);

Modified heromesh.h from [bec75427a1] to [2c25e1e23b].

40
41
42
43
44
45
46




47
48
49
50
51
52
53
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57







+
+
+
+








extern sqlite3*userdb;
extern xrm_db*resourcedb;
extern const char*basefilename;
extern xrm_quark optionquery[16];
extern char main_options[128];
extern Uint8 message_trace[0x4100/8];
extern Uint16 level_id,level_ord,level_version,level_code;
extern unsigned char*level_title;
extern Uint16*level_index;
extern int level_nindex;

#ifdef __GNUC__
extern char stack_protect_mode;
extern void*stack_protect_mark;
extern void*stack_protect_low;
extern void*stack_protect_high;
#define StackProtection() (stack_protect_mode && ( \

Modified main.c from [f785d8c807] to [3f8604fa56].

39
40
41
42
43
44
45




46
47
48
49
50
51
52
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56







+
+
+
+








sqlite3*userdb;
xrm_db*resourcedb;
const char*basefilename;
xrm_quark optionquery[16];
char main_options[128];
Uint8 message_trace[0x4100/8];
Uint16 level_id,level_ord,level_version,level_code;
unsigned char*level_title;
Uint16*level_index;
int level_nindex;

#ifdef __GNUC__
char stack_protect_mode=0;
void*stack_protect_mark;
void*stack_protect_low;
void*stack_protect_high;
#endif
185
186
187
188
189
190
191


192
193


194

195
196
197
198
199



200
201
202
203
204
205
206
189
190
191
192
193
194
195
196
197
198
199
200
201

202
203
204
205
206

207
208
209
210
211
212
213
214
215
216







+
+


+
+
-
+




-
+
+
+







  unsigned char*p=buf;
  unsigned char*end=buf+sz;
  int i,n,x,y,z;
  Uint32 o;
  Uint32 mru[2];
  if(lvl<0) return "Invalid level ID";
  if(!buf) return "Cannot find level";
  free(level_title);
  level_title=0;
  annihilate();
  generation_number=TY_MAXTYPE+1;
  level_version=p[0]|(p[1]<<8);
  level_code=p[2]|(p[3]<<8);
  p+=4; // skip level version and level code for now
  p+=4;
  pfwidth=(*p++&63)+1;
  pfheight=(*p++&63)+1;
  while(*p && p<end) p++; // skip text for now
  p++; // skip null terminator
  if(p==end) goto bad1;
  if(p>=end) goto bad1;
  level_title=strdup(buf+6);
  if(!level_title) fatal("Allocation failed\n");
  x=0;
  y=1;
  n=0;
  mru[0]=mru[1]=VOIDLINK;
  for(;;) {
    if(n) {
      o=objalloc(objects[*mru]->class);
283
284
285
286
287
288
289

290
291
292
293
294
295
296
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307







+







        n=0;
      }
    }
  }
  // skip level strings for now
  if(p>end) goto bad1;
  free(buf);
  level_id=lvl;
  return 0;
bad1:
  free(buf);
  return "Corrupted level data";
bad2:
  free(buf);
  return "Object out of bounds";