using System.Collections.Generic; using System.IO; using UnityEngine; using System.Linq; public class ProgramModel { private List allPrograms = new List(); 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(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 GetAllProgramIds() { List ids = new List(); 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)); } } }