11dofile (" $CONTENT_40639a2c-bb9f-4d4f-b88c-41bfe264ffa8/Scripts/ModDatabase.lua" )
22
3+ if not cmi_hideout_trader_storage then
4+ cmi_hideout_trader_storage = {}
5+ end
6+
7+ if not cmi_crafter_object_storage then
8+ cmi_crafter_object_storage = {}
9+ end
10+
311local function is_uuid_valid (uuid )
412 local s_item = sm .item
513 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 )
@@ -52,34 +60,15 @@ local function sort_valid_recipe_files(out_table_ref, input_table)
5260 end
5361end
5462
55- local cmi_recipe_cache_file = " $CONTENT_DATA/Scripts/CraftingRecipeCache.json"
56- function initialize_crafting_recipes ()
57- ModDatabase .loadDescriptions ()
58-
63+ local function clean_valid_recipes ()
5964 cmi_valid_crafting_recipes .craftbot = {}
6065 cmi_valid_crafting_recipes .workbench = {}
6166 cmi_valid_crafting_recipes .hideout = {}
67+ end
6268
63- -- read the last timestamp or make it random if it doesn't exist
64- local last_timestamp = math.random (0 , 10000 )
65- local success , timestamp_json = pcall (sm .json .open , " $CONTENT_40639a2c-bb9f-4d4f-b88c-41bfe264ffa8/Scripts/data/last_update.json" )
66- if success then
67- last_timestamp = timestamp_json .unix_timestamp
68- end
69-
70- local has_file = sm .json .fileExists (cmi_recipe_cache_file )
71- if has_file then
72- local json_data = sm .json .open (cmi_recipe_cache_file )
73- if json_data .time_stamp == last_timestamp then -- means we can skip the whole search of new crafting recipe files
74-
75- sort_valid_recipe_files (cmi_valid_crafting_recipes .craftbot , json_data .craftbot )
76- sort_valid_recipe_files (cmi_valid_crafting_recipes .workbench , json_data .workbench )
77- sort_valid_recipe_files (cmi_valid_crafting_recipes .hideout , json_data .hideout )
78-
79- return
80- end
81- end
82-
69+ local cmi_last_time_stamp = math.random (0 , 10000 )
70+ local cmi_recipe_cache_file = " $CONTENT_DATA/Scripts/CraftingRecipeCache.json"
71+ function cmi_scan_crafting_recipes_and_save ()
8372 local l_craftbot_recipes = { " $SURVIVAL_DATA/CraftingRecipes/craftbot.json" }
8473 local l_workbench_recipes = { " $SURVIVAL_DATA/CraftingRecipes/workbench.json" }
8574 local l_hideout_recipes = { " $SURVIVAL_DATA/CraftingRecipes/hideout.json" }
@@ -109,17 +98,62 @@ function initialize_crafting_recipes()
10998 end
11099 end
111100
101+ -- clean before setting new data
102+ clean_valid_recipes ()
103+
104+ -- set new data
112105 sort_valid_recipe_files (cmi_valid_crafting_recipes .craftbot , l_craftbot_recipes )
113106 sort_valid_recipe_files (cmi_valid_crafting_recipes .workbench , l_workbench_recipes )
114107 sort_valid_recipe_files (cmi_valid_crafting_recipes .hideout , l_hideout_recipes )
115108
116109 local json_save_data =
117110 {
118- time_stamp = last_timestamp ,
111+ time_stamp = cmi_last_time_stamp ,
119112 craftbot = l_craftbot_recipes ,
120113 workbench = l_workbench_recipes ,
121114 hideout = l_hideout_recipes
122115 }
123116
124117 sm .json .save (json_save_data , cmi_recipe_cache_file )
118+ end
119+
120+ local function cmi_update_crafters (crafter_array , callback )
121+ for k , inter in pairs (crafter_array ) do
122+ if inter and sm .exists (inter ) then
123+ sm .event .sendToInteractable (inter , callback )
124+ end
125+ end
126+ end
127+
128+ function cmi_update_all_crafters ()
129+ cmi_update_crafters (cmi_hideout_trader_storage , " cl_updateTradeGrid" )
130+ cmi_update_crafters (cmi_crafter_object_storage , " cl_updateRecipeGrid" )
131+ end
132+
133+ function initialize_crafting_recipes (ignore_cache )
134+ ModDatabase .loadDescriptions ()
135+
136+ -- read the last timestamp or make it random if it doesn't exist
137+ local success , timestamp_json = pcall (sm .json .open , " $CONTENT_40639a2c-bb9f-4d4f-b88c-41bfe264ffa8/Scripts/data/last_update.json" )
138+ if success then
139+ cmi_last_time_stamp = timestamp_json .unix_timestamp
140+ end
141+
142+ if ignore_cache == nil then
143+ local has_file = sm .json .fileExists (cmi_recipe_cache_file )
144+ if has_file then
145+ local json_data = sm .json .open (cmi_recipe_cache_file )
146+ if json_data .time_stamp == cmi_last_time_stamp then -- means we can skip the whole search of new crafting recipe files
147+ clean_valid_recipes ()
148+
149+ sort_valid_recipe_files (cmi_valid_crafting_recipes .craftbot , json_data .craftbot )
150+ sort_valid_recipe_files (cmi_valid_crafting_recipes .workbench , json_data .workbench )
151+ sort_valid_recipe_files (cmi_valid_crafting_recipes .hideout , json_data .hideout )
152+
153+ return
154+ end
155+ end
156+ end
157+
158+ cmi_scan_crafting_recipes_and_save ()
125159end
0 commit comments