Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ The package version runs on its own semver; the schema it is generated from is `

## Unreleased

## 0.144.0 (2026-11-23)

- **A share leaves out a file it could not read as an element.** `write_world_file(include_hidden=False)` and `world_file_bytes` carry a `.json` file in a type folder only when they read it as an element and found no hidden mark. A file there that could not be read is left out too, since its mark cannot be told: one that is not UTF-8 JSON (a write cut short), is nested more than 200 deep, holds a number that is not finite or an integer of more than 4,300 digits, or is not an object. Until now a share carried a file that was no JSON as it was, so a hidden element whose file was cut short travelled with its text. `unread_left_out=`, a second list the caller passes, receives those files' paths, sorted, apart from `hidden_left_out=`: a broken file is one a person may want to mend. This is the folder format's rule for a share, as ruled on 2026-11-23.
- **A copy carries such a file byte for byte, and no longer stops.** 0.143.0 raised `ValueError` on an element file nested too deep or holding a number it could not hold, in a share and in a copy that leaves author fields out. A copy is how a person moves their own world, broken file included: it now carries the file as it is, with whatever author fields it holds, and never rewrites it. Nothing raises for such a file any more.

## 0.143.0 (2026-11-23)

- **Fixed: `write_world_file(include_hidden=False)` could carry a hidden element.** 0.142.0 made the package's reader refuse a file nested more than 200 deep, and the writer of a world file took that refusal for "this file is no element": a hidden element whose file was nested that deep travelled in a share, whole and with its author fields, where 0.141.0 had left it out. The same was true in every earlier version for an element file holding an integer of more than 4,300 digits. Now a file the reader refuses stops the write: `write_world_file` and `world_file_bytes` raise `ValueError` naming the file and why, and nothing is written, whenever they would have had to read that file, to leave out the hidden (`include_hidden=False`) or to take out author fields (the default). A copy that leaves nothing out (`include_hidden=True, include_author_fields=True`) reads no element file and still carries every one byte for byte. Text that is no JSON at all is no element to any reader and travels as it is, as before. If you share world files from folders you did not write yourself, upgrade.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ world = read_world_file("moppetopia.owworld")
world.to_folder("moppetopia-copy")
```

`write_world_file` carries `world.json` byte for byte (its `producer`, `source` and `format_version` included) and the folder's parts, `elements/`, `spatial/`, `media/` and `records/`, and nothing else: never an `api` block, never a tool's own folder unless you hand it in (`private_dir=".atlas"`, which makes the file a private copy rather than one to send around), and never a producer's records its `records/RECORDS.json` entry marks `"private": true` unless you ask (`include_private_records=True`); the manifest itself always travels as it is. A hidden element (`hidden: true` in its file) travels byte for byte, since the file is a copy; for a file you hand to someone else, `include_hidden=False` leaves every hidden element's file out whole, and a list passed as `hidden_left_out=` receives their paths. Nothing else changes, so a link from a visible element to a hidden one keeps an id the file no longer holds. An element's author fields (`created_by`, `changed_by`, `changed_via`, `changed_client` and `doors`) record which member and which tool wrote it, which means nothing to someone the world is handed to, so every element file leaves them out by default (`created_at` and `updated_at` stay): a file holding any of them is rewritten without them in the folder's own form, and a file holding none travels byte for byte. To archive your own world as it is on disk, pass `include_author_fields=True`. An element file the writer must read and cannot hold (nested more than 200 deep, or holding a number that is not finite or an integer of more than 4,300 digits) stops the write with a `ValueError` naming it: whether it is hidden cannot be told, and nothing is carried by guess. The same folder gives the same bytes on one Python. `read_world_file` checks the whole archive before it unpacks anything and refuses a file it cannot open safely with a `WorldFileError`, whose `reason` names one of the spec's twelve causes (`unsafe_path`, `collision`, `too_big`, ...). It gives the same verdict as the TypeScript and Unity SDKs on every case of the format's conformance set.
`write_world_file` carries `world.json` byte for byte (its `producer`, `source` and `format_version` included) and the folder's parts, `elements/`, `spatial/`, `media/` and `records/`, and nothing else: never an `api` block, never a tool's own folder unless you hand it in (`private_dir=".atlas"`, which makes the file a private copy rather than one to send around), and never a producer's records its `records/RECORDS.json` entry marks `"private": true` unless you ask (`include_private_records=True`); the manifest itself always travels as it is. A hidden element (`hidden: true` in its file) travels byte for byte, since the file is a copy; for a file you hand to someone else, `include_hidden=False` leaves every hidden element's file out whole, and a list passed as `hidden_left_out=` receives their paths. Nothing else changes, so a link from a visible element to a hidden one keeps an id the file no longer holds. An element's author fields (`created_by`, `changed_by`, `changed_via`, `changed_client` and `doors`) record which member and which tool wrote it, which means nothing to someone the world is handed to, so every element file leaves them out by default (`created_at` and `updated_at` stay): a file holding any of them is rewritten without them in the folder's own form, and a file holding none travels byte for byte. To archive your own world as it is on disk, pass `include_author_fields=True`. A share carries a file in a type folder only when it was read as an element and no mark was found: one that could not be read (not UTF-8 JSON, a write cut short, nested more than 200 deep, a number that is not finite, not an object) is left out as well, and a second list, `unread_left_out=`, receives those paths. A copy carries such a file byte for byte. The same folder gives the same bytes on one Python. `read_world_file` checks the whole archive before it unpacks anything and refuses a file it cannot open safely with a `WorldFileError`, whose `reason` names one of the spec's twelve causes (`unsafe_path`, `collision`, `too_big`, ...). It gives the same verdict as the TypeScript and Unity SDKs on every case of the format's conformance set.

