Batch Simple Plugin System Concept

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Batch Simple Plugin System Concept

#1 Post by Adrianvdh » 05 Jan 2014 15:21

Haha, Hey everyone.
Sorry I have been away for a while :).
During the holiday I thought about learning C and building a plugin system for learning experience.
But I wondered if it was possible if I could make such a thing in batch, my apotheosis is that I can.

Sadly it would be very limited.
Let me explain how it would work. In the 'main' batch file you would have all the labels

main.bat:

Code: Select all

:mainlabel
::1
::2
::3
:default
::1
::2
:settings


and in the plugin.bat file we would make a function, the function can do anything:

plugin.bat:

Code: Select all

:function
echo This is the test function that is shown in the main batch file :)
pause


call :function mainlabel 2, settings 5
exit /b


Now the parameters work as follows:
We call the function 'function' in the plugin.bat with the parameter 'mainlabel', which is the name of the label this function 'function' is called in the main batch file, and optional parameter with the number i.e '2' is the 3nd line after the label 'mainlabel' in the main batch file for it to be called, this is separated buy a comma so you can use the function multiple times.

Just using that and some fancy code we can do this, as well as the main batch file calling the plugin file.
Building code for this 'experiment' and a plugin manager that is something similar to Notepad++'s plugin manager, downloading and updating etc.

So to call the plugin.bat batch file word work as follows:

In the simple plugin manager it would list all the batch files in a folder called plugins, which is in the root directory of the main batch file.
It will contain all these batch files and using a 'for' command it would call them.

e.g:

Code: Select all

listing code...

call a%%

the variable 'a' is the names of all the batch files.




Sadly I do not have the batch file programming skill to build a system like this and I require help from the DosTips community.
It might take some time but we can do it, many people with batch file games can implement this into there batch file. e.g

The optimized batch Snake.bat can have this and others can make plugins such as score boards, etc, but obviously that would require to draw menus and stuff :)

So it is up to you guys, if you're up for the challenge I would gladly appreciate your help :)

Regards,
Adrianvdh

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch Simple Plugin System Consept

#2 Post by foxidrive » 05 Jan 2014 21:29

A plugin system mihgt work if people collaborate and test their code very well.
The downside is that debugging could be difficult.

An issue I see is that variable names and temp files could be reused in plugins - so a plugin to create unique variable names and temp filenames would be needed so that collisions don't occur.

Also the format for calling the plugins must be very well defined, and somehow it has to allow for dozens of parameters.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch Simple Plugin System Consept

#3 Post by foxidrive » 05 Jan 2014 22:11

berserker wrote:what's the reason for doing this? as a hobby?


I think that's the reason

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: Batch Simple Plugin System Consept

#4 Post by berserker » 06 Jan 2014 00:16

foxidrive wrote:A plugin system mihgt work if people collaborate and test their code very well.
The downside is that debugging could be difficult.

along with that, maintainence , support issues.

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Batch Simple Plugin System Consept

#5 Post by Adrianvdh » 06 Jan 2014 03:25

Well yes, there are really smart people o this forum so I think we will be able to do it.

Debugging? Ya I guess, but they should treat debugging as they would to a normal batch file. It would say the error in the main batch file

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Batch Simple Plugin System Consept

#6 Post by Adrianvdh » 06 Jan 2014 08:50

Sorry I don't want to be pushy, but has anyone taken a look at doing this?

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch Simple Plugin System Consept

#7 Post by Aacini » 06 Jan 2014 12:45

Excuse me. I am afraid I don't understand the purpose of such application. Batch files are not a "system", but a programming language, so I don't see how a Plugin System could be generated for any programming language. For example, how do you develop a Plugin System for the C programming language? (NOT for the applications that may be written in C).

Forget for a moment the examples and explain in plain English: What is the purpose and benefits of such system? It will be used as a replacement or as a companion of a set of libraries (or none of the above)? Remember that there are multiple techniques that may be used in standard programming languages for the development of large applications by separated teams of programmers, like modular programming. Are you talking about this concept?

Antonio

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch Simple Plugin System Consept

#8 Post by penpen » 06 Jan 2014 13:17

I'm not sure if i understood it right:
The "main.bat" should be executed, containing the plugin manager, that searches for
plugins named "*plugin.bat". It searches within every plugin file for such a line:

Code: Select all

call :function mainlabel 2, settings 5
to initialize what function within the plugin should called from main.bat.
The order of operation is specified by:

Code: Select all

:mainlabel
::1
::2
::3
:default
::1
::2
:settings
Am i right, or wrong?

Only if i'm right:
For what are the labels within the main.bat good for: Only to see the order of operations?
You Better may create a main.bat command line switch "/slots" to echo them in such a case,
so the labels could be used within main.bat.

You should add these lines to the top of the plugins, to make it easier to use them:

Code: Select all

@echo off
for /F "tokens=1*" %%l in (%*) do (goto %%~l %%m)
goto :eof

For the same reason you should add a "goto :eof", "exit /b [n]", or "exit" at the end of
the plugins function

So maybe you are searching something like this:
main.bat

Code: Select all

@echo off
setlocal enableDelayedExpansion

:: initializing
set "slots=":mainlabel" "::1" "::2" "::3" ":default" "::1" "::2" ":settings""
set "label="
for %%s in (%slots%) do (
   set "token=%%~s"
   if "!token:::=!" == "!token!" ( set "label=%%~s"
   ) else ( set "!label!%%~s= "
   )
)

:: process parameter
set "dislayInfo="
set "processed="
for %%p in (%*) do (
   if "%~1" == "/slots" ( for %%s in (%slots%) do echo %%~s
   ) else (
      set "dislayInfo=1"
   )
   set "processed=1"
)
if not defined processed if not "%*" == "" set "dislayInfo=1"
if defined dislayInfo (
   set "dislayInfo="
   echo usage: %~nx0 [/slots] [/?^|/help] ...
   echo [...]
   goto :eof
)

:: plugin manager (currently a loader only)
:: %%p = pluging, %%f = function, %%g = greedy load param(s), %%l = label, %%m = mark,
for %%p in (*plugin.bat) do (
   for /F "tokens=2*" %%f in ('findstr "call" "%%~p"') do (
      set "function=%%~f"
      set "greedyLoadParams=%%g"
   )

   for %%g in ("!greedyLoadParams:, =" "!") do (
      for /F "tokens=1,2" %%l in ("%%~g") do (
         set "_%%~l__%%~m=call "%%~p" "!function!""
      )
   )
)

:: run program
:: %%t = token, %%l = label, %%m = mark,
set "label="
for %%t in (%slots%) do (
   set "token=%%~t"
   if "!token:::=!" == "!token!" ( set "label=!token!"
   ) else ( for %%l in ("!label::=_!") do for %%m in ("!token::=_!") do !%%~l%%~m!
   )
)

goto :eof
plugin.bat

Code: Select all

@echo off
for /F "tokens=1*" %%l in (%*) do (goto %%~l %%m)
goto :eof

:function
echo This is the test function that is shown in the main batch file :)
pause
goto :eof

call :function mainlabel 2, settings 5
exit /b

Please note:
- functionalities are not encapsulated in this solution, and
- only one function per plugin supported.
- i'm not sure if it was intened that "setting 5" is not part of the parameterized plugin-operation-space

penpen

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Batch Simple Plugin System Consept

#9 Post by Adrianvdh » 06 Jan 2014 14:05

I meant to say I was going to write a plugin manager for a program I am going to make for learning experience in C, sorry :)

Sorry I am not good at explaining stuff.

