/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2012 I-maginer This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ----------------------------------------------------------------------------- */ struct PlugConfigFile = [ PCF_instance : PInstance, PCF_srcFile : S, PCF_XMLfile : XMLfile, PCF_XMLdef : XMLfile ]mkPlugConfigFile;; var sConfigPath = "tmp/config/";; fun deleteOb(inst, confstr)= XMLclose confstr.PCF_XMLfile; XMLclose confstr.PCF_XMLdef; 0;; fun opInts(sp, v, op)= let strextr sp -> l in let "" -> nv in ( while (l != nil) do ( let hd l -> lv1 in ( if (((hd lv1) != nil) && ((strlen nv) != 0)) then set nv = strcat nv "\n"; let 0 -> nbval in while (lv1 != nil) do ( let hd lv1 -> v2 in let if (op == 1) then (itoa ((atoi v2) + v)) else (itoa ((atoi v2) - v)) -> av in if (nbval == 0) then set nv = strcat nv av else set nv = strcatn nv::" "::av::nil; set lv1 = tl lv1; set nbval = nbval + 1; ); ); set l = tl l; ); nv; );; // if we cannot find the value in the current config file we take it from the default file and store it in the config file; fun getVarValue(confstr, name)= let XMLgetMarkByValue confstr.PCF_XMLfile "config" -> confmark in let XMLgetMarkByValueAndParamValueFromMark confmark "var" "name" name -> varmark in if varmark != nil then varmark else let XMLgetMarkByValue confstr.PCF_XMLdef "config" -> defconfmark in let XMLgetMarkByValueAndParamValueFromMark defconfmark "var" "name" name -> defvarmark in XMLaddMark confstr.PCF_XMLfile "var" varmark ["name" (strtrim name)]::nil (XMLgetData defvarmark);; fun cbConfigAction(inst, from, action, param, rep, confstr) = let substr action ((strlen (getPluginInstanceName inst)) + 1) (strlen action) -> action in if (!strcmp "Get" (substr action 0 3)) then ( let substr action 4 (strlen action) -> varname in let getVarValue confstr varname -> varmark in SendPluginEvent inst strcat varname " value" (XMLgetData varmark) nil; 0; ) else if (!strcmp "Set" (substr action 0 3)) then ( let substr action 4 (strlen action) -> varname in let getVarValue confstr varname -> varmark in ( // update default config let XMLgetMarkByValue confstr.PCF_XMLdef "config" -> defconfmark in let XMLgetMarkByValueAndParamValueFromMark defconfmark "var" "name" varname -> defvarmark in XMLsetData defvarmark param; XMLsetData varmark param; XMLwrite confstr.PCF_XMLfile strcatn sConfigPath::(getShortName getPluginInstanceFullname inst)::".conf"::nil; set confstr.PCF_srcFile = strcatn sConfigPath::(getShortName getPluginInstanceFullname inst)::".conf"::nil; SendPluginEvent inst strcat varname " value" (XMLgetData varmark) nil; ); 0; ) else if (!strcmp "Increment" (substr action 0 9)) then ( let substr action 10 (strlen action) -> varname in let getVarValue confstr varname -> varmark in let XMLgetData varmark -> value in let if ((atof param) != nil) then opInts value (atoi param) 1 else strcat value param -> value in ( // update default config let XMLgetMarkByValue confstr.PCF_XMLdef "config" -> defconfmark in let XMLgetMarkByValueAndParamValueFromMark defconfmark "var" "name" varname -> defvarmark in XMLsetData defvarmark value; XMLsetData varmark value; XMLwrite confstr.PCF_XMLfile strcatn sConfigPath::(getShortName getPluginInstanceFullname inst)::".conf"::nil; set confstr.PCF_srcFile = strcatn sConfigPath::(getShortName getPluginInstanceFullname inst)::".conf"::nil; SendPluginEvent inst strcat varname " value" (XMLgetData varmark) nil; ); 0; ) else if (!strcmp "Decrement" (substr action 0 9)) then ( let substr action 10 (strlen action) -> varname in let getVarValue confstr varname -> varmark in let XMLgetData varmark -> value in let if ((atof param) != nil) then opInts value (atoi param) 0 else strcat value param -> value in ( // update default config let XMLgetMarkByValue confstr.PCF_XMLdef "config" -> defconfmark in let XMLgetMarkByValueAndParamValueFromMark defconfmark "var" "name" varname -> defvarmark in XMLsetData defvarmark value; XMLsetData varmark value; XMLwrite confstr.PCF_XMLfile strcatn sConfigPath::(getShortName getPluginInstanceFullname inst)::".conf"::nil; set confstr.PCF_srcFile = strcatn sConfigPath::(getShortName getPluginInstanceFullname inst)::".conf"::nil; SendPluginEvent inst strcat varname " value" (XMLgetData varmark) nil; ); 0; ) else nil; 0;; fun cbLoad(inst, from, action, param, rep, confstr) = let XMLloadString param -> xmlparam in if (xmlparam == nil) || !(sizelist xmlparam.XMLmarks) || ((XMLgetMarkByValue xmlparam "config") == nil) then ( //reload file if (confstr.PCF_XMLdef == confstr.PCF_XMLfile) then ( XMLclose confstr.PCF_XMLdef; set confstr.PCF_XMLdef = XMLload confstr.PCF_srcFile; ); XMLclose confstr.PCF_XMLfile; set confstr.PCF_XMLfile = XMLload confstr.PCF_srcFile; ) else ( set confstr.PCF_XMLdef = confstr.PCF_XMLfile; set confstr.PCF_XMLfile = xmlparam; ); let XMLgetMarkByValue confstr.PCF_XMLdef "config" -> defconfmark in let XMLgetMarksByValueFromMarkSons defconfmark "var" -> levent in let XMLgetMarkByValue confstr.PCF_XMLfile "config" -> confmark in while (levent != nil) do ( let hd levent -> defvarmark in let XMLgetMarkByValueAndParamValueFromMark confmark "var" "name" (XMLgetParam defvarmark "name") -> varmark in if (varmark != nil) then ( //add new value in def mark XMLsetData defvarmark (XMLgetData varmark); SendPluginEvent inst strcat (XMLgetParam varmark "name") " value" (XMLgetData varmark) nil; 0; ) else ( XMLcopyMark confstr.PCF_XMLfile defvarmark confmark; 0; ); set levent = tl levent; ); if !strcmp (XMLserialize confstr.PCF_XMLfile) (XMLserialize confstr.PCF_XMLdef) then nil else ( XMLwrite confstr.PCF_XMLfile strcatn sConfigPath::(getShortName getPluginInstanceFullname inst)::".conf"::nil; set confstr.PCF_srcFile = strcatn sConfigPath::(getShortName getPluginInstanceFullname inst)::".conf"::nil; ); 0;; fun cbSave(inst, from, action, param, rep, confstr) = XMLwrite confstr.PCF_XMLfile nil; 0;; fun cbLoaded(inst, confstr)= cbLoad inst nil nil nil nil confstr; 0;; fun newOb(inst)= let atoi (getPluginInstanceParam inst "loadonstart") -> loadonstart in let if loadonstart == nil then 1 else loadonstart -> loadonstart in let strcatn sConfigPath::(getShortName getPluginInstanceFullname inst)::".conf"::nil -> configfilepath in let strcatn sConfigPath::(getPluginInstanceName inst)::".conf"::nil -> configfilepathold in let if (((_checkpack configfilepath) == nil) && ((_checkpack configfilepathold) != nil)) then configfilepathold else configfilepath -> configfilepath in let XMLload configfilepath -> xmlfile in let XMLloadString webtostr (getPluginInstanceParam inst "xmldata") -> xmldata in let if xmlfile != nil then xmlfile else (XMLwrite xmldata configfilepath; XMLload configfilepath;) -> xmlfile in let mkPlugConfigFile [inst configfilepath xmlfile xmldata] -> confstr in ( PluginRegisterAction inst "Load" mkfun6 @cbLoad confstr; PluginRegisterAction inst "Save" mkfun6 @cbSave confstr; let getPluginInstanceUserActions inst -> laction in let sizelist laction -> size in let 0 -> i in while i < size do ( let nth_list laction i -> act in PluginRegisterAction inst act mkfun6 @cbConfigAction confstr; set i = i + 1; ); if !loadonstart then nil else ( if inst.INST_groupstr.GRP_project.PRJ_bPluginsLoaded then cbLoad inst nil nil nil nil confstr else ( setPluginInstanceCbAllPluginsLoaded inst mkfun2 @cbLoaded confstr; 0; ); ); setPluginInstanceCbDel inst mkfun2 @deleteOb confstr; ); 0;; fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;