Skip to content

Commit 54c4bdc

Browse files
committed
Better recipe detection system
1 parent cd484ee commit 54c4bdc

1 file changed

Lines changed: 70 additions & 43 deletions

File tree

Scripts/RecipeLoader.lua

Lines changed: 70 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,99 @@
11
dofile("$CONTENT_40639a2c-bb9f-4d4f-b88c-41bfe264ffa8/Scripts/ModDatabase.lua")
2-
ModDatabase.loadShapesets()
32

4-
local sv_mod_uuid_table = ModDatabase.getAllLoadedMods()
3+
local function is_uuid_valid(uuid)
4+
local s_item = sm.item
5+
return s_item.isBlock(uuid) or s_item.isHarvestablePart(uuid) or s_item.isJoint(uuid) or s_item.isPart(uuid) or s_item.isTool(uuid)
6+
end
57

68
local function load_recipes_and_store_in_table(path, out_table)
7-
if sm.json.fileExists(path) then
8-
local success, json_data = pcall(sm.json.open, path)
9-
if success == true then
10-
print("Loading modded crafting recipes:", path)
9+
local success, file_exists = pcall(sm.json.fileExists, path)
10+
if not (success == true and file_exists == true) then
11+
return
12+
end
1113

12-
for k, mod_recipe in ipairs(json_data) do
13-
out_table[#out_table + 1] = mod_recipe
14-
end
14+
local success, json_data = pcall(sm.json.open, path)
15+
if success ~= true then
16+
return
17+
end
18+
19+
local temp_recipe_storage = {}
20+
21+
local l_table_insert = table.insert
22+
local l_uuid_new = sm.uuid.new
23+
24+
for k, mod_recipe in ipairs(json_data) do
25+
if mod_recipe.craftTime == nil then
26+
mod_recipe.craftTime = 30
27+
end
28+
29+
local success, item_uuid = pcall(l_uuid_new, mod_recipe.itemId)
30+
if success == true and is_uuid_valid(item_uuid) then
31+
l_table_insert(temp_recipe_storage, mod_recipe)
32+
else
33+
sm.log.warning("Found an invalid recipe in: ", path, item_uuid)
34+
return
1535
end
1636
end
37+
38+
print("[CraftbotRecipes] Successfully loaded crafting recipes from:", path)
39+
for k, v in ipairs(temp_recipe_storage) do
40+
out_table[#out_table + 1] = v
41+
end
1742
end
1843

1944
--this list contains the mods that already have crafting recipes, but they will most likely not be compatible with the custom game
2045
local mod_exception_list =
2146
{
22-
["f5452069-d631-422c-a1dc-2957c935cbaa"] =
23-
{
24-
craftbot = "/CraftingRecipes/nomad_default.json"
25-
},
26-
["df10d497-a28e-4413-a707-5a07813aec37"] =
47+
["df10d497-a28e-4413-a707-5a07813aec37"] = --wings mod
2748
{
2849
craftbot = "/Survival/CraftingRecipes/craftbot.json"
2950
}
3051
}
3152

32-
local function load_modded_crafting_recipes(out_table, file_name)
33-
for k, mod_uuid in ipairs(sv_mod_uuid_table) do
53+
cmi_merged_recipes_paths =
54+
{
55+
craftbot = "$CONTENT_DATA/Scripts/MergedRecipes.json",
56+
workbench = "$CONTENT_DATA/Scripts/WorkbenchMergedRecipes.json",
57+
hideout = "$CONTENT_DATA/Scripts/HideoutMergedRecipes.json"
58+
}
59+
60+
function merge_custom_crafting_recipes()
61+
ModDatabase.loadDescriptions()
62+
63+
local craftbot_recipes = {}
64+
local workbench_recipes = {}
65+
local hideout_recipes = {}
66+
67+
load_recipes_and_store_in_table("$SURVIVAL_DATA/CraftingRecipes/craftbot.json" , craftbot_recipes )
68+
load_recipes_and_store_in_table("$SURVIVAL_DATA/CraftingRecipes/workbench.json", workbench_recipes)
69+
load_recipes_and_store_in_table("$SURVIVAL_DATA/CraftingRecipes/hideout.json" , hideout_recipes )
70+
71+
for mod_uuid, v in pairs(ModDatabase.databases.descriptions) do
3472
local cur_exception = mod_exception_list[mod_uuid]
3573
local mod_key = "$CONTENT_"..mod_uuid
3674

3775
if cur_exception == nil then
38-
local full_path = mod_key.."/CraftingRecipes/"..file_name..".json"
76+
local recipe_folder = mod_key.."/CraftingRecipes/"
3977

40-
load_recipes_and_store_in_table(full_path, out_table)
78+
load_recipes_and_store_in_table(recipe_folder.."craftbot.json", craftbot_recipes)
79+
load_recipes_and_store_in_table(recipe_folder.."workbench.json", workbench_recipes)
80+
load_recipes_and_store_in_table(recipe_folder.."hideout.json", hideout_recipes)
4181
else
42-
local cur_exc_path = cur_exception[file_name]
43-
if cur_exc_path ~= nil then
44-
local full_path = mod_key..cur_exc_path
82+
if cur_exception.craftbot then
83+
load_recipes_and_store_in_table(mod_key..cur_exception.craftbot, craftbot_recipes)
84+
end
4585

46-
load_recipes_and_store_in_table(full_path, out_table)
86+
if cur_exception.workbench then
87+
load_recipes_and_store_in_table(mod_key..cur_exception.workbench, workbench_recipes)
88+
end
89+
90+
if cur_exception.hideout then
91+
load_recipes_and_store_in_table(mod_key..cur_exception.hideout, hideout_recipes)
4792
end
4893
end
4994
end
50-
end
51-
52-
cmi_merged_recipes_paths =
53-
{
54-
craftbot = "$CONTENT_DATA/Scripts/MergedRecipes.json",
55-
workbench = "$CONTENT_DATA/Scripts/WorkbenchMergedRecipes.json",
56-
hideout = "$CONTENT_DATA/Scripts/HideoutMergedRecipes.json"
57-
}
58-
59-
local function merge_craftbot_recipes(game_path, name)
60-
local merged_recipes = {}
6195

62-
load_recipes_and_store_in_table(game_path, merged_recipes)
63-
load_modded_crafting_recipes(merged_recipes, name)
64-
65-
sm.json.save(merged_recipes, cmi_merged_recipes_paths[name])
66-
end
67-
68-
function merge_custom_crafting_recipes()
69-
merge_craftbot_recipes("$SURVIVAL_DATA/CraftingRecipes/craftbot.json", "craftbot")
70-
merge_craftbot_recipes("$SURVIVAL_DATA/CraftingRecipes/workbench.json", "workbench")
71-
merge_craftbot_recipes("$SURVIVAL_DATA/CraftingRecipes/hideout.json", "hideout")
96+
sm.json.save(craftbot_recipes, cmi_merged_recipes_paths.craftbot)
97+
sm.json.save(workbench_recipes, cmi_merged_recipes_paths.workbench)
98+
sm.json.save(hideout_recipes, cmi_merged_recipes_paths.hideout)
7299
end

0 commit comments

Comments
 (0)