Skip to content

Commit

Permalink
Fix generated .exe not working if the project name contains special c…
Browse files Browse the repository at this point in the history
…haracters (#5254)
  • Loading branch information
ClementPasteau committed May 3, 2023
1 parent 65a3358 commit 0c0fb5d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
58 changes: 45 additions & 13 deletions GDJS/GDJS/IDE/ExporterHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ static void InsertUnique(std::vector<gd::String> &container, gd::String str) {
container.push_back(str);
}

static gd::String CleanProjectName(gd::String projectName) {
gd::String partiallyCleanedProjectName = projectName;

static const gd::String forbiddenFileNameCharacters =
"\\/:*?\"<>|"; // See
// https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file

for (size_t i = 0; i < partiallyCleanedProjectName.size();) {
// Delete all characters that are not allowed in a filename
if (forbiddenFileNameCharacters.find(partiallyCleanedProjectName[i]) !=
gd::String::npos) {
partiallyCleanedProjectName.erase(i, 1);
} else {
i++;
}
}

if (partiallyCleanedProjectName.empty())
partiallyCleanedProjectName = "Project";

return partiallyCleanedProjectName;
}

ExporterHelper::ExporterHelper(gd::AbstractFileSystem &fileSystem,
gd::String gdjsRoot_,
gd::String codeOutputDir_)
Expand Down Expand Up @@ -101,8 +124,9 @@ bool ExporterHelper::ExportProjectForPixiPreview(
} else {
// Most of the time, we skip the logo and minimum duration so that
// the preview start as soon as possible.
exportedProject.GetLoadingScreen().ShowGDevelopLogoDuringLoadingScreen(false).SetMinDuration(
0);
exportedProject.GetLoadingScreen()
.ShowGDevelopLogoDuringLoadingScreen(false)
.SetMinDuration(0);
exportedProject.GetWatermark().ShowGDevelopWatermark(false);
}

Expand Down Expand Up @@ -308,16 +332,22 @@ bool ExporterHelper::ExportCordovaFiles(const gd::Project &project,
}

// Splashscreen icon for Android 12+.
gd::String splashScreenIconFilename = getIconFilename("android", "windowSplashScreenAnimatedIcon");
gd::String splashScreenIconFilename =
getIconFilename("android", "windowSplashScreenAnimatedIcon");
if (!splashScreenIconFilename.empty())
output += "<preference name=\"AndroidWindowSplashScreenAnimatedIcon\" value=\""
+ splashScreenIconFilename + "\" />\n";
output +=
"<preference name=\"AndroidWindowSplashScreenAnimatedIcon\" "
"value=\"" +
splashScreenIconFilename + "\" />\n";

// Splashscreen "branding" image for Android 12+.
gd::String splashScreenBrandingImageFilename = getIconFilename("android", "windowSplashScreenBrandingImage");
gd::String splashScreenBrandingImageFilename =
getIconFilename("android", "windowSplashScreenBrandingImage");
if (!splashScreenBrandingImageFilename.empty())
output += "<preference name=\"AndroidWindowSplashScreenBrandingImage\" value=\""
+ splashScreenBrandingImageFilename + "\" />\n";
output +=
"<preference name=\"AndroidWindowSplashScreenBrandingImage\" "
"value=\"" +
splashScreenBrandingImageFilename + "\" />\n";

return output;
};
Expand Down Expand Up @@ -458,11 +488,15 @@ bool ExporterHelper::ExportElectronFiles(const gd::Project &project,
->GetMangledSceneName(project.GetName())
.LowerCase()
.FindAndReplace(" ", "-")));
// It's important to clean the project name from special characters,
// otherwise Windows executable may be corrupted when electron builds it.
gd::String jsonCleanedName = gd::Serializer::ToJSON(
gd::SerializerElement(CleanProjectName(project.GetName())));

{
gd::String str =
fs.ReadFile(gdjsRoot + "/Runtime/Electron/package.json")
.FindAndReplace("\"GDJS_GAME_NAME\"", jsonName)
.FindAndReplace("\"GDJS_GAME_NAME\"", jsonCleanedName)
.FindAndReplace("\"GDJS_GAME_PACKAGE_NAME\"", jsonPackageName)
.FindAndReplace("\"GDJS_GAME_AUTHOR\"", jsonAuthor)
.FindAndReplace("\"GDJS_GAME_VERSION\"", jsonVersion)
Expand Down Expand Up @@ -651,10 +685,8 @@ void ExporterHelper::AddLibsInclude(bool pixiRenderers,
InsertUnique(includesFiles, "pixi-renderers/pixi-bitmapfont-manager.js");
InsertUnique(includesFiles,
"pixi-renderers/spriteruntimeobject-pixi-renderer.js");
InsertUnique(includesFiles,
"pixi-renderers/CustomObjectPixiRenderer.js");
InsertUnique(includesFiles,
"pixi-renderers/DebuggerPixiRenderer.js");
InsertUnique(includesFiles, "pixi-renderers/CustomObjectPixiRenderer.js");
InsertUnique(includesFiles, "pixi-renderers/DebuggerPixiRenderer.js");
InsertUnique(includesFiles,
"pixi-renderers/loadingscreen-pixi-renderer.js");
InsertUnique(includesFiles, "pixi-renderers/pixi-effects-manager.js");
Expand Down
4 changes: 2 additions & 2 deletions GDJS/GDJS/IDE/ExporterHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ struct PreviewExportOptions {
}

/**
* Set the token to use by the game engine when requiring any resource stored on
* GDevelop Cloud buckets. Note that this is only useful during previews.
* Set the token to use by the game engine when requiring any resource stored
* on GDevelop Cloud buckets. Note that this is only useful during previews.
*/
PreviewExportOptions &SetGDevelopResourceToken(
const gd::String &gdevelopResourceToken_) {
Expand Down
4 changes: 2 additions & 2 deletions newIDE/app/src/Export/ExportDialog/ExportLauncher.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export default class ExportLauncher extends Component<Props, State> {
t`A game with this ID already exists and you are not the owner.`
),
i18n._(
t`A test link will be created but the game will not be registered.`
t`A link or file will be created but the game will not be registered.`
),
].join('\n\n'),
rawError: registerError,
Expand All @@ -279,7 +279,7 @@ export default class ExportLauncher extends Component<Props, State> {
t`You have reached the maximum number of games you can register! You can unregister games in your Games Dashboard.`
),
i18n._(
t`A test link will be created but the game will not be registered.`
t`A link or file will be created but the game will not be registered.`
),
].join('\n\n'),
rawError: registerError,
Expand Down

0 comments on commit 0c0fb5d

Please sign in to comment.