To send a folder's edits back, keep a copy exported before you edited, then push the difference:

Expand Down
2 changes: 1 addition & 1 deletion src/onlyworlds/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.143.0"
__version__ = "0.144.0"
93 changes: 59 additions & 34 deletions src/onlyworlds/worldfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
from __future__ import annotations

import io
import json
import os
import re
import shutil
Expand Down Expand Up @@ -836,35 +835,28 @@ def _default_top(world: Mapping[str, Any]) -> str:
return f"{slugify(name if isinstance(name, str) else None) or 'world'}-{str(world['id'])[:8]}"


def _element_body(rel: str, src: Path, to: str) -> Any:
class _Unread:
"""An element file the writer could not read as an element."""


_UNREAD = _Unread()


def _element_body(rel: str, src: Path) -> dict[str, Any] | _Unread | None:
"""What an element file holds (a ``.json`` file directly in a type folder of ``elements/`` or legacy
``spatial/``), for a writer that must read it ``to`` leave something out. None for a file anywhere else,
or one that is not UTF-8 JSON: no reader takes that as an element, and it is carried as it is, as ever.
``spatial/``): its JSON object. None for a file anywhere else, which is no element file.

A file that is JSON this package's reader refuses (nested past its depth, a number it cannot hold) raises
``ValueError``: what it holds cannot be read, so the writer leaves nothing in by guess, as with a records
manifest it cannot read (OW-8)."""
``_UNREAD`` for a file there that could not be read as an element: not UTF-8, not JSON (a write cut
short), JSON this package's reader refuses (nested past its depth, a number it cannot hold), or not an
object. What such a file holds cannot be told, its hidden mark included."""
segs = rel.split("/")
if len(segs) != 3 or segs[0] not in (ELEMENTS_DIR, LEGACY_SPATIAL_DIR) or not rel.lower().endswith(".json"):
return None
try:
return _outside_json.loads(src.read_bytes().decode("utf-8-sig"))
except (UnicodeDecodeError, json.JSONDecodeError):
return None
except ValueError as exc:
raise ValueError(
f"{src} cannot be read ({exc}), so the writer cannot {to} and carries nothing by guess. Fix or "
"remove the file, or pass include_hidden=True and include_author_fields=True to carry every file "
"as it is."
) from None


def _hidden_element_file(rel: str, src: Path) -> bool:
"""An element file whose JSON object carries the hidden mark at the top level (``folder.is_hidden``).
A file that is not an element file, or not a JSON object, is carried as it is, as ever; one the reader
refuses raises (``_element_body``)."""
body = _element_body(rel, src, "tell whether it is hidden")
return isinstance(body, dict) and is_hidden(body)
body = _outside_json.loads(src.read_bytes().decode("utf-8-sig"))
except ValueError: # not UTF-8, not JSON, or JSON the reader refuses
return _UNREAD
return body if isinstance(body, dict) else _UNREAD


#: Which member and which tool wrote an element (a membership id, a door): nothing to someone the
Expand All @@ -876,10 +868,9 @@ def _without_author_fields(rel: str, src: Path) -> bytes | None:
"""An element file (``.json`` directly in a type folder of ``elements/`` or legacy ``spatial/``,
as the TypeScript writer reads one) without its author fields, in the folder's own form
(``_jsonfmt.encode``: two-space indent, a trailing newline, the other keys in their order), or
None when it holds none of them or is not a JSON object: then it travels byte for byte. Only
top-level keys go; an ``x_*`` value that holds one of the names is the extension's, and stays.
A file the reader refuses raises (``_element_body``): its author fields cannot be told from its bytes."""
body = _element_body(rel, src, "take out its author fields")
None when it holds none of them or could not be read as an element: then it travels byte for byte.
Only top-level keys go; an ``x_*`` value that holds one of the names is the extension's, and stays."""
body = _element_body(rel, src)
if not isinstance(body, dict) or not any(k in body for k in _AUTHOR_FIELDS):
return None
return encode({k: v for k, v in body.items() if k not in _AUTHOR_FIELDS})
Expand All @@ -893,6 +884,7 @@ def _plan(
include_hidden: bool = True,
hidden_left_out: list[str] | None = None,
include_author_fields: bool = False,
unread_left_out: list[str] | None = None,
) -> tuple[str, list[tuple[str, Path | bytes]]]:
root = Path(folder)
if not root.is_dir():
Expand All @@ -916,11 +908,23 @@ def in_private(rel: str) -> bool:

