Skip to content

Commit 9fa068a

Browse files
authored
Merge pull request #39 from RedMser/export-plugin-cleanup
Add Editor and Export plugin
2 parents 42ac013 + e7d419f commit 9fa068a

20 files changed

+232
-39
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Godot 4+ specific ignores
22
.godot/
3+
**/bin/**
4+
!**/bin/.gdignore
35

46
# Temporary directories
57
node_modules/

godot/default/bin/.gdignore

Whitespace-only changes.

godot/default/export_presets.cfg

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[preset.0]
2+
3+
name="Windows Desktop"
4+
platform="Windows Desktop"
5+
runnable=true
6+
advanced_options=false
7+
dedicated_server=false
8+
custom_features=""
9+
export_filter="all_resources"
10+
include_filter=""
11+
exclude_filter=""
12+
export_path="bin/fluent-translation.exe"
13+
encryption_include_filters=""
14+
encryption_exclude_filters=""
15+
encrypt_pck=false
16+
encrypt_directory=false
17+
script_export_mode=2
18+
19+
[preset.0.options]
20+
21+
custom_template/debug=""
22+
custom_template/release=""
23+
debug/export_console_wrapper=1
24+
binary_format/embed_pck=false
25+
texture_format/s3tc_bptc=true
26+
texture_format/etc2_astc=false
27+
binary_format/architecture="x86_64"
28+
codesign/enable=false
29+
codesign/timestamp=true
30+
codesign/timestamp_server_url=""
31+
codesign/digest_algorithm=1
32+
codesign/description=""
33+
codesign/custom_options=PackedStringArray()
34+
application/modify_resources=true
35+
application/icon=""
36+
application/console_wrapper_icon=""
37+
application/icon_interpolation=4
38+
application/file_version=""
39+
application/product_version=""
40+
application/company_name=""
41+
application/product_name=""
42+
application/file_description=""
43+
application/copyright=""
44+
application/trademarks=""
45+
application/export_angle=0
46+
application/export_d3d12=0
47+
application/d3d12_agility_sdk_multiarch=true
48+
ssh_remote_deploy/enabled=false
49+
ssh_remote_deploy/host="user@host_ip"
50+
ssh_remote_deploy/port="22"
51+
ssh_remote_deploy/extra_args_ssh=""
52+
ssh_remote_deploy/extra_args_scp=""
53+
ssh_remote_deploy/run_script="Expand-Archive -LiteralPath '{temp_dir}\\{archive_name}' -DestinationPath '{temp_dir}'
54+
$action = New-ScheduledTaskAction -Execute '{temp_dir}\\{exe_name}' -Argument '{cmd_args}'
55+
$trigger = New-ScheduledTaskTrigger -Once -At 00:00
56+
$settings = New-ScheduledTaskSettingsSet
57+
$task = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings
58+
Register-ScheduledTask godot_remote_debug -InputObject $task -Force:$true
59+
Start-ScheduledTask -TaskName godot_remote_debug
60+
while (Get-ScheduledTask -TaskName godot_remote_debug | ? State -eq running) { Start-Sleep -Milliseconds 100 }
61+
Unregister-ScheduledTask -TaskName godot_remote_debug -Confirm:$false -ErrorAction:SilentlyContinue"
62+
ssh_remote_deploy/cleanup_script="Stop-ScheduledTask -TaskName godot_remote_debug -ErrorAction:SilentlyContinue
63+
Unregister-ScheduledTask -TaskName godot_remote_debug -Confirm:$false -ErrorAction:SilentlyContinue
64+
Remove-Item -Recurse -Force '{temp_dir}'"
65+
fluent/strip_comments=true

