Editor/core: resolve caminhos relativos pela pasta do projeto

Blocos que abrem arquivo (carregue o PNG, Image.Load, FS.*) passam a
resolver caminhos relativos contra o diretorio-base do projeto, definido
pelo editor ao abrir um .sb. Assim exemplos acham atlas.png etc. mesmo
quando o editor roda de outro cwd (ex.: AppImage aberto pelo menu), sem
mudar o cwd global (que quebrava "Ver codigo-fonte" e o harness).

- tb_path_set_base / tb_path_resolve, na API publica (src/tinyblocks.c)
- editor: project_set_dir no project_load (sem chdir)
- tb_png, tb_fs e tb_imglib usam o resolvedor
- MAX_USER_BLOCKS 16 -> 8192 (documentado em docs/editor.md)

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5 committed 2026-10-29 02:03:18 -03:00
1 parent e165e07e02
commit 85bd6f7cf4
9 files changed
+70 -5

No files matched your search

@@ -203,7 +203,7 @@ enum { CV_CODE, CV_STAGE };
#define STAGE_BUF 16384
#define LIT_TEXT_CAP 512
#define MAX_HOT_TYPES 64
#define MAX_USER_BLOCKS 16
#define MAX_USER_BLOCKS 8192
#define MAX_UBLK_SLOTS 8
#define MAX_UBLK_PARTS 16
@@ -3639,7 +3639,21 @@ static bool has_ext(const char* path, const char* ext) {
return n > e && !strcasecmp(path + n - e, ext);
}
/* Define o diretorio-base do projeto aberto, para que caminhos relativos
(imagens, FS.ReadText...) achem os arquivos do exemplo mesmo quando o
editor roda de outro cwd (ex.: AppImage aberto pelo menu). Nao muda o cwd. */
static void project_set_dir(const char* path) {
char buf[1024];
if (!path || !path[0] || strlen(path) >= sizeof(buf)) return;
strcpy(buf, path);
char* slash = strrchr(buf, '/');
if (!slash) return;
*slash = '\0';
if (buf[0]) tb_path_set_base(buf);
}
bool project_load(Ed* ed, const char* path) {
project_set_dir(path);
if (has_ext(path, ".txt") || has_ext(path, ".sb") || has_ext(path, ".scratchblocks"))
return sb_import_file(ed, path); /* notacao de texto do forum do Scratch */
char err[160] = "";
+6 -3
View File
@@ -45,10 +45,11 @@ static int64_t in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
static void eval_open(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* path = tb_block_input_text(self, rt, 0);
char rpath[1100];
int slot = fs_slot();
int64_t handle = 0;
if (slot >= 0 && path[0]) {
FILE* f = fopen(path, "rb");
FILE* f = fopen(tb_path_resolve(path, rpath, sizeof(rpath)), "rb");
if (f) { s_files[slot] = f; handle = slot + 1; }
}
tb_block_set_output(self, 0, tb_value_integer(handle));
@@ -80,9 +81,10 @@ static void eval_read(TinyBlocks_Block* self, TinyBlocks* rt) {
static void eval_readtext(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* path = tb_block_input_text(self, rt, 0);
char rpath[1100];
char* buf = NULL;
if (path[0]) {
FILE* f = fopen(path, "rb");
FILE* f = fopen(tb_path_resolve(path, rpath, sizeof(rpath)), "rb");
if (f) {
fseek(f, 0, SEEK_END);
long end = ftell(f);
@@ -109,8 +111,9 @@ static void eval_write(TinyBlocks_Block* self, TinyBlocks* rt) {
static void eval_append(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* path = tb_block_input_text(self, rt, 0);
const char* text = tb_block_input_text(self, rt, 1);
char rpath[1100];
if (path[0]) {
FILE* f = fopen(path, "ab");
FILE* f = fopen(tb_path_resolve(path, rpath, sizeof(rpath)), "ab");
if (f) { fwrite(text, 1, strlen(text), f); fclose(f); }
}
self->dirty = false;
@@ -141,6 +141,8 @@ static void eval_scale(TinyBlocks_Block* self, TinyBlocks* rt) {
}
static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* path = tb_block_input_text(self, rt, 0);
char rpath[1100];
path = tb_path_resolve(path, rpath, sizeof(rpath));
TinyBlocks_Image* img = tb_image_load(path);
if (!img) { tinyblocks_set_error(rt, "nao carregou '%s' (formato?)", path); self->dirty = false; return; }
/* A imagem carregada ganha nome (nome do arquivo) e numero no armazem,
+5
View File
@@ -21,6 +21,9 @@ const char* tb_png_last_error(void) { return s_err; }
TinyBlocks_Image* tb_png_load(const char* path) {
s_err[0] = 0;
if (!path) { snprintf(s_err, sizeof(s_err), "caminho nulo"); return NULL; }
char rpath[1100];
tb_path_resolve(path, rpath, sizeof(rpath));
path = rpath;
png_image pv;
memset(&pv, 0, sizeof(pv));
pv.version = PNG_IMAGE_VERSION;
@@ -48,6 +51,8 @@ TinyBlocks_Image* tb_png_load(const char* path) {
bool tb_png_save(const TinyBlocks_Image* img, const char* path) {
s_err[0] = 0;
if (!img || !path) { snprintf(s_err, sizeof(s_err), "imagem/caminho nulo"); return false; }
char rpath[1100];
path = tb_path_resolve(path, rpath, sizeof(rpath));
int w = tb_image_width(img), h = tb_image_height(img);
if (w <= 0 || h <= 0) { snprintf(s_err, sizeof(s_err), "imagem vazia"); return false; }
size_t sz = (size_t)w * h * 4;