Skip to content

Commit

Permalink
Added "TryGetLocalTexturePath" and "GetLocalTexturePath" to "DatAsset…
Browse files Browse the repository at this point in the history
…Cache". Additional editorconfig fixes.
  • Loading branch information
dlamkins committed May 31, 2022
1 parent d2e1560 commit 519d5fb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
25 changes: 17 additions & 8 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,22 @@ resharper_csharp_align_multiline_parameter=true
resharper_csharp_align_multiple_declaration=true
resharper_csharp_blank_lines_around_field=0
resharper_csharp_empty_block_style=together
resharper_csharp_keep_blank_lines_in_code=1
resharper_csharp_max_line_length=240
resharper_csharp_stick_comment=false
resharper_csharp_wrap_after_declaration_lpar=true
resharper_csharp_wrap_after_invocation_lpar=true
resharper_csharp_wrap_after_declaration_lpar=false
resharper_csharp_wrap_after_invocation_lpar=false
resharper_csharp_wrap_before_binary_opsign=true
resharper_csharp_wrap_chained_method_calls=chop_if_long
resharper_csharp_wrap_lines=true
resharper_for_other_types=use_var_when_evident
resharper_indent_nested_foreach_stmt=true
resharper_indent_nested_for_stmt=true
resharper_indent_nested_usings_stmt=true
resharper_indent_nested_while_stmt=true
resharper_indent_preprocessor_if=usual_indent
resharper_indent_preprocessor_other=usual_indent
resharper_int_align=true
resharper_int_align_assignments=true
resharper_int_align_binary_expressions=true
resharper_int_align_comments=true
Expand All @@ -138,8 +141,9 @@ resharper_int_align_parameters=true
resharper_int_align_properties=true
resharper_int_align_switch_sections=true
resharper_int_align_variables=true
resharper_keep_existing_embedded_arrangement=false
resharper_keep_existing_expr_member_arrangement=false
resharper_keep_existing_attribute_arrangement=true
resharper_keep_existing_embedded_arrangement=true
resharper_keep_existing_expr_member_arrangement=true
resharper_keep_existing_invocation_parens_arrangement=false
resharper_max_enum_members_on_line=1
resharper_max_formal_parameters_on_line=8
Expand All @@ -152,16 +156,21 @@ resharper_new_line_before_finally=false
resharper_outdent_binary_ops=true
resharper_outdent_commas=true
resharper_outdent_dots=true
resharper_place_accessorholder_attribute_on_same_line=false
resharper_place_accessor_with_attrs_holder_on_single_line=true
resharper_place_field_attribute_on_same_line=false
resharper_place_linq_into_on_new_line=false
resharper_place_method_attribute_on_same_line=if_owner_is_single_line
resharper_place_method_attribute_on_same_line=false
resharper_place_simple_accessor_on_single_line=false
resharper_place_simple_initializer_on_single_line=false
resharper_place_type_attribute_on_same_line=if_owner_is_single_line
resharper_place_type_attribute_on_same_line=false
resharper_remove_blank_lines_near_braces_in_code=false
resharper_remove_blank_lines_near_braces_in_declarations=false
resharper_use_indent_from_vs=false
resharper_wrap_before_declaration_rpar=true
resharper_wrap_before_invocation_rpar=true
resharper_wrap_after_declaration_lpar=false
resharper_wrap_after_invocation_lpar=false
resharper_wrap_before_declaration_rpar=false
resharper_wrap_before_invocation_rpar=false
resharper_wrap_linq_expressions=chop_always

# ReSharper inspection severities
Expand Down
35 changes: 31 additions & 4 deletions Blish HUD/GameServices/Content/DatAssetCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public TextureReference(int sizeReference) {

private readonly string _assetCachePath;

public DatAssetCache(ContentService service) : base(service) {
internal DatAssetCache(ContentService service) : base(service) {
_assetCachePath = DirectoryUtil.RegisterDirectory(DirectoryUtil.CachePath, ASSETCACHE_PATH);

// We must load ASAP
Expand Down Expand Up @@ -172,15 +172,42 @@ private static async Task<Texture2D> LoadTextureFromServ(string path, int assetI
return TextureUtil.FromStreamPremultiplied(new MemoryStream(rawAsset));
}

private AsyncTexture2D LoadTexture(int assetId, TextureReference textureReference) {
/// <summary>
/// Returns the path of a cached asset texture based on its <paramref name="assetId"/>.
/// The path is returned regardless of if a cached copy of the texture actually exists.
/// Do not modify the texture at this path. If you wish to read this file, ensure you
/// specify <see cref="FileShare.ReadWrite"/> to avoid conflicting with the caching mechanism.
/// </summary>
public string GetLocalTexturePath(int assetId) {
string textureName = $"{assetId}.png";
string textureDir = Path.Combine(_assetCachePath, $"{textureName[0]}");
string localTexture = Path.Combine(textureDir, textureName);

Directory.CreateDirectory(textureDir);
try {
Directory.CreateDirectory(textureDir);
} catch (Exception ex) {
Logger.Warn(ex, "Failed to create directory for texture with path {texturePath}.", localTexture);
}

return localTexture;
}

/// <summary>
/// Returns <c>true</c> and assigns the local texture path to
/// <paramref name="texturePath"/> if the texture has been cached
/// locally or returns <c>false</c> if no texture is found locally.
/// </summary>
public bool TryGetLocalTexturePath(int assetId, out string texturePath) {
texturePath = GetLocalTexturePath(assetId);

return File.Exists(texturePath);
}

private AsyncTexture2D LoadTexture(int assetId, TextureReference textureReference) {
var texture = new AsyncTexture2D(_transparentTextures[textureReference.SizeReference]);

bool locallyCached = TryGetLocalTexturePath(assetId, out string localTexture);

async void HandleResponse(Task<Texture2D> textureResponse) {
var loadedTexture = ContentService.Textures.Error;

Expand Down Expand Up @@ -210,7 +237,7 @@ async void HandleResponse(Task<Texture2D> textureResponse) {
texture.SwapTexture(loadedTexture);
}

if (File.Exists(localTexture)) {
if (locallyCached) {
LoadTextureFromFileSystem(localTexture).ContinueWith(HandleResponse);
} else {
LoadTextureFromServ(localTexture, assetId).ContinueWith(HandleResponse);
Expand Down

0 comments on commit 519d5fb

Please sign in to comment.