Skip to content

Commit

Permalink
hack to support bios files for multiple cores that come from the same…
Browse files Browse the repository at this point in the history
… repo. whatever

add prelim support for github api key
unpack all asset zip files for a release, now. this should solve the gb/gbc problem going forward
  • Loading branch information
mattpannella committed Aug 28, 2022
1 parent 3d986b5 commit cfcee77
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 35 deletions.
3 changes: 1 addition & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

internal class Program
{
private const string VERSION = "1.2.0";
private const string VERSION = "1.3.0";
private const string API_URL = "https://api.github.com/repos/mattpannella/pocket_core_autoupdate_net/releases";

private const string REMOTE_CORES_FILE = "https://raw.githubusercontent.com/mattpannella/pocket_core_autoupdate_net/main/auto_update.json";
Expand Down Expand Up @@ -53,7 +53,6 @@ private static async Task Main(string[] args)

updater.StatusUpdated += updater_StatusUpdated;
updater.InstallBiosFiles(true);

Console.WriteLine("Starting update process...");

await updater.RunUpdates();
Expand Down
86 changes: 54 additions & 32 deletions Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class PocketCoreUpdater
private const string ZIP_FILE_NAME = "core.zip";
private bool _installBios = false;

private string _githubApiKey;

private bool _useConsole = false;
/// <summary>
/// The directory where fpga cores will be installed and updated into
Expand Down Expand Up @@ -69,14 +71,18 @@ public async Task RunUpdates()
}
string json = File.ReadAllText(CoresFile);
List<Core>? coresList = JsonSerializer.Deserialize<List<Core>>(json);
//TODO check if null

foreach(Core core in coresList) {
if(core.skip) {
continue;
}
Repo repo = core.repo;
_writeMessage("Starting Repo: " + repo.project);
string name = core.name;
if(name == null) {
_writeMessage("Core Name is required. Skipping.");
continue;
}
bool allowPrerelease = core.allowPrerelease;
string url = String.Format(GITHUB_API_URL, repo.user, repo.project);
string response = await _fetchReleases(url);
Expand All @@ -85,34 +91,22 @@ public async Task RunUpdates()
}
List<Github.Release>? releases = JsonSerializer.Deserialize<List<Github.Release>>(response);
var mostRecentRelease = _getMostRecentRelease(releases, allowPrerelease);

if(mostRecentRelease == null) {
_writeMessage("No releases found. Skipping");
continue;
}
string tag_name = mostRecentRelease.tag_name;
List<Github.Asset> assets = mostRecentRelease.assets;

string releaseSemver = SemverUtil.FindSemver(tag_name);

Github.Asset coreAsset = null;

// might need to search for the right zip here if there's more than one
//iterate through assets to find the zip release
for(int i = 0; i < assets.Count; i++) {
if(ZIP_TYPES.Contains(assets[i].content_type)) {
coreAsset = assets[i];
break;
}
}

if(coreAsset == null) {
_writeMessage("No zip file found for release. Skipping");
continue;
}

string nameGuess = name ?? coreAsset.name.Split("_")[0];
_writeMessage(tag_name + " is the most recent release, checking local core...");
string localCoreFile = Path.Combine(UpdateDirectory, "Cores/"+nameGuess+"/core.json");
string localCoreFile = Path.Combine(UpdateDirectory, "Cores/"+name+"/core.json");
bool fileExists = File.Exists(localCoreFile);

