Add a check for world state component getting sent

This commit is contained in:
IQuant 2024-10-21 14:14:35 +03:00
parent a5419e559b
commit 7854557e7e
6 changed files with 29 additions and 17 deletions

View file

@ -95,7 +95,7 @@ function inventory_helper.serialize_single_item(item)
local varp = EntityGetFilename(item) == "data/entities/items/wand_varpuluuta.xml" local varp = EntityGetFilename(item) == "data/entities/items/wand_varpuluuta.xml"
item_data = {true, wand:Serialize(true, true), x, y, extra, is_new, {vx, vy}, sprite, varp} item_data = {true, wand:Serialize(true, true), x, y, extra, is_new, {vx, vy}, sprite, varp}
else else
item_data = {false, np.SerializeEntity(item), x, y} item_data = {false, util.serialize_entity(item), x, y}
end end
local item_cost_component = EntityGetFirstComponentIncludingDisabled(item, "ItemCostComponent") local item_cost_component = EntityGetFirstComponentIncludingDisabled(item, "ItemCostComponent")
if item_cost_component and item_cost_component ~= 0 then if item_cost_component and item_cost_component ~= 0 then
@ -146,8 +146,7 @@ function inventory_helper.deserialize_single_item(item_data)
end end
-- EntityAddTag(item, "does_physics_update") -- EntityAddTag(item, "does_physics_update")
else else
item = EntityCreateNew() item = util.deserialize_entity(item_data[2], x, y)
np.DeserializeEntity(item, item_data[2], x, y)
end end
if item_data.shop_info ~= nil then if item_data.shop_info ~= nil then

View file

@ -292,4 +292,22 @@ function util.log(...)
end end
end end
function util.serialize_entity(ent)
-- Serialized entities usually get sent to other clients, and it's a very bad idea to try and send them another WorldState.
if EntityHasTag(ent, "world_state") or EntityGetFirstComponentIncludingDisabled(ent, "WorldStateComponent") ~= nil then
error("Tried to serialize WorldStateEntity")
end
return np.SerializeEntity(ent)
end
function util.deserialize_entity(ent_data, x, y)
local ent = EntityCreateNew()
np.DeserializeEntity(ent, ent_data, x, y)
if EntityGetFirstComponentIncludingDisabled(ent, "WorldStateComponent") ~= nil then
error("Tried to deserialize WorldStateEntity. The world is screwed.")
EntityKill(ent)
end
return ent
end
return util return util

View file

