libmpq-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ *.bundle
2
+ pkg/*
@@ -0,0 +1,17 @@
1
+ ## libmpq-ruby
2
+
3
+ A very basic wrapper to the [libmpq](https://libmpq.org/) library. It allows you to read, decrypt and unpack MPQ files.
4
+
5
+ ### Usage
6
+
7
+ require 'mpq'
8
+
9
+ archive = MPQ("test/fixtures/replay.SC2replay")
10
+ # => #<MPQ::Archive:0x1003732b8 @path="test/fixtures/replay.SC2replay">
11
+
12
+ # list the files in the archive
13
+ archive.manifest
14
+ # => ["replay.attributes.events", "replay.details", ... truncated for legibility
15
+
16
+ archive.read_file('replay.message.events')
17
+ # => "\000\"\200\000\000\034\000\000 ... truncated for legibility
@@ -0,0 +1,50 @@
1
+ require 'rake/clean'
2
+ require 'digest/md5'
3
+
4
+ DLEXT = Config::MAKEFILE_CONFIG['DLEXT']
5
+ RUBYDIGEST = Digest::MD5.hexdigest(`#{RUBY} --version`)
6
+
7
+ file "ext/ruby-#{RUBYDIGEST}" do |f|
8
+ rm_f FileList["ext/ruby-*"]
9
+ touch f.name
10
+ end
11
+
12
+ CLEAN.include "ext/ruby-*"
13
+
14
+ file 'ext/Makefile' => FileList['ext/*.{c,h,rb}', "ext/ruby-#{RUBYDIGEST}"] do
15
+ chdir('ext') { ruby 'extconf.rb' }
16
+ end
17
+ CLEAN.include 'ext/Makefile', 'ext/mkmf.log'
18
+
19
+ file "ext/mpq_archive.#{DLEXT}" => FileList["ext/Makefile"] do |f|
20
+ sh 'cd ext && make clean && make && rm -rf conftest.dSYM'
21
+ end
22
+ CLEAN.include 'ext/*.{o,bundle,so,dll}'
23
+
24
+ file "lib/mpq_archive.#{DLEXT}" => "ext/mpq_archive.#{DLEXT}" do |f|
25
+ cp f.prerequisites, "lib/", :preserve => true
26
+ end
27
+
28
+ namespace :ext do
29
+ desc 'Build the mpq extension'
30
+ task :build => "lib/mpq_archive.#{DLEXT}"
31
+ end
32
+
33
+ # PACKAGING ============
34
+
35
+ require 'rubygems'
36
+ begin
37
+ require 'jeweler'
38
+ Jeweler::Tasks.new do |gem|
39
+ gem.name = "libmpq-ruby"
40
+ gem.summary = %Q{ Library for reading MPQ files using libmpq }
41
+ gem.description = %Q{ Uses libmpq to provie access to read, decrypting and unpacking
42
+ MPQ files in Ruby }
43
+ gem.email = "beaucollins@gmail.com"
44
+ gem.homepage = "http://github.com/beaucollins/libmpq-ruby"
45
+ gem.authors = ["Beau Collins"]
46
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
47
+ end
48
+ rescue LoadError
49
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,3 @@
1
+ *.o
2
+ Makefile
3
+ ruby-*
@@ -0,0 +1,13 @@
1
+ require 'mkmf'
2
+
3
+ if have_library('mpq')
4
+ find_library('mpq', '/usr/local/lib')
5
+ end
6
+
7
+ if have_header('mpq.h')
8
+ puts "no prefix"
9
+ elsif have_header('libmpq/mpq.h')
10
+ puts "prefix libmpq"
11
+ end
12
+
13
+ create_makefile('mpq_archive')
@@ -0,0 +1,70 @@
1
+ #include <stdio.h>
2
+ #include <libmpq/mpq.h>
3
+ #include "ruby.h"
4
+
5
+ static VALUE rb_mMPQ;
6
+ static VALUE rb_cMPQArchive;
7
+
8
+
9
+ static VALUE rb_mpq_archive_read_file(VALUE self, VALUE name){
10
+
11
+ // archive struct
12
+ mpq_archive_s *archive;
13
+ // stores the index number for "(listfile)"
14
+ unsigned int file_index;
15
+ // stores the contents of the "(listfile)"
16
+ char *contents;
17
+ // stores the file size for string malloc
18
+ off_t file_size;
19
+ // retrieve the file path from the class
20
+ VALUE file_path = rb_funcall(self, rb_intern("path"), 0);
21
+ // make sure we have a string
22
+ Check_Type(file_path, T_STRING);
23
+ Check_Type(name, T_STRING);
24
+
25
+ VALUE buf = rb_str_buf_new(1024);
26
+
27
+ // start your engines
28
+ libmpq__init();
29
+ int return_code;
30
+ if(return_code = libmpq__archive_open(&archive, RSTRING_PTR(file_path), -1) != 0)
31
+ {
32
+ // raise an error? or just return the non-zero int?
33
+ return INT2FIX(return_code) ;
34
+ }
35
+
36
+ // get filenumber and size for listfile
37
+ if (return_code = libmpq__file_number(archive, RSTRING_PTR(name), &file_index) != 0) {
38
+ printf("No %s in '%s'.\n", RSTRING_PTR(name), RSTRING_PTR(file_path));
39
+ libmpq__archive_close(archive);
40
+ return INT2FIX(return_code);
41
+ }
42
+
43
+ // we've succesffully opene the archive
44
+ libmpq__file_unpacked_size(archive, file_index, &file_size);
45
+
46
+ // read listfile content into memory
47
+ contents = malloc(file_size);
48
+
49
+ libmpq__file_read(archive, file_index, contents, file_size, NULL);
50
+
51
+ rb_str_cat(buf, contents, file_size);
52
+
53
+ free(contents);
54
+
55
+ // free up your shit
56
+ libmpq__shutdown();
57
+
58
+ return buf;
59
+
60
+ }
61
+
62
+
63
+ void Init_mpq_archive(){
64
+
65
+ rb_mMPQ = rb_define_module("MPQ");
66
+ rb_cMPQArchive = rb_define_class_under(rb_mMPQ, "Archive", rb_cObject);
67
+
68
+ rb_define_method(rb_cMPQArchive, "read_file", rb_mpq_archive_read_file, 1);
69
+
70
+ }
@@ -0,0 +1,27 @@
1
+
2
+ require 'mpq_archive'
3
+
4
+
5
+ class MPQ::Archive
6
+
7
+ attr_reader :path
8
+
9
+ LISTFILE = "(listfile)"
10
+
11
+ def initialize(path)
12
+ @path = path
13
+ end
14
+
15
+ def listfile
16
+ read_file LISTFILE
17
+ end
18
+
19
+ def manifest
20
+ listfile.split("\n").collect(&:strip)
21
+ end
22
+
23
+ end
24
+
25
+ def MPQ(file)
26
+ MPQ::Archive.new(file)
27
+ end
@@ -0,0 +1,48 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{libmpq-ruby}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Beau Collins"]
12
+ s.date = %q{2010-12-30}
13
+ s.description = %q{ Uses libmpq to provie access to read, decrypting and unpacking
14
+ MPQ files in Ruby }
15
+ s.email = %q{beaucollins@gmail.com}
16
+ s.extensions = ["ext/extconf.rb"]
17
+ s.extra_rdoc_files = [
18
+ "README.markdown"
19
+ ]
20
+ s.files = [
21
+ ".gitignore",
22
+ "README.markdown",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "ext/.gitignore",
26
+ "ext/extconf.rb",
27
+ "ext/mpq_archive.c",
28
+ "lib/mpq.rb",
29
+ "libmpq-ruby.gemspec",
30
+ "test/fixtures/replay.SC2Replay"
31
+ ]
32
+ s.homepage = %q{http://github.com/beaucollins/libmpq-ruby}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{Library for reading MPQ files using libmpq}
37
+
38
+ if s.respond_to? :specification_version then
39
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ else
44
+ end
45
+ else
46
+ end
47
+ end
48
+
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: libmpq-ruby
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Beau Collins
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-30 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: " Uses libmpq to provie access to read, decrypting and unpacking\n MPQ files in Ruby "
23
+ email: beaucollins@gmail.com
24
+ executables: []
25
+
26
+ extensions:
27
+ - ext/extconf.rb
28
+ extra_rdoc_files:
29
+ - README.markdown
30
+ files:
31
+ - .gitignore
32
+ - README.markdown
33
+ - Rakefile
34
+ - VERSION
35
+ - ext/.gitignore
36
+ - ext/extconf.rb
37
+ - ext/mpq_archive.c
38
+ - lib/mpq.rb
39
+ - libmpq-ruby.gemspec
40
+ - test/fixtures/replay.SC2Replay
41
+ has_rdoc: true
42
+ homepage: http://github.com/beaucollins/libmpq-ruby
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Library for reading MPQ files using libmpq
75
+ test_files: []
76
+