| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using System.Reflection; |
| | 4 | |
|
| | 5 | | namespace Morpho25.Management |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Database source enum. |
| | 9 | | /// </summary> |
| | 10 | | public enum DatabaseSource |
| | 11 | | { |
| | 12 | | User, |
| | 13 | | Project |
| | 14 | | } |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Workspace class. |
| | 18 | | /// </summary> |
| | 19 | | public class Workspace |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// Default envimet folder. |
| | 23 | | /// </summary> |
| | 24 | | public const string DEFAULT_FOLDER = "ENVImet5"; |
| | 25 | | #region System details |
| | 26 | | private const string VERSION = "7174219"; |
| | 27 | | private const string ENCRIPTION_LEVEL = "5194660"; |
| | 28 | | private const string CHECK_SUM = "18076640"; |
| | 29 | | private const string SYSTEM_FOLDER = "sys.basedata"; |
| | 30 | | private const string PYTHON_FOLDER = "sys.python"; |
| | 31 | | #endregion |
| | 32 | |
|
| | 33 | | #region User details |
| | 34 | | private const string USER_FOLDER = "sys.userdata"; |
| | 35 | | private const string USER_SETTINGS = "usersettings.setx"; |
| | 36 | | private const string USER_DB = "userdatabase.edb"; |
| | 37 | | public const string FOLDER_NAME = "ENVI-met"; |
| | 38 | | #endregion |
| | 39 | |
|
| | 40 | | #region Project details |
| | 41 | | private const string PROJECT_INFOX = "project.infox"; |
| | 42 | | private const string PROJECT_DB = "projectdatabase.edb"; |
| | 43 | | private const string SYSTEM_DB = "database.edb"; |
| | 44 | | #endregion |
| | 45 | |
|
| | 46 | | #region Main paths |
| | 47 | | private string _workspaceFolder; |
| | 48 | | private string _projectFolder; |
| | 49 | | private string _pythonFolder; |
| | 50 | | #endregion |
| | 51 | |
|
| | 52 | | #region DB paths |
| | 53 | | private string _systemFolder; |
| | 54 | | private string _userFolder; |
| | 55 | | #endregion |
| | 56 | |
|
| 0 | 57 | | public string EnvimetFolder { get; set; } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Workspace folder. |
| | 61 | | /// </summary> |
| | 62 | | public string WorkspaceFolder |
| | 63 | | { |
| 0 | 64 | | get { return _workspaceFolder; } |
| | 65 | | private set |
| 0 | 66 | | { |
| 0 | 67 | | string folder = GetFolder(value); |
| 0 | 68 | | _workspaceFolder = folder; |
| 0 | 69 | | } |
| | 70 | | } |
| | 71 | | /// <summary> |
| | 72 | | /// Project folder. |
| | 73 | | /// </summary> |
| | 74 | | public string ProjectFolder |
| | 75 | | { |
| 0 | 76 | | get { return _projectFolder; } |
| | 77 | | private set |
| 0 | 78 | | { |
| 0 | 79 | | string folder = GetFolder(value); |
| 0 | 80 | | _projectFolder = value; |
| 0 | 81 | | } |
| | 82 | | } |
| | 83 | | /// <summary> |
| | 84 | | /// Project name. |
| | 85 | | /// </summary> |
| 0 | 86 | | public string ProjectName { get; private set; } |
| | 87 | | /// <summary> |
| | 88 | | /// Model name. |
| | 89 | | /// </summary> |
| 0 | 90 | | public string ModelName { get; private set; } |
| | 91 | | /// <summary> |
| | 92 | | /// Model path. |
| | 93 | | /// </summary> |
| 0 | 94 | | public string ModelPath { get; private set; } |
| | 95 | | /// <summary> |
| | 96 | | /// Project database location. |
| | 97 | | /// </summary> |
| 0 | 98 | | public string ProjectDB { get; private set; } |
| | 99 | | /// <summary> |
| | 100 | | /// System database location. |
| | 101 | | /// </summary> |
| 0 | 102 | | public string SystemDB { get; private set; } |
| | 103 | | /// <summary> |
| | 104 | | /// User database location. |
| | 105 | | /// </summary> |
| 0 | 106 | | public string UserDB { get; private set; } |
| | 107 | |
|
| | 108 | | private string GetFolder(string folder) |
| 0 | 109 | | { |
| 0 | 110 | | if (!(Directory.Exists(folder))) |
| 0 | 111 | | { |
| 0 | 112 | | Directory.CreateDirectory(folder); |
| 0 | 113 | | } |
| 0 | 114 | | return folder; |
| 0 | 115 | | } |
| | 116 | | /// <summary> |
| | 117 | | /// Create a workspace object. |
| | 118 | | /// </summary> |
| | 119 | | /// <param name="workspaceFolder">Workspace folder.</param> |
| | 120 | | /// <param name="databaseSource">Database source.</param> |
| | 121 | | /// <param name="envimetFolder">Envimet folder on machine.</param> |
| | 122 | | /// <exception cref="ArgumentOutOfRangeException">Envimet not found.</exception> |
| 4 | 123 | | public Workspace(string workspaceFolder, |
| 4 | 124 | | DatabaseSource databaseSource, |
| 4 | 125 | | string envimetFolder = null) |
| 4 | 126 | | { |
| 4 | 127 | | _systemFolder = GetEnvimetSystemFolder(SYSTEM_FOLDER, envimetFolder); |
| 4 | 128 | | _userFolder = GetEnvimetSystemFolder(USER_FOLDER, envimetFolder); |
| 4 | 129 | | _pythonFolder = GetEnvimetSystemFolder(PYTHON_FOLDER, envimetFolder); |
| | 130 | |
|
| 4 | 131 | | if (_systemFolder == null) |
| 4 | 132 | | throw new ArgumentOutOfRangeException("Envimet not found! " + |
| 4 | 133 | | "Check on your machine or set EnvimetFolder."); |
| | 134 | |
|
| 0 | 135 | | WorkspaceFolder = workspaceFolder; |
| 0 | 136 | | ProjectName = "Project"; |
| 0 | 137 | | ModelName = "Model"; |
| | 138 | |
|
| 0 | 139 | | ProjectFolder = Path.Combine(WorkspaceFolder, ProjectName); |
| 0 | 140 | | ModelPath = Path.Combine(ProjectFolder, ModelName + ".inx"); |
| | 141 | |
|
| 0 | 142 | | string projectInfoxAbsPath = Path.Combine(ProjectFolder, PROJECT_INFOX); |
| | 143 | |
|
| 0 | 144 | | SetUserSettings(); |
| 0 | 145 | | SetProjectInfox(projectInfoxAbsPath, databaseSource); |
| | 146 | |
|
| 0 | 147 | | SetDatabase(databaseSource, _systemFolder, _userFolder); |
| 0 | 148 | | } |
| | 149 | | /// <summary> |
| | 150 | | /// Create a workspace object. |
| | 151 | | /// </summary> |
| | 152 | | /// <param name="workspaceFolder">Workspace folder.</param> |
| | 153 | | /// <param name="databaseSource">Database source.</param> |
| | 154 | | /// <param name="projectName">Project name.</param> |
| | 155 | | /// <param name="modelName">Model name.</param> |
| | 156 | | /// <param name="envimetFolder">Envimet folder.</param> |
| | 157 | | public Workspace(string workspaceFolder, DatabaseSource databaseSource, |
| | 158 | | string projectName, string modelName, string envimetFolder) |
| 0 | 159 | | : this(workspaceFolder, databaseSource, envimetFolder) |
| 0 | 160 | | { |
| 0 | 161 | | ProjectName = projectName; |
| 0 | 162 | | ProjectFolder = Path.Combine(WorkspaceFolder, ProjectName); |
| 0 | 163 | | ModelName = modelName; |
| 0 | 164 | | ModelPath = Path.Combine(ProjectFolder, ModelName + ".inx"); |
| | 165 | |
|
| 0 | 166 | | string projectInfoxAbsPath = Path.Combine(ProjectFolder, PROJECT_INFOX); |
| | 167 | |
|
| 0 | 168 | | SetUserSettings(); |
| 0 | 169 | | SetProjectInfox(projectInfoxAbsPath, databaseSource); |
| | 170 | |
|
| 0 | 171 | | SetDatabase(databaseSource, _systemFolder, _userFolder); |
| 0 | 172 | | } |
| | 173 | | /// <summary> |
| | 174 | | /// String representation of the workspace object. |
| | 175 | | /// </summary> |
| | 176 | | /// <returns>String representation.</returns> |
| | 177 | | public override string ToString() |
| 0 | 178 | | { |
| 0 | 179 | | return String.Format("Workspace::{0}::{1}", |
| 0 | 180 | | ProjectName, ModelName); |
| 0 | 181 | | } |
| | 182 | |
|
| | 183 | | private void SetDatabase(DatabaseSource databaseSource, |
| | 184 | | string systemFolder, string userFolder) |
| 0 | 185 | | { |
| 0 | 186 | | SystemDB = Path.Combine(systemFolder, SYSTEM_DB); |
| | 187 | |
|
| 0 | 188 | | if (databaseSource == DatabaseSource.Project) |
| 0 | 189 | | { |
| 0 | 190 | | ProjectDB = Path.Combine(ProjectFolder, PROJECT_DB); |
| 0 | 191 | | if (!File.Exists(ProjectDB)) |
| 0 | 192 | | SetProjectDB(); |
| 0 | 193 | | } |
| | 194 | | else |
| 0 | 195 | | { |
| 0 | 196 | | UserDB = Path.Combine(userFolder, USER_DB); |
| 0 | 197 | | } |
| 0 | 198 | | } |
| | 199 | |
|
| | 200 | | private string GetEnvimetSystemFolder(string folderName, |
| | 201 | | string envimetFolder) |
| 12 | 202 | | { |
| 12 | 203 | | string root = System.IO.Path.GetPathRoot(Environment |
| 12 | 204 | | .GetFolderPath(Environment.SpecialFolder.ApplicationData)); |
| 12 | 205 | | string directory = System.IO.Path.Combine(root, |
| 12 | 206 | | DEFAULT_FOLDER + $"\\{folderName}\\"); |
| | 207 | |
|
| 12 | 208 | | if (envimetFolder != null) |
| 0 | 209 | | directory = System.IO.Path.Combine(envimetFolder, $"{folderName}\\"); |
| | 210 | |
|
| 12 | 211 | | if (System.IO.Directory.Exists(directory)) |
| 0 | 212 | | return directory; |
| | 213 | | else |
| 12 | 214 | | return null; |
| 12 | 215 | | } |
| | 216 | |
|
| | 217 | | private void SetUserSettings() |
| 0 | 218 | | { |
| 0 | 219 | | var folder = Environment.GetFolderPath( |
| 0 | 220 | | Environment.SpecialFolder.ApplicationData); |
| 0 | 221 | | var targetFolder = Path.Combine(folder, FOLDER_NAME); |
| 0 | 222 | | var targetFile = Path.Combine(targetFolder, USER_SETTINGS); |
| | 223 | |
|
| 0 | 224 | | if (!Directory.Exists(targetFolder)) |
| 0 | 225 | | Directory.CreateDirectory(targetFolder); |
| | 226 | |
|
| 0 | 227 | | WriteUserSettings(targetFile); |
| 0 | 228 | | } |
| | 229 | |
|
| | 230 | | private void WriteUserSettings(string userSettingsAbsPath) |
| 0 | 231 | | { |
| 0 | 232 | | var now = DateTime.Now; |
| | 233 | |
|
| 0 | 234 | | string[] userSettingsData = { |
| 0 | 235 | | "<ENVI-MET_Datafile>", |
| 0 | 236 | | "<Header>", |
| 0 | 237 | | "<filetype>SETX ENVI-met User Settings</filetype>", |
| 0 | 238 | | $"<version>{VERSION}</version>", |
| 0 | 239 | | $"<revisiondate>{now.ToString("yyyy-MM-dd HH:mm: ss")}</revisiondate>", |
| 0 | 240 | | "<remark></remark>", |
| 0 | 241 | | "<checksum>0</checksum>", |
| 0 | 242 | | "<encryptionlevel>0</encryptionlevel>", |
| 0 | 243 | | "</Header>", |
| 0 | 244 | | "<current_workspace>", |
| 0 | 245 | | $"<absolute_path> {WorkspaceFolder} </absolute_path>", |
| 0 | 246 | | $"<last_active> {ProjectName} </last_active>", |
| 0 | 247 | | "</current_workspace>", |
| 0 | 248 | | "<python_settings>", |
| 0 | 249 | | $"<selectedPython> {_pythonFolder} </selectedPython>", |
| 0 | 250 | | "</python_settings>", |
| 0 | 251 | | "<userpathinfo>", |
| 0 | 252 | | "<userpathmode> 1 </userpathmode>", |
| 0 | 253 | | $"<userpathinfo> {_userFolder} </userpathinfo>", |
| 0 | 254 | | "</userpathinfo>", |
| 0 | 255 | | "</ENVI-MET_Datafile>" |
| 0 | 256 | | }; |
| | 257 | |
|
| 0 | 258 | | File.WriteAllLines(userSettingsAbsPath, userSettingsData); |
| 0 | 259 | | } |
| | 260 | |
|
| | 261 | | private void SetProjectDB() |
| 0 | 262 | | { |
| 0 | 263 | | var now = DateTime.Now; |
| | 264 | |
|
| 0 | 265 | | string[] projectDBdata = { |
| 0 | 266 | | "<ENVI-MET_Datafile>", |
| 0 | 267 | | "<Header>", |
| 0 | 268 | | "<filetype>DATA</filetype>", |
| 0 | 269 | | "<version>1</version>", |
| 0 | 270 | | String.Format("<revisiondate>{0}</revisiondate>", |
| 0 | 271 | | now.ToString("yyyy-MM-dd HH:mm:ss")), |
| 0 | 272 | | "<remark>Envi-Data</remark>", |
| 0 | 273 | | "<checksum>0</checksum>", |
| 0 | 274 | | "<encryptionlevel>1</encryptionlevel>", |
| 0 | 275 | | "</Header>", |
| 0 | 276 | | "</ENVI-MET_Datafile>" |
| 0 | 277 | | }; |
| | 278 | |
|
| 0 | 279 | | File.WriteAllLines(ProjectDB, projectDBdata); |
| 0 | 280 | | } |
| | 281 | |
|
| | 282 | | private void SetProjectInfox(string projectInfoxAbsPath, DatabaseSource databaseSource) |
| 0 | 283 | | { |
| 0 | 284 | | var now = DateTime.Now; |
| | 285 | |
|
| 0 | 286 | | string[] projectInfoData = { |
| 0 | 287 | | "<ENVI-MET_Datafile>", |
| 0 | 288 | | "<Header>", |
| 0 | 289 | | "<filetype>infoX ENVI-met Project Description File</filetype>", |
| 0 | 290 | | $"<version>{VERSION}</version>", |
| 0 | 291 | | $"<revisiondate>{now.ToString("yyyy-MM-dd HH:mm:ss")}</revisiondate>", |
| 0 | 292 | | "<remark></remark>", |
| 0 | 293 | | $"<checksum>{CHECK_SUM}</checksum>", |
| 0 | 294 | | $"<encryptionlevel>{ENCRIPTION_LEVEL}</encryptionlevel>", |
| 0 | 295 | | "</Header>", |
| 0 | 296 | | "<project_description>", |
| 0 | 297 | | $"<name> {ProjectName} </name>", |
| 0 | 298 | | "<description> </description>", |
| 0 | 299 | | $"<useProjectDB> {(int) databaseSource} </useProjectDB>", |
| 0 | 300 | | "</project_description>", |
| 0 | 301 | | "</ENVI-MET_Datafile>" |
| 0 | 302 | | }; |
| | 303 | |
|
| 0 | 304 | | File.WriteAllLines(projectInfoxAbsPath, projectInfoData); |
| 0 | 305 | | } |
| | 306 | | } |
| | 307 | | } |