skywind3000 / PyStand

:rocket: Python Standalone Deploy Environment !!
MIT License
641 stars 75 forks source link

能否增加一个编译选项, 开启后加载指定文件名的int文件 #36

Closed zhang-weiyu closed 1 year ago

zhang-weiyu commented 1 year ago

现在exe启动时会加载同名的int文件, 如果修改exe文件的文件名的话也要同时修改int文件的文件名, 能否增加一个选项, 开启后固定加载指定文件名的int文件, 那样的话int文件名只需最初设置一次, 之后修改exe文件名就无需修改int文件的文件名了

myd7349 commented 1 year ago

这样做的话,int 文件改名了该怎么办?

如果真要避免文件名不一致带来的问题。可考虑如下思路:

自己修改 PyStand.cpp 的代码,将 int 文件的内容直接嵌入到 PyStand.cpp 中。

为了便于嵌入,可考虑将你的 int 文件尽量简化,然后缩成一行。或者,base64 编码一下,变成一行(exec 前先 decode 一下)。

zhang-weiyu commented 1 year ago

@myd7349 感谢您的指点, 因为我之前想的是用enigma virtual box把int嵌进exe里, 这样int文件就不存在改名的问题, 只有exe会改名, 您说的这个方式我再琢磨琢磨看看

LeeGaning commented 1 year ago

修改下函数,加载脚本先搜索固定脚本,没有再找其他的

int PyStand::DetectScript()
{
    // init: _script (init script like PyStand.int or PyStand.py)
    std::wstring static_script = L"py.init";
    if (PathFileExistsW(static_script.c_str())) {
        _script = static_script;
    } else {
        int size = (int)_pystand.size() - 1;
        for (; size >= 0; size--) {
            if (_pystand[size] == L'.') break;
        }
        if (size < 0) size = (int)_pystand.size();
        std::wstring main = _pystand.substr(0, size);
        std::vector<const wchar_t*> exts;
        std::vector<std::wstring> scripts;
        exts.push_back(L".int");
        exts.push_back(L".py");
        exts.push_back(L".pyw");
        _script = L"";
        for (int i = 0; i < (int)exts.size(); i++) {
            std::wstring test = main + exts[i];
            scripts.push_back(test);
            if (PathFileExistsW(test.c_str())) {
                _script = test;
                break;
            }
        }
        if (_script.size() == 0) {
            std::wstring msg = L"Can't find either of:\r\n";
            for (int j = 0; j < (int)scripts.size(); j++) {
                msg += scripts[j] + L"\r\n";
            }
            MessageBoxW(NULL, msg.c_str(), L"ERROR", MB_OK);
            return -1;
        }
    }
    SetEnvironmentVariableW(L"PYSTAND_SCRIPT", _script.c_str());
    return 0;
}
zhang-weiyu commented 1 year ago

@LeeGaning 感谢大佬:)

skywind3000 commented 1 year ago

上面那段代码没有考虑当前路径不是 PyStand 目录的问题,我已经在正式版里加了这个功能了, 如果存在 _pystand_static.int 则会优先启动:

https://github.com/skywind3000/PyStand/releases/tag/1.1.0