/
GeekNerd
/
ServerModelingProject
Обзор
Документация
Войти
/
GeekNerd
/
ServerModelingProject
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Assets/Scripts/App/ProjectService.cs
147 строк
5 KB
Gorney-Alex
Init
30 май 2026, 19:05
30 май 2026, 19:05
3cdcc49
Код
Авторство
О чём код?
using System; using System.IO; using DataClasses.Project; using LoadServices; using UnityEngine; namespace App { public class ProjectService { private const string ConfigFileName = "project_config.json"; private const string DataFolderName = "Json"; private ProjectConfig _currentProject; public ProjectConfig CurrentProject => _currentProject; public bool CreateProject(string projectName, string folderPath) { if (string.IsNullOrWhiteSpace(projectName)) { Debug.LogWarning("[ProjectService] Project name is empty."); return false; } if (string.IsNullOrWhiteSpace(folderPath) || !Directory.Exists(folderPath)) { Debug.LogWarning("[ProjectService] Invalid folder path."); return false; } string projectFolder = Path.Combine(folderPath, SanitizeName(projectName)); if (Directory.Exists(projectFolder)) { Debug.LogWarning($"[ProjectService] Project folder already exists: {projectFolder}"); return false; } Directory.CreateDirectory(projectFolder); Directory.CreateDirectory(GetDataFolderPath(projectFolder)); _currentProject = new ProjectConfig { ProjectName = projectName, CreatedAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), LastSavedAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ProjectFolderPath = projectFolder }; SaveConfig(); Debug.Log($"[ProjectService] Project created at: {projectFolder}"); return true; } public void SaveData<T>(T data, string fileName) { if (_currentProject == null) { Debug.LogError("[ProjectService] No active project."); return; } string path = GetDataPath(fileName); JSONLoader.SaveJsonToPath(data, path); _currentProject.LastSavedAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); SaveConfig(); } public T LoadData<T>(string fileName) { if (_currentProject == null) { Debug.LogError("[ProjectService] No active project."); return default; } string path = GetDataPath(fileName); if (File.Exists(path)) { return JSONLoader.LoadJsonFromPath<T>(path); } // Backward compatibility: older projects may store data files in the project root. string legacyPath = GetLegacyDataPath(fileName); return JSONLoader.LoadJsonFromPath<T>(legacyPath); } public bool LoadProject(string projectFolderPath) { string configPath = Path.Combine(projectFolderPath, ConfigFileName); if (!File.Exists(configPath)) { Debug.LogError($"[ProjectService] Config not found at: {configPath}"); return false; } _currentProject = JSONLoader.LoadJsonFromPath<ProjectConfig>(configPath); if (_currentProject != null) { Directory.CreateDirectory(GetDataFolderPath(_currentProject.ProjectFolderPath)); } Debug.Log($"[ProjectService] Loaded project: {_currentProject.ProjectName}"); return true; } private void SaveConfig() { string path = Path.Combine(_currentProject.ProjectFolderPath, ConfigFileName); JSONLoader.SaveJsonToPath(_currentProject, path); } private string GetDataPath(string fileName) { string dataFolderPath = GetDataFolderPath(_currentProject.ProjectFolderPath); if (!Directory.Exists(dataFolderPath)) { Directory.CreateDirectory(dataFolderPath); } return Path.Combine(dataFolderPath, fileName + ".json"); } private string GetLegacyDataPath(string fileName) { return Path.Combine(_currentProject.ProjectFolderPath, fileName + ".json"); } private static string GetDataFolderPath(string projectFolderPath) { return Path.Combine(projectFolderPath, DataFolderName); } private static string SanitizeName(string name) { foreach (char c in Path.GetInvalidFileNameChars()) { name = name.Replace(c, '_'); } return name; } } }