if (fileExists) {
bool foundZip = false;

if (fileExists) {
json = File.ReadAllText(localCoreFile);

Analogue.Config? config = JsonSerializer.Deserialize<Analogue.Config>(json);
Expand All @@ -126,21 +120,36 @@ public async Task RunUpdates()

if (!SemverUtil.IsActuallySemver(localSemver) || !SemverUtil.IsActuallySemver(releaseSemver)) {
_writeMessage("downloading core anyway");
await _updateCore(coreAsset.browser_download_url);
continue;
}

if (SemverUtil.SemverCompare(releaseSemver, localSemver)){
} else if (SemverUtil.SemverCompare(releaseSemver, localSemver)){
_writeMessage("Updating core");
await _updateCore(coreAsset.browser_download_url);
} else {
await SetupBios(core.bios); //check for bios even if core isn't updating
_writeMessage("Up to date. Skipping core");
_writeMessage("------------");
continue;
}
} else {
_writeMessage("Downloading core");
await _updateCore(coreAsset.browser_download_url);
}

// might need to search for the right zip here if there's more than one
//iterate through assets to find the zip release
foreach(Github.Asset asset in assets) {
if(!ZIP_TYPES.Contains(asset.content_type)) {
//not a zip file. move on
continue;
}
foundZip = true;
await _getAsset(asset.browser_download_url);
}

if(!foundZip) {
_writeMessage("No zip file found for release. Skipping");
_writeMessage("------------");
continue;
}
await SetupBios(core.bios);
_writeMessage("Installation complete.");
_writeMessage("------------");
}
}
Expand All @@ -150,8 +159,13 @@ private async Task SetupBios(Bios bios)
if(_installBios && bios != null) {
_writeMessage("Looking for BIOS");
string path = Path.Combine(UpdateDirectory, bios.location);
string filePath;
foreach(BiosFile file in bios.files) {
string filePath = Path.Combine(path, file.file_name);
if(file.overrideLocation != null) { //we have a location override
filePath = Path.Combine(UpdateDirectory, file.overrideLocation, file.file_name);
} else {
filePath = Path.Combine(path, file.file_name);
}
if(File.Exists(filePath)) {
_writeMessage("BIOS file already installed: " + file.file_name);
} else {
Expand Down Expand Up @@ -200,20 +214,24 @@ private async Task<string> _fetchReleases(string url)
};
var agent = new ProductInfoHeaderValue("Analogue-Pocket-Auto-Updater", "1.0");
request.Headers.UserAgent.Add(agent);
if(_githubApiKey != null) {
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("token", _githubApiKey);
}
var response = await client.SendAsync(request).ConfigureAwait(false);
response.EnsureSuccessStatusCode();

var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

return responseBody;
} catch (HttpRequestException e) {
_writeMessage("Error pulling communicating with Github API.");
_writeMessage("Error communicating with Github API.");
_writeMessage(e.Message);
return null;
}
}

private async Task _updateCore(string downloadLink)
private async Task _getAsset(string downloadLink)
{
_writeMessage("Downloading file " + downloadLink + "...");
string zipPath = Path.Combine(UpdateDirectory, ZIP_FILE_NAME);
Expand All @@ -223,7 +241,6 @@ private async Task _updateCore(string downloadLink)
_writeMessage("Extracting...");
ZipFile.ExtractToDirectory(zipPath, extractPath, true);
File.Delete(zipPath);
_writeMessage("Installation complete.");
}

private void _writeMessage(string message)
Expand All @@ -236,6 +253,11 @@ private void _writeMessage(string message)
OnStatusUpdated(args);
}

public void SetGithubApiKey(string key)
{
_githubApiKey = key;
}

protected virtual void OnStatusUpdated(StatusUpdatedEventArgs e)
{
EventHandler<StatusUpdatedEventArgs> handler = StatusUpdated;
Expand Down
8 changes: 7 additions & 1 deletion auto_update.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
{
"file_name": "gbc_bios.bin",
"url": "https://archive.org/download/mister-console-bios-pack_theypsilon/Gameboy.zip/GBC_boot_ROM.gb"
},
{
"overrideLocation": "assets/gb/common/",
"file_name": "dmg_bios.bin",
"url": "https://archive.org/download/mister-console-bios-pack_theypsilon/Gameboy.zip/GB_boot_ROM.gb"
}
]
},
Expand All @@ -38,7 +43,8 @@
"user": "spacemen3",
"project": "PDP-1"
},
"platform": "PDP-1"
"platform": "PDP-1",
"name": "Spacemen3.PDP1"
},
{
"repo": {
Expand Down
1 change: 1 addition & 0 deletions models/Bios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class BiosFile
public string file_name { get; set; }
public bool zip { get; set; }
public string zip_file { get; set; }
public string overrideLocation { get; set; }
}

0 comments on commit cfcee77

Please sign in to comment.