diff --git a/cbscript.py b/cbscript.py index d7d4507..d3ceb74 100644 --- a/cbscript.py +++ b/cbscript.py @@ -97,7 +97,7 @@ def compile_all(self): if type != 'program': self.log('Script does not contain a full program.') return False - + self.global_context = global_context.global_context(self.namespace) global_environment = environment(self.global_context) global_environment.set_dollarid('namespace', self.namespace) @@ -142,6 +142,7 @@ def compile_all(self): world.write_loot_tables(self.global_context.loot_tables) world.write_predicates(self.global_context.predicates) world.write_item_modifiers(self.global_context.item_modifiers) + world.write_data(parsed['data']) world.write_zip() self.dependencies = [source_file(d) for d in self.global_context.dependencies] diff --git a/mcworld.py b/mcworld.py index 6b0bef8..90611ff 100644 --- a/mcworld.py +++ b/mcworld.py @@ -22,7 +22,7 @@ def get_latest_log_file(self): logfile = os.path.join(logsdir, 'latest.log') return logfile - + def write_functions(self, functions): function_dir = f'data/{self.namespace}/functions/' @@ -113,6 +113,21 @@ def write_mcmeta(self, desc): self.zip.writestr(mcmeta_file, json.dumps({'pack':{'pack_format':1, 'description':desc}}, indent=4)) + def write_data(self, data): + data_dir = 'data' + if data is not None: + for subdir, dirs, files in os.walk(data): + for file in files: + data_copy_path = os.path.join(subdir, file) + + data_raw_path = os.path.relpath(data_copy_path, data) + data_zip_path = os.path.join(data_dir, data_raw_path) + if data_zip_path.replace(os.sep, '/') not in self.zip.namelist(): + with open(data_copy_path, "rb") as file: + file_bytes = file.read() + self.zip.writestr(data_zip_path, file_bytes) + + def write_zip(self): self.zip.close() diff --git a/scriptlex.py b/scriptlex.py index d918408..d345f04 100644 --- a/scriptlex.py +++ b/scriptlex.py @@ -8,7 +8,7 @@ 'reset', 'clock', 'function', 'if', 'unless', 'then', 'do', 'else', 'switch', 'case', 'default', 'return', 'while', 'macro', 'block', 'block_data', 'block_tag', 'entity_tag', 'item_tag', 'define', 'array', 'remove', 'success', 'result', 'shaped', 'recipe', 'keys', 'eyes', 'feet', 'advancement', 'loot_table', 'predicate', "item_modifier", - 'push', 'pop', 'true', 'false', + 'push', 'pop', 'true', 'false', 'data' ) tokens = keywords + ( diff --git a/scriptparse.py b/scriptparse.py index 9f7a414..186c04f 100644 --- a/scriptparse.py +++ b/scriptparse.py @@ -116,15 +116,16 @@ def p_parsed_expr(p): #### Program def p_program(p): - '''program : optcomments dir string newlines optdesc file_params top_level_blocks''' + '''program : optcomments dir string newlines optdesc optdata file_params top_level_blocks''' p[0] = {} p[0]['dir'] = p[3] p[0]['desc'] = p[5] + p[0]['data'] = p[6] - for param_name in p[6]: - p[0][param_name] = p[6][param_name] + for param_name in p[7]: + p[0][param_name] = p[7][param_name] - p[0]['lines'] = p[7] + p[0]['lines'] = p[8] ### Lib def p_lib(p): @@ -162,7 +163,15 @@ def p_file_param(p): raise SyntaxError(f'Unknown file parameter: "{p[1]}" at line {p.lineno(1)}') p[0] = (p[1], int(p[2])) - + +def p_optdata(p): + '''optdata : data string newlines + | empty''' + if len(p) < 4: + p[0] = None + else: + p[0] = p[2] + #### Sections def p_section_commented(p): '''section_commented : optcomments section'''