Skip to content

Commit

Permalink
style: format Lua files (cataclysmbnteam#3819)
Browse files Browse the repository at this point in the history
* build: add dprint-stylua plugin

we choose to use dprint because:
- its plugin system is useful when unifying formatters
- deno uses dprint internally, thus less migration debt
- lua IDE formatter is hard to set up

* docs: lua style guide

* style: format lua files

---------

Co-authored-by: nocontribute <>
  • Loading branch information
scarf005 authored Dec 6, 2023
1 parent fd9f745 commit f3d7db3
Show file tree
Hide file tree
Showing 26 changed files with 1,302 additions and 1,214 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/autofix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ jobs:
- name: format C++ files
run: make astyle

- name: format markdown and typescript files
- name: format Markdown and TypeScript files
run: deno fmt

- name: json formatting
- name: format Lua files
run: deno task dprint fmt

- name: format JSON
run: make style-all-json-parallel RELEASE=1

- uses: autofix-ci/action@bee19d72e71787c12ca0f29de72f2833e437e4c9
Expand Down
39 changes: 21 additions & 18 deletions data/mods/saveload_lua_test/main.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
gdebug.log_info("SLT: main")

local mod = game.mod_runtime[ game.current_mod ]
local storage = game.mod_storage[ game.current_mod ]
local mod = game.mod_runtime[game.current_mod]
local storage = game.mod_storage[game.current_mod]

--[[
If we keep all our data simple and in mod.storage,
we won't even have to register save/load hooks,
it'll be saved/loaded automatically.
]]--
]]
--
mod.storage = storage

--[[
Expand All @@ -19,33 +20,35 @@ storage.num = 12.3
-- Usertype
storage.tri = Tripoint.new(3, 4, 5)
-- String
storage.tri_as_str = tostring( Tripoint.new(3, 4, 5) )
storage.tri_as_str = tostring(Tripoint.new(3, 4, 5))

--[[
If we want to build complex state out of loaded data
we may create a hook that would read loaded data from mod_storage
and create our complex state in the mod_runtime.
]]--
]]
--
mod.on_game_load_hook = function()
gdebug.log_info("SLT: on_load")
if storage.num then
gdebug.log_info( "Data found! num = ", storage.num )
end
if storage.tri then
gdebug.log_info( "Data found! tri = ", storage.tri )
end
gdebug.log_info("SLT: on_load")

if storage.num then
gdebug.log_info("Data found! num = ", storage.num)
end
if storage.tri then
gdebug.log_info("Data found! tri = ", storage.tri)
end
end

--[[
If we have complex enough state (e.g. recursive tables, or with custom metatables)
we may create a hook that would write a simplified version into mod_storage,
so the hardcoded JSON serializer would be able to handle it.
]]--
]]
--
mod.on_game_save_hook = function()
gdebug.log_info("SLT: on_save")
gdebug.log_info("SLT: on_save")

if storage.num then
gdebug.log_info("Saving NUM value = ", storage.num)
end
if storage.num then
gdebug.log_info("Saving NUM value = ", storage.num)
end
end
10 changes: 5 additions & 5 deletions data/mods/saveload_lua_test/preload.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
gdebug.log_info("SLT: preload")

local mod = game.mod_runtime[ game.current_mod ]
local mod = game.mod_runtime[game.current_mod]

game.hooks.on_game_load[ #game.hooks.on_game_load + 1 ] = function( ... )
return mod.on_game_load_hook( ... )
game.hooks.on_game_load[#game.hooks.on_game_load + 1] = function(...)
return mod.on_game_load_hook(...)
end

game.hooks.on_game_save[ #game.hooks.on_game_save + 1 ] = function( ... )
return mod.on_game_save_hook( ... )
game.hooks.on_game_save[#game.hooks.on_game_save + 1] = function(...)
return mod.on_game_save_hook(...)
end
Loading

0 comments on commit f3d7db3

Please sign in to comment.