Yes 'main.bat' will contain the Plugin Manager obviously.
New searching concept. In the folder 'plugins' there are batch files with any name e.g. "My plugin.bat". The batch file MUST have a comment so the 'main.bat' will know it is a plugin. A text like 'MY_BATCH_FILE_NAME_TEXT' and if it contains it it is considered/recognized as a plugin and will call the plugin batch file.

Code: Select all

:function


Is just the name of a function a user can write for the plugin.
The parameters are as follows:

Code: Select all

call :callfunction: function, mainlabel 2, settings 5

:function
echo It works.
exit /b

:callfunction
REM The call function is what every plugin has to have OR contained in the main batch file. It managers the parameters.
REM 'callfunction: function' it the name of the function that must be called, the "," is a separator to the new parameter/argument.
REM 'mainlabel' is the name of the existing label in 'main.bat'. Also the 'callfunction' must check if 'mainlabel' exists first and if not show an error, for debugging purposes.
REM Any way 'callfunction' will call the function 'function' at the label in the main batch file with the argument 2, which is optional. The number is for how many lines
REM down after the label.
REM Those '::Number' were just indicators for helping explaining. Sorry
REM 'settings' is another label in the main batch file, so it MUST support multi-labels if you can say that. So the function can be used over and over.
REM OR/AND
REM You can do this:
[code]
call :callfunction: function, mainlabel 2
call :callfunction: function, settings 5
[/code]

So for 'mainlabel' the function 'function' will be called from the plugin.

The prefix:
Let us add a argument to use white lines:


call :callfunction: 'The function name contained in the plugin batch file'. "," 'Comma to separate it to the next argument.' 'The label name in the main batch file' 'The line number after the label to be called' 'Should we allow white lines? Optionally we can put 'y' or 'n'

[code]
call :callfunction: function, mainlabel 5 y
[/code]

There for in:

[code]
:mainlabel




echo It works.
[/code]
It still works with the argument 'y'

It would execute like this in code view:

call :callfunction: function, mainlabel 5 n

[code]
:mainlabel
echo Line 1
echo Line 2
echo Line 3
echo Line 4
echo It works.


The labels in the main batch file can be for anything, such as a menu. Well the main batch file is someones batch file that they are wanting to implement the plugin system for.
I hope that can give you a good idea what it is suppose to do. To make sure please explain it to me the way you understand it :)

Another thing, how long did it take you to write the above, your code?
And where can I learn to code as well as you? Making those fancy functions and what not.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch Simple Plugin System Consept

#10 Post by Aacini » 06 Jan 2014 15:11

Adrianvdh wrote:Sorry I am not good at explaining stuff.

Yes 'main.bat' will contain the Plugin Manager obviously.
New searching concept. In the folder 'plugins' there are batch files with any name e.g. "My plugin.bat". The batch file MUST have a comment so the 'main.bat' will know it is a plugin. A text like 'MY_BATCH_FILE_NAME_TEXT' and if it contains it it is considered/recognized as a plugin and will call the plugin batch file.


A more basic question: what this system will be used for? For example, an image editor may use a plugin to load a new image type. This way, the functionality of the editor remains exactly the same than before; the plugin just allows to add a new routine in an internal table of image loading routines.

Could you state an example of a Batch program that may use the plugins system? I'm sorry, but if the answer is "no matter the example, this is just a mental exercise", then I am afraid than no matter the details on how the plugins system can be programmed.

I think that there is not a real Batch application that could use the plugins system as you state it. In other words, if a Batch program would require an extensible code that allows to add features to it, I think that would be a large number of different solutions with different advantages and drawbacks each (as is common in the particular case of Batch file programming).

Antonio

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch Simple Plugin System Consept

#11 Post by penpen » 06 Jan 2014 19:45

Most is still unclear to me i fear (sorry for that), but first what i have understood (i hope):
Adrianvdh wrote:New searching concept. In the folder 'plugins' there are batch files with any name e.g. "My plugin.bat".
That's easy, just replace "*plugin.bat" with "plugins\*.bat".

So do i understand it right that a sample plugin file in pseudocode should look like this "PluginSample.bat"?

