nuri_game-tsx_reader 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4c4b8006d2c3562328037fbd925f9c85c02932d690117c6b0fbe5185ae99c1a5
4
+ data.tar.gz: 2bee46cacff8dc0bb8acdad254f86c18cdd7b1e37337b471d17d1fb2aa354d96
5
+ SHA512:
6
+ metadata.gz: cfd6db6e7a381d833899c89fcfa6a31acfd198c2b3b3a80cca4690072279df9b6152b94518318ddbde27ca94a369ac5761f9a8a43123f92646ee0882a7489b47
7
+ data.tar.gz: fe3cefcd9144e3aea3e92eac03225df2e8e34b05bd98c9f8b7decc9b1402342ff71d4a06cd737f798427dd7f7e2e77163a00412e6a7c9b8bcff5aabb0da3444e
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in nuri_game-tsx_reader.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # NuriGame::TsxReader
2
+
3
+ TSXReader that convert .tsx files to a ruby object
4
+
5
+ ## Note
6
+
7
+ Currently the following feature of tileset aren't supported :
8
+ * animation
9
+ * wangsets
10
+ * grid
11
+ * tile
12
+
13
+ (I don't even know how some works and I couldn't understand so you're free to help for that ^^')
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'nuri_game-tsx_reader'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install nuri_game-tsx_reader
30
+
31
+ ## Usage
32
+
33
+ ```ruby
34
+ require 'nuri_game/tsx_reader'
35
+
36
+ # [...]
37
+
38
+ tileset = NuriGame::TsxReader.new('tilesetname.tsx')
39
+ # access here to tileset properties (see doc)
40
+ ```
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests at https://gitlab.com/NuriYuri/nuri_game-tsx_reader.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nuri_game/tsx_reader"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,120 @@
1
+ require 'nuri_game/tsx_reader/version'
2
+ require 'rexml/document'
3
+ require 'zlib'
4
+ require 'base64'
5
+
6
+ # NuriGame namespace containing all the utility made by Nuri Yuri in order to make Games.
7
+ module NuriGame
8
+ # class that converts TSX file to Ruby object
9
+ class TsxReader
10
+ # Definition of an image
11
+ Image = Struct.new(:format, :source, :trans, :width, :height, :data)
12
+
13
+ # Constant holding the list of tileset information
14
+ TILESET_INFOS = %w[tilewidth tileheight tilecount columns spacing margin].freeze
15
+
16
+ # @return [String] name of the tileset
17
+ attr_reader :name
18
+
19
+ # @return [Integer] number of tiles in the tileset
20
+ attr_reader :tilecount
21
+
22
+ # @return [Integer] number of columns in the tileset
23
+ attr_reader :columns
24
+
25
+ # @return [Integer] width of a tile on the tileset
26
+ attr_reader :tilewidth
27
+
28
+ # @return [Integer] height of a tile on the tileset
29
+ attr_reader :tileheight
30
+
31
+ # @return [Integer] space in px between tiles in the tileset image
32
+ attr_reader :spacing
33
+
34
+ # @return [Integer] margin around the tile of the tileset
35
+ attr_reader :margin
36
+
37
+ # @return [Hash{String => Integer}] terrains of the tileset by name
38
+ attr_reader :terrains
39
+
40
+ # @return [Image] image used in the tileset
41
+ attr_reader :image
42
+
43
+ # Create a new TmxReader
44
+ # @param filename [String] name of the tmx file to read
45
+ def initialize(filename)
46
+ contents = File.read(filename).force_encoding(Encoding::UTF_8)
47
+ tileset = REXML::Document.new(contents).root
48
+ @terrains = {}
49
+ validate_tileset(tileset)
50
+ load_tileset_infos(tileset)
51
+ load_tileset_image(tileset)
52
+ load_tileset_terrains(tileset)
53
+ end
54
+
55
+ private
56
+
57
+ # Validate the tileset informations
58
+ # @parma tileset [REXML::Element]
59
+ def validate_tileset(tileset)
60
+ raise 'Invalid TSX data, root element is not a <tileset>' if tileset.name != 'tileset'
61
+ end
62
+
63
+ # Load the tileset informations
64
+ # @param tileset [REXML::Element]
65
+ def load_tileset_infos(tileset)
66
+ @name = xml_value(tileset, 'name').to_s
67
+ TILESET_INFOS.each { |name| instance_variable_set(:"@#{name}", xml_value(tileset, name).to_i) }
68
+ end
69
+
70
+ # Load the image of the tileset
71
+ # @param tileset [REXML::Element]
72
+ def load_tileset_image(tileset)
73
+ image = REXML::XPath.first(tileset, 'image')
74
+ data = REXML::XPath.first(image, 'data')
75
+ data = load_image_data(data)
76
+ @image = Image.new(
77
+ xml_value(image, 'format'),
78
+ xml_value(image, 'source'),
79
+ xml_value(image, 'trans'),
80
+ xml_value(image, 'width').to_i,
81
+ xml_value(image, 'height').to_i,
82
+ data
83
+ )
84
+ end
85
+
86
+ # Load the terrains of the tileset
87
+ # @param tileset [REXML::Element]
88
+ def load_tileset_terrains(tileset)
89
+ tileset.each_element('terraintypes/terrain') do |terrain|
90
+ @terrains[xml_value(terrain, 'name')] = xml_value(terrain, 'tile').to_i
91
+ end
92
+ end
93
+
94
+ # Load the image data of the tileset
95
+ # @param data [REXML::Element]
96
+ def load_image_data(data)
97
+ return unless data
98
+ raw_data = data.text.strip
99
+ enc = xml_value(data, 'encoding')
100
+ comp = xml_value(data, 'compression')
101
+ raw_data = Base64.decode64(raw_data) if enc == 'base64'
102
+ case comp
103
+ when 'zlib'
104
+ return Zlib::Inflate.inflate(raw_data)
105
+ when 'gzip'
106
+ return Zlib::GzipReader.new(StringIO.new(raw_data)).read
107
+ end
108
+ return raw_data
109
+ end
110
+
111
+ # Return an xml value
112
+ # @param element [REXML::Element]
113
+ # @param value_name [String]
114
+ # @return [String, nil] nil means no value
115
+ def xml_value(element, value_name)
116
+ val = element.attribute(value_name)
117
+ return val ? val.value : nil
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,5 @@
1
+ module NuriGame
2
+ class TsxReader
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "nuri_game/tsx_reader/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nuri_game-tsx_reader"
8
+ spec.version = NuriGame::TsxReader::VERSION
9
+ spec.authors = ["Nuri Yuri"]
10
+ spec.email = ["hostmaster@pokemonworkshop.com"]
11
+
12
+ spec.summary = %q{TSXReader that convert .tsx files to a ruby object}
13
+ spec.homepage = "https://gitlab.com/NuriYuri/nuri_game-tsx_reader"
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+ spec.required_ruby_version = '>= 2.5.0'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nuri_game-tsx_reader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nuri Yuri
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - hostmaster@pokemonworkshop.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - bin/console
53
+ - bin/setup
54
+ - lib/nuri_game/tsx_reader.rb
55
+ - lib/nuri_game/tsx_reader/version.rb
56
+ - nuri_game-tsx_reader.gemspec
57
+ homepage: https://gitlab.com/NuriYuri/nuri_game-tsx_reader
58
+ licenses: []
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 2.5.0
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.7.3
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: TSXReader that convert .tsx files to a ruby object
80
+ test_files: []