Skip to content

Commit f94ceb2

Browse files
committed
Faster recipe loader
1 parent e8d3d6c commit f94ceb2

1 file changed

Lines changed: 38 additions & 23 deletions

File tree

Scripts/RecipeLoader.lua

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dofile("$CONTENT_40639a2c-bb9f-4d4f-b88c-41bfe264ffa8/Scripts/ModDatabase.lua")
2+
ModDatabase.unloadDescriptions()
23
ModDatabase.loadDescriptions()
34

45
if not cmi_hideout_trader_storage then
@@ -9,27 +10,30 @@ if not cmi_crafter_object_storage then
910
cmi_crafter_object_storage = {}
1011
end
1112

13+
local _sm_shape_uuidExists = sm.shape.uuidExists
14+
local _sm_tool_uuidExists = sm.tool.uuidExists
1215
local function is_uuid_valid(uuid)
13-
local s_item = sm.item
14-
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)
16+
return _sm_shape_uuidExists(uuid) or _sm_tool_uuidExists(uuid)
1517
end
1618

19+
local _sm_json_open = sm.json.open
20+
local _sm_uuid_new = sm.uuid.new
21+
local _sm_log_warning = sm.log.warning
1722
local function is_recipe_file_valid(path, table, uuid_check)
18-
local success, json_data = pcall(sm.json.open, path)
23+
local success, json_data = pcall(_sm_json_open, path)
1924
if success ~= true then
2025
return
2126
end
2227

2328
if uuid_check == true then
24-
local l_uuid_new = sm.uuid.new
2529
for k, mod_recipe in ipairs(json_data) do
2630
if mod_recipe.craftTime == nil then
2731
mod_recipe.craftTime = 27
2832
end
2933

30-
local success, item_uuid = pcall(l_uuid_new, mod_recipe.itemId)
34+
local success, item_uuid = pcall(_sm_uuid_new, mod_recipe.itemId)
3135
if not (success == true and is_uuid_valid(item_uuid)) then
32-
sm.log.warning("Found an invalid recipe in: ", path, item_uuid)
36+
_sm_log_warning("Found an invalid recipe in: ", path, item_uuid)
3337
return
3438
end
3539
end
@@ -67,10 +71,12 @@ local function clean_valid_recipes()
6771
cmi_valid_crafting_recipes.hideout = {}
6872
end
6973

74+
local _sm_exists = sm.exists
75+
local _sm_event_sendToInteractable = sm.event.sendToInteractable
7076
local function cmi_update_crafters(crafter_array, callback)
7177
for k, inter in pairs(crafter_array) do
72-
if inter and sm.exists(inter) then
73-
sm.event.sendToInteractable(inter, callback)
78+
if inter and _sm_exists(inter) then
79+
_sm_event_sendToInteractable(inter, callback)
7480
end
7581
end
7682
end
@@ -80,32 +86,41 @@ function cmi_update_all_crafters()
8086
cmi_update_crafters(cmi_crafter_object_storage, "cl_updateRecipeGrid")
8187
end
8288

89+
local _json_file_exists = sm.json.fileExists
8390
function initialize_crafting_recipes()
8491
local l_craftbot_recipes = { "$SURVIVAL_DATA/CraftingRecipes/craftbot.json" }
8592
local l_workbench_recipes = { "$SURVIVAL_DATA/CraftingRecipes/workbench.json" }
8693
local l_hideout_recipes = { "$SURVIVAL_DATA/CraftingRecipes/hideout.json" }
8794

8895
for mod_uuid, v in pairs(ModDatabase.databases.descriptions) do
89-
local cur_exception = mod_exception_list[mod_uuid]
9096
local mod_key = "$CONTENT_"..mod_uuid
9197

92-
if cur_exception == nil then
93-
local recipe_folder = mod_key.."/CraftingRecipes/"
98+
local success, fileExists = pcall(_json_file_exists, mod_key)
99+
if success == true and fileExists == true then
100+
local cur_exception = mod_exception_list[mod_uuid]
94101

95-
is_recipe_file_valid(recipe_folder.."craftbot.json" , l_craftbot_recipes )
96-
is_recipe_file_valid(recipe_folder.."workbench.json", l_workbench_recipes)
97-
is_recipe_file_valid(recipe_folder.."hideout.json" , l_hideout_recipes )
98-
else
99-
if cur_exception.craftbot then
100-
is_recipe_file_valid(mod_key..cur_exception.craftbot, l_craftbot_recipes)
101-
end
102+
if cur_exception == nil then
103+
local recipe_folder = mod_key.."/CraftingRecipes/"
102104

103-
if cur_exception.workbench then
104-
is_recipe_file_valid(mod_key..cur_exception.workbench, l_workbench_recipes)
105-
end
105+
is_recipe_file_valid(recipe_folder.."craftbot.json" , l_craftbot_recipes )
106+
is_recipe_file_valid(recipe_folder.."workbench.json", l_workbench_recipes)
107+
is_recipe_file_valid(recipe_folder.."hideout.json" , l_hideout_recipes )
108+
else
109+
local exc_craftbot = cur_exception.craftbot
110+
local exc_workbench = cur_exception.workbench
111+
local exc_hideout = cur_exception.hideout
112+
113+
if exc_craftbot then
114+
is_recipe_file_valid(mod_key..exc_craftbot, l_craftbot_recipes)
115+
end
116+
117+
if exc_workbench then
118+
is_recipe_file_valid(mod_key..exc_workbench, l_workbench_recipes)
119+
end
106120

107-
if cur_exception.hideout then
108-
is_recipe_file_valid(mod_key..cur_exception.hideout, l_hideout_recipes)
121+
if exc_hideout then
122+
is_recipe_file_valid(mod_key..exc_hideout, l_hideout_recipes)
123+
end
109124
end
110125
end
111126
end

0 commit comments

Comments
 (0)