Code: Select all

rem MY_BATCH_FILE_NAME_TEXT
:: PluginSample.bat

Either this {
   call :callfunction :labelXY, mainlabel 2, settings 5
} or that {
   call :callfunction: labelXY, mainlabel 2
   call :callfunction: labelXY, settings 5
}
   exit /B

:labelXY
   echo It works.
   exit /b

:callfunction
   let be %* := functionName (parameter argument)+ == functionName, parameter1 argument(s)1, parameter2 argument(s)2, ...
   for all (parameter argument(s)) in %* do {
      if (parameter exists in "..\main.bat") {
         call functionName parameter argument(s)
      } else if (debugging is enabled) {
         showError (parameter not in "..\main.bat": cannot call functionName parameter argument(s).)
      }
   }
   exit/b


Now: My problems in understanding how you want to realize this task:
Adrianvdh wrote:

Code: Select all

REM 'settings' is another label in the main batch file, so it MUST support multi-labels if you can say that. So the function can be used over and over.

Why do you outline this, this should be no special for a common batch file (and main.bat is such a file)?
The main.bat is allowed to support any numbers of labels you want, and you may call it as many times you want.
I have the strong feeling i'm missing something... .

Adrianvdh wrote:

Code: Select all

REM Any way 'callfunction' will call the function 'function' at the label in the main batch file with the argument 2, which is optional. The number is for how many lines
REM down after the label.
REM Those '::Number' were just indicators for helping explaining. Sorry

What do you mean, when you say: "The number is for how many lines down after the label."?


Adrianvdh wrote:So for 'mainlabel' the function 'function' will be called from the plugin.
How do you mean this? It sounds as if you want to do something like this (in pseudo code):

Code: Select all

rem main.bat content
   loadAllPlugins (); // result: unique map Plugin:={(key1=":mainlabel", value1="call pluginXY with optional parameters"), (key2, value2), ...}
:: ...

:mainlabel
   for (key, value) of Plugin with key == ":mainlabel" do execute value
:: ...

:settings
   for (key, value) of Plugin with key == ":settings" do execute value
:: ...
But it also could mean that you want to that (in pseudo code):

Code: Select all

rem plugin.bat content
:: ...
call :mainlabel of main.bat

If the first case is true, why is there any need of the plugin structure?
The :callfunction, ... is not needed in such a case.
You then only need something like that (in the first line for example):

Code: Select all

@rem MY_BATCH_FILE_NAME_TEXT: functionXY, mainlabel 2, setting 5 (, mainLabel options)*
This has the advantage, that you build a smaller call stack (also faster).

If the second case is true, then please carefully read my next Note.

Adrianvdh wrote:The labels in the main batch file can be for anything, such as a menu.
Well the main batch file is someones batch file that they are wanting to implement the plugin system for.
This is my main problem in understanding what you want to do. I'm sure i don't understand it right,
but this sounds, as if you want the plugins to access some functions of the main batch file.

If this is the case you should notice that plugins are designed to be used by a program,
not vice versa:
Example: A flash player (FP) within a brower (B):
1) B has a mapping of objects: MAP := {(fileType, application)| ...} == { ("html", B), ("htm", B), ("txt", B), ... }
2) B offers an interface for plugins: function draw (reference to Memory, reference to object data)
3) Installing plugin: FP adds ("flash", FP) to MAP (somehow: method is defined by B).

If B reads a webpage page with a "flash" object data (FO) in it, then B assigns some memory (M).
Then B searches the MAP for (fileType, application) with fileType == "flash": ("flash", FP) is found.
Finally B calls the function FP.draw (M, FO), then B copies the result to the webpage view.
FP never accesses any functionality from B.

