Skip to content

Commit 461f1b7

Browse files
committed
refactor: Reorganize code (& temporarily remove comments)
1 parent 186da17 commit 461f1b7

File tree

13 files changed

+86
-240
lines changed

13 files changed

+86
-240
lines changed

rokit.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ lune = "lune-org/[email protected]"
88
darklua = "seaofvoices/[email protected]"
99
selene = "Kampfkarren/[email protected]"
1010
StyLua = "JohnnyMorganz/[email protected]"
11+
wally = "UpliftGames/[email protected]"

src/Classes/SteamApp.luau

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ local process = require("@lune/process")
33

44
local Metadata = require("../Metadata")
55

6-
-- Constants
7-
local steamImageLocation = process.env.HOME .. "/.local/share/Steam/appcache/librarycache/"
8-
96
local SteamApp = {}
107

118
SteamApp.Interface = {}
9+
SteamApp.Prototype = {}
1210

1311
function SteamApp.Interface.new(
1412
appID: number,
@@ -36,7 +34,7 @@ function SteamApp.Interface.new(
3634
-- What type is the app ?
3735
-- Current way of detecting if an app is a game or a tool... not the greatest.
3836
local type: "Game" | "Other" | "Proton" = "Game"
39-
if not fs.metadata(steamImageLocation .. appID .. "_library_600x900.jpg").exists then
37+
if not fs.metadata(process.env.HOME .. "/.local/share/Steam/appcache/librarycache/" .. appID .. "_library_600x900.jpg").exists then
4038
type = "Other"
4139
if name:find("Proton") then
4240
type = "Proton"

src/Configuration/init.luau

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
return {
2-
Games = require("./Games")
2+
Games = require("./Games/Functions")
33
}

src/Metadata/init.luau

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
local process = require("@lune/process")
22

3-
--[=[
4-
@class Metadata
5-
Application constants used throughout the program.
6-
]=]
7-
local Metadata = {
3+
return {
84
name = "STweaks",
95
description = "A work-in-progress fast, simple and modern Libadwaita alternative to SteamTinkerLaunch.",
106
version = "indev",
@@ -16,5 +12,3 @@ local Metadata = {
1612
cache = process.env.HOME .. "/.cache/Stweaks",
1713
},
1814
}
19-
20-
return Metadata

src/Steam/Configuration.luau

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,39 @@ local SteamUtilities = require("./Utilities")
99
local VDFParser = require("./VDFParser")
1010
local SteamApp = require("../Classes").SteamApp
1111

12+
local function VDFHandler(filePath: string, errorMessage: string, logging: boolean?): any
13+
if logging == nil then
14+
logging = true
15+
end
16+
17+
local timeStart
18+
if logging then
19+
timeStart = os.clock()
20+
Logging.write("data", "Parsing VDF file : " .. filePath)
21+
end
22+
23+
local result, parsedData = pcall(VDFParser.parseFile, filePath)
24+
if not result then
25+
error(errorMessage)
26+
else
27+
if logging then
28+
Logging.write("speed", timeStart)
29+
end
30+
return parsedData
31+
end
32+
end
33+
34+
local function getMostRecentUserID(userData: loginusers_vdf): number
35+
for id, data in pairs(userData.users) do
36+
if data.MostRecent == 1 then
37+
return id
38+
end
39+
end
40+
error("No most recent user found in loginusers.vdf. What ?!")
41+
end
42+
1243
-- --------------------------- Definitions -----------------------------
1344

14-
-- Steam files
1545
-- Note : Most of the entries are unused and simply for informative purposes.
1646

1747
type loginusers_vdf = {
@@ -137,19 +167,14 @@ type compat_vdf = {
137167

138168
local Configuration = {}
139169

140-
Configuration.Private = {}
141-
Configuration.Public = {}
142-
143-
-- Public
144-
145-
function Configuration.Public.getSteamConfiguration()
170+
function Configuration.getSteamConfiguration()
146171
--[[
147172
Chapter 1 : We recover the Steam user config so we can get the last active user.
148173
And also the SteamID3 to access their settings folder.
149174
]]
150175
local userConfiguration: loginusers_vdf =
151-
Configuration.Private.VDFHandler(process.env.HOME .. "/.local/share/Steam/config/loginusers.vdf", "Failed to parse loginusers.vdf file.")
152-
local activeUserSteamID3 = SteamUtilities.convertToSteamID3(Configuration.Private.getMostRecentUserID(userConfiguration))
176+
VDFHandler(process.env.HOME .. "/.local/share/Steam/config/loginusers.vdf", "Failed to parse loginusers.vdf file.")
177+
local activeUserSteamID3 = SteamUtilities.convertToSteamID3(getMostRecentUserID(userConfiguration))
153178
Logging.write("info", "Active user SteamID3 : " .. activeUserSteamID3)
154179
Logging.separator()
155180

@@ -158,7 +183,7 @@ function Configuration.Public.getSteamConfiguration()
158183
159184
Note : Error handling should be added for the scenario where the configuration can't be loaded.
160185
]]
161-
local localconfig_vdf: localconfig_vdf = Configuration.Private.VDFHandler(
186+
local localconfig_vdf: localconfig_vdf = VDFHandler(
162187
process.env.HOME .. "/.local/share/Steam/userdata/" .. activeUserSteamID3 .. "/config/localconfig.vdf",
163188
"Failed to parse active user localconfig.vdf file with SteamID3: " .. activeUserSteamID3
164189
)
@@ -171,7 +196,7 @@ function Configuration.Public.getSteamConfiguration()
171196
We can't use it directly to get the platform details, as games that have previously used Proton will still be set to "windows" here.
172197
Logically, a game completely absent from this list is a native Linux game.
173198
]]
174-
local compat_vdf: compat_vdf = Configuration.Private.VDFHandler(
199+
local compat_vdf: compat_vdf = VDFHandler(
175200
process.env.HOME .. "/.local/share/Steam/userdata/" .. activeUserSteamID3 .. "/config/compat.vdf",
176201
"Failed to parse compat.vdf file."
177202
)
@@ -183,7 +208,7 @@ function Configuration.Public.getSteamConfiguration()
183208
We can't use just libraryfolders.vdf to get the game IDs, as Steam seems to not always update it immediately.
184209
]]
185210
local libraries: libraryfolders_vdf =
186-
Configuration.Private.VDFHandler(process.env.HOME .. "/.local/share/Steam/config/libraryfolders.vdf", "Failed to parse libraryfolders.vdf file.")
211+
VDFHandler(process.env.HOME .. "/.local/share/Steam/config/libraryfolders.vdf", "Failed to parse libraryfolders.vdf file.")
187212
Logging.write("info", "Steam libraries loaded.")
188213
Logging.separator()
189214

@@ -200,7 +225,7 @@ function Configuration.Public.getSteamConfiguration()
200225

201226
Async.asyncForEach(appManifestFilenames, function(_, appManifestFilename)
202227
local appManifestPath: string = library.path .. "/steamapps/" .. appManifestFilename
203-
local appManifest: appmanifest_vdf = Configuration.Private.VDFHandler(appManifestPath, "Failed to parse " .. appManifestPath .. ".", false)
228+
local appManifest: appmanifest_vdf = VDFHandler(appManifestPath, "Failed to parse " .. appManifestPath .. ".", false)
204229

205230
Logging.write("info", 'Found "' .. appManifest.AppState.name .. '" (' .. appManifest.AppState.appid .. ")")
206231

@@ -247,39 +272,4 @@ function Configuration.Public.getSteamConfiguration()
247272
return steamApps
248273
end
249274

250-
-- Private
251-
252-
function Configuration.Private.VDFHandler(filePath: string, errorMessage: string, logging: boolean?): any
253-
if logging == nil then
254-
logging = true
255-
end
256-
257-
local timeStart
258-
if logging then
259-
timeStart = os.clock()
260-
Logging.write("data", "Parsing VDF file : " .. filePath)
261-
end
262-
263-
local result, parsedData = pcall(VDFParser.parseFile, filePath)
264-
if not result then
265-
error(errorMessage)
266-
else
267-
if logging then
268-
Logging.write("speed", timeStart)
269-
end
270-
return parsedData
271-
end
272-
end
273-
274-
-- loginuser.vdf
275-
276-
function Configuration.Private.getMostRecentUserID(userData: loginusers_vdf): number
277-
for id, data in pairs(userData.users) do
278-
if data.MostRecent == 1 then
279-
return id
280-
end
281-
end
282-
error("No most recent user found in loginusers.vdf. What ?!")
283-
end
284-
285-
return Configuration.Public
275+
return Configuration

src/Steam/Utilities.luau

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
local process = require("@lune/process")
22

3-
--[=[
4-
@class Utilities
5-
6-
Minor Steam-related helper functions.
7-
]=]
83
local Utilities = {}
94

10-
--[=[
11-
Checks if the arguments are from Steam or not.
12-
Non-Steam game support later on will probably necessitate changes.
13-
14-
@return number -- Returns the launched AppID if the arguments are from Steam, otherwise nil.
15-
]=]
165
function Utilities.isSteamLaunch(): number?
176
local appIDArgumentIndex
187
for index, argument in ipairs(process.args) do
@@ -30,12 +19,6 @@ function Utilities.isSteamLaunch(): number?
3019
end
3120
end
3221

33-
--[=[
34-
Converts SteamID64 to SteamID3.
35-
36-
@param steamID64 number -- The SteamID64 to convert.
37-
@return number -- Returns the corresponding SteamID3.
38-
]=]
3922
function Utilities.convertToSteamID3(steamID64: number): number
4023
local offset_id = steamID64 - 76561197960265728
4124
local account_type = offset_id % 2

src/Steam/VDFParser.luau

Lines changed: 34 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,9 @@
11
local fs = require("@lune/fs")
22

3-
--[=[
4-
Contains functions for parsing VDF files and strings.
5-
Lune does not support VDF files natively.
6-
]=]
7-
local VDFParser = {}
8-
9-
VDFParser.Private = {}
10-
VDFParser.Public = {}
11-
12-
-- ---------------------------------- Public
13-
14-
--[=[
15-
Parses a VDF file into a table.
16-
17-
@param path string -- The path to the file.
18-
@return The parsed file if successful.
19-
]=]
20-
function VDFParser.Public.parseFile(path: string)
21-
return VDFParser.Public.parseString(fs.readFile(path))
22-
end
23-
24-
--[=[
25-
Parses a VDF string into a table.
26-
27-
@param input string -- The VDF string to parse.
28-
@return The parsed table if successful.
29-
]=]
30-
function VDFParser.Public.parseString(input: string)
31-
return VDFParser.Private.sanitize(VDFParser.Private.parse(input))
32-
end
33-
34-
-- ---------------------------------- Private
35-
363
type VDFTable = { [string]: VDFValue }
374
type VDFValue = string | VDFTable
385

39-
--[=[
40-
Parses a VDF string into a table. Private side of the parser.
41-
42-
@param input The VDF string to parse.
43-
@return The parsed table if successful.
44-
]=]
45-
function VDFParser.Private.parse(input: string): VDFTable
6+
local function parseVDFData(input: string): VDFTable
467
-- Trim function to remove whitespace
478
local function trim(s: string): string?
489
return s:match("^%s*(.-)%s*$")
@@ -142,48 +103,46 @@ function VDFParser.Private.parse(input: string): VDFTable
142103
return currentTable
143104
end
144105

145-
--[=[
146-
Sanitizes a VDF table by converting string values to their proper types.
106+
local function sanitizeVDFTable(input: VDFTable)
107+
local function convertValue(value: any): any
108+
if type(value) == "string" then
109+
-- Check for number values (integers or floats)
110+
local numberValue = tonumber(value)
111+
if numberValue ~= nil then
112+
return numberValue
113+
end
114+
-- Check for empty strings
115+
if value == "" then
116+
return nil
117+
end
118+
-- If it's neither, return the original string
119+
return value
120+
elseif type(value) == "table" then
121+
-- Recursively sanitize nested tables
122+
return sanitizeVDFTable(value)
123+
else
124+
-- Return the value as-is for unsupported types (shouldn't happen in a VDF)
125+
return value
126+
end
127+
end
147128

148-
@param input -- The VDF table to sanitize.
149-
@return The sanitized table.
150-
]=]
151-
function VDFParser.Private.sanitize(input: VDFTable)
152129
local result = {}
153-
154130
for key, value in pairs(input) do
155-
result[VDFParser.Private.convertValue(key)] = VDFParser.Private.convertValue(value)
131+
result[convertValue(key)] = convertValue(value)
156132
end
157133

158134
return result
159135
end
160136

161-
--[=[
162-
Converts a VDF value to its proper type.
163-
164-
@param value -- The value to convert.
165-
@return The converted value.
166-
]=]
167-
function VDFParser.Private.convertValue(value: any): any
168-
if type(value) == "string" then
169-
-- Check for number values (integers or floats)
170-
local numberValue = tonumber(value)
171-
if numberValue ~= nil then
172-
return numberValue
173-
end
174-
-- Check for empty strings
175-
if value == "" then
176-
return nil
177-
end
178-
-- If it's neither, return the original string
179-
return value
180-
elseif type(value) == "table" then
181-
-- Recursively sanitize nested tables
182-
return VDFParser.Private.sanitize(value)
183-
else
184-
-- Return the value as-is for unsupported types (shouldn't happen in a VDF)
185-
return value
186-
end
137+
138+
local VDFParser = {}
139+
140+
function VDFParser.parseFile(path: string)
141+
return VDFParser.parseString(fs.readFile(path))
142+
end
143+
144+
function VDFParser.parseString(input: string)
145+
return sanitizeVDFTable(parseVDFData(input))
187146
end
188147

189-
return VDFParser.Public
148+
return VDFParser

src/Utilities/Async.luau

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
local task = require("@lune/task")
22

3-
--[=[
4-
@class Utilities.Async
5-
]=]
63
local Async = {}
74

8-
--[=[
9-
Executes a function in parallel over all elements in a table.
10-
It's similar to Rust's rayon.
11-
Pretty big upgrade compared to the previous implementation.
12-
13-
@param tbl The table to iterate over.
14-
@param func The function to execute in parallel for each element.
15-
]=]
165
function Async.asyncForEach(tbl: { any }, func: (any, any) -> ())
176
local total = 0
187
local completed = 0

0 commit comments

Comments
 (0)