Old 10-03-2019, 10:13 AM   #1
snooks
Human being with feelings
 
Join Date: Sep 2015
Posts: 1,624
Default fileops: a few extra Lua functions in a Windows dll

v0.3 : now even works!

To use these, unzip the package and put the dll somewhere, preferably remembering where you put it. Then, in Lua (eg):

Code:
local path = "C:/Users/paul-/AppData/Roaming/REAPER/UserPlugins/fileops.dll"
local nameOfFunction = package.loadlib(path, "nameOfFunction")
assert(type(nameOfFunction) == "function", "\nError: failed to load function from dll")
Functions available:

ok, result_or_error = compareFileTimesFromPaths(string: path1, string: path2)
Quickly see whether files have the same timestamp.
Returns:
ok - bool: error flag | result_or_error - Error message if error or:
-1 : first file older than the second
0 : they are the same age
1 : the first file is newer than the second

ok, error = copyFile(string: src_path, string: dst_path)
Copy a file using a WinAPI function.
Returns:
ok - bool: error flag | error - Error message if error

ok, hi_time_or_error, lo_time = getLastModified(string: path)
Get easily comparable last modified numbers, taken from:
https://docs.microsoft.com/en-us/win...nbase-filetime
Returns:
ok - bool: error flag | hi_time_or_error - Error message if error or hi portion of file time | lo_time low portion of file time

version = getVersion()
Get version string.
Returns:
version - erm, version string
Attached Files
File Type: zip fileops-v0.3.zip (34.0 KB, 9 views)

Last edited by snooks; 10-09-2019 at 01:04 AM.
snooks is offline   Reply With Quote
Old 10-03-2019, 10:35 AM   #2
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 7,914
Default

How did you manage to get that working? Did you reverse engineer where the Lua state pointer is in Reaper's memory?
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass, Paul(X)Stretch and λ) :
http://xenakios.wordpress.com/
Xenakios is online now   Reply With Quote
Old 10-03-2019, 10:37 AM   #3
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,241
Default

Did you have to do anything specific to make the .dll useable from within Reaper? My understanding was that we simply couldn't pull in .dlls at all.
Lokasenna is online now   Reply With Quote
Old 10-03-2019, 11:52 AM   #4
snooks
Human being with feelings
 
Join Date: Sep 2015
Posts: 1,624
Default

I discovered it in the thread mespotine made about package.loadlib.

I'm compiling it against the static Lua 5.3 lib with VS2019 (v142) and there are only functions in the file, no luaopen_ or luaL_Reg stuff.

Just...
Code:
#include "lua.hpp"
#define LUA_API extern "C" __declspec( dllexport ) int

LUA_API blah(lua_State *L)
{
    lua_pushstring(L, "from blah");
    return 1;
}
… kind of stuff.
snooks is offline   Reply With Quote
Old 10-03-2019, 03:52 PM   #5
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 6,031
Default

Is the extension open source ?
so that macOS build can be made etc ?
X-Raym is offline   Reply With Quote
Old 10-03-2019, 11:24 PM   #6
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 189
Default

What is the magic inside that make the copying so fast?
dsyrock is offline   Reply With Quote
Old 10-04-2019, 06:21 AM   #7
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,241
Default

Quote:
Originally Posted by dsyrock View Post
What is the magic inside that make the copying so fast?
It can use Windows' native API rather than going through the terminal.
Lokasenna is online now   Reply With Quote
Old 10-04-2019, 07:07 AM   #8
mespotine
Human being with feelings
 
mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig, Germany
Posts: 1,555
Default

Nice! Really really nice!

If there's a chance to make a Mac and Linux-version too, this would be awesome.

Great work

Edit: Would getting file-attributes be possible as well?
__________________
Ultraschall-API - a Lua-functions-library4Reaper: https://forum.cockos.com/showthread....98#post2067798
Reaper Internals - Developerdocs4Reaper: https://forum.cockos.com/showthread.php?t=207635
mespotine is offline   Reply With Quote
Old 10-04-2019, 03:26 PM   #9
snooks
Human being with feelings
 
Join Date: Sep 2015
Posts: 1,624
Default

I removed a function that was messing up international characters and changed the getLastModified to both get the actually last modified details and to just follow the win32 API to get two easily comparable numbers.

Here's the source code if anyone wants a look:

https://pastebin.com/9j7kkRbG