@ -740,7 +740,7 @@ function enemy_sync.on_projectile_fired(shooter_id, projectile_id, initial_rng,
if projectileComponent ~= nil then if projectileComponent ~= nil then
local entity_that_shot = ComponentGetValue2(projectileComponent, "mEntityThatShot") local entity_that_shot = ComponentGetValue2(projectileComponent, "mEntityThatShot")
if entity_that_shot == 0 then if entity_that_shot == 0 then
rpc.replicate_projectile(np.SerializeEntity(projectile_id), position_x, position_y, target_x, target_y, shooter_id, initial_rng) rpc.replicate_projectile(util.serialize_entity(projectile_id), position_x, position_y, target_x, target_y, shooter_id, initial_rng)
end end
end end
end end
@ -755,8 +755,7 @@ function rpc.replicate_projectile(seri_ent, position_x, position_y, target_x, ta
return return
end end
local source_ent = ctx.entity_by_remote_id[remote_source_ent].id local source_ent = ctx.entity_by_remote_id[remote_source_ent].id
local ent = EntityCreateNew() local ent = util.deserialize_entity(seri_ent)
np.DeserializeEntity(ent, seri_ent)
GameShootProjectile(source_ent, position_x, position_y, target_x, target_y, ent) GameShootProjectile(source_ent, position_x, position_y, target_x, target_y, ent)
end end

View file

@ -54,7 +54,7 @@ function effect_sync.get_sync_data(entity, perks)
table.insert(sync_data, name) table.insert(sync_data, name)
end end
else else
table.insert(sync_data, np.SerializeEntity(effect)) table.insert(sync_data, util.serialize_entity(effect))
end end
end end
return sync_data return sync_data
@ -115,8 +115,7 @@ function effect_sync.apply_effects(effects, entity, perks)
elseif type(effect) == "number" then elseif type(effect) == "number" then
name = constants.game_effects[effect] name = constants.game_effects[effect]
else else
local serialized = EntityCreateNew() local serialized = util.deserialize_entity(effect)
np.DeserializeEntity(serialized, effect)
local com = EntityGetFirstComponentIncludingDisabled(serialized, "GameEffectComponent") local com = EntityGetFirstComponentIncludingDisabled(serialized, "GameEffectComponent")
local effect_name = get_name(serialized) local effect_name = get_name(serialized)
for _, old_effect in ipairs(old_local_effects) do for _, old_effect in ipairs(old_local_effects) do

View file

@ -41,8 +41,7 @@ local function end_poly_effect(ent)
return return
end end
local x, y = EntityGetTransform(ent) local x, y = EntityGetTransform(ent)
local new_ent = EntityCreateNew() local new_ent = util.deserialize_entity(base64.decode(serialized), x, y)
np.DeserializeEntity(new_ent, base64.decode(serialized), x, y)
np.SetPlayerEntity(new_ent) np.SetPlayerEntity(new_ent)
EntityKill(ent) EntityKill(ent)
GameAddFlagRun("ew_cam_wait") GameAddFlagRun("ew_cam_wait")

View file

@ -18,7 +18,7 @@ local function entity_changed()
ComponentSetValue2(damage_model, "wait_for_kill_flag_on_death", true) ComponentSetValue2(damage_model, "wait_for_kill_flag_on_death", true)
end end
rpc.change_entity({data = np.SerializeEntity(ctx.my_player.entity)}) rpc.change_entity({data = util.serialize_entity(ctx.my_player.entity)})
else else
rpc.change_entity(nil) rpc.change_entity(nil)
local controls = EntityGetFirstComponentIncludingDisabled(ctx.my_player.entity, "ControlsComponent") local controls = EntityGetFirstComponentIncludingDisabled(ctx.my_player.entity, "ControlsComponent")
@ -90,7 +90,7 @@ function module.on_projectile_fired(shooter_id, projectile_id, initial_rng, posi
if projectileComponent ~= nil then if projectileComponent ~= nil then
local entity_that_shot = ComponentGetValue2(projectileComponent, "mEntityThatShot") local entity_that_shot = ComponentGetValue2(projectileComponent, "mEntityThatShot")
if entity_that_shot == 0 then if entity_that_shot == 0 then
rpc.replicate_projectile(np.SerializeEntity(projectile_id), position_x, position_y, target_x, target_y) rpc.replicate_projectile(util.serialize_entity(projectile_id), position_x, position_y, target_x, target_y)
end end
EntityAddTag(projectile_id, "ew_no_enemy_sync") EntityAddTag(projectile_id, "ew_no_enemy_sync")
end end
@ -99,16 +99,14 @@ end
rpc.opts_reliable() rpc.opts_reliable()
function rpc.replicate_projectile(seri_ent, position_x, position_y, target_x, target_y) function rpc.replicate_projectile(seri_ent, position_x, position_y, target_x, target_y)
local ent = EntityCreateNew() local ent = util.deserialize_entity(seri_ent)
np.DeserializeEntity(ent, seri_ent)
EntityAddTag(ent, "ew_no_enemy_sync") EntityAddTag(ent, "ew_no_enemy_sync")
GameShootProjectile(ctx.rpc_player_data.entity, position_x, position_y, target_x, target_y, ent) GameShootProjectile(ctx.rpc_player_data.entity, position_x, position_y, target_x, target_y, ent)
end end
local function apply_seri_ent(player_data, seri_ent) local function apply_seri_ent(player_data, seri_ent)
if seri_ent ~= nil then if seri_ent ~= nil then
local ent = EntityCreateNew() local ent = util.deserialize_entity(seri_ent.data)
np.DeserializeEntity(ent, seri_ent.data)
EntityAddTag(ent, "ew_no_enemy_sync") EntityAddTag(ent, "ew_no_enemy_sync")
EntityAddTag(ent, "ew_client") EntityAddTag(ent, "ew_client")