Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 67 additions & 86 deletions Runtime/Core/Assets/PatchingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
using PatchManager.Core.Cache.Json;
using PatchManager.Core.Utility;
using PatchManager.SassyPatching.Execution;
using PatchManager.SassyPatching.Interfaces;
using PatchManager.Shared;
using PatchManager.Shared.Interfaces;
using SpaceWarp.API.Mods;
using UniLinq;
using UnityEngine;
Expand All @@ -20,14 +20,12 @@ namespace PatchManager.Core.Assets
{
internal static class PatchingManager
{
internal static readonly List<ITextPatcher> Patchers = new();
internal static readonly List<ITextAssetGenerator> Generators = new();
internal static Universe Universe;

private static readonly PatchHashes CurrentPatchHashes = PatchHashes.CreateDefault();

private static int _initialLibraryCount;
private static Dictionary<string, List<(string name, string text)>> _createdAssets = new();
private static Dictionary<string, List<(string name, ISelectable data)>> _createdAssets = new();

internal static int TotalPatchCount;
internal static int TotalErrorCount;
Expand All @@ -36,81 +34,74 @@ public static void GenerateUniverse(HashSet<string> singleFileModIds)
{
var loadedPlugins = PluginList.AllEnabledAndActivePlugins.Select(x => x.Guid).ToList();
loadedPlugins.AddRange(singleFileModIds);
Universe = new(RegisterPatcher, Logging.LogError, Logging.LogMessage, RegisterGenerator,
Universe = new(Logging.LogError, Logging.LogMessage,
loadedPlugins);
_initialLibraryCount = Universe.AllLibraries.Count;
}

private static void RegisterPatcher(ITextPatcher patcher)
{
for (var index = 0; index < Patchers.Count; index++)
{
if (Patchers[index].Priority <= patcher.Priority)
{
continue;
}
// private static void RegisterPatcher(ITextPatcher patcher)
// {
// for (var index = 0; index < Patchers.Count; index++)
// {
// if (Patchers[index].Priority <= patcher.Priority)
// {
// continue;
// }
//
// Patchers.Insert(index, patcher);
// return;
// }
//
// Patchers.Add(patcher);
// }
//
// private static void RegisterGenerator(ITextAssetGenerator generator)
// {
// for (var index = 0; index < Generators.Count; index++)
// {
// if (Generators[index].Priority <= generator.Priority)
// {
// continue;
// }
//
// Generators.Insert(index, generator);
// return;
// }
//
// Generators.Add(generator);
// }

Patchers.Insert(index, patcher);
return;
}

Patchers.Add(patcher);
}

private static void RegisterGenerator(ITextAssetGenerator generator)
private static string PatchJson(string label, string assetName, string text)
{
for (var index = 0; index < Generators.Count; index++)
{
if (Generators[index].Priority <= generator.Priority)
{
continue;
}
Logging.LogInfo($"Patching {label}:{assetName}");

Generators.Insert(index, generator);
return;
text = Universe.RunAllPatchesFor(label, assetName, text, out var patchCount, out var errorCount);
TotalErrorCount += errorCount;
TotalPatchCount += patchCount;
if (patchCount > 0)
{
Logging.LogInfo($"Patched {label}:{assetName} with {patchCount} patches. Total: {TotalPatchCount}");
}

Generators.Add(generator);
return text;
}

private static string PatchJson(string label, string assetName, string text)
private static string PatchJson(string label, string assetName, ISelectable data)
{
Console.WriteLine($"Patching {label}:{assetName}");
var patchCount = 0;

foreach (var patcher in Patchers)
{
var backup = text;
try
{
var wasPatched = patcher.TryPatch(label, assetName, ref text);
if (wasPatched)
{
patchCount++;
}
}
catch (Exception e)
{
TotalErrorCount += 1;
Console.WriteLine($"Patch of {label}:{assetName} errored due to: {e}");
text = backup;
}

if (text == "")
{
break;
}
}
Logging.LogInfo($"Patching {label}:{assetName}");

var text = Universe.RunAllPatchesFor(label, assetName, data, out var patchCount, out var errorCount);
TotalErrorCount += errorCount;
TotalPatchCount += patchCount;
if (patchCount > 0)
{
Console.WriteLine($"Patched {label}:{assetName} with {patchCount} patches. Total: {TotalPatchCount}");
Logging.LogInfo($"Patched {label}:{assetName} with {patchCount} patches. Total: {TotalPatchCount}");
}

return text;
}


private static int _previousLibraryCount = -1;

public static void ImportModPatches(string modName, string modFolder)
Expand Down Expand Up @@ -150,8 +141,8 @@ public static void RegisterPatches()
{
Logging.LogInfo($"Registering all patches!");
Universe.RegisterAllPatches();
Logging.LogInfo($"{Patchers.Count} patchers registered!");
Logging.LogInfo($"{Generators.Count} generators registered!");
Logging.LogInfo($"{Universe.TotalPatchCount} patchers registered!");
Logging.LogInfo($"{Universe.Generators.Count} generators registered!");
}

/// <summary>
Expand Down Expand Up @@ -241,7 +232,7 @@ private static AsyncOperationHandle<IList<TextAsset>> RebuildCache(string label)
}
catch (Exception e)
{
Console.WriteLine($"Unable to patch {asset.name} due to: {e.Message}");
Logging.LogError($"Unable to patch {asset.name} due to: {e.Message}, {e.StackTrace}");
}
});

Expand All @@ -261,7 +252,7 @@ void SaveArchive()
CacheManager.Inventory.CacheEntries.AddRangeUnique(assetsCacheEntries);
CacheManager.SaveInventory();

Console.WriteLine($"Cache for label '{label}' rebuilt.");
Logging.LogInfo($"Cache for label '{label}' rebuilt.");
}

