Browse Source

play snapshot works!

vlod 3 years ago
parent
commit
9f3debbf17

+ 61 - 14
Pika/core/pikaEditor/containersWindow/containersWindow.cpp

@@ -37,7 +37,6 @@ void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pi
 			{
 				ImGui::Text("Available containers");
 				ImGui::Separator();
-				static int itemCurrent;//todo move
 
 				//left
 				ImGui::PushID(pikaImgui::EditorImguiIds::containersWindow+1);
@@ -58,7 +57,7 @@ void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pi
 					contentSize.y -= ImGui::GetFrameHeightWithSpacing();
 					contentSize.x /= 2;
 
-					ImGui::ListWithFilter("##list box container info", &itemCurrent, filter, sizeof(filter),
+					ImGui::ListWithFilter("##list box container info", &itemCurrentAvailableCOntainers, filter, sizeof(filter),
 						containerNames, contentSize);
 
 
@@ -72,9 +71,9 @@ void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pi
 				ImGui::PushID(pikaImgui::EditorImguiIds::containersWindow + 2);
 				ImGui::BeginGroup();
 				{
-					if (itemCurrent < loadedDll.containerInfo.size())
+					if (itemCurrentAvailableCOntainers < loadedDll.containerInfo.size())
 					{
-						auto &c = loadedDll.containerInfo[itemCurrent];
+						auto &c = loadedDll.containerInfo[itemCurrentAvailableCOntainers];
 
 						ImGui::Text("Container info: %s", c.containerName.c_str());
 						ImGui::Separator();
@@ -202,8 +201,6 @@ void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pi
 
 				ImGui::Separator();
 			
-				static int itemCurrent;//todo move
-
 				//left
 				std::vector<pika::containerId_t> containerIds;
 				std::vector<std::string> containerNames;
@@ -212,7 +209,7 @@ void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pi
 				ImGui::BeginGroup();
 				{
 
-					static char filter[256] = {};
+					static char filter[256] = {}; //todo move
 
 
 					
@@ -230,7 +227,7 @@ void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pi
 					contentSize.y -= ImGui::GetFrameHeightWithSpacing();
 					contentSize.x /= 2;
 
-					ImGui::ListWithFilter("##list box container info", &itemCurrent, filter, sizeof(filter),
+					ImGui::ListWithFilter("##list box container info", &itemCurrentCreatedContainers, filter, sizeof(filter),
 						containerNames, contentSize);
 
 
@@ -244,12 +241,12 @@ void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pi
 				ImGui::PushID(pikaImgui::EditorImguiIds::containersWindow + 4);
 				ImGui::BeginGroup();
 				{
-					if (itemCurrent < containerIds.size())
+					if (itemCurrentCreatedContainers < containerIds.size())
 					{
-						auto &c = containerManager.runningContainers[containerIds[itemCurrent]];
+						auto &c = containerManager.runningContainers[containerIds[itemCurrentCreatedContainers]];
 
 						
-						ImGui::Text("Running container: %s #%u", c.baseContainerName, containerIds[itemCurrent]);
+						ImGui::Text("Running container: %s #%u", c.baseContainerName, containerIds[itemCurrentCreatedContainers]);
 						ImGui::Separator();
 
 					#pragma region buttons
@@ -299,7 +296,7 @@ void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pi
 						if (ImGui::Button(ICON_FK_STOP))
 						{
 							//todo mabe defer here when api is made
-							containerManager.destroyContainer(containerIds[itemCurrent], loadedDll, logManager);
+							containerManager.destroyContainer(containerIds[itemCurrentCreatedContainers], loadedDll, logManager);
 							stopped = true;
 						}
 						if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
@@ -311,7 +308,7 @@ void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pi
 
 						if (ImGui::Button(ICON_FK_EJECT))
 						{
-							containerManager.forceTerminateContainer(containerIds[itemCurrent], loadedDll, logManager);
+							containerManager.forceTerminateContainer(containerIds[itemCurrentCreatedContainers], loadedDll, logManager);
 							stopped = true;
 						}
 						if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
@@ -337,7 +334,7 @@ void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pi
 							{
 								if (pika::isFileNameValid(snapshotName, sizeof(snapshotName)))
 								{
-									if (!containerManager.makeSnapshot(containerIds[itemCurrent], logManager, snapshotName))
+									if (!containerManager.makeSnapshot(containerIds[itemCurrentCreatedContainers], logManager, snapshotName))
 									{
 										logManager.log("Coultn't make snapshot", pika::logError);
 									}else
@@ -363,6 +360,56 @@ void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pi
 							ImGui::InputText("snapshot name", snapshotName, sizeof(snapshotName));
 
 
+							ImGui::NewLine();
+							ImGui::Separator();
+
+						#pragma region snapshots
+							{
+								static char filter[256] = {}; //todo move
+								static int currentSelectedSnapshot = 0;
+
+								auto snapshots = pika::getAvailableSnapshots(
+									containerManager.runningContainers[containerIds[itemCurrentCreatedContainers]]);
+
+								auto contentSize = ImGui::GetItemRectSize();
+								contentSize.y -= ImGui::GetFrameHeightWithSpacing();
+								contentSize.x /= 2;
+
+								ImGui::ListWithFilter("##list box snapshots", &currentSelectedSnapshot,
+									filter, sizeof(filter),
+									snapshots, contentSize);
+
+								ImGui::SameLine();
+
+								if (snapshots.size() == 0 || currentSelectedSnapshot >= snapshots.size())
+								{
+									ImGui::BeginDisabled(true);
+								}
+								else
+								{
+									ImGui::BeginDisabled(false);
+								}
+
+								if(ImGui::Button(ICON_FK_PLAY "##play snapshot"))
+								{
+									if (!containerManager.setSnapshotToContainer(
+										containerIds[itemCurrentCreatedContainers],
+										snapshots[currentSelectedSnapshot].c_str(), logManager
+										))
+									{
+										logManager.log("Failed to assign snapshot", pika::logError);
+									}
+								}
+
+								ImGui::EndDisabled();
+
+								if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
+								{
+									ImGui::SetTooltip("Play this snapshot to this container");
+								}
+
+							}
+						#pragma endregion
 
 						}
 					}

+ 4 - 0
Pika/core/pikaEditor/containersWindow/containersWindow.h

@@ -20,6 +20,10 @@ namespace pika
 		static constexpr char *NAME = "Containers manager";
 		static constexpr char *ICON_NAME = ICON_FK_MICROCHIP " Containers manager";
 
+
+		int itemCurrentAvailableCOntainers = 0;
+		int itemCurrentCreatedContainers = 0;
+
 	};
 
 

+ 8 - 0
Pika/core/pikaEditor/editor/editor.cpp

@@ -83,6 +83,8 @@ void pika::Editor::update(const pika::Input &input,
 	
 		ImGui::PushID(pika::pikaImgui::EditorImguiIds::mainEditorWindow);
 
+		ImGui::SetNextWindowBgAlpha(0);
+
 		if (ImGui::Begin(
 			"Main window",
 			/*p_open=*/nullptr,
@@ -144,6 +146,12 @@ void pika::Editor::update(const pika::Input &input,
 					ImGui::MenuItem(pika::EditShortcutsWindow::ICON_NAME,
 						shortcutManager.getShortcut(EDIT_SHORTCUTS), &windowFlags.editShortcutsWindow);
 
+					pika::pikaImgui::displayMemorySizeToggle();
+
+					//todo
+					//ImGui::MenuItem(pika::EditShortcutsWindow::ICON_NAME,
+					//	shortcutManager.getShortcut(EDIT_SHORTCUTS), &windowFlags.editShortcutsWindow);
+					//transparentWindow
 
 					ImGui::EndMenu();
 				}

+ 2 - 0
Pika/core/pikaEditor/editor/editor.h

@@ -44,6 +44,8 @@ namespace pika
 		bool lastHideWindowState = optionsFlags.hideMainWindow;
 
 		bool shouldReloadDll = 0;
+
+		bool transparentWindow = 1;
 	};
 
 

+ 106 - 5
Pika/core/pikaRuntime/containerManager/containerManager.cpp

@@ -8,6 +8,7 @@
 #include <unordered_map>
 #include <fileManipulation/fileManipulation.h>
 #include <stringManipulation/stringManipulation.h>
+#include <filesystem>
 
 pika::containerId_t pika::ContainerManager::createContainer(std::string containerName, 
 	pika::LoadedDll &loadedDll, pika::LogManager &logManager)
@@ -28,15 +29,43 @@ pika::containerId_t pika::ContainerManager::createContainer(std::string containe
 	return 0;
 }
 
-pika::containerId_t 
-pika::ContainerManager::createContainerFromSnapshot(std::string containerName, pika::LoadedDll &loadedDll, pika::LogManager &logManager, const char *fileName)
+bool pika::ContainerManager::setSnapshotToContainer(pika::containerId_t containerId, const char *snapshotName, pika::LogManager &logManager)
 {
+	auto c = runningContainers.find(containerId);
+	if (c == runningContainers.end())
+	{
+		logManager.log((std::string("Couldn't find container for setting snapshot: #") 
+			+ std::to_string(containerId)).c_str(),
+			pika::logError);
+		return false;
+	}
+
+	if (!checkIfSnapshotIsCompatible(c->second, snapshotName))
+	{
+		logManager.log((std::string("Snapshot incompatible: ") + snapshotName).c_str(),
+			pika::logError);
+		return false;
+	}
 	
+	std::string file = PIKA_ENGINE_RESOURCES_PATH;
+	file += snapshotName;
+	file += ".snapshot";
 
+	auto s = pika::getFileSize(file.c_str());
 
-	
-	
-	return containerId_t();
+	if (s != (c->second.totalSize + sizeof(pika::RuntimeContainer)) ) 
+	{
+		logManager.log((std::string("Snapshot corrupted probably, file size incorrect: ") 
+			+ snapshotName).c_str(),
+			pika::logError);
+		return false;
+	}
+
+	pika::readEntireFile(file.c_str(), c->second.getBaseAdress(),
+		c->second.totalSize, sizeof(pika::RuntimeContainer));
+
+	logManager.log("Loaded snapshot");
+	return true;
 }
 
 //todo use regions further appart in production
@@ -538,3 +567,75 @@ void pika::ContainerManager::deallocateOSMemory(void *baseAdress)
 
 #endif
 
+std::vector<std::string> pika::getAvailableSnapshots(pika::RuntimeContainer &info)
+{
+	std::vector<std::string> files;
+
+	auto curDir = std::filesystem::directory_iterator(PIKA_ENGINE_RESOURCES_PATH);
+
+	for (const auto &iter : curDir)
+	{
+		if (std::filesystem::is_regular_file(iter)
+			&& iter.path().extension() == ".snapshot"
+			)
+		{
+			if (pika::checkIfSnapshotIsCompatible(info, iter.path().stem().string().c_str()))
+			{
+				files.push_back(iter.path().stem().string());
+			}
+
+		}
+	}
+
+	return files;
+}
+
+bool pika::checkIfSnapshotIsCompatible(pika::RuntimeContainer &info, const char *snapshotName)
+{
+
+	std::string file = PIKA_ENGINE_RESOURCES_PATH;
+	file += snapshotName;
+	file += ".snapshot";
+	
+	pika::RuntimeContainer loadedInfo = {};
+
+	auto s = pika::readEntireFile(file.c_str(), &loadedInfo, sizeof(loadedInfo));
+
+	if (s != sizeof(loadedInfo))
+	{
+		return 0;
+	}
+
+	//todo mabe a method here?
+	if (loadedInfo.allocator.originalBaseMemory != info.allocator.originalBaseMemory)
+	{
+		return false;
+	}
+
+	if (loadedInfo.arena.containerStructMemory.block != info.arena.containerStructMemory.block)
+	{
+		return false;
+	}
+
+	if (loadedInfo.arena.containerStructMemory.size != info.arena.containerStructMemory.size)
+	{
+		return false;
+	}
+
+	if (std::strcmp(loadedInfo.baseContainerName, info.baseContainerName) != 0)
+	{
+		return false;
+	}
+
+	if (loadedInfo.requestedContainerInfo.bonusAllocators != info.requestedContainerInfo.bonusAllocators)
+	{
+		return false;
+	}
+
+	if (loadedInfo.totalSize != info.totalSize)
+	{
+		return false;
+	}
+		
+	return true;
+}

+ 4 - 4
Pika/core/pikaRuntime/containerManager/containerManager.h

@@ -28,10 +28,8 @@ namespace pika
 			std::string containerName,
 			pika::LoadedDll &loadedDll, pika::LogManager &logManager);
 
-		containerId_t createContainerFromSnapshot(
-			std::string containerName,
-			pika::LoadedDll &loadedDll, pika::LogManager &logManager,
-			const char *fileName);
+		bool setSnapshotToContainer(pika::containerId_t containerId, const char* snapshotName,
+			pika::LogManager &logManager);
 
 		void* allocateContainerMemory(pika::RuntimeContainer &container, pika::ContainerInformation containerInformation, void *memPos = 0);
 
@@ -75,6 +73,8 @@ namespace pika
 	};
 
 
+	std::vector<std::string> getAvailableSnapshots(pika::RuntimeContainer &info);
 
+	bool checkIfSnapshotIsCompatible(pika::RuntimeContainer &info, const char *snapshotName);
 
 }

+ 0 - 1
Pika/core/pikaRuntime/runtimeContainer/runtimeContainer.h

@@ -26,7 +26,6 @@ struct RuntimeContainer
 
 	//this is the allocator of the arena.
 	pika::memory::FreeListAllocator allocator = {};
-	size_t allocatorSize = 0;
 	size_t totalSize = 0;
 
 	//bonus allocators

+ 6 - 0
Pika/core/pikaSTD/compilerIntrinsics.h

@@ -25,6 +25,12 @@
 	#define PIKA_ASSUME(x)
 #endif
 
+//assume unreachable //todo for other compilers
+#ifdef PIKA_MSVC
+#define PIKA_UNREACHABLE() __assume((0))
+#else
+#define PIKA_UNREACHABLE()
+#endif
 
 
 //optimize off

+ 6 - 2
Pika/core/pikaSTD/fileManipulation/fileManipulation.cpp

@@ -19,16 +19,20 @@ namespace pika
 		return size;
 	}
 
-	bool readEntireFile(const char *name, void *buffer, size_t s)
+	size_t readEntireFile(const char *name, void *buffer, size_t s, size_t from)
 	{
 		std::ifstream f(name, std::ios::binary);
 		if (!f.is_open()) { return 0; }
 
+		f.seekg(from);
+
 		f.read((char*)buffer, s);
 
+		size_t pos = f.tellg();
+
 		f.close();
 
-		return true;
+		return pos;
 	}
 
 	bool writeEntireFile(const char *name, void *buffer, size_t s)

+ 1 - 1
Pika/core/pikaSTD/fileManipulation/fileManipulation.h

@@ -5,7 +5,7 @@ namespace pika
 {
 
 	size_t getFileSize(const char *name);
-	bool readEntireFile(const char* name, void *buffer, size_t s);
+	size_t readEntireFile(const char* name, void *buffer, size_t s, size_t from = 0);
 
 	bool writeEntireFile(const char *name, void *buffer, size_t s);
 

+ 3 - 3
Pika/core/pikaSTD/logs/assert.cpp

@@ -4,18 +4,18 @@
 #include <cstdio>
 #include <assert.h>
 #include <logs/log.h>
-
+#include <compilerIntrinsics.h>
 
 namespace pika
 {
 
 	namespace assert
 	{
-
+		//todo noreturn
 		inline void terminate(...)
 		{
 			std::abort();
-			__assume(0); //optimize code after the exit
+			PIKA_UNREACHABLE(); //optimize code after the exit
 		}
 
 

+ 3 - 0
Pika/core/pikaSTD/pikaSizes.h

@@ -11,6 +11,9 @@ namespace pika
 	inline size_t MB(size_t x) { return KB(x) * 1024ull; }
 	inline size_t GB(size_t x) { return MB(x) * 1024ull; }
 
+	inline float BYTES_TO_KB(size_t x) { return x / 1024.f; }
+	inline float BYTES_TO_MB(size_t x) { return BYTES_TO_KB(x) / 1024.f; }
+	inline float BYTES_TO_GB(size_t x) { return BYTES_TO_MB(x) / 1024.f; }
 
 	inline void align64(size_t &val)
 	{

+ 1 - 1
Pika/core/pikaSTD/staticVector.h

@@ -103,7 +103,7 @@ namespace pika
 
 			for (int i = 0; i < size_; i++)
 			{
-				if ((*this)[i] != other[i])
+				if ( (*this)[i] != other[i])
 				{
 					return false;
 				}

+ 23 - 9
Pika/core/sharedRuntime/pikaImgui/pikaImgui.cpp

@@ -4,6 +4,7 @@
 #include "IconsForkAwesome.h"
 #include <pikaAllocator/freeListAllocator.h>
 #include <logs/assert.h>
+#include <compilerIntrinsics.h>
 
 #if !(PIKA_SHOULD_REMOVE_IMGUI)
 
@@ -169,18 +170,31 @@ static int sizesType = 0;
 void pika::pikaImgui::displayMemorySizeValue(size_t value)
 {
 
-	const char *items[] = 
-	{"Bytes", 
-		"KB",
-		"MB",
-		"GB",
-		"TB"};
-
-	//ImGui::Combo("##sizes type pika", &sizesType, "Bytes\0KB\0MB\0GB\0TB\0");
+	switch (sizesType)
+	{
+	case 0:
+		ImGui::Text("%" IM_PRIu64 " (bytes)", value);
+		break;
+	case 1:
+		ImGui::Text("%f (KB)", pika::BYTES_TO_KB(value));
+		break;
+	case 2:
+		ImGui::Text("%f (MB)", pika::BYTES_TO_MB(value));
+		break;
+	case 3:
+		ImGui::Text("%f (GB)", pika::BYTES_TO_GB(value));
+		break;
+	default:
+		PIKA_UNREACHABLE();
+	}
 
-	ImGui::Text("%" IM_PRIu64 " (bytes)", value);
+	//ImGui::Text("%" IM_PRIu64 " (bytes)", value); 
 
+}
 
+void pika::pikaImgui::displayMemorySizeToggle()
+{
+	ImGui::Combo("Sizes type##pika", &sizesType, "Bytes\0KB\0MB\0GB\0");
 }
 
 

+ 3 - 0
Pika/core/sharedRuntime/pikaImgui/pikaImgui.h

@@ -71,5 +71,8 @@ namespace pika
 		
 		void displayMemorySizeValue(size_t value);
 
+		//todo move to internal
+		void displayMemorySizeToggle();
+
 	};
 };

BIN
Pika/engineResources/current.snapshot


BIN
Pika/engineResources/test.snapshot


BIN
Pika/engineResources/test1.snapshot


+ 6 - 4
Pika/resources/logs.txt

@@ -1,4 +1,6 @@
-#2022-10-07 17:51:29: Created container: Gameplay
-#2022-10-07 17:51:56[error]: File name invalid
-#2022-10-07 17:51:58: Successfully created snapshot.
-#2022-10-07 17:52:02: Destroyed continer: Gameplay #1
+#2022-10-10 11:37:38: Created container: Gameplay
+#2022-10-10 11:37:48: Successfully created snapshot.
+#2022-10-10 11:37:59: Loaded snapshot
+#2022-10-10 11:37:59: Loaded snapshot
+#2022-10-10 11:38:08: Loaded snapshot
+#2022-10-10 11:38:34: Destroyed continer: Gameplay #1