Commits

Chris Klimas  committed 3e66bea

Handle .. in asset pathnames

This resolves bug 16.

  • Participants
  • Parent commits f4db9ea

Comments (0)

Files changed (2)

File tests/spritetypes.lua

 SpriteTypes = TestApp:extend
 {
 	title = 'Sprite Types',
-	blueGem = 'tests/assets/bluegem.png',
-	chestAnim = 'tests/assets/animation.png',
+
+	-- test .. in paths
+	blueGem = 'tests/assets/../assets/bluegem.png',
+	chestAnim = 'tests/assets/../../tests/assets/animation.png',
 	
 	onNew = function (self)
 		print 'This demonstrates the different types of sprites available in Zoetrope.'

File zoetrope/core/cached.lua

 	--		Love image object
 
 	image = function (self, path)
-		assert(type(path) == 'string', 'path must be a string')
-
-		if not self._library.image[path] then
-			self._library.image[path] = love.graphics.newImage(path)
+		if STRICT then
+			assert(type(path) == 'string', 'path must be a string')
 		end
 
-		return self._library.image[path]
+		local realPath = self:_absolutePath(path)
+
+		if not self._library.image[realPath] then
+			self._library.image[realPath] = love.graphics.newImage(realPath)
+		end
+
+		return self._library.image[realPath]
 	end,
 
 	-- Method: text
 	--		string
 
 	text = function (self, path)
-		assert(type(path) == 'string', 'path must be a string')
-
-		if not self._library.text[path] then
-			self._library.text[path] = love.filesystem.read(path)
+		if STRICT then
+			assert(type(path) == 'string', 'path must be a string')
 		end
 
-		return self._library.text[path]
+		local realPath = self:_absolutePath(path)
+
+		if not self._library.text[realPath] then
+			self._library.text[realPath] = love.filesystem.read(realPath)
+		end
+
+		return self._library.text[realPath]
 	end,
 
 	-- Method: sound
 	--		<playSound>, <sound>
 
 	sound = function (self, path, length)
-		assert(type(path) == 'string', 'path must be a string')
+		if STRICT then
+			assert(type(path) == 'string', 'path must be a string')
+		end
 
-		if not self._library.sound[path] then
+		local realPath = self:_absolutePath(path)
+
+		if not self._library.sound[realPath] then
 			if length == 'short' then
-				self._library.sound[path] = love.sound.newSoundData(path)
+				self._library.sound[realPath] = love.sound.newSoundData(realPath)
 			elseif length == 'long' then
-				self._library.sound[path] = love.sound.newDecoder(path)
+				self._library.sound[realPath] = love.sound.newDecoder(realPath)
 			else
 				error('length must be either "short" or "long"')
 			end
 		local arg = {...}
 		local libKey = arg[1]
 
+		if type(libKey) == 'string' then
+			libKey = self:_absolutePath(libKey)
+		end
+
 		if #arg > 1 then libKey = libKey .. arg[2] end
 
 		if not self._library.font[libKey] then
 	
 		self._library.binds[{func, obj, arg}] = result
 		return result
+	end,
+
+	-- internal function: _absolutePath
+	-- Replaces any .. references in a path, where possible. 
+	--
+	-- Arguments:
+	--		rawPath - string path to expand
+	--
+	-- Returns:
+	--		string
+
+	_absolutePath = function (self, rawPath)
+		local matches
+		local result = rawPath
+
+		repeat
+			result, matches = string.gsub(result, '[^/]+/%.%./', '')
+			print(result)
+		until matches == 0
+
+		return result
 	end
 }