Pārlūkot izejas kodu

Add Repository and Compiler python libraries

Daniele Bartolini 12 gadi atpakaļ
vecāks
revīzija
4b2b94e153

+ 20 - 56
tools/compilers/resource-compiler.py

@@ -1,55 +1,12 @@
 #!/bin/python
 
 import os
-import subprocess
 
-resource_extensions = ('.txt', '.tga', '.dae', '.lua')
-resources = []
-resource_hashes = []
-root_path = "/home/dani/test/resources"
+from crown.resources import Repository
+from crown.resources import Compiler
 
-#------------------------------------------------------------------------------
-def read_resources(root_path):
-	for dirname, dirnames, filenames in os.walk(root_path):
-
-		# print path to all filenames.
-		for filename in filenames:
-	
-			# get the resource name
-			abs_path = os.path.join(dirname, filename)
-			resource = os.path.relpath(abs_path, root_path)
-
-			# filter resource names by type
-			if resource.endswith(resource_extensions):
-				resources.append(resource)
+root_path = "/home/daniele/resources"
 
-#------------------------------------------------------------------------------
-def perfect_seed(resources):
-	seed = 0
-
-	while True:
-		for res in resources:
-			p = subprocess.check_output(["resource-hash", "--resource-in", res, "--seed", str(seed)])
-			resource_hashes.append(str(p))
-
-		if len(resource_hashes) == len(set(resource_hashes)):
-			return seed
-		
-		resource_hashes.clear()
-		seed = seed + 1;
-
-#------------------------------------------------------------------------------
-def compile(dest_path, resource, perfect_seed):
-	if resource.endswith('.txt'):
-		p = subprocess.call(["txt-compiler", "--root-path", root_path, "--dest-path", dest_path, "--resource-in", resource, "--seed", str(perfect_seed)]);
-	if resource.endswith('.tga'):
-		p = subprocess.call(["tga-compiler", "--root-path", root_path, "--dest-path", dest_path, "--resource-in", resource, "--seed", str(perfect_seed)]);	
-
-#------------------------------------------------------------------------------
-def compile_all(dest_path, resources, perfect_seed):
-	for res in resources:
-		compile(dest_path, res, perfect_seed)
-		
 #------------------------------------------------------------------------------
 def create_output_folder(root_path):
 	output_path = os.path.dirname(root_path) + "/" + os.path.basename(root_path) + "_compiled"
@@ -69,22 +26,29 @@ def write_perfect_seed(dest_path, perfect_seed):
 	file.write(str(perfect_seed))
 	file.close()
 
-#------------------------------------------------------------------------------
-print("Reading resources...\n")
-read_resources(root_path)
+repository = Repository.Repository(root_path)
 
-perfect_seed = perfect_seed(resources)
+#------------------------------------------------------------------------------
+print("Reading resources...")
+repository.scan()
 
-print("Perfect seed is: ", perfect_seed)
+print("Found", len(repository.all_resources()), "resources.")
 
-print("Creating output folder...\n")
+print("Creating output folder...")
 dest_path = create_output_folder(root_path)
 
-print("Writing perfect seed...\n")
-write_perfect_seed(dest_path, perfect_seed)
+compiler = Compiler.Compiler(repository, dest_path)
+compiler.compute_perfect_seed()
+
+print("Compiling resources...")
+
+print("Perfect seed is: ", compiler.perfect_seed())
+
+print("Writing resources...")
+compiler.compile_all()
 
-print("Compiling resources...\n")
-compile_all(dest_path, resources, perfect_seed)
+print("Writing perfect seed...")
+write_perfect_seed(dest_path, compiler.perfect_seed())
 
 print("\n")
 

+ 0 - 0
tools/crown/__init__.py


+ 99 - 0
tools/crown/resources/Compiler.py

