feat: add lumina to DataManager

This commit is contained in:
goat 2020-02-16 20:06:28 +09:00
parent c069f75fba
commit e1b29a2797
7 changed files with 733 additions and 27 deletions

View file

@ -1,12 +1,18 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Lumina.Data;
using Lumina.Excel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serilog;
using LuminaOptions = Lumina.LuminaOptions;
using ParsedFilePath = Lumina.ParsedFilePath;
namespace Dalamud.Data
{
@ -16,22 +22,41 @@ namespace Dalamud.Data
public class DataManager {
private const string DataBaseUrl = "https://goaaats.github.io/ffxiv/tools/launcher/addons/Hooks/Data/";
public ReadOnlyDictionary<string, ushort> ServerOpCodes;
public ReadOnlyDictionary<uint, JObject> ContentFinderCondition;
public ReadOnlyDictionary<string, ushort> ServerOpCodes { get; private set; }
/// <summary>
/// An <see cref="ExcelModule"/> object which gives access to any of the game's sheet data.
/// </summary>
public ExcelModule Excel => this.gameData?.Excel;
/// <summary>
/// Indicates whether Game Data is ready to be read.
/// </summary>
public bool IsDataReady { get; private set; }
public DataManager() {
/// <summary>
/// A <see cref="Lumina"/> object which gives access to any excel/game data.
/// </summary>
private Lumina.Lumina gameData;
private ClientLanguage language;
public DataManager(ClientLanguage language)
{
// Set up default values so plugins do not null-reference when data is being loaded.
this.ServerOpCodes = new ReadOnlyDictionary<string, ushort>(new Dictionary<string, ushort>());
this.ContentFinderCondition = new ReadOnlyDictionary<uint, JObject>(new Dictionary<uint, JObject>());
this.language = language;
}
public async Task Initialize() {
try {
public async Task Initialize()
{
try
{
Log.Verbose("Starting data download...");
using var client = new HttpClient() {
using var client = new HttpClient()
{
BaseAddress = new Uri(DataBaseUrl)
};
@ -42,16 +67,79 @@ namespace Dalamud.Data
Log.Verbose("Loaded {0} ServerOpCodes.", opCodeDict.Count);
var cfcs = JsonConvert.DeserializeObject<Dictionary<uint, JObject>>(
await client.GetStringAsync(DataBaseUrl + "contentfindercondition.json"));
this.ContentFinderCondition = new ReadOnlyDictionary<uint, JObject>(cfcs);
Log.Verbose("Loaded {0} ContentFinderCondition.", cfcs.Count);
var luminaOptions = new LuminaOptions
{
CacheFileResources = true
};
switch (this.language)
{
case ClientLanguage.Japanese:
luminaOptions.DefaultExcelLanguage = Language.Japanese;
break;
case ClientLanguage.English:
luminaOptions.DefaultExcelLanguage = Language.English;
break;
case ClientLanguage.German:
luminaOptions.DefaultExcelLanguage = Language.German;
break;
case ClientLanguage.French:
luminaOptions.DefaultExcelLanguage = Language.French;
break;
default:
throw new ArgumentOutOfRangeException(nameof(this.language), "Unknown Language: " + this.language);
}
gameData = new Lumina.Lumina(Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), "sqpack"), luminaOptions);
Log.Information("Lumina is ready: {0}", gameData.DataPath);
IsDataReady = true;
} catch (Exception ex) {
}
catch (Exception ex)
{
Log.Error(ex, "Could not download data.");
}
}
#region Lumina Wrappers
/// <summary>
/// Get an <see cref="ExcelSheet{T}"/> with the given Excel sheet row type.
/// </summary>
/// <typeparam name="T">The excel sheet type to get.</typeparam>
/// <returns>The <see cref="ExcelSheet{T}"/>, giving access to game rows.</returns>
public ExcelSheet<T> GetExcelSheet<T>() where T : IExcelRow
{
return this.Excel.GetSheet<T>();
}
/// <summary>
/// Get a <see cref="FileResource"/> with the given path.
/// </summary>
/// <param name="path">The path inside of the game files.</param>
/// <returns>The <see cref="FileResource"/> of the file.</returns>
public FileResource GetFile(string path)
{
return this.GetFile<FileResource>(path);
}
/// <summary>
/// Get a <see cref="FileResource"/> with the given path, of the given type.
/// </summary>
/// <typeparam name="T">The type of resource</typeparam>
/// <param name="path">The path inside of the game files.</param>
/// <returns>The <see cref="FileResource"/> of the file.</returns>
public T GetFile<T>(string path) where T : FileResource
{
ParsedFilePath filePath = Lumina.Lumina.ParseFilePath(path);
if (filePath == null)
return default(T);
Repository repository;
return this.gameData.Repositories.TryGetValue(filePath.Repository, out repository) ? repository.GetFile<T>(filePath.Category, filePath) : default(T);
}
#endregion
}
}