Skip to content

Commit

Permalink
Instance json packager (#108)
Browse files Browse the repository at this point in the history
* quick n dirty for testing

* kinda sorting working instance json builder

* add limit check

* bump version

* couple minor error checks
  • Loading branch information
mattpannella authored Feb 6, 2023
1 parent eb843c4 commit 82ea8f8
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 2 deletions.
1 change: 1 addition & 0 deletions models/Analogue/AnalogueInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class Instance
{
public DataSlot[] data_slots { get; set; }
public string data_path { get; set; } = "";
public string magic { get; set; } = "APF_VER_1";
}
9 changes: 9 additions & 0 deletions models/Analogue/AnalogueInstanceDataSlot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Analogue;

using System.Collections;

public class InstanceDataSlot
{
public string id { get; set; }
public string? filename { get; set; }
}
8 changes: 8 additions & 0 deletions models/Analogue/AnalogueSimpleInstance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Analogue;

public class SimpleInstance
{
public InstanceDataSlot[] data_slots { get; set; }
public string data_path { get; set; } = "";
public string magic { get; set; } = "APF_VER_1";
}
6 changes: 6 additions & 0 deletions models/Analogue/AnalogueSimpleInstanceJSON.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Analogue;

public class SimpleInstanceJSON
{
public SimpleInstance instance { get; set; }
}
96 changes: 95 additions & 1 deletion models/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace pannella.analoguepocket;
using System.Net.Http;
using System.Text.Json;
using Force.Crc32;
using System.Collections;

public class Core : Base
{
Expand Down Expand Up @@ -239,6 +240,16 @@ public async Task<Dictionary<string, List<string>>> DownloadAssets()
{"skipped", skipped }
}; //nah
}

string instancePackagerFile = Path.Combine(UpdateDirectory, "Cores", this.identifier, "instance-packager.json");
if(File.Exists(instancePackagerFile)) {
BuildInstanceJSONs();
return new Dictionary<string, List<string>>{
{"installed", installed },
{"skipped", skipped }
};
}

if(Directory.Exists(instancesDirectory)) {
string[] files = Directory.GetFiles(instancesDirectory,"*.json", SearchOption.AllDirectories);
foreach(string file in files) {
Expand Down Expand Up @@ -283,7 +294,11 @@ public async Task<Dictionary<string, List<string>>> DownloadAssets()
checkUpdateDirectory();
string file = Path.Combine(UpdateDirectory, "Cores", this.identifier, "core.json");
string json = File.ReadAllText(file);
Analogue.Config? config = JsonSerializer.Deserialize<Analogue.Config>(json);
var options = new JsonSerializerOptions()
{
AllowTrailingCommas = true
};
Analogue.Config? config = JsonSerializer.Deserialize<Analogue.Config>(json, options);

return config;
}
Expand Down Expand Up @@ -348,4 +363,83 @@ private bool CheckCRC(string filepath)
_writeMessage("Bad checksum!");
return false;
}

private void BuildInstanceJSONs(bool overwrite = true)
{
string instancePackagerFile = Path.Combine(UpdateDirectory, "Cores", this.identifier, "instance-packager.json");
if(!File.Exists(instancePackagerFile)) {
return;
}
_writeMessage("Building instance json files.");
InstancePackager packager = JsonSerializer.Deserialize<InstancePackager>(File.ReadAllText(instancePackagerFile));
string outputDir = Path.Combine(UpdateDirectory, packager.output);
bool warning = false;
foreach(string dir in Directory.GetDirectories(Path.Combine(UpdateDirectory, "Assets", packager.platform_id, "common"))) {
Analogue.SimpleInstanceJSON instancejson = new Analogue.SimpleInstanceJSON();
Analogue.SimpleInstance instance = new Analogue.SimpleInstance();
string dirName = Path.GetFileName(dir);
try {
instance.data_path = dirName + "/";
List<Analogue.InstanceDataSlot> slots = new List<Analogue.InstanceDataSlot>();
string jsonFileName = dirName + ".json";
foreach(DataSlot slot in packager.data_slots) {
string[] files = Directory.GetFiles(dir, slot.filename);
int index = slot.id;
switch(slot.sort) {
case "single":
case "ascending":
Array.Sort(files);
break;
case "descending":
IComparer myComparer = new myReverserClass();
Array.Sort(files, myComparer);
break;
}
if(slot.required && files.Count() == 0) {
throw new Exception("Missing required files.");
}
foreach(string file in files) {
Analogue.InstanceDataSlot current = new Analogue.InstanceDataSlot();
string filename = Path.GetFileName(file);
if(slot.as_filename) {
jsonFileName = Path.GetFileNameWithoutExtension(file) + ".json";
}
current.id = index.ToString();
current.filename = filename;
index++;
slots.Add(current);
}
}
var limit = (JsonElement)packager.slot_limit["count"];
if (slots.Count == 0 || (packager.slot_limit != null && slots.Count > limit.GetInt32())) {
_writeMessage("Unable to build " + jsonFileName);
warning = true;
continue;
}
instance.data_slots = slots.ToArray();
instancejson.instance = instance;
var options = new JsonSerializerOptions()
{
WriteIndented = true
};
string json = JsonSerializer.Serialize<Analogue.SimpleInstanceJSON>(instancejson, options);
_writeMessage("Saving " + jsonFileName);
File.WriteAllText(Path.Combine(UpdateDirectory, packager.output, jsonFileName), json);
} catch(Exception e) {
_writeMessage("Unable to build " + dirName);
}
}
if (warning) {
var message = (JsonElement)packager.slot_limit["message"];
_writeMessage(message.GetString());
}
_writeMessage("Finished");
}
}
public class myReverserClass : IComparer {

// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare( Object x, Object y ) {
return( (new CaseInsensitiveComparer()).Compare( y, x ) );
}
}
10 changes: 10 additions & 0 deletions models/InstancePackager/DataSlot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace pannella.analoguepocket;

public class DataSlot
{
public int id { get; set; }
public string filename { get; set; }
public bool required { get; set; }
public string sort { get; set; } = "ascending";
public bool as_filename { get; set; }
}
9 changes: 9 additions & 0 deletions models/InstancePackager/InstancePackager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace pannella.analoguepocket;

public class InstancePackager
{
public List<DataSlot> data_slots { get; set; }
public string output { get; set; }
public string platform_id { get; set; }
public Dictionary<string, object> slot_limit { get; set; }
}
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.12.0</Version>
<Version>2.13.0</Version>
<Description>Keep your Analogue Pocket up to date</Description>
<Copyright>2022 Matt Pannella</Copyright>
<Authors>Matt Pannella</Authors>
Expand Down

0 comments on commit 82ea8f8

Please sign in to comment.