if (handle.Status == AsyncOperationStatus.Failed && !unchanged)
Expand All @@ -285,21 +276,21 @@ void SaveArchive()

public static void CreateNewAssets(Action resolve, Action<string> reject)
{
foreach (var generator in Generators)
foreach (var generator in Universe.Generators)
{
try
{
var text = generator.Create(out var label, out var name);
Logging.LogDebug($"Generated an asset with the label {label}, and name {name}:\n{text}");
var data = generator.Create(out var label, out var name);
Logging.LogDebug($"Generated an asset with the label {label}, and name {name}:\n{data}");

if (!_createdAssets.ContainsKey(label))
{
_createdAssets[label] = new List<(string name, string text)>();
_createdAssets[label] = new List<(string name, ISelectable data)>();
}

if (!_createdAssets[label].Any(x => x.name == name))
{
_createdAssets[label].Add((name, text));
_createdAssets[label].Add((name, data));
}

}
Expand Down Expand Up @@ -327,29 +318,19 @@ GenericFlowAction CreateIndexedFlowAction(int idx)
(resolve2, _) =>
{
var handle = RebuildCache(distinctKeys[idx]);
var killTips = false;
if (idx + 1 < distinctKeys.Count)
{
GameManager.Instance.LoadingFlow.FlowActions.Insert(
GameManager.Instance.LoadingFlow.flowIndex + 1,
CreateIndexedFlowAction(idx + 1)
);
}
else
{
killTips = true;
}

CoroutineUtil.Instance.DoCoroutine(WaitForCacheRebuildSingleHandle(handle, resolve2, killTips));
CoroutineUtil.Instance.DoCoroutine(WaitForCacheRebuildSingleHandle(handle, resolve2, idx + 1 == distinctKeys.Count));
});
}

if (distinctKeys.Count > 0)
{
GameManager.Instance.LoadingFlow.FlowActions.Insert(
GameManager.Instance.LoadingFlow.flowIndex + 1,
CreateIndexedFlowAction(0)
);
for (var i = distinctKeys.Count - 1; i >= 0; i--)
{
GameManager.Instance.LoadingFlow.FlowActions.Insert(
GameManager.Instance.LoadingFlow.flowIndex + 1,
CreateIndexedFlowAction(i)
);
}
}

resolve();
Expand Down
4 changes: 2 additions & 2 deletions Runtime/Core/CoreModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ public override VisualElement GetDetails()
visible = true
};
var text = new TextElement();
text.text += $"Amount of loaded patchers: {PatchingManager.Patchers.Count}\n";
text.text += $"Amount of loaded generators: {PatchingManager.Generators.Count}\n";
text.text += $"Amount of loaded patchers: {PatchingManager.Universe.TotalPatchCount}\n";
text.text += $"Amount of loaded generators: {PatchingManager.Universe.Generators.Count}\n";
text.text += $"Amount of loaded libraries: {PatchingManager.Universe.AllLibraries.Count}\n";
if (_wasCacheInvalidated)
{
Expand Down
11 changes: 11 additions & 0 deletions Runtime/Generic/SassyPatching/Rulesets/JsonRuleset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ public bool Matches(string label)
return true;
}