files = {rel: src for rel, src in files.items() if not in_private(rel)}
if not include_hidden:
# A share (§4.8): each hidden element's file left out whole; nothing else rewritten.
hidden = sorted(rel for rel, src in files.items() if _hidden_element_file(rel, src))
files = {rel: src for rel, src in files.items() if rel not in set(hidden)}
# A share (§4.8): it carries an element file only when it was read as an element and no mark was
# found. A hidden element's file is left out whole, and so is one that could not be read, listed
# apart; nothing else is rewritten.
hidden: list[str] = []
unread: list[str] = []
for rel in sorted(files):
body = _element_body(rel, files[rel])
if isinstance(body, _Unread):
unread.append(rel)
elif body is not None and is_hidden(body):
hidden.append(rel)
left = {*hidden, *unread}
files = {rel: src for rel, src in files.items() if rel not in left}
if hidden_left_out is not None:
hidden_left_out.extend(hidden)
if unread_left_out is not None:
unread_left_out.extend(unread)
raw = files[WORLD_FILE].read_bytes()
world_bytes, world = _without_link(raw, _parse_world(raw))
top = _default_top(world) if top_dir is None else top_dir
Expand Down Expand Up @@ -983,13 +987,21 @@ def world_file_bytes(
include_hidden: bool = True,
hidden_left_out: list[str] | None = None,
include_author_fields: bool = False,
unread_left_out: list[str] | None = None,
) -> bytes:
"""The world file for the folder at ``folder``, as bytes. See ``write_world_file``: the
same keywords, the same refusals, private records left out unless
``include_private_records=True``, hidden elements kept unless ``include_hidden=False``,
author fields left out unless ``include_author_fields=True``."""
top, plan = _plan(
folder, top_dir, private_dir, include_private_records, include_hidden, hidden_left_out, include_author_fields
folder,
top_dir,
private_dir,
include_private_records,
include_hidden,
hidden_left_out,
include_author_fields,
unread_left_out,
)
buf = io.BytesIO()
_write(buf, top, plan)
Expand All @@ -1006,6 +1018,7 @@ def write_world_file(
include_hidden: bool = True,
hidden_left_out: list[str] | None = None,
include_author_fields: bool = False,
unread_left_out: list[str] | None = None,
) -> Path:
"""Write the folder at ``folder`` as one world file at ``path``; returns ``path``.

Expand Down Expand Up @@ -1035,7 +1048,12 @@ def write_world_file(
passes, receives each left-out file's path inside the folder (``elements/character/x.json``),
sorted, and its length is the count. Nothing else is rewritten: a visible element that links
to a hidden one keeps the id, which dangles in the file (a reader meets it as a link to an
element the world does not hold).
element the world does not hold). A share carries a file in a type folder only when it
was read as an element and no mark was found: one that could not be read (not UTF-8 JSON, a
write cut short, nested more than 200 deep, a number that is not finite, not an object) is
left out too, since its mark cannot be told, and ``unread_left_out``, a second list the
caller passes, receives those paths, sorted. They are counted apart: a broken file is one a
person may want to mend. A copy carries such a file byte for byte.
- Author fields are left out: ``created_by``, ``changed_by``, ``changed_via``,
``changed_client`` and ``doors`` record which member and which tool wrote each element (a
membership id, a door), which means nothing to someone the world is handed to, so every
Expand All @@ -1062,7 +1080,14 @@ def write_world_file(
a crash never leaves half a file.
"""
top, plan = _plan(
folder, top_dir, private_dir, include_private_records, include_hidden, hidden_left_out, include_author_fields
folder,
top_dir,
private_dir,
include_private_records,
include_hidden,
hidden_left_out,
include_author_fields,
unread_left_out,
)
target = Path(path)
tmp = target.parent / f".{target.name}.tmp-{uuid.uuid4().hex[:8]}"
Expand Down
Loading
Loading