Skip to content

Commit 2bf5a2d

Browse files
committed
Proper program loading
1 parent da76720 commit 2bf5a2d

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

src/displayapp/screens/Pawn.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,36 @@ static cell AMX_NATIVE_CALL F_sprintf(AMX* amx, const cell* params) {
211211
return ret;
212212
}
213213

214-
Pawn::Pawn() {
215-
#include "program.h"
216214

217-
uint8_t* prog = new uint8_t[program_len];
218-
memcpy(prog, program, program_len);
215+
static int load_program(AMX* amx, const uint8_t* data) {
216+
AMX_HEADER hdr;
217+
memcpy(&hdr, data, sizeof(hdr));
218+
219+
if (hdr.magic != AMX_MAGIC)
220+
return AMX_ERR_FORMAT;
221+
222+
void* memblock = malloc(hdr.stp);
223+
if (memblock == NULL)
224+
return AMX_ERR_MEMORY;
225+
226+
memcpy(memblock, data, hdr.size);
227+
228+
memset(amx, 0, sizeof(*amx));
229+
230+
int result = amx_Init(amx, memblock);
231+
if (result != AMX_ERR_NONE) {
232+
free(memblock);
233+
amx->base = NULL;
234+
}
235+
236+
return result;
237+
}
238+
239+
Pawn::Pawn(Controllers::DateTime& dateTimeController) : dateTimeController(dateTimeController) {
240+
#include "program.h"
219241

220-
memset(&amx, 0, sizeof(amx));
221-
amx_Init(&amx, prog);
242+
load_program(&amx, program);
243+
(void) program_len;
222244

223245
amx.userdata[0] = this;
224246

@@ -261,7 +283,7 @@ Pawn::~Pawn() {
261283
lv_obj_clean(lv_scr_act());
262284

263285
amx_Cleanup(&amx);
264-
delete amx.base;
286+
free(amx.base);
265287
}
266288

267289
void Pawn::Refresh() {

0 commit comments

Comments
 (0)