public string[] Labels => null;

/// <inheritdoc />
public ISelectable ConvertToSelectable(string type, string name, string jsonData)
{
return new JTokenSelectable(() => { }, JToken.Parse(jsonData), name, type);
}

public bool CanIngestSelectable(ISelectable selectable) => selectable is JTokenSelectable;

public bool CanGetAssetNameFromSelectableName => false;

public string SelectableNameToAssetName(string selectableName)
{
throw new System.NotImplementedException();
}

/// <inheritdoc />
public INewAsset CreateNew(List<DataValue> dataValues)
{
Expand Down
12 changes: 11 additions & 1 deletion Runtime/Missions/Rulesets/MissionRuleset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@ namespace PatchManager.Missions.Rulesets
public class MissionRuleset : IPatcherRuleSet
{
/// <inheritdoc/>
public bool Matches(string label) => label == "missions";
public string[] Labels => new[] { "missions" };

/// <inheritdoc/>
public ISelectable ConvertToSelectable(string type, string name, string jsonData) =>
new MissionSelectable(JObject.Parse(jsonData));

public bool CanIngestSelectable(ISelectable selectable) => selectable is MissionSelectable
{
Deleted: false
};

// TODO: Validate
public bool CanGetAssetNameFromSelectableName => true;

public string SelectableNameToAssetName(string selectableName) => selectableName;

/// <inheritdoc/>
public INewAsset CreateNew(List<DataValue> dataValues)
{
Expand Down
15 changes: 13 additions & 2 deletions Runtime/Missions/Selectables/ActionsSelectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public sealed class ActionsSelectable : BaseSelectable
/// </summary>
public JArray Actions;

public void SetModified()
{
Selectable.SetModified();
}

public override bool WasModified => Selectable.WasModified;
public override void ClearModified()
{
}

private static string TrimTypeName(string typeName)
{
var comma = typeName.IndexOf(',');
Expand Down Expand Up @@ -97,11 +107,12 @@ public override bool IsSameAs(ISelectable other) =>
other is ActionsSelectable actionsSelectable && actionsSelectable.Actions == Actions;

/// <inheritdoc />
public override IModifiable OpenModification() => new JTokenModifiable(Actions, Selectable.SetModified);
public override IModifiable OpenModification() => new JTokenModifiable(Actions, SetModified);

/// <inheritdoc />
public override ISelectable AddElement(string elementType)
{
SetModified();
var actualType = MissionsTypes.Actions[elementType];
var elementObject = new JObject()
{
Expand All @@ -112,7 +123,7 @@ public override ISelectable AddElement(string elementType)
elementObject[key] = value;
}

var selectable = new JTokenSelectable(Selectable.SetModified, elementObject,
var selectable = new JTokenSelectable(SetModified, elementObject,
token => TrimTypeName(((JObject)token)!.Value<string>()!), elementType);
Children.Add(selectable);
Classes.Add(elementType);
Expand Down
15 changes: 13 additions & 2 deletions Runtime/Missions/Selectables/ConditionSetSelectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public sealed class ConditionSetSelectable : BaseSelectable
/// </summary>
private JArray _children;

public void SetModified()
{
MissionSelectable.SetModified();
}

public override bool WasModified => MissionSelectable.WasModified;
public override void ClearModified()
{
}

/// <summary>
/// Create a new condition set selectable
/// </summary>
Expand Down Expand Up @@ -97,11 +107,12 @@ public override bool IsSameAs(ISelectable other) => other is ConditionSetSelecta
conditionSetSelectable.ConditionSet == ConditionSet;

/// <inheritdoc />
public override IModifiable OpenModification() => new JTokenModifiable(ConditionSet, MissionSelectable.SetModified);
public override IModifiable OpenModification() => new JTokenModifiable(ConditionSet, SetModified);

/// <inheritdoc />
public override ISelectable AddElement(string elementType)
{
SetModified();
var conditionType = MissionsTypes.Conditions[elementType];
var conditionObject = new JObject()
{
Expand All @@ -121,7 +132,7 @@ public override ISelectable AddElement(string elementType)
}
else
{
var selectable = new JTokenSelectable(MissionSelectable.SetModified, conditionObject,
var selectable = new JTokenSelectable(SetModified, conditionObject,
"scriptableCondition", "scriptableCondition");
Children.Add(selectable);
return selectable;
Expand Down
Loading
Loading