<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Xml.Serialization;
using Rynchodon.Update;
using Sandbox.ModAPI;
using VRage;
using VRage.Collections;
using VRage.ModAPI;

namespace Rynchodon.Utility.Network
{

	public abstract class EntityValue
	{

		[Serializable]
		public class Builder_EntityValues
		{
			[XmlAttribute]
			public long entityId;
			public byte[] valueIds;
			public string[] values;
		}

		protected class StaticVariables
		{
			public Dictionary<long, Dictionary<byte, EntityValue>> allEntityValues = new Dictionary<long, Dictionary<byte, EntityValue>>();
			public Logger logger = new Logger("EntityValue");
			public MyConcurrentPool<StringBuilder> updateSB = new MyConcurrentPool<StringBuilder>();
			public int bytesSent, messagesSent;
		}

		protected static StaticVariables Static = new StaticVariables();

		static EntityValue()
		{
			MyAPIGateway.Entities.OnCloseAll += Entities_OnCloseAll;
			MessageHandler.Handlers.Add(MessageHandler.SubMod.SyncEntityValue, Handle_SyncEntityValue);
			if (MyAPIGateway.Multiplayer.IsServer)
				MessageHandler.Handlers.Add(MessageHandler.SubMod.RequestEntityValue, Handle_RequestEntityValue);
		}

		private static void Entities_OnCloseAll()
		{
			Static.logger.debugLog("bytes sent: " + Static.bytesSent + ", messages sent: " + Static.messagesSent, Logger.severity.INFO);
			MyAPIGateway.Entities.OnCloseAll -= Entities_OnCloseAll;
			Static = null;
		}

		public static Builder_EntityValues[] GetBuilders()
		{
			if (Static == null)
				return null;

			List<Builder_EntityValues> all = new List<Builder_EntityValues>(Static.allEntityValues.Count);
			List<byte> valueIds = new List<byte>();
			List<string> values = new List<string>();

			foreach (KeyValuePair<long, Dictionary<byte, EntityValue>> entityValues in Static.allEntityValues)
			{
				valueIds.Clear();
				values.Clear();

				foreach (KeyValuePair<byte, EntityValue> byteValue in entityValues.Value)
				{
					if (!byteValue.Value.Save)
						continue;
					string value = byteValue.Value.GetValue();
					if (value != null)
					{
						valueIds.Add(byteValue.Key);
						values.Add(value);
					}
				}

				if (valueIds.Count != 0)
				{
					all.Add(new Builder_EntityValues()
					{
						entityId = entityValues.Key,
						valueIds = valueIds.ToArray(),
						values = values.ToArray()
					});
				}
			}

			return all.ToArray();
		}

		public static void ResumeFromSave(Builder_EntityValues[] savedValues)
		{
			if (Static == null)
				return;

			foreach (Builder_EntityValues builder in savedValues)
			{
				Dictionary<byte, EntityValue> entityValues;
				if (!Static.allEntityValues.TryGetValue(builder.entityId, out entityValues))
				{
					Static.logger.alwaysLog("Entity not found in world: " + builder.entityId, Logger.severity.WARNING);
					continue;
				}
				for (int index = 0; index < builder.valueIds.Length; index++)
				{
					EntityValue value;
					if (!entityValues.TryGetValue(builder.valueIds[index], out value))
					{
						Static.logger.alwaysLog("Entity value: " + builder.valueIds[index] + " is missing for " + builder.entityId, Logger.severity.WARNING);
						continue;
					}
					if (!string.IsNullOrEmpty(builder.values[index]))
						value.SetValue(builder.values[index]);
				}
			}
		}

