Skip to content

Commit

Permalink
rename jotego platform files automatically (mattpannella#132)
Browse files Browse the repository at this point in the history
* rename jotego platform files automatically

* woops
  • Loading branch information
mattpannella authored Mar 20, 2023
1 parent a719879 commit 34ea2e9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
26 changes: 17 additions & 9 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ private static async Task Main(string[] args)
try {
string location = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
string? path = Path.GetDirectoryName(location);
bool extractAll = false;
bool coreSelector = false;
bool preservePlatformsFolder = false;
bool forceUpdate = false;
bool forceInstanceGenerator = false;


Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(o =>
{
Expand All @@ -29,10 +29,6 @@ private static async Task Main(string[] args)
path = o.InstallPath;
cliMode = true;
}
if(o.ExtractAll) {
extractAll = true;
cliMode = true;
}
if(o.CoreSelector) {
coreSelector = true;
cliMode = true;
Expand Down Expand Up @@ -87,6 +83,7 @@ private static async Task Main(string[] args)
updater.DeleteSkippedCores(settings.GetConfig().delete_skipped_cores);
updater.SetGithubApiKey(settings.GetConfig().github_token);
updater.DownloadFirmware(settings.GetConfig().download_firmware);
updater.RenameJotegoCores(settings.GetConfig().fix_jt_names);
updater.StatusUpdated += updater_StatusUpdated;
updater.UpdateProcessComplete += updater_UpdateProcessComplete;
updater.DownloadAssets(settings.GetConfig().download_assets);
Expand Down Expand Up @@ -202,6 +199,7 @@ static void SetUpdaterFlags(ref PocketCoreUpdater updater, ref SettingsManager s
updater.SetGithubApiKey(settings.GetConfig().github_token);
updater.DownloadFirmware(settings.GetConfig().download_firmware);
updater.DownloadAssets(settings.GetConfig().download_assets);
updater.RenameJotegoCores(settings.GetConfig().fix_jt_names);
}

static async Task RunInstanceGenerator(PocketCoreUpdater updater, bool force = false)
Expand Down Expand Up @@ -297,6 +295,19 @@ static void RunConfigWizard(ref SettingsManager settings)
}
Console.WriteLine("");
valid = false;
while(!valid) {
Console.Write("\nAutomatically rename Jotego cores during 'Update All'?[Y/n] ");
response = Console.ReadKey(false).Key;
if (response == ConsoleKey.N) {
settings.GetConfig().fix_jt_names = false;
valid = true;
} else if (response == ConsoleKey.Y || response == ConsoleKey.Enter) {
settings.GetConfig().fix_jt_names = true;
valid = true;
}
}
Console.WriteLine("");
valid = false;
while(!valid) {
Console.Write("\nPreserve 'Platforms' folder during 'Update All'?[y/N] ");
response = Console.ReadKey(false).Key;
Expand Down Expand Up @@ -587,7 +598,7 @@ private static void AskAboutNewCores(ref SettingsManager settings, bool force =
"Select Cores",
"Download Platform Image Packs",
"Generate Instance JSON Files",
"Configuration Wizard",
"Settings",
"Exit"
};
}
Expand All @@ -599,9 +610,6 @@ public class Options
[Option('p', "path", HelpText = "Absolute path to install location", Required = false)]
public string? InstallPath { get; set; }

[Option ('a', "all", Required = false, HelpText = "Extract all release assets, instead of just ones containing openFPGA cores.")]
public bool ExtractAll { get; set; }

[Option ('c', "coreselector", Required = false, HelpText = "Run the core selector.")]
public bool CoreSelector { get; set; }

Expand Down
46 changes: 43 additions & 3 deletions Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class PocketCoreUpdater : Base
private bool _downloadFirmare = true;
private bool _deleteSkippedCores = true;
private bool _useConsole = false;
private bool _renameJotegoCores = true;
/// <summary>
/// The directory where fpga cores will be installed and updated into
/// </summary>
Expand All @@ -41,6 +42,8 @@ public class PocketCoreUpdater : Base
private archiveorg.Archive _archiveFiles;
private string[] _blacklist;

private Dictionary<string, string> _platformFiles = new Dictionary<string, string>();

/// <summary>
/// Constructor
/// </summary>
Expand All @@ -60,14 +63,28 @@ public PocketCoreUpdater(string updateDirectory, string? settingsPath = null)

public async Task Initialize()
{

await LoadPlatformFiles();
await LoadCores();
// await LoadDependencies();
LoadSettings();
await LoadArchive();
await LoadBlacklist();
}

private async Task LoadPlatformFiles()
{
List<Github.File> files = await GithubApi.GetFiles("dyreschlock", "pocket-platform-images", "arcade/Platforms");
Dictionary<string, string> platformFiles = new Dictionary<string, string>();
foreach(Github.File file in files) {
string url = file.download_url;
string filename = file.name;
if (filename.EndsWith(".json")) {
string platform = Path.GetFileNameWithoutExtension(filename);
platformFiles.Add(platform, url);
}
}
_platformFiles = platformFiles;
}

private async Task LoadArchive()
{
_archiveFiles = await ArchiveService.GetFiles(this._settingsManager.GetConfig().archive_name);
Expand All @@ -88,7 +105,6 @@ public async Task LoadCores()
foreach(Core core in _cores) {
core.StatusUpdated += updater_StatusUpdated; //attach handler to bubble event up
}
// await LoadNonAPICores();
}

public async Task LoadNonAPICores()
Expand All @@ -112,6 +128,11 @@ public void PrintToConsole(bool set)
_useConsole = set;
}

public void RenameJotegoCores(bool set)
{
_renameJotegoCores = set;
}

/// <summary>
/// Turn on/off the automatic BIOS downloader
/// </summary>
Expand Down Expand Up @@ -218,6 +239,7 @@ public async Task RunUpdates()
results = await core.DownloadAssets();
installedAssets.AddRange(results["installed"]);
skippedAssets.AddRange(results["skipped"]);
await JotegoRename(core);
Divide();
continue;
}
Expand All @@ -235,6 +257,7 @@ public async Task RunUpdates()
_writeMessage("Updating core");
} else {
results = await core.DownloadAssets();
await JotegoRename(core);
installedAssets.AddRange(results["installed"]);
skippedAssets.AddRange(results["skipped"]);
_writeMessage("Up to date. Skipping core");
Expand All @@ -252,6 +275,7 @@ public async Task RunUpdates()
summary.Add("platform", core.platform.name);
installed.Add(summary);
}
await JotegoRename(core);
results = await core.DownloadAssets();
installedAssets.AddRange(results["installed"]);
skippedAssets.AddRange(results["skipped"]);
Expand All @@ -278,6 +302,22 @@ public async Task RunUpdates()
OnUpdateProcessComplete(args);
}

