-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
170 lines (152 loc) · 4.54 KB
/
init.lua
File metadata and controls
170 lines (152 loc) · 4.54 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
-- // Paficent \\ --
local security = require("lib/security")
local SETTINGS_PATH = mod.getRoot() .. "/settings.json"
local MANIFEST_PATH = mod.getRoot() .. "/manifest.json"
local manifest = json.decode(file.read(MANIFEST_PATH))
local settings = {
fallback_dlc_url = "",
accounts = {
{
username = "Steam",
email = "",
password = "",
type = "Steam",
portrait = "steam"
}
}
}
local function parse(raw)
local ok, parsed = pcall(net.jsonDecode, raw)
if not ok or type(parsed) ~= "table" then return false end
settings = parsed
if type(settings.accounts) ~= "table" then
settings.accounts = {}
end
if type(settings.cache) ~= "table" then
settings.cache = {}
end
return true
end
local defaultPortraits = {
Steam = "steam",
Email = "email",
Mobile = "mobile",
Guest = "house",
}
local function build_info(i, acc)
local kind = acc.type or "Email"
local suffix = kind == "Steam" and "" or " (" .. kind .. ")"
return {
index = i,
username = acc.username or kind .. " account",
email = acc.email or "",
type = kind,
label = (acc.username or "") .. suffix,
portrait = acc.portrait or defaultPortraits[kind] or defaultPortraits.email,
}
end
local BetterLogin = {}
function BetterLogin.version()
return manifest.version or "unknown"
end
function BetterLogin.count()
return #settings.accounts
end
function BetterLogin.info(i)
local acc = settings.accounts[i]
if not acc then return nil end
return build_info(i, acc)
end
function BetterLogin.accounts()
local out = {}
for i, acc in ipairs(settings.accounts) do
out[i] = build_info(i, acc)
end
return out
end
function BetterLogin.login(i)
local acc = settings.accounts[i]
if not acc then return false end
local pw = security.dec(acc.password)
if acc.type == "Steam" then
game.authWithSteam()
elseif acc.type == "Guest" then
game.authWithAnon()
elseif acc.type == "Mobile" then
game.authWithEmail("$" .. acc.email, pw)
else
game.authWithEmail(acc.email, pw)
end
return true
end
function BetterLogin.add(username, email, password, acctype, portrait)
acctype = acctype or "Email"
if acctype == "Guest" then
settings.accounts[#settings.accounts + 1] = {
username = username or acctype .. " account",
email = email,
password = "",
type = "Guest",
portrait = portrait or defaultPortraits.guest,
}
else
if type(username) ~= "string" or type(password) ~= "string" then return false end
settings.accounts[#settings.accounts + 1] = {
username = username,
email = email,
password = security.enc(password),
type = acctype,
portrait = portrait or defaultPortraits[acctype] or defaultPortraits.email,
}
end
BetterLogin.save()
return #settings.accounts
end
function BetterLogin.remove(i)
if type(i) ~= "number" or i < 1 or i > #settings.accounts then return false end
table.remove(settings.accounts, i)
BetterLogin.save()
return true
end
function BetterLogin.save()
local ok, raw = pcall(file.read, SETTINGS_PATH)
if ok and raw then
local parseOk, existing = pcall(net.jsonDecode, raw)
if parseOk and type(existing) == "table" and existing.cache then
settings.cache = existing.cache
end
end
pcall(file.write, SETTINGS_PATH, net.jsonEncode(settings))
end
function BetterLogin.reload()
local ok, raw = pcall(file.read, SETTINGS_PATH)
if ok and raw then
parse(raw)
local dirty = false
for _, acc in ipairs(settings.accounts) do
if acc.password ~= "" and not security.is_encrypted(acc.password) then
acc.password = security.enc(acc.password)
dirty = true
end
end
if dirty then BetterLogin.save() end
else
print("something went wrong... ", ok, raw)
file.write(SETTINGS_PATH, net.jsonEncode(settings))
BetterLogin.reload()
end
end
-- daktzill did this and honestly its fine, yes its hacky but theres no issue with it
-- ty pafi :steamhappy:
local username = ""
function BetterLogin.username(t, val)
val = val or nil
if t == "get" then
return username
elseif t == "set" then
username = val
end
end
security.init(settings)
BetterLogin.reload()
jeode.registerGlobal("BetterLogin", BetterLogin)