## Filesystem Hack: Set Identity to Whatever You Want

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Filesystem Hack: Set Identity to Whatever You Want

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.
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: Filesystem Hack: Set Identity to Whatever You Want

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
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.
When I write def I mean function.
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Filesystem Hack: Set Identity to Whatever You Want

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 \).
thelinx
The Strongest
Posts: 852
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

### Re: Filesystem Hack: Set Identity to Whatever You Want

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.
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: Filesystem Hack: Set Identity to Whatever You Want

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
When I write def I mean function.
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Filesystem Hack: Set Identity to Whatever You Want

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.
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

### Re: Filesystem Hack: Set Identity to Whatever You Want

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?
kalle2990
Party member
Posts: 245
Joined: Sat Sep 12, 2009 1:17 pm
Location: Sweden

### Re: Filesystem Hack: Set Identity to Whatever You Want

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
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
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

### Re: Filesystem Hack: Set Identity to Whatever You Want

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).
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Filesystem Hack: Set Identity to Whatever You Want

I think it is needed when you use 'love .', as . is an invalid folder name, so no save folder is 'setIdentitied' by default.

### Who is online

Users browsing this forum: No registered users and 21 guests