godot/default/test.en.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
# this is a comment
2+
## another comment
13
-term = email
4+
### third kind of comment
25
HELLO =
36
{ $unreadEmails ->
47
[one] You have one unread { -term }.

rust/.cargo/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[env]
2-
GODOT4_BIN = { value = "../build/godot.windows.editor.x86_64.console.exe", relative = true }
2+
GODOT4_BIN = { value = "../build/godot.windows.editor.x86_64.exe", relative = true }

rust/Cargo.lock

Lines changed: 30 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ crate-type = ["cdylib"] # Compile this crate to a dynamic C library.
1818
constcat = "0.5.0"
1919
fluent = { git = "https://github.com/projectfluent/fluent-rs", branch = "main" }
2020
fluent-syntax = { git = "https://github.com/projectfluent/fluent-rs", branch = "main" }
21-
godot = { git = "https://github.com/godot-rust/gdext", branch = "master", features = ["experimental-threads"] }
21+
godot = { git = "https://github.com/godot-rust/gdext", branch = "master", features = ["register-docs", "experimental-threads"] }
2222
itertools = "0.13.0"
2323
unic-langid = "0.9.4"

rust/src/fluent/editor_plugin.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use godot::{classes::{EditorPlugin, IEditorPlugin}, prelude::*};
2+
3+
use super::FluentExportPlugin;
4+
5+
#[derive(GodotClass)]
6+
#[class(tool, editor_plugin, init, base=EditorPlugin)]
7+
pub struct FluentEditorPlugin {
8+
export_plugin: Option<Gd<FluentExportPlugin>>,
9+
base: Base<EditorPlugin>,
10+
}
11+
12+
#[godot_api]
13+
impl IEditorPlugin for FluentEditorPlugin {
14+
fn enter_tree(&mut self) {
15+
let export_plugin = FluentExportPlugin::new_gd();
16+
self.export_plugin = Some(export_plugin.clone());
17+
self.base_mut().add_export_plugin(export_plugin);
18+
}
19+
20+
fn exit_tree(&mut self) {
21+
let export_plugin = self.export_plugin.take();
22+
self.base_mut().remove_export_plugin(export_plugin);
23+
self.export_plugin = None;
24+
}
25+
}

rust/src/fluent/export_plugin.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use godot::{classes::{EditorExportPlatform, EditorExportPlugin, IEditorExportPlugin}, prelude::*};
2+
use constcat::concat as constcat;
3+
4+
use super::strip_comments;
5+
6+
const EXPORT_OPTION_PREFIX: &str = "fluent/";
7+
const EXPORT_OPTION_STRIP_COMMENTS: &str = constcat!(EXPORT_OPTION_PREFIX, "strip_comments");
8+
9+
#[derive(GodotClass)]
10+
#[class(base=EditorExportPlugin)]
11+
pub struct FluentExportPlugin {
12+
base: Base<EditorExportPlugin>,
13+
}
14+
15+
#[godot_api]
16+
impl IEditorExportPlugin for FluentExportPlugin {
17+
fn init(base: Base<EditorExportPlugin>) -> Self {
18+
Self {
19+
base,
20+
}
21+
}
22+
23+
fn get_export_options(&self, _platform: Gd<EditorExportPlatform>) -> Array<Dictionary> {
24+
array![dict! {
25+
"option": dict! {
26+
"name": GString::from(EXPORT_OPTION_STRIP_COMMENTS),
27+
"type": VariantType::BOOL,
28+
},
29+
"default_value": Variant::from(true),
30+
}]
31+
}
32+
33+
fn export_file(&mut self, path: GString, _type: GString, _features: PackedStringArray) {
34+
if !path.to_string().to_lowercase().ends_with("ftl") {
35+
return;
36+
}
37+
38+
if self.base().get_option(StringName::from(EXPORT_OPTION_STRIP_COMMENTS)).booleanize() {
39+
// Strip comments from file
40+
let contents = strip_comments(path.clone());
41+
let binary = PackedByteArray::from_iter(contents.bytes());
42+
43+
self.base_mut().skip();
44+
self.base_mut().add_file(path, binary, false);
45+
}
46+
}
47+
}

rust/src/fluent/extractor_packed_scene.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::collections::{HashMap, HashSet};
22

3-
use godot::engine::node::AutoTranslateMode;
4-
use godot::engine::{ClassDb, RegEx};
5-
use godot::engine::{resource_loader::CacheMode, ResourceLoader};
3+
use godot::classes::{ClassDb, RegEx, ResourceLoader};
4+
use godot::classes::node::AutoTranslateMode;
5+
use godot::classes::resource_loader::CacheMode;
66
use godot::prelude::*;
77

88
use super::{FluentTranslationParser, MessageGeneration};

rust/src/fluent/generator.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use godot::{engine::{utilities::error_string, ProjectSettings, RegEx, RegExMatch}, prelude::*};
1+
use godot::prelude::*;
2+
use godot::classes::{ProjectSettings, RegEx, RegExMatch};
3+
use godot::global::error_string;
24
use itertools::Itertools;
35
use std::{collections::HashMap, path::PathBuf};
46
use fluent_syntax::{ast, parser::parse};
57
use fluent_syntax::serializer::serialize;
68

79
use crate::utils::{create_or_open_file_for_read_write, get_files_recursive};
8-
use godot::engine::global::Error as GdErr;
10+
use godot::global::Error as GdErr;
911

1012
use super::{project_settings::{INVALID_MESSAGE_HANDLING_SKIP, PROJECT_SETTING_GENERATOR_INVALID_MESSAGE_HANDLING, PROJECT_SETTING_GENERATOR_LOCALES, PROJECT_SETTING_GENERATOR_PATTERNS}, FluentPackedSceneTranslationParser, FluentTranslationParser};
1113

rust/src/fluent/global.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use godot::{engine::ResourceLoader, prelude::*};
1+
use godot::prelude::*;
2+
use godot::classes::ResourceLoader;
23

34
use super::ResourceFormatLoaderFluent;
45

@@ -12,10 +13,10 @@ impl FluentI18nSingleton {
1213
pub(crate) const SINGLETON_NAME: &'static str = "FluentI18nSingleton";
1314

1415
pub(crate) fn register(&self) {
15-
ResourceLoader::singleton().add_resource_format_loader(self.loader.clone().upcast());
16+
ResourceLoader::singleton().add_resource_format_loader(self.loader.clone());
1617
}
1718

1819
pub(crate) fn unregister(&self) {
19-
ResourceLoader::singleton().remove_resource_format_loader(self.loader.clone().upcast());
20+
ResourceLoader::singleton().remove_resource_format_loader(self.loader.clone());
2021
}
2122
}

rust/src/fluent/importer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::path::PathBuf;
22

3-
use godot::{engine::{FileAccess, IResourceFormatLoader, ProjectSettings, RegEx, ResourceFormatLoader}, prelude::*};
4-
use godot::engine::global::Error as GdErr;
3+
use godot::prelude::*;
4+
use godot::classes::{FileAccess, IResourceFormatLoader, ProjectSettings, RegEx, ResourceFormatLoader};
5+
use godot::global::Error as GdErr;
56

67
use super::{locale::{compute_locale, compute_message_pattern}, project_settings::*, TranslationFluent};
78

rust/src/fluent/locale.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::{self, PathBuf};
22

3-
use godot::engine::{ProjectSettings, RegEx, RegExMatch};
3+
use godot::classes::{ProjectSettings, RegEx, RegExMatch};
44
use godot::prelude::*;
55
use unic_langid::LanguageIdentifier;
66

rust/src/fluent/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ mod importer;
1010
pub use self::importer::*;
1111
mod translation;
1212
pub use self::translation::*;
13+
mod export_plugin;
14+
pub use self::export_plugin::*;
15+
mod strip_comments;
16+
pub use self::strip_comments::*;
1317
pub mod locale;
1418
#[allow(dead_code)]
1519
pub mod project_settings;
20+
mod editor_plugin;

rust/src/fluent/project_settings.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use godot::engine::global::PropertyHint;
1+
use godot::global::PropertyHint;
22
use godot::prelude::*;
3-
use godot::engine::ProjectSettings;
3+
use godot::classes::ProjectSettings;
44
use constcat::concat as constcat;
55

66
const PROJECT_SETTING_PREFIX: &str = "internationalization/fluent/";

rust/src/fluent/strip_comments.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use godot::prelude::*;
2+
use fluent_syntax::{ast, parser::parse, serializer::serialize};
3+
use godot::classes::FileAccess;
4+
5+
pub fn strip_comments(path: GString) -> String {
6+
let contents = FileAccess::get_file_as_string(path.clone());
7+
let ftl = parse(contents.to_string());
8+
let mut ftl = match ftl {
9+
Ok(ftl) => ftl,
10+
Err((ftl, err)) => {
11+
godot_warn!("Error parsing {}: {:?}", path, err);
12+
ftl
13+
},
14+
};
15+
16+
ftl.body.retain(|ast| {
17+
match ast {
18+
ast::Entry::Comment(_) | ast::Entry::GroupComment(_) | ast::Entry::ResourceComment(_) => false,
19+
_ => true,
20+
}
21+
});
22+
23+
let contents = serialize(&ftl);
24+
contents
25+
}

0 commit comments

Comments
 (0)