feat: use bl_file to replicate font and sound files

This commit is contained in:
Swann
2020-09-20 23:31:24 +02:00
parent 150054d19c
commit b77ab2dd05
3 changed files with 32 additions and 48 deletions

View File

@ -44,7 +44,7 @@ class BlFile(ReplicatedDatablock):
bl_id = 'file' bl_id = 'file'
bl_name = "file" bl_name = "file"
bl_class = Path bl_class = Path
bl_delay_refresh = 1 bl_delay_refresh = 0
bl_delay_apply = 1 bl_delay_apply = 1
bl_automatic_push = True bl_automatic_push = True
bl_check_common = False bl_check_common = False
@ -56,7 +56,6 @@ class BlFile(ReplicatedDatablock):
# TODO: handle packed_file # TODO: handle packed_file
# TODO: ensure absolute path # TODO: ensure absolute path
# TODO: ensure file exist # TODO: ensure file exist
logging.info(self.instance)
self.preferences = utils.get_preferences() self.preferences = utils.get_preferences()
self.diff_method = DIFF_BINARY self.diff_method = DIFF_BINARY
@ -105,18 +104,20 @@ class BlFile(ReplicatedDatablock):
""" """
logging.info(f"Writing {data['name']} to {target}") logging.info(f"Writing {data['name']} to {target}")
# TODO: check fiile already exist
# TODO: check for empty data # 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.") logging.info("File already loaded, skipping.")
return return
try: try:
file = open(target, "wb") file = open(target, "wb")
file.write(data['file']) file.write()
except IOError: except IOError:
logging.warning(f"{target} doesn't exist, skipping") logging.warning(f"{target} doesn't exist, skipping")
else: else:
file.close() file.close()
def diff(self): 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

View File

@ -21,9 +21,7 @@ import os
from pathlib import Path from pathlib import Path
import bpy import bpy
import mathutils
from .. import utils
from .bl_datablock import BlDatablock from .bl_datablock import BlDatablock
from .bl_file import get_filepath from .bl_file import get_filepath
from .dump_anything import Dumper, Loader from .dump_anything import Dumper, Loader
@ -43,13 +41,11 @@ class BlFont(BlDatablock):
return bpy.data.fonts.load(data['filepath']) return bpy.data.fonts.load(data['filepath'])
else: else:
filename = Path(data['filepath']).name filename = Path(data['filepath']).name
logging.info(f'loading {filename}')
return bpy.data.fonts.load(get_filepath(filename)) return bpy.data.fonts.load(get_filepath(filename))
def _load(self, data, target): def _load(self, data, target):
pass loader = Loader()
loader.load(target, data)
def _dump(self, instance=None): def _dump(self, instance=None):
return { return {

View File

@ -16,14 +16,16 @@
# ##### END GPL LICENSE BLOCK ##### # ##### END GPL LICENSE BLOCK #####
import bpy
import mathutils
import os
import logging import logging
import pathlib import os
from .. import utils from pathlib import Path
from .dump_anything import Loader, Dumper
import bpy
from .bl_file import get_filepath
from .bl_datablock import BlDatablock from .bl_datablock import BlDatablock
from .dump_anything import Dumper, Loader
class BlSound(BlDatablock): class BlSound(BlDatablock):
bl_id = "sounds" bl_id = "sounds"
@ -35,40 +37,25 @@ class BlSound(BlDatablock):
bl_icon = 'SOUND' bl_icon = 'SOUND'
def _construct(self, data): def _construct(self, data):
if 'file' in data.keys(): filename = Path(data['filepath']).name
prefs = utils.get_preferences() return bpy.data.sounds.load(get_filepath(filename))
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)
def _load(self, data, target): def _load(self, data, target):
loader = Loader() loader = Loader()
loader.load(target, data) 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): def diff(self):
return False 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