Skip to content

Commit

Permalink
[PR] New build
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
wolfcomp authored Jan 8, 2024
2 parents cd88eac + 65c2c07 commit d05da4b
Show file tree
Hide file tree
Showing 18 changed files with 300 additions and 780 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ csharp_indent_labels = one_less_than_current
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_prefer_braces = true:silent
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_namespace_declarations = file_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,5 @@ ASALocalRun/
# BeatPulse healthcheck temp database
healthchecksdb

global.json
global.json
DeepDungeonDex/data.dat
11 changes: 10 additions & 1 deletion DeepDungeonDex/DeepDungeonDex.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
<DalamudLibPath Condition="$(DALAMUD_HOME) != ''">$(DALAMUD_HOME)/</DalamudLibPath>
</PropertyGroup>

<Import Project="$(DalamudLibPath)/targets/Dalamud.Plugin.targets"/>
<Import Project="$(DalamudLibPath)/targets/Dalamud.Plugin.targets" />

<ItemGroup>
<None Remove="data.dat" />
<None Remove="UnknownDebuf.png" />
</ItemGroup>

Expand All @@ -38,4 +39,12 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="data.dat" />
</ItemGroup>

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="dotnet run --project $(ProjectDir)..\DeepDungeonDexConsole\DeepDungeonDexConsole.csproj" />
</Target>

</Project>
36 changes: 3 additions & 33 deletions DeepDungeonDex/Models/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.IO;
using System.IO;

namespace DeepDungeonDex.Models;

public class Configuration : IBinaryLoadable
public class Configuration
{
public byte Version { get; } = 2;
public bool ClickThrough { get; set; }
Expand All @@ -24,7 +24,7 @@ public class Configuration : IBinaryLoadable
[JsonIgnore] public Action<Configuration>? OnChange { get; set; }
[JsonIgnore] public Action? OnSizeChange { get; set; }

public NamedType? Save(string path)
public void Save(string path)
{
if (FontSize != PrevFontSize)
{
Expand All @@ -37,20 +37,6 @@ public class Configuration : IBinaryLoadable
OnSizeChange?.Invoke();
}
OnChange?.Invoke(this);
return BinarySave(path);
}

public void Dispose()
{
}

public IBinaryLoadable StringLoad(string str)
{
throw new NotImplementedException();
}

public NamedType? BinarySave(string path)
{
var origPath = path;
if(!path.EndsWith(".tmp"))
path += ".tmp";
Expand All @@ -72,21 +58,5 @@ public IBinaryLoadable StringLoad(string str)
if (File.Exists(origPath))
File.Delete(origPath);
File.Move(path, origPath);
return null;
}

public IBinaryLoadable BinaryLoad(string path)
{
throw new NotImplementedException();
}

public Storage.Storage Load(string path)
{
throw new NotImplementedException();
}

public Storage.Storage Load(string path, string name)
{
throw new NotImplementedException();
}
}
21 changes: 4 additions & 17 deletions DeepDungeonDex/Models/SaveAndLoad.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
namespace DeepDungeonDex.Models;

public interface ISaveable : IDisposable
public interface ILoad : IDisposable
{
public NamedType? Save(string path);
public object Load(string str);
}

public interface ILoadable : ISaveable
public interface ILoad<out T> : ILoad where T : class
{
public Storage.Storage Load(string path);
public Storage.Storage Load(string path, string name);
}

public interface ILoadableString : ILoadable
{
public Storage.Storage Load(string str, bool fromFile);
}

public interface IBinaryLoadable : ILoadable
{
public IBinaryLoadable StringLoad(string str);
public NamedType? BinarySave(string path);
public IBinaryLoadable BinaryLoad(string path);
public new T Load(string str);
}

public class NamedType
Expand Down
52 changes: 31 additions & 21 deletions DeepDungeonDex/Requests/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public partial class Requests : IDisposable
public bool RequestingLang { get; private set; }
public bool IsRequesting => RequestingData || RequestingLang;


