From 0569c29b19e4d23f055845a167c706f11590fa2a Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期二, 19 八月 2025 15:55:00 +0800
Subject: [PATCH] 1.CControlJob和CProcessJob的序列化和反序列化;

---
 SourceCode/Bond/Servo/CMaster.cpp |   85 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/SourceCode/Bond/Servo/CMaster.cpp b/SourceCode/Bond/Servo/CMaster.cpp
index 81a0115..77b3a37 100644
--- a/SourceCode/Bond/Servo/CMaster.cpp
+++ b/SourceCode/Bond/Servo/CMaster.cpp
@@ -4,6 +4,8 @@
 #include <future>
 #include <vector>
 #include "RecipeManager.h"
+#include <fstream>
+#include "SerializeUtil.h"
 
 
 namespace SERVO {
@@ -1864,6 +1866,8 @@
 		}
 
 		m_processJobs = temp;
+		this->saveState();
+
 		return m_processJobs.size();
 	}
 
@@ -1919,6 +1923,9 @@
 			}
 		}
 		m_pControlJob->setPJs(temps);
+		this->saveState();
+
+
 		return 0;
 	}
 
@@ -1967,4 +1974,82 @@
 		return true;
 	}
 
+	bool CMaster::saveState() const
+	{
+		std::ofstream ofs(m_strStatePath, std::ios::binary);
+		if (!ofs) return false;
+
+		// 文件头
+		uint32_t magic = 0x4D415354; // 'MAST'
+		uint16_t version = 1;
+		ofs.write(reinterpret_cast<const char*>(&magic), sizeof(magic));
+		ofs.write(reinterpret_cast<const char*>(&version), sizeof(version));
+
+		// 保存 ControlJob
+		bool hasCJ = (m_pControlJob != nullptr);
+		ofs.write(reinterpret_cast<const char*>(&hasCJ), sizeof(hasCJ));
+		if (hasCJ) {
+			m_pControlJob->serialize(ofs);
+		}
+
+		// 保存 ProcessJob 列表
+		uint32_t count = static_cast<uint32_t>(m_processJobs.size());
+		ofs.write(reinterpret_cast<const char*>(&count), sizeof(count));
+		for (const auto& job : m_processJobs) {
+			job->serialize(ofs);
+		}
+
+		// 以后可以在这里追加新字段
+		return true;
+	}
+
+	bool CMaster::loadState(const std::string& path)
+	{
+		// 保存文件路径
+		m_strStatePath = path;
+
+
+		std::ifstream ifs(path, std::ios::binary);
+		if (!ifs) return false;
+
+		// 文件头
+		uint32_t magic = 0;
+		uint16_t version = 0;
+		ifs.read(reinterpret_cast<char*>(&magic), sizeof(magic));
+		ifs.read(reinterpret_cast<char*>(&version), sizeof(version));
+
+		if (magic != 0x4D415354) {
+			// 文件不合法
+			return false;
+		}
+
+		if (m_pControlJob != nullptr) {
+			delete m_pControlJob;
+			m_pControlJob = nullptr;
+		}
+
+		// 读取 ControlJob
+		bool hasCJ = false;
+		ifs.read(reinterpret_cast<char*>(&hasCJ), sizeof(hasCJ));
+		if (hasCJ) {
+			m_pControlJob = new CControlJob();
+			if (!CControlJob::deserialize(ifs, *m_pControlJob)) return false;
+		}
+
+
+		// 读取 ProcessJob 列表
+		uint32_t count = 0;
+		ifs.read(reinterpret_cast<char*>(&count), sizeof(count));
+		m_processJobs.clear();
+		for (uint32_t i = 0; i < count; i++) {
+			CProcessJob* pProcessJob = new CProcessJob();
+			if (!CProcessJob::deserialize(ifs, *pProcessJob)) return false;
+			m_processJobs.push_back(pProcessJob);
+		}
+
+		// 如果版本升级,可在这里判断 version 来加载新字段
+
+
+		return true;
+	}
 }

--
Gitblit v1.9.3