forked from andrew-d/lua-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.lua
152 lines (119 loc) · 3.88 KB
/
platform.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
--- A module for determining information about the execution platform
-- @module platform
-- Our global environment.
local P = {}
-- Import section:
-- We declare everything this package needs from "outside" here.
local _type = type
local package = package
local string = string
local io = io
local os = os
local version = _VERSION
-- Note that we define this ahead of the below, since we need to know the
-- version of Lua to figure out whether we need to use setfenv.
local _lua_version = string.sub(version, 5)
-- No more external access after this point.
if _lua_version == '5.2' then
_ENV = P
else
setfenv(1, P)
end
-- Use undocumented package.config to get directory and path separator, if
-- available; otherwise fall back to defaults.
local dirsep, pathsep
if package and package.config then
dirsep, pathsep = string.match(package.config, "^([^\n]+)\n([^\n]+)\n")
else
dirsep = '/'
pathsep = ';'
end
--- Platform-specific constants
const = {
dirsep = dirsep, -- The directory separator
pathsep = pathsep, -- The path separator
extsep = '.', -- The extension separator
}
-------------------------------------------------------------------------------
-- A constant indicating whether the current execution platform is Windows.
is_windows = const.dirsep == '\\'
is_minecraft = false
if _type(os.version) == "function" then
if string.sub(os.version(),1,7) == "CraftOS" or
string.sub(os.version(),1,8) == "TurtleOS" then
is_minecraft = true
end
end
-------------------------------------------------------------------------------
-- Returns the current execution platform.
-- Values can include "windows", "darwin", "linux", or whatever the lower-case
-- version of `uname` returns. "unknown" is returned if the output from
-- `uname` is empty (e.g. in case of an error).
-- @return A string indicating the current execution platform
function platform()
if is_windows then
return 'windows'
end
if is_minecraft then
return 'minecraft'
end
-- Return the lower-cased output from `uname`. The redirection means that
-- if the command doesn't exist, we don't get a strange error.
local uname = io.popen('uname 2>/dev/null'):read()
if #uname > 0 then
return uname:lower()
end
return 'unknown'
end
local _win_mapping = {
['AMD64'] = 'x64',
['IA64'] = 'ia64',
['x86'] = 'x86',
}
local _other_mapping = {
['i686'] = 'x86',
['i386'] = 'x86',
['x86_64'] = 'x64',
}
-------------------------------------------------------------------------------
-- Returns the current processor architecture.
-- The string returned will be one of 'x86', 'x64', 'ia64', 'unknown', or
-- whatever `uname -p` returns.
-- @return A string indicating the processor architecture
function architecture()
local arch
if is_windows then
-- Try WOW64 first.
arch = os.getenv('PROCESSOR_ARCHITEW6432')
if arch == nil then
arch = os.getenv('PROCESSOR_ARCHITECTURE')
end
return _win_mapping[arch] or 'unknown'
end
if is_minecraft then
if string.sub(os.version(),1,8) == "TurtleOS" then
return "turtle"
else
return "computer"
end
end
-- For now, we just try getting the information from uname.
arch = io.popen('uname -a 2>/dev/null'):read()
if arch ~= nil then
if arch:find('x86_64') then
return 'x64'
elseif arch:find('i386') then
return 'x86'
end
end
-- Default to uname -p, mapped to common names if possible.
arch = io.popen('uname -p 2>/dev/null'):read()
if arch ~= nil then
return _other_mapping[arch] or arch
end
return 'unknown'
end
-------------------------------------------------------------------------------
-- The current version of Lua, as a string (e.g. "5.1", "5.2", etc.)
lua_version = _lua_version
return P