Files
HDRobotics/Assets/Scripts/Model/ProgramModel.cs

90 lines
2.4 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System.Linq;
public class ProgramModel
{
private List<RobotProgram> allPrograms = new List<RobotProgram>();
private string savePath;
public RobotProgram CurrentProgram { get; private set; }
public ProgramModel()
{
savePath = Path.Combine(Application.persistentDataPath, "programs");
Directory.CreateDirectory(savePath); // <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
LoadAllPrograms();
}
public bool CreateNewProgram(string userInputId)
{
if (string.IsNullOrEmpty(userInputId)) return false;
string newProgramId = $"{userInputId}.job";
Debug.Log($"{userInputId}.job <20><><EFBFBD><EFBFBD>");
// ID<49><44> <20>̹<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD> Ȯ<><C8AE>
if (GetAllProgramIds().Contains(newProgramId))
{
Debug.LogError($"<22><><EFBFBD>α׷<CEB1> ID '{newProgramId}'<27><> <20>̹<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>մϴ<D5B4>.");
return false;
}
CurrentProgram = new RobotProgram(newProgramId); // <20>޸𸮿<DEB8> <20><> <20><><EFBFBD>α׷<CEB1> <20><><EFBFBD><EFBFBD>
SaveCurrentProgram();
return true;
}
public bool LoadProgram(string programId)
{
string filePath = Path.Combine(savePath, programId + ".job");
if (File.Exists(filePath))
{
string json = File.ReadAllText(filePath);
CurrentProgram = JsonUtility.FromJson<RobotProgram>(json);
return true;
}
return false;
}
public void SaveCurrentProgram()
{
if (CurrentProgram == null) return;
string json = JsonUtility.ToJson(CurrentProgram, true);
string filePath = Path.Combine(savePath, CurrentProgram.programId + ".job");
File.WriteAllText(filePath, json);
// <20><>ü <20><><EFBFBD>α׷<CEB1> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
LoadAllPrograms();
}
public void AddPointToCurrentProgram(Vector3 point)
{
CurrentProgram.endpointPositions.Add(point);
}
public List<string> GetAllProgramIds()
{
List<string> ids = new List<string>();
foreach (var program in allPrograms)
{
ids.Add(program.programId);
}
return ids;
}
private void LoadAllPrograms()
{
allPrograms.Clear();
string[] files = Directory.GetFiles(savePath, "*.job");
foreach (string file in files)
{
string programId = Path.GetFileNameWithoutExtension(file);
allPrograms.Add(new RobotProgram(programId));
}
}
}