Files
Simulation/Assets/WorkSpace/LWJ/SaveLoadmanager.cs
2025-06-25 14:41:15 +09:00

368 lines
14 KiB
C#

using SFB;
using UnityEngine;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.IO;
using System.Linq;
using System.Text;
using UVC.Networks;
namespace Octopus.Simulator.Networks
{
public class SaveLoadmanager : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.K))
{
Onclick_Save_Local();
}
if (Input.GetKey(KeyCode.L))
{
Onclick_Load_Local();
}
}
public void Onclick_Save_Local()
{
var path = StandaloneFileBrowser.SaveFilePanel("Save Layout", "", "", "");
var simulationModels = FindObjectsByType<SimulationModel>(FindObjectsSortMode.None);
List<SimulationModelInfo> saveInfos = new List<SimulationModelInfo>();
foreach (var item in simulationModels)
{
var tmpSimulationModelInfo = new SimulationModelInfo();
tmpSimulationModelInfo.prefabName = item.gameObject.name.Replace("(Clone)", "");
tmpSimulationModelInfo.modelName = item.modelName;
//tmpSimulationModelInfo.modelType = item.modelType.ToString();
tmpSimulationModelInfo.modelID = item.modelID;
tmpSimulationModelInfo.nodeID = item.nodeID;
tmpSimulationModelInfo.position = item.transform.position.ToString();
tmpSimulationModelInfo.rotation = item.transform.rotation.ToString();
tmpSimulationModelInfo.scale = item.transform.localScale.ToString();
saveInfos.Add(tmpSimulationModelInfo);
}
string jsonObject = JsonConvert.SerializeObject(saveInfos, Formatting.Indented);
FileStream fileStream = new FileStream(string.Format("{0}.json", path), FileMode.Create);
byte[] buffer = Encoding.UTF8.GetBytes(jsonObject);
fileStream.Write(buffer, 0, buffer.Length);
fileStream.Close();
}
public void Onclick_Load_Local()
{
var path = StandaloneFileBrowser.OpenFilePanel("Open Layout", "", "", false);
FileStream fileStream = new FileStream(string.Format("{0}", path), FileMode.Open);
byte[] data = new byte[fileStream.Length];
fileStream.Read(data, 0, data.Length);
fileStream.Close();
string jsonData = Encoding.UTF8.GetString(data);
List<SimulationModelInfo> loadInfos = new List<SimulationModelInfo>();
loadInfos = JsonConvert.DeserializeObject<List<SimulationModelInfo>>(jsonData);
string resourcePath = "ModelsUpdated";
Dictionary<string, SimulationModel> loadModels = new Dictionary<string, SimulationModel>();
foreach (var item in loadInfos)
{
var simulationModel = Resources.Load<GameObject>(string.Format("{0}/{1}", resourcePath, item.prefabName));
var spawnItem = Instantiate(simulationModel).GetComponent<SimulationModel>();
spawnItem.modelName = item.modelName;
//spawnItem.modelType = item.modelType;
spawnItem.modelID = item.modelID;
spawnItem.nodeID = item.nodeID;
spawnItem.gameObject.transform.position = ParseVector3(item.position);
spawnItem.gameObject.transform.localScale = ParseVector3(item.scale);
spawnItem.gameObject.transform.rotation = ParseQuaternion(item.rotation);
if (!string.IsNullOrEmpty(item.nodeID))
loadModels.Add(item.nodeID, spawnItem);
}
LogicMappingDataBase.LoadDataBase(loadModels);
}
public void Onclick_Save()
{
//var path = StandaloneFileBrowser.SaveFilePanel("Save Layout", "", "", "");
var simulationModels = FindObjectsByType<SimulationModel>(FindObjectsSortMode.None);
List<SimulationModelInfo> saveInfos = new List<SimulationModelInfo>();
foreach (var item in simulationModels)
{
var tmpSimulationModelInfo = new SimulationModelInfo();
tmpSimulationModelInfo.prefabName = item.gameObject.name.Replace("(Clone)", "");
tmpSimulationModelInfo.modelName = item.modelName;
//tmpSimulationModelInfo.modelType = item.modelType;
tmpSimulationModelInfo.modelID = item.modelID;
tmpSimulationModelInfo.nodeID = item.nodeID;
tmpSimulationModelInfo.position = item.transform.position.ToString();
tmpSimulationModelInfo.rotation = item.transform.rotation.ToString();
tmpSimulationModelInfo.scale = item.transform.localScale.ToString();
saveInfos.Add(tmpSimulationModelInfo);
}
string jsonObject = JsonConvert.SerializeObject(saveInfos, Formatting.Indented);
var modelClass = new WebModelClass();
modelClass.logicId = int.Parse(WebParameters.config.logicId);
modelClass.name = "new model";
modelClass.description = "new model";
modelClass.data.info = jsonObject;
if (string.IsNullOrEmpty(WebParameters.config.modelId))
{
var requestURL = $"{WebManager.Instance.apiConfig.model}";
WebManager.Instance.Reqeust<WebModelReturnData>(requestURL, RequestType.POST, ModelCallback, modelClass);
//WebManager.Instance.Request_Post(modelClass, requestURL, (flag, value) =>
//{
// if (flag)
// {
// var webreturnClass = JsonConvert.DeserializeObject<WebModelReturnClass>(value);
// WebParameters.config.modelId = webreturnClass.data.insertedId.ToString();
// }
//});
}
else
{
var requestURL = $"{WebManager.Instance.apiConfig.model}/{WebParameters.config.modelId}";
WebManager.Instance.Reqeust<WebModelReturnData>(requestURL, RequestType.PUT, ModelCallback, modelClass);
//WebManager.Instance.Request_Put(modelClass, requestURL, (flag, value) =>
//{
// if (flag)
// {
// var webreturnClass = JsonConvert.DeserializeObject<WebModelReturnClass>(value);
// WebParameters.config.modelId = webreturnClass.data.insertedId.ToString();
// }
//});
}
/*
FileStream fileStream = new FileStream(string.Format("{0}.json", path), FileMode.Create);
byte[] buffer = Encoding.UTF8.GetBytes(jsonObject);
fileStream.Write(buffer, 0, buffer.Length);
fileStream.Close();
*/
}
public void ModelCallback(WebModelReturnData response)
{
//WebParameters.config.modelId = response.insertedId.ToString();
WebParameters.config.modelId = response.updatedIds[0].ToString();
}
public void Onclick_Load(string jsonData)
{
List<SimulationModelInfo> loadInfos = new List<SimulationModelInfo>();
loadInfos = JsonConvert.DeserializeObject<List<SimulationModelInfo>>(jsonData);
string resourcePath = "Library/LibraryPrefab";
Dictionary<string, SimulationModel> loadModels = new Dictionary<string, SimulationModel>();
foreach (var item in loadInfos)
{
var simulationModel = Resources.Load<GameObject>(string.Format("{0}/{1}", resourcePath, item.prefabName));
var spawn = Instantiate(simulationModel);
var spawnItem = spawn.GetComponent<SimulationModel>();
spawnItem.modelName = item.modelName;
//spawnItem.modelType = item.modelType;
spawnItem.modelID = item.modelID;
spawnItem.nodeID = item.nodeID;
spawnItem.gameObject.transform.position = ParseVector3(item.position);
spawnItem.gameObject.transform.localScale = ParseVector3(item.scale);
spawnItem.gameObject.transform.rotation = ParseQuaternion(item.rotation);
DataManager.I.AddModel(spawnItem);
if (!string.IsNullOrEmpty(item.nodeID) && !loadModels.ContainsKey(item.nodeID))
loadModels.Add(item.nodeID, spawnItem);
}
LogicMappingDataBase.LoadDataBase(loadModels);
}
//public void Onclick_Save()
//{
// var path = StandaloneFileBrowser.SaveFilePanel("Save Layout", "", "", "");
// var simulationModels = FindObjectsByType<SimulationModel>(FindObjectsSortMode.None);
// List<SimulationModelInfo> saveInfos = new List<SimulationModelInfo>();
// foreach (var item in simulationModels)
// {
// if (item is SimulationModelProduct)
// continue;
// var tmpSimulationModelInfo = new SimulationModelInfo();
// tmpSimulationModelInfo.prefabName = item.gameObject.name.Replace("(Clone)", "");
// tmpSimulationModelInfo.modelName = item.modelName;
// tmpSimulationModelInfo.modelType = item.modelType;
// tmpSimulationModelInfo.modelID = item.modelID;
// tmpSimulationModelInfo.nodeID = item.nodeID;
// tmpSimulationModelInfo.position = item.transform.position.ToString();
// tmpSimulationModelInfo.rotation = item.transform.rotation.ToString();
// tmpSimulationModelInfo.scale = item.transform.localScale.ToString();
// saveInfos.Add(tmpSimulationModelInfo);
// }
// string jsonObject = JsonConvert.SerializeObject(saveInfos, Formatting.Indented);
// var modelClass = new WebModelClass();
// modelClass.logicId = int.Parse(WebParameters.config.logicId);
// modelClass.name = "new model";
// modelClass.description = "new model";
// modelClass.data.info = jsonObject;
// string requestAPI = $"{WebManager.webManager.apiConfig.model}";
// Debug.Log(requestAPI);
// if (string.IsNullOrEmpty(WebParameters.config.modelId))
// {
// WebManager.webManager.Request_Post(modelClass, requestAPI, (flag, value) =>
// {
// if (flag)
// {
// var webreturnClass = JsonConvert.DeserializeObject<WebModelReturnClass>(value);
// WebParameters.config.modelId = webreturnClass.data.insertedId.ToString();
// }
// });
// }
// else
// {
// WebManager.webManager.Request_Put(modelClass, $"{requestAPI}/{WebParameters.config.modelId}", (flag, value) =>
// {
// if (flag)
// {
// var webreturnClass = JsonConvert.DeserializeObject<WebModelReturnClass>(value);
// WebParameters.config.modelId = webreturnClass.data.insertedId.ToString();
// }
// });
// }
// /*
// FileStream fileStream = new FileStream(string.Format("{0}.json", path), FileMode.Create);
// byte[] buffer = Encoding.UTF8.GetBytes(jsonObject);
// fileStream.Write(buffer, 0, buffer.Length);
// fileStream.Close();
//}
//public void Onclick_Load(string jsonData)
//{
// /*
// var path = StandaloneFileBrowser.OpenFilePanel("Open Layout", "", "", false);
// FileStream fileStream = new FileStream(string.Format("{0}", path), FileMode.Open);
// byte[] data = new byte[fileStream.Length];
// fileStream.Read(data, 0, data.Length);
// fileStream.Close();
// string jsonData = Encoding.UTF8.GetString(data);
// */
// List<SimulationModelInfo> loadInfos = new List<SimulationModelInfo>();
// loadInfos = JsonConvert.DeserializeObject<List<SimulationModelInfo>>(jsonData);
// string resourcePath = "Models";
// Dictionary<string, SimulationModel> loadModels = new Dictionary<string, SimulationModel>();
// foreach(var item in loadInfos)
// {
// var simulationModel = Resources.Load<GameObject>(string.Format("{0}/{1}", resourcePath, item.prefabName));
// var spawnItem = Instantiate(simulationModel).GetComponent<SimulationModel>();
// spawnItem.modelName = item.modelName;
// spawnItem.modelType = item.modelType;
// spawnItem.modelID = item.modelID;
// spawnItem.nodeID = item.nodeID;
// spawnItem.gameObject.transform.position = ParseVector3(item.position);
// spawnItem.gameObject.transform.localScale = ParseVector3(item.scale);
// spawnItem.gameObject.transform.rotation = ParseQuaternion(item.rotation);
// if ( !string.IsNullOrEmpty(item.nodeID))
// loadModels.Add(item.nodeID, spawnItem);
// }
// LogicMappingDataBase.LoadDataBase(loadModels);
//}
public Vector3 ParseVector3(string input)
{
Vector3 returnValue;
input = input.Replace("(", "");
input = input.Replace(")", "");
var nakedString = input.Split(",");
returnValue.x = float.Parse(nakedString[0]);
returnValue.y = float.Parse(nakedString[1]);
returnValue.z = float.Parse(nakedString[2]);
return returnValue;
}
public Quaternion ParseQuaternion(string input)
{
Quaternion returnValue;
input = input.Replace("(", "");
input = input.Replace(")", "");
var nakedString = input.Split(",");
returnValue.x = float.Parse(nakedString[0]);
returnValue.y = float.Parse(nakedString[1]);
returnValue.z = float.Parse(nakedString[2]);
returnValue.w = float.Parse(nakedString[3]);
return returnValue;
}
}
}