Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 07dfe03
Showing
10 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.bundle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## libmpq-ruby | ||
|
||
A very basic wrapper to the [libmpq](https://libmpq.org/) library. It allows you to read, decrypt and unpack MPQ files. | ||
|
||
### Usage | ||
|
||
require 'mpq' | ||
|
||
archive = MPQ("path/to/file.mpq") | ||
|
||
# list the files in the archive | ||
archive.manifest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'rake/clean' | ||
require 'digest/md5' | ||
|
||
DLEXT = Config::MAKEFILE_CONFIG['DLEXT'] | ||
RUBYDIGEST = Digest::MD5.hexdigest(`#{RUBY} --version`) | ||
|
||
file "ext/ruby-#{RUBYDIGEST}" do |f| | ||
rm_f FileList["ext/ruby-*"] | ||
touch f.name | ||
end | ||
|
||
CLEAN.include "ext/ruby-*" | ||
|
||
file 'ext/Makefile' => FileList['ext/*.{c,h,rb}', "ext/ruby-#{RUBYDIGEST}"] do | ||
chdir('ext') { ruby 'extconf.rb' } | ||
end | ||
CLEAN.include 'ext/Makefile', 'ext/mkmf.log' | ||
|
||
file "ext/mpq_archive.#{DLEXT}" => FileList["ext/Makefile"] do |f| | ||
sh 'cd ext && make clean && make && rm -rf conftest.dSYM' | ||
end | ||
CLEAN.include 'ext/*.{o,bundle,so,dll}' | ||
|
||
file "lib/mpq_archive.#{DLEXT}" => "ext/mpq_archive.#{DLEXT}" do |f| | ||
cp f.prerequisites, "lib/", :preserve => true | ||
end | ||
|
||
namespace :ext do | ||
desc 'Build the mpq extension' | ||
task :build => "lib/mpq_archive.#{DLEXT}" | ||
end | ||
|
||
# PACKAGING ============ | ||
|
||
require 'rubygems' | ||
begin | ||
require 'jeweler' | ||
Jeweler::Tasks.new do |gem| | ||
gem.name = "libmpq-ruby" | ||
gem.summary = %Q{ Library for reading MPQ files using libmpq } | ||
gem.description = %Q{ Uses libmpq to provie access to read, decrypting and unpacking | ||
MPQ files in Ruby } | ||
gem.email = "beaucollins@gmail.com" | ||
gem.homepage = "http://github.com/beaucollins/libmpq-ruby" | ||
gem.authors = ["Beau Collins"] | ||
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings | ||
end | ||
rescue LoadError | ||
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.o | ||
Makefile | ||
ruby-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'mkmf' | ||
|
||
if have_library('mpq') | ||
find_library('mpq', '/usr/local/lib') | ||
end | ||
|
||
if have_header('mpq.h') | ||
puts "no prefix" | ||
elsif have_header('libmpq/mpq.h') | ||
puts "prefix libmpq" | ||
end | ||
|
||
create_makefile('mpq_archive') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <stdio.h> | ||
#include <libmpq/mpq.h> | ||
#include "ruby.h" | ||
|
||
static VALUE rb_mMPQ; | ||
static VALUE rb_cMPQArchive; | ||
|
||
|
||
static VALUE rb_mpq_archive_read_file(VALUE self, VALUE name){ | ||
|
||
// archive struct | ||
mpq_archive_s *archive; | ||
// stores the index number for "(listfile)" | ||
unsigned int file_index; | ||
// stores the contents of the "(listfile)" | ||
char *contents; | ||
// stores the file size for string malloc | ||
off_t file_size; | ||
// retrieve the file path from the class | ||
VALUE file_path = rb_funcall(self, rb_intern("path"), 0); | ||
// make sure we have a string | ||
Check_Type(file_path, T_STRING); | ||
Check_Type(name, T_STRING); | ||
|
||
VALUE buf = rb_str_buf_new(1024); | ||
|
||
// start your engines | ||
libmpq__init(); | ||
int return_code; | ||
if(return_code = libmpq__archive_open(&archive, RSTRING_PTR(file_path), -1) != 0) | ||
{ | ||
// raise an error? or just return the non-zero int? | ||
return INT2FIX(return_code) ; | ||
} | ||
|
||
// get filenumber and size for listfile | ||
if (return_code = libmpq__file_number(archive, RSTRING_PTR(name), &file_index) != 0) { | ||
printf("No %s in '%s'.\n", RSTRING_PTR(name), RSTRING_PTR(file_path)); | ||
libmpq__archive_close(archive); | ||
return INT2FIX(return_code); | ||
} | ||
|
||
// we've succesffully opene the archive | ||
libmpq__file_unpacked_size(archive, file_index, &file_size); | ||
|
||
// read listfile content into memory | ||
contents = malloc(file_size); | ||
|
||
libmpq__file_read(archive, file_index, contents, file_size, NULL); | ||
|
||
rb_str_cat(buf, contents, file_size); | ||
|
||
free(contents); | ||
|
||
// free up your shit | ||
libmpq__shutdown(); | ||
|
||
return buf; | ||
|
||
} | ||
|
||
|
||
void Init_mpq_archive(){ | ||
|
||
rb_mMPQ = rb_define_module("MPQ"); | ||
rb_cMPQArchive = rb_define_class_under(rb_mMPQ, "Archive", rb_cObject); | ||
|
||
rb_define_method(rb_cMPQArchive, "read_file", rb_mpq_archive_read_file, 1); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
require 'mpq_archive' | ||
|
||
|
||
class MPQ::Archive | ||
|
||
attr_reader :path | ||
|
||
LISTFILE = "(listfile)" | ||
|
||
def initialize(path) | ||
@path = path | ||
end | ||
|
||
def listfile | ||
read_file LISTFILE | ||
end | ||
|
||
def manifest | ||
listfile.split("\n").collect(&:strip) | ||
end | ||
|
||
end | ||
|
||
def MPQ(file) | ||
MPQ::Archive.new(file) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Generated by jeweler | ||
# DO NOT EDIT THIS FILE DIRECTLY | ||
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command | ||
# -*- encoding: utf-8 -*- | ||
|
||
Gem::Specification.new do |s| | ||
s.name = %q{libmpq-ruby} | ||
s.version = "0.0.0" | ||
|
||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= | ||
s.authors = ["Beau Collins"] | ||
s.date = %q{2010-12-30} | ||
s.description = %q{ Uses libmpq to provie access to read, decrypting and unpacking | ||
MPQ files in Ruby } | ||
s.email = %q{beaucollins@gmail.com} | ||
s.extensions = ["ext/extconf.rb"] | ||
s.extra_rdoc_files = [ | ||
"README.markdown" | ||
] | ||
s.files = [ | ||
".gitignore", | ||
"Rakefile", | ||
"VERSION", | ||
"ext/.gitignore", | ||
"ext/extconf.rb", | ||
"ext/mpq_archive.c", | ||
"lib/mpq.rb", | ||
"test/fixtures/replay.SC2Replay" | ||
] | ||
s.homepage = %q{http://github.com/beaucollins/libmpq-ruby} | ||
s.rdoc_options = ["--charset=UTF-8"] | ||
s.require_paths = ["lib"] | ||
s.rubygems_version = %q{1.3.7} | ||
s.summary = %q{Library for reading MPQ files using libmpq} | ||
|
||
if s.respond_to? :specification_version then | ||
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION | ||
s.specification_version = 3 | ||
|
||
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then | ||
else | ||
end | ||
else | ||
end | ||
end | ||
|
Binary file not shown.