Skip to content

Commit

Permalink
Added source icons build script
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteAsian123 committed Jun 19, 2023
1 parent 06de320 commit bc3ff34
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
63 changes: 63 additions & 0 deletions Assets/Editor/SourceIconsBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using YARG.Song;
using YARG.Util;

namespace Editor {
public class SourceIconsBuilder : IPreprocessBuildWithReport {
public int callbackOrder => 0;

public void OnPreprocessBuild(BuildReport report) {
var sourcesFolder = Path.Combine(Application.streamingAssetsPath, "sources");

// Otherwise, update!
try {
// Delete version.txt
var txt = Path.Combine(sourcesFolder, "version.txt");
if (File.Exists(txt)) {
File.Delete(txt);
}

// Download
string zipPath = Path.Combine(sourcesFolder, "update.zip");
using (var client = new WebClient()) {
client.DownloadFile(SongSources.SOURCE_ZIP_URL, zipPath);
}

// Delete the old folder
var repoDir = Path.Combine(sourcesFolder, SongSources.SOURCE_REPO_FOLDER);
if (Directory.Exists(repoDir)) {
Directory.Delete(repoDir, true);
}

// Extract the base and extras folder
ZipFile.ExtractToDirectory(zipPath, sourcesFolder);

// Delete the random folders
foreach (var folder in Directory.GetDirectories(repoDir)) {
if (PathHelper.PathsEqual(Path.GetFileName(folder), "base")) {
continue;
}

Directory.Delete(folder, true);
}

// Delete the random files
foreach (var file in Directory.GetFiles(repoDir)) {
File.Delete(file);
}

// Delete the zip
File.Delete(zipPath);
} catch (Exception e) {
Debug.LogError("Failed to download newest song source version.");
Debug.LogException(e);
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Editor/SourceIconsBuilder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions Assets/Script/Song/SongSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ public async UniTask<Sprite> GetIcon() {

public static string SourcesFolder => Path.Combine(PathHelper.StreamingAssetsPath, "sources");

private const string SOURCE_REPO_FOLDER = "OpenSource-master";
public const string SOURCE_REPO_FOLDER = "OpenSource-master";

private const string SOURCE_COMMIT_URL = "https://api.github.com/repos/YARC-Official/OpenSource/commits?per_page=1";
private const string SOURCE_ZIP_URL = "https://github.com/YARC-Official/OpenSource/archive/refs/heads/master.zip";
public const string SOURCE_COMMIT_URL = "https://api.github.com/repos/YARC-Official/OpenSource/commits?per_page=1";
public const string SOURCE_ZIP_URL = "https://github.com/YARC-Official/OpenSource/archive/refs/heads/master.zip";

private static readonly string[] SourceTypes = {
"base",
Expand Down Expand Up @@ -176,28 +176,28 @@ await UniTask.RunOnThreadPool(() => {
}

// Delete the old folder
var baseDir = Path.Combine(SourcesFolder, SOURCE_REPO_FOLDER);
if (Directory.Exists(baseDir)) {
Directory.Delete(baseDir, true);
var repoDir = Path.Combine(SourcesFolder, SOURCE_REPO_FOLDER);
if (Directory.Exists(repoDir)) {
Directory.Delete(repoDir, true);
}

// Extract the base and extras folder
updateText("Extracting new version...");
ZipFile.ExtractToDirectory(zipPath, SourcesFolder);

// Delete the random folders
var ignoreFolder = Path.Combine(baseDir, "ignore");
var ignoreFolder = Path.Combine(repoDir, "ignore");
if (Directory.Exists(ignoreFolder)) {
Directory.Delete(ignoreFolder, true);
}

var githubFolder = Path.Combine(baseDir, ".github");
var githubFolder = Path.Combine(repoDir, ".github");
if (Directory.Exists(githubFolder)) {
Directory.Delete(githubFolder, true);
}

// Delete the random files
foreach (var file in Directory.GetFiles(baseDir)) {
foreach (var file in Directory.GetFiles(repoDir)) {
File.Delete(file);
}

Expand Down

0 comments on commit bc3ff34

Please sign in to comment.