<commit_before>﻿using System;
using System.Collections.Generic;
using UnityEngine;
using KSP.UI.Screens.Flight.Dialogs;
using KSP.Localization;

namespace KERBALISM
{


	// Remove the data from experiments (and set them inoperable) as soon as the
	// science dialog is opened, and store the data in the vessel drive.
	// This method support any module that set an appropriate OnDiscardData() callback
	// when opening the science dialog, this include stock science experiments and others.
	// Hiding the science dialog can be used by who doesn't want it.
	public sealed class MiniHijacker : MonoBehaviour
	{
		void Start()
		{
			// get dialog
			dialog = gameObject.GetComponentInParent<ExperimentsResultDialog>();
			if (dialog == null) { Destroy(gameObject); return; }

			// prevent rendering
			dialog.gameObject.SetActive(false);

			// for each page
			// - some mod may collect multiple experiments at once
			while (dialog.pages.Count > 0)
			{
				// get page
				var page = dialog.pages[0];

				// get science data
				ScienceData data = page.pageData;

				// collect and deduce all info necessary
				MetaData meta = new MetaData(data, page.host, page.xmitDataScalar);

				if (meta.subjectData == null)
					continue;

				// ignore non-collectable experiments
				if (!meta.is_collectable)
				{
					page.OnKeepData(data);
					continue;					
				}

				bool recorded = false;
				bool partial_record = false;

				if (!meta.is_sample)
				{
					var remaining = RecordData(data, meta);
					if (remaining > 0) partial_record = true;
					recorded = remaining < data.dataAmount;
				}
				else
				{
					Drive drive = Drive.SampleDrive(meta.vessel.KerbalismData(), data.dataAmount, meta.subjectData);
					if (drive != null)
						recorded = drive.Record_sample(meta.subjectData, data.dataAmount, meta.subjectData.ExpInfo.MassPerMB * data.dataAmount, true);
				}

				if (recorded)
				{
					if(!partial_record)
					{
						// render experiment inoperable if necessary
						if (!meta.is_rerunnable)
						{
							meta.experiment.SetInoperable();
						}

						// inform the user
						Message.Post(
							Lib.BuildString("<b>", meta.subjectData.FullTitle, "</b> recorded"),
							!meta.is_rerunnable ? Localizer.Format("#KERBALISM_Science_inoperable") : string.Empty
						);
					}

					// dump the data
					page.OnDiscardData(data);
				}
				else
				{
					Message.Post(
						Lib.Color(Lib.BuildString(meta.subjectData.FullTitle, " can not be stored"), Lib.Kolor.Red),
						"Not enough space on hard drive"
					);
				}
			}

			// dismiss the dialog
			dialog.Dismiss();
		}

		internal static double RecordData(ScienceData data, MetaData meta)
		{
			double remaining = data.dataAmount;

			foreach(var drive in Drive.GetDrives(meta.vessel.KerbalismData(), false))
			{
				var size = Math.Min(remaining, drive.FileCapacityAvailable());
				drive.Record_file(meta.subjectData, size, true, true);
				remaining -= size;
			}

			if (remaining > 0)
			{
				Message.Post(
					Lib.Color(Lib.BuildString(meta.subjectData.FullTitle, " stored partially"), Lib.Kolor.Orange),
					"Not enough space on hard drive"
				);<commit_msg>Avoid zero-size files<commit_after>﻿using System;
using System.Collections.Generic;
using UnityEngine;
using KSP.UI.Screens.Flight.Dialogs;
using KSP.Localization;

namespace KERBALISM
{


	// Remove the data from experiments (and set them inoperable) as soon as the
	// science dialog is opened, and store the data in the vessel drive.
	// This method support any module that set an appropriate OnDiscardData() callback
	// when opening the science dialog, this include stock science experiments and others.
	// Hiding the science dialog can be used by who doesn't want it.
	public sealed class MiniHijacker : MonoBehaviour
	{
		void Start()
		{
			// get dialog
			dialog = gameObject.GetComponentInParent<ExperimentsResultDialog>();
			if (dialog == null) { Destroy(gameObject); return; }

			// prevent rendering
			dialog.gameObject.SetActive(false);

			// for each page
			// - some mod may collect multiple experiments at once
			while (dialog.pages.Count > 0)
			{
				// get page
				var page = dialog.pages[0];

				// get science data
				ScienceData data = page.pageData;

				// collect and deduce all info necessary
				MetaData meta = new MetaData(data, page.host, page.xmitDataScalar);

				if (meta.subjectData == null)
					continue;

				// ignore non-collectable experiments
				if (!meta.is_collectable)
				{
					page.OnKeepData(data);
					continue;					
				}

				bool recorded = false;
				bool partial_record = false;

				if (!meta.is_sample)
				{
					var remaining = RecordData(data, meta);
					if (remaining > 0) partial_record = true;
					recorded = remaining < data.dataAmount;
				}
				else
				{
					Drive drive = Drive.SampleDrive(meta.vessel.KerbalismData(), data.dataAmount, meta.subjectData);
					if (drive != null)
						recorded = drive.Record_sample(meta.subjectData, data.dataAmount, meta.subjectData.ExpInfo.MassPerMB * data.dataAmount, true);
				}

				if (recorded)
				{
					if(!partial_record)
					{
						// render experiment inoperable if necessary
						if (!meta.is_rerunnable)
						{
							meta.experiment.SetInoperable();
						}

						// inform the user
						Message.Post(
							Lib.BuildString("<b>", meta.subjectData.FullTitle, "</b> recorded"),
							!meta.is_rerunnable ? Localizer.Format("#KERBALISM_Science_inoperable") : string.Empty
						);
					}

					// dump the data
					page.OnDiscardData(data);
				}
				else
				{
					Message.Post(
						Lib.Color(Lib.BuildString(meta.subjectData.FullTitle, " can not be stored"), Lib.Kolor.Red),
						"Not enough space on hard drive"
					);
				}
			}

			// dismiss the dialog
			dialog.Dismiss();
		}

		internal static double RecordData(ScienceData data, MetaData meta)
		{
			double remaining = data.dataAmount;

			foreach(var drive in Drive.GetDrives(meta.vessel.KerbalismData(), false))
			{
				var size = Math.Min(remaining, drive.FileCapacityAvailable());
				if(size > 0)
				{
					drive.Record_file(meta.subjectData, size, true, true);
					remaining -= size;
				}
			}

			if (remaining > 0)
			{
				Message.Post(
					Lib.Color(Lib.BuildString(meta.subjectData.FullTitle, " stored partially"), Lib.Kolor.Orange),
					"Not enough space on hard drive"
				);
{'fraction_non_alphanumeric': 0.10652003142183818, 'fraction_numerical': 0.0014139827179890023, 'mean_word_length': 2.837251356238698, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 16, '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': '1666175', 'n_tokens_mistral': 2532, 'n_tokens_neox': 1887, 'n_words': 518}