Spaces:
Build error
Build error
Commit ·
a2217b9
1
Parent(s): e0584fd
ui update
Browse files
app.py
CHANGED
|
@@ -7,28 +7,39 @@ agent = TranslationAgent()
|
|
| 7 |
title = "Unity → Unreal Code Translator"
|
| 8 |
description = "Paste a Unity C# script and get Unreal C++ (.h/.cpp) + Unreal Blueprint Python generator."
|
| 9 |
|
|
|
|
| 10 |
def extract_class_name(csharp_code: str) -> str:
|
| 11 |
import re
|
| 12 |
match = re.search(r"class\s+(\w+)", csharp_code)
|
| 13 |
return match.group(1) if match else "MyActor"
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def do_translate(csharp_code, target_format, model_choice):
|
| 16 |
"""Run translation and return all outputs."""
|
| 17 |
class_name = extract_class_name(csharp_code)
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
if len(outputs) == 3:
|
| 23 |
-
header_code, cpp_code, blueprint_code = outputs
|
| 24 |
-
else:
|
| 25 |
-
header_code, cpp_code, blueprint_code = outputs[0], "", ""
|
| 26 |
-
elif isinstance(outputs, dict):
|
| 27 |
-
header_code = outputs.get("header_code", "")
|
| 28 |
-
cpp_code = outputs.get("cpp_code", "")
|
| 29 |
-
blueprint_code = outputs.get("blueprint_code", "")
|
| 30 |
-
else:
|
| 31 |
-
header_code = cpp_code = blueprint_code = ""
|
| 32 |
|
| 33 |
# Save files
|
| 34 |
h_filename = f"{class_name}.h"
|
|
@@ -36,14 +47,14 @@ def do_translate(csharp_code, target_format, model_choice):
|
|
| 36 |
with open(h_filename, "w", encoding="utf-8") as h:
|
| 37 |
h.write(header_code or "// No header generated")
|
| 38 |
with open(cpp_filename, "w", encoding="utf-8") as c:
|
| 39 |
-
c.write(
|
| 40 |
|
| 41 |
-
# Split blueprint output
|
| 42 |
blueprint_parts = blueprint_code.split("###")
|
| 43 |
main_script = blueprint_parts[0].strip()
|
| 44 |
extra_info = blueprint_parts[1].strip() if len(blueprint_parts) > 1 else ""
|
| 45 |
|
| 46 |
-
return header_code,
|
| 47 |
|
| 48 |
|
| 49 |
with gr.Blocks(title=title) as demo:
|
|
@@ -76,7 +87,8 @@ with gr.Blocks(title=title) as demo:
|
|
| 76 |
with gr.Row():
|
| 77 |
blueprint_main = gr.Textbox(label="Blueprint Python Script", lines=15)
|
| 78 |
with gr.Row():
|
| 79 |
-
blueprint_notes = gr.Textbox(label="Blueprint Hints / Logs", lines=10,
|
|
|
|
| 80 |
|
| 81 |
translate_btn.click(
|
| 82 |
do_translate,
|
|
|
|
| 7 |
title = "Unity → Unreal Code Translator"
|
| 8 |
description = "Paste a Unity C# script and get Unreal C++ (.h/.cpp) + Unreal Blueprint Python generator."
|
| 9 |
|
| 10 |
+
|
| 11 |
def extract_class_name(csharp_code: str) -> str:
|
| 12 |
import re
|
| 13 |
match = re.search(r"class\s+(\w+)", csharp_code)
|
| 14 |
return match.group(1) if match else "MyActor"
|
| 15 |
|
| 16 |
+
|
| 17 |
+
def split_cpp_code(full_cpp_code: str):
|
| 18 |
+
"""Split combined C++ output into .h and .cpp sections."""
|
| 19 |
+
if not full_cpp_code:
|
| 20 |
+
return "", ""
|
| 21 |
+
lines = full_cpp_code.splitlines()
|
| 22 |
+
header_lines, cpp_lines = [], []
|
| 23 |
+
in_header = True
|
| 24 |
+
for line in lines:
|
| 25 |
+
if line.strip().startswith("#include") or line.strip().endswith(".cpp"):
|
| 26 |
+
in_header = False
|
| 27 |
+
if in_header:
|
| 28 |
+
header_lines.append(line)
|
| 29 |
+
else:
|
| 30 |
+
cpp_lines.append(line)
|
| 31 |
+
return "\n".join(header_lines).strip(), "\n".join(cpp_lines).strip()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
def do_translate(csharp_code, target_format, model_choice):
|
| 35 |
"""Run translation and return all outputs."""
|
| 36 |
class_name = extract_class_name(csharp_code)
|
| 37 |
+
cpp_code, blueprint_code = agent.translate(
|
| 38 |
+
csharp_code, target_format=target_format, model_choice=model_choice
|
| 39 |
+
)
|
| 40 |
|
| 41 |
+
# Split .h / .cpp parts if present
|
| 42 |
+
header_code, cpp_code_only = split_cpp_code(cpp_code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Save files
|
| 45 |
h_filename = f"{class_name}.h"
|
|
|
|
| 47 |
with open(h_filename, "w", encoding="utf-8") as h:
|
| 48 |
h.write(header_code or "// No header generated")
|
| 49 |
with open(cpp_filename, "w", encoding="utf-8") as c:
|
| 50 |
+
c.write(cpp_code_only or "// No cpp generated")
|
| 51 |
|
| 52 |
+
# Split blueprint output into main and logs if available
|
| 53 |
blueprint_parts = blueprint_code.split("###")
|
| 54 |
main_script = blueprint_parts[0].strip()
|
| 55 |
extra_info = blueprint_parts[1].strip() if len(blueprint_parts) > 1 else ""
|
| 56 |
|
| 57 |
+
return header_code, cpp_code_only, main_script, extra_info, h_filename, cpp_filename
|
| 58 |
|
| 59 |
|
| 60 |
with gr.Blocks(title=title) as demo:
|
|
|
|
| 87 |
with gr.Row():
|
| 88 |
blueprint_main = gr.Textbox(label="Blueprint Python Script", lines=15)
|
| 89 |
with gr.Row():
|
| 90 |
+
blueprint_notes = gr.Textbox(label="Blueprint Hints / Logs", lines=10,
|
| 91 |
+
placeholder="Any additional output, logs, or notes will appear here.")
|
| 92 |
|
| 93 |
translate_btn.click(
|
| 94 |
do_translate,
|