private async Task JotegoRename(Core core)
{
if(_renameJotegoCores && core.identifier.Contains("jotego")) {
core.platform_id = core.identifier.Split('.')[1]; //whatever
string path = Path.Combine(UpdateDirectory, "Platforms", core.platform_id + ".json");
string json = File.ReadAllText(path);
Dictionary<string, Platform> data = JsonSerializer.Deserialize<Dictionary<string,Platform>>(json);
Platform platform = data["platform"];
if(_platformFiles.ContainsKey(core.platform_id) && platform.name == core.platform_id) {
_writeMessage("Updating JT Platform Name...");
await HttpHelper.DownloadFileAsync(_platformFiles[core.platform_id], path);
_writeMessage("Complete");
}
}
}

public async Task RunAssetDownloader(string? id = null)
{
List<string> installedAssets = new List<string>();
Expand Down
2 changes: 2 additions & 0 deletions models/Settings/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Config
public string? download_new_cores { get; set; }
public bool build_instance_jsons { get; set; }
public bool crc_check { get; set; }
public bool fix_jt_names { get; set; }

public Config()
{
Expand All @@ -24,5 +25,6 @@ public Config()
download_new_cores = null;
build_instance_jsons = true;
crc_check = true;
fix_jt_names = true;
}
}
2 changes: 1 addition & 1 deletion pocket_updater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.19.2</Version>
<Version>2.20.0</Version>
<Description>Keep your Analogue Pocket up to date</Description>
<Copyright>2023 Matt Pannella</Copyright>
<Authors>Matt Pannella</Authors>
Expand Down
10 changes: 10 additions & 0 deletions services/GithubApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public static class GithubApi
return file;
}

public static async Task<List<Github.File>?> GetFiles(string user, string repository, string path, string? token = "")
{
string url = String.Format(CONTENTS, user, repository, path);

var responseBody = await CallAPI(url, token);
List<Github.File>? files = JsonSerializer.Deserialize<List<Github.File>>(responseBody);

return files;
}

private static async Task<string> CallAPI(string url, string? token = "")
{
var client = new HttpClient();
Expand Down

0 comments on commit 34ea2e9

Please sign in to comment.