feat: use bl_file to replicate font and sound files
This commit is contained in:
@ -44,7 +44,7 @@ class BlFile(ReplicatedDatablock):
|
||||
bl_id = 'file'
|
||||
bl_name = "file"
|
||||
bl_class = Path
|
||||
bl_delay_refresh = 1
|
||||
bl_delay_refresh = 0
|
||||
bl_delay_apply = 1
|
||||
bl_automatic_push = True
|
||||
bl_check_common = False
|
||||
@ -56,7 +56,6 @@ class BlFile(ReplicatedDatablock):
|
||||
# TODO: handle packed_file
|
||||
# TODO: ensure absolute path
|
||||
# TODO: ensure file exist
|
||||
logging.info(self.instance)
|
||||
|
||||
self.preferences = utils.get_preferences()
|
||||
self.diff_method = DIFF_BINARY
|
||||
@ -105,18 +104,20 @@ class BlFile(ReplicatedDatablock):
|
||||
"""
|
||||
logging.info(f"Writing {data['name']} to {target}")
|
||||
|
||||
# TODO: check fiile already exist
|
||||
# TODO: check for empty data
|
||||
if target.exists() and (sys.getsizeof(data['file']) == self.instance.stat().st_size):
|
||||
|
||||
if target.exists() and not self.diff():
|
||||
logging.info("File already loaded, skipping.")
|
||||
return
|
||||
try:
|
||||
file = open(target, "wb")
|
||||
file.write(data['file'])
|
||||
file.write()
|
||||
except IOError:
|
||||
logging.warning(f"{target} doesn't exist, skipping")
|
||||
else:
|
||||
file.close()
|
||||
|
||||
def diff(self):
|
||||
return False
|
||||
memory_size = sys.getsizeof(self.data['file'])-33
|
||||
disk_size = self.instance.stat().st_size
|
||||
return memory_size == disk_size
|
||||
|
@ -21,9 +21,7 @@ import os
|
||||
from pathlib import Path
|
||||
|
||||
import bpy
|
||||
import mathutils
|
||||
|
||||
from .. import utils
|
||||
from .bl_datablock import BlDatablock
|
||||
from .bl_file import get_filepath
|
||||
from .dump_anything import Dumper, Loader
|
||||
@ -43,13 +41,11 @@ class BlFont(BlDatablock):
|
||||
return bpy.data.fonts.load(data['filepath'])
|
||||
else:
|
||||
filename = Path(data['filepath']).name
|
||||
|
||||
logging.info(f'loading {filename}')
|
||||
|
||||
return bpy.data.fonts.load(get_filepath(filename))
|
||||
|
||||
def _load(self, data, target):
|
||||
pass
|
||||
loader = Loader()
|
||||
loader.load(target, data)
|
||||
|
||||
def _dump(self, instance=None):
|
||||
return {
|
||||
|
@ -16,14 +16,16 @@
|
||||
# ##### END GPL LICENSE BLOCK #####
|
||||
|
||||
|
||||
import bpy
|
||||
import mathutils
|
||||
import os
|
||||
import logging
|
||||
import pathlib
|
||||
from .. import utils
|
||||
from .dump_anything import Loader, Dumper
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import bpy
|
||||
|
||||
from .bl_file import get_filepath
|
||||
from .bl_datablock import BlDatablock
|
||||
from .dump_anything import Dumper, Loader
|
||||
|
||||
|
||||
class BlSound(BlDatablock):
|
||||
bl_id = "sounds"
|
||||
@ -35,40 +37,25 @@ class BlSound(BlDatablock):
|
||||
bl_icon = 'SOUND'
|
||||
|
||||
def _construct(self, data):
|
||||
if 'file' in data.keys():
|
||||
prefs = utils.get_preferences()
|
||||
ext = data['filepath'].split(".")[-1]
|
||||
sound_name = f"{self.uuid}.{ext}"
|
||||
sound_path = os.path.join(prefs.cache_directory, sound_name)
|
||||
|
||||
os.makedirs(prefs.cache_directory, exist_ok=True)
|
||||
file = open(sound_path, 'wb')
|
||||
file.write(data["file"])
|
||||
file.close()
|
||||
|
||||
logging.info(f'loading {sound_path}')
|
||||
return bpy.data.sounds.load(sound_path)
|
||||
filename = Path(data['filepath']).name
|
||||
return bpy.data.sounds.load(get_filepath(filename))
|
||||
|
||||
def _load(self, data, target):
|
||||
loader = Loader()
|
||||
loader.load(target, data)
|
||||
|
||||
def _dump(self, instance=None):
|
||||
if not instance.packed_file:
|
||||
# prefs = utils.get_preferences()
|
||||
# ext = pathlib.Path(instance.filepath).suffix
|
||||
# sound_name = f"{self.uuid}{ext}"
|
||||
# sound_path = os.path.join(prefs.cache_directory, sound_name)
|
||||
# instance.filepath = sound_path
|
||||
instance.pack()
|
||||
#TODO:use file locally with unpack(method='USE_ORIGINAL') ?
|
||||
|
||||
return {
|
||||
'filepath':instance.filepath,
|
||||
'name':instance.name,
|
||||
'file': instance.packed_file.data
|
||||
}
|
||||
|
||||
|
||||
def diff(self):
|
||||
return False
|
||||
|
||||
def _dump(self, instance=None):
|
||||
return {
|
||||
'filepath': instance.filepath,
|
||||
'name': instance.name
|
||||
}
|
||||
|
||||
def _resolve_deps_implementation(self):
|
||||
deps = []
|
||||
if self.instance.filepath and self.instance.filepath != '<builtin>':
|
||||
deps.append(Path(self.instance.filepath))
|
||||
|
||||
return deps
|
||||
|
Reference in New Issue
Block a user