Skip to content

Commit

Permalink
Added support for loading configs and persistence in conf.lua
Browse files Browse the repository at this point in the history
Program title is now set in conf.lua rather than in selene_main
Moved common "lua setup" code into own file, is used in main.lua and conf.lua
This sets up require paths and Selene
Adjusted some files to prevent crashes with love.system not being loaded yet in conf.lua
  • Loading branch information
Cruor committed Dec 29, 2021
1 parent 7709a95 commit c2b2229
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 29 deletions.
7 changes: 7 additions & 0 deletions src/conf.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
require("lua_setup")

local meta = require("meta")
local configs = require("configs")
local persistence = require("persistence")

function love.conf(t)
t.console = true

t.title = meta.title
t.window.resizable = true
t.window.minwidth = 1280
t.window.width = 1280
Expand Down
3 changes: 2 additions & 1 deletion src/file_locations.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local filesystem = require("utils.filesystem")
local config = require("utils.config")
local utils = require("utils")

local fileLocations = {}

Expand All @@ -9,7 +10,7 @@ fileLocations.loennLinuxFolderName = "Lönn"
fileLocations.loennZipFolderName = "L" .. string.char(148) .. "nn"

function fileLocations.getStorageDir()
local userOS = love.system.getOS()
local userOS = utils.getOS()

local windowsFolderName = fileLocations.loennWindowsFolderName
local linuxFolderName = fileLocations.loennLinuxFolderName
Expand Down
24 changes: 24 additions & 0 deletions src/lua_setup.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Setting up globals for a new Lua environment (main.lua or conf.lua)

local ffi = require("ffi")

local path = "selene/selene/lib/?.lua;selene/selene/lib/?/init.lua;?/?.lua;" .. love.filesystem.getRequirePath()
love.filesystem.setRequirePath(path)

-- love.system might not exist yet
if ffi and ffi.os == "OSX" then
package.cpath = package.cpath .. ";" .. love.filesystem.getSourceBaseDirectory() .. "/?.so"
end

-- Load faster unpack function
require("lib.fast_unpack")

-- Keeping it here since it is an option, and seems to make a difference at some points
-- Attempt to expose to config option at some point
--[[
_G._selene = {}
_G._selene.notypecheck = true
]]

require("selene").load()
require("selene/selene/wrappers/searcher/love2d/searcher").load()
22 changes: 1 addition & 21 deletions src/main.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
function love.load()
local path = "selene/selene/lib/?.lua;selene/selene/lib/?/init.lua;?/?.lua;" .. love.filesystem.getRequirePath()
love.filesystem.setRequirePath(path)

-- The English language contains over 500,000 words and not a single one of them is suitable for describing just how much I want to purge macos from existence.
if love.system.getOS() == "OS X" then
package.cpath = package.cpath .. ";" .. love.filesystem.getSourceBaseDirectory() .. "/?.so"
end

-- Load faster unpack function
require("lib.fast_unpack")

-- Keeping it here since it is an option, and seems to make a difference at some points
-- Attempt to expose to config option at some point
--[[
_G._selene = {}
_G._selene.notypecheck = true
]]

require("selene").load()
require("selene/selene/wrappers/searcher/love2d/searcher").load()

require("lua_setup")
require("selene_main")
end

Expand Down
3 changes: 0 additions & 3 deletions src/selene_main.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
-- love.load() is not called again, put stuff here.

local logging = require("logging")
local meta = require("meta")

love.window.setTitle(meta.title)

love.keyboard.setKeyRepeat(true)

Expand Down
8 changes: 5 additions & 3 deletions src/utils/filesystem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ local hasRequest, request = requireUtils.tryrequire("lib.luajit-request.luajit-r

local filesystem = {}

filesystem.supportWindowsInThreads = love.system.getOS() ~= "OS X"
function filesystem.supportWindowsInThreads()
return love.system.getOS() ~= "OS X"
end

function filesystem.filename(path, sep)
sep = sep or physfs.getDirSeparator()
Expand Down Expand Up @@ -176,7 +178,7 @@ function filesystem.saveDialog(path, filter, callback)
-- TODO - Verify arguments, documentation was very existant

if callback then
if filesystem.supportWindowsInThreads then
if filesystem.supportWindowsInThreads() then
local code = [[
local args = {...}
local channelName, path, filter = unpack(args)
Expand Down Expand Up @@ -207,7 +209,7 @@ function filesystem.openDialog(path, filter, callback)
-- TODO - Verify arguments, documentation was very existant

if callback then
if filesystem.supportWindowsInThreads then
if filesystem.supportWindowsInThreads() then
local code = [[
local args = {...}
local channelName, path, filter = unpack(args)
Expand Down
19 changes: 18 additions & 1 deletion src/utils/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local filesystem = require("utils.filesystem")
local requireUtils = require("utils.require")
local xnaColors = require("consts.xna_colors")
local bit = require("bit")
local ffi = require("ffi")

local rectangles = require("structs.rectangle")

Expand Down Expand Up @@ -677,6 +678,22 @@ function utils.round(n, decimals)
end
end

-- ffi to love.system names
local ffiOSLookup = {
Windows = "Windows",
Linux = "Linux",
OSX = "OS X"
}

function utils.getOS()
if love.system then
return love.system.getOS()
end

-- Fallback to ffi.os, some names differ but it is good enough
return ffiOSLookup[ffi.os]
end

-- Add all of require utils into utils
for k, v <- requireUtils do
utils[k] = v
Expand All @@ -689,7 +706,7 @@ for k, v <- filesystem do
end

-- Add filesystem specific helper methods
local osFilename = love.system.getOS():lower():gsub(" ", "_")
local osFilename = utils.getOS():lower():gsub(" ", "_")
local hasOSHelper, osHelper = requireUtils.tryrequire("utils.system." .. osFilename)

function utils.getProcessId()
Expand Down

0 comments on commit c2b2229

Please sign in to comment.