Skip to content

Commit

Permalink
ReaScript function export: updated python function wrapper generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff0S committed Mar 30, 2015
1 parent c6b4df3 commit 51538bb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
25 changes: 14 additions & 11 deletions reascript_helper.pl
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
# Original script by Cockos (http://landoleet.org/dev/sws/), also see http://code.google.com/p/sws-extension/issues/detail?id=432
# JFB mods: tailored for the SWS/S&M extension, i.e. process ./ReaScript.cpp rather than ../plugin.cpp

#!/usr/bin/perl

# Original script by Cockos (http://landoleet.org/dev/sws/)
# JFB mods: tailored for the SWS/S&M extension: process ./ReaScript.cpp rather than ../plugin.cpp

sub ReadAPIFuncs
{
@funclist=();
%rtype={};
%ptypes={};
%pnames={};
%callfuncs={};

open INFILE, "ReaScript.cpp" or die "Can't read ReaScript.cpp";
while (<INFILE>)
{
if (/\s*\{\s*APIFUNC/)
{
$fname="";
$callfunc="";
if (/APIFUNC\((.+?)\),\s*(.*)/)
{
$fname=$1;
$callfunc=$fname=$1;
$_=$2;
}
elsif (/APIFUNC_NAMED\((.+?)\,(.+?)\),\s*(.*)/)
{
$fname=$1;
$callfunc=$2;
$_=$3;
}
$fname or die "Can't parse function name";
push @funclist, $fname;
$callfuncs{$fname}=$callfunc;

(/\"(.+?)\"\,\s*(.*)/) or die "Can't parse return type";
(/\"(.+?)\"\s*\,\s*(.*)/) or die "Can't parse return type";
$rtype{$fname}=$1;
$_=$2;

(/\"(.*?)\"\,\s*(.*)/) or die "Can't parse parameter types";
(/\"(.*?)\"\s*\,\s*(.*)/) or die "Can't parse parameter types";
$ptypes{$fname}=$1;
$_=$2;

# not using these
#(/\"(.*?)\"/) or die "Can't parse parameter names";
#$pnames{$fname}=$1;
(/\"(.*?)\"/) or die "Can't parse parameter names";
$pnames{$fname}=$1;
}
}
close INFILE;

@funclist=sort {lc $a cmp lc $b} @funclist;
return (\@funclist, \%rtype, \%ptypes);
return (\@funclist, \%rtype, \%ptypes, \%pnames, \%callfuncs);
}

sub IsInt
Expand Down Expand Up @@ -92,4 +95,4 @@ sub UnderlyingType



1;
1;
28 changes: 16 additions & 12 deletions reascript_python.pl
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Original script by Cockos (http://landoleet.org/dev/sws/), also see http://code.google.com/p/sws-extension/issues/detail?id=432
# JFB mods: tailored for the SWS/S&M extension, i.e. simplified (use a reaper_python import), gets function pointers with rpr_getfp(), removed "RPR_" prefix

#!/usr/bin/perl

# Original script by Cockos (http://landoleet.org/dev/sws/)
# JFB mods: tailored for the SWS/S&M extension:
# - re-use rpr_ internal functions from reaper_python import
# - gets function pointers with rpr_getfp()
# - removed "RPR_" prefix

require "reascript_helper.pl";

$arg=shift @ARGV;
Expand Down Expand Up @@ -35,7 +38,8 @@ sub PyPack
{
my ($t, $v)=@_;
IsPlainData($t) and return sprintf "%s(%s)", PyType($t), $v;
$t eq "const char*" and return sprintf "rpr_packsc(%s)", $v;
($t eq "const char*" && $v ne "bytestr") and return sprintf "rpr_packsc(%s)", $v;
($t eq "const char*" && $v eq "bytestr") and return sprintf "c_char_p(%s)", $v;
$t eq "char*" and return sprintf "rpr_packs(%s)", $v;
# todo handle char**
IsPlainDataPtr($t) and return sprintf "%s(%s)", PyType(UnderlyingType($t)), $v;
Expand All @@ -48,10 +52,9 @@ sub PyUnpack
my ($t, $ov, $v)=@_;
(IsPlainData($t) || IsOpaquePtr($t) || $t eq "const char*") and return $ov;
$t eq "char*" and return sprintf "rpr_unpacks(%s)", $v;
($t eq "bool*" || $t eq "char*" || IsInt(UnderlyingType($t))) and
return sprintf "int(%s.value)", $v;
($t eq "float*" || $t eq "double*") and return sprintf "float(%s.value)", $v;
# todo handle char**
($t eq "bool*" || IsInt(UnderlyingType($t))) and return sprintf "int(%s.value)", $v;
($t eq "float*" || $t eq "double*") and return sprintf "float(%s.value)", $v;
IsPlainDataPtr($t) and return PyUnpack(UnderlyingType($t), $v);
die "unknown type $t";
}
Expand All @@ -70,6 +73,7 @@ sub PyRetUnpack
foreach $fname (@$funclist)
{
$fname eq "ValidatePtr" and next;
$fname eq "GetAudioAccessorSamples" and next;

@inparms=();
@proto=();
Expand All @@ -83,14 +87,14 @@ sub PyRetUnpack
$i=0;
foreach $t (split(",", $ptypes->{$fname}))
{
$isptr=IsPlainDataPtr($t);

($t ne "const char*") and $t =~ s/const //;
push @inparms, "p$i";
push @proto, PyType($t);
push @pack, PyPack($t, "p$i");
push @cparms, (($isptr && $t ne "char*") ? "byref(t[$i])" : "t[$i]");
push @cparms, (IsPlainDataPtr($t) && !IsString($t) ? "byref(t[$i])" : "t[$i]");
push @unpack, PyUnpack($t, "p$i", "t[$i]");
$isptr and $byref=1;
IsPlainDataPtr($t) and $byref=1;
++$i;
}

Expand All @@ -114,5 +118,5 @@ sub PyRetUnpack
}


warn "Parsed " . @$funclist . " functions\n";
print STDERR "Parsed " . @$funclist . " functions\n";

1 change: 1 addition & 0 deletions whatsnew.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Added actions
- SWS/BR: Play from edit cursor position and solo active item for the duration (perform until shortcut released)

ReaScript
+Improved Python function wrappers (sws_python*.py)
+Added functions to convert track/item/take from and to GUID:
- BR_GetMediaItemByGUID
- BR_GetMediaItemGUID
Expand Down

0 comments on commit 51538bb

Please sign in to comment.