Skip to content

Commit

Permalink
Remove LoadControls
Browse files Browse the repository at this point in the history
  • Loading branch information
cooolbros committed Dec 11, 2023
1 parent 19c014a commit d5b1de0
Showing 1 changed file with 0 additions and 173 deletions.
173 changes: 0 additions & 173 deletions src/HUDMerger/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,179 +11,6 @@ namespace HUDMerger;

public static class Utilities
{
/// <summary>
/// Loads controls from a file the same way TF2 does.
/// </summary>
public static Dictionary<string, dynamic> LoadControls(string hudRoot, string relativeFilePath)
{
// #base files get loaded in order, with keys from the topmost files being used,
// then the keyvalues from the original file get applied over everything.

// values that are strings in base files will get overrided by objects
// loaded in a higher priority file

// values that are objects in base files will get overrided by string
// loaded in a higher priority file

// multiple objects in the same #base file will not override previous properties set,
// but multiple objects in the same origin file will overrie previous properties set.
//
// file1.res:
// Container
// {
// Element
// {
// "xpos" "10"
// "bgcolor_override" "0 255 0 255"
// }
// Element1 StringValue
// }
//
// file2.res:
// Container
// {
// Element
// {
// "xpos" "20"
// }
// }
//
// Origin file:
// #base file1.res
// #base file2.res
// Container
// {
// Element
// {
// "bgcolor_override" "255 0 0 255"
// }
// Element1
// {
// "ControlName" "EditablePanel"
// "fieldName" "Element1"
// "xpos" "0"
// "ypos" "0"
// "zpos" "10"
// "wide" "100"
// "tall" "100"
// "visible" "1"
// "enabled" "1"
// "bgcolor_override" "255 100 0 255"
// }
// }
//
// The origin file will be loaded as:
// Container
// {
// Element
// {
// "xpos" "20"
// "bgcolor_override" "255 0 0 255"
// }
// Element1
// {
// "ControlName" "EditablePanel"
// "fieldName" "Element1"
// "xpos" "0"
// "ypos" "0"
// "zpos" "10"
// "wide" "100"
// "tall" "100"
// "visible" "1"
// "enabled" "1"
// "bgcolor_override" "255 100 0 255"
// }
// }
//

Dictionary<string, dynamic> origin = new();

void AddControls(string _folderPath, string _fileName, bool overrideKeys)
{
string _filePath = Path.Join(_folderPath, _fileName);
Dictionary<string, dynamic> obj;
if (File.Exists(_filePath))
{
obj = VDFTryParse(_filePath);
}
else if (File.Exists(Path.Join("Resources\\HUD", _filePath)))
{
obj = VDFTryParse(Path.Join("Resources\\HUD", _filePath));
}
else
{
obj = new();
}

if (obj.ContainsKey("#base"))
{
List<string> baseFiles = new();

if (obj["#base"].GetType() == typeof(List<dynamic>))
{
foreach (dynamic baseFile in obj["#base"])
{
baseFiles.Add(baseFile);
}
}
else
{
baseFiles.Add(obj["#base"]);
}

string folderName = Path.GetDirectoryName(_filePath);

foreach (string baseFile in baseFiles)
{
AddControls(folderName, baseFile, false);
}

obj.Remove("#base");
}

Merge(origin, obj, overrideKeys);
}

Dictionary<string, dynamic> Merge(Dictionary<string, dynamic> obj1, Dictionary<string, dynamic> obj2, bool overrideKeys)
{
foreach (string i in obj2.Keys)
{
if (obj1.ContainsKey(i))
{
if (obj1[i].GetType() == typeof(Dictionary<string, dynamic>) && obj2[i].GetType() == typeof(Dictionary<string, dynamic>))
{
Merge(obj1[i], obj2[i], overrideKeys);
}
else
{
if (overrideKeys)
{
obj1[i] = obj2[i];
}
}
}
else
{
if (obj2[i] is List<dynamic> items)
{
obj1[i] = items.Aggregate((a, b) => a.GetType() == typeof(Dictionary<string, dynamic>) ? Merge(obj1[i], obj2[i], overrideKeys) : b);
}
else
{
// We dont need overrideKeys to write to object
obj1[i] = obj2[i];
}
}
}

return obj1;
}

FileInfo fileInfo = new FileInfo(Path.Join(hudRoot, relativeFilePath));
AddControls(fileInfo.Directory.FullName, fileInfo.Name, true);
return origin;
}

/// <summary>
/// Determines whether the specified file exists. Does not throw an error if any folder in the path doesn't exist.
/// </summary>
Expand Down

0 comments on commit d5b1de0

Please sign in to comment.