164 lines
5.3 KiB
C#
164 lines
5.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace QuestAppLauncher
|
|
{
|
|
/// <summary>
|
|
/// Root config object
|
|
/// </summary>
|
|
[Serializable]
|
|
public class Config
|
|
{
|
|
// Supported category mode
|
|
public const string Category_Off = "off";
|
|
public const string Category_Top = "top";
|
|
public const string Category_Left = "left";
|
|
public const string Category_Right = "right";
|
|
|
|
// Sort settings
|
|
public const string Sort_AZ = "az";
|
|
public const string Sort_MostRecent = "mostRecent";
|
|
|
|
// Download repos
|
|
public const string DownloadRepo_Type_GitHub = "github";
|
|
|
|
/// <summary>
|
|
/// Asset source backed by a plain HTTP endpoint that hosts a manifest JSON.
|
|
/// <c>repoUri</c> should be the full URL to the manifest file.
|
|
/// Manifest format: { "files": [ { "name": "iconpack_v1.zip", "url": "...", "updatedAt": "..." } ] }
|
|
/// </summary>
|
|
public const string DownloadRepo_Type_Http = "http";
|
|
|
|
public const string DownloadRepo_Default = @"hooverhigh/QuestAppLauncher_Assets/releases/latest";
|
|
|
|
// Background
|
|
public const string Background_Default = "default";
|
|
|
|
/// <summary>
|
|
/// Grid size
|
|
/// </summary>
|
|
[Serializable]
|
|
public class GridSize
|
|
{
|
|
public int rows = 3;
|
|
public int cols = 3;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Download repo type
|
|
/// </summary>
|
|
[Serializable]
|
|
public class DownloadRepo
|
|
{
|
|
public string repoUri;
|
|
public string type;
|
|
}
|
|
|
|
// Grid size, specified as cols x rows
|
|
public GridSize gridSize = new GridSize();
|
|
|
|
// Sort mode
|
|
public string sortMode = Sort_AZ;
|
|
|
|
// Whether to show 2D apps
|
|
public bool show2D = true;
|
|
|
|
// Auto Category: Apps are automatically categorized into 3 tabs - Quest, Go/GearVr, 2D
|
|
public string autoCategory = Category_Top;
|
|
|
|
// Custom Category: Apps are categorized according to appnames.json
|
|
public string customCategory = Category_Right;
|
|
|
|
// Whether to auto-download updates
|
|
public bool autoUpdate = true;
|
|
|
|
// Background image path
|
|
public string background = Background_Default;
|
|
|
|
// Download repos
|
|
public List<DownloadRepo> downloadRepos = new List<DownloadRepo>();
|
|
|
|
/// <summary>
|
|
/// Optional URL for the managed-policy endpoint.
|
|
/// When non-empty, policies are fetched from this URL on every startup.
|
|
/// Can also be set at compile time via the DEV_ENDPOINT scripting define.
|
|
/// </summary>
|
|
public string managedPolicyEndpoint = "";
|
|
|
|
public Config(bool initDefaults = false)
|
|
{
|
|
if (initDefaults)
|
|
{
|
|
// We must initialize any default collection values here. Otherwise, if we initialize them inline,
|
|
// we'll keep adding duplicate values whenever we persist via JSON.NET (since it invokes the default contructor as part
|
|
// of deserialization, which again adds the default value).
|
|
this.downloadRepos.Add(new DownloadRepo { repoUri = DownloadRepo_Default, type = DownloadRepo_Type_GitHub });
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Class responsible for loading / saving config into a config.json file.
|
|
/// </summary>
|
|
public class ConfigPersistence
|
|
{
|
|
// File name of app name overrides
|
|
const string ConfigFileName = "config.json";
|
|
|
|
/// <summary>
|
|
/// Load config from file
|
|
/// </summary>
|
|
/// <param name="config"></param>
|
|
/// <returns>Config object</returns>
|
|
static public Config LoadConfig()
|
|
{
|
|
var configFilePath = Path.Combine(UnityEngine.Application.persistentDataPath, ConfigFileName);
|
|
if (File.Exists(configFilePath))
|
|
{
|
|
Debug.Log("Found config file: " + configFilePath);
|
|
var jsonConfig = File.ReadAllText(configFilePath);
|
|
|
|
try
|
|
{
|
|
return JsonConvert.DeserializeObject<Config>(jsonConfig);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.Log(string.Format("Failed to read config: {0}", e.Message));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Did not find config file: " + configFilePath);
|
|
}
|
|
|
|
// Return default config
|
|
return new Config(true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Save config to a file
|
|
/// </summary>
|
|
/// <param name="config"></param>
|
|
static public void SaveConfig(Config config)
|
|
{
|
|
var configFilePath = Path.Combine(UnityEngine.Application.persistentDataPath, ConfigFileName);
|
|
Debug.Log("Saving config file: " + configFilePath);
|
|
|
|
try
|
|
{
|
|
File.WriteAllText(configFilePath, JsonConvert.SerializeObject(config, Formatting.Indented));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.Log(string.Format("Failed to write config: {0}", e.Message));
|
|
}
|
|
}
|
|
}
|
|
}
|