Skip to content

Instantly share code, notes, and snippets.

@FWDekker
Created January 8, 2019 17:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FWDekker/92d1dd8ed48705feacebc40d4ccc357b to your computer and use it in GitHub Desktop.
Save FWDekker/92d1dd8ed48705feacebc40d4ccc357b to your computer and use it in GitHub Desktop.
Serialises all selected records in xEdit to JSON
unit _SerialiseToJson;
var
outputLines: TStringList;
function Initialize: integer;
begin
outputLines := TStringList.Create;
outputLines.Add('{');
end;
function Process(e: IInterface): integer;
begin
outputLines.Add(' "' + IntToHex(FormID(e), 8) + '" : ' + Serialize(e, 1) + ',');
end;
function Finalize: integer;
begin
outputLines.Add('}');
if (outputLines.Count > 0) then
begin
outputLines.SaveToFile('fo76_dump_all.json');
end;
end;
function Serialize(e: IInterface; indentLevel: integer): string;
var
eType: Integer;
i: integer;
element: IInterface;
begin
eType := ElementType(e);
if ((Ord(eType) = Ord(etValue)) OR (Ord(eType) = Ord(etSubRecord))) then
begin
// Raw values
Result := '"' + EscapeJsonString(GetEditValue(e)) + '"';
end
else if ((Ord(eType) = Ord(etMainRecord)) OR (Ord(eType) = Ord(etStruct)) OR (Ord(eType) = Ord(etSubRecordStruct))) then
begin
// Objects
Result := #10 + Indent(indentLevel) + '{';
for i := 0 to ElementCount(e) - 1 do
begin
element := ElementByIndex(e, i);
Result := Result + #10 + Indent(indentLevel + 1)
+ '"' + Name(element) + '" : '
+ Serialize(element, indentLevel + 1);
if (i < (ElementCount(e) - 1)) then
begin
Result := Result + ',';
end;
end;
Result := Result + #10 + Indent(indentLevel) + '}';
end
else if ((Ord(eType)) = Ord(etSubRecordArray)) then
begin
// Arrays
Result := #10 + Indent(indentLevel) + '[';
for i := 0 to ElementCount(e) - 1 do
begin
Result := Result + #10 + Indent(indentLevel + 1) + Serialize(ElementByIndex(e, i), indentLevel + 1);
if (i < (ElementCount(e) - 1)) then
begin
Result := Result + ',';
end;
end;
Result := Result + #10 + Indent(indentLevel) + ']';
end
else
begin
AddMessage('UNKNOWN TYPE!!! ' + Name(e) + ' // ' + etToString(ElementType(e)));
Result := '"<!>' + etToString(ElementType(e)) + '</!>"';
end;
end;
function Indent(indentLevel: integer): string;
var
i: integer;
begin
Result := '';
for i := 0 to (indentLevel - 1) do
begin
Result := Result + ' ';
end;
end;
function EscapeJsonString(s: string): string;
begin
Result := s;
Result := StringReplace(Result, '\', '\\', [rfReplaceAll]);
Result := StringReplace(Result, '"', '\"', [rfReplaceAll]);
Result := StringReplace(Result, #13 + #10, '\n', [rfReplaceAll]);
Result := StringReplace(Result, #10, '\n', [rfReplaceAll]);
end;
function etToString(et: TwbElementType): string;
begin
case Ord(et) of
Ord(etFile): Result := 'etFile';
Ord(etMainRecord): Result := 'etMainRecord';
Ord(etGroupRecord): Result := 'etGroupRecord';
Ord(etSubRecord): Result := 'etSubRecord';
Ord(etSubRecordStruct): Result := 'etSubRecordStruct';
Ord(etSubRecordArray): Result := 'etSubRecordArray';
Ord(etSubRecordUnion): Result := 'etSubRecordUnion';
Ord(etArray): Result := 'etArray';
Ord(etStruct): Result := 'etStruct';
Ord(etValue): Result := 'etValue';
Ord(etFlag): Result := 'etFlag';
Ord(etStringListTerminator): Result := 'etStringListTerminator';
Ord(etUnion): Result := 'etUnion';
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment