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"
item_data = {true, wand:Serialize(true, true), x, y, extra, is_new, {vx, vy}, sprite, varp}
else
item_data = {false, np.SerializeEntity(item), x, y}
item_data = {false, util.serialize_entity(item), x, y}
end
local item_cost_component = EntityGetFirstComponentIncludingDisabled(item, "ItemCostComponent")
if item_cost_component and item_cost_component ~= 0 then
@ -146,8 +146,7 @@ function inventory_helper.deserialize_single_item(item_data)
end
-- EntityAddTag(item, "does_physics_update")
else
item = EntityCreateNew()
np.DeserializeEntity(item, item_data[2], x, y)
item = util.deserialize_entity(item_data[2], x, y)
end
if item_data.shop_info ~= nil then

View file

@ -292,4 +292,22 @@ function util.log(...)
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

View file

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

View file

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

View file

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

View file

@ -18,7 +18,7 @@ local function entity_changed()
ComponentSetValue2(damage_model, "wait_for_kill_flag_on_death", true)
end
rpc.change_entity({data = np.SerializeEntity(ctx.my_player.entity)})
rpc.change_entity({data = util.serialize_entity(ctx.my_player.entity)})
else
rpc.change_entity(nil)
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
local entity_that_shot = ComponentGetValue2(projectileComponent, "mEntityThatShot")
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
EntityAddTag(projectile_id, "ew_no_enemy_sync")
end
@ -99,16 +99,14 @@ end
rpc.opts_reliable()
function rpc.replicate_projectile(seri_ent, position_x, position_y, target_x, target_y)
local ent = EntityCreateNew()
np.DeserializeEntity(ent, seri_ent)
local ent = util.deserialize_entity(seri_ent)
EntityAddTag(ent, "ew_no_enemy_sync")
GameShootProjectile(ctx.rpc_player_data.entity, position_x, position_y, target_x, target_y, ent)
end
local function apply_seri_ent(player_data, seri_ent)
if seri_ent ~= nil then
local ent = EntityCreateNew()
np.DeserializeEntity(ent, seri_ent.data)
local ent = util.deserialize_entity(seri_ent.data)
EntityAddTag(ent, "ew_no_enemy_sync")
EntityAddTag(ent, "ew_client")