Spaces:
Running
Running
2410191053
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .dockerignore +7 -0
- .gitignore +1 -0
- Dockerfile +13 -0
- README.md +2 -2
- app.py +119 -0
- public/data.js +0 -0
- public/data/1.png +0 -0
- public/data/10.png +0 -0
- public/data/100.png +0 -0
- public/data/101.png +0 -0
- public/data/102.png +0 -0
- public/data/103.png +0 -0
- public/data/104.png +0 -0
- public/data/105.png +0 -0
- public/data/106.png +0 -0
- public/data/107.png +0 -0
- public/data/108.png +0 -0
- public/data/109.png +0 -0
- public/data/11.png +0 -0
- public/data/110.png +0 -0
- public/data/111.png +0 -0
- public/data/112.png +0 -0
- public/data/113.png +0 -0
- public/data/114.png +0 -0
- public/data/115.png +0 -0
- public/data/116.png +0 -0
- public/data/117.png +0 -0
- public/data/118.png +0 -0
- public/data/119.png +0 -0
- public/data/12.png +0 -0
- public/data/120.png +0 -0
- public/data/121.png +0 -0
- public/data/122.png +0 -0
- public/data/123.png +0 -0
- public/data/124.png +0 -0
- public/data/125.png +0 -0
- public/data/126.png +0 -0
- public/data/127.png +0 -0
- public/data/128.png +0 -0
- public/data/129.png +0 -0
- public/data/13.png +0 -0
- public/data/130.png +0 -0
- public/data/131.png +0 -0
- public/data/132.png +0 -0
- public/data/133.png +0 -0
- public/data/134.png +0 -0
- public/data/135.png +0 -0
- public/data/136.png +0 -0
- public/data/137.png +0 -0
- public/data/138.png +0 -0
.dockerignore
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*
|
2 |
+
|
3 |
+
!/public/
|
4 |
+
|
5 |
+
!/requirements.txt
|
6 |
+
!/mmp4.zip
|
7 |
+
!/app.py
|
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
*.zip
|
Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10
|
2 |
+
|
3 |
+
RUN useradd -m -u 1000 user
|
4 |
+
USER user
|
5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
6 |
+
|
7 |
+
WORKDIR /app
|
8 |
+
|
9 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
10 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
11 |
+
|
12 |
+
COPY --chown=user . /app
|
13 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
title: Minecraft MOD Maker 4
|
3 |
-
emoji:
|
4 |
colorFrom: green
|
5 |
-
colorTo:
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
---
|
|
|
1 |
---
|
2 |
title: Minecraft MOD Maker 4
|
3 |
+
emoji: ⛏
|
4 |
colorFrom: green
|
5 |
+
colorTo: gray
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
---
|
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi.middleware.cors import CORSMiddleware
|
2 |
+
from fastapi.staticfiles import StaticFiles
|
3 |
+
from fastapi import APIRouter, FastAPI
|
4 |
+
import base64, json, zipfile, uvicorn
|
5 |
+
from strgen import StringGenerator
|
6 |
+
from pathlib import Path
|
7 |
+
from io import BytesIO
|
8 |
+
from PIL import Image
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
app.add_middleware(
|
13 |
+
CORSMiddleware,
|
14 |
+
allow_origins=[
|
15 |
+
"http://localhost:3000",
|
16 |
+
"http://localhost:8000",
|
17 |
+
"http://127.0.0.1:3000",
|
18 |
+
"http://127.0.0.1:8000",
|
19 |
+
],
|
20 |
+
allow_credentials=True,
|
21 |
+
allow_methods=["*"],
|
22 |
+
allow_headers=["*"],
|
23 |
+
)
|
24 |
+
|
25 |
+
router = APIRouter()
|
26 |
+
|
27 |
+
@router.post('/save')
|
28 |
+
def save(data):
|
29 |
+
zip_buffer = BytesIO()
|
30 |
+
|
31 |
+
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
|
32 |
+
zip_file.writestr('data.xml', data[0].encode('utf-8'))
|
33 |
+
zip_file.writestr('data.txt', data[1].encode('utf-8'))
|
34 |
+
|
35 |
+
for i in range(2, len(data)):
|
36 |
+
file_data = base64.b64decode(data[i].split(',', 1)[1])
|
37 |
+
file_name = f'{i - 1}.{data[i].split(";")[0].split("/")[1]}'
|
38 |
+
zip_file.writestr(file_name, file_data)
|
39 |
+
|
40 |
+
zip_buffer.seek(0)
|
41 |
+
return base64.b64encode(zip_buffer.getvalue()).decode('utf-8')
|
42 |
+
|
43 |
+
@router.post('/load')
|
44 |
+
def load(zip_data):
|
45 |
+
zip_buffer = BytesIO(base64.b64decode(zip_data))
|
46 |
+
data = []
|
47 |
+
with zipfile.ZipFile(zip_buffer, 'r') as zip_file:
|
48 |
+
data.append(zip_file.open('data.xml').read().decode('utf-8'))
|
49 |
+
data.append(zip_file.open('data.txt').read().decode('utf-8'))
|
50 |
+
|
51 |
+
for file_info in zip_file.infolist():
|
52 |
+
if file_info.filename.startswith('data.'): continue
|
53 |
+
with zip_file.open(file_info) as f:
|
54 |
+
ext = file_info.filename.split('.')[-1]
|
55 |
+
url = base64.b64encode(f.read()).decode('utf-8')
|
56 |
+
data.append(f'data:image/{ext};base64,{url}')
|
57 |
+
|
58 |
+
return data
|
59 |
+
|
60 |
+
@router.post('/sb3')
|
61 |
+
def sb3(data):
|
62 |
+
# まず、mmp4.zipからproject.jsonを読み込む
|
63 |
+
with zipfile.ZipFile('mmp4.zip', 'r') as template_zip:
|
64 |
+
with template_zip.open('project.json') as f:
|
65 |
+
project = json.loads(f.read().decode('utf-8'))
|
66 |
+
|
67 |
+
# 新しいZIPファイルを作成
|
68 |
+
zip_buffer = BytesIO()
|
69 |
+
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
|
70 |
+
variables = project['targets'][0]['variables']
|
71 |
+
tiles = project['targets'].index([v for v in project['targets'] if v['name'] == 'Tiles'][0])
|
72 |
+
|
73 |
+
project['targets'][0]['variables'][[v for v in variables if variables[v][0] == 'MODコード'][0]][1] = data[0]
|
74 |
+
|
75 |
+
names = StringGenerator('[a-f\\d]{32}').render_list(len(data) - 1, unique=True)
|
76 |
+
for i in range(1, len(data)):
|
77 |
+
name = f'{names[i-1]}.png'
|
78 |
+
image_data = base64.b64decode(data[i].split(',', 1)[1])
|
79 |
+
|
80 |
+
# 画像をリサイズ
|
81 |
+
img = Image.open(BytesIO(image_data))
|
82 |
+
img_resized = img.resize((40, 40))
|
83 |
+
img_buffer = BytesIO()
|
84 |
+
img_resized.save(img_buffer, format='PNG')
|
85 |
+
img_buffer.seek(0)
|
86 |
+
|
87 |
+
# リサイズした画像をZIPに追加
|
88 |
+
zip_file.writestr(name, img_buffer.getvalue())
|
89 |
+
|
90 |
+
project['targets'][tiles]['costumes'].append({
|
91 |
+
"name": str(i),
|
92 |
+
"bitmapResolution": 2,
|
93 |
+
"dataFormat": "png",
|
94 |
+
"assetId": names[i-1],
|
95 |
+
"md5ext": f"{names[i-1]}.png",
|
96 |
+
"rotationCenterX": 40,
|
97 |
+
"rotationCenterY": 40
|
98 |
+
})
|
99 |
+
|
100 |
+
# 更新されたproject.jsonを書き込む
|
101 |
+
zip_file.writestr('project.json', json.dumps(project).encode('utf-8'))
|
102 |
+
|
103 |
+
# mmp4.zipの他のファイルもコピー
|
104 |
+
with zipfile.ZipFile('mmp4.zip', 'r') as template_zip:
|
105 |
+
for item in template_zip.infolist():
|
106 |
+
if item.filename != 'project.json':
|
107 |
+
zip_file.writestr(item.filename, template_zip.read(item.filename))
|
108 |
+
|
109 |
+
# ZIPファイルのバイナリデータをBase64エンコード
|
110 |
+
zip_buffer.seek(0)
|
111 |
+
sb3_base64 = base64.b64encode(zip_buffer.getvalue()).decode('utf-8')
|
112 |
+
|
113 |
+
return sb3_base64
|
114 |
+
|
115 |
+
app.include_router(router, prefix="/api")
|
116 |
+
|
117 |
+
if __name__ == "__main__":
|
118 |
+
app.mount("/", StaticFiles(directory=Path('public'), html=True), name='public')
|
119 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
public/data.js
ADDED
The diff for this file is too large to render.
See raw diff
|
|
public/data/1.png
ADDED
public/data/10.png
ADDED
public/data/100.png
ADDED
public/data/101.png
ADDED
public/data/102.png
ADDED
public/data/103.png
ADDED
public/data/104.png
ADDED
public/data/105.png
ADDED
public/data/106.png
ADDED
public/data/107.png
ADDED
public/data/108.png
ADDED
public/data/109.png
ADDED
public/data/11.png
ADDED
public/data/110.png
ADDED
public/data/111.png
ADDED
public/data/112.png
ADDED
public/data/113.png
ADDED
public/data/114.png
ADDED
public/data/115.png
ADDED
public/data/116.png
ADDED
public/data/117.png
ADDED
public/data/118.png
ADDED
public/data/119.png
ADDED
public/data/12.png
ADDED
public/data/120.png
ADDED
public/data/121.png
ADDED
public/data/122.png
ADDED
public/data/123.png
ADDED
public/data/124.png
ADDED
public/data/125.png
ADDED
public/data/126.png
ADDED
public/data/127.png
ADDED
public/data/128.png
ADDED
public/data/129.png
ADDED
public/data/13.png
ADDED
public/data/130.png
ADDED
public/data/131.png
ADDED
public/data/132.png
ADDED
public/data/133.png
ADDED
public/data/134.png
ADDED
public/data/135.png
ADDED
public/data/136.png
ADDED
public/data/137.png
ADDED
public/data/138.png
ADDED