<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using SRPCommon.Util;

namespace SRPCommon.Scene
{
	// Implementation of load and save functions for scenes.
	// Split into a separate file simply for cleanliness.
	public partial class Scene
	{
		static private JsonSerializerSettings _serializerSettings = new JsonSerializerSettings()
		{
			Formatting = Formatting.Indented,
			ContractResolver = new SceneContractResolver(),
			Converters = { new PrimitiveCreationConverter() }
		};

		// Load an existing scene from disk.
		public static Scene LoadFromFile(string filename)
		{
			// Any relative paths are relative to the scene file itself.
			// TODO: do this more elegantly.
			var prevCurrentDir = Environment.CurrentDirectory;
			Environment.CurrentDirectory = Path.GetDirectoryName(filename);

			try
			{
				// Load JSON file.
				var contents = File.ReadAllText(filename);
				var result = JsonConvert.DeserializeObject<Scene>(contents, _serializerSettings);

				result.filename = filename;

				result.PostLoad();
				result.NotifyChanged();

				Environment.CurrentDirectory = prevCurrentDir;
				return result;
			}
			catch (IOException ex)
			{
				OutputLogger.Instance.LogLine(LogCategory.Log, "Failed to load scene {0}", filename);
				OutputLogger.Instance.LogLine(LogCategory.Log, ex.Message);
				return null;
			}
			catch (JsonException ex)
			{
				OutputLogger.Instance.LogLine(LogCategory.Log, "Failed to parse scene {0}", filename);
				OutputLogger.Instance.LogLine(LogCategory.Log, ex.Message);
				return null;
			}
			catch (Exception ex)
			{
				// Catch-all for any exception thrown during the parsing process in case of malformed scene.
				// TODO: Perhaps be more selective?
				OutputLogger.Instance.LogLine(LogCategory.Log, "Scene is invalid: {0}", filename);
				OutputLogger.Instance.LogLine(LogCategory.Log, ex.Message);
				return null;
			}
		}

		public void Save()
		{
			// Serialise and write to file.
			var json = JsonConvert.SerializeObject(this, _serializerSettings);
			File.WriteAllText(filename + ".test", json);
		}

		private void PostLoad()
		{
			// Fix up mesh and material names after serialisation.
			foreach (var kvp in Meshes)
			{
				kvp.Value.Name = kvp.Key;
			}
			foreach (var kvp in Materials)
			{
				kvp.Value.Name = kvp.Key;
			}

			// Call PostLoad on sub-objects.
			Meshes.Values.ForEach(mesh => mesh.PostLoad());
			Primitives.ForEach(mesh => mesh.PostLoad(this));
		}<commit_msg>Save scene to the actual filename<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using SRPCommon.Util;

namespace SRPCommon.Scene
{
	// Implementation of load and save functions for scenes.
	// Split into a separate file simply for cleanliness.
	public partial class Scene
	{
		static private JsonSerializerSettings _serializerSettings = new JsonSerializerSettings()
		{
			Formatting = Formatting.Indented,
			ContractResolver = new SceneContractResolver(),
			Converters = { new PrimitiveCreationConverter() }
		};

		// Load an existing scene from disk.
		public static Scene LoadFromFile(string filename)
		{
			// Any relative paths are relative to the scene file itself.
			// TODO: do this more elegantly.
			var prevCurrentDir = Environment.CurrentDirectory;
			Environment.CurrentDirectory = Path.GetDirectoryName(filename);

			try
			{
				// Load JSON file.
				var contents = File.ReadAllText(filename);
				var result = JsonConvert.DeserializeObject<Scene>(contents, _serializerSettings);

				result.filename = filename;

				result.PostLoad();
				result.NotifyChanged();

				Environment.CurrentDirectory = prevCurrentDir;
				return result;
			}
			catch (IOException ex)
			{
				OutputLogger.Instance.LogLine(LogCategory.Log, "Failed to load scene {0}", filename);
				OutputLogger.Instance.LogLine(LogCategory.Log, ex.Message);
				return null;
			}
			catch (JsonException ex)
			{
				OutputLogger.Instance.LogLine(LogCategory.Log, "Failed to parse scene {0}", filename);
				OutputLogger.Instance.LogLine(LogCategory.Log, ex.Message);
				return null;
			}
			catch (Exception ex)
			{
				// Catch-all for any exception thrown during the parsing process in case of malformed scene.
				// TODO: Perhaps be more selective?
				OutputLogger.Instance.LogLine(LogCategory.Log, "Scene is invalid: {0}", filename);
				OutputLogger.Instance.LogLine(LogCategory.Log, ex.Message);
				return null;
			}
		}

		public void Save()
		{
			// Serialise and write to file.
			var json = JsonConvert.SerializeObject(this, _serializerSettings);
			File.WriteAllText(filename, json);
		}

		private void PostLoad()
		{
			// Fix up mesh and material names after serialisation.
			foreach (var kvp in Meshes)
			{
				kvp.Value.Name = kvp.Key;
			}
			foreach (var kvp in Materials)
			{
				kvp.Value.Name = kvp.Key;
			}

			// Call PostLoad on sub-objects.
			Meshes.Values.ForEach(mesh => mesh.PostLoad());
			Primitives.ForEach(mesh => mesh.PostLoad(this));
		}
{'fraction_non_alphanumeric': 0.09854294478527607, 'fraction_numerical': 0.0011503067484662577, 'mean_word_length': 3.572304995617879, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '1351688', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1493, 'n_words': 424}