SceneEdit_RunScript auto adds path challenges

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

SceneEdit_RunScript auto adds path challenges

Post by KnightHawk75 »

So with the addition of FileNamePath some time ago to the Scenario Wrapper it's now possible to keep everything self contained into one's own distribution folder, images\css\html\delta-ini's\etc this is very nice. However it's more a challenge than needed to try and keep Lua scripts in there because at present RunScript() tries to tack on automatically <<CMOPATH>>\Lua\ onto whatever you feed it. While I'm sure this is helpful to most (and myself at times) as the default, it's not sometimes.

I'd like to suggested a change to it to account for those that don't want it touching the path fed to it. First thing that comes to mind is changing it to something like ScenEdit_RunScript(string thepath, bool resolvepath=true) such that nothing will change for any existing Lua code in the wild. But if someone were to feed it the new second parameter and set it to false such as ScenEdit_RunScript(VP_GetScenario().FileNamePath .. '\\somescript.lua',false); the false would tell it to skip the default addition and run non-gimped. Another method could be no second parameter but instead adding a check if the string submitted .contains(@":\") || .contains(@"\\"), first check covers typical cases, the second would cover cases where it's a network share path, if either true avoid the path manipulation and just proceed to the file exists check.

Now there is some path manipulation to work around this at present, but only reliably if the scene path is under <<CMOPATH>>\Scenarios. Posted here in case anyone needs work around for that common case.

Code: Select all

 -- @param scriptfilename - optional string of the script file name you want added to the prepared path.
 -- @returns  - nil on error, string path on return, if scriptname was provided it will be added.
 function buildCMOScenePathForRunScript(strfullpath,scriptfilename)
   if strfullpath ~=nil then
     local a,b = strfullpath:find("\\Scenarios");
     if (a ~=nil) and scriptfilename == nil then
       return ".." .. strfullpath:sub(a) .. "\\";
     elseif  a ~=nil then 
       return ".." .. strfullpath:sub(a) .. "\\" .. scriptfilename;
     else
       print("buildCMOScenePathForRunScript(): path parameter did not contain \\Scenarios" );
     end
   else
     print("buildCMOScenePathForRunScript(): missing required scenepath parameter.");
   end
   return nil;
 end
 --usage
 buildCMOScenePathForRunScript(VP_GetScenario().FileNamePath); --just get the fixed up path
 buildCMOScenePathForRunScript(VP_GetScenario().FileNamePath,"Spy7Test.lua"); -- tack on filename in root of scene folder.
 -- execute from a subfolder inside the source folder for the scene
 local retval = pcall(ScenEdit_RunScript,buildCMOScenePathForRunScript(VP_GetScenario().FileNamePath,"scripts\\Spy7Test.lua"));
 
Post Reply

Return to “Lua Legion”