multimodalart HF Staff commited on
Commit
c801583
·
verified ·
1 Parent(s): 23d3a95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -25
app.py CHANGED
@@ -30,54 +30,41 @@ def make_html_file(generated_level):
30
  html_filename = f"demo-{unique_id}.html"
31
  html_content = f'''<!DOCTYPE html>
32
  <html lang="en">
33
-
34
  <head>
35
  <meta charset="utf-8">
36
  <title>Mario Game</title>
37
  <script src="https://cjrtnc.leaningtech.com/4.2/loader.js"></script>
38
  </head>
39
-
40
  <body>
41
- <p>Loading game, please wait...</p>
42
  </body>
43
  <script>
44
  async function runGame() {{
 
45
  try {{
46
- // Step 1: Pre-load the JAR to avoid server range-request issues.
47
  const response = await fetch("/gradio_api/file=static/mario.jar");
48
- if (!response.ok) {{
49
- throw new Error(`HTTP error loading JAR: ${{response.status}}`);
50
- }}
51
  const jarData = await response.arrayBuffer();
52
 
53
- // Step 2: Initialize CheerpJ.
54
  await cheerpjInit();
55
 
56
- // Step 3: Add files to the temporary /str/ mount point.
57
  cheerpjAddStringFile("/str/mylevel.txt", `{level_text}`);
 
58
  cheerpjAddStringFile("/str/mario.jar", new Uint8Array(jarData));
59
 
60
- // Step 4: Copy the level file from /str/ to the /files/ working directory.
61
- // This is the critical step to solve the "infinite load".
62
- const lib = await cheerpjRunLibrary('/str/mario.jar');
63
- const Files = await lib.java.nio.file.Files;
64
- const Paths = await lib.java.nio.file.Paths;
65
- const StandardCopyOption = await lib.java.nio.file.StandardCopyOption;
66
-
67
- const sourcePath = await Paths.get("/str/mylevel.txt", []);
68
- const destPath = await Paths.get("/files/mylevel.txt", []);
69
-
70
- await Files.copy(sourcePath, destPath, [StandardCopyOption.REPLACE_EXISTING]);
71
- console.log("Successfully copied mylevel.txt to /files/ working directory.");
72
 
73
- // Step 5: Run the game now that its files are in place.
74
- document.body.innerHTML = ''; // Clear the "Loading..." message
75
  cheerpjCreateDisplay(512, 500);
76
- cheerpjRunJar("/str/mario.jar", "/files/mylevel.txt");
77
 
78
  }} catch (error) {{
79
  console.error("Failed to load the Mario game:", error);
80
- document.body.innerHTML = "<h1>Error</h1><p>Could not load the game. Please check the browser console for details.</p>";
81
  }}
82
  }}
83
 
 
30
  html_filename = f"demo-{unique_id}.html"
31
  html_content = f'''<!DOCTYPE html>
32
  <html lang="en">
 
33
  <head>
34
  <meta charset="utf-8">
35
  <title>Mario Game</title>
36
  <script src="https://cjrtnc.leaningtech.com/4.2/loader.js"></script>
37
  </head>
 
38
  <body>
39
+ <p id="loading-status">Loading game, please wait...</p>
40
  </body>
41
  <script>
42
  async function runGame() {{
43
+ const statusElement = document.getElementById("loading-status");
44
  try {{
45
+ statusElement.textContent = "Downloading game assets...";
46
  const response = await fetch("/gradio_api/file=static/mario.jar");
47
+ if (!response.ok) throw new Error(`HTTP error loading JAR: ${{response.status}}`);
 
 
48
  const jarData = await response.arrayBuffer();
49
 
50
+ statusElement.textContent = "Initializing Java runtime...";
51
  await cheerpjInit();
52
 
53
+ statusElement.textContent = "Setting up virtual filesystem...";
54
  cheerpjAddStringFile("/str/mylevel.txt", `{level_text}`);
55
+
56
  cheerpjAddStringFile("/str/mario.jar", new Uint8Array(jarData));
57
 
58
+ console.log("Virtual filesystem is ready.");
 
 
 
 
 
 
 
 
 
 
 
59
 
60
+ statusElement.textContent = "Starting game...";
61
+ document.body.innerHTML = '';
62
  cheerpjCreateDisplay(512, 500);
63
+ cheerpjRunJar("/str/mario.jar");
64
 
65
  }} catch (error) {{
66
  console.error("Failed to load the Mario game:", error);
67
+ statusElement.innerHTML = "<h1>Error</h1><p>Could not load the game. Please check the browser console for details.</p>";
68
  }}
69
  }}
70