If you want the plugins to use the main batch and not vice versa, then you use "main.bat" as a kind of a dll.
The good news in this case: Just use the "main.bat" as a plugin and the "*plugins.bat" as the main batch.
Will work only if not both sides should be able to call functions from the other:
In that case you should think of exporting these functions to another (dll)batch, to clearly divide the functionalities.
This avoids hidden complex endless loops, and some more ugly side effects like dead locks and so (although i'm not sure if these side effects could be realized within batch).


Finally:
Adrianvdh wrote:Another thing, how long did it take you to write the above, your code?
And where can I learn to code as well as you?
[/quote]I don't know, maybe round about 30 minutes (just guessed).
Just learning by doing, first reading the help pages and try to do some examples (without call);
then search for piping and redirecting and try to do some more examples;
finally read the help page of call and combine different commands.
If you've used them several times you should get a feeling in how to use them intuitively.

penpen

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Batch Simple Plugin System Consept

#12 Post by Adrianvdh » 07 Jan 2014 05:31

The reason such a system should exist in batch is for many reasons.
A few:

1. The main batch file could be under license and modification is denied, but allowing code to be inserted from the community for development is allowed.
2. If the author allows any sort of plugins, but denies a certain function to his/her batch file the community can implement it themselves.
3. The main batch file could be retired from development but is under a modification is denied license but code insurances is allowed.
4. I just want to see if it was possible to do it in batch, and trying to get a learning experience from it.

Use your imagination.


Next I have a new mission, to explain to penpen as best as I can :)

penpen wrote:Now: My problems in understanding how you want to realize this task:
Adrianvdh wrote:
Code:
REM 'settings' is another label in the main batch file, so it MUST support multi-labels if you can say that. So the function can be used over and over.

Why do you outline this, this should be no special for a common batch file (and main.bat is such a file)?
The main.bat is allowed to support any numbers of labels you want, and you may call it as many times you want.


Yes, I already knew that, just making sure, as you put that way, not my way it sound degrading, sorry.

Ok,

Code: Select all

:mainlabel
taskkill ie.exe
echo Hello. This is the second line
::3
::4
call :function within 'plugin.bat'
REM So line 5 is the line where the function within the plugin must be called, with this option well if the user leaves it out it will
REM be called from line after the label 'mainlabel', i.e "taskill i.e.exe"


The 'callfunction' should actually be in the 'main.bat' just for convenience. Because if it gets updated all the plugins will have to be updated.

i.e. (plugin.bat):

Code: Select all

