<commit_before>﻿using System;
using System.Collections.Generic;
using UnityEngine;
using KSP.Localization;

namespace KERBALISM
{

	public sealed class Drive
	{
		public Drive(string name, double dataCapacity, int sampleCapacity, bool is_private = false)
		{
			this.files = new Dictionary<SubjectData, File>();
			this.samples = new Dictionary<SubjectData, Sample>();
			this.fileSendFlags = new Dictionary<string, bool>();
			this.dataCapacity = dataCapacity;
			this.sampleCapacity = sampleCapacity;
			this.name = name;
		}

		public Drive(ConfigNode node)
		{
			// parse science  files
			files = new Dictionary<SubjectData, File>();
			if (node.HasNode("files"))
			{
				foreach (var file_node in node.GetNode("files").GetNodes())
				{
					string subject_id = DB.From_safe_key(file_node.name);
					File file = File.Load(subject_id, file_node);
					if (file != null)
					{
						files.Add(file.subjectData, file);
						file.subjectData.AddDataCollectedInFlight(file.size);
					}
					else
					{
						file = File.LoadOldFormat(subject_id, file_node);
						if (file != null)
						{
							Lib.Log("Drive file load : converted '" + subject_id + "' to new format");
							files.Add(file.subjectData, file);
							file.subjectData.AddDataCollectedInFlight(file.size);
						}
					}
				}
			}

			// parse science samples
			samples = new Dictionary<SubjectData, Sample>();
			if (node.HasNode("samples"))
			{
				foreach (var sample_node in node.GetNode("samples").GetNodes())
				{
					string subject_id = DB.From_safe_key(sample_node.name);
					Sample sample = Sample.Load(subject_id, sample_node);
					if (sample != null)
					{
						samples.Add(sample.subjectData, sample);
						sample.subjectData.AddDataCollectedInFlight(sample.size);
					}
					else<commit_msg>Added some checks to avoid corrupting the science data base when loading a save with duplicate IDs<commit_after>﻿using System;
using System.Collections.Generic;
using UnityEngine;
using KSP.Localization;

namespace KERBALISM
{

	public sealed class Drive
	{
		public Drive(string name, double dataCapacity, int sampleCapacity, bool is_private = false)
		{
			this.files = new Dictionary<SubjectData, File>();
			this.samples = new Dictionary<SubjectData, Sample>();
			this.fileSendFlags = new Dictionary<string, bool>();
			this.dataCapacity = dataCapacity;
			this.sampleCapacity = sampleCapacity;
			this.name = name;
		}

		public Drive(ConfigNode node)
		{
			// parse science  files
			files = new Dictionary<SubjectData, File>();
			if (node.HasNode("files"))
			{
				foreach (var file_node in node.GetNode("files").GetNodes())
				{
					string subject_id = DB.From_safe_key(file_node.name);
					File file = File.Load(subject_id, file_node);
					if (file != null)
					{
						if(files.ContainsKey(file.subjectData))
						{
							Lib.Log("Warning: discarding duplicate subject " + file.subjectData);
						}
						else
						{
							files.Add(file.subjectData, file);
							file.subjectData.AddDataCollectedInFlight(file.size);
						}
					}
					else
					{
						file = File.LoadOldFormat(subject_id, file_node);
						if (file != null)
						{
							Lib.Log("Drive file load : converted '" + subject_id + "' to new format");
							if(files.ContainsKey(file.subjectData))
							{
								Lib.Log("Warning: discarding duplicate converted subject " + file.subjectData);
							}
							else
							{
								files.Add(file.subjectData, file);
								file.subjectData.AddDataCollectedInFlight(file.size);
							}
						}
					}
				}
			}

			// parse science samples
			samples = new Dictionary<SubjectData, Sample>();
			if (node.HasNode("samples"))
			{
				foreach (var sample_node in node.GetNode("samples").GetNodes())
				{
					string subject_id = DB.From_safe_key(sample_node.name);
					Sample sample = Sample.Load(subject_id, sample_node);
					if (sample != null)
					{
						samples.Add(sample.subjectData, sample);
						sample.subjectData.AddDataCollectedInFlight(sample.size);
					}
					else
{'fraction_non_alphanumeric': 0.11586275484833417, 'fraction_numerical': 0.0, 'mean_word_length': 3.04321608040201, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '100546', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1202, 'n_words': 266}