class_name SheetStore extends RefCounted ## Sheets and sheet templates as JSON files: user://sheets/.json and ## user://sheet_templates/.json. `root` can point elsewhere for tests. const SHEETS := "sheets" const TEMPLATES := "sheet_templates" var root := "user://" func _init(p_root: String = "user://") -> void: root = p_root func dir_for(templates: bool) -> String: return root.path_join(TEMPLATES if templates else SHEETS) func path_for(id: String, templates: bool) -> String: return dir_for(templates).path_join(id + ".json") ## Rows for the Sheets list, newest first: {id, name, modified, from_template}. func list(templates: bool = false) -> Array[Dictionary]: var out: Array[Dictionary] = [] var dir := dir_for(templates) if not DirAccess.dir_exists_absolute(dir): return out for file in DirAccess.get_files_at(dir): if not file.ends_with(".json"): continue var path := dir.path_join(file) var d := _read(path) if d.is_empty(): continue out.append({"id": file.get_basename(), "name": str(d.get("name", file.get_basename())), "modified": FileAccess.get_modified_time(path), "from_template": str(d.get("from_template", ""))}) out.sort_custom(func(a: Dictionary, b: Dictionary) -> bool: return int(a["modified"]) > int(b["modified"])) return out ## The sheet with `id`, or null. func load_sheet(id: String, templates: bool = false) -> SheetDoc: if not LibraryStore.is_valid_id(id): return null var d := _read(path_for(id, templates)) if d.is_empty(): return null var doc := SheetDoc.from_dict(d) doc.id = id return doc func save(doc: SheetDoc, templates: bool = false) -> Error: if not LibraryStore.is_valid_id(doc.id): return ERR_INVALID_PARAMETER DirAccess.make_dir_recursive_absolute(dir_for(templates)) return BoardFileIO.write_text_atomic(path_for(doc.id, templates), JSON.stringify(doc.to_dict(), "\t")) func delete(id: String, templates: bool = false) -> Error: if not LibraryStore.is_valid_id(id): return ERR_INVALID_PARAMETER return DirAccess.remove_absolute(path_for(id, templates)) ## A file id from `name` not used yet in that folder ("brin", "brin-2"…). func free_id(name: String, templates: bool = false) -> String: var base := LibraryStore.slugify(name) if base == "": base = "sheet" var id := base var n := 2 while FileAccess.file_exists(path_for(id, templates)): id = "%s-%d" % [base, n] n += 1 return id ## Copies a picture into the sheets' pictures folder (so the sheet keeps it ## if the original moves) and returns the copy's path ("" if unreadable). func keep_picture(path: String) -> String: return keep_picture_bytes(FileAccess.get_file_as_bytes(path), path.get_extension()) ## Saves picture `bytes` in the pictures folder, named by their hash (the ## same picture is kept once); returns its path ("" if it can't). func keep_picture_bytes(bytes: PackedByteArray, extension: String) -> String: if bytes.is_empty(): return "" DirAccess.make_dir_recursive_absolute(pictures_dir()) var kept := pictures_dir().path_join("%s.%s" % [str(hash(bytes)).replace("-", "n"), extension.to_lower()]) var file := FileAccess.open(kept, FileAccess.WRITE) if file == null: return "" file.store_buffer(bytes) return kept func pictures_dir() -> String: return dir_for(false).path_join("pictures") ## A new sheet, blank or copied from a template, saved and returned. func create(name: String, template_id: String = "") -> SheetDoc: var doc: SheetDoc = null if template_id != "": var tpl := load_sheet(template_id, true) if tpl: doc = tpl.copy() doc.from_template = template_id if doc == null: doc = SheetDoc.new() doc.name = name doc.id = free_id(name) save(doc) return doc ## Saves a copy of `doc` as a template named `name`; returns its id. func save_as_template(doc: SheetDoc, name: String) -> String: var tpl := doc.copy() tpl.name = name tpl.from_template = "" tpl.id = free_id(name, true) save(tpl, true) return tpl.id func _read(path: String) -> Dictionary: if not FileAccess.file_exists(path): return {} var parsed: Variant = JSON.parse_string(FileAccess.get_file_as_string(path)) return parsed if parsed is Dictionary else {}