Page 1 of 1

[SOLVED] Love as a dependancy or require 'love2d'?

Posted: Wed Jan 06, 2016 4:51 pm
by pancreas
I'm trying to use love2d as an optional dependency in a project that is not a game.

Love requires that I put all of my dependencies inside of it (which is a major pain) and run my application from `main.lua`. This is not what I want, but I can most likely make it work.

Ideally, I'd like to `require 'love2d'`, do the initialization and override the callbacks and then run it.

My questions are:

Has anyone done this already?
Can someone estimate the level of pain I'm in for if I decide to try to hack Love2D into submission?

If this sounds like a Bad Idea and you think that you understand what I'm going for, I'm open to suggestions.

I think that the alternative would be to set Love's require path to `"../?.lua;../?/init.lua"` and make the `main.lua` load my test file... or something.

I don't want to over-complicate this, but right now, things are overcomplicated. Thank you in advance for your help!

Re: Love as a dependancy or require 'love2d'?

Posted: Wed Jan 06, 2016 4:59 pm
by bartbes
It is definitely possible, it's just fairly tricky to pull off. The easiest way might be to set package.preload.love to the result of calling package.loadlib on the love so/dll with "luaopen_love", and then requiring love. This loads love, but that probably isn't all. The next step, probably, is to require love.filesystem, then immediately call love.filesystem.init with the program's argv0. After that, if everything works, you should be able to require all the modules and use them as normal.

Note that unless you require love.boot and run the result, you don't have the normal initialisation done for you, like whatever's set from conf.lua. If you do run that, it starts the normal "boot sequence" and tries to load your main.lua etc.

Re: Love as a dependancy or require 'love2d'?

Posted: Wed Jan 06, 2016 6:52 pm
by pgimeno
Wow, it actually works!

EDIT: see below for a much simpler approach

Code: Select all

#!/usr/bin/env luajit
package.preload.love = package.loadlib('./liblove.so.0', 'luaopen_love')
require('love')
require('love.filesystem')
love.filesystem.init(arg[0])
require('boot')()
It needs to disable the love.filesystem.init() call in boot.lua (this one is from 0.9.2 which is the only one I have that is compiled dynamically; I've compiled all others as static):

Code: Select all

$ diff -u ../../compile/love-hg/src/scripts/boot.lua .
--- ../../compile/love-hg/src/scripts/boot.lua	2016-01-06 19:20:05.000000000 +0100
+++ ./boot.lua	2016-01-06 19:25:57.000000000 +0100
@@ -247,7 +247,7 @@
 	local o = love.arg.options
 
 	local arg0 = love.arg.getLow(arg)
-	love.filesystem.init(arg0)
+--	love.filesystem.init(arg0)
 
 	-- Is this one of those fancy "fused" games?
 	local can_has_game = pcall(love.filesystem.setSource, arg0)

Code: Select all

chmod +x love.lua
./love.lua /path/to/game.love


The only problem in this case is that the library name is OS-dependent.

(Edited to change lua to luajit)

Re: Love as a dependancy or require 'love2d'?

Posted: Wed Jan 06, 2016 8:36 pm
by pancreas
Woah. Much love to you all. Thank you! It will be a day before I can try this out, but I wanted to thank you both for your help and for trying this out.

Re: Love as a dependancy or require 'love2d'?

Posted: Wed Jan 06, 2016 10:34 pm
by slime
pgimeno wrote:It needs to disable the love.filesystem.init() call in boot.lua
You shouldn't need to do that if you avoid calling love.filesystem.init or any other love.filesystem functions before calling love.boot (i.e. if you let love.boot initialize love.filesystem).

Re: Love as a dependancy or require 'love2d'?

Posted: Wed Jan 06, 2016 10:43 pm
by pgimeno
slime wrote:
pgimeno wrote:It needs to disable the love.filesystem.init() call in boot.lua
You shouldn't need to do that if you avoid calling love.filesystem.init or any other love.filesystem functions before calling love.boot (i.e. if you let love.boot initialize love.filesystem).
I didn't get that to work earlier. I think it's because I followed bartbes' recipe and then forgot to re-add the call to love.filesystem.init to boot.lua. But yes, this works just as well:

Code: Select all

#!/usr/bin/env luajit
package.preload.love = package.loadlib('./liblove.so.0', 'luaopen_love')
require('boot')()
Thanks!

Re: [SOLVED] Love as a dependancy or require 'love2d'?

Posted: Wed Feb 10, 2016 9:21 am
by Idorrac
Hello people! :awesome:
I am rather new to Love2d. I was actually also trying to 'require 'love2d'' and by searching around I bumped into this thread. I am on Windows, so I am trying to load love.dll with the following command:

Code: Select all

package.preload.love = package.loadlib('./love.dll', 'luaopen_love') 
When I do this I get the following error:

Code: Select all

%1 is not a valid Win32 application.
Does anybody have an idea as to what the problem might be? I have no problems running Love2d the 'normal way', just if I try to load the library from lua in this way. I have bumped my head around it quite a lot, this is not strictly necessary for my project but now I am curious to know how the problem could be solved. :D I am using the compiled Love2d binaries from the main page and Lua 5.1.4 at the moment.

Re: [SOLVED] Love as a dependancy or require 'love2d'?

Posted: Wed Feb 10, 2016 4:11 pm
by bartbes
Are you trying to load a 64-bit love using 32-bit lua, or the other way around, perhaps?

Re: [SOLVED] Love as a dependancy or require 'love2d'?

Posted: Thu Feb 11, 2016 2:32 pm
by Idorrac
Thanks, it changed by installing Love2d 32 bit, so I guess that was the issue! :)

Re: [SOLVED] Love as a dependancy or require 'love2d'?

Posted: Wed Jun 01, 2016 1:31 pm
by drinkinggourd
I am also trying to require love2d as a dependency but I cannot figure out how to build liblove.so.0
I am using MacOS.
First I downloaded the MacOS SDK and put the framework files in Libraries/Framework
Then I downloaded the sources and opened liblove.xcodeproj (in subfolder platform/xcode) with XCode.
Build is successful but generates a love.framework thing.
I can't get Xcode to build what I want, which is liblove.so.0

Would someone mind explaining how to achieve this (step-by-step if not too much trouble)?

Many thanks