<commit_before>replication_system = inherits_from (processing_system)

function replication_system:constructor(owner_scene)
	self.object_by_id = {}
	self.owner_scene = owner_scene

	processing_system.constructor(self)
end

function replication_system:read_object_state(object, input_bs)
	-- read what modules have changed
	local replica = object.replication.modules
			
	for i=1, #protocol.module_mappings do
		local module_name = protocol.module_mappings[i]
		local module_object = replica[module_name]
	
		if module_object ~= nil then
			module_object:read_state(object, input_bs)
		end
	end
end

function replication_system:update_states_from_bitstream(msg)
	local input_bs = msg.input_bs
	
	for i=1, msg.data.object_count do
		input_bs:name_property("object_id")
		
		local id = input_bs:ReadUshort()
		protocol.LAST_READ_BITSTREAM = input_bs
		local object = self.object_by_id[id]
		
		self:read_object_state(object, input_bs)
	end
end

function replication_system:create_objects_or_change_modules(msg)
	local input_bs = msg.input_bs
	
	for i=1, msg.data.object_count do
		local new_object = {} 
		protocol.read_sig(protocol.new_object_signature, new_object, input_bs)
		
		local archetype_name = protocol.archetype_by_id[new_object.archetype_id]
		
		local is_object_new = self.object_by_id[new_object.id] == nil
		local replica = {}
		
		-- read what modules the replica has
		for i=1, #protocol.module_mappings do
			input_bs:name_property("has_module " .. i)
			if input_bs:ReadBit() then
				local module_name = protocol.module_mappings[i]
				
				replica[module_name] = replication_module:create(protocol.replication_tables[module_name])
			end
		end
			
		local new_entity;
		
		-- if the object is new
		if is_object_new then
			-- creation phase
			new_entity = world_archetype_callbacks[archetype_name].creation(self, new_object.id)
			
			-- save replication data (not as a component; just a table)
			new_entity.replication = { modules = replica, id = new_object.id }
			
			-- save the newly created entity
			self.object_by_id[new_object.id] = new_entity
			self.owner_entity_system:add_entity(new_entity)
		-- otherwise just update the replica
		else
			print "RECREATING OBJECT"
			new_entity = self.object_by_id[new_object.id]
			new_entity.replication.modules = replica
		end
		
		-- whole initial state is always read
		for i=1, #protocol.module_mappings do
			local module_name = protocol.module_mappings[i]
			if replica[module_name] ~= nil then
				replica[module_name]:read_initial_state(new_entity, input_bs)
			end
		end
			
		-- creation is necessary, though construction is not
		local construction_callback = world_archetype_callbacks[archetype_name].construction
		
		if construction_callback then 
			construction_callback (self, new_entity, is_object_new)
		end
	end
end

function replication_system:get_variable_message_size(msg)
	if msg.info.name == "STATE_UPDATE" then
		return msg.data.bits<commit_msg>adding the entity after construction phase<commit_after>replication_system = inherits_from (processing_system)

function replication_system:constructor(owner_scene)
	self.object_by_id = {}
	self.owner_scene = owner_scene

	processing_system.constructor(self)
end

function replication_system:read_object_state(object, input_bs)
	-- read what modules have changed
	local replica = object.replication.modules
			
	for i=1, #protocol.module_mappings do
		local module_name = protocol.module_mappings[i]
		local module_object = replica[module_name]
	
		if module_object ~= nil then
			module_object:read_state(object, input_bs)
		end
	end
end

function replication_system:update_states_from_bitstream(msg)
	local input_bs = msg.input_bs
	
	for i=1, msg.data.object_count do
		input_bs:name_property("object_id")
		
		local id = input_bs:ReadUshort()
		protocol.LAST_READ_BITSTREAM = input_bs
		local object = self.object_by_id[id]
		
		self:read_object_state(object, input_bs)
	end
end

function replication_system:create_objects_or_change_modules(msg)
	local input_bs = msg.input_bs
	
	for i=1, msg.data.object_count do
		local new_object = {} 
		protocol.read_sig(protocol.new_object_signature, new_object, input_bs)
		
		local archetype_name = protocol.archetype_by_id[new_object.archetype_id]
		
		local is_object_new = self.object_by_id[new_object.id] == nil
		local replica = {}
		
		-- read what modules the replica has
		for i=1, #protocol.module_mappings do
			input_bs:name_property("has_module " .. i)
			if input_bs:ReadBit() then
				local module_name = protocol.module_mappings[i]
				
				replica[module_name] = replication_module:create(protocol.replication_tables[module_name])
			end
		end
			
		local new_entity;
		
		-- if the object is new
		if is_object_new then
			-- creation phase
			new_entity = world_archetype_callbacks[archetype_name].creation(self, new_object.id)
			
			-- save replication data (not as a component; just a table)
			new_entity.replication = { modules = replica, id = new_object.id }
			
			-- save the newly created entity
			self.object_by_id[new_object.id] = new_entity
		-- otherwise just update the replica
		else
			print "RECREATING OBJECT"
			new_entity = self.object_by_id[new_object.id]
			new_entity.replication.modules = replica
		end
		
		-- whole initial state is always read
		for i=1, #protocol.module_mappings do
			local module_name = protocol.module_mappings[i]
			if replica[module_name] ~= nil then
				replica[module_name]:read_initial_state(new_entity, input_bs)
			end
		end
			
		-- creation is necessary, though construction is not
		local construction_callback = world_archetype_callbacks[archetype_name].construction
		
		if construction_callback then 
			construction_callback (self, new_entity, is_object_new)
		end
		
		-- call the system constructors after the archetype construction phase
		if is_object_new then
			self.owner_entity_system:add_entity(new_entity)
		end
	end
end

function replication_system:get_variable_message_size(msg)
	if msg.info.name == "STATE_UPDATE" then
		return msg.data.bits
{'fraction_non_alphanumeric': 0.07507058628134862, 'fraction_numerical': 0.0016608536787908986, 'mean_word_length': 4.622782446311858, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1560999', 'n_tokens_mistral': 2207, 'n_tokens_neox': 1919, 'n_words': 433}