File size: 3,471 Bytes
6711fd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php
// /api/services/ModrinthService.php

class ModrinthService {
    private string $apiUrl = 'https://api.modrinth.com/v2';
    private string $apiKey;

    public function __construct(string $apiKey) {
        $this->apiKey = $apiKey;
    }

    /**
     * إنشاء مشروع جديد على Modrinth.
     * @param array $projectData بيانات المشروع.
     * @return array البيانات المرجعة من Modrinth.
     */
    public function createProject(array $projectData): array {
        $url = $this->apiUrl . '/project';
        
        // بيانات المشروع يجب أن تتطابق مع مواصفات Modrinth
        $data = [
            'title' => $projectData['title'],
            'description' => $projectData['description'],
            'project_type' => $projectData['project_type'],
            'requested_status' => 'unlisted', // <--- مهم جداً: unlisted
            'initial_versions' => [], // يمكن إضافة النسخة الأولى هنا أو لاحقاً
            'categories' => $projectData['categories'] ?? [],
            'client_side' => $projectData['client_side'] ?? 'required',
            'server_side' => $projectData['server_side'] ?? 'required',
            'body' => $projectData['description'], // Modrinth يستخدم body للوصف الطويل
        ];

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Authorization: ' . $this->apiKey,
            'Content-Type: application/json',
            'User-Agent: Craft-FX/1.0.0 (contact@craft-fx.com)'
        ]);

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($httpCode >= 400) {
            throw new Exception("فشل إنشاء المشروع على Modrinth: " . $response);
        }

        return json_decode($response, true);
    }

    /**
     * رفع نسخة جديدة من الملف إلى Modrinth.
     */
    public function createVersion(string $projectId, array $versionData, string $filePath): array {
        $url = $this->apiUrl . '/version';
        
        $data = [
            'data' => json_encode([
                'name' => $versionData['name'],
                'version_number' => $versionData['version_number'],
                'game_versions' => $versionData['game_versions'],
                'loaders' => $versionData['loaders'],
                'featured' => true,
                'project_id' => $projectId,
                'file_parts' => ['file']
            ]),
            'file' => new CURLFile($filePath)
        ];

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Authorization: ' . $this->apiKey,
            'User-Agent: Craft-FX/1.0.0 (contact@craft-fx.com)'
        ]);

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($httpCode >= 400) {
            throw new Exception("فشل رفع النسخة إلى Modrinth: " . $response);
        }

        return json_decode($response, true);
    }
}