90 lines
2.4 KiB
C#
90 lines
2.4 KiB
C#
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); // 저장 폴더가 없으면 생성
|
|
LoadAllPrograms();
|
|
}
|
|
|
|
public bool CreateNewProgram(string userInputId)
|
|
{
|
|
if (string.IsNullOrEmpty(userInputId)) return false;
|
|
|
|
string newProgramId = $"{userInputId}.job";
|
|
Debug.Log($"{userInputId}.job 생성");
|
|
|
|
// ID가 이미 존재하는지 확인
|
|
if (GetAllProgramIds().Contains(newProgramId))
|
|
{
|
|
Debug.LogError($"프로그램 ID '{newProgramId}'가 이미 존재합니다.");
|
|
return false;
|
|
}
|
|
|
|
CurrentProgram = new RobotProgram(newProgramId); // 메모리에 새 프로그램 생성
|
|
|
|
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);
|
|
|
|
// 전체 프로그램 목록 갱신
|
|
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));
|
|
}
|
|
}
|
|
} |