		public static EntityValue TryGetEntityValue(long entityId, byte valueId)
		{
			if (Static == null)
				return null;

			Dictionary<byte, EntityValue> entityValues;
			if (!Static.allEntityValues.TryGetValue(entityId, out entityValues))
				return null;

			EntityValue value;
			if (!entityValues.TryGetValue(valueId, out value))
				return null;<commit_msg>logging<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Xml.Serialization;
using Rynchodon.Update;
using Sandbox.ModAPI;
using VRage;
using VRage.Collections;
using VRage.ModAPI;

namespace Rynchodon.Utility.Network
{

	public abstract class EntityValue
	{

		[Serializable]
		public class Builder_EntityValues
		{
			[XmlAttribute]
			public long entityId;
			public byte[] valueIds;
			public string[] values;
		}

		protected class StaticVariables
		{
			public Dictionary<long, Dictionary<byte, EntityValue>> allEntityValues = new Dictionary<long, Dictionary<byte, EntityValue>>();
			public Logger logger = new Logger("EntityValue");
			public MyConcurrentPool<StringBuilder> updateSB = new MyConcurrentPool<StringBuilder>();
			public int bytesSent, messagesSent;
		}

		protected static StaticVariables Static = new StaticVariables();

		static EntityValue()
		{
			MyAPIGateway.Entities.OnCloseAll += Entities_OnCloseAll;
			MessageHandler.Handlers.Add(MessageHandler.SubMod.SyncEntityValue, Handle_SyncEntityValue);
			if (MyAPIGateway.Multiplayer.IsServer)
				MessageHandler.Handlers.Add(MessageHandler.SubMod.RequestEntityValue, Handle_RequestEntityValue);
		}

		private static void Entities_OnCloseAll()
		{
			Static.logger.debugLog("bytes sent: " + Static.bytesSent + ", messages sent: " + Static.messagesSent, Logger.severity.INFO);
			MyAPIGateway.Entities.OnCloseAll -= Entities_OnCloseAll;
			Static = null;
		}

		public static Builder_EntityValues[] GetBuilders()
		{
			if (Static == null)
				return null;

			List<Builder_EntityValues> all = new List<Builder_EntityValues>(Static.allEntityValues.Count);
			List<byte> valueIds = new List<byte>();
			List<string> values = new List<string>();

			foreach (KeyValuePair<long, Dictionary<byte, EntityValue>> entityValues in Static.allEntityValues)
			{
				valueIds.Clear();
				values.Clear();

				foreach (KeyValuePair<byte, EntityValue> byteValue in entityValues.Value)
				{
					if (!byteValue.Value.Save)
						continue;
					string value = byteValue.Value.GetValue();
					if (value != null)
					{
						valueIds.Add(byteValue.Key);
						values.Add(value);
					}
				}

				if (valueIds.Count != 0)
				{
					all.Add(new Builder_EntityValues()
					{
						entityId = entityValues.Key,
						valueIds = valueIds.ToArray(),
						values = values.ToArray()
					});
				}
			}

			return all.ToArray();
		}

		public static void ResumeFromSave(Builder_EntityValues[] savedValues)
		{
			if (Static == null)
				return;

			foreach (Builder_EntityValues builder in savedValues)
			{
				Dictionary<byte, EntityValue> entityValues;
				if (!Static.allEntityValues.TryGetValue(builder.entityId, out entityValues))
				{
					Static.logger.alwaysLog("Entity not found in world: " + builder.entityId, Logger.severity.WARNING);
					continue;
				}
				for (int index = 0; index < builder.valueIds.Length; index++)
				{
					EntityValue value;
					if (!entityValues.TryGetValue(builder.valueIds[index], out value))
					{
						IMyEntity entity;
						if (MyAPIGateway.Entities.TryGetEntityById(builder.entityId, out entity))
							Static.logger.alwaysLog("Entity value: " + builder.valueIds[index] + " is missing for " + entity.nameWithId(), Logger.severity.WARNING);
						else
							Static.logger.alwaysLog("Entity value: " + builder.valueIds[index] + " is missing for " + builder.entityId, Logger.severity.WARNING);
						continue;
					}
					if (!string.IsNullOrEmpty(builder.values[index]))
						value.SetValue(builder.values[index]);
				}
			}
		}

		public static EntityValue TryGetEntityValue(long entityId, byte valueId)
		{
			if (Static == null)
				return null;

			Dictionary<byte, EntityValue> entityValues;
			if (!Static.allEntityValues.TryGetValue(entityId, out entityValues))
				return null;

			EntityValue value;
			if (!entityValues.TryGetValue(valueId, out value))
				return null;
{'fraction_non_alphanumeric': 0.10787894599530394, 'fraction_numerical': 0.0005217845030002609, 'mean_word_length': 3.8990415335463258, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 37, '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': '2541429', 'n_tokens_mistral': 2840, 'n_tokens_neox': 2260, 'n_words': 494}