Skip to content

Commit

Permalink
1.1.12 - fixes and commands
Browse files Browse the repository at this point in the history
level lighting commands from 1.1.11
pet color commands from 1.1.10 / 1.1.11
fixed itemDump command
  • Loading branch information
BlazingTwist committed Oct 12, 2021
1 parent cc9197e commit e8cc9b9
Show file tree
Hide file tree
Showing 10 changed files with 518 additions and 92 deletions.
3 changes: 3 additions & 0 deletions SoD_BaseMod/SoD_BaseMod.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
<Compile Include="src\asm\EREelPatcher.cs" />
<Compile Include="src\asm\Extensions\EelRoastManagerExtensions.cs" />
<Compile Include="src\asm\Extensions\KAConsoleExtensions.cs" />
<Compile Include="src\asm\Extensions\SanctuaryPet_Extensions.cs" />
<Compile Include="src\asm\Extensions\UiAvatarControlsExtensions.cs" />
<Compile Include="src\asm\Extensions\UiBackpackExtensions.cs" />
<Compile Include="src\asm\Extensions\UiChestExtensions.cs" />
Expand Down Expand Up @@ -199,6 +200,8 @@
<Compile Include="src\basemod\console\commands\BTTweakDataCommand.cs" />
<Compile Include="src\basemod\console\commands\itemdumphandlers\FullItemDumpHandler.cs" />
<Compile Include="src\basemod\console\commands\itemdumphandlers\ItemStateDumpHandler.cs" />
<Compile Include="src\basemod\extensions\ColorExtensions.cs" />
<Compile Include="src\basemod\extensions\StringBuilderExtensions.cs" />
<Compile Include="src\SodBaseMod.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
15 changes: 15 additions & 0 deletions SoD_BaseMod/src/asm/Extensions/SanctuaryPet_Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;

namespace SoD_BaseMod.Extensions {
[HarmonyPatch(declaringType: typeof(SanctuaryPet))]
public static class SanctuaryPet_Extensions {
public static Dictionary<string, SkinnedMeshRenderer> GetRendererMap(this SanctuaryPet pet) {
return Traverse
.Create(pet)
.Field("mRendererMap")
.GetValue<Dictionary<string, SkinnedMeshRenderer>>();
}
}
}
164 changes: 86 additions & 78 deletions SoD_BaseMod/src/basemod/console/BTConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,91 @@ private static List<string> SplitInputCommand(string command) {
return result;
}

private static BTConsoleCommand GetBestMatchingCommand(string inputString, List<string> input, bool isHelpRequest, bool isMultilineCommand) {
BTConsoleCommand exactMatchCommand = commandList.FirstOrDefault(cmd => cmd.IsCommandMatchingExact(input));
if (exactMatchCommand != null) {
return exactMatchCommand;
}

Dictionary<int, List<BTConsoleCommand>> suggestionDict = new Dictionary<int, List<BTConsoleCommand>>();
int maxSuggestionStrength = 0;
foreach (BTConsoleCommand cmd in commandList) {
int suggestionStrength = cmd.ShowAsSuggestion(input);
if (suggestionStrength == 0) {
continue;
}

if (suggestionStrength > maxSuggestionStrength) {
maxSuggestionStrength = suggestionStrength;
}

if (!suggestionDict.ContainsKey(suggestionStrength)) {
suggestionDict.Add(suggestionStrength, new List<BTConsoleCommand>());
}

suggestionDict[suggestionStrength].Add(cmd);
}

if (maxSuggestionStrength == 0) {
WriteLine("unable to find command: '" + inputString + "'");
return null;
}

List<BTConsoleCommand> matchingCommands;
if (isHelpRequest) {
matchingCommands = commandList
.Where(cmd => cmd.IsFullNamespaceMatching(input))
.ToList();
} else {
matchingCommands = commandList
.Where(cmd => cmd.IsCommandMatching(input))
.ToList();
}

if (matchingCommands.Count == 0) {
if (isHelpRequest) {
WriteLine("unable to print help for command: '" + inputString + "' - no such command found");
return null;
}

if (isMultilineCommand) {
WriteLine("unable to find command: '" + inputString + "' - autocomplete is not supported for multiline-commands");
return null;
}

// no full matches -> autocomplete best suggestion
List<BTConsoleCommand> bestSuggestions = suggestionDict[maxSuggestionStrength];
if (bestSuggestions.Count > 1) {
WriteLine("Found multiple equally strong suggestions - can't autocomplete");
return null;
}

inputText = bestSuggestions[0].Autocomplete(input);
return null;
}

if (matchingCommands.Count == 1) {
return matchingCommands[0];
}
{ //count > 1
List<BTConsoleCommand> bestSuggestions = suggestionDict[maxSuggestionStrength];
List<BTConsoleCommand> bestMatchingCommands = matchingCommands
.Where(cmd => bestSuggestions.Contains(cmd))
.ToList();
int bestMatchingCommandsCount = bestMatchingCommands.Count;
switch (bestMatchingCommandsCount) {
case 0:
WriteLine("Can't identify command: '" + inputString + "'! BestSuggestions and MatchingCommands are disjointed (somehow)");
return null;
case 1:
return bestMatchingCommands.First();
default:
WriteLine("Can't identify command: '" + inputString + "'! Found multiple full matches of equal suggestionStrength!");
return null;
}
}
}

