<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using Terraria_Server.Commands;
using Terraria_Server.Plugins;
using Terraria_Server.Misc;
using Terraria_Server.Collections;
using Terraria_Server.Definitions;
using Terraria_Server.Logging;
using Terraria_Server.Networking;

namespace Terraria_Server.WorldMod
{
	public class WorldIO
	{
		private static object padlock = new object();
		public static bool worldCleared = false;
		private static int tempMoonPhase = Main.moonPhase;
		private static bool tempDayTime = Main.dayTime;
		private static bool tempBloodMoon = Main.bloodMoon;
		private static double tempTime = Main.time;

		public static void setWorldSize()
		{
			Main.bottomWorld = (float)(Main.maxTilesY * 16);
			Main.rightWorld = (float)(Main.maxTilesX * 16);
			Main.maxSectionsX = Main.maxTilesX / 200;
			Main.maxSectionsY = Main.maxTilesY / 150;
		}

        public static void SaveWorldCallback(object threadContext)
        {
            saveWorld(Server.World.SavePath, false);
        }

        public static void SaveWorldThreaded()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(SaveWorldCallback), 1);
        }

		public static void clearWorld()
		{
			Statics.WorldLoaded = false;
			
			Main.trashItem = new Item();
			WorldModify.spawnEye = false;
			WorldModify.spawnNPC = 0;
			WorldModify.shadowOrbCount = 0;
			Main.helpText = 0;
			Main.dungeonX = 0;
			Main.dungeonY = 0;
			NPC.downedBoss1 = false;
			NPC.downedBoss2 = false;
			NPC.downedBoss3 = false;
			WorldModify.shadowOrbSmashed = false;
			WorldModify.spawnMeteor = false;
			WorldModify.stopDrops = false;
			Main.invasionDelay = 0;
			Main.invasionType = 0;
			Main.invasionSize = 0;
			Main.invasionWarn = 0;
			Main.invasionX = 0.0;
			WorldModify.noLiquidCheck = false;
			Liquid.numLiquid = 0;
			LiquidBuffer.numLiquidBuffer = 0;

//			if (WorldModify.lastMaxTilesX > Main.maxTilesX || WorldModify.lastMaxTilesY > Main.maxTilesY)
//			{
//				using (var prog = new ProgressLogger(WorldModify.lastMaxTilesX - 1, "Freeing unused resources"))
//					for (int i = 0; i < WorldModify.lastMaxTilesX; i++)
//					{
//						prog.Value = i;
//						for (int j = 0; j < WorldModify.lastMaxTilesY; j++)
//						{
//							Main.tile.CreateTileAt(i, j);
//						}
//					}
//			}
			WorldModify.lastMaxTilesX = Main.maxTilesX;
			WorldModify.lastMaxTilesY = Main.maxTilesY;

            if (Main.tile == null || Main.tile.SizeX != Main.maxTilesX || Main.tile.SizeY != Main.maxTilesY)
			{
                Main.tile = null;
				GC.Collect ();
                Main.tile = new TileCollection(Main.maxTilesX, Main.maxTilesY);
			}
			else
			{
				using (var prog = new ProgressLogger(Main.maxTilesX - 1, "Clearing world tiles"))
					for (int k = 0; k < Main.maxTilesX; k++)
					{
						prog.Value = k;
						for (int l = 0; l < Main.maxTilesY; l++)
						{
							Main.tile.CreateTileAt(k, l);
// for testing
//							if ((l == 25 || l == 26) && k > 4000 && k < 5000)
//							{
//								Main.tile.At(k, l).SetLiquid (250);
//								Liquid.AddWater (k, l);
//							}
						}
					}
			}
			
			using (var prog = new ProgressLogger(12212 + Liquid.resLiquid - 1, "Resetting game objects"))
			{
				for (int num3 = 0; num3 < 201; num3++)
				{
					Main.item[num3] = null;
					Main.npcs[num3] = null;
				}
				prog.Value++;
				
				for (int num4 = 0; num4 < 1000; num4++)
				{
					Main.projectile[num4] = null;
					Main.chest[num4] = null;
					Main.sign[num4] = null;
				}
				prog.Value++;
				
//				for (int num8 = 0; num8 < Liquid.resLiquid; num8++)
//				{
//					Main.liquid[num8] = null;
//				}
//				prog.Value++;
				
//				for (int num9 = 0; num9 < 10000; num9++)
//				{
//					Main.liquidBuffer[num9] = null;
//				}
//				prog.Value += 10;
				
				GC.Collect ();
				prog.Value += 10;

				for (int num3 = 0; num3 < 201; num3++)
				{
					Main.item[num3] = new Item();
					Main.npcs[num3] = new NPC();
					prog.Value++;
				}
				for (int num4 = 0; num4 < 1001; num4++)
				{
					
					prog.Value++;
				}
//				for (int num5 = 0; num5 < 1000; num5++)
//				{
//					Main.projectile[num5] = new Projectile();
//					prog.Value++;
//				}
				Projectile.ResetProjectiles ();
				prog.Value += 1000;
				for (int num8 = 0; num8 < Liquid.resLiquid; num8++)
				{
					Main.liquid[num8] = new Liquid();
					prog.Value++;
				}
				for (int num9 = 0; num9 < 10000; num9++)
				{
					Main.liquidBuffer[num9] = new LiquidBuffer();
					prog.Value++;
				}
			}
			setWorldSize();
			
			LiquidUpdateBuffer.Initialize (Main.maxSectionsX, Main.maxSectionsY);
			
			worldCleared = true;
		}

        public static bool saveWorld(string savePath, bool resetTime = false)
		{
            bool success = true;

			if (savePath == null)
			{
				return false;
			}

			if (WorldModify.saveLock)
			{
				return false;
			}

            try
            {
				WorldModify.saveLock = true;
                while (WorldIO.hardLock)
                {
                    WorldModify.statusText = "Setting hard mode...";
                }
				lock (padlock)
				{
					bool value = Main.dayTime;
					tempTime = Main.time;
					tempMoonPhase = Main.moonPhase;
					tempBloodMoon = Main.bloodMoon;

					if (resetTime)
					{<commit_msg>commented out for now<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using Terraria_Server.Commands;
using Terraria_Server.Plugins;
using Terraria_Server.Misc;
using Terraria_Server.Collections;
using Terraria_Server.Definitions;
using Terraria_Server.Logging;
using Terraria_Server.Networking;

namespace Terraria_Server.WorldMod
{
	public class WorldIO
	{
		private static object padlock = new object();
		public static bool worldCleared = false;
		private static int tempMoonPhase = Main.moonPhase;
		private static bool tempDayTime = Main.dayTime;
		private static bool tempBloodMoon = Main.bloodMoon;
		private static double tempTime = Main.time;

		public static void setWorldSize()
		{
			Main.bottomWorld = (float)(Main.maxTilesY * 16);
			Main.rightWorld = (float)(Main.maxTilesX * 16);
			Main.maxSectionsX = Main.maxTilesX / 200;
			Main.maxSectionsY = Main.maxTilesY / 150;
		}

        public static void SaveWorldCallback(object threadContext)
        {
            saveWorld(Server.World.SavePath, false);
        }

        public static void SaveWorldThreaded()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(SaveWorldCallback), 1);
        }

		public static void clearWorld()
		{
			Statics.WorldLoaded = false;
			
			Main.trashItem = new Item();
			WorldModify.spawnEye = false;
			WorldModify.spawnNPC = 0;
			WorldModify.shadowOrbCount = 0;
			Main.helpText = 0;
			Main.dungeonX = 0;
			Main.dungeonY = 0;
			NPC.downedBoss1 = false;
			NPC.downedBoss2 = false;
			NPC.downedBoss3 = false;
			WorldModify.shadowOrbSmashed = false;
			WorldModify.spawnMeteor = false;
			WorldModify.stopDrops = false;
			Main.invasionDelay = 0;
			Main.invasionType = 0;
			Main.invasionSize = 0;
			Main.invasionWarn = 0;
			Main.invasionX = 0.0;
			WorldModify.noLiquidCheck = false;
			Liquid.numLiquid = 0;
			LiquidBuffer.numLiquidBuffer = 0;

//			if (WorldModify.lastMaxTilesX > Main.maxTilesX || WorldModify.lastMaxTilesY > Main.maxTilesY)
//			{
//				using (var prog = new ProgressLogger(WorldModify.lastMaxTilesX - 1, "Freeing unused resources"))
//					for (int i = 0; i < WorldModify.lastMaxTilesX; i++)
//					{
//						prog.Value = i;
//						for (int j = 0; j < WorldModify.lastMaxTilesY; j++)
//						{
//							Main.tile.CreateTileAt(i, j);
//						}
//					}
//			}
			WorldModify.lastMaxTilesX = Main.maxTilesX;
			WorldModify.lastMaxTilesY = Main.maxTilesY;

            if (Main.tile == null || Main.tile.SizeX != Main.maxTilesX || Main.tile.SizeY != Main.maxTilesY)
			{
                Main.tile = null;
				GC.Collect ();
                Main.tile = new TileCollection(Main.maxTilesX, Main.maxTilesY);
			}
			else
			{
				using (var prog = new ProgressLogger(Main.maxTilesX - 1, "Clearing world tiles"))
					for (int k = 0; k < Main.maxTilesX; k++)
					{
						prog.Value = k;
						for (int l = 0; l < Main.maxTilesY; l++)
						{
							Main.tile.CreateTileAt(k, l);
// for testing
//							if ((l == 25 || l == 26) && k > 4000 && k < 5000)
//							{
//								Main.tile.At(k, l).SetLiquid (250);
//								Liquid.AddWater (k, l);
//							}
						}
					}
			}
			
			using (var prog = new ProgressLogger(12212 + Liquid.resLiquid - 1, "Resetting game objects"))
			{
				for (int num3 = 0; num3 < 201; num3++)
				{
					Main.item[num3] = null;
					Main.npcs[num3] = null;
				}
				prog.Value++;
				
				for (int num4 = 0; num4 < 1000; num4++)
				{
					Main.projectile[num4] = null;
					Main.chest[num4] = null;
					Main.sign[num4] = null;
				}
				prog.Value++;
				
//				for (int num8 = 0; num8 < Liquid.resLiquid; num8++)
//				{
//					Main.liquid[num8] = null;
//				}
//				prog.Value++;
				
//				for (int num9 = 0; num9 < 10000; num9++)
//				{
//					Main.liquidBuffer[num9] = null;
//				}
//				prog.Value += 10;
				
				GC.Collect ();
				prog.Value += 10;

				for (int num3 = 0; num3 < 201; num3++)
				{
					Main.item[num3] = new Item();
					Main.npcs[num3] = new NPC();
					prog.Value++;
				}
				for (int num4 = 0; num4 < 1001; num4++)
				{
					
					prog.Value++;
				}
//				for (int num5 = 0; num5 < 1000; num5++)
//				{
//					Main.projectile[num5] = new Projectile();
//					prog.Value++;
//				}
				Projectile.ResetProjectiles ();
				prog.Value += 1000;
				for (int num8 = 0; num8 < Liquid.resLiquid; num8++)
				{
					Main.liquid[num8] = new Liquid();
					prog.Value++;
				}
				for (int num9 = 0; num9 < 10000; num9++)
				{
					Main.liquidBuffer[num9] = new LiquidBuffer();
					prog.Value++;
				}
			}
			setWorldSize();
			
			LiquidUpdateBuffer.Initialize (Main.maxSectionsX, Main.maxSectionsY);
			
			worldCleared = true;
		}

        public static bool saveWorld(string savePath, bool resetTime = false)
		{
            bool success = true;

			if (savePath == null)
			{
				return false;
			}

			if (WorldModify.saveLock)
			{
				return false;
			}

            try
            {
				WorldModify.saveLock = true;
                //while (WorldIO.hardLock)
                //{
                //    WorldModify.statusText = "Setting hard mode...";
                //    Thread.Sleep(100);
                //}
				lock (padlock)
				{
					bool value = Main.dayTime;
					tempTime = Main.time;
					tempMoonPhase = Main.moonPhase;
					tempBloodMoon = Main.bloodMoon;

					if (resetTime)
					{
{'fraction_non_alphanumeric': 0.13325836378971043, 'fraction_numerical': 0.02614562833848749, 'mean_word_length': 2.2163954189270645, 'pattern_counts': {'":': 0, '<': 31, '<?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': '1668387', 'n_tokens_mistral': 4972, 'n_tokens_neox': 3799, 'n_words': 800}