Skip to content

Commit

Permalink
feat: CR2WJson: support guids read/write as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
nikich340 committed Jul 21, 2023
1 parent 29df528 commit c8a4846
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
5 changes: 4 additions & 1 deletion WolvenKit.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class Options
[Option("bytes_as_list", Required = false, HelpText = "Output byte array vars as int list (by default as base64 string)")]
public bool BytesAsIntList { get; set; } = false;

[Option("guids_as_strings", Required = false, HelpText = "Output CGUID vars as string values (by default as bytearray)")]
public bool GuidAsString { get; set; } = false;

[Option("ignore_embedded_cr2w", Required = false, HelpText = "Do NOT serialize embedded cr2w bytearrays - flatCompiledData, etc (serialized by default if possible)")]
public bool IgnoreEmbeddedCR2W { get; set; } = false;

Expand Down Expand Up @@ -99,7 +102,7 @@ static void RunCLIOptions(Options opts)
}
Stopwatch watch = new Stopwatch();
watch.Start();
var ToolOptions = new CR2WJsonToolOptions(opts.Verbose, opts.BytesAsIntList, opts.IgnoreEmbeddedCR2W);
var ToolOptions = new CR2WJsonToolOptions(opts.Verbose, opts.BytesAsIntList, opts.IgnoreEmbeddedCR2W, opts.GuidAsString);
if (opts.ExportJSON)
{
if (string.IsNullOrEmpty(opts.OutputPath))
Expand Down
23 changes: 19 additions & 4 deletions WolvenKit.CR2W/JSON/CR2WJsonTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ namespace WolvenKit.CR2W.JSON
public class CR2WJsonToolOptions
{
public CR2WJsonToolOptions() { }
public CR2WJsonToolOptions(bool verbose, bool bytesAsIntList, bool ignoreEmbeddedCR2W)
public CR2WJsonToolOptions(bool verbose, bool bytesAsIntList, bool ignoreEmbeddedCR2W, bool guidAsString)
{
Verbose = verbose;
BytesAsIntList = bytesAsIntList;
GuidAsString = guidAsString;
IgnoreEmbeddedCR2W = ignoreEmbeddedCR2W;
}

public bool Verbose { get; set; } = false;
public bool BytesAsIntList { get; set; } = false;
public bool GuidAsString { get; set; } = false;
public bool IgnoreEmbeddedCR2W { get; set; } = false;
}

Expand Down Expand Up @@ -342,9 +344,13 @@ protected static void DewalkNode(CVariable cvar, CR2WJsonObject node, Dictionary
{
cba.InternalType = scalar.type;
}
if (scalar.value is string base64str)
if (scalar.value is string str)
{
cvar.SetValue(Convert.FromBase64String(base64str));
if (cvar is CGUID && options.GuidAsString)
{
cvar.SetValue(str);
}
cvar.SetValue(Convert.FromBase64String(str));
}
else if (scalar.value is byte[] byteArray)
{
Expand Down Expand Up @@ -729,7 +735,16 @@ protected static CR2WJsonObject WalkNode(IEditableVariable node, string extensio
return WalkCR2W(extraCR2W, $"{extension}{node.REDName}::", logLevel + 1, options);
}
}
var primitiveValue = primitive.GetValueObject();
object primitiveValue = null;
if (options.GuidAsString && primitive is CGUID guid)
{
primitiveValue = guid.GuidString;
}
else
{
primitiveValue = primitive.GetValueObject();
}

if (options.Verbose)
{
Print($"{LogIndent(logLevel)}{node.REDName} ({node.REDType}) -> PRIMITIVE = {primitiveValue}");
Expand Down
11 changes: 7 additions & 4 deletions WolvenKit.CR2W/Types/Primitive/NetPrimitive/CGUID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public string GuidString
if (Guid.TryParse(value, out g))
{
guid = g.ToByteArray();
SetIsSerialized();
}
}
}
Expand Down Expand Up @@ -54,6 +55,10 @@ public override CVariable SetValue(object val)
this.guid = cvar.guid;
SetIsSerialized();
break;
case string str:
GuidString = str;
// ^ SetIsSerialized in setter on success
break;
}

return this;
Expand All @@ -70,13 +75,11 @@ public override CVariable Copy(CR2WCopyAction context)

public override string ToString()
{
if (guid != null && guid.Length > 0)
return new Guid(guid).ToString();
else
if (guid == null || guid.Length == 0)
{
guid = new byte[16];
return ToString();
}
return new Guid(guid).ToString();
}
}
}

0 comments on commit c8a4846

Please sign in to comment.