call :callfunction(within 'main.bat') mainlabel 5(within 'main.bat), settings 5 y


penpen wrote:If this is the case you should notice that plugins are designed to be used by a program,
not vice versa:


Can't it be, what if my batch file has a 'getWinKey' function that contains Windows serial key, but the plugin wants to copy the serial key from a variable and echo it.

So therefore 'main.bat' and 'plugin.bat' are both plugins, but 'main.bat' has the manager and the 'callfunction', which the 'plugin.bat' has access to.
So yet again the functions between both batch file would be BiDi, sorry I put that in the wrong term but I think you know what I mean.
But if it is better to only go 1 direction from the 'plugin.bat' to 'main.bat' or vice versa, but 'plugin.bat' only has access to one function called 'callfunction' I guess that could be ok.
Last edited by Adrianvdh on 07 Jan 2014 12:14, edited 1 time in total.

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Batch Simple Plugin System Consept

#13 Post by Adrianvdh » 07 Jan 2014 12:06

I want to update the 'callfunction' function arguments:

Code: Select all

call :callfunction function, mainlabel 5 y, settings 5 y


I added the comma if you didn't see that :)
And the y/n argument should be added for both labels, but is optional:

e.g.:

Code: Select all

call :callfunction(within 'main.bat') :function, mainlabel 5 y(within 'main.bat), settings 5 y

OR

call :callfunction :function, mainlabel 5, settings 5

OR

call :callfunction :function, mainlabel 5, settings 5



Remember the y/n are for supporting white spaces after a label with in the main batch file. i.e. 'mainlabel'

y = supporting white spaces
n = not supporting white spaces

And if you programs line 5 is taken lets make it that it will override that line and move the first one down"

Code: Select all

:mainlabel
echo Line 1
::2
::3
::4
call function withing 'plugin.bat'
echo Line 5


As that is the latest update for the arguments
Last edited by Adrianvdh on 07 Jan 2014 12:29, edited 1 time in total.

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Batch Simple Plugin System Consept

#14 Post by Adrianvdh » 07 Jan 2014 12:26

My "SamplePlugin.bat" looks like this:

Code: Select all

::MY_PROGRAM_PLUGIN
set version=1.0

call :callcuntion :function, mainmenu 5 n

:function
echo It works^^!
pause
exit /b

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch Simple Plugin System Consept

#15 Post by penpen » 07 Jan 2014 13:50

Adrianvdh wrote:

Code: Select all

::MY_PROGRAM_PLUGIN
set version=1.0

call :callcuntion :function, mainmenu 5 n

:function
echo It works^^!
pause
exit /b

The problem with this is, that the batch file will be executed from the first line, so the
"call :callfunction ..." line will be executed and after that the function code, as there is no goto.

I think (or better i hope), i've now understood how you wanted to design this system.
The problem seems to be (IF i have really understood it), that you call ":callfunction" for both:
- registering the functions to the main system, and
- executing the function within the main system.
That was (beside others) a little bit irritating.
But although this is possible, i would prefer to just split functionality, so the plugin doesn't call
the main batch's internal functions; instead it is allowed to access a "batch-dll".

Adrianvdh wrote:Can't it be, what if my batch file has a 'getWinKey' function that contains Windows serial key, but the plugin wants to copy the serial key from a variable and echo it.
If the main.bat is not a batch file with self-modifying code, then there should be no (big) problem:
The main.bat then needs to offer a switch that directly calls the needed function, if allowed (security check may be implemented).

But in terms of programming this is not an example of good programming style or practices:
Technically there is a subgroup of *.exe that may export functions, as *.dll: Portable Executables.

But although this is possible, there are many reasons, why they should be avoided
(and it IS avoided in most systems: i've found no example where it was mixed up, although i'm far away from knowing all programs in the world):
- security issues
- easily produce dead locks,
- easily produce complex endless loops
- ...
Although i'm not sure if all, or if not all, which of these problems could be realized using batch, even if you want.
But if i understand it right, you also want to do this in ANSI C, too.


So you better divide the functionality:
- one or multiple "dll-batch" files to be called from other batches (main and plugins) to execute the "dll-batch"'s functions
- a main batch file that manages plugins, and may call functions from dll-batches
- plugin.bat that is called from main, and may call functions from dll-batches

This is an example of how it could be implemented:
Lets assume the following describe their function by name the following is given:
Sample.dll.bat [/list|/call functionName parameter(s)]
/list returns a list of all exported functions
/call executes a function, if it is an exported function,
an appropriate error message is displayed else.
functionName denotes a label that defines an exported function
parameter(s) one or more parameters.

If no parameter is used, then an appropriate error message is displayed.

SampleMain.bat
Plugins are allowed, under the terms of this plugin contract:
Batch files, that should be used as plugins:
- have to be be placed within the SampleMain.bat's subfolder named "plugins",
- must have "@rem " as the first 4 bytes followed by "MY_BATCH_FILE_NAME_TEXT " followed by the name of the function that should be replaced followed by <\r\n>.

plugins/SamplePlugin.bat:
As a plugin for SampleMain.bat, it meets the requirements of the plugin contract.


I've tried to use as many of your specifications, to implement an example of such a system.
But i've changed the definition of the :callfunction.
The advantage is, that it is much easier now, to define "batch-plugins".

So this is such an example system;
Sample.dll.bat

Code: Select all

:: processing parameters
@   if /I .%1 == ./list if .%2 == . goto :list
@   if /I .%1 == ./call goto :call
@   goto :help


:call
@   for /F %%e in ('call %~f0 /list') do @(
      if /I "%%~e" == "%2" goto %%~e
   )

@   echo No such exported function found: %0 %*.
@   exit /b 1

:SampleFunction
@   echo Successfully called: %0 %*
@   exit /b 0

:list
@   for %%e in (
      ":SampleFunction"
   ) do @(
      echo(%%~e
   )
@   exit /b 0

:help
@   for %%h in (
      "Sample.dll.bat [/list|/call functionName parameter(s)]"
      "  /list               returns a list of all exported functions"
      "  /call               executes a function, if it is an exported function,"
      "                      an appropriate error message is displayed else."
      "        functionName  denotes a label that defines an exported function"
      "        parameter(s)  one or more parameters."
      ""
   ) do @(
      echo(%%h
   )
@   exit /b 0


SampleMain.bat

Code: Select all

@echo off
setlocal enableDelayedExpansion
cls

:: initiate
   set "PluginFunctions.length=3"
   set "PluginFunctions[Pluginable1]=2"
   set "PluginFunctions[Pluginable2]=3"
   set "PluginFunctions[Pluginable3]=1"
   set "PluginFunctions[1]=call :Pluginable3"
   set "PluginFunctions[2]=call :Pluginable1"
   set "PluginFunctions[3]=call :Pluginable2"
   set "usePlugins=true"
   goto :processParams

:processParams
   if /I .%1 == ./noPlugins set "usePlugins=false"
   goto :pluginManager

:pluginManager
   if %usePlugins% == false goto :main
   for %%f in (plugins\*.bat) do (
      set "header="
      (set /P "header=" < %%f)
      for /F "tokens=1-3" %%r in ("!header!") do (
         if .%%r%%s == .@remMY_BATCH_FILE_NAME_TEXT (
            set "PluginFunction=%%~t"
            set "PluggedInFunction=%%~f"

            for /F %%p in ("!PluginFunction::=!") do (
               set "PluginFunctions[!PluginFunctions[%%~p]!]=call "!PluggedInFunction!""
            )
         )
      )
   )
   goto :main

:main
   echo call Sample.dll.bat from main
   call Sample.dll.bat /call :SampleFunction
   goto :mainlabel

:mainlabel
echo =================================================================
::2
::3
::4
call :callfunction :Pluginable1, mainlabel 5 n
echo =================================================================

:default
::1
call :callfunction :Pluginable2, default 2 n
echo =================================================================

:settings
::1
::2
call :callfunction :Pluginable3, settings 2 n
::4
::5
echo =================================================================
   exit /b 0


:callfunction
   set "params=%*"
   set "command=%1"
   for /F %%c in ("!PluginFunctions[%command::=%]!") do set "command=!PluginFunctions[%%~c]!"
   %command% %params:* =%
   exit /b %ErrorLevel%


:Pluginable1
   echo %0 :Pluginable1
   echo params: %*
   exit /b 0

:Pluginable2
   echo %0 :Pluginable2
   echo params: %*
   exit /b 0

:Pluginable3
   echo %0 :Pluginable3
   echo params: %*
   exit /b 0

plugins\SamplePlugin.bat

Code: Select all

@rem MY_BATCH_FILE_NAME_TEXT :Pluginable2

@echo(This is the test function that is shown in the main batch file :)
@echo(
@echo(call Sample.dll.bat /call :SampleFunction
@call Sample.dll.bat /call :SampleFunction
@echo(Notice: The working directory is that of the SampleMain.bat file.
@echo(
@echo(Finished
@exit /b

plugins\NoPlugin.bat

Code: Select all

@echo MY_BATCH_FILE_NAME_TEXT MY_BATCH_FILE_NAME_TEXT2 MY_BATCH_FILE_NAME_TEXT3

plugins\NoPlugin2.bat

I know it is not exact, what you wanted to do, but i think this is:
- easier to read
- easier to implement

penpen

Post Reply