add support to compress bme json
This commit is contained in:
		
							
								
								
									
										5
									
								
								.gitattributes
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.gitattributes
									
									
									
									
										vendored
									
									
								
							@ -2,6 +2,5 @@
 | 
				
			|||||||
*.png binary
 | 
					*.png binary
 | 
				
			||||||
# our generated mesh should be save as binary
 | 
					# our generated mesh should be save as binary
 | 
				
			||||||
*.bin binary
 | 
					*.bin binary
 | 
				
			||||||
# json is data and not good for human reading(althought I edit it on my own hand.)
 | 
					# the compressed json data should be binary
 | 
				
			||||||
# so set it as binary
 | 
					bbp_ng/raw_jsons/*.json    binary
 | 
				
			||||||
bbp_ng/json/*.json    binary
 | 
					 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -7,6 +7,8 @@ bbp_ng/PyBMap/*.pdb
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
bbp_ng/icons/*
 | 
					bbp_ng/icons/*
 | 
				
			||||||
!bbp_ng/icons/.gitkeep
 | 
					!bbp_ng/icons/.gitkeep
 | 
				
			||||||
 | 
					bbp_ng/jsons/*
 | 
				
			||||||
 | 
					!bbp_ng/jsons/.gitkeep
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Byte-compiled / optimized / DLL files
 | 
					# Byte-compiled / optimized / DLL files
 | 
				
			||||||
__pycache__/
 | 
					__pycache__/
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										0
									
								
								bbp_ng/jsons/.gitkeep
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								bbp_ng/jsons/.gitkeep
									
									
									
									
									
										Normal file
									
								
							@ -97,9 +97,9 @@
 | 
				
			|||||||
                "indices": [3, 7, 4, 0],
 | 
					                "indices": [3, 7, 4, 0],
 | 
				
			||||||
                "uvs": [
 | 
					                "uvs": [
 | 
				
			||||||
                    "(0, 0)",
 | 
					                    "(0, 0)",
 | 
				
			||||||
                    "(1, 0)",
 | 
					                    "(uv_height, 0)",
 | 
				
			||||||
                    "(1, uv_height)",
 | 
					                    "(uv_height, 1)",
 | 
				
			||||||
                    "(0, uv_height)"
 | 
					                    "(0, 1)"
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
                "normals": [
 | 
					                "normals": [
 | 
				
			||||||
                    "(-1, 0, 0)",
 | 
					                    "(-1, 0, 0)",
 | 
				
			||||||
@ -114,9 +114,9 @@
 | 
				
			|||||||
                "indices": [1, 5, 6, 2],
 | 
					                "indices": [1, 5, 6, 2],
 | 
				
			||||||
                "uvs": [
 | 
					                "uvs": [
 | 
				
			||||||
                    "(0, 0)",
 | 
					                    "(0, 0)",
 | 
				
			||||||
                    "(1, 0)",
 | 
					                    "(uv_height, 0)",
 | 
				
			||||||
                    "(1, uv_height)",
 | 
					                    "(uv_height, 1)",
 | 
				
			||||||
                    "(0, uv_height)"
 | 
					                    "(0, 1)"
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
                "normals": [
 | 
					                "normals": [
 | 
				
			||||||
                    "(1, 0, 0)",
 | 
					                    "(1, 0, 0)",
 | 
				
			||||||
@ -131,9 +131,9 @@
 | 
				
			|||||||
                "indices": [0, 4, 5, 1],
 | 
					                "indices": [0, 4, 5, 1],
 | 
				
			||||||
                "uvs": [
 | 
					                "uvs": [
 | 
				
			||||||
                    "(0, 0)",
 | 
					                    "(0, 0)",
 | 
				
			||||||
                    "(1, 0)",
 | 
					                    "(uv_height, 0)",
 | 
				
			||||||
                    "(1, uv_height)",
 | 
					                    "(uv_height, 1)",
 | 
				
			||||||
                    "(0, uv_height)"
 | 
					                    "(0, 1)"
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
                "normals": [
 | 
					                "normals": [
 | 
				
			||||||
                    "(0, -1, 0)",
 | 
					                    "(0, -1, 0)",
 | 
				
			||||||
@ -148,15 +148,15 @@
 | 
				
			|||||||
                "indices": [2, 6, 7, 3],
 | 
					                "indices": [2, 6, 7, 3],
 | 
				
			||||||
                "uvs": [
 | 
					                "uvs": [
 | 
				
			||||||
                    "(0, 0)",
 | 
					                    "(0, 0)",
 | 
				
			||||||
                    "(1, 0)",
 | 
					                    "(uv_height, 0)",
 | 
				
			||||||
                    "(1, uv_height)",
 | 
					                    "(uv_height, 1)",
 | 
				
			||||||
                    "(0, uv_height)"
 | 
					                    "(0, 1)"
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
                "normals": [
 | 
					                "normals": [
 | 
				
			||||||
                    "(1, 0, 0)",
 | 
					                    "(0, 1, 0)",
 | 
				
			||||||
                    "(1, 0, 0)",
 | 
					                    "(0, 1, 0)",
 | 
				
			||||||
                    "(1, 0, 0)",
 | 
					                    "(0, 1, 0)",
 | 
				
			||||||
                    "(1, 0, 0)"
 | 
					                    "(0, 1, 0)"
 | 
				
			||||||
                ]
 | 
					                ]
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        ],
 | 
					        ],
 | 
				
			||||||
@ -1,4 +1,5 @@
 | 
				
			|||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					import common
 | 
				
			||||||
import PIL, PIL.Image
 | 
					import PIL, PIL.Image
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# the config for thumbnail
 | 
					# the config for thumbnail
 | 
				
			||||||
@ -12,29 +13,29 @@ def resize_image(src_file: str, dst_file: str) -> None:
 | 
				
			|||||||
    # save to new file
 | 
					    # save to new file
 | 
				
			||||||
    src_image.save(dst_file)
 | 
					    src_image.save(dst_file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def relative_to_folder(abs_path: str, src_parent: str, dst_parent: str) -> str:
 | 
					 | 
				
			||||||
    return os.path.join(dst_parent, os.path.relpath(abs_path, src_parent))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def create_thumbnails() -> None:
 | 
					def create_thumbnails() -> None:
 | 
				
			||||||
    # get folder path
 | 
					    # get folder path
 | 
				
			||||||
    root_folder: str = os.path.dirname(os.path.dirname(__file__))
 | 
					    root_folder: str = os.path.dirname(os.path.dirname(__file__))
 | 
				
			||||||
    raw_icons_folder: str = os.path.join(root_folder, 'raw_icons')
 | 
					 | 
				
			||||||
    icons_folder: str = os.path.join(root_folder, 'icons')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # iterate raw icons folder
 | 
					    # prepare handler
 | 
				
			||||||
    for root, dirs, files in os.walk(raw_icons_folder, topdown = True):
 | 
					    def folder_handler(src_folder: str, dst_folder: str) -> None:
 | 
				
			||||||
        # iterate folder and create it in dest folder
 | 
					        # just create folder
 | 
				
			||||||
        for name in dirs:
 | 
					        print(f'Creating Folder: {src_folder} -> {dst_folder}')
 | 
				
			||||||
            src_folder: str = os.path.join(root, name)
 | 
					        os.makedirs(dst_folder, exist_ok = True)
 | 
				
			||||||
            dst_folder: str = relative_to_folder(src_folder, raw_icons_folder, icons_folder)
 | 
					    def file_handler(src_file: str, dst_file: str) -> None:
 | 
				
			||||||
            print(f'Creating Folder: {src_folder} -> {dst_folder}')
 | 
					        # skip non-image
 | 
				
			||||||
            os.makedirs(dst_folder, exist_ok = True)
 | 
					        if not src_file.endswith('.png'): return
 | 
				
			||||||
        for name in files:
 | 
					        # call thumbnail func
 | 
				
			||||||
            if not name.endswith('.png'): continue  # skip non-image
 | 
					        print(f'Processing Thumbnail: {src_file} -> {dst_file}')
 | 
				
			||||||
            src_file: str = os.path.join(root, name)
 | 
					        resize_image(src_file, dst_file)
 | 
				
			||||||
            dst_file: str = relative_to_folder(src_file, raw_icons_folder, icons_folder)
 | 
					
 | 
				
			||||||
            print(f'Processing Thumbnail: {src_file} -> {dst_file}')
 | 
					    # call common processor
 | 
				
			||||||
            resize_image(src_file, dst_file)
 | 
					    common.common_file_migrator(
 | 
				
			||||||
 | 
					        os.path.join(root_folder, 'raw_icons'),
 | 
				
			||||||
 | 
					        os.path.join(root_folder, 'icons'),
 | 
				
			||||||
 | 
					        folder_handler,
 | 
				
			||||||
 | 
					        file_handler
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    print('Done.')
 | 
					    print('Done.')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										43
									
								
								bbp_ng/tools/build_jsons.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								bbp_ng/tools/build_jsons.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,43 @@
 | 
				
			|||||||
 | 
					import os, json
 | 
				
			||||||
 | 
					import common
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def compress_json(src_file: str, dst_file: str) -> None:
 | 
				
			||||||
 | 
					    with open(src_file, 'r', encoding = 'utf-8') as fr:
 | 
				
			||||||
 | 
					        with open(dst_file, 'w', encoding = 'utf-8') as fw:
 | 
				
			||||||
 | 
					            json.dump(
 | 
				
			||||||
 | 
					                json.load(fr),  # load from src file
 | 
				
			||||||
 | 
					                fw,
 | 
				
			||||||
 | 
					                indent = None,  # no indent. the most narrow style.
 | 
				
			||||||
 | 
					                separators = (',', ':'),    # also for narrow style.
 | 
				
			||||||
 | 
					                sort_keys = False,  # do not sort key
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def create_compressed_jsons() -> None:
 | 
				
			||||||
 | 
					    # get folder path
 | 
				
			||||||
 | 
					    root_folder: str = os.path.dirname(os.path.dirname(__file__))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # prepare handler
 | 
				
			||||||
 | 
					    def folder_handler(src_folder: str, dst_folder: str) -> None:
 | 
				
			||||||
 | 
					        # just create folder
 | 
				
			||||||
 | 
					        print(f'Creating Folder: {src_folder} -> {dst_folder}')
 | 
				
			||||||
 | 
					        os.makedirs(dst_folder, exist_ok = True)
 | 
				
			||||||
 | 
					    def file_handler(src_file: str, dst_file: str) -> None:
 | 
				
			||||||
 | 
					        # skip non-json
 | 
				
			||||||
 | 
					        if not src_file.endswith('.json'): return
 | 
				
			||||||
 | 
					        # call compress func
 | 
				
			||||||
 | 
					        print(f'Processing Json: {src_file} -> {dst_file}')
 | 
				
			||||||
 | 
					        compress_json(src_file, dst_file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # call common processor
 | 
				
			||||||
 | 
					    common.common_file_migrator(
 | 
				
			||||||
 | 
					        os.path.join(root_folder, 'raw_jsons'),
 | 
				
			||||||
 | 
					        os.path.join(root_folder, 'jsons'),
 | 
				
			||||||
 | 
					        folder_handler,
 | 
				
			||||||
 | 
					        file_handler
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    print('Done.')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
					    create_compressed_jsons()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							
								
								
									
										57
									
								
								bbp_ng/tools/common.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								bbp_ng/tools/common.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,57 @@
 | 
				
			|||||||
 | 
					import os, typing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def relative_to_folder(abs_path: str, src_parent: str, dst_parent: str) -> str:
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Rebase one path to another path.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Give a absolute file path and folder path, and compute the relative path of given file to given folder.
 | 
				
			||||||
 | 
					    Then applied the computed relative path to another given folder path.
 | 
				
			||||||
 | 
					    Thus it seems like the file was rebased to from a folder to another folder with keeping the folder hierarchy.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    For example, given `/path/to/file` and `/path`, it will compute relative path `to/file`.
 | 
				
			||||||
 | 
					    Then it was applied to another folder path `/new` and got `/new/to/file`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @param abs_path[in] The absolute path to a folder or file.
 | 
				
			||||||
 | 
					    @param src_parent[in] The absolute path to folder which the `abs_path` will have relative path to.
 | 
				
			||||||
 | 
					    @param dst_parent[in] The absolute path to folder which the relative path will be applied to.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    return os.path.join(dst_parent, os.path.relpath(abs_path, src_parent))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def common_file_migrator(
 | 
				
			||||||
 | 
					        from_folder: str, to_folder: str,
 | 
				
			||||||
 | 
					        fct_proc_folder: typing.Callable[[str, str], None],
 | 
				
			||||||
 | 
					        fct_proc_file: typing.Callable[[str, str], None]) -> None:
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Common file migrator used by some build script.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    This function receive 2 absolute folder path. `from_folder` indicate the file migrated out,
 | 
				
			||||||
 | 
					    and `to_folder` indicate the file migrated in.
 | 
				
			||||||
 | 
					    `fct_proc_folder` is a function pointer from caller which handle folder migration in detail.
 | 
				
			||||||
 | 
					    `fct_proc_file` is same but handle file migration.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    `fct_proc_folder` will receive 2 args. First is the source folder. Second is expected dest folder.
 | 
				
			||||||
 | 
					    `fct_proc_file` is same, but receive the file path instead.
 | 
				
			||||||
 | 
					    Both of these function pointer should do the migration in detail. This function will only just iterate
 | 
				
			||||||
 | 
					    folder and give essential args and will not do any migration operations such as copying or moving.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @param from_folder[in] The folder need to be migrated.
 | 
				
			||||||
 | 
					    @param to_folder[in] The folder will be migrated to.
 | 
				
			||||||
 | 
					    @param fct_proc_folder[in] Folder migration detail handler.
 | 
				
			||||||
 | 
					    @param fct_proc_file[in] File migration detail handler.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    # iterate from_folder folder
 | 
				
			||||||
 | 
					    for root, dirs, files in os.walk(from_folder, topdown = True):
 | 
				
			||||||
 | 
					        # iterate folders
 | 
				
			||||||
 | 
					        for name in dirs:
 | 
				
			||||||
 | 
					            # prepare handler args
 | 
				
			||||||
 | 
					            src_folder: str = os.path.join(root, name)
 | 
				
			||||||
 | 
					            dst_folder: str = relative_to_folder(src_folder, from_folder, to_folder)
 | 
				
			||||||
 | 
					            # call handler
 | 
				
			||||||
 | 
					            fct_proc_folder(src_folder, dst_folder)
 | 
				
			||||||
 | 
					        # iterate files
 | 
				
			||||||
 | 
					        for name in files:
 | 
				
			||||||
 | 
					            # prepare handler args
 | 
				
			||||||
 | 
					            src_file: str = os.path.join(root, name)
 | 
				
			||||||
 | 
					            dst_file: str = relative_to_folder(src_file, from_folder, to_folder)
 | 
				
			||||||
 | 
					            # call handler
 | 
				
			||||||
 | 
					            fct_proc_file(src_file, dst_file)
 | 
				
			||||||
		Reference in New Issue
	
	Block a user