Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements ImportText() #1706

Closed
wants to merge 5 commits into from
Closed
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
48 changes: 48 additions & 0 deletions Content.Tests/DMProject/Tests/Savefile/ImportText.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/obj/savetest

/proc/RunTest()
var/obj/savetest/O = new() //create a test object
O.name = "test"

var/savefile/F = new() //create a temporary savefile

F["dir"] = O
F["dir2"] = "object(\".0\")"
F["dir3"] = 1080
F["dir4"] = "the afternoon of the 3rd"
F["dir6/subdir6"] = 321
F["dir7"] = null
F["dir8"] = "double entry 1 = 5; double entry 2 = 10"
F["list"] << list("1",2,"three"=3,4, new /datum(), new /datum(), list(1,2,3, new /datum()))

var/total_export = F.ExportText()

var/savefile/F2 = new()
F2.ImportText("/",total_export)
//object test
ASSERT(F2["dir"].name == "test")
//string test
ASSERT(F2["dir2"] == F["dir2"])
ASSERT(F2["dir4"] == F["dir4"])
//int test
ASSERT(F2["dir3"] == F["dir3"])
//null test
ASSERT(F2["dir7"] == null)
//subdir test
ASSERT(F2["dir6"] == null)
ASSERT(F2["dir6/subdir6"] == 321)
//list test
ASSERT(F2["list"][1] == "1")
ASSERT(F2["list"][2] == 2)
ASSERT(F2["list"]["three"] == 3)
ASSERT(F2["list"][4] == 4)
ASSERT(istype(F2["list"][5], /datum))
ASSERT(istype(F2["list"][6], /datum))
//nested list
ASSERT(F2["list"][7][1] == 1)
ASSERT(F2["list"][7][2] == 2)
ASSERT(F2["list"][7][3] == 3)
ASSERT(istype(F2["list"][7][4], /datum))
//multiple entry test
ASSERT(F2["dir8"]["double entry 1"] == 5)
ASSERT(F2["dir8"]["double entry 2"] == 10)
1 change: 1 addition & 0 deletions Content.Tests/DMProject/Tests/Savefile/ImportTextFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
textfile_import_test = 2
1 change: 0 additions & 1 deletion DMCompiler/DMStandard/Types/Savefile.dm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
proc/ExportText(path = cd, file)

proc/ImportText(path = cd, source)
set opendream_unimplemented = TRUE

proc/Lock(timeout)
set opendream_unimplemented = TRUE
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public static void SetupNativeProcs(DreamObjectTree objectTree) {
objectTree.SetNativeProc(objectTree.Icon, DreamProcNativeIcon.NativeProc_Turn);

objectTree.SetNativeProc(objectTree.Savefile, DreamProcNativeSavefile.NativeProc_ExportText);
objectTree.SetNativeProc(objectTree.Savefile, DreamProcNativeSavefile.NativeProc_ImportText);
objectTree.SetNativeProc(objectTree.Savefile, DreamProcNativeSavefile.NativeProc_Flush);

objectTree.SetNativeProc(objectTree.World, DreamProcNativeWorld.NativeProc_Export);
Expand Down
111 changes: 111 additions & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeSavefile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,117 @@ private static string ExportTextInternalListFormat(DreamObjectSavefile.SFDreamJs

}

[DreamProc("ImportText")]
[DreamProcParameter("path", Type = DreamValueTypeFlag.String)]
[DreamProcParameter("source", Type = DreamValueTypeFlag.String | DreamValueTypeFlag.DreamResource)]
public static DreamValue NativeProc_ImportText(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
var savefile = (DreamObjectSavefile)src!;
DreamValue path = bundle.GetArgument(0, "path");
DreamValue source = bundle.GetArgument(1, "source");

string oldPath = savefile.CurrentPath;
if(!path.IsNull && path.TryGetValueAsString(out var pathStr)) { //invalid path values are just ignored in BYOND
savefile.CurrentPath = pathStr;
}

int result = ImportTextInternal(savefile, source, bundle);

savefile.CurrentPath = oldPath; //restore current directory after a query
return new DreamValue(result);
}

private static int ImportTextInternal(DreamObjectSavefile savefile, DreamValue source, NativeProc.Bundle bundle) {
if(source.IsNull) {
return 0;
}

if(source.TryGetValueAsString(out var sourceStr)) {
return ImportTextParseLine(sourceStr, savefile, bundle);
} else if(source.TryGetValueAsDreamResource(out var fileResource)) {
if(fileResource.ResourceData == null) {
return 0;
}

string fileString = fileResource.ReadAsString()!;
string[] fileStrings = fileString.Split("\n");
bool returnedFalse = false;
foreach(string fileLine in fileStrings) {
int returnValue = ImportTextParseLine(fileLine, savefile, bundle);
if(returnValue == 0) {
returnedFalse = true;
}
}
if(returnedFalse) {
return 0;
} else {
return 1;
}
}

return 0;
}

private static int ImportTextParseLine(string lineToParse, DreamObjectSavefile savefile, NativeProc.Bundle bundle) {
// ; is used to split individual savefile entries in ImportText()
string[] savefileImports = lineToParse.Split(";");
foreach(string savefileEntry in savefileImports) {
// We cannot split by " = " or similar because we cannot guarantee there will be only one = or that there is consistent spacing
string[] spaceSplit = savefileEntry.Split(" ");
bool foundEquals = false;
bool firstEntryIsEquals = false;
string foundValue = "";
string foundIndex = "";
for(int i = 0; i < spaceSplit.Length; i++) {
if(i == 0) {
if(spaceSplit[i] == "=") {
firstEntryIsEquals = true;
}
foundIndex = spaceSplit[i];
continue;
}
string individualWord = spaceSplit[i];
if(individualWord == "=") {
// "entry = ="
if(foundEquals) {
bundle.DreamManager.WriteWorldLog($"failed to parse savefile text at line 0 (reading 'end of file'): expecting ;");
}
foundEquals = true;
continue;
}
// "entry = a b"
if(spaceSplit[i - 1] != "=") {
bundle.DreamManager.WriteWorldLog($"failed to parse savefile text at line 0 (reading 'end of file'): expecting ;");
} else {
foundValue = individualWord;
}
}

// "entry ="
if(foundEquals && (foundValue == "")) {
bundle.DreamManager.WriteWorldLog($"failed to parse savefile text at line 0 (reading 'end of file'): expecting ;");
}

if(!foundEquals && (foundValue == "")) {
// GetSavefileValue creates the index in the savefile if it doesn't exist while not needing a value
savefile.GetSavefileValue(foundIndex);
}

if((foundValue != "") && (foundIndex != "")) {
bool canConvert = float.TryParse(foundValue, out float possibleNumber);
// DM implicitly converts values to numbers if possible
if (canConvert) {
DreamValue value = new(possibleNumber);
savefile.SetSavefileValue(!firstEntryIsEquals ? foundIndex : null, value);
} else {
DreamValue value = new(foundValue);
savefile.SetSavefileValue(!firstEntryIsEquals ? foundIndex : null, value);
}
}

}
return 1;
}

[DreamProc("Flush")]
public static DreamValue NativeProc_Flush(NativeProc.Bundle bundle, DreamObject? src, DreamObject? usr) {
var savefile = (DreamObjectSavefile)src!;
Expand Down
Loading