From c917f9d18ba50583e04be9c1d53ce5761b820591 Mon Sep 17 00:00:00 2001
From: Mauco03 <marco.gaibotti@skywarder.eu>
Date: Fri, 10 May 2024 13:09:38 +0200
Subject: [PATCH] [refactoring-ode][Mission] Added ability to load mission from
 constructor arguments

---
 classes/Mission.m | 55 +++++++++++++++++++++++++++++------------------
 1 file changed, 34 insertions(+), 21 deletions(-)

diff --git a/classes/Mission.m b/classes/Mission.m
index 8e5f955..fe5590f 100644
--- a/classes/Mission.m
+++ b/classes/Mission.m
@@ -1,14 +1,14 @@
 classdef Mission < Config
-% Mission: Contains names and paths needed to access mission-dependent 
-% config files
-%
-%   Constructor:
-%       - Mission: Creates an instance of the Mission class.
-%           Loaded config: missionConfig.m
-%           Loaded data: -
-%           Arguments:
-%               - loadConfig: bool, Whether to load config file or return
-%               an empty mission
+    % Mission: Contains names and paths needed to access mission-dependent
+    % config files
+    %
+    %   Constructor:
+    %       - Mission: Creates an instance of the Mission class.
+    %           Loaded config: missionConfig.m
+    %           Loaded data: -
+    %           Arguments:
+    %               - loadConfig: bool, Whether to load config file or return
+    %               an empty mission
 
     properties
         name % Mission name, used to access <mission> folder
@@ -27,23 +27,39 @@ classdef Mission < Config
     end
 
     methods
-        function obj = Mission(loadConfig)
+        function obj = Mission(m)
             arguments
-                loadConfig {islogical} = false
+                m {mustBeA(m, {'logical', 'string', 'char'})} = false
+            end
+
+            if ~m, return; end
+
+            filePath = fullfile(fileparts(mfilename("fullpath")), '..', 'missions');
+            filePath = trimPath(filePath);
+
+            obj.currentPath = filePath;
+            if isa(m, 'char') || isa(m, 'string')
+                obj.name = m;
+            else
+                obj.loadConfig();
             end
-            if loadConfig, obj.loadConfig; end
-            obj.updatePath;
         end
 
         function set.name(obj, name)
+            if ~isempty(obj.currentPath) && ...
+                    ~isfolder(fullfile(obj.currentPath, name)) %#ok<MCSUP>
+                error('Invalid mission: %s', name)
+            end
+
             obj.name = name;
-            obj.updatePath;
+            obj.updatePath();
         end
     end
 
     methods(Access = protected)
         function updatePath(obj)
             if isempty(obj.name) || isempty(obj.currentPath), return; end
+
             obj.configPath = fullfile(obj.currentPath, obj.name, 'config');
             obj.dataPath = fullfile(obj.currentPath, obj.name, 'data');
             obj.msaPath = trimPath( ...
@@ -53,19 +69,16 @@ classdef Mission < Config
 
         function loadConfig(obj)
             fileName = obj.configName;
-            filePath = fullfile(fileparts(mfilename("fullpath")), '..', 'missions');
-            filePath = trimPath(filePath);
+            filePath = obj.currentPath;
 
             if ~isfile(fullfile(filePath, fileName))
-                error(strcat("File not found inside the config folder: ", filePath)); 
+                error(strcat("File not found inside the config folder: ", filePath));
             end
 
             varName = strtok(fileName,'C');
             run(fileName);
             configObj = eval(varName);
-            fields = obj.getProperties('writable');
-            for j = 1:size(fields, 2), obj.(fields{j}) = configObj.(fields{j}); end
-            obj.currentPath = filePath;
+            obj.name = configObj.name;
         end
     end
 
-- 
GitLab