Skip to content

Commit

Permalink
fix: 当用户名有特殊字符时, 使用别的临时目录解压依赖资源
Browse files Browse the repository at this point in the history
  • Loading branch information
shabbywu committed Jul 11, 2024
1 parent 330cd93 commit 1f09f06
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
15 changes: 14 additions & 1 deletion src/bb-translator.main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,19 @@ int gui_main()
std::filesystem::path userConfigFolder =
HelloImGui::IniFolderLocation(HelloImGui::IniFolderType::AppUserConfigFolder);

if (userConfigFolder.string() != acp_to_utf8(userConfigFolder.string()))
{
userConfigFolder = std::filesystem::current_path() / ".temp";
std::filesystem::create_directories(userConfigFolder);

std::ofstream f(userConfigFolder / "README.txt", std::ios::out | std::ios::binary | std::ios::trunc);

f << "该目录存放的是临时文件, 仅当你的用户名有特殊字符时会创建该目录, 该目录可随意删除\r\n";
f << utf8_to_acp(
std::string("该目录存放的是临时文件, 仅当你的用户名有特殊字符时会创建该目录, 该目录可随意删除\r\n"));
f.close();
}

auto appFolder = userConfigFolder / "bb-translator";
state.assetsDir = appFolder / "assets";
state.pythonRootDir = appFolder / "binary";
Expand Down Expand Up @@ -373,7 +386,7 @@ int gui_main()
}
git_init();
}

HelloImGui::SetAssetsFolder(acp_to_utf8(state.assetsDir.string()));
setup_python(&state);
};
Expand Down
18 changes: 13 additions & 5 deletions src/bb-translator/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <zip.h>

#include "binding.h"
#include "../utf8.h"
#include <bundle/base_library.h>
#include <bundle/bb_translator.h>
#include <bundle/purepython.h>
Expand Down Expand Up @@ -107,11 +108,18 @@ void setup_python(AppState *state)
{
auto &pythonZip = bin2cpp::getPythonZipFile();
auto path = state->pythonRootDir.string();
auto c_path = (const char *)path.c_str();
zip_stream_extract(pythonZip.getBuffer(), pythonZip.getSize(), c_path, nullptr, nullptr);
std::ofstream f(state->pythonRootDir / ".binary.unzip.v1",
std::ios::out | std::ios::binary | std::ios::trunc);
f.close();
if (auto error_code =
zip_stream_extract(pythonZip.getBuffer(), pythonZip.getSize(), path.data(), nullptr, nullptr);
error_code < 0)
{
std::cout << "failed to extract binary: " << zip_strerror(error_code) << std::endl;
}
else
{
std::ofstream f(state->pythonRootDir / ".binary.unzip.v1",
std::ios::out | std::ios::binary | std::ios::trunc);
f.close();
}
}

#ifdef _WIN32
Expand Down

0 comments on commit 1f09f06

Please sign in to comment.