Class: Pixiurge::Middleware::TmxJson

Inherits:
Object
  • Object
show all
Defined in:
lib/pixiurge/config_ru.rb

Overview

The TmxJson middleware reads TMX files in the normal XML mode that Tiled loads and saves easily, but serves it in the AJAX-friendly JSON format that Tiled exports. This allows JavaScript-friendly usage directly from a standard XML TMX file without the manual export step every time.

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ TmxJson

Constructor. Rack has a standard format for these, though the options are specific to the TmxJson middleware.

Parameters:

  • app (Rack::App)

    The next innermost Rack app

  • options (Hash) (defaults to: {})

    Options to this middleware

Options Hash (options):

  • :urls (String, Array<String>)

    A root or list of URL roots to serve TMX files from

  • :root (String)

    The file system root to serve from (default: Dir.pwd)

  • :cache (Demiurge::Tmx::TileCache)

    The tile cache to serve from; use this option to share or clear the cache from elsewhere

Since:

  • 0.1.0



204
205
206
207
208
209
# File 'lib/pixiurge/config_ru.rb', line 204

def initialize(app, options = {})
  @app = app
  @urls = [(options[:urls] || "/tmx")].flatten
  @root = options[:root] || Dir.pwd
  @cache = options[:cache] || ::Demiurge::Tmx::TmxCache.new(:root_dir => @root)
end

Instance Method Details

#call(env) ⇒ Object

The Rack .call method for middleware.

Parameters:

  • env (Hash)

    The Rack environment hash.

Since:

  • 0.1.0



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/pixiurge/config_ru.rb', line 215

def call(env)
  # If no TMX path is matched, forward the call to the next middleware
  call_root = matches_url(env["PATH_INFO"])
  return @app.call(env) unless call_root

  # Okay, a TMX directory was matched...

  local_path = File.join(@root, env["PATH_INFO"])

  # Check for a .tmx.json, .manasource.json or .conv.json
  json_local_path = local_path.sub(/\.([^.]+)\.json$/, ".tmx")
  subformat = $1
  existing = [local_path, json_local_path].detect { |f| File.exists?(f) }
  unless existing
    return [404, {}, [""]]
  else
    if existing == local_path
      # Whatever it is, it's a path that literally exists right there.
      return [ 200, {}, Rack::File.new(existing) ]
    elsif subformat == "conv"
      tmx_map = Tmx.load(existing)
      json_contents = tmx_map.export_to_string :filename => existing, :format => :json
      return [200, { "type" => "application/json" }, [ json_contents ] ]
    elsif subformat == "tmx"
      return [200, { "type" => "application/json" }, [ MultiJson.dump(@cache.tmx_entry("tmx", existing)) ] ]
    elsif subformat == "tmxpretty"
      return [200, { "type" => "application/json" }, [ MultiJson.dump(@cache.tmx_entry("tmx", existing), :pretty => true) ] ]
    elsif subformat == "manasource"
      return [200, { "type" => "application/json" }, [ MultiJson.dump(@cache.tmx_entry("manasource", existing)) ] ]
    elsif subformat == "manasourcepretty"
      return [200, { "type" => "application/json" }, [ MultiJson.dump(@cache.tmx_entry("manasource", existing), :pretty => true) ] ]
    else
      # Okay, no clue what they're asking for even though we have a matching .tmx file.
      return [404, {}, [""]]
    end
  end
end