Page 3 of 4

Re: Filesystem Hack: Set Identity to Whatever You Want

Posted: Wed May 25, 2011 1:45 pm
by Robin
BlackBulletIV wrote:I think the only way to detect whether the operating system is Mac is by checking a few common root folders that are on a Mac (and not on Linux, unless someone's creating new root folders for some reason), like "/Users", "/System", "/Applications", and "/Library".
It's not water-tight, I think there are some Linux distributions that create those folder. How about running "uname" first? If prints "Linux", it's Linux, if it prints "Darwin" it's OS X. The thing is, I'm not sure how to redirect standard output in Lua (other than writing it to a temporary file and reading that).
BlackBulletIV wrote:I did suggest a while ago something like a love.platform variable which would greatly aid this sort of thing, but I don't think that was very successful because it would really only be useful in these sort of applications.
And allowing hacks is one thing, but supporting them is another. ;)

Re: Filesystem Hack: Set Identity to Whatever You Want

Posted: Wed May 25, 2011 3:46 pm
by kikito
This page has very useful information regarding platform detection:

http://lua-users.org/wiki/PlatformDetection

Differentiating between windows & unix can be done by using package.config; the first character is the folder path separator (\ on windows, / on unix).
Robin wrote:It's not water-tight, I think there are some Linux distributions that create those folder. How about running "uname" first? If prints "Linux", it's Linux, if it prints "Darwin" it's OS X. The thing is, I'm not sure how to redirect standard output in Lua (other than writing it to a temporary file and reading that).
The capturing output thing can be done with io.popen, according to one of the links in PlatformDetection

This should detect Windows / Linux / Darwin (and probably other unix-likes, such as OpenBSD or Windows+MingGW):

Code: Select all

function platform() -- returns 'Windows', 'Linux' or 'Darwin'
  if package.config:sub(1,1) == '\\' then return 'Windows' end
  return io.popen("uname -s"):read("*l")
end
I know all this because I investigated about platform detection while doing my ansicolors module. I don't know the Lua-users wiki by heart.

Re: Filesystem Hack: Set Identity to Whatever You Want

Posted: Wed May 25, 2011 3:59 pm
by bartbes
Except that io.popen isn't available everywhere, and, more importantly you never match windows, a single character can never match 2 characters (note the single quotes, yet the escaped \).

Re: Filesystem Hack: Set Identity to Whatever You Want

Posted: Wed May 25, 2011 4:04 pm
by thelinx
bartbes wrote:Except that io.popen isn't available everywhere, and, more importantly you never match windows, a single character can never match 2 characters (note the single quotes, yet the escaped \).
In Lua you still need to escape backslashes when enclosed in single quotes.

Re: Filesystem Hack: Set Identity to Whatever You Want

Posted: Wed May 25, 2011 4:07 pm
by kikito
bartbes wrote:Except that io.popen isn't available everywhere, and, more importantly you never match windows, a single character can never match 2 characters (note the single quotes, yet the escaped \).
I believe the escaped bar works in both apostrophes and regular quotes. "\\"=='\\' returns true.

And I wasn't claiming that this was an all-round solution. It's just well enough if you target Windows, Linux or Mac.

Edit: Ninja'd

Re: Filesystem Hack: Set Identity to Whatever You Want

Posted: Wed May 25, 2011 6:49 pm
by bartbes
thelinx wrote: In Lua you still need to escape backslashes when enclosed in single quotes.
Now, I seriously wonder what language I confused this with..

EDIT: Probably sh and friends.

Re: Filesystem Hack: Set Identity to Whatever You Want

Posted: Wed May 25, 2011 7:59 pm
by BlackBulletIV
Robin wrote:It's not water-tight, I think there are some Linux distributions that create those folder. How about running "uname" first? If prints "Linux", it's Linux, if it prints "Darwin" it's OS X. The thing is, I'm not sure how to redirect standard output in Lua (other than writing it to a temporary file and reading that).
Ah ok. That sounds pretty good. However, probably the most solid file you could check would be "/mach_kernel", as of course Linux would be used as the kernel on Linux distros. (If you don't know, Mac uses a combination of a modified BSD kernel for some stuff and the Mach kernel for other things like memory management.)
Robin wrote:And allowing hacks is one thing, but supporting them is another. ;)
Who said it's just about supporting hacks?

Re: Filesystem Hack: Set Identity to Whatever You Want

Posted: Thu May 26, 2011 6:23 am
by kalle2990
kikito wrote:This should detect Windows / Linux / Darwin (and probably other unix-likes, such as OpenBSD or Windows+MingGW):

Code: Select all

function platform() -- returns 'Windows', 'Linux' or 'Darwin'
  if package.config:sub(1,1) == '\\' then return 'Windows' end
  return io.popen("uname -s"):read("*l")
end
I know all this because I investigated about platform detection while doing my ansicolors module. I don't know the Lua-users wiki by heart.
Thanks for telling me, I have added the code together with some configurative settings afterwards. I will test it later today on Ubuntu, but it seems to run fine on Windows :)

Re: Filesystem Hack: Set Identity to Whatever You Want

Posted: Thu May 26, 2011 10:32 am
by vrld
The setIdentity function can be made shorter:

Code: Select all

function setIdentity(to)
    local path = love.filesystem.getSaveDirectory()
    path = path:match("^[^/]*/(.+)$"):gsub('//', '/'):gsub('[^/]+', '..')
    love.filesystem.setIdentity(path .. (to or ""):gsub('^/', ''))
end
Not sure if that first love.filesystem.setIdentity("tmp") is needed or not (for me it works without it).

Re: Filesystem Hack: Set Identity to Whatever You Want

Posted: Thu May 26, 2011 12:53 pm
by bartbes
I think it is needed when you use 'love .', as . is an invalid folder name, so no save folder is 'setIdentitied' by default.