Skip to content

Commit

Permalink
dir
Browse files Browse the repository at this point in the history
  • Loading branch information
amylizzle committed Feb 2, 2024
1 parent e77bcd2 commit b4193f5
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 4 deletions.
25 changes: 25 additions & 0 deletions Content.Tests/DMProject/Tests/Savefile/BasicReadAndWrite.dm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
S["DEF"] >> V
ASSERT(S["DEF"] == 10)
ASSERT(V == 10)
S["notakey"] >> V
ASSERT(V == null)

// test path
S["pathymcpathface"] << /datum/foobar
Expand All @@ -33,6 +35,7 @@
// Shouldn't evaluate CRASH
S2?["ABC"] << CRASH("rhs should not evaluate due to null-conditional")

// Test EOF
S.cd = "DEF"
var/out
ASSERT(S.eof == 0)
Expand All @@ -43,4 +46,26 @@
S.cd = "/"
ASSERT(S["DEF"] == null)

//Test dir
S.cd = "/"
var/dir = S.dir
ASSERT(dir ~= list("ABC", "DEF", "pathymcpathface", "pie", "pie2"))

//test add
dir += "test/beep"
ASSERT(dir ~= list("ABC", "DEF", "pathymcpathface", "pie", "pie2", "test"))
ASSERT(S["test"] == null)
S.cd = "test"
ASSERT(dir ~= list("beep"))

//test del
S.cd = "/"
dir -= "test"
ASSERT(dir ~= list("ABC", "DEF", "pathymcpathface", "pie", "pie2"))

//test rename and null
dir[1] = "CBA"
ASSERT(dir ~= list("CBA", "DEF", "pathymcpathface", "pie", "pie2"))
ASSERT(S["CBA"] == null)

fdel("savefile.sav")
58 changes: 58 additions & 0 deletions OpenDreamRuntime/Objects/Types/DreamList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,3 +1292,61 @@ public override int GetLength() {
return _state.ArgumentCount;
}
}

// Savefile Dir List - always sync'd with Savefiles currentDir. Only stores keys.
sealed class SavefileDirList : DreamList {
private readonly DreamObjectSavefile _save;

public SavefileDirList(DreamObjectDefinition listDef, DreamObjectSavefile backedSaveFile) : base(listDef, 0) {
_save = backedSaveFile;
}

public override DreamValue GetValue(DreamValue key) {
if (!key.TryGetValueAsInteger(out var index))
throw new Exception($"Invalid index on savefile dir list: {key}");
if (index < 1 || index > _save.CurrentDir.Count)
throw new Exception($"Out of bounds index on savefile dir list: {index}");
return new DreamValue(_save.CurrentDir.Keys.ToArray()[index - 1]);
}

public override List<DreamValue> GetValues() {
List<DreamValue> values = new(_save.CurrentDir.Count);

foreach (string value in _save.CurrentDir.Keys) {
values.Add(new DreamValue(value));
}
return values;
}

public override void SetValue(DreamValue key, DreamValue value, bool allowGrowth = false) {
if (!key.TryGetValueAsInteger(out var index))
throw new Exception($"Invalid index on savefile dir list: {key}");
if (!value.TryGetValueAsString(out var valueStr))
throw new Exception($"Invalid value on savefile dir name: {value}");
if (index < 1 || index > _save.CurrentDir.Count)
throw new Exception($"Out of bounds index on savefile dir list: {index}");

_save.RenameAndNullSavefileValue(_save.CurrentDir.Keys.ToArray()[index - 1], valueStr);
}

public override void AddValue(DreamValue value) {
if (!value.TryGetValueAsString(out var valueStr))
throw new Exception($"Invalid value on savefile dir name: {value}");
_save.AddSavefileDir(valueStr);

}

public override void RemoveValue(DreamValue value) {
if (!value.TryGetValueAsString(out var valueStr))
throw new Exception($"Invalid value on savefile dir name: {value}");
_save.RemoveSavefileValue(valueStr);
}

public override void Cut(int start = 1, int end = 0) {
throw new Exception("Cannot cut savefile dir list"); //BYOND actually throws undefined proc for this
}

public override int GetLength() {
return _save.CurrentDir.Count;
}
}
28 changes: 25 additions & 3 deletions OpenDreamRuntime/Objects/Types/DreamObjectSavefile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ protected override bool TryGetVar(string varName, out DreamValue value) {
value = new DreamValue(Resource.ResourcePath ?? "[no path]");
return true;
case "dir":
DreamList dirList = ObjectTree.CreateList([.. CurrentDir.Keys]);
//TODO: dirList.Add(), dirList.Remove() should affect the directories in a savefile - needs a custom list type, save file backed (ew)
value = new DreamValue(dirList);
value = new DreamValue(new SavefileDirList(ObjectTree.List.ObjectDefinition, this));
return true;
default:
return base.TryGetVar(varName, out value);
Expand Down Expand Up @@ -282,6 +280,29 @@ public DreamValue GetSavefileValue(string? index) {
return DeserializeJsonValue(SeekTo(index));
}

public void RemoveSavefileValue(string index){
if(CurrentDir.TryGetValue(index, out var value)) {
CurrentDir.Remove(index);
_savefilesToFlush.Add(this);
}
}

public void RenameAndNullSavefileValue(string index, string newIndex){
if(CurrentDir.TryGetValue(index, out var value)) {
CurrentDir.Remove(index);
SFDreamDir newDir = new SFDreamDir();
foreach(var key in value.Keys) {
newDir[key] = value[key];
}
CurrentDir[newIndex] = newDir;
_savefilesToFlush.Add(this);
}
}

public void AddSavefileDir(string index){
SeekTo(index, true);
}

public void SetSavefileValue(string? index, DreamValue value) {
if (index == null) {
SFDreamJsonValue newCurrentDir = SerializeDreamValue(value);
Expand Down Expand Up @@ -529,6 +550,7 @@ public SFDreamJsonValue this[string key] {
[JsonIgnore]
public int Count => nodes.Count;
public void Clear() => nodes.Clear();
public bool Remove(string key) => nodes.Remove(key);

}

Expand Down
2 changes: 1 addition & 1 deletion OpenDreamRuntime/Procs/DMOpcodeHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2094,7 +2094,7 @@ public static ProcStatus Input(DMProcState state) {
}
} else if (state.GetReferenceValue(leftRef).TryGetValueAsDreamObject<DreamObjectSavefile>(out var SF)) {
// Savefiles get some special treatment.
// "savefile >> B" is the same as "B = savefile[current_dir++]"
// "savefile >> B" is the same as "B = savefile[current_dir]"
state.AssignReference(rightRef, SF.OperatorInput());
return ProcStatus.Continue;
}
Expand Down

0 comments on commit b4193f5

Please sign in to comment.