Skip to content

Commit

Permalink
refactor mod (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsor4035 authored May 26, 2024
1 parent eb7d9fd commit 5b4b19a
Show file tree
Hide file tree
Showing 18 changed files with 683 additions and 549 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Thanks to:

## Usage

See [DEV.md](DEV.md) for detailed documentation.
See the respective sub apis doc file in /doc for detailed documentation.

## Directly supported games and mods

Expand Down
12 changes: 12 additions & 0 deletions doc/gameid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# GameId API

## minetest versions >= 5.7

simply returns `minetest.get_game_info().id`

## minetest versions < 5.7

approximates the gameid value via a hardcoded table of gameid=>modname
and then checks via `minetest.get_modpath()`. If it fails, it falls
back to using `xcompat_unknown_gameid` as the id. See the chart in the
readme for which games are supported
3 changes: 3 additions & 0 deletions doc/materials.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Materials API

consult `/src/materials/minetest.lua` at this time
14 changes: 4 additions & 10 deletions DEV.md → doc/sounds.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Xcompat dev docs
# Sound API

## Sound API

### Option 1: Agnostically depend
## Option 1: Agnostically depend

You can do this by using a custom field in your node def instead of the `sounds` key.

Expand All @@ -22,7 +20,7 @@ where:
* key: string name of the field from the sound api you want to use, for example `node_sound_stone_defaults`
* input: table input of fields you want passed to the key field, used to override specific sounds.

### Option 2: Hard depend
## Option 2: Hard depend

add this mod to your mod.confs depends and directly call the sound_api as follows

Expand All @@ -34,8 +32,4 @@ minetest.register_node(nodename, {
})
```

* input: optional table to override some or all of returned values

## Materials API

consult `/src/materials.lua` at this time
* input: optional table to override some or all of returned values
3 changes: 3 additions & 0 deletions doc/textures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Textures API

consult `/src/texture/minetest.lua` at this time
13 changes: 10 additions & 3 deletions init.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
local modpath = minetest.get_modpath("xcompat")

xcompat = {
sounds = dofile(modpath .. "/src/sounds.lua"),
materials = dofile(modpath .. "/src/materials.lua"),
textures = dofile(modpath .. "/src/textures.lua"),
modpath = modpath,
}

xcompat.gameid = dofile(modpath .. "/src/gameid.lua")
xcompat.utilities = dofile(modpath .. "/src/utilities.lua")

xcompat.sounds = dofile(modpath .. "/src/sounds.lua")
xcompat.materials = dofile(modpath .. "/src/materials.lua")
xcompat.textures = dofile(modpath .. "/src/textures.lua")

local function validate_sound(key)
if key and xcompat.sounds[key] then
return true
Expand Down Expand Up @@ -35,3 +40,5 @@ minetest.register_on_mods_loaded(function()
old_reg_node(name, def)
end
end)

dofile(modpath .. "/src/commands.lua")
51 changes: 51 additions & 0 deletions src/commands.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
local materials_list = minetest.get_dir_list(xcompat.modpath.."/src/materials", false)
local materials = {}
for _, material in ipairs(materials_list) do
local gameid = material:sub(1, -5)
materials[gameid] = dofile(xcompat.modpath.."/src/materials/"..material)
end

local textures_list = minetest.get_dir_list(xcompat.modpath.."/src/textures", false)
local textures = {}
for _, texture in ipairs(textures_list) do
local gameid = texture:sub(1, -5)
textures[gameid] = dofile(xcompat.modpath.."/src/textures/"..texture)
end

minetest.register_chatcommand("xcompat_test_materials", {
description = "Test materials",
privs = {server=true},
func = function(name, _)
local reference_materials = materials["minetest"]

for gameid, game_materials in pairs(materials) do
for material, _ in pairs(reference_materials) do
if not game_materials[material] then
minetest.chat_send_player(name, "Missing material: "..material.." in game: "..gameid)
end
end
end

minetest.chat_send_player(name, "Materials test complete")
end
})

--WARNING: only handles top level of table currently
--TODO: handle nested tables
minetest.register_chatcommand("xcompat_test_textures", {
description = "Test textures",
privs = {server=true},
func = function(name, _)
local reference_textures = textures["xcompat_agnostic"]

for gameid, game_textures in pairs(textures) do
for texture, _ in pairs(reference_textures) do
if not game_textures[texture] then
minetest.chat_send_player(name, "Missing texture: "..texture.." in game: "..gameid)
end
end
end

minetest.chat_send_player(name, "Textures test complete")
end
})
30 changes: 30 additions & 0 deletions src/gameid.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local game_alias = {
mineclone2 = "mineclonia",
}

local game_modnames = {
mineclonia = "mcl_core",
farlands_reloaded = "fl_core",
minetest = "default",
hades = "hades_core",
exile = "exile_env_sounds",
ksurvive2 = "ks_metals",
}

local gameid = "xcompat_unknown_gameid"

if type(minetest.get_game_info) == "function" then
gameid = minetest.get_game_info().id
else
for game, modname in pairs(game_modnames) do
if minetest.get_modpath(modname) then
gameid = game
break
end
end
end

--for games that are similar/derviatives of other games
if game_alias[gameid] then gameid = game_alias[gameid] end

return gameid
Loading

0 comments on commit 5b4b19a

Please sign in to comment.