@@ -0,0 +1,99 @@
+# Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+# Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+#
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this software and associated documentation
+# files (the "Software"), to deal in the Software without
+# restriction, including without limitation the rights to use,
+# copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following
+# conditions:
+# 
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+# 
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+# OTHER DEALINGS IN THE SOFTWARE.
+
+import subprocess
+
+TXT_C = "txt-compiler"
+TGA_C = "tga-compiler"
+RES_H = "resource-hash"
+
+ROOT_P = "--root-path"
+DEST_P = "--dest-path"
+RES_IN = "--resource-in"
+SEED = "--seed"
+
+# Compiler is an helper for compiling resources in a repository
+# either one by one or in a whole
+class Compiler:
+	def __init__(self, repository, dest_path):
+		self.m_repository = repository
+		self.m_perfect_seed = -1
+		self.m_dest_path = dest_path
+
+	# Returns the destination path where the compiled
+	# resources will be put
+	def dest_path(self):
+		return self.m_dest_path
+
+	# Returns the perfect seed
+	def perfect_seed(self):
+		return self.m_perfect_seed
+
+	# Computes a perfect seed for the resources contained
+	# in the repository
+	def compute_perfect_seed(self):
+		# Obtain resources from repository
+		resources = self.m_repository.all_resources()
+
+		resource_hashes = []
+
+		seed = 0
+
+		# Calculate the hash resource by resource
+		# Note: we can speed-up this process by modifying resource-hash to
+		# calculate more than a hash at time...
+		while True:
+			for res in resources:
+				p = subprocess.check_output([RES_H, RES_IN, res, SEED, str(seed)])
+				resource_hashes.append(str(p))
+
+			if len(resource_hashes) == len(set(resource_hashes)):
+				self.m_perfect_seed = seed
+				return
+
+			resource_hashes.clear()
+			seed = seed + 1;
+
+	# Compiles all the resources in the repository
+	def compile_all(self):
+		# Obtain resources from repository
+		resources = self.m_repository.all_resources()
+
+		for res in resources:
+			self.compile(res)
+
+	# Compile a single resource from the repository
+	def compile(self, resource):
+		# Compute perfect seed if necessary
+		if (self.m_perfect_seed == -1):
+			compute_perfect_seed()
+
+		root_path = self.m_repository.root_path()
+
+		# Call appropriate compiler based on resource extension
+		if resource.endswith('.txt'):
+			p = subprocess.call([TXT_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
+		if resource.endswith('.tga'):
+			p = subprocess.call([TGA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);	
+

+ 101 - 0
tools/crown/resources/Repository.py

@@ -0,0 +1,101 @@
+# Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+# Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+#
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this software and associated documentation
+# files (the "Software"), to deal in the Software without
+# restriction, including without limitation the rights to use,
+# copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following
+# conditions:
+# 
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+# 
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+# OTHER DEALINGS IN THE SOFTWARE.
+
+import os
+
+TEXTURE_EXTENSION = ('.tga')
+TEXT_EXTENSION = ('.txt')
+MESH_EXTENSION = ('.dae')
+LUA_EXTENSION = ('.lua')
+
+resource_extensions = ('.txt', '.tga', '.dae', '.lua')
+
+# Represents the folder containing the resources
+# Can filter resources by type and other useful suff
+class Repository:
+	def __init__(self, root_path):
+		self.m_root_path = root_path
+		self.m_resources = []
+
+	# Returns the root path
+	def root_path(self):
+		return self.m_root_path
+
+	# Returns a list of all the resources found
+	def all_resources(self):
+		return self.m_resources
+
+	# Returns a list of all the texture resources found
+	def texture_resources(self):
+		textures = []
+
+		for res in resources:
+			if (res.endswith(TEXTURE_EXTENSION)):
+				textures.append(res)
+
+		return textures
+
+	# Returns a list of all the text resources found
+	def text_resources(self):
+		texts = []
+
+		for res in resources:
+			if (res.endswith(TEXT_EXTENSION)):
+				texts.append(res)
+
+		return texts
+
+	# Returns a list of all the mesh resources found
+	def mesh_resources(self):
+		meshes = []
+
+		for res in resources:
+			if (res.endswith(MESH_EXTENSION)):
+				meshes.append(res)
+
+		return meshes
+
+	# Returns a list of all the lua resources found
+	def lua_resources(self):
+		luas = []
+
+		for res in resources:
+			if (res.endswith(LUA_EXTENSION)):
+				luas.append(res)
+
+		return luas
+
+	# Scans the root path to find resources
+	def scan(self):
+
+		for dirname, dirnames, filenames in os.walk(self.m_root_path):
+			for filename in filenames:
+				# Get the resource name
+				abs_path = os.path.join(dirname, filename)
+				resource_name = os.path.relpath(abs_path, self.m_root_path)
+
+				# Filter resource names by type
+				if resource_name.endswith(resource_extensions):
+					self.m_resources.append(resource_name)
+

+ 0 - 0
tools/crown/resources/__init__.py