Skip to content

Instantly share code, notes, and snippets.

@pblocz
Created June 21, 2022 21:15
Show Gist options
  • Save pblocz/2943d1d430cbba89bc46ab628a3bde83 to your computer and use it in GitHub Desktop.
Save pblocz/2943d1d430cbba89bc46ab628a3bde83 to your computer and use it in GitHub Desktop.
Update owlbear exported map. Old is the map with the notes, tokens, etc and New is the map with the updated background.
#%%
from dataclasses import InitVar, dataclass, field
import json
from pathlib import Path
from typing import Any, Dict
import jmespath as jm
#%%
old = Path("old.owlbear")
new = Path("new.owlbear")
old_json = json.load(old.open())
new_json = json.load(new.open())
# json.dump(old_json, open('test-pre.owlbear', 'w'), indent=2)
#%%
@dataclass
class Buffer:
name: str
id: str
buffer: str = None
source: Any = None
@dataclass
class Files:
name: str
file: InitVar[Buffer]
thumbnail: InitVar[Buffer]
resolutions: InitVar[Dict[str, Buffer]]
_buffers: Dict[str, Buffer] = field(init=False, default_factory=dict)
def __post_init__(self, file: Buffer, thumbnail: Buffer, resolutions: Dict[str, Buffer]):
self._file = file.id
self._buffers[file.id] = file
self._thumbnail = thumbnail.id
self._buffers[thumbnail.id] = thumbnail
self._resolutions = {}
for k, v in resolutions.items():
self._resolutions[k] = v.id
self._buffers[v.id] = v
@property
def file(self):
return self._buffers[self._file]
@property
def thumbnail(self):
return self._buffers[self._file]
@property
def resolutions(self):
return {k: self._buffers[v] for k, v in self._resolutions.items()}
def process_owlbear(data):
maps_data = jm.search('data.data[?tableName == `maps`].rows[]', data)
assets_data = jm.search('data.data[?tableName == `assets`].rows[]', data)
def process_map(map):
return Files(
name=map['name'],
file=Buffer('file', map['file']),
thumbnail=Buffer('thumbnail', map['thumbnail']),
resolutions={r: Buffer(r, v) for r, v in map['resolutions'].items()}
)
map_buffers = [process_map(map) for map in maps_data]
for map in map_buffers:
for _id, buffer in map._buffers.items():
print(_id)
buffer.source = jm.search(f'[?id == `{_id}`].file', assets_data)[0]
buffer.buffer = buffer.source['buffer']
print(id(buffer.source))
print(id(jm.search(f'[?id == `{_id}`].file', assets_data)[0]))
print(len(buffer.buffer))
return map_buffers
#%%
processed_new_data = process_owlbear(new_json)
processed_old_data = process_owlbear(old_json)
# %%
print(processed_new_data[0].resolutions.keys())
print(processed_old_data[0].resolutions.keys())
print(processed_new_data[0].file.id)
print(processed_old_data[0].file.id)
processed_old_data[0].file.source["buffer"] = processed_new_data[0].file.buffer
processed_old_data[0].thumbnail.source["buffer"] = processed_new_data[0].thumbnail.buffer
for k, v in processed_new_data[0].resolutions.items():
processed_old_data[0].resolutions[k].source["buffer"] = v.buffer
json.dump(old_json, open('updated.owlbear', 'w'), indent=2)
# print(processed_old_data[0].file.id)
# print(f'data.data[?tableName == `assets`].rows[?id == `{processed_old_data[0].file.id}`]')
# print(
# jm.search(f'data.data[?tableName == `assets`].rows[?id == `{processed_old_data[0].file.id}`]', old_json))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment