Skip to content

Commit

Permalink
Refactor of Package Service to fix 3am programming errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
TBN-MapleWheels committed Oct 4, 2024
1 parent 1a27ee2 commit 99959b0
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@

namespace Barotrauma.LuaCs.Data;

public partial interface IModConfigInfo
{
/// <summary>
/// Collection of loadable styles data.
/// </summary>
ImmutableArray<IStylesResourceInfo> StylesResourceInfos { get; }
}
public partial interface IModConfigInfo : IStylesResourcesInfo { }
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
namespace Barotrauma.LuaCs.Data;
using System.Collections.Immutable;

namespace Barotrauma.LuaCs.Data;

public interface IStylesResourceInfo : IResourceInfo, IResourceCultureInfo, ILazyLoadableResourceInfo, IPackageDependenciesInfo { }

public interface IStylesResourcesInfo
{
/// <summary>
/// Collection of loadable styles data.
/// </summary>
ImmutableArray<IStylesResourceInfo> StylesResourceInfos { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/// <summary>
/// Loads XML Style assets from the given content package.
/// </summary>
public interface IXmlAssetService : IService
public interface IStylesService : IService
{
/// <summary>
/// Tries to load the styles file for the given contentpackage and path into a new UIStylesProcessor instance.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
using System;
using System.Collections.Immutable;
using Barotrauma.LuaCs.Data;
using Barotrauma.LuaCs.Services.Processing;

namespace Barotrauma.LuaCs.Services;

public partial class PackageService
public partial class PackageService : IStylesResourcesInfo
{
private readonly Lazy<IXmlStylesToResConverterService> _stylesConverterService;
private readonly Lazy<IStylesService> _stylesService;

public PackageService(
Lazy<IXmlModConfigConverterService> converterService,
Lazy<ILegacyConfigService> legacyConfigService,
Lazy<IXmlLocalizationResConverterService> localizationConverterService,
Lazy<IXmlStylesToResConverterService> stylesConverterService,
Lazy<ILuaScriptService> luaScriptService,
Lazy<ILocalizationService> localizationService,
Lazy<IPluginService> pluginService,
Lazy<IStylesService> stylesService,
IPluginManagementService pluginManagementService,
IPackageManagementService packageManagementService,
IStorageService storageService,
ILoggerService loggerService)
{
_modConfigConverterService = converterService;
_legacyConfigService = legacyConfigService;
_localizationConverterService = localizationConverterService;
_stylesConverterService = stylesConverterService;
_luaScriptService = luaScriptService;
_localizationService = localizationService;
_pluginService = pluginService;
_stylesService = stylesService;
_pluginManagementService = pluginManagementService;
_packageManagementService = packageManagementService;
_storageService = storageService;
_loggerService = loggerService;
}
partial void TryParsePackageClient(ContentPackage package)

public ImmutableArray<IStylesResourceInfo> StylesResourceInfos => ModConfigInfo?.StylesResourceInfos ?? ImmutableArray<IStylesResourceInfo>.Empty;

public partial bool TryLoadStyles()
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace Barotrauma.LuaCs.Services;

public class XmlAssetService : IXmlAssetService
public class StylesService : IStylesService
{
private readonly Dictionary<string, UIStyleProcessor> _loadedProcessors = new();
private readonly IStorageService _storageService;
private readonly ILoggerService _loggerService;

public XmlAssetService(IStorageService storageService, ILoggerService loggerService)
public StylesService(IStorageService storageService, ILoggerService loggerService)
{
_storageService = storageService;
_loggerService = loggerService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
using System;
using Barotrauma.LuaCs.Services.Processing;

// ReSharper disable once CheckNamespace
namespace Barotrauma.LuaCs.Services;

public partial class PackageService
{
public PackageService(
Lazy<IXmlModConfigConverterService> converterService,
Lazy<ILegacyConfigService> legacyConfigService,
Lazy<IXmlLocalizationResConverterService> localizationConverterService,
Lazy<ILuaScriptService> luaScriptService,
Lazy<ILocalizationService> localizationService,
Lazy<IPluginService> pluginService,
IPluginManagementService pluginManagementService,
IPackageManagementService packageManagementService,
IStorageService storageService,
ILoggerService loggerService)
{
_modConfigConverterService = converterService;
_legacyConfigService = legacyConfigService;
_localizationConverterService = localizationConverterService;
_luaScriptService = luaScriptService;
_localizationService = localizationService;
_pluginService = pluginService;
_pluginManagementService = pluginManagementService;
_packageManagementService = packageManagementService;
_storageService = storageService;
_loggerService = loggerService;
}
// No implementation
partial void TryParsePackageClient(ContentPackage package) {}

// The server doesn't use styles data.
public partial bool TryLoadStyles()
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ namespace Barotrauma.LuaCs.Data;
public ContentPackage Package { get; init; }
public string PackageName { get; init; }
public TargetRunMode RunModes { get; init; }
public ImmutableArray<CultureInfo> SupportedCultures { get; init; }

// metadata
public ImmutableArray<CultureInfo> SupportedCultures { get; init; }
public ImmutableArray<IPackageDependencyInfo> Dependencies { get; init; }
public ImmutableArray<IAssemblyResourceInfo> LoadableAssemblies { get; init; }
public ImmutableArray<ILocalizationResourceInfo> LocalizationFiles { get; init; }
public ImmutableArray<IAssemblyResourceInfo> Assemblies { get; init; }
public ImmutableArray<ILocalizationResourceInfo> Localizations { get; init; }
public ImmutableArray<ILuaResourceInfo> LuaScripts { get; init; }
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface IResourceCultureInfo
/// <summary>
/// List of supported cultures by this resource.
/// </summary>
ImmutableArray<CultureInfo> SupportedCultures { get; init; }
ImmutableArray<CultureInfo> SupportedCultures { get; }
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

namespace Barotrauma.LuaCs.Data;

public partial interface IModConfigInfo : IPackageDependenciesInfo, IResourceCultureInfo
public partial interface IModConfigInfo : IPackageDependenciesInfo, IResourceCultureInfo, IAssembliesResourcesInfo, ILocalizationsResourcesInfo, ILuaScriptsResourcesInfo
{
// package info
ContentPackage Package { get; }
string PackageName { get; }

// loadable content metadata
ImmutableArray<IAssemblyResourceInfo> LoadableAssemblies { get; }
ImmutableArray<ILocalizationResourceInfo> LocalizationFiles { get; }

// configuration
TargetRunMode RunModes { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;

namespace Barotrauma.LuaCs.Data;
Expand All @@ -22,3 +23,23 @@ public interface IAssemblyResourceInfo : IResourceInfo, IResourceCultureInfo, IP
/// </summary>
public bool IsScript { get; }
}


#region Collections

public interface IAssembliesResourcesInfo
{
ImmutableArray<IAssemblyResourceInfo> Assemblies { get; }
}

public interface ILocalizationsResourcesInfo
{
ImmutableArray<ILocalizationResourceInfo> Localizations { get; }
}

public interface ILuaScriptsResourcesInfo
{
ImmutableArray<ILuaResourceInfo> LuaScripts { get; }
}

#endregion
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@

namespace Barotrauma.LuaCs.Services;

public interface IContentPackageService : IService
public interface IContentPackageService : IService,
// These allow us the pass the IContentPackageService to anything that needs the data without having to directly reference the member
IPackageDependenciesInfo, IResourceCultureInfo, IAssembliesResourcesInfo, ILocalizationsResourcesInfo, ILuaScriptsResourcesInfo
{
ContentPackage Package { get; }
IModConfigInfo ModConfigInfo { get; }
/// <summary>
/// Tries to parse a package to produce a working ModConfigInfo.
/// Try to load the XML config and resources information from the given package.
/// </summary>
/// <param name="package"></param>
/// <returns></returns>
bool TryParsePackage(ContentPackage package);
ContentPackage Package { get; }
IModConfigInfo ModConfigInfo { get; }
/// <returns>Whether the package was parsed without errors and any information was found. Will return false for purely vanilla packages.</returns>
bool TryLoadResourcesInfo(ContentPackage package);

bool TryLoadAssemblies();
bool TryLoadLocalizations();
bool TryLoadLuaScripts();
bool TryLoadStyles();
bool TryLoadConfig();
}

Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;

namespace Barotrauma.LuaCs.Services;

public class PackageManagementService : IPackageManagementService
public class PackageManagementService : IPackageManagementService, IPluginManagementService
{
private Func<IContentPackageService> GetPackageServiceInstance { get; init; }
private readonly Func<IContentPackageService> _contentPackageServiceFactory;

public PackageManagementService(Func<IContentPackageService> getPackageService)
{
this.GetPackageServiceInstance = getPackageService;
this._contentPackageServiceFactory = getPackageService;
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
using System;
using System.Collections.Immutable;
using System.Globalization;
using Barotrauma.LuaCs.Data;
using Barotrauma.LuaCs.Services.Processing;

namespace Barotrauma.LuaCs.Services;

public partial class PackageService : IContentPackageService
{
// mod config / package scanners/parsers
private readonly Lazy<IXmlModConfigConverterService> _modConfigConverterService;
private readonly Lazy<ILegacyConfigService> _legacyConfigService;
private readonly Lazy<IXmlLocalizationResConverterService> _localizationConverterService;
private readonly Lazy<ILuaScriptService> _luaScriptService;
private readonly Lazy<ILocalizationService> _localizationService;
private readonly Lazy<IPluginService> _pluginService;
private readonly IPluginManagementService _pluginManagementService;
private readonly IPackageManagementService _packageManagementService;
private readonly IStorageService _storageService;
private readonly ILoggerService _loggerService;

// cctor in server source and client source
// .ctor in server source and client source

public ContentPackage Package { get; private set; }

#region DataContracts

public IModConfigInfo ModConfigInfo { get; private set; }
public ImmutableArray<IPackageDependencyInfo> Dependencies => ModConfigInfo?.Dependencies ?? ImmutableArray<IPackageDependencyInfo>.Empty;
public ImmutableArray<CultureInfo> SupportedCultures => ModConfigInfo?.SupportedCultures ?? ImmutableArray<CultureInfo>.Empty;
public ImmutableArray<IAssemblyResourceInfo> Assemblies => ModConfigInfo?.Assemblies ?? ImmutableArray<IAssemblyResourceInfo>.Empty;
public ImmutableArray<ILocalizationResourceInfo> Localizations => ModConfigInfo?.Localizations ?? ImmutableArray<ILocalizationResourceInfo>.Empty;
public ImmutableArray<ILuaResourceInfo> LuaScripts => ModConfigInfo?.LuaScripts ?? ImmutableArray<ILuaResourceInfo>.Empty;

public bool TryParsePackage(ContentPackage package)
#endregion

public bool TryLoadResourcesInfo(ContentPackage package)
{
// try loading the ModConfig.xml. If it fails, use the Legacy loader to try and construct one from the package structure.
if (_storageService.TryLoadPackageXml(package, "ModConfig.xml", out var configXml)
&& configXml.Root is not null)
{
Expand All @@ -29,7 +46,7 @@ public bool TryParsePackage(ContentPackage package)
}
else
{
_loggerService.LogError($"Failed to parse ModConfig.xml for package {package.Name}");
_loggerService.LogError($"Failed to parse ModConfig.xml for package {package.Name}, package mod content not loaded.");
return false;
}
}
Expand All @@ -42,16 +59,36 @@ public bool TryParsePackage(ContentPackage package)
// vanilla mod or broken
return false;
}

// load client resources
TryParsePackageClient(package);
return true;

throw new NotImplementedException();
}

public bool TryLoadAssemblies()
{
throw new NotImplementedException();
}

public bool TryLoadLocalizations()
{
throw new NotImplementedException();
}

public bool TryLoadLuaScripts()
{
throw new NotImplementedException();
}

public partial bool TryLoadStyles();

public bool TryLoadConfig()
{
throw new NotImplementedException();
}

partial void TryParsePackageClient(ContentPackage package);

public void Dispose()
{
// TODO release managed resources here
throw new NotImplementedException();
}


}

This file was deleted.

0 comments on commit 99959b0

Please sign in to comment.