通过 #Include 指令扩展事件函数

发布于 2024-10-05 19:29:37 字数 392 浏览 1 评论 0原文

我有一个基本的 inno-setup 脚本,用作多个安装程序的模板。作为基本脚本的一部分,我调用了事件函数 NextButtonClick

我现在想向 NextButtonClick 事件添加一些额外的代码,该代码仅由我的一个安装程序执行。有什么方法可以“扩展” NextButtonClick 事件吗?我正在考虑类似于 Python 的 super() 函数。

Inno-setup 使用 Pascal 作为脚本语言,因此也许 Pascal 专家可以提供一些见解。

I have a base inno-setup script that I use as a template for several installers. As part of the base script I have a call to the event function, NextButtonClick.

I would now like to add some additional code to the NextButtonClick event that will only be executed by one of my installers. Is there some way to "extend" the NextButtonClick event? I'm thinking of something along the lines of Python's super() function.

Inno-setup uses Pascal as a scripting language, so perhaps a Pascal expert can offer some insight.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶气 2024-10-12 19:29:38

不直接

记住#include指令只是一个预编译器指令,它使包含的文件出现在inno安装脚本编译器的指令所在的位置。

但是
为了避免在模板脚本中包含单独的安装程序代码,您可以创建一个约定来调用模板中的过程。

您必须遵循的唯一规则是每个安装程序都必须声明该过程,即使是空白。这样,您可以根据每个安装程序进行自定义,同时保持中性模板。

你的模板可能是这样的:

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  Result := BeforeNextButtonClick(CurPageID);
  //if the per-installer code decides not to allow the change, 
  //this code prevents further execution, but you may want it to run anyway.
  if not Result then
    Exit;  
  //your template logic here

  Result := Anything and More or Other;

  //same here!
  if not Result then
    Exit;

  //calling the per-installer code
  Result := AfternextButtonClck(CurPageID);
end;

那么各个安装程序可能看起来像这样:

function BeforeNextButtonClick(CurPageID: Integer): Boolean;
begin
  //specific logic here
  Result := OtherThing;
end

function AfterNextButtonClick(CurPageID: Integer): Boolean;
begin
  //and here, a blank implementation
  Result := True;
end;

#include MyCodeTemplate.iss

也许可以实现一种复杂的方法,我只是不记得 PascalScript 是否支持过程类型,并且没有时间检查 inno。

免责声明所有代码直接写在这里向您展示想法,它可能无法编译。

Not directly

Remember the #include directive is just a pre-compiler directive which makes the included file to appear in the place the directive is to the inno setup script compiler.

but
To avoid including individual installer code on the template script, you can create a convention to call a procedure in the template.

The only rule you have to follow is that every installer must declare the procedure, even blank. That way, you can customize as per-installer basis while maintaining a neutral template.

Your template may be something like:

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  Result := BeforeNextButtonClick(CurPageID);
  //if the per-installer code decides not to allow the change, 
  //this code prevents further execution, but you may want it to run anyway.
  if not Result then
    Exit;  
  //your template logic here

  Result := Anything and More or Other;

  //same here!
  if not Result then
    Exit;

  //calling the per-installer code
  Result := AfternextButtonClck(CurPageID);
end;

Then individual installers may look like this:

function BeforeNextButtonClick(CurPageID: Integer): Boolean;
begin
  //specific logic here
  Result := OtherThing;
end

function AfterNextButtonClick(CurPageID: Integer): Boolean;
begin
  //and here, a blank implementation
  Result := True;
end;

#include MyCodeTemplate.iss

Maybe it is possible to implement a complex approach, I just can't remember if PascalScript supports procedural types and no time to check with inno.

disclaimer all code written directly here to show you the idea, it may not compile.

向地狱狂奔 2024-10-12 19:29:38

我正在使用以下解决方法,这可能会使事情最终难以管理,但使用版本控制,我现在可以控制它:

在我的个人安装程序中,我有一系列 #define< /code> 指令。例如:

#define IncludeSomeFeature
#define IncludeSomeOtherOption

然后在我的基本模板中,我使用 #ifdef 指令选择性地在 Pascal 脚本事件中包含不同的代码片段:

function NextButtonClick(CurPageID: Integer): Boolean;
var
    ResultCode: Integer;
begin
  Result := True;
  if CurPageID = wpReady then begin
    #ifdef IncludeSomeFeature
        ... some code ...
    #endif
    #ifdef IncludeSomeOtherOption
        ... some more code ...
    #endif
    ... code to run for every installer ...
  end;
end;

这种方法的一个缺点是基本模板会慢慢地编写代码填写真正属于各个安装程序的代码。但是,由于这些是编译时指令,因此生成的安装可执行文件不应变得臃肿。

但实际上,我对这种方法最大的问题是它感觉不像 The Right Way™。

I'm using the following workaround, which may make things hard to manage eventually, but using version control I'm able to keep a handle on it for now:

In my individual installers, I have a series of #define directives. For example:

#define IncludeSomeFeature
#define IncludeSomeOtherOption

Then in my base template, I use the #ifdef directives to optionally include different pieces of code within the Pascal scripting events:

function NextButtonClick(CurPageID: Integer): Boolean;
var
    ResultCode: Integer;
begin
  Result := True;
  if CurPageID = wpReady then begin
    #ifdef IncludeSomeFeature
        ... some code ...
    #endif
    #ifdef IncludeSomeOtherOption
        ... some more code ...
    #endif
    ... code to run for every installer ...
  end;
end;

The one downside to this approach is that code that the base template will slowly fill up with code that really belongs with the individual installer. However, since these are compile time directives, the resulting setup executables should not get bloated.

Really, though, my biggest problem with this approach is that it just doesn't feel like The Right Way™.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文