File size: 4,731 Bytes
0330748
 
 
4385610
bfca1dd
8a8a1f3
0330748
79028b9
0330748
bfca1dd
79028b9
0330748
bfca1dd
 
0330748
79028b9
9541140
3a2ee82
 
 
bfca1dd
 
 
79028b9
bfca1dd
 
 
 
 
 
 
3a2ee82
79028b9
3a2ee82
bfca1dd
0330748
5c77f11
0330748
bfca1dd
 
0330748
bfca1dd
 
 
8256401
79028b9
8a8a1f3
bfca1dd
8a8a1f3
79028b9
 
 
 
 
 
 
c9c40c7
 
 
 
 
 
 
 
 
79028b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aea8061
0330748
 
79028b9
0330748
79028b9
bfca1dd
0330748
8a8a1f3
 
8371bce
bfca1dd
448cd0c
 
bfca1dd
c3f0e83
79028b9
 
 
 
c9c40c7
 
 
 
 
79028b9
 
 
bfca1dd
0330748
bfca1dd
 
 
0330748
4206a9f
7865f2f
4385610
8256401
8a8a1f3
8371bce
5c77f11
0d53631
c3b21a5
c3f0e83
3cd343a
ba70e1b
b9ca255
68748a7
8bb4b3f
b1712e3
cff5e12
9541140
c9c40c7
79028b9
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import gradio as gr
import os
import zipfile
from pathlib import Path
import shutil
import urllib.parse

# Define constants
ZIP_FILE = "nes.zip"
EXTRACTED_FOLDER = "extracted_nes"
PUBLIC_FOLDER = "public/roms"

# Ensure public ROM folder exists
os.makedirs(PUBLIC_FOLDER, exist_ok=True)

# Function to extract ROMs from the zip
def extract_roms():
    try:
        if os.path.exists(ZIP_FILE):
            with zipfile.ZipFile(ZIP_FILE, 'r') as zip_ref:
                zip_ref.extractall(EXTRACTED_FOLDER)
                print(f"Extracted into {EXTRACTED_FOLDER}")

            # Move ROMs into the public folder
            for root, dirs, files in os.walk(EXTRACTED_FOLDER):
                for file in files:
                    if file.lower().endswith(".nes"):
                        source_path = os.path.join(root, file)
                        target_path = os.path.join(PUBLIC_FOLDER, file)
                        shutil.move(source_path, target_path)
                        print(f"Moved ROM: {file} -> {target_path}")
        else:
            raise FileNotFoundError("`nes.zip` not found.")
    except PermissionError as e:
        print(f"Permission error: {e}")

# Function to list available ROMs
def list_roms():
    if not os.path.exists(PUBLIC_FOLDER):
        print("No ROMs found.")
        return []
    roms = [f for f in os.listdir(PUBLIC_FOLDER) if f.endswith(".nes")]
    print("Available ROMs:", roms)
    return roms

# Function to generate the HTML content for the selected ROM
def generate_rom_page(rom_name):
    rom_url = f"/roms/{urllib.parse.quote(rom_name)}"
    html_content = f"""
    <div style='width:640px;height:480px;max-width:100%'>
        <div id='game'></div>
    </div>
    <script>
        if (typeof EJS_player === 'undefined') {{
            var script = document.createElement('script');
            script.src = "https://cdn.emulatorjs.org/stable/data/loader.js";
            script.onload = function() {{
                window.addEventListener('load', function() {{
                    EJS_player = "#game";
                    EJS_core = "nes";
                    EJS_color = "#0064ff";
                    EJS_pathtodata = "https://cdn.emulatorjs.org/stable/data/";
                    EJS_gameUrl = "{rom_url}";
                }});
            }};
            document.body.appendChild(script);
        }}
    </script>
    <style>
        #game {{
            width: 100%;
            height: 100%;
            background-color: #000;
            position: relative;
        }}
        .fullscreen-btn {{
            position: absolute;
            top: 10px;
            right: 10px;
            background: rgba(0, 100, 255, 0.7);
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            color: white;
        }}
    </style>
    <button class="fullscreen-btn" onclick="toggleFullScreen()">Full Screen</button>
    <button class="fullscreen-btn" onclick="reloadGame()">Reload</button>
    <script>
        function toggleFullScreen() {{
            const gameContainer = document.getElementById('game');
            if (gameContainer.requestFullscreen) {{
                gameContainer.requestFullscreen();
            }} else if (gameContainer.mozRequestFullScreen) {{
                gameContainer.mozRequestFullScreen();
            }} else if (gameContainer.webkitRequestFullscreen) {{
                gameContainer.webkitRequestFullscreen();
            }} else if (gameContainer.msRequestFullscreen) {{
                gameContainer.msRequestFullscreen();
            }}
        }}

        function reloadGame() {{
            const iframe = document.querySelector('iframe');
            if (iframe) {{
                iframe.src = iframe.src;
            }}
        }}
    </script>
    """
    return html_content

# Main Gradio app function
def gradio_app():
    with gr.Blocks() as app:
        gr.Markdown("# 🎮 Play NES ROMs in Browser!")

        rom_files = list_roms()

        with gr.Column():
            rom_dropdown = gr.Dropdown(choices=rom_files, label="Select a ROM", interactive=True)
            output_html = gr.HTML(value="Select a ROM to start playing!", elem_id="emulator")

            rom_dropdown.change(fn=generate_rom_page, inputs=rom_dropdown, outputs=output_html)

        app.css = """
        .gradio-dropdown { 
            max-height: 400px; 
            overflow-y: auto; 
            margin-bottom: 20px;
        }
        .gradio-button { 
            margin-top: 10px; 
            margin-bottom: 10px;
        }
        """
    
    app.launch(server_name="0.0.0.0", server_port=7860)

if __name__ == "__main__":
    extract_roms()
    gradio_app()