public Requests(StorageHandler handler, IPluginLog log)
{
Handler = handler;
_log = log;
handler.AddJsonStorage("index.json", GetFileList().Result!);
#pragma warning disable CS4014
_loadFileThread = new Thread(() => RefreshFileList());
_loadLangThread = new Thread(() => RefreshLang());
Expand All @@ -39,7 +39,7 @@ public Requests(StorageHandler handler, IPluginLog log)
{
_log.Verbose("Getting file list");
var content = await Get("index.json");
return JsonConvert.DeserializeObject<Dictionary<string, string[]>>(content);
return string.IsNullOrWhiteSpace(content) ? null : JsonConvert.DeserializeObject<Dictionary<string, string[]>>(content);
}
catch (Exception e)
{
Expand All @@ -50,15 +50,21 @@ public Requests(StorageHandler handler, IPluginLog log)

public async Task RefreshFileList(bool continuous = true)
{
StartRequest:
RequestingData = true;
var list = await GetFileList();
if (list == null)
{
_log.Error("Failed to get file list using precompiled data");
list = await GetFileListFromFile();
}
if (list == null)
goto RefreshEnd;

Handler.AddJsonStorage("index.json", list);
Handler.AddStorage("index.json", list);

_log.Verbose("Loading Types");
var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.GetInterfaces().Contains(typeof(ILoadableString))).ToArray();
var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.GetInterfaces().Contains(typeof(ILoad))).ToArray();

foreach (var (className, files) in list)
{
Expand All @@ -75,12 +81,9 @@ public async Task RefreshFileList(bool continuous = true)
if (string.IsNullOrWhiteSpace(content))
continue;
_log.Verbose("Creating instance");
var instance = (ILoadableString)Activator.CreateInstance(type)!;
var instance = (ILoad)Activator.CreateInstance(type)!;
_log.Verbose("Loading content");
if (instance is IBinaryLoadable loadable)
Handler.AddBinaryStorage(file.Replace(".yml", ".dat"), loadable.StringLoad(content));
else
Handler.AddYmlStorage(file, instance.Load(content, false));
Handler.AddStorage(file, instance.Load(content));
}
catch (Exception e)
{
Expand All @@ -89,31 +92,37 @@ public async Task RefreshFileList(bool continuous = true)
}
}

_log.Verbose("Loading complete saving storage");
Handler.Save();

RefreshEnd:
RequestingData = false;
if (continuous)
{
_log.Verbose($"Refreshing file list in {CacheTime:g}");
await Task.Delay(CacheTime, _token.Token);
if (!_token.IsCancellationRequested)
await RefreshFileList();
goto StartRequest;
}
}

public async Task<string> Get(string url)
public async Task<string?> Get(string url)
{
_log.Verbose($"Requesting {BaseUrl}/{url}");
var response = await Client.GetAsync($"{BaseUrl}/{url}");
_log.Verbose($"Response: {response.StatusCode}");
if (response.IsSuccessStatusCode)
try
{
_log.Verbose($"Requesting {BaseUrl}/{url}");
var response = await Client.GetAsync($"{BaseUrl}/{url}");
_log.Verbose($"Response: {response.StatusCode}");
if (response.IsSuccessStatusCode)
{
_log.Verbose("Reading string");
return await response.Content.ReadAsStringAsync();
}

throw new HttpRequestException($"Request: {url}\nFailed with status code {response.StatusCode}");
}
catch (Exception e)
{
_log.Verbose("Reading string");
return await response.Content.ReadAsStringAsync();
_log.Error(e, "Trying to get file from precompiled data");
return await GetFromFile(url);
}
throw new HttpRequestException($"Request: {url}\nFailed with status code {response.StatusCode}");
}

public void Dispose()
Expand All @@ -122,6 +131,7 @@ public void Dispose()
_loadFileThread.Join();
_loadLangThread.Join();
_token.Dispose();
_fileStream?.Dispose();
_log = null!;
}
}
49 changes: 49 additions & 0 deletions DeepDungeonDex/Requests/File.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeepDungeonDex;

public partial class Requests
{
private ZipArchive? _fileStream;

public async Task<string?> GetFromFile(string path)
{
if (_fileStream == null)
GetDat("DeepDungeonDex.data.dat");

try
{
path = path.Replace("/", "\\");
var entry = _fileStream!.Entries.FirstOrDefault(e => e.FullName == path);
if (entry == null)
return null;
await using var stream = entry.Open();
using var reader = new StreamReader(stream);
return await reader.ReadToEndAsync();
}
catch (Exception e)
{
_log.Error(e, $"Error while trying to read: {path}");
return null;
}
}

public async Task<Dictionary<string, string[]>?> GetFileListFromFile()
{
var content = await GetFromFile("index.json");
return content == null ? null : JsonConvert.DeserializeObject<Dictionary<string, string[]>>(content);
}

public void GetDat(string path)
{
var compStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path)!;
var stream = new ZipArchive(compStream, ZipArchiveMode.Read);
_fileStream = stream;
}
}
Loading

0 comments on commit d05da4b

Please sign in to comment.