refactor: new code style in DataManager.cs

This commit is contained in:
goat 2021-03-31 23:58:27 +02:00
parent d4ed340ad7
commit dc35fc6c3f

View file

@ -3,17 +3,14 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Dalamud.Data.LuminaExtensions;
using Lumina.Data; using Lumina.Data;
using Lumina.Data.Files; using Lumina.Data.Files;
using Lumina.Excel; using Lumina.Excel;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serilog; using Serilog;
using LuminaOptions = Lumina.LuminaOptions; using LuminaOptions = Lumina.LuminaOptions;
using ParsedFilePath = Lumina.ParsedFilePath; using ParsedFilePath = Lumina.ParsedFilePath;
@ -22,25 +19,9 @@ namespace Dalamud.Data
/// <summary> /// <summary>
/// This class provides data for Dalamud-internal features, but can also be used by plugins if needed. /// This class provides data for Dalamud-internal features, but can also be used by plugins if needed.
/// </summary> /// </summary>
public class DataManager : IDisposable { public class DataManager : IDisposable
/// <summary> {
/// OpCodes sent by the server to the client. private const string IconFileFormat = "ui/icon/{0:D3}000/{1}{2:D6}.tex";
/// </summary>
public ReadOnlyDictionary<string, ushort> ServerOpCodes { get; private set; }
/// <summary>
/// OpCodes sent by the client to the server.
/// </summary>
public ReadOnlyDictionary<string, ushort> ClientOpCodes { 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; }
/// <summary> /// <summary>
/// A <see cref="Lumina"/> object which gives access to any excel/game data. /// A <see cref="Lumina"/> object which gives access to any excel/game data.
@ -49,10 +30,12 @@ namespace Dalamud.Data
private ClientLanguage language; private ClientLanguage language;
private const string IconFileFormat = "ui/icon/{0:D3}000/{1}{2:D6}.tex";
private Thread luminaResourceThread; private Thread luminaResourceThread;
/// <summary>
/// Initializes a new instance of the <see cref="DataManager"/> class.
/// </summary>
/// <param name="language">The language to load data with by default.</param>
public DataManager(ClientLanguage language) public DataManager(ClientLanguage language)
{ {
// Set up default values so plugins do not null-reference when data is being loaded. // Set up default values so plugins do not null-reference when data is being loaded.
@ -61,6 +44,30 @@ namespace Dalamud.Data
this.language = language; this.language = language;
} }
/// <summary>
/// Gets the OpCodes sent by the server to the client.
/// </summary>
public ReadOnlyDictionary<string, ushort> ServerOpCodes { get; private set; }
/// <summary>
/// Gets the OpCodes sent by the client to the server.
/// </summary>
public ReadOnlyDictionary<string, ushort> ClientOpCodes { get; private set; }
/// <summary>
/// Gets an <see cref="ExcelModule"/> object which gives access to any of the game's sheet data.
/// </summary>
public ExcelModule Excel => this.gameData?.Excel;
/// <summary>
/// Gets a value indicating whether Game Data is ready to be read.
/// </summary>
public bool IsDataReady { get; private set; }
/// <summary>
/// Initialize this data manager.
/// </summary>
/// <param name="baseDir">The directory to load data from.</param>
public void Initialize(string baseDir) public void Initialize(string baseDir)
{ {
try try
@ -69,18 +76,18 @@ namespace Dalamud.Data
var zoneOpCodeDict = var zoneOpCodeDict =
JsonConvert.DeserializeObject<Dictionary<string, ushort>>(File.ReadAllText(Path.Combine(baseDir, "UIRes", "serveropcode.json"))); JsonConvert.DeserializeObject<Dictionary<string, ushort>>(File.ReadAllText(Path.Combine(baseDir, "UIRes", "serveropcode.json")));
ServerOpCodes = new ReadOnlyDictionary<string, ushort>(zoneOpCodeDict); this.ServerOpCodes = new ReadOnlyDictionary<string, ushort>(zoneOpCodeDict);
Log.Verbose("Loaded {0} ServerOpCodes.", zoneOpCodeDict.Count); Log.Verbose("Loaded {0} ServerOpCodes.", zoneOpCodeDict.Count);
var clientOpCodeDict = var clientOpCodeDict =
JsonConvert.DeserializeObject<Dictionary<string, ushort>>(File.ReadAllText(Path.Combine(baseDir, "UIRes", "clientopcode.json"))); JsonConvert.DeserializeObject<Dictionary<string, ushort>>(File.ReadAllText(Path.Combine(baseDir, "UIRes", "clientopcode.json")));
ClientOpCodes = new ReadOnlyDictionary<string, ushort>(clientOpCodeDict); this.ClientOpCodes = new ReadOnlyDictionary<string, ushort>(clientOpCodeDict);
Log.Verbose("Loaded {0} ClientOpCodes.", clientOpCodeDict.Count); Log.Verbose("Loaded {0} ClientOpCodes.", clientOpCodeDict.Count);
var luminaOptions = new LuminaOptions
var luminaOptions = new LuminaOptions { {
CacheFileResources = true, CacheFileResources = true,
#if DEBUG #if DEBUG
@ -94,30 +101,32 @@ namespace Dalamud.Data
ClientLanguage.English => Language.English, ClientLanguage.English => Language.English,
ClientLanguage.German => Language.German, ClientLanguage.German => Language.German,
ClientLanguage.French => Language.French, ClientLanguage.French => Language.French,
_ => throw new ArgumentOutOfRangeException(nameof(this.language), _ => throw new ArgumentOutOfRangeException(
"Unknown Language: " + this.language) nameof(this.language),
} "Unknown Language: " + this.language),
},
}; };
this.gameData = new Lumina.Lumina(Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), "sqpack"), luminaOptions); this.gameData = new Lumina.Lumina(Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), "sqpack"), luminaOptions);
Log.Information("Lumina is ready: {0}", gameData.DataPath); Log.Information("Lumina is ready: {0}", this.gameData.DataPath);
IsDataReady = true; this.IsDataReady = true;
this.luminaResourceThread = new Thread( () => this.luminaResourceThread = new Thread(() =>
{ {
while (true) while (true)
{ {
if (gameData.FileHandleManager.HasPendingFileLoads) if (this.gameData.FileHandleManager.HasPendingFileLoads)
{ {
gameData.ProcessFileHandleQueue(); this.gameData.ProcessFileHandleQueue();
} }
else else
{ {
Thread.Sleep(5); Thread.Sleep(5);
} }
} }
// ReSharper disable once FunctionNeverReturns // ReSharper disable once FunctionNeverReturns
}); });
this.luminaResourceThread.Start(); this.luminaResourceThread.Start();
@ -146,7 +155,8 @@ namespace Dalamud.Data
/// <param name="language">Language of the sheet to get.</param> /// <param name="language">Language of the sheet to get.</param>
/// <typeparam name="T">The excel sheet type to get.</typeparam> /// <typeparam name="T">The excel sheet type to get.</typeparam>
/// <returns>The <see cref="ExcelSheet{T}"/>, giving access to game rows.</returns> /// <returns>The <see cref="ExcelSheet{T}"/>, giving access to game rows.</returns>
public ExcelSheet<T> GetExcelSheet<T>(ClientLanguage language) where T : class, IExcelRow { public ExcelSheet<T> GetExcelSheet<T>(ClientLanguage language) where T : class, IExcelRow
{
var lang = language switch { var lang = language switch {
ClientLanguage.Japanese => Language.Japanese, ClientLanguage.Japanese => Language.Japanese,
ClientLanguage.English => Language.English, ClientLanguage.English => Language.English,
@ -170,7 +180,7 @@ namespace Dalamud.Data
/// <summary> /// <summary>
/// Get a <see cref="FileResource"/> with the given path, of the given type. /// Get a <see cref="FileResource"/> with the given path, of the given type.
/// </summary> /// </summary>
/// <typeparam name="T">The type of resource</typeparam> /// <typeparam name="T">The type of resource.</typeparam>
/// <param name="path">The path inside of the game files.</param> /// <param name="path">The path inside of the game files.</param>
/// <returns>The <see cref="FileResource"/> of the file.</returns> /// <returns>The <see cref="FileResource"/> of the file.</returns>
public T GetFile<T>(string path) where T : FileResource public T GetFile<T>(string path) where T : FileResource
@ -199,7 +209,7 @@ namespace Dalamud.Data
/// <returns>The <see cref="TexFile"/> containing the icon.</returns> /// <returns>The <see cref="TexFile"/> containing the icon.</returns>
public TexFile GetIcon(int iconId) public TexFile GetIcon(int iconId)
{ {
return GetIcon(this.language, iconId); return this.GetIcon(this.language, iconId);
} }
/// <summary> /// <summary>
@ -215,11 +225,10 @@ namespace Dalamud.Data
ClientLanguage.English => "en/", ClientLanguage.English => "en/",
ClientLanguage.German => "de/", ClientLanguage.German => "de/",
ClientLanguage.French => "fr/", ClientLanguage.French => "fr/",
_ => throw new ArgumentOutOfRangeException(nameof(this.language), _ => throw new ArgumentOutOfRangeException(nameof(this.language), "Unknown Language: " + this.language)
"Unknown Language: " + this.language)
}; };
return GetIcon(type, iconId); return this.GetIcon(type, iconId);
} }
/// <summary> /// <summary>
@ -247,6 +256,9 @@ namespace Dalamud.Data
#endregion #endregion
/// <summary>
/// Dispose this DataManager.
/// </summary>
public void Dispose() public void Dispose()
{ {
this.luminaResourceThread.Abort(); this.luminaResourceThread.Abort();