Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/mod_uploader/StarModUploader.cpp
blob: a2d0d201e8229d16e8bfb324dee4aa80b1976827 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include <QApplication>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QProgressDialog>
#include <QMessageBox>

#include "StarModUploader.hpp"
#include "StarFile.hpp"
#include "StarThread.hpp"
#include "StarLexicalCast.hpp"
#include "StarPackedAssetSource.hpp"
#include "StarStringConversion.hpp"

namespace Star {

ModUploader::ModUploader()
  : QMainWindow() {

  auto selectDirectoryButton = new QPushButton("Select Mod Directory");
  m_directoryLabel = new QLabel();
  m_reloadButton = new QPushButton("Reload");
  m_nameEditor = new QLineEdit();
  m_titleEditor = new QLineEdit();
  m_authorEditor = new QLineEdit();
  m_versionEditor = new QLineEdit();
  m_descriptionEditor = new SPlainTextEdit();
  m_previewImageLabel = new QLabel();
  auto selectPreviewImageButton = new QPushButton("Select");
  m_modIdLabel = new QLabel();
  auto resetModIdButton = new QPushButton("Reset Steam Mod Information");
  auto userAgreementLabel = new QLabel("By submitting this item, you agree to the <a href=\"http://steamcommunity.com/sharedfiles/workshoplegalagreement\">workshop terms of service</a>");
  auto uploadButton = new QPushButton("Upload to Steam!");

  m_categorySelectors.set("Armor and Clothes", new QCheckBox("Armor and Clothes"));
  m_categorySelectors.set("Character Improvements", new QCheckBox("Character Improvements"));
  m_categorySelectors.set("Cheats and God Items", new QCheckBox("Cheats and God Items"));
  m_categorySelectors.set("Crafting and Building", new QCheckBox("Crafting and Building"));
  m_categorySelectors.set("Dungeons", new QCheckBox("Dungeons"));
  m_categorySelectors.set("Food and Farming", new QCheckBox("Food and Farming"));
  m_categorySelectors.set("Furniture and Objects", new QCheckBox("Furniture and Objects"));
  m_categorySelectors.set("In-Game Tools", new QCheckBox("In-Game Tools"));
  m_categorySelectors.set("Mechanics", new QCheckBox("Mechanics"));
  m_categorySelectors.set("Miscellaneous", new QCheckBox("Miscellaneous"));
  m_categorySelectors.set("Musical Instruments and Songs", new QCheckBox("Musical Instruments and Songs"));
  m_categorySelectors.set("NPCs and Creatures", new QCheckBox("NPCs and Creatures"));
  m_categorySelectors.set("Planets and Environments", new QCheckBox("Planets and Environments"));
  m_categorySelectors.set("Quests", new QCheckBox("Quests"));
  m_categorySelectors.set("Species", new QCheckBox("Species"));
  m_categorySelectors.set("Ships", new QCheckBox("Ships"));
  m_categorySelectors.set("User Interface", new QCheckBox("User Interface"));
  m_categorySelectors.set("Vehicles and Mounts", new QCheckBox("Vehicles and Mounts"));
  m_categorySelectors.set("Weapons", new QCheckBox("Weapons"));

  m_modIdLabel->setOpenExternalLinks(true);
  userAgreementLabel->setOpenExternalLinks(true);

  connect(selectDirectoryButton, SIGNAL(clicked()), this, SLOT(selectDirectory()));
  connect(m_reloadButton, SIGNAL(clicked()), this, SLOT(loadDirectory()));
  connect(selectPreviewImageButton, SIGNAL(clicked()), this, SLOT(selectPreview()));
  connect(m_nameEditor, SIGNAL(editingFinished()), this, SLOT(writeMetadata()));
  connect(m_titleEditor, SIGNAL(editingFinished()), this, SLOT(writeMetadata()));
  connect(m_authorEditor, SIGNAL(editingFinished()), this, SLOT(writeMetadata()));
  connect(m_versionEditor, SIGNAL(editingFinished()), this, SLOT(writeMetadata()));
  connect(m_descriptionEditor, SIGNAL(editingFinished()), this, SLOT(writeMetadata()));
  connect(resetModIdButton, SIGNAL(clicked()), this, SLOT(resetModId()));
  connect(uploadButton, SIGNAL(clicked()), this, SLOT(uploadToSteam()));

  for (auto pair : m_categorySelectors) {
    connect(pair.second, SIGNAL(clicked()), this, SLOT(writeMetadata()));
  }

  auto loadDirectoryLayout = new QHBoxLayout();
  loadDirectoryLayout->addWidget(selectDirectoryButton);
  loadDirectoryLayout->addWidget(m_directoryLabel, 1);
  loadDirectoryLayout->addWidget(m_reloadButton);

  QGridLayout* editorLeftLayout = new QGridLayout();
  editorLeftLayout->addWidget(new QLabel("Name"), 0, 0);
  editorLeftLayout->addWidget(m_nameEditor, 0, 1, 1, 2);

  editorLeftLayout->addWidget(new QLabel("Title"), 1, 0);
  editorLeftLayout->addWidget(m_titleEditor, 1, 1, 1, 2);

  editorLeftLayout->addWidget(new QLabel("Author"), 2, 0);
  editorLeftLayout->addWidget(m_authorEditor, 2, 1, 1, 2);

  editorLeftLayout->addWidget(new QLabel("Version"), 3, 0);
  editorLeftLayout->addWidget(m_versionEditor, 3, 1, 1, 2);

  editorLeftLayout->addWidget(new QLabel("Description"), 4, 0);
  editorLeftLayout->addWidget(m_descriptionEditor, 4, 1, 1, 2);

  editorLeftLayout->addWidget(new QLabel("Preview Image"), 5, 0);
  editorLeftLayout->addWidget(m_previewImageLabel, 5, 1);
  editorLeftLayout->addWidget(selectPreviewImageButton, 5, 2);

  editorLeftLayout->addWidget(new QLabel("Mod ID"), 6, 0);
  editorLeftLayout->addWidget(m_modIdLabel, 6, 1);
  editorLeftLayout->addWidget(resetModIdButton, 6, 2);

  editorLeftLayout->addWidget(userAgreementLabel, 7, 0, 1, 3, Qt::AlignCenter);
  editorLeftLayout->addWidget(uploadButton, 8, 0, 1, 3);

  editorLeftLayout->setColumnStretch(1, 1);

  QVBoxLayout* categoryLayout = new QVBoxLayout();
  categoryLayout->addWidget(new QLabel("Categories"));
  auto categoryKeys = m_categorySelectors.keys();
  categoryKeys.sort();
  for (auto k : categoryKeys) {
    categoryLayout->addWidget(m_categorySelectors[k]);
  }
  categoryLayout-> addWidget(new QWidget(), 1);

  QHBoxLayout* editorLayout = new QHBoxLayout();
  editorLayout->addLayout(editorLeftLayout, 1);
  editorLayout->addLayout(categoryLayout);

  m_editorSection = new QWidget();
  m_editorSection->setLayout(editorLayout);

  auto mainLayout = new QVBoxLayout();
  mainLayout->addLayout(loadDirectoryLayout);
  mainLayout->addWidget(m_editorSection);

  auto* centralWidget = new QWidget(this);
  centralWidget->setLayout(mainLayout);
  setCentralWidget(centralWidget);

  m_reloadButton->setEnabled(false);
  m_editorSection->setEnabled(false);

  setWindowTitle("Steam Mod Uploader");
  resize(1000, 600);
}

void ModUploader::selectDirectory() {
  QString dir = QFileDialog::getExistingDirectory(this, "Select the top-level mod directory");
  m_modDirectory = toSString(dir);

  loadDirectory();
}

void ModUploader::loadDirectory() {
  QProgressDialog progress("Loading mod directory...", "", 0, 0, this);
  progress.setWindowModality(Qt::WindowModal);
  progress.setCancelButton(nullptr);
  progress.setAutoReset(false);
  progress.show();

  if (m_modDirectory && !File::isDirectory(*m_modDirectory))
    m_modDirectory.reset();

  if (!m_modDirectory) {
    m_reloadButton->setEnabled(false);
    m_directoryLabel->setText("");
    m_editorSection->setEnabled(false);
    m_assetSource.reset();
    return;
  }

  m_reloadButton->setEnabled(true);
  m_directoryLabel->setText(toQString(*m_modDirectory));
  m_editorSection->setEnabled(true);
  m_assetSource = DirectoryAssetSource(*m_modDirectory);

  JsonObject metadata = m_assetSource->metadata();
  m_nameEditor->setText(toQString(metadata.value("name", "").toString()));
  m_titleEditor->setText(toQString(metadata.value("friendlyName", "").toString()));
  m_authorEditor->setText(toQString(metadata.value("author", "").toString()));
  m_versionEditor->setText(toQString(metadata.value("version", "").toString()));
  m_descriptionEditor->setPlainText(toQString(metadata.value("description", "").toString()));

  for (auto pair : m_categorySelectors)
    pair.second->setChecked(false);

  auto tagString = metadata.value("tags", "").toString();
  auto tagList = tagString.split('|');
  for (auto tag : tagList) {
    if (m_categorySelectors.contains(tag))
      m_categorySelectors[tag]->setChecked(true);
  }

  String modId = metadata.value("steamContentId", "").toString();
  m_modIdLabel->setText(toQString(strf("<a href=\"steam://url/CommunityFilePage/{}\">{}</a>", modId, modId)));

  String previewFile = File::relativeTo(*m_modDirectory, "_previewimage");
  if (File::isFile(previewFile)) {
    m_modPreview = QImage(toQString(previewFile), "PNG");
    m_previewImageLabel->setPixmap(QPixmap::fromImage(m_modPreview));
  } else {
    m_modPreview = {};
    m_previewImageLabel->setPixmap({});
  }
}

void ModUploader::selectPreview() {
  QString image = QFileDialog::getOpenFileName(this, "Select a mod preview image", "", "Images (*.png *.jpg)");

  m_modPreview = {};
  m_previewImageLabel->setPixmap({});

  if (!image.isEmpty()) {
    if (m_modPreview.load(image))
      m_previewImageLabel->setPixmap(QPixmap::fromImage(m_modPreview));
    else
      QMessageBox::critical(this, "Error", "Could not load preview image");
  }

  writePreview();
}

void ModUploader::writeMetadata() {
  if (!m_assetSource)
    return;

  auto metadata = m_assetSource->metadata();
  auto setMetadata = [&metadata](String const& key, String const& value) {
    if (value.empty())
      metadata.remove(key);
    else
      metadata[key] = value;
  };

  setMetadata("name", toSString(m_nameEditor->text()));
  setMetadata("friendlyName", toSString(m_titleEditor->text()));
  setMetadata("author", toSString(m_authorEditor->text()));
  setMetadata("version", toSString(m_versionEditor->text()));
  setMetadata("description", toSString(m_descriptionEditor->toPlainText()));

  auto tagList = StringList();
  for (auto pair : m_categorySelectors) {
    if (pair.second->isChecked())
      tagList.append(pair.first);
  }
  auto tagString = tagList.join("|");
  setMetadata("tags", tagString);

  m_assetSource->setMetadata(metadata);
}

void ModUploader::writePreview() {
  if (m_modPreview.isNull())
    return;

  String modPreviewFile = File::relativeTo(*m_modDirectory, "_previewimage");
  m_modPreview.save(toQString(modPreviewFile), "PNG");
}

void ModUploader::resetModId() {
  m_modIdLabel->setText("");
  auto metadata = m_assetSource->metadata();
  metadata.remove("steamContentId");
  m_assetSource->setMetadata(metadata);
}

void ModUploader::uploadToSteam() {
  if (!m_modDirectory)
    return;

  QProgressDialog progress("Uploading to Steam...", "", 0, 0, this);
  progress.setWindowModality(Qt::WindowModal);
  progress.setCancelButton(nullptr);
  progress.setAutoReset(false);
  progress.show();

  if (m_assetSource->assetPaths().empty()) {
    QMessageBox::critical(this, "Error", "Cannot upload, mod has no content");
    return;
  }

  m_steamItemCreateResult = {};
  m_steamItemSubmitResult = {};

  JsonObject metadata = m_assetSource->metadata();
  String modIdString = metadata.value("steamContentId", "").toString();
  if (modIdString.empty()) {
    CCallResult<ModUploader, CreateItemResult_t> callResultCreate;
    callResultCreate.Set(SteamUGC()->CreateItem(SteamUtils()->GetAppID(), k_EWorkshopFileTypeCommunity),
        this, &ModUploader::onSteamCreateItem);

    progress.setLabelText("Creating new Steam UGC Item");
    while (!m_steamItemCreateResult) {
      QApplication::processEvents();
      SteamAPI_RunCallbacks();
      Thread::sleep(20);
    }

    if (m_steamItemCreateResult->second) {
      QMessageBox::critical(this, "Error", "There was an IO error creating a new Steam UGC item");
      return;
    }

    if (m_steamItemCreateResult->first.m_bUserNeedsToAcceptWorkshopLegalAgreement) {
      QMessageBox::critical(this, "Error", "The current Steam user has not agreed to the workshop legal agreement");
      return;
    }

    if (m_steamItemCreateResult->first.m_eResult == k_EResultInsufficientPrivilege) {
      QMessageBox::critical(this, "Error", "Insufficient privileges to create a new Steam UGC item");
      return;
    }

    if (m_steamItemCreateResult->first.m_eResult != k_EResultOK) {
      QMessageBox::critical(this, "Error", strf("Error creating new Steam UGC Item ({})", m_steamItemCreateResult->first.m_eResult).c_str());
      return;
    }

    modIdString = toString(m_steamItemCreateResult->first.m_nPublishedFileId);
    String modUrl = strf("steam://url/CommunityFilePage/{}", modIdString);

    metadata.set("steamContentId", modIdString);
    metadata.set("link", modUrl);
    m_assetSource->setMetadata(metadata);

    m_modIdLabel->setText(toQString(strf("<a href=\"{}\">{}</a>", modUrl, modIdString)));
  }

  String steamUploadDir = File::temporaryDirectory();
  auto progressCallback = [&progress](size_t i, size_t total, String, String assetPath) {
    progress.setLabelText(toQString(strf("Packing '{}'", assetPath)));
    progress.setMaximum(total);
    progress.setValue(i);
    QApplication::processEvents();
  };

  String packedPath = File::relativeTo(steamUploadDir, "contents.pak");
  PackedAssetSource::build(*m_assetSource, packedPath, {}, progressCallback);

  PublishedFileId_t modId = lexicalCast<PublishedFileId_t>(modIdString);

  UGCUpdateHandle_t updateHandle = SteamUGC()->StartItemUpdate(SteamUtils()->GetAppID(), modId);
  SteamUGC()->SetItemTitle(updateHandle, toSString(m_titleEditor->text()).utf8Ptr());
  SteamUGC()->SetItemDescription(updateHandle, toSString(m_descriptionEditor->toPlainText()).utf8Ptr());
  if (!m_modPreview.isNull())
    SteamUGC()->SetItemPreview(updateHandle, File::relativeTo(*m_modDirectory, "_previewimage").utf8Ptr());
  SteamUGC()->SetItemContent(updateHandle, steamUploadDir.utf8Ptr());

  // construct tags
  auto tagList = StringList();
  for (auto entry : m_categorySelectors.pairs()) {
    if (entry.second->isChecked())
      tagList.append(entry.first);
  }
  const char** tagStrings = new const char*[tagList.size()];
  for (int i = 0; i < tagList.size(); ++i) {
    tagStrings[i] = tagList[i].utf8Ptr();
  }
  SteamUGC()->SetItemTags(updateHandle, &SteamParamStringArray_t{tagStrings, (int32_t)tagList.size()});

  CCallResult<ModUploader, SubmitItemUpdateResult_t> callResultSubmit;
  callResultSubmit.Set(SteamUGC()->SubmitItemUpdate(updateHandle, nullptr),
      this, &ModUploader::onSteamSubmitItem);

  progress.setLabelText("Updating Steam UGC Item");
  while (!m_steamItemSubmitResult) {
    uint64 bytesProcessed;
    uint64 bytesTotal;
    SteamUGC()->GetItemUpdateProgress(updateHandle, &bytesProcessed, &bytesTotal);
    progress.setMaximum(bytesTotal);
    progress.setValue(bytesProcessed);
    QApplication::processEvents();
    SteamAPI_RunCallbacks();
    Thread::sleep(20);
  }

  File::removeDirectoryRecursive(steamUploadDir);

  if (m_steamItemSubmitResult->second) {
    QMessageBox::critical(this, "Error", "There was an IO error submitting changes to the Steam UGC item");
    return;
  }

  if (m_steamItemSubmitResult->first.m_bUserNeedsToAcceptWorkshopLegalAgreement) {
    QMessageBox::critical(this, "Error", "The current Steam user has not agreed to the workshop legal agreement");
    return;
  }

  if (m_steamItemSubmitResult->first.m_eResult != k_EResultOK) {
    QMessageBox::critical(this, "Error", strf("Error submitting changes to the Steam UGC item ({})", m_steamItemSubmitResult->first.m_eResult).c_str());
    return;
  }
}

void ModUploader::onSteamCreateItem(CreateItemResult_t* result, bool ioFailure) {
  m_steamItemCreateResult = make_pair(*result, ioFailure);
}

void ModUploader::onSteamSubmitItem(SubmitItemUpdateResult_t* result, bool ioFailure) {
  m_steamItemSubmitResult = make_pair(*result, ioFailure);
}

}