Skip to content

Commit d89b7af

Browse files
committed
Recipes can now be reloaded
1 parent 7eb84a4 commit d89b7af

4 files changed

Lines changed: 81 additions & 33 deletions

File tree

Scripts/Interactables/Crafter.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,14 @@ function Crafter.server_canErase( self )
127127
end
128128

129129
function Crafter.client_onCreate( self )
130+
cmi_crafter_object_storage[self.interactable.id] = self.interactable
131+
130132
self:cl_init()
131133
end
132134

133135
function Crafter.client_onDestroy( self )
136+
cmi_crafter_object_storage[self.interactable.id] = nil
137+
134138
for _,effect in ipairs( self.cl.mainEffects ) do
135139
effect:destroy()
136140
end
@@ -415,7 +419,7 @@ function Crafter.cl_updateRecipeGrid( self )
415419
self.cl.guiInterface:clearGrid( "RecipeGrid" )
416420
for _, recipeSet in ipairs( self.crafter.recipeSets ) do
417421
local cur_recipe_files = g_craftingRecipes[recipeSet.name].path
418-
print( "Adding", cur_recipe_files )
422+
print( "Crafter Adding", cur_recipe_files )
419423

420424
for k, recipe_path in pairs(cur_recipe_files) do
421425
self.cl.guiInterface:addGridItemsFromFile("RecipeGrid", recipe_path, { locked = recipeSet.locked })

Scripts/Interactables/HideoutTrader.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ end
241241
-- Client
242242

243243
function HideoutTrader.client_onCreate( self )
244+
cmi_hideout_trader_storage[self.interactable.id] = self.interactable
244245
self:cl_init()
245246
end
246247

@@ -288,6 +289,7 @@ function HideoutTrader.cl_init( self )
288289
end
289290

290291
function HideoutTrader.client_onDestroy(self)
292+
cmi_hideout_trader_storage[self.interactable.id] = nil
291293
-- Destroy animation effects
292294
for name, effect in pairs( self.cl.animationEffects ) do
293295
effect:stop()
@@ -297,6 +299,8 @@ end
297299
function HideoutTrader.cl_updateTradeGrid( self )
298300
self.cl.guiInterface:clearGrid( "TradeGrid" )
299301

302+
print("Hideout Add:", cmi_valid_crafting_recipes.hideout)
303+
300304
for k, path in ipairs(cmi_valid_crafting_recipes.hideout) do
301305
self.cl.guiInterface:addGridItemsFromFile("TradeGrid", path)
302306
end

Scripts/RecipeLoader.lua

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
dofile("$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+
311
local 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
5361
end
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()
125159
end

Scripts/SurvivalGame.lua

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,14 @@ function SurvivalGame.bindChatCommands( self )
245245
sm.game.bindChatCommand( "/reloadcell", {{ "int", "x", true }, { "int", "y", true }}, "cl_onChatCommand", "Reload cells at self or {x,y}" )
246246
sm.game.bindChatCommand( "/tutorialstartkit", {}, "cl_onChatCommand", "Spawn a starter kit for building a scrap car" )
247247

248-
249-
250-
251-
252-
248+
sm.game.bindChatCommand( "/reloadcraftingrecipes", {}, "cl_onReloadCraftingRecipes", "Reloads crafting recipes for the custom game (Can be useful for mod developers to make their mods compatible with this game mode)" )
253249
end
254250
end
255251

252+
function SurvivalGame.cl_onReloadCraftingRecipes(self)
253+
self:loadCraftingRecipes(true)
254+
end
255+
256256
function SurvivalGame.client_onClientDataUpdate( self, clientData, channel )
257257
if channel == 2 then
258258
self.cl.time = clientData.time
@@ -263,8 +263,10 @@ function SurvivalGame.client_onClientDataUpdate( self, clientData, channel )
263263
end
264264

265265

266-
function SurvivalGame.loadCraftingRecipes( self )
267-
initialize_crafting_recipes()
266+
function SurvivalGame.loadCraftingRecipes( self, ignore_cache )
267+
initialize_crafting_recipes(ignore_cache)
268+
269+
g_craftingRecipes = nil
268270

269271
LoadCraftingRecipes({
270272
workbench = cmi_valid_crafting_recipes.workbench,
@@ -273,6 +275,10 @@ function SurvivalGame.loadCraftingRecipes( self )
273275
craftbot = cmi_valid_crafting_recipes.craftbot,
274276
dressbot = "$SURVIVAL_DATA/CraftingRecipes/dressbot.json"
275277
})
278+
279+
if ignore_cache then
280+
cmi_update_all_crafters()
281+
end
276282
end
277283

278284
function SurvivalGame.server_onFixedUpdate( self, timeStep )

0 commit comments

Comments
 (0)