File size: 4,438 Bytes
04dd2f1
 
9b15797
d00747b
04dd2f1
 
 
e0584fd
050de2f
e0584fd
a2217b9
4705a6e
d00747b
e0584fd
 
04dd2f1
a2217b9
 
d00747b
 
 
 
 
a2217b9
 
d00747b
 
 
 
 
 
 
 
 
 
 
 
a2217b9
 
4705a6e
e0584fd
4705a6e
a2217b9
 
 
0b272f5
a2217b9
 
c3b34cd
d00747b
e0584fd
 
d00747b
e0584fd
 
d00747b
e0584fd
a2217b9
c3b34cd
d00747b
e0584fd
 
 
c3b34cd
a2217b9
c3b34cd
40789e9
d00747b
4705a6e
050de2f
4705a6e
0b272f5
 
4705a6e
 
 
 
 
0b272f5
 
d00747b
 
 
 
 
 
 
 
 
 
0b272f5
e0584fd
 
d00747b
4705a6e
e0584fd
 
 
 
 
 
0b272f5
d00747b
4705a6e
e0584fd
 
 
d00747b
 
 
 
 
0b272f5
 
4705a6e
 
e0584fd
 
 
 
 
0b272f5
 
9b15797
e0584fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
import gradio as gr
from translator.agent import TranslationAgent
import re

agent = TranslationAgent()

title = "Unity → Unreal Code Translator"
description = "AI-assisted developer tool that automatically converts Unity C# scripts into Unreal Engine-ready C++ and Blueprint Python scripts. It's designed to help developers migrate gameplay logic, components, and class structures from Unity to Unreal with minimal manual rewriting. Powered by large language models Hugging Face Mistral and OpenAI"


def extract_class_name(csharp_code: str) -> str:
    """Extracts Unity class name for file naming."""
    match = re.search(r"class\s+(\w+)", csharp_code)
    return match.group(1) if match else "MyActor"


def split_cpp_code(full_cpp_code: str):
    """
    Split model output into .h and .cpp parts using file markers like:
    ---FILE: MyActor.h---
    ---FILE: MyActor.cpp---
    """
    if not full_cpp_code:
        return "", ""

    header_match = re.search(r"---FILE:\s*.*?\.h---([\s\S]*?)(?=---FILE:|$)", full_cpp_code)
    cpp_match = re.search(r"---FILE:\s*.*?\.cpp---([\s\S]*?)(?=---FILE:|$)", full_cpp_code)

    header_code = header_match.group(1).strip() if header_match else ""
    cpp_code = cpp_match.group(1).strip() if cpp_match else full_cpp_code.strip()

    # Cleanup any ``` fences if the model wrapped code blocks
    header_code = re.sub(r"^```[a-zA-Z]*|```$", "", header_code, flags=re.MULTILINE).strip()
    cpp_code = re.sub(r"^```[a-zA-Z]*|```$", "", cpp_code, flags=re.MULTILINE).strip()

    return header_code, cpp_code


def do_translate(csharp_code, target_format, model_choice):
    """Run translation and return all outputs."""
    class_name = extract_class_name(csharp_code)
    cpp_code, blueprint_code = agent.translate(
        csharp_code, target_format=target_format, model_choice=model_choice
    )

    # Split .h / .cpp parts if present
    header_code, cpp_code_only = split_cpp_code(cpp_code)

    # Save to temporary files for download
    h_filename = f"{class_name}.h"
    cpp_filename = f"{class_name}.cpp"

    with open(h_filename, "w", encoding="utf-8") as h:
        h.write(header_code or "// No header generated")

    with open(cpp_filename, "w", encoding="utf-8") as c:
        c.write(cpp_code_only or "// No cpp generated")

    # Split blueprint output into main and extra sections if available
    blueprint_parts = blueprint_code.split("###")
    main_script = blueprint_parts[0].strip()
    extra_info = blueprint_parts[1].strip() if len(blueprint_parts) > 1 else ""

    return header_code, cpp_code_only, main_script, extra_info, h_filename, cpp_filename


# Gradio UI
with gr.Blocks(title=title) as demo:
    gr.Markdown(f"### {title}")
    gr.Markdown(description)

    with gr.Row():
        csharp_input = gr.Textbox(
            lines=20,
            label="Unity C# Code",
            placeholder="Paste your Unity MonoBehaviour or script here..."
        )

    with gr.Row():
        target_format = gr.Radio(
            ["cpp", "blueprint", "both"],
            value="both",
            label="Target Format"
        )
        model_choice = gr.Radio(
            ["hf", "openai"],
            value="hf",
            label="Model Backend"
        )

    translate_btn = gr.Button("🔁 Translate")

    # C++ output tab
    with gr.Tab("C++ Output"):
        with gr.Row():
            header_box = gr.Code(label=".h (Header File)", language="cpp")
            cpp_box = gr.Code(label=".cpp (Implementation File)", language="cpp")
        with gr.Row():
            download_h = gr.File(label="Download .h")
            download_cpp = gr.File(label="Download .cpp")

    # Blueprint output tab
    with gr.Tab("Blueprint Output"):
        with gr.Row():
            blueprint_main = gr.Textbox(label="Blueprint Python Script", lines=15)
        with gr.Row():
            blueprint_notes = gr.Textbox(
                label="Blueprint Hints / Logs",
                lines=10,
                placeholder="Any additional output, logs, or notes will appear here."
            )

    translate_btn.click(
        do_translate,
        inputs=[csharp_input, target_format, model_choice],
        outputs=[
            header_box, cpp_box,
            blueprint_main, blueprint_notes,
            download_h, download_cpp
        ]
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860, share=True, debug=True)