|
2 | 2 | import os |
3 | 3 | import shutil |
4 | 4 | import json |
| 5 | +from pathlib import Path |
5 | 6 |
|
6 | 7 | PLUGIN_CONFIG = { |
7 | 8 | "name": "DynamicTranslation", |
|
16 | 17 |
|
17 | 18 |
|
18 | 19 | def get_paths(): |
19 | | - arg_dir = sys.argv[1] if len(sys.argv) > 1 else os.getcwd() |
20 | | - target_dir = os.path.abspath(arg_dir) |
21 | | - if not os.path.isdir(target_dir): |
22 | | - raise SystemExit(f"Invalid target directory: {target_dir}") |
23 | | - |
24 | | - # Check if is in RPG Maker project www directory |
25 | | - www_dir = os.path.join(target_dir, "www") |
26 | | - if not os.path.isdir(www_dir): |
27 | | - raise SystemExit(f"Invalid target directory: {target_dir}") |
| 20 | + # Use pathlib.Path for better unicode path handling |
| 21 | + if len(sys.argv) > 1: |
| 22 | + arg_dir = sys.argv[1] |
| 23 | + # Ensure proper unicode handling - pathlib handles this better than os.path |
| 24 | + target_path = Path(arg_dir).resolve() |
| 25 | + else: |
| 26 | + target_path = Path.cwd().resolve() |
| 27 | + |
| 28 | + if not target_path.is_dir(): |
| 29 | + raise SystemExit(f"Invalid target directory: {target_path}") |
| 30 | + |
| 31 | + # Check for RPG Maker project structure |
| 32 | + # Support both traditional (project/www/js/) and exported/flat (project/js/) structures |
| 33 | + www_path = target_path / "www" |
| 34 | + js_path_in_www = www_path / "js" |
| 35 | + js_path_direct = target_path / "js" |
| 36 | + plugins_path_in_www = js_path_in_www / "plugins" |
| 37 | + plugins_path_direct = js_path_direct / "plugins" |
| 38 | + |
| 39 | + if www_path.is_dir() and js_path_in_www.is_dir(): |
| 40 | + # Traditional structure: project/www/js/ |
| 41 | + www_dir = www_path |
| 42 | + js_path = js_path_in_www |
| 43 | + elif js_path_direct.is_dir(): |
| 44 | + # Exported/flat structure: project/js/ (no www subdirectory) |
| 45 | + # Even without www, if js/plugins exists, we can install |
| 46 | + # Treat the project root as the "www" equivalent |
| 47 | + www_dir = target_path |
| 48 | + js_path = js_path_direct |
| 49 | + if plugins_path_direct.is_dir(): |
| 50 | + print("Note: Detected exported/flat RPG Maker project structure (no www subdirectory)") |
| 51 | + else: |
| 52 | + print("Note: Detected exported/flat RPG Maker project structure (no www subdirectory)") |
| 53 | + print(f"Warning: plugins directory not found at {plugins_path_direct}") |
| 54 | + else: |
| 55 | + # Neither structure found |
| 56 | + raise SystemExit( |
| 57 | + f"Invalid target directory: {target_path}\n" |
| 58 | + "Expected RPG Maker project with either:\n" |
| 59 | + " - Traditional structure: project/www/js/\n" |
| 60 | + " - Exported structure: project/js/ (with or without plugins subdirectory)" |
| 61 | + ) |
28 | 62 |
|
29 | | - # Basic sanity: must contain js directory |
30 | | - js_dir = os.path.join(www_dir, "js") |
31 | | - if not os.path.isdir(js_dir): |
32 | | - print(f"Warning: {js_dir} not found; continuing but some steps may be skipped.") |
33 | | - script_dir = os.path.dirname(os.path.abspath(__file__)) |
34 | | - return target_dir, www_dir, script_dir |
| 63 | + script_path = Path(__file__).resolve().parent |
| 64 | + |
| 65 | + # Return as strings for backward compatibility with rest of code |
| 66 | + return str(target_path), str(www_dir), str(script_path) |
35 | 67 |
|
36 | 68 |
|
37 | 69 | def copy_plugin_file(script_dir, target_dir): |
38 | 70 | plugin_source = os.path.join(script_dir, "DynamicTranslation.js") |
39 | 71 | plugin_dest_dir = os.path.join(target_dir, "js", "plugins") |
40 | 72 | plugin_dest = os.path.join(plugin_dest_dir, "DynamicTranslation.js") |
41 | 73 |
|
| 74 | + # Create plugins directory if it doesn't exist |
42 | 75 | if not os.path.exists(plugin_dest_dir): |
43 | | - print(f"Error: plugins directory not found at {plugin_dest_dir}") |
44 | | - print( |
45 | | - "Make sure you are running this script in the root of an RPG Maker project." |
46 | | - ) |
47 | | - sys.exit(1) |
| 76 | + print(f"Creating plugins directory at {plugin_dest_dir}...") |
| 77 | + os.makedirs(plugin_dest_dir, exist_ok=True) |
48 | 78 |
|
49 | 79 | print("Copying DynamicTranslation.js...") |
50 | 80 | shutil.copy2(plugin_source, plugin_dest) |
@@ -88,8 +118,11 @@ def update_plugins_js(target_dir): |
88 | 118 | print("plugins.js updated successfully.") |
89 | 119 |
|
90 | 120 |
|
91 | | -def install_translation(target_dir): |
92 | | - translations_dir = os.path.join(target_dir, "www", "translations") |
| 121 | +def install_translation(target_dir, www_dir): |
| 122 | + # translations folder should be relative to where HTML file is located |
| 123 | + # For traditional structure: www/translations/ (HTML is in www/) |
| 124 | + # For exported structure: translations/ (HTML is in project root, www_dir == target_dir) |
| 125 | + translations_dir = os.path.join(www_dir, "translations") |
93 | 126 | print("Searching for translation files...") |
94 | 127 |
|
95 | 128 | if not os.path.exists(translations_dir): |
@@ -133,7 +166,7 @@ def main(): |
133 | 166 |
|
134 | 167 | copy_plugin_file(script_dir, www_dir) |
135 | 168 | update_plugins_js(www_dir) |
136 | | - install_translation(target_dir) |
| 169 | + install_translation(target_dir, www_dir) |
137 | 170 |
|
138 | 171 | print("Installation complete!") |
139 | 172 |
|
|
0 commit comments