Last edited by snooks; 10-05-2019 at 10:00 AM. Reason: replaced with v0.3
snooks is offline   Reply With Quote
Old 10-04-2019, 07:02 PM   #10
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 189
Default

I did a test with the new one, seems like it still has international characters issue

Code:
path_sr="e:\\测试1.wav"
path_des="e:\\测试2.wav"

local path_dll=reaper.GetResourcePath().."\\UserPlugins\\fileops.dll"
local copyFile = package.loadlib(path_dll, "copyFile")

a,b=copyFile(path_sr, path_des)
msg(a)
msg(b)
And it returned:

Code:
false
Error: an unknown error occured copying the file.
By the way when the filename only contain English, it still works.
dsyrock is offline   Reply With Quote
Old 10-05-2019, 12:41 AM   #11
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 189
Default

I did several more tests. Function compareFileTimesFromPaths works with non-English filename, but getLastModified doesn't.
dsyrock is offline   Reply With Quote
Old 10-05-2019, 04:30 AM   #12
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 2,768
Default

The strings should be properly converted from UTF-8 to UTF-16 (rather than just copying the UTF-8-encoded string into a wide string byte to word) to support all possible characters independently of the current system codepage.

Instead of:
Code:
std::string str_path1 = lua_tostring(L, 1);
std::wstring w1;
w1.assign(begin(str_path1), end(str_path1));
Assuming the file is built with the UNICODE macro defined:
Code:
// extracted from ReaPack's source code
static std::wstring widen(const char *input, const UINT codepage = CP_UTF8)
{
  const int size = MultiByteToWideChar(codepage, 0, input, -1, nullptr, 0) - 1;

  std::wstring output(size, 0);
  MultiByteToWideChar(codepage, 0, input, -1, &output[0], size);

  return output;
}

// usage:
const std::wstring &path1 = widen(lua_tostring(L, 1)),
                   &path2 = widen(lua_tostring(L, 2));
AnyWindowsAPIFunction(path1.c_str(), path2.c_str());

Last edited by cfillion; 10-05-2019 at 04:52 AM.
cfillion is offline   Reply With Quote
Old 10-05-2019, 05:45 AM   #13
snooks
Human being with feelings
 
Join Date: Sep 2015
Posts: 1,624
Default

Cool, thanks very much. I'm a Win32 API n00b and was literally just creating a MultiByteToWideChar helper function, I'll just copy/paste your code in there. Cheers!
snooks is offline   Reply With Quote
Old 10-08-2019, 06:49 PM   #14
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 189
Default

Quote:
Originally Posted by snooks View Post
Here's the source code if anyone wants a look:

https://pastebin.com/9j7kkRbG
I'm a newbie of C++. I'm wondering what tools can convert this code to a dll file?
dsyrock is offline   Reply With Quote
Old 10-09-2019, 05:55 AM   #15
snooks
Human being with feelings
 
Join Date: Sep 2015
Posts: 1,624
Default

Of course,

Download:
Visual Studio 2019 Community Edition
Run the installer.

Select:
Desktop development with C++
... and whatever else you want.

I'll upload the project later.

I highly recommend installing and using "vcpkg" if you are developing C++ with Visual Studio too. It makes using libraries very simple.
snooks is offline   Reply With Quote
Old 10-09-2019, 06:12 AM   #16
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 189
Default

Thanks!
dsyrock is offline   Reply With Quote
Old 10-09-2019, 03:31 PM   #17
snooks
Human being with feelings
 
Join Date: Sep 2015
Posts: 1,624
Default

I have attached the project file.

The only thing you (should) have to change is the Post Build Event where it copies the dll to the UserPlugins directory.

Right click on the project name in Solution Explorer panel. Click on Properties (or press Alt + F10 when Solution Explorer has focus).

Go to Build Events. Change the Post Build Event "copy" command so that the output directory matches your UserPlugins path.

In Visual Studio the properties are different for different "Configurations" and "Platforms". So Debug vs Release and x86 vs x64. When you are changing things, make sure it's for the correct Config and Platform. There are options at the top of the Properties window to select All Configurations and All Platforms, so probably do that for this.

Only build x64 Release though, debug doesn't work for some reason.
Attached Files
File Type: zip vs-fileops-v0.3.zip (315.2 KB, 8 views)
snooks is offline   Reply With Quote
Old 10-09-2019, 06:39 PM   #18
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 189
Default

Thanks again
dsyrock is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 09:02 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.