private static void OnCommandSubmitted(string multiCommand, bool addToHistory) {
if (addToHistory) {
AddCommandToHistory(multiCommand);
Expand All @@ -298,84 +383,7 @@ private static void OnCommandSubmitted(string multiCommand, bool addToHistory) {
}

List<string> input = SplitInputCommand(command);
Dictionary<int, List<BTConsoleCommand>> suggestionDict = new Dictionary<int, List<BTConsoleCommand>>();
int maxSuggestionStrength = 0;
foreach (BTConsoleCommand cmd in commandList) {
int suggestionStrength = cmd.ShowAsSuggestion(input);
if (suggestionStrength == 0) {
continue;
}

if (suggestionStrength > maxSuggestionStrength) {
maxSuggestionStrength = suggestionStrength;
}

if (!suggestionDict.ContainsKey(suggestionStrength)) {
suggestionDict.Add(suggestionStrength, new List<BTConsoleCommand>());
}

suggestionDict[suggestionStrength].Add(cmd);
}

if (maxSuggestionStrength == 0) {
WriteLine("unable to find command: '" + command + "'");
continue;
}

List<BTConsoleCommand> matchingCommands;
if (isHelpRequest) {
matchingCommands = commandList
.Where(cmd => cmd.IsFullNamespaceMatching(input))
.ToList();
} else {
matchingCommands = commandList
.Where(cmd => cmd.IsCommandMatching(input))
.ToList();
}

if (matchingCommands.Count == 0) {
if (isHelpRequest) {
WriteLine("unable to print help for command: '" + command + "' - no such command found");
continue;
}

if (inputCommandCount > 1) {
WriteLine("unable to find command: '" + command + "' - autocomplete is not supported for multiline-commands");
continue;
}

// no full matches -> autocomplete best suggestion
List<BTConsoleCommand> bestSuggestions = suggestionDict[maxSuggestionStrength];
if (bestSuggestions.Count > 1) {
WriteLine("Found multiple equally strong suggestions - can't autocomplete");
continue;
}

inputText = bestSuggestions[0].Autocomplete(input);
continue;
}

BTConsoleCommand bestMatchingCommand;
if (matchingCommands.Count == 1) {
bestMatchingCommand = matchingCommands[0];
} else { //count > 1
List<BTConsoleCommand> bestSuggestions = suggestionDict[maxSuggestionStrength];
List<BTConsoleCommand> bestMatchingCommands = matchingCommands
.Where(cmd => bestSuggestions.Contains(cmd))
.ToList();
int bestMatchingCommandsCount = bestMatchingCommands.Count;
switch (bestMatchingCommandsCount) {
case 0:
WriteLine("Can't identify command: '" + command + "'! BestSuggestions and MatchingCommands are disjointed (somehow)");
continue;
case 1:
bestMatchingCommand = bestMatchingCommands.First();
break;
default:
WriteLine("Can't identify command: '" + command + "'! Found multiple full matches of equal suggestionStrength!");
continue;
}
}
BTConsoleCommand bestMatchingCommand = GetBestMatchingCommand(command, input, isHelpRequest, inputCommandCount > 1);

inputText = "";
if (isHelpRequest) {
Expand Down
31 changes: 30 additions & 1 deletion SoD_BaseMod/src/basemod/console/BTConsoleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ public string Autocomplete(List<string> input) {
return resultBuilder.ToString();
}

public bool IsCommandMatchingExact(List<string> input) {
int inputCount = input.Count;
int namespaceCount = commandNamespace.Count;
int totalArgumentCount = commandInput.TotalArgumentCount();
int requiredArgumentCount = commandInput.RequiredArgumentCount();
if (inputCount < namespaceCount + requiredArgumentCount || inputCount > namespaceCount + totalArgumentCount) {
return false;
}

for (int index = 0; index < namespaceCount; index++) {
string inputString = input[index];
string namespaceString = commandNamespace[index];
if (inputString != namespaceString) {
return false;
}
}
return true;
}

public bool IsFullNamespaceMatching(List<string> input) {
int inputCount = input.Count;
int namespaceCount = commandNamespace.Count;
Expand Down Expand Up @@ -279,10 +298,20 @@ public void Reset() {
public void Consume(string value) {
object argument = valueType.IsEnum
? Enum.Parse(valueType, value, true)
: Convert.ChangeType(value, valueType, CultureInfo.InvariantCulture);
: valueType == typeof(int)
? ConvertIntegerLiteral(value)
: Convert.ChangeType(value, valueType, CultureInfo.InvariantCulture);
valueConsumer.Invoke(argument, true);
}

private static int ConvertIntegerLiteral(string value) {
return value.StartsWith("0x")
? Convert.ToInt32(value.Substring(2), 16)
: value.StartsWith("0b")
? Convert.ToInt32(value.Substring(2), 2)
: Convert.ToInt32(value, 10);
}

public string GetFormattedDisplayName() {
if (optional) {
return "<" + displayName + ">";
Expand Down
45 changes: 45 additions & 0 deletions SoD_BaseMod/src/basemod/console/commands/BTDebugCommands.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System.Text;
using KA.Framework;
using KnowledgeAdventure.Multiplayer.Utility;
using SoD_BaseMod.Extensions;
using UnityEngine;

namespace SoD_BaseMod.console {
Expand Down Expand Up @@ -59,6 +62,12 @@ public static void Register() {
"adds or clears a UtDebug._Mask",
OnExecuteDebugMask
));
BTConsole.AddCommand(new BTConsoleCommand(
new List<string> { "DebugCommand" },
new BTDebugCommandInput(),
"debug command used for executing test code on command",
OnExecuteDebugCommand
));
}

private static void OnExecuteDebugUnload(BTConsoleCommand.BTCommandInput input) {
Expand Down Expand Up @@ -236,5 +245,41 @@ private void SetAdd(object add, bool isPresent) {
};
}
}

private static void OnExecuteDebugCommand(BTConsoleCommand.BTCommandInput input) {
var cmdInput = (BTDebugCommandInput) input;
StringBuilder outputBuilder = new StringBuilder();
Dictionary<string, SkinnedMeshRenderer> rendererMap = SanctuaryManager.pCurPetInstance.GetRendererMap();
foreach (KeyValuePair<string, SkinnedMeshRenderer> skinnedMeshRenderer in rendererMap) {
outputBuilder.AppendLine($"Renderer key: '{skinnedMeshRenderer.Key}'");
if (skinnedMeshRenderer.Value == null) {
outputBuilder.AppendLine("\tValue is null.");
continue;
}
foreach (Material material in skinnedMeshRenderer.Value.materials) {
if (material == null) {
continue;
}
outputBuilder.AppendLine($"\tmaterial name: '{material.name}'");
outputBuilder.AppendLine(
$"\t\tshader keywords: '{(material.shaderKeywords != null ? string.Join("','", material.shaderKeywords) : "null")}'");
outputBuilder.AppendLine($"\t\tshader name: '{(material.shader != null ? material.shader.name : null)}'");
outputBuilder.AppendLine($"\t\tmain texture: '{(material.mainTexture != null ? material.mainTexture.name : "null")}'");
string[] texturePropertyNames = material.GetTexturePropertyNames();
if (texturePropertyNames != null) {
outputBuilder.AppendLine($"\t\tmaterial property names: '{string.Join("','", texturePropertyNames)}'");
}
}
}
outputBuilder.Append("==========\n");
outputBuilder.AppendLine($"Also, ProductSettings.pInstance._Resource: {ProductSettings.pInstance._Resource}");
BTConsole.WriteLine(outputBuilder.ToString());
}

private class BTDebugCommandInput : BTConsoleCommand.BTCommandInput {
protected override IEnumerable<BTConsoleCommand.BTConsoleArgument> BuildConsoleArguments() {
return new List<BTConsoleCommand.BTConsoleArgument>();
}
}
}
}
Loading

0 comments on commit e8